Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1 | //===---- ScheduleDAG.cpp - Implement the ScheduleDAG class ---------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by James M. Laskey and is distributed under the |
| 6 | // University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This implements a simple two pass scheduler. The first pass attempts to push |
| 11 | // backward any lengthy instructions and critical paths. The second pass packs |
| 12 | // instructions into semi-optimal time slots. |
| 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
| 16 | #define DEBUG_TYPE "pre-RA-sched" |
| 17 | #include "llvm/Type.h" |
| 18 | #include "llvm/CodeGen/ScheduleDAG.h" |
| 19 | #include "llvm/CodeGen/MachineConstantPool.h" |
| 20 | #include "llvm/CodeGen/MachineFunction.h" |
| 21 | #include "llvm/CodeGen/SSARegMap.h" |
| 22 | #include "llvm/Target/TargetData.h" |
| 23 | #include "llvm/Target/TargetMachine.h" |
| 24 | #include "llvm/Target/TargetInstrInfo.h" |
| 25 | #include "llvm/Target/TargetLowering.h" |
| 26 | #include "llvm/Support/Debug.h" |
| 27 | #include "llvm/Support/MathExtras.h" |
| 28 | using namespace llvm; |
| 29 | |
Evan Cheng | 93f143e | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 30 | |
| 31 | /// getPhysicalRegisterRegClass - Returns the Register Class of a physical |
| 32 | /// register. |
| 33 | static const TargetRegisterClass *getPhysicalRegisterRegClass( |
| 34 | const MRegisterInfo *MRI, |
| 35 | MVT::ValueType VT, |
| 36 | unsigned reg) { |
| 37 | assert(MRegisterInfo::isPhysicalRegister(reg) && |
| 38 | "reg must be a physical register"); |
| 39 | // Pick the register class of the right type that contains this physreg. |
| 40 | for (MRegisterInfo::regclass_iterator I = MRI->regclass_begin(), |
| 41 | E = MRI->regclass_end(); I != E; ++I) |
| 42 | if ((*I)->hasType(VT) && (*I)->contains(reg)) |
| 43 | return *I; |
| 44 | assert(false && "Couldn't find the register class"); |
| 45 | return 0; |
| 46 | } |
| 47 | |
| 48 | |
| 49 | /// CheckForPhysRegDependency - Check if the dependency between def and use of |
| 50 | /// a specified operand is a physical register dependency. If so, returns the |
| 51 | /// register and the cost of copying the register. |
| 52 | static void CheckForPhysRegDependency(SDNode *Def, SDNode *Use, unsigned Op, |
| 53 | const MRegisterInfo *MRI, |
| 54 | const TargetInstrInfo *TII, |
| 55 | unsigned &PhysReg, int &Cost) { |
| 56 | if (Op != 2 || Use->getOpcode() != ISD::CopyToReg) |
| 57 | return; |
| 58 | |
| 59 | unsigned Reg = cast<RegisterSDNode>(Use->getOperand(1))->getReg(); |
| 60 | if (MRegisterInfo::isVirtualRegister(Reg)) |
| 61 | return; |
| 62 | |
| 63 | unsigned ResNo = Use->getOperand(2).ResNo; |
| 64 | if (Def->isTargetOpcode()) { |
| 65 | const TargetInstrDescriptor &II = TII->get(Def->getTargetOpcode()); |
| 66 | if (ResNo >= II.numDefs && |
| 67 | II.ImplicitDefs[ResNo - II.numDefs] == Reg) { |
| 68 | PhysReg = Reg; |
| 69 | const TargetRegisterClass *RC = |
| 70 | getPhysicalRegisterRegClass(MRI, Def->getValueType(ResNo), Reg); |
| 71 | Cost = RC->getCopyCost(); |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | SUnit *ScheduleDAG::Clone(SUnit *Old) { |
| 77 | SUnit *SU = NewSUnit(Old->Node); |
| 78 | for (unsigned i = 0, e = SU->FlaggedNodes.size(); i != e; ++i) |
| 79 | SU->FlaggedNodes.push_back(SU->FlaggedNodes[i]); |
| 80 | SU->InstanceNo = SUnitMap[Old->Node].size(); |
| 81 | SU->Latency = Old->Latency; |
| 82 | SU->isTwoAddress = Old->isTwoAddress; |
| 83 | SU->isCommutable = Old->isCommutable; |
| 84 | SU->hasImplicitDefs = Old->hasImplicitDefs; |
| 85 | SUnitMap[Old->Node].push_back(SU); |
| 86 | return SU; |
| 87 | } |
| 88 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 89 | /// BuildSchedUnits - Build SUnits from the selection dag that we are input. |
| 90 | /// This SUnit graph is similar to the SelectionDAG, but represents flagged |
| 91 | /// together nodes with a single SUnit. |
| 92 | void ScheduleDAG::BuildSchedUnits() { |
| 93 | // Reserve entries in the vector for each of the SUnits we are creating. This |
| 94 | // ensure that reallocation of the vector won't happen, so SUnit*'s won't get |
| 95 | // invalidated. |
| 96 | SUnits.reserve(std::distance(DAG.allnodes_begin(), DAG.allnodes_end())); |
| 97 | |
| 98 | const InstrItineraryData &InstrItins = TM.getInstrItineraryData(); |
| 99 | |
| 100 | for (SelectionDAG::allnodes_iterator NI = DAG.allnodes_begin(), |
| 101 | E = DAG.allnodes_end(); NI != E; ++NI) { |
| 102 | if (isPassiveNode(NI)) // Leaf node, e.g. a TargetImmediate. |
| 103 | continue; |
| 104 | |
| 105 | // If this node has already been processed, stop now. |
Evan Cheng | 93f143e | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 106 | if (SUnitMap[NI].size()) continue; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 107 | |
| 108 | SUnit *NodeSUnit = NewSUnit(NI); |
| 109 | |
| 110 | // See if anything is flagged to this node, if so, add them to flagged |
| 111 | // nodes. Nodes can have at most one flag input and one flag output. Flags |
| 112 | // are required the be the last operand and result of a node. |
| 113 | |
| 114 | // Scan up, adding flagged preds to FlaggedNodes. |
| 115 | SDNode *N = NI; |
| 116 | if (N->getNumOperands() && |
| 117 | N->getOperand(N->getNumOperands()-1).getValueType() == MVT::Flag) { |
| 118 | do { |
| 119 | N = N->getOperand(N->getNumOperands()-1).Val; |
| 120 | NodeSUnit->FlaggedNodes.push_back(N); |
Evan Cheng | 93f143e | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 121 | SUnitMap[N].push_back(NodeSUnit); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 122 | } while (N->getNumOperands() && |
| 123 | N->getOperand(N->getNumOperands()-1).getValueType()== MVT::Flag); |
| 124 | std::reverse(NodeSUnit->FlaggedNodes.begin(), |
| 125 | NodeSUnit->FlaggedNodes.end()); |
| 126 | } |
| 127 | |
| 128 | // Scan down, adding this node and any flagged succs to FlaggedNodes if they |
| 129 | // have a user of the flag operand. |
| 130 | N = NI; |
| 131 | while (N->getValueType(N->getNumValues()-1) == MVT::Flag) { |
| 132 | SDOperand FlagVal(N, N->getNumValues()-1); |
| 133 | |
| 134 | // There are either zero or one users of the Flag result. |
| 135 | bool HasFlagUse = false; |
| 136 | for (SDNode::use_iterator UI = N->use_begin(), E = N->use_end(); |
| 137 | UI != E; ++UI) |
| 138 | if (FlagVal.isOperand(*UI)) { |
| 139 | HasFlagUse = true; |
| 140 | NodeSUnit->FlaggedNodes.push_back(N); |
Evan Cheng | 93f143e | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 141 | SUnitMap[N].push_back(NodeSUnit); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 142 | N = *UI; |
| 143 | break; |
| 144 | } |
| 145 | if (!HasFlagUse) break; |
| 146 | } |
| 147 | |
| 148 | // Now all flagged nodes are in FlaggedNodes and N is the bottom-most node. |
| 149 | // Update the SUnit |
| 150 | NodeSUnit->Node = N; |
Evan Cheng | 93f143e | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 151 | SUnitMap[N].push_back(NodeSUnit); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 152 | |
| 153 | // Compute the latency for the node. We use the sum of the latencies for |
| 154 | // all nodes flagged together into this SUnit. |
| 155 | if (InstrItins.isEmpty()) { |
| 156 | // No latency information. |
| 157 | NodeSUnit->Latency = 1; |
| 158 | } else { |
| 159 | NodeSUnit->Latency = 0; |
| 160 | if (N->isTargetOpcode()) { |
| 161 | unsigned SchedClass = TII->getSchedClass(N->getTargetOpcode()); |
| 162 | InstrStage *S = InstrItins.begin(SchedClass); |
| 163 | InstrStage *E = InstrItins.end(SchedClass); |
| 164 | for (; S != E; ++S) |
| 165 | NodeSUnit->Latency += S->Cycles; |
| 166 | } |
| 167 | for (unsigned i = 0, e = NodeSUnit->FlaggedNodes.size(); i != e; ++i) { |
| 168 | SDNode *FNode = NodeSUnit->FlaggedNodes[i]; |
| 169 | if (FNode->isTargetOpcode()) { |
| 170 | unsigned SchedClass = TII->getSchedClass(FNode->getTargetOpcode()); |
| 171 | InstrStage *S = InstrItins.begin(SchedClass); |
| 172 | InstrStage *E = InstrItins.end(SchedClass); |
| 173 | for (; S != E; ++S) |
| 174 | NodeSUnit->Latency += S->Cycles; |
| 175 | } |
| 176 | } |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | // Pass 2: add the preds, succs, etc. |
| 181 | for (unsigned su = 0, e = SUnits.size(); su != e; ++su) { |
| 182 | SUnit *SU = &SUnits[su]; |
| 183 | SDNode *MainNode = SU->Node; |
| 184 | |
| 185 | if (MainNode->isTargetOpcode()) { |
| 186 | unsigned Opc = MainNode->getTargetOpcode(); |
Evan Cheng | 93f143e | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 187 | const TargetInstrDescriptor &TID = TII->get(Opc); |
| 188 | if (TID.ImplicitDefs) |
| 189 | SU->hasImplicitDefs = true; |
| 190 | for (unsigned i = 0; i != TID.numOperands; ++i) { |
| 191 | if (TID.getOperandConstraint(i, TOI::TIED_TO) != -1) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 192 | SU->isTwoAddress = true; |
| 193 | break; |
| 194 | } |
| 195 | } |
Evan Cheng | 93f143e | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 196 | if (TID.Flags & M_COMMUTABLE) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 197 | SU->isCommutable = true; |
| 198 | } |
| 199 | |
| 200 | // Find all predecessors and successors of the group. |
| 201 | // Temporarily add N to make code simpler. |
| 202 | SU->FlaggedNodes.push_back(MainNode); |
| 203 | |
| 204 | for (unsigned n = 0, e = SU->FlaggedNodes.size(); n != e; ++n) { |
| 205 | SDNode *N = SU->FlaggedNodes[n]; |
Evan Cheng | 93f143e | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 206 | if (N->isTargetOpcode() && TII->getImplicitDefs(N->getTargetOpcode())) |
| 207 | SU->hasImplicitDefs = true; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 208 | |
| 209 | for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) { |
| 210 | SDNode *OpN = N->getOperand(i).Val; |
| 211 | if (isPassiveNode(OpN)) continue; // Not scheduled. |
Evan Cheng | 93f143e | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 212 | SUnit *OpSU = SUnitMap[OpN].front(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 213 | assert(OpSU && "Node has no SUnit!"); |
| 214 | if (OpSU == SU) continue; // In the same group. |
| 215 | |
| 216 | MVT::ValueType OpVT = N->getOperand(i).getValueType(); |
| 217 | assert(OpVT != MVT::Flag && "Flagged nodes should be in same sunit!"); |
| 218 | bool isChain = OpVT == MVT::Other; |
Evan Cheng | 93f143e | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 219 | |
| 220 | unsigned PhysReg = 0; |
| 221 | int Cost = 1; |
| 222 | // Determine if this is a physical register dependency. |
| 223 | CheckForPhysRegDependency(OpN, N, i, MRI, TII, PhysReg, Cost); |
| 224 | SU->addPred(OpSU, isChain, false, PhysReg, Cost); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 225 | } |
| 226 | } |
| 227 | |
| 228 | // Remove MainNode from FlaggedNodes again. |
| 229 | SU->FlaggedNodes.pop_back(); |
| 230 | } |
| 231 | |
| 232 | return; |
| 233 | } |
| 234 | |
| 235 | void ScheduleDAG::CalculateDepths() { |
| 236 | std::vector<std::pair<SUnit*, unsigned> > WorkList; |
| 237 | for (unsigned i = 0, e = SUnits.size(); i != e; ++i) |
Evan Cheng | 9668960 | 2007-09-12 23:45:46 +0000 | [diff] [blame] | 238 | if (SUnits[i].Preds.size() == 0) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 239 | WorkList.push_back(std::make_pair(&SUnits[i], 0U)); |
| 240 | |
| 241 | while (!WorkList.empty()) { |
| 242 | SUnit *SU = WorkList.back().first; |
| 243 | unsigned Depth = WorkList.back().second; |
| 244 | WorkList.pop_back(); |
| 245 | if (SU->Depth == 0 || Depth > SU->Depth) { |
| 246 | SU->Depth = Depth; |
| 247 | for (SUnit::succ_iterator I = SU->Succs.begin(), E = SU->Succs.end(); |
| 248 | I != E; ++I) |
Evan Cheng | e795947 | 2007-09-19 01:38:40 +0000 | [diff] [blame] | 249 | WorkList.push_back(std::make_pair(I->Dep, Depth+1)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 250 | } |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | void ScheduleDAG::CalculateHeights() { |
| 255 | std::vector<std::pair<SUnit*, unsigned> > WorkList; |
Evan Cheng | 93f143e | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 256 | SUnit *Root = SUnitMap[DAG.getRoot().Val].front(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 257 | WorkList.push_back(std::make_pair(Root, 0U)); |
| 258 | |
| 259 | while (!WorkList.empty()) { |
| 260 | SUnit *SU = WorkList.back().first; |
| 261 | unsigned Height = WorkList.back().second; |
| 262 | WorkList.pop_back(); |
| 263 | if (SU->Height == 0 || Height > SU->Height) { |
| 264 | SU->Height = Height; |
| 265 | for (SUnit::pred_iterator I = SU->Preds.begin(), E = SU->Preds.end(); |
| 266 | I != E; ++I) |
Evan Cheng | e795947 | 2007-09-19 01:38:40 +0000 | [diff] [blame] | 267 | WorkList.push_back(std::make_pair(I->Dep, Height+1)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 268 | } |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | /// CountResults - The results of target nodes have register or immediate |
| 273 | /// operands first, then an optional chain, and optional flag operands (which do |
| 274 | /// not go into the machine instrs.) |
| 275 | unsigned ScheduleDAG::CountResults(SDNode *Node) { |
| 276 | unsigned N = Node->getNumValues(); |
| 277 | while (N && Node->getValueType(N - 1) == MVT::Flag) |
| 278 | --N; |
| 279 | if (N && Node->getValueType(N - 1) == MVT::Other) |
| 280 | --N; // Skip over chain result. |
| 281 | return N; |
| 282 | } |
| 283 | |
| 284 | /// CountOperands The inputs to target nodes have any actual inputs first, |
| 285 | /// followed by an optional chain operand, then flag operands. Compute the |
| 286 | /// number of actual operands that will go into the machine instr. |
| 287 | unsigned ScheduleDAG::CountOperands(SDNode *Node) { |
| 288 | unsigned N = Node->getNumOperands(); |
| 289 | while (N && Node->getOperand(N - 1).getValueType() == MVT::Flag) |
| 290 | --N; |
| 291 | if (N && Node->getOperand(N - 1).getValueType() == MVT::Other) |
| 292 | --N; // Ignore chain if it exists. |
| 293 | return N; |
| 294 | } |
| 295 | |
| 296 | static const TargetRegisterClass *getInstrOperandRegClass( |
| 297 | const MRegisterInfo *MRI, |
| 298 | const TargetInstrInfo *TII, |
| 299 | const TargetInstrDescriptor *II, |
| 300 | unsigned Op) { |
| 301 | if (Op >= II->numOperands) { |
| 302 | assert((II->Flags & M_VARIABLE_OPS)&& "Invalid operand # of instruction"); |
| 303 | return NULL; |
| 304 | } |
| 305 | const TargetOperandInfo &toi = II->OpInfo[Op]; |
| 306 | return (toi.Flags & M_LOOK_UP_PTR_REG_CLASS) |
| 307 | ? TII->getPointerRegClass() : MRI->getRegClass(toi.RegClass); |
| 308 | } |
| 309 | |
Evan Cheng | 93f143e | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 310 | void ScheduleDAG::EmitCopyFromReg(SDNode *Node, unsigned ResNo, |
| 311 | unsigned InstanceNo, unsigned SrcReg, |
Evan Cheng | 2663978 | 2007-08-02 00:28:15 +0000 | [diff] [blame] | 312 | DenseMap<SDOperand, unsigned> &VRBaseMap) { |
| 313 | unsigned VRBase = 0; |
| 314 | if (MRegisterInfo::isVirtualRegister(SrcReg)) { |
| 315 | // Just use the input register directly! |
Evan Cheng | 93f143e | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 316 | if (InstanceNo > 0) |
| 317 | VRBaseMap.erase(SDOperand(Node, ResNo)); |
Evan Cheng | 2663978 | 2007-08-02 00:28:15 +0000 | [diff] [blame] | 318 | bool isNew = VRBaseMap.insert(std::make_pair(SDOperand(Node,ResNo),SrcReg)); |
| 319 | assert(isNew && "Node emitted out of order - early"); |
| 320 | return; |
| 321 | } |
| 322 | |
| 323 | // If the node is only used by a CopyToReg and the dest reg is a vreg, use |
| 324 | // the CopyToReg'd destination register instead of creating a new vreg. |
Evan Cheng | 93f143e | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 325 | bool MatchReg = true; |
Evan Cheng | 2663978 | 2007-08-02 00:28:15 +0000 | [diff] [blame] | 326 | for (SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end(); |
| 327 | UI != E; ++UI) { |
| 328 | SDNode *Use = *UI; |
Evan Cheng | 93f143e | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 329 | bool Match = true; |
Evan Cheng | 2663978 | 2007-08-02 00:28:15 +0000 | [diff] [blame] | 330 | if (Use->getOpcode() == ISD::CopyToReg && |
| 331 | Use->getOperand(2).Val == Node && |
| 332 | Use->getOperand(2).ResNo == ResNo) { |
| 333 | unsigned DestReg = cast<RegisterSDNode>(Use->getOperand(1))->getReg(); |
| 334 | if (MRegisterInfo::isVirtualRegister(DestReg)) { |
| 335 | VRBase = DestReg; |
Evan Cheng | 93f143e | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 336 | Match = false; |
| 337 | } else if (DestReg != SrcReg) |
| 338 | Match = false; |
| 339 | } else { |
| 340 | for (unsigned i = 0, e = Use->getNumOperands(); i != e; ++i) { |
| 341 | SDOperand Op = Use->getOperand(i); |
| 342 | if (Op.Val != Node) |
| 343 | continue; |
| 344 | MVT::ValueType VT = Node->getValueType(Op.ResNo); |
| 345 | if (VT != MVT::Other && VT != MVT::Flag) |
| 346 | Match = false; |
Evan Cheng | 2663978 | 2007-08-02 00:28:15 +0000 | [diff] [blame] | 347 | } |
| 348 | } |
Evan Cheng | 93f143e | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 349 | MatchReg &= Match; |
| 350 | if (VRBase) |
| 351 | break; |
Evan Cheng | 2663978 | 2007-08-02 00:28:15 +0000 | [diff] [blame] | 352 | } |
| 353 | |
Evan Cheng | 2663978 | 2007-08-02 00:28:15 +0000 | [diff] [blame] | 354 | const TargetRegisterClass *TRC = 0; |
Evan Cheng | 93f143e | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 355 | // Figure out the register class to create for the destreg. |
| 356 | if (VRBase) |
Evan Cheng | 2663978 | 2007-08-02 00:28:15 +0000 | [diff] [blame] | 357 | TRC = RegMap->getRegClass(VRBase); |
Evan Cheng | 93f143e | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 358 | else |
Evan Cheng | 2663978 | 2007-08-02 00:28:15 +0000 | [diff] [blame] | 359 | TRC = getPhysicalRegisterRegClass(MRI, Node->getValueType(ResNo), SrcReg); |
Evan Cheng | 93f143e | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 360 | |
| 361 | // If all uses are reading from the src physical register and copying the |
| 362 | // register is either impossible or very expensive, then don't create a copy. |
| 363 | if (MatchReg && TRC->getCopyCost() < 0) { |
| 364 | VRBase = SrcReg; |
| 365 | } else { |
Evan Cheng | 2663978 | 2007-08-02 00:28:15 +0000 | [diff] [blame] | 366 | // Create the reg, emit the copy. |
| 367 | VRBase = RegMap->createVirtualRegister(TRC); |
Evan Cheng | 93f143e | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 368 | MRI->copyRegToReg(*BB, BB->end(), VRBase, SrcReg, TRC); |
Evan Cheng | 2663978 | 2007-08-02 00:28:15 +0000 | [diff] [blame] | 369 | } |
Evan Cheng | 2663978 | 2007-08-02 00:28:15 +0000 | [diff] [blame] | 370 | |
Evan Cheng | 93f143e | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 371 | if (InstanceNo > 0) |
| 372 | VRBaseMap.erase(SDOperand(Node, ResNo)); |
Evan Cheng | 2663978 | 2007-08-02 00:28:15 +0000 | [diff] [blame] | 373 | bool isNew = VRBaseMap.insert(std::make_pair(SDOperand(Node,ResNo), VRBase)); |
| 374 | assert(isNew && "Node emitted out of order - early"); |
| 375 | } |
| 376 | |
| 377 | void ScheduleDAG::CreateVirtualRegisters(SDNode *Node, |
| 378 | MachineInstr *MI, |
| 379 | const TargetInstrDescriptor &II, |
| 380 | DenseMap<SDOperand, unsigned> &VRBaseMap) { |
| 381 | for (unsigned i = 0; i < II.numDefs; ++i) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 382 | // If the specific node value is only used by a CopyToReg and the dest reg |
| 383 | // is a vreg, use the CopyToReg'd destination register instead of creating |
| 384 | // a new vreg. |
| 385 | unsigned VRBase = 0; |
| 386 | for (SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end(); |
| 387 | UI != E; ++UI) { |
| 388 | SDNode *Use = *UI; |
| 389 | if (Use->getOpcode() == ISD::CopyToReg && |
| 390 | Use->getOperand(2).Val == Node && |
| 391 | Use->getOperand(2).ResNo == i) { |
| 392 | unsigned Reg = cast<RegisterSDNode>(Use->getOperand(1))->getReg(); |
| 393 | if (MRegisterInfo::isVirtualRegister(Reg)) { |
| 394 | VRBase = Reg; |
| 395 | MI->addRegOperand(Reg, true); |
| 396 | break; |
| 397 | } |
| 398 | } |
| 399 | } |
| 400 | |
Evan Cheng | 2663978 | 2007-08-02 00:28:15 +0000 | [diff] [blame] | 401 | // Create the result registers for this node and add the result regs to |
| 402 | // the machine instruction. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 403 | if (VRBase == 0) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 404 | const TargetRegisterClass *RC = getInstrOperandRegClass(MRI, TII, &II, i); |
| 405 | assert(RC && "Isn't a register operand!"); |
| 406 | VRBase = RegMap->createVirtualRegister(RC); |
| 407 | MI->addRegOperand(VRBase, true); |
| 408 | } |
| 409 | |
| 410 | bool isNew = VRBaseMap.insert(std::make_pair(SDOperand(Node,i), VRBase)); |
| 411 | assert(isNew && "Node emitted out of order - early"); |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | /// getVR - Return the virtual register corresponding to the specified result |
| 416 | /// of the specified node. |
| 417 | static unsigned getVR(SDOperand Op, DenseMap<SDOperand, unsigned> &VRBaseMap) { |
| 418 | DenseMap<SDOperand, unsigned>::iterator I = VRBaseMap.find(Op); |
| 419 | assert(I != VRBaseMap.end() && "Node emitted out of order - late"); |
| 420 | return I->second; |
| 421 | } |
| 422 | |
| 423 | |
| 424 | /// AddOperand - Add the specified operand to the specified machine instr. II |
| 425 | /// specifies the instruction information for the node, and IIOpNum is the |
| 426 | /// operand number (in the II) that we are adding. IIOpNum and II are used for |
| 427 | /// assertions only. |
| 428 | void ScheduleDAG::AddOperand(MachineInstr *MI, SDOperand Op, |
| 429 | unsigned IIOpNum, |
| 430 | const TargetInstrDescriptor *II, |
| 431 | DenseMap<SDOperand, unsigned> &VRBaseMap) { |
| 432 | if (Op.isTargetOpcode()) { |
| 433 | // Note that this case is redundant with the final else block, but we |
| 434 | // include it because it is the most common and it makes the logic |
| 435 | // simpler here. |
| 436 | assert(Op.getValueType() != MVT::Other && |
| 437 | Op.getValueType() != MVT::Flag && |
| 438 | "Chain and flag operands should occur at end of operand list!"); |
| 439 | |
| 440 | // Get/emit the operand. |
| 441 | unsigned VReg = getVR(Op, VRBaseMap); |
| 442 | const TargetInstrDescriptor *TID = MI->getInstrDescriptor(); |
| 443 | bool isOptDef = (IIOpNum < TID->numOperands) |
| 444 | ? (TID->OpInfo[IIOpNum].Flags & M_OPTIONAL_DEF_OPERAND) : false; |
| 445 | MI->addRegOperand(VReg, isOptDef); |
| 446 | |
| 447 | // Verify that it is right. |
| 448 | assert(MRegisterInfo::isVirtualRegister(VReg) && "Not a vreg?"); |
| 449 | if (II) { |
| 450 | const TargetRegisterClass *RC = |
| 451 | getInstrOperandRegClass(MRI, TII, II, IIOpNum); |
| 452 | assert(RC && "Don't have operand info for this instruction!"); |
| 453 | const TargetRegisterClass *VRC = RegMap->getRegClass(VReg); |
| 454 | if (VRC != RC) { |
| 455 | cerr << "Register class of operand and regclass of use don't agree!\n"; |
| 456 | #ifndef NDEBUG |
| 457 | cerr << "Operand = " << IIOpNum << "\n"; |
| 458 | cerr << "Op->Val = "; Op.Val->dump(&DAG); cerr << "\n"; |
| 459 | cerr << "MI = "; MI->print(cerr); |
| 460 | cerr << "VReg = " << VReg << "\n"; |
| 461 | cerr << "VReg RegClass size = " << VRC->getSize() |
| 462 | << ", align = " << VRC->getAlignment() << "\n"; |
| 463 | cerr << "Expected RegClass size = " << RC->getSize() |
| 464 | << ", align = " << RC->getAlignment() << "\n"; |
| 465 | #endif |
| 466 | cerr << "Fatal error, aborting.\n"; |
| 467 | abort(); |
| 468 | } |
| 469 | } |
| 470 | } else if (ConstantSDNode *C = |
| 471 | dyn_cast<ConstantSDNode>(Op)) { |
| 472 | MI->addImmOperand(C->getValue()); |
| 473 | } else if (RegisterSDNode *R = |
| 474 | dyn_cast<RegisterSDNode>(Op)) { |
| 475 | MI->addRegOperand(R->getReg(), false); |
| 476 | } else if (GlobalAddressSDNode *TGA = |
| 477 | dyn_cast<GlobalAddressSDNode>(Op)) { |
| 478 | MI->addGlobalAddressOperand(TGA->getGlobal(), TGA->getOffset()); |
| 479 | } else if (BasicBlockSDNode *BB = |
| 480 | dyn_cast<BasicBlockSDNode>(Op)) { |
| 481 | MI->addMachineBasicBlockOperand(BB->getBasicBlock()); |
| 482 | } else if (FrameIndexSDNode *FI = |
| 483 | dyn_cast<FrameIndexSDNode>(Op)) { |
| 484 | MI->addFrameIndexOperand(FI->getIndex()); |
| 485 | } else if (JumpTableSDNode *JT = |
| 486 | dyn_cast<JumpTableSDNode>(Op)) { |
| 487 | MI->addJumpTableIndexOperand(JT->getIndex()); |
| 488 | } else if (ConstantPoolSDNode *CP = |
| 489 | dyn_cast<ConstantPoolSDNode>(Op)) { |
| 490 | int Offset = CP->getOffset(); |
| 491 | unsigned Align = CP->getAlignment(); |
| 492 | const Type *Type = CP->getType(); |
| 493 | // MachineConstantPool wants an explicit alignment. |
| 494 | if (Align == 0) { |
| 495 | Align = TM.getTargetData()->getPreferredTypeAlignmentShift(Type); |
| 496 | if (Align == 0) { |
| 497 | // Alignment of vector types. FIXME! |
| 498 | Align = TM.getTargetData()->getTypeSize(Type); |
| 499 | Align = Log2_64(Align); |
| 500 | } |
| 501 | } |
| 502 | |
| 503 | unsigned Idx; |
| 504 | if (CP->isMachineConstantPoolEntry()) |
| 505 | Idx = ConstPool->getConstantPoolIndex(CP->getMachineCPVal(), Align); |
| 506 | else |
| 507 | Idx = ConstPool->getConstantPoolIndex(CP->getConstVal(), Align); |
| 508 | MI->addConstantPoolIndexOperand(Idx, Offset); |
| 509 | } else if (ExternalSymbolSDNode *ES = |
| 510 | dyn_cast<ExternalSymbolSDNode>(Op)) { |
| 511 | MI->addExternalSymbolOperand(ES->getSymbol()); |
| 512 | } else { |
| 513 | assert(Op.getValueType() != MVT::Other && |
| 514 | Op.getValueType() != MVT::Flag && |
| 515 | "Chain and flag operands should occur at end of operand list!"); |
| 516 | unsigned VReg = getVR(Op, VRBaseMap); |
| 517 | MI->addRegOperand(VReg, false); |
| 518 | |
| 519 | // Verify that it is right. |
| 520 | assert(MRegisterInfo::isVirtualRegister(VReg) && "Not a vreg?"); |
| 521 | if (II) { |
| 522 | const TargetRegisterClass *RC = |
| 523 | getInstrOperandRegClass(MRI, TII, II, IIOpNum); |
| 524 | assert(RC && "Don't have operand info for this instruction!"); |
| 525 | assert(RegMap->getRegClass(VReg) == RC && |
| 526 | "Register class of operand and regclass of use don't agree!"); |
| 527 | } |
| 528 | } |
| 529 | |
| 530 | } |
| 531 | |
Christopher Lamb | e95328d | 2007-07-26 08:12:07 +0000 | [diff] [blame] | 532 | // Returns the Register Class of a subregister |
| 533 | static const TargetRegisterClass *getSubRegisterRegClass( |
| 534 | const TargetRegisterClass *TRC, |
| 535 | unsigned SubIdx) { |
| 536 | // Pick the register class of the subregister |
| 537 | MRegisterInfo::regclass_iterator I = TRC->subregclasses_begin() + SubIdx-1; |
| 538 | assert(I < TRC->subregclasses_end() && |
| 539 | "Invalid subregister index for register class"); |
| 540 | return *I; |
| 541 | } |
| 542 | |
| 543 | static const TargetRegisterClass *getSuperregRegisterClass( |
| 544 | const TargetRegisterClass *TRC, |
| 545 | unsigned SubIdx, |
| 546 | MVT::ValueType VT) { |
| 547 | // Pick the register class of the superegister for this type |
| 548 | for (MRegisterInfo::regclass_iterator I = TRC->superregclasses_begin(), |
| 549 | E = TRC->superregclasses_end(); I != E; ++I) |
| 550 | if ((*I)->hasType(VT) && getSubRegisterRegClass(*I, SubIdx) == TRC) |
| 551 | return *I; |
| 552 | assert(false && "Couldn't find the register class"); |
| 553 | return 0; |
| 554 | } |
| 555 | |
| 556 | /// EmitSubregNode - Generate machine code for subreg nodes. |
| 557 | /// |
| 558 | void ScheduleDAG::EmitSubregNode(SDNode *Node, |
| 559 | DenseMap<SDOperand, unsigned> &VRBaseMap) { |
| 560 | unsigned VRBase = 0; |
| 561 | unsigned Opc = Node->getTargetOpcode(); |
| 562 | if (Opc == TargetInstrInfo::EXTRACT_SUBREG) { |
| 563 | // If the node is only used by a CopyToReg and the dest reg is a vreg, use |
| 564 | // the CopyToReg'd destination register instead of creating a new vreg. |
| 565 | for (SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end(); |
| 566 | UI != E; ++UI) { |
| 567 | SDNode *Use = *UI; |
| 568 | if (Use->getOpcode() == ISD::CopyToReg && |
| 569 | Use->getOperand(2).Val == Node) { |
| 570 | unsigned DestReg = cast<RegisterSDNode>(Use->getOperand(1))->getReg(); |
| 571 | if (MRegisterInfo::isVirtualRegister(DestReg)) { |
| 572 | VRBase = DestReg; |
| 573 | break; |
| 574 | } |
| 575 | } |
| 576 | } |
| 577 | |
| 578 | unsigned SubIdx = cast<ConstantSDNode>(Node->getOperand(1))->getValue(); |
| 579 | |
| 580 | // TODO: If the node is a use of a CopyFromReg from a physical register |
| 581 | // fold the extract into the copy now |
| 582 | |
| 583 | // TODO: Add tracking info to SSARegMap of which vregs are subregs |
| 584 | // to allow coalescing in the allocator |
| 585 | |
| 586 | // Create the extract_subreg machine instruction. |
| 587 | MachineInstr *MI = |
| 588 | new MachineInstr(BB, TII->get(TargetInstrInfo::EXTRACT_SUBREG)); |
| 589 | |
| 590 | // Figure out the register class to create for the destreg. |
| 591 | unsigned VReg = getVR(Node->getOperand(0), VRBaseMap); |
| 592 | const TargetRegisterClass *TRC = RegMap->getRegClass(VReg); |
| 593 | const TargetRegisterClass *SRC = getSubRegisterRegClass(TRC, SubIdx); |
| 594 | |
| 595 | if (VRBase) { |
| 596 | // Grab the destination register |
| 597 | const TargetRegisterClass *DRC = 0; |
| 598 | DRC = RegMap->getRegClass(VRBase); |
| 599 | assert(SRC == DRC && |
| 600 | "Source subregister and destination must have the same class"); |
| 601 | } else { |
| 602 | // Create the reg |
| 603 | VRBase = RegMap->createVirtualRegister(SRC); |
| 604 | } |
| 605 | |
| 606 | // Add def, source, and subreg index |
| 607 | MI->addRegOperand(VRBase, true); |
| 608 | AddOperand(MI, Node->getOperand(0), 0, 0, VRBaseMap); |
| 609 | MI->addImmOperand(SubIdx); |
| 610 | |
| 611 | } else if (Opc == TargetInstrInfo::INSERT_SUBREG) { |
| 612 | assert((Node->getNumOperands() == 2 || Node->getNumOperands() == 3) && |
| 613 | "Malformed insert_subreg node"); |
| 614 | bool isUndefInput = (Node->getNumOperands() == 2); |
| 615 | unsigned SubReg = 0; |
| 616 | unsigned SubIdx = 0; |
| 617 | |
| 618 | if (isUndefInput) { |
| 619 | SubReg = getVR(Node->getOperand(0), VRBaseMap); |
| 620 | SubIdx = cast<ConstantSDNode>(Node->getOperand(1))->getValue(); |
| 621 | } else { |
| 622 | SubReg = getVR(Node->getOperand(1), VRBaseMap); |
| 623 | SubIdx = cast<ConstantSDNode>(Node->getOperand(2))->getValue(); |
| 624 | } |
| 625 | |
| 626 | // TODO: Add tracking info to SSARegMap of which vregs are subregs |
| 627 | // to allow coalescing in the allocator |
| 628 | |
| 629 | // If the node is only used by a CopyToReg and the dest reg is a vreg, use |
| 630 | // the CopyToReg'd destination register instead of creating a new vreg. |
| 631 | // If the CopyToReg'd destination register is physical, then fold the |
| 632 | // insert into the copy |
| 633 | for (SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end(); |
| 634 | UI != E; ++UI) { |
| 635 | SDNode *Use = *UI; |
| 636 | if (Use->getOpcode() == ISD::CopyToReg && |
| 637 | Use->getOperand(2).Val == Node) { |
| 638 | unsigned DestReg = cast<RegisterSDNode>(Use->getOperand(1))->getReg(); |
| 639 | if (MRegisterInfo::isVirtualRegister(DestReg)) { |
| 640 | VRBase = DestReg; |
| 641 | break; |
| 642 | } |
| 643 | } |
| 644 | } |
| 645 | |
| 646 | // Create the insert_subreg machine instruction. |
| 647 | MachineInstr *MI = |
| 648 | new MachineInstr(BB, TII->get(TargetInstrInfo::INSERT_SUBREG)); |
| 649 | |
| 650 | // Figure out the register class to create for the destreg. |
| 651 | const TargetRegisterClass *TRC = 0; |
| 652 | if (VRBase) { |
| 653 | TRC = RegMap->getRegClass(VRBase); |
| 654 | } else { |
| 655 | TRC = getSuperregRegisterClass(RegMap->getRegClass(SubReg), |
| 656 | SubIdx, |
| 657 | Node->getValueType(0)); |
| 658 | assert(TRC && "Couldn't determine register class for insert_subreg"); |
| 659 | VRBase = RegMap->createVirtualRegister(TRC); // Create the reg |
| 660 | } |
| 661 | |
| 662 | MI->addRegOperand(VRBase, true); |
| 663 | AddOperand(MI, Node->getOperand(0), 0, 0, VRBaseMap); |
| 664 | if (!isUndefInput) |
| 665 | AddOperand(MI, Node->getOperand(1), 0, 0, VRBaseMap); |
| 666 | MI->addImmOperand(SubIdx); |
| 667 | } else |
| 668 | assert(0 && "Node is not a subreg insert or extract"); |
| 669 | |
| 670 | bool isNew = VRBaseMap.insert(std::make_pair(SDOperand(Node,0), VRBase)); |
| 671 | assert(isNew && "Node emitted out of order - early"); |
| 672 | } |
| 673 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 674 | /// EmitNode - Generate machine code for an node and needed dependencies. |
| 675 | /// |
Evan Cheng | 93f143e | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 676 | void ScheduleDAG::EmitNode(SDNode *Node, unsigned InstanceNo, |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 677 | DenseMap<SDOperand, unsigned> &VRBaseMap) { |
| 678 | // If machine instruction |
| 679 | if (Node->isTargetOpcode()) { |
| 680 | unsigned Opc = Node->getTargetOpcode(); |
Christopher Lamb | e95328d | 2007-07-26 08:12:07 +0000 | [diff] [blame] | 681 | |
| 682 | // Handle subreg insert/extract specially |
| 683 | if (Opc == TargetInstrInfo::EXTRACT_SUBREG || |
| 684 | Opc == TargetInstrInfo::INSERT_SUBREG) { |
| 685 | EmitSubregNode(Node, VRBaseMap); |
| 686 | return; |
| 687 | } |
| 688 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 689 | const TargetInstrDescriptor &II = TII->get(Opc); |
| 690 | |
| 691 | unsigned NumResults = CountResults(Node); |
| 692 | unsigned NodeOperands = CountOperands(Node); |
| 693 | unsigned NumMIOperands = NodeOperands + NumResults; |
Evan Cheng | 2663978 | 2007-08-02 00:28:15 +0000 | [diff] [blame] | 694 | bool HasPhysRegOuts = (NumResults > II.numDefs) && II.ImplicitDefs; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 695 | #ifndef NDEBUG |
| 696 | assert((unsigned(II.numOperands) == NumMIOperands || |
Evan Cheng | 2663978 | 2007-08-02 00:28:15 +0000 | [diff] [blame] | 697 | HasPhysRegOuts || (II.Flags & M_VARIABLE_OPS)) && |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 698 | "#operands for dag node doesn't match .td file!"); |
| 699 | #endif |
| 700 | |
| 701 | // Create the new machine instruction. |
| 702 | MachineInstr *MI = new MachineInstr(II); |
| 703 | |
| 704 | // Add result register values for things that are defined by this |
| 705 | // instruction. |
| 706 | if (NumResults) |
Evan Cheng | 2663978 | 2007-08-02 00:28:15 +0000 | [diff] [blame] | 707 | CreateVirtualRegisters(Node, MI, II, VRBaseMap); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 708 | |
| 709 | // Emit all of the actual operands of this instruction, adding them to the |
| 710 | // instruction as appropriate. |
| 711 | for (unsigned i = 0; i != NodeOperands; ++i) |
Evan Cheng | 2663978 | 2007-08-02 00:28:15 +0000 | [diff] [blame] | 712 | AddOperand(MI, Node->getOperand(i), i+II.numDefs, &II, VRBaseMap); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 713 | |
| 714 | // Commute node if it has been determined to be profitable. |
| 715 | if (CommuteSet.count(Node)) { |
| 716 | MachineInstr *NewMI = TII->commuteInstruction(MI); |
| 717 | if (NewMI == 0) |
| 718 | DOUT << "Sched: COMMUTING FAILED!\n"; |
| 719 | else { |
| 720 | DOUT << "Sched: COMMUTED TO: " << *NewMI; |
| 721 | if (MI != NewMI) { |
| 722 | delete MI; |
| 723 | MI = NewMI; |
| 724 | } |
| 725 | } |
| 726 | } |
| 727 | |
| 728 | // Now that we have emitted all operands, emit this instruction itself. |
| 729 | if ((II.Flags & M_USES_CUSTOM_DAG_SCHED_INSERTION) == 0) { |
| 730 | BB->insert(BB->end(), MI); |
| 731 | } else { |
| 732 | // Insert this instruction into the end of the basic block, potentially |
| 733 | // taking some custom action. |
| 734 | BB = DAG.getTargetLoweringInfo().InsertAtEndOfBasicBlock(MI, BB); |
| 735 | } |
Evan Cheng | 2663978 | 2007-08-02 00:28:15 +0000 | [diff] [blame] | 736 | |
| 737 | // Additional results must be an physical register def. |
| 738 | if (HasPhysRegOuts) { |
| 739 | for (unsigned i = II.numDefs; i < NumResults; ++i) { |
| 740 | unsigned Reg = II.ImplicitDefs[i - II.numDefs]; |
Evan Cheng | 0af04f7 | 2007-08-02 05:29:38 +0000 | [diff] [blame] | 741 | if (Node->hasAnyUseOfValue(i)) |
Evan Cheng | 93f143e | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 742 | EmitCopyFromReg(Node, i, InstanceNo, Reg, VRBaseMap); |
Evan Cheng | 2663978 | 2007-08-02 00:28:15 +0000 | [diff] [blame] | 743 | } |
| 744 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 745 | } else { |
| 746 | switch (Node->getOpcode()) { |
| 747 | default: |
| 748 | #ifndef NDEBUG |
| 749 | Node->dump(&DAG); |
| 750 | #endif |
| 751 | assert(0 && "This target-independent node should have been selected!"); |
| 752 | case ISD::EntryToken: // fall thru |
| 753 | case ISD::TokenFactor: |
| 754 | case ISD::LABEL: |
| 755 | break; |
| 756 | case ISD::CopyToReg: { |
| 757 | unsigned InReg; |
| 758 | if (RegisterSDNode *R = dyn_cast<RegisterSDNode>(Node->getOperand(2))) |
| 759 | InReg = R->getReg(); |
| 760 | else |
| 761 | InReg = getVR(Node->getOperand(2), VRBaseMap); |
| 762 | unsigned DestReg = cast<RegisterSDNode>(Node->getOperand(1))->getReg(); |
| 763 | if (InReg != DestReg) {// Coalesced away the copy? |
| 764 | const TargetRegisterClass *TRC = 0; |
| 765 | // Get the target register class |
| 766 | if (MRegisterInfo::isVirtualRegister(InReg)) |
| 767 | TRC = RegMap->getRegClass(InReg); |
| 768 | else |
| 769 | TRC = getPhysicalRegisterRegClass(MRI, |
| 770 | Node->getOperand(2).getValueType(), |
| 771 | InReg); |
| 772 | MRI->copyRegToReg(*BB, BB->end(), DestReg, InReg, TRC); |
| 773 | } |
| 774 | break; |
| 775 | } |
| 776 | case ISD::CopyFromReg: { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 777 | unsigned SrcReg = cast<RegisterSDNode>(Node->getOperand(1))->getReg(); |
Evan Cheng | 93f143e | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 778 | EmitCopyFromReg(Node, 0, InstanceNo, SrcReg, VRBaseMap); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 779 | break; |
| 780 | } |
| 781 | case ISD::INLINEASM: { |
| 782 | unsigned NumOps = Node->getNumOperands(); |
| 783 | if (Node->getOperand(NumOps-1).getValueType() == MVT::Flag) |
| 784 | --NumOps; // Ignore the flag operand. |
| 785 | |
| 786 | // Create the inline asm machine instruction. |
| 787 | MachineInstr *MI = |
| 788 | new MachineInstr(BB, TII->get(TargetInstrInfo::INLINEASM)); |
| 789 | |
| 790 | // Add the asm string as an external symbol operand. |
| 791 | const char *AsmStr = |
| 792 | cast<ExternalSymbolSDNode>(Node->getOperand(1))->getSymbol(); |
| 793 | MI->addExternalSymbolOperand(AsmStr); |
| 794 | |
| 795 | // Add all of the operand registers to the instruction. |
| 796 | for (unsigned i = 2; i != NumOps;) { |
| 797 | unsigned Flags = cast<ConstantSDNode>(Node->getOperand(i))->getValue(); |
| 798 | unsigned NumVals = Flags >> 3; |
| 799 | |
| 800 | MI->addImmOperand(Flags); |
| 801 | ++i; // Skip the ID value. |
| 802 | |
| 803 | switch (Flags & 7) { |
| 804 | default: assert(0 && "Bad flags!"); |
| 805 | case 1: // Use of register. |
| 806 | for (; NumVals; --NumVals, ++i) { |
| 807 | unsigned Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg(); |
| 808 | MI->addRegOperand(Reg, false); |
| 809 | } |
| 810 | break; |
| 811 | case 2: // Def of register. |
| 812 | for (; NumVals; --NumVals, ++i) { |
| 813 | unsigned Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg(); |
| 814 | MI->addRegOperand(Reg, true); |
| 815 | } |
| 816 | break; |
| 817 | case 3: { // Immediate. |
Chris Lattner | 23544c1 | 2007-08-25 00:53:07 +0000 | [diff] [blame] | 818 | for (; NumVals; --NumVals, ++i) { |
| 819 | if (ConstantSDNode *CS = |
| 820 | dyn_cast<ConstantSDNode>(Node->getOperand(i))) { |
| 821 | MI->addImmOperand(CS->getValue()); |
| 822 | } else { |
| 823 | GlobalAddressSDNode *GA = |
| 824 | cast<GlobalAddressSDNode>(Node->getOperand(i)); |
| 825 | MI->addGlobalAddressOperand(GA->getGlobal(), GA->getOffset()); |
| 826 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 827 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 828 | break; |
| 829 | } |
| 830 | case 4: // Addressing mode. |
| 831 | // The addressing mode has been selected, just add all of the |
| 832 | // operands to the machine instruction. |
| 833 | for (; NumVals; --NumVals, ++i) |
| 834 | AddOperand(MI, Node->getOperand(i), 0, 0, VRBaseMap); |
| 835 | break; |
| 836 | } |
| 837 | } |
| 838 | break; |
| 839 | } |
| 840 | } |
| 841 | } |
| 842 | } |
| 843 | |
| 844 | void ScheduleDAG::EmitNoop() { |
| 845 | TII->insertNoop(*BB, BB->end()); |
| 846 | } |
| 847 | |
| 848 | /// EmitSchedule - Emit the machine code in scheduled order. |
| 849 | void ScheduleDAG::EmitSchedule() { |
| 850 | // If this is the first basic block in the function, and if it has live ins |
| 851 | // that need to be copied into vregs, emit the copies into the top of the |
| 852 | // block before emitting the code for the block. |
| 853 | MachineFunction &MF = DAG.getMachineFunction(); |
| 854 | if (&MF.front() == BB && MF.livein_begin() != MF.livein_end()) { |
| 855 | for (MachineFunction::livein_iterator LI = MF.livein_begin(), |
| 856 | E = MF.livein_end(); LI != E; ++LI) |
| 857 | if (LI->second) |
| 858 | MRI->copyRegToReg(*MF.begin(), MF.begin()->end(), LI->second, |
| 859 | LI->first, RegMap->getRegClass(LI->second)); |
| 860 | } |
| 861 | |
| 862 | |
| 863 | // Finally, emit the code for all of the scheduled instructions. |
| 864 | DenseMap<SDOperand, unsigned> VRBaseMap; |
| 865 | for (unsigned i = 0, e = Sequence.size(); i != e; i++) { |
| 866 | if (SUnit *SU = Sequence[i]) { |
Evan Cheng | 93f143e | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 867 | for (unsigned j = 0, ee = SU->FlaggedNodes.size(); j != ee; ++j) |
| 868 | EmitNode(SU->FlaggedNodes[j], SU->InstanceNo, VRBaseMap); |
| 869 | EmitNode(SU->Node, SU->InstanceNo, VRBaseMap); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 870 | } else { |
| 871 | // Null SUnit* is a noop. |
| 872 | EmitNoop(); |
| 873 | } |
| 874 | } |
| 875 | } |
| 876 | |
| 877 | /// dump - dump the schedule. |
| 878 | void ScheduleDAG::dumpSchedule() const { |
| 879 | for (unsigned i = 0, e = Sequence.size(); i != e; i++) { |
| 880 | if (SUnit *SU = Sequence[i]) |
| 881 | SU->dump(&DAG); |
| 882 | else |
| 883 | cerr << "**** NOOP ****\n"; |
| 884 | } |
| 885 | } |
| 886 | |
| 887 | |
| 888 | /// Run - perform scheduling. |
| 889 | /// |
| 890 | MachineBasicBlock *ScheduleDAG::Run() { |
| 891 | TII = TM.getInstrInfo(); |
| 892 | MRI = TM.getRegisterInfo(); |
| 893 | RegMap = BB->getParent()->getSSARegMap(); |
| 894 | ConstPool = BB->getParent()->getConstantPool(); |
| 895 | |
| 896 | Schedule(); |
| 897 | return BB; |
| 898 | } |
| 899 | |
| 900 | /// SUnit - Scheduling unit. It's an wrapper around either a single SDNode or |
| 901 | /// a group of nodes flagged together. |
| 902 | void SUnit::dump(const SelectionDAG *G) const { |
| 903 | cerr << "SU(" << NodeNum << "): "; |
| 904 | Node->dump(G); |
| 905 | cerr << "\n"; |
| 906 | if (FlaggedNodes.size() != 0) { |
| 907 | for (unsigned i = 0, e = FlaggedNodes.size(); i != e; i++) { |
| 908 | cerr << " "; |
| 909 | FlaggedNodes[i]->dump(G); |
| 910 | cerr << "\n"; |
| 911 | } |
| 912 | } |
| 913 | } |
| 914 | |
| 915 | void SUnit::dumpAll(const SelectionDAG *G) const { |
| 916 | dump(G); |
| 917 | |
| 918 | cerr << " # preds left : " << NumPredsLeft << "\n"; |
| 919 | cerr << " # succs left : " << NumSuccsLeft << "\n"; |
| 920 | cerr << " # chain preds left : " << NumChainPredsLeft << "\n"; |
| 921 | cerr << " # chain succs left : " << NumChainSuccsLeft << "\n"; |
| 922 | cerr << " Latency : " << Latency << "\n"; |
| 923 | cerr << " Depth : " << Depth << "\n"; |
| 924 | cerr << " Height : " << Height << "\n"; |
| 925 | |
| 926 | if (Preds.size() != 0) { |
| 927 | cerr << " Predecessors:\n"; |
| 928 | for (SUnit::const_succ_iterator I = Preds.begin(), E = Preds.end(); |
| 929 | I != E; ++I) { |
Evan Cheng | e795947 | 2007-09-19 01:38:40 +0000 | [diff] [blame] | 930 | if (I->isCtrl) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 931 | cerr << " ch #"; |
| 932 | else |
| 933 | cerr << " val #"; |
Evan Cheng | 93f143e | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 934 | cerr << I->Dep << " - SU(" << I->Dep->NodeNum << ")"; |
| 935 | if (I->isSpecial) |
| 936 | cerr << " *"; |
| 937 | cerr << "\n"; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 938 | } |
| 939 | } |
| 940 | if (Succs.size() != 0) { |
| 941 | cerr << " Successors:\n"; |
| 942 | for (SUnit::const_succ_iterator I = Succs.begin(), E = Succs.end(); |
| 943 | I != E; ++I) { |
Evan Cheng | e795947 | 2007-09-19 01:38:40 +0000 | [diff] [blame] | 944 | if (I->isCtrl) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 945 | cerr << " ch #"; |
| 946 | else |
| 947 | cerr << " val #"; |
Evan Cheng | 93f143e | 2007-09-25 01:54:36 +0000 | [diff] [blame] | 948 | cerr << I->Dep << " - SU(" << I->Dep->NodeNum << ")"; |
| 949 | if (I->isSpecial) |
| 950 | cerr << " *"; |
| 951 | cerr << "\n"; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 952 | } |
| 953 | } |
| 954 | cerr << "\n"; |
| 955 | } |