Evan Cheng | a9c2091 | 2006-01-21 02:32:06 +0000 | [diff] [blame] | 1 | //===---- ScheduleDAG.cpp - Implement the ScheduleDAG class ---------------===// |
Chris Lattner | d32b236 | 2005-08-18 18:45:24 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Jim Laskey | 5a608dd | 2005-10-31 12:49:09 +0000 | [diff] [blame] | 5 | // This file was developed by James M. Laskey and is distributed under the |
Chris Lattner | d32b236 | 2005-08-18 18:45:24 +0000 | [diff] [blame] | 6 | // University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Jim Laskey | e6b90fb | 2005-09-26 21:57:04 +0000 | [diff] [blame] | 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. |
Chris Lattner | d32b236 | 2005-08-18 18:45:24 +0000 | [diff] [blame] | 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
Evan Cheng | e165a78 | 2006-05-11 23:55:42 +0000 | [diff] [blame^] | 16 | #define DEBUG_TYPE "sched" |
Chris Lattner | b0d21ef | 2006-03-08 04:25:59 +0000 | [diff] [blame] | 17 | #include "llvm/CodeGen/ScheduleDAG.h" |
Chris Lattner | 5839bf2 | 2005-08-26 17:15:30 +0000 | [diff] [blame] | 18 | #include "llvm/CodeGen/MachineConstantPool.h" |
Chris Lattner | 4ccd406 | 2005-08-19 20:45:43 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/MachineFunction.h" |
Chris Lattner | 4ccd406 | 2005-08-19 20:45:43 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/SSARegMap.h" |
Chris Lattner | 2d973e4 | 2005-08-18 20:07:59 +0000 | [diff] [blame] | 21 | #include "llvm/Target/TargetMachine.h" |
| 22 | #include "llvm/Target/TargetInstrInfo.h" |
Chris Lattner | 025c39b | 2005-08-26 20:54:47 +0000 | [diff] [blame] | 23 | #include "llvm/Target/TargetLowering.h" |
Evan Cheng | e165a78 | 2006-05-11 23:55:42 +0000 | [diff] [blame^] | 24 | #include "llvm/Support/Debug.h" |
Chris Lattner | 54a30b9 | 2006-03-20 01:51:46 +0000 | [diff] [blame] | 25 | #include "llvm/Support/MathExtras.h" |
Evan Cheng | e165a78 | 2006-05-11 23:55:42 +0000 | [diff] [blame^] | 26 | #include <iostream> |
Chris Lattner | d32b236 | 2005-08-18 18:45:24 +0000 | [diff] [blame] | 27 | using namespace llvm; |
| 28 | |
Jim Laskey | e6b90fb | 2005-09-26 21:57:04 +0000 | [diff] [blame] | 29 | |
Evan Cheng | e165a78 | 2006-05-11 23:55:42 +0000 | [diff] [blame^] | 30 | /// BuildSchedUnits - Build SUnits from the selection dag that we are input. |
| 31 | /// This SUnit graph is similar to the SelectionDAG, but represents flagged |
| 32 | /// together nodes with a single SUnit. |
| 33 | void ScheduleDAG::BuildSchedUnits() { |
| 34 | // Reserve entries in the vector for each of the SUnits we are creating. This |
| 35 | // ensure that reallocation of the vector won't happen, so SUnit*'s won't get |
| 36 | // invalidated. |
| 37 | SUnits.reserve(std::distance(DAG.allnodes_begin(), DAG.allnodes_end())); |
| 38 | |
| 39 | const InstrItineraryData &InstrItins = TM.getInstrItineraryData(); |
| 40 | |
| 41 | for (SelectionDAG::allnodes_iterator NI = DAG.allnodes_begin(), |
| 42 | E = DAG.allnodes_end(); NI != E; ++NI) { |
| 43 | if (isPassiveNode(NI)) // Leaf node, e.g. a TargetImmediate. |
| 44 | continue; |
| 45 | |
| 46 | // If this node has already been processed, stop now. |
| 47 | if (SUnitMap[NI]) continue; |
| 48 | |
| 49 | SUnit *NodeSUnit = NewSUnit(NI); |
| 50 | |
| 51 | // See if anything is flagged to this node, if so, add them to flagged |
| 52 | // nodes. Nodes can have at most one flag input and one flag output. Flags |
| 53 | // are required the be the last operand and result of a node. |
| 54 | |
| 55 | // Scan up, adding flagged preds to FlaggedNodes. |
| 56 | SDNode *N = NI; |
| 57 | while (N->getNumOperands() && |
| 58 | N->getOperand(N->getNumOperands()-1).getValueType() == MVT::Flag) { |
| 59 | N = N->getOperand(N->getNumOperands()-1).Val; |
| 60 | NodeSUnit->FlaggedNodes.push_back(N); |
| 61 | SUnitMap[N] = NodeSUnit; |
| 62 | } |
| 63 | |
| 64 | // Scan down, adding this node and any flagged succs to FlaggedNodes if they |
| 65 | // have a user of the flag operand. |
| 66 | N = NI; |
| 67 | while (N->getValueType(N->getNumValues()-1) == MVT::Flag) { |
| 68 | SDOperand FlagVal(N, N->getNumValues()-1); |
| 69 | |
| 70 | // There are either zero or one users of the Flag result. |
| 71 | bool HasFlagUse = false; |
| 72 | for (SDNode::use_iterator UI = N->use_begin(), E = N->use_end(); |
| 73 | UI != E; ++UI) |
| 74 | if (FlagVal.isOperand(*UI)) { |
| 75 | HasFlagUse = true; |
| 76 | NodeSUnit->FlaggedNodes.push_back(N); |
| 77 | SUnitMap[N] = NodeSUnit; |
| 78 | N = *UI; |
| 79 | break; |
| 80 | } |
| 81 | if (!HasFlagUse) break; |
| 82 | } |
| 83 | |
| 84 | // Now all flagged nodes are in FlaggedNodes and N is the bottom-most node. |
| 85 | // Update the SUnit |
| 86 | NodeSUnit->Node = N; |
| 87 | SUnitMap[N] = NodeSUnit; |
| 88 | |
| 89 | // Compute the latency for the node. We use the sum of the latencies for |
| 90 | // all nodes flagged together into this SUnit. |
| 91 | if (InstrItins.isEmpty()) { |
| 92 | // No latency information. |
| 93 | NodeSUnit->Latency = 1; |
| 94 | } else { |
| 95 | NodeSUnit->Latency = 0; |
| 96 | if (N->isTargetOpcode()) { |
| 97 | unsigned SchedClass = TII->getSchedClass(N->getTargetOpcode()); |
| 98 | InstrStage *S = InstrItins.begin(SchedClass); |
| 99 | InstrStage *E = InstrItins.end(SchedClass); |
| 100 | for (; S != E; ++S) |
| 101 | NodeSUnit->Latency += S->Cycles; |
| 102 | } |
| 103 | for (unsigned i = 0, e = NodeSUnit->FlaggedNodes.size(); i != e; ++i) { |
| 104 | SDNode *FNode = NodeSUnit->FlaggedNodes[i]; |
| 105 | if (FNode->isTargetOpcode()) { |
| 106 | unsigned SchedClass = TII->getSchedClass(FNode->getTargetOpcode()); |
| 107 | InstrStage *S = InstrItins.begin(SchedClass); |
| 108 | InstrStage *E = InstrItins.end(SchedClass); |
| 109 | for (; S != E; ++S) |
| 110 | NodeSUnit->Latency += S->Cycles; |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | // Pass 2: add the preds, succs, etc. |
| 117 | for (unsigned su = 0, e = SUnits.size(); su != e; ++su) { |
| 118 | SUnit *SU = &SUnits[su]; |
| 119 | SDNode *MainNode = SU->Node; |
| 120 | |
| 121 | if (MainNode->isTargetOpcode()) { |
| 122 | unsigned Opc = MainNode->getTargetOpcode(); |
| 123 | if (TII->isTwoAddrInstr(Opc)) { |
| 124 | SU->isTwoAddress = true; |
| 125 | SDNode *OpN = MainNode->getOperand(0).Val; |
| 126 | SUnit *OpSU = SUnitMap[OpN]; |
| 127 | if (OpSU) |
| 128 | OpSU->isDefNUseOperand = true; |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | // Find all predecessors and successors of the group. |
| 133 | // Temporarily add N to make code simpler. |
| 134 | SU->FlaggedNodes.push_back(MainNode); |
| 135 | |
| 136 | for (unsigned n = 0, e = SU->FlaggedNodes.size(); n != e; ++n) { |
| 137 | SDNode *N = SU->FlaggedNodes[n]; |
| 138 | |
| 139 | for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) { |
| 140 | SDNode *OpN = N->getOperand(i).Val; |
| 141 | if (isPassiveNode(OpN)) continue; // Not scheduled. |
| 142 | SUnit *OpSU = SUnitMap[OpN]; |
| 143 | assert(OpSU && "Node has no SUnit!"); |
| 144 | if (OpSU == SU) continue; // In the same group. |
| 145 | |
| 146 | MVT::ValueType OpVT = N->getOperand(i).getValueType(); |
| 147 | assert(OpVT != MVT::Flag && "Flagged nodes should be in same sunit!"); |
| 148 | bool isChain = OpVT == MVT::Other; |
| 149 | |
| 150 | if (SU->Preds.insert(std::make_pair(OpSU, isChain)).second) { |
| 151 | if (!isChain) { |
| 152 | SU->NumPreds++; |
| 153 | SU->NumPredsLeft++; |
| 154 | } else { |
| 155 | SU->NumChainPredsLeft++; |
| 156 | } |
| 157 | } |
| 158 | if (OpSU->Succs.insert(std::make_pair(SU, isChain)).second) { |
| 159 | if (!isChain) { |
| 160 | OpSU->NumSuccs++; |
| 161 | OpSU->NumSuccsLeft++; |
| 162 | } else { |
| 163 | OpSU->NumChainSuccsLeft++; |
| 164 | } |
| 165 | } |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | // Remove MainNode from FlaggedNodes again. |
| 170 | SU->FlaggedNodes.pop_back(); |
| 171 | } |
| 172 | |
| 173 | return; |
| 174 | } |
| 175 | |
| 176 | static void CalculateDepths(SUnit *SU, unsigned Depth) { |
| 177 | if (Depth > SU->Depth) SU->Depth = Depth; |
| 178 | for (std::set<std::pair<SUnit*, bool> >::iterator I = SU->Succs.begin(), |
| 179 | E = SU->Succs.end(); I != E; ++I) |
| 180 | CalculateDepths(I->first, Depth+1); |
| 181 | } |
| 182 | |
| 183 | void ScheduleDAG::CalculateDepths() { |
| 184 | SUnit *Entry = SUnitMap[DAG.getEntryNode().Val]; |
| 185 | ::CalculateDepths(Entry, 0U); |
| 186 | for (unsigned i = 0, e = SUnits.size(); i != e; ++i) |
| 187 | if (SUnits[i].Preds.size() == 0 && &SUnits[i] != Entry) { |
| 188 | ::CalculateDepths(&SUnits[i], 0U); |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | static void CalculateHeights(SUnit *SU, unsigned Height) { |
| 193 | if (Height > SU->Height) SU->Height = Height; |
| 194 | for (std::set<std::pair<SUnit*, bool> >::iterator I = SU->Preds.begin(), |
| 195 | E = SU->Preds.end(); I != E; ++I) |
| 196 | CalculateHeights(I->first, Height+1); |
| 197 | } |
| 198 | void ScheduleDAG::CalculateHeights() { |
| 199 | SUnit *Root = SUnitMap[DAG.getRoot().Val]; |
| 200 | ::CalculateHeights(Root, 0U); |
| 201 | } |
| 202 | |
Jim Laskey | e6b90fb | 2005-09-26 21:57:04 +0000 | [diff] [blame] | 203 | /// CountResults - The results of target nodes have register or immediate |
| 204 | /// operands first, then an optional chain, and optional flag operands (which do |
| 205 | /// not go into the machine instrs.) |
Evan Cheng | a9c2091 | 2006-01-21 02:32:06 +0000 | [diff] [blame] | 206 | static unsigned CountResults(SDNode *Node) { |
Jim Laskey | b6d4c2c | 2005-09-30 19:15:27 +0000 | [diff] [blame] | 207 | unsigned N = Node->getNumValues(); |
| 208 | while (N && Node->getValueType(N - 1) == MVT::Flag) |
Jim Laskey | e6b90fb | 2005-09-26 21:57:04 +0000 | [diff] [blame] | 209 | --N; |
Jim Laskey | b6d4c2c | 2005-09-30 19:15:27 +0000 | [diff] [blame] | 210 | if (N && Node->getValueType(N - 1) == MVT::Other) |
Jim Laskey | e6b90fb | 2005-09-26 21:57:04 +0000 | [diff] [blame] | 211 | --N; // Skip over chain result. |
| 212 | return N; |
| 213 | } |
| 214 | |
| 215 | /// CountOperands The inputs to target nodes have any actual inputs first, |
| 216 | /// followed by an optional chain operand, then flag operands. Compute the |
| 217 | /// number of actual operands that will go into the machine instr. |
Evan Cheng | a9c2091 | 2006-01-21 02:32:06 +0000 | [diff] [blame] | 218 | static unsigned CountOperands(SDNode *Node) { |
Jim Laskey | b6d4c2c | 2005-09-30 19:15:27 +0000 | [diff] [blame] | 219 | unsigned N = Node->getNumOperands(); |
| 220 | while (N && Node->getOperand(N - 1).getValueType() == MVT::Flag) |
Jim Laskey | e6b90fb | 2005-09-26 21:57:04 +0000 | [diff] [blame] | 221 | --N; |
Jim Laskey | b6d4c2c | 2005-09-30 19:15:27 +0000 | [diff] [blame] | 222 | if (N && Node->getOperand(N - 1).getValueType() == MVT::Other) |
Jim Laskey | e6b90fb | 2005-09-26 21:57:04 +0000 | [diff] [blame] | 223 | --N; // Ignore chain if it exists. |
| 224 | return N; |
| 225 | } |
| 226 | |
Evan Cheng | 4ef1086 | 2006-01-23 07:01:07 +0000 | [diff] [blame] | 227 | static unsigned CreateVirtualRegisters(MachineInstr *MI, |
| 228 | unsigned NumResults, |
| 229 | SSARegMap *RegMap, |
| 230 | const TargetInstrDescriptor &II) { |
Jim Laskey | e6b90fb | 2005-09-26 21:57:04 +0000 | [diff] [blame] | 231 | // Create the result registers for this node and add the result regs to |
| 232 | // the machine instruction. |
| 233 | const TargetOperandInfo *OpInfo = II.OpInfo; |
| 234 | unsigned ResultReg = RegMap->createVirtualRegister(OpInfo[0].RegClass); |
| 235 | MI->addRegOperand(ResultReg, MachineOperand::Def); |
| 236 | for (unsigned i = 1; i != NumResults; ++i) { |
| 237 | assert(OpInfo[i].RegClass && "Isn't a register operand!"); |
Chris Lattner | 505277a | 2005-10-01 07:45:09 +0000 | [diff] [blame] | 238 | MI->addRegOperand(RegMap->createVirtualRegister(OpInfo[i].RegClass), |
Jim Laskey | e6b90fb | 2005-09-26 21:57:04 +0000 | [diff] [blame] | 239 | MachineOperand::Def); |
| 240 | } |
| 241 | return ResultReg; |
| 242 | } |
| 243 | |
Chris Lattner | df37506 | 2006-03-10 07:25:12 +0000 | [diff] [blame] | 244 | /// getVR - Return the virtual register corresponding to the specified result |
| 245 | /// of the specified node. |
| 246 | static unsigned getVR(SDOperand Op, std::map<SDNode*, unsigned> &VRBaseMap) { |
| 247 | std::map<SDNode*, unsigned>::iterator I = VRBaseMap.find(Op.Val); |
| 248 | assert(I != VRBaseMap.end() && "Node emitted out of order - late"); |
| 249 | return I->second + Op.ResNo; |
| 250 | } |
| 251 | |
| 252 | |
Chris Lattner | ed18b68 | 2006-02-24 18:54:03 +0000 | [diff] [blame] | 253 | /// AddOperand - Add the specified operand to the specified machine instr. II |
| 254 | /// specifies the instruction information for the node, and IIOpNum is the |
| 255 | /// operand number (in the II) that we are adding. IIOpNum and II are used for |
| 256 | /// assertions only. |
| 257 | void ScheduleDAG::AddOperand(MachineInstr *MI, SDOperand Op, |
| 258 | unsigned IIOpNum, |
Chris Lattner | df37506 | 2006-03-10 07:25:12 +0000 | [diff] [blame] | 259 | const TargetInstrDescriptor *II, |
| 260 | std::map<SDNode*, unsigned> &VRBaseMap) { |
Chris Lattner | ed18b68 | 2006-02-24 18:54:03 +0000 | [diff] [blame] | 261 | if (Op.isTargetOpcode()) { |
| 262 | // Note that this case is redundant with the final else block, but we |
| 263 | // include it because it is the most common and it makes the logic |
| 264 | // simpler here. |
| 265 | assert(Op.getValueType() != MVT::Other && |
| 266 | Op.getValueType() != MVT::Flag && |
| 267 | "Chain and flag operands should occur at end of operand list!"); |
| 268 | |
| 269 | // Get/emit the operand. |
Chris Lattner | df37506 | 2006-03-10 07:25:12 +0000 | [diff] [blame] | 270 | unsigned VReg = getVR(Op, VRBaseMap); |
Chris Lattner | ed18b68 | 2006-02-24 18:54:03 +0000 | [diff] [blame] | 271 | MI->addRegOperand(VReg, MachineOperand::Use); |
| 272 | |
| 273 | // Verify that it is right. |
| 274 | assert(MRegisterInfo::isVirtualRegister(VReg) && "Not a vreg?"); |
| 275 | if (II) { |
| 276 | assert(II->OpInfo[IIOpNum].RegClass && |
| 277 | "Don't have operand info for this instruction!"); |
| 278 | assert(RegMap->getRegClass(VReg) == II->OpInfo[IIOpNum].RegClass && |
| 279 | "Register class of operand and regclass of use don't agree!"); |
| 280 | } |
| 281 | } else if (ConstantSDNode *C = |
| 282 | dyn_cast<ConstantSDNode>(Op)) { |
Chris Lattner | 2d90ac7 | 2006-05-04 18:05:43 +0000 | [diff] [blame] | 283 | MI->addImmOperand(C->getValue()); |
Chris Lattner | ed18b68 | 2006-02-24 18:54:03 +0000 | [diff] [blame] | 284 | } else if (RegisterSDNode*R = |
| 285 | dyn_cast<RegisterSDNode>(Op)) { |
| 286 | MI->addRegOperand(R->getReg(), MachineOperand::Use); |
| 287 | } else if (GlobalAddressSDNode *TGA = |
| 288 | dyn_cast<GlobalAddressSDNode>(Op)) { |
Chris Lattner | ea50fab | 2006-05-04 01:15:02 +0000 | [diff] [blame] | 289 | MI->addGlobalAddressOperand(TGA->getGlobal(), TGA->getOffset()); |
Chris Lattner | ed18b68 | 2006-02-24 18:54:03 +0000 | [diff] [blame] | 290 | } else if (BasicBlockSDNode *BB = |
| 291 | dyn_cast<BasicBlockSDNode>(Op)) { |
| 292 | MI->addMachineBasicBlockOperand(BB->getBasicBlock()); |
| 293 | } else if (FrameIndexSDNode *FI = |
| 294 | dyn_cast<FrameIndexSDNode>(Op)) { |
| 295 | MI->addFrameIndexOperand(FI->getIndex()); |
Nate Begeman | 37efe67 | 2006-04-22 18:53:45 +0000 | [diff] [blame] | 296 | } else if (JumpTableSDNode *JT = |
| 297 | dyn_cast<JumpTableSDNode>(Op)) { |
| 298 | MI->addJumpTableIndexOperand(JT->getIndex()); |
Chris Lattner | ed18b68 | 2006-02-24 18:54:03 +0000 | [diff] [blame] | 299 | } else if (ConstantPoolSDNode *CP = |
| 300 | dyn_cast<ConstantPoolSDNode>(Op)) { |
Evan Cheng | 404cb4f | 2006-02-25 09:54:52 +0000 | [diff] [blame] | 301 | int Offset = CP->getOffset(); |
Chris Lattner | ed18b68 | 2006-02-24 18:54:03 +0000 | [diff] [blame] | 302 | unsigned Align = CP->getAlignment(); |
| 303 | // MachineConstantPool wants an explicit alignment. |
| 304 | if (Align == 0) { |
| 305 | if (CP->get()->getType() == Type::DoubleTy) |
| 306 | Align = 3; // always 8-byte align doubles. |
Chris Lattner | 54a30b9 | 2006-03-20 01:51:46 +0000 | [diff] [blame] | 307 | else { |
Chris Lattner | ed18b68 | 2006-02-24 18:54:03 +0000 | [diff] [blame] | 308 | Align = TM.getTargetData() |
Owen Anderson | a69571c | 2006-05-03 01:29:57 +0000 | [diff] [blame] | 309 | ->getTypeAlignmentShift(CP->get()->getType()); |
Chris Lattner | 54a30b9 | 2006-03-20 01:51:46 +0000 | [diff] [blame] | 310 | if (Align == 0) { |
| 311 | // Alignment of packed types. FIXME! |
Owen Anderson | a69571c | 2006-05-03 01:29:57 +0000 | [diff] [blame] | 312 | Align = TM.getTargetData()->getTypeSize(CP->get()->getType()); |
Chris Lattner | 54a30b9 | 2006-03-20 01:51:46 +0000 | [diff] [blame] | 313 | Align = Log2_64(Align); |
| 314 | } |
| 315 | } |
Chris Lattner | ed18b68 | 2006-02-24 18:54:03 +0000 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | unsigned Idx = ConstPool->getConstantPoolIndex(CP->get(), Align); |
Evan Cheng | 404cb4f | 2006-02-25 09:54:52 +0000 | [diff] [blame] | 319 | MI->addConstantPoolIndexOperand(Idx, Offset); |
Chris Lattner | ed18b68 | 2006-02-24 18:54:03 +0000 | [diff] [blame] | 320 | } else if (ExternalSymbolSDNode *ES = |
| 321 | dyn_cast<ExternalSymbolSDNode>(Op)) { |
Chris Lattner | ea50fab | 2006-05-04 01:15:02 +0000 | [diff] [blame] | 322 | MI->addExternalSymbolOperand(ES->getSymbol()); |
Chris Lattner | ed18b68 | 2006-02-24 18:54:03 +0000 | [diff] [blame] | 323 | } else { |
| 324 | assert(Op.getValueType() != MVT::Other && |
| 325 | Op.getValueType() != MVT::Flag && |
| 326 | "Chain and flag operands should occur at end of operand list!"); |
Chris Lattner | df37506 | 2006-03-10 07:25:12 +0000 | [diff] [blame] | 327 | unsigned VReg = getVR(Op, VRBaseMap); |
Chris Lattner | ed18b68 | 2006-02-24 18:54:03 +0000 | [diff] [blame] | 328 | MI->addRegOperand(VReg, MachineOperand::Use); |
| 329 | |
| 330 | // Verify that it is right. |
| 331 | assert(MRegisterInfo::isVirtualRegister(VReg) && "Not a vreg?"); |
| 332 | if (II) { |
| 333 | assert(II->OpInfo[IIOpNum].RegClass && |
| 334 | "Don't have operand info for this instruction!"); |
| 335 | assert(RegMap->getRegClass(VReg) == II->OpInfo[IIOpNum].RegClass && |
| 336 | "Register class of operand and regclass of use don't agree!"); |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | } |
| 341 | |
| 342 | |
Jim Laskey | b6d4c2c | 2005-09-30 19:15:27 +0000 | [diff] [blame] | 343 | /// EmitNode - Generate machine code for an node and needed dependencies. |
Jim Laskey | e6b90fb | 2005-09-26 21:57:04 +0000 | [diff] [blame] | 344 | /// |
Chris Lattner | 8c7ef05 | 2006-03-10 07:28:36 +0000 | [diff] [blame] | 345 | void ScheduleDAG::EmitNode(SDNode *Node, |
Chris Lattner | df37506 | 2006-03-10 07:25:12 +0000 | [diff] [blame] | 346 | std::map<SDNode*, unsigned> &VRBaseMap) { |
Jim Laskey | b6d4c2c | 2005-09-30 19:15:27 +0000 | [diff] [blame] | 347 | unsigned VRBase = 0; // First virtual register for node |
Chris Lattner | 2d973e4 | 2005-08-18 20:07:59 +0000 | [diff] [blame] | 348 | |
Jim Laskey | b6d4c2c | 2005-09-30 19:15:27 +0000 | [diff] [blame] | 349 | // If machine instruction |
| 350 | if (Node->isTargetOpcode()) { |
| 351 | unsigned Opc = Node->getTargetOpcode(); |
Evan Cheng | a9c2091 | 2006-01-21 02:32:06 +0000 | [diff] [blame] | 352 | const TargetInstrDescriptor &II = TII->get(Opc); |
Chris Lattner | 2d973e4 | 2005-08-18 20:07:59 +0000 | [diff] [blame] | 353 | |
Jim Laskey | b6d4c2c | 2005-09-30 19:15:27 +0000 | [diff] [blame] | 354 | unsigned NumResults = CountResults(Node); |
| 355 | unsigned NodeOperands = CountOperands(Node); |
Jim Laskey | e6b90fb | 2005-09-26 21:57:04 +0000 | [diff] [blame] | 356 | unsigned NumMIOperands = NodeOperands + NumResults; |
Chris Lattner | da8abb0 | 2005-09-01 18:44:10 +0000 | [diff] [blame] | 357 | #ifndef NDEBUG |
Chris Lattner | 14b392a | 2005-08-24 22:02:41 +0000 | [diff] [blame] | 358 | assert((unsigned(II.numOperands) == NumMIOperands || II.numOperands == -1)&& |
Chris Lattner | 2d973e4 | 2005-08-18 20:07:59 +0000 | [diff] [blame] | 359 | "#operands for dag node doesn't match .td file!"); |
Chris Lattner | ca6aa2f | 2005-08-19 01:01:34 +0000 | [diff] [blame] | 360 | #endif |
Chris Lattner | 2d973e4 | 2005-08-18 20:07:59 +0000 | [diff] [blame] | 361 | |
| 362 | // Create the new machine instruction. |
Chris Lattner | 8b915b4 | 2006-05-04 18:16:01 +0000 | [diff] [blame] | 363 | MachineInstr *MI = new MachineInstr(Opc, NumMIOperands); |
Chris Lattner | 2d973e4 | 2005-08-18 20:07:59 +0000 | [diff] [blame] | 364 | |
| 365 | // Add result register values for things that are defined by this |
| 366 | // instruction. |
Chris Lattner | a417652 | 2005-10-30 18:54:27 +0000 | [diff] [blame] | 367 | |
| 368 | // If the node is only used by a CopyToReg and the dest reg is a vreg, use |
| 369 | // the CopyToReg'd destination register instead of creating a new vreg. |
| 370 | if (NumResults == 1) { |
| 371 | for (SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end(); |
| 372 | UI != E; ++UI) { |
| 373 | SDNode *Use = *UI; |
| 374 | if (Use->getOpcode() == ISD::CopyToReg && |
| 375 | Use->getOperand(2).Val == Node) { |
| 376 | unsigned Reg = cast<RegisterSDNode>(Use->getOperand(1))->getReg(); |
| 377 | if (MRegisterInfo::isVirtualRegister(Reg)) { |
| 378 | VRBase = Reg; |
| 379 | MI->addRegOperand(Reg, MachineOperand::Def); |
| 380 | break; |
| 381 | } |
| 382 | } |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | // Otherwise, create new virtual registers. |
| 387 | if (NumResults && VRBase == 0) |
Evan Cheng | 4ef1086 | 2006-01-23 07:01:07 +0000 | [diff] [blame] | 388 | VRBase = CreateVirtualRegisters(MI, NumResults, RegMap, II); |
Jim Laskey | b6d4c2c | 2005-09-30 19:15:27 +0000 | [diff] [blame] | 389 | |
| 390 | // Emit all of the actual operands of this instruction, adding them to the |
| 391 | // instruction as appropriate. |
Chris Lattner | ed18b68 | 2006-02-24 18:54:03 +0000 | [diff] [blame] | 392 | for (unsigned i = 0; i != NodeOperands; ++i) |
Chris Lattner | df37506 | 2006-03-10 07:25:12 +0000 | [diff] [blame] | 393 | AddOperand(MI, Node->getOperand(i), i+NumResults, &II, VRBaseMap); |
Jim Laskey | b6d4c2c | 2005-09-30 19:15:27 +0000 | [diff] [blame] | 394 | |
| 395 | // Now that we have emitted all operands, emit this instruction itself. |
| 396 | if ((II.Flags & M_USES_CUSTOM_DAG_SCHED_INSERTION) == 0) { |
| 397 | BB->insert(BB->end(), MI); |
| 398 | } else { |
| 399 | // Insert this instruction into the end of the basic block, potentially |
| 400 | // taking some custom action. |
| 401 | BB = DAG.getTargetLoweringInfo().InsertAtEndOfBasicBlock(MI, BB); |
| 402 | } |
| 403 | } else { |
| 404 | switch (Node->getOpcode()) { |
| 405 | default: |
| 406 | Node->dump(); |
| 407 | assert(0 && "This target-independent node should have been selected!"); |
| 408 | case ISD::EntryToken: // fall thru |
| 409 | case ISD::TokenFactor: |
| 410 | break; |
| 411 | case ISD::CopyToReg: { |
Chris Lattner | df37506 | 2006-03-10 07:25:12 +0000 | [diff] [blame] | 412 | unsigned InReg = getVR(Node->getOperand(2), VRBaseMap); |
Chris Lattner | a417652 | 2005-10-30 18:54:27 +0000 | [diff] [blame] | 413 | unsigned DestReg = cast<RegisterSDNode>(Node->getOperand(1))->getReg(); |
Chris Lattner | 45053fc | 2006-03-24 07:15:07 +0000 | [diff] [blame] | 414 | if (InReg != DestReg) // Coalesced away the copy? |
Evan Cheng | a9c2091 | 2006-01-21 02:32:06 +0000 | [diff] [blame] | 415 | MRI->copyRegToReg(*BB, BB->end(), DestReg, InReg, |
| 416 | RegMap->getRegClass(InReg)); |
Jim Laskey | b6d4c2c | 2005-09-30 19:15:27 +0000 | [diff] [blame] | 417 | break; |
| 418 | } |
| 419 | case ISD::CopyFromReg: { |
| 420 | unsigned SrcReg = cast<RegisterSDNode>(Node->getOperand(1))->getReg(); |
Chris Lattner | 089c25c | 2005-10-09 05:58:56 +0000 | [diff] [blame] | 421 | if (MRegisterInfo::isVirtualRegister(SrcReg)) { |
| 422 | VRBase = SrcReg; // Just use the input register directly! |
| 423 | break; |
| 424 | } |
| 425 | |
Chris Lattner | a417652 | 2005-10-30 18:54:27 +0000 | [diff] [blame] | 426 | // If the node is only used by a CopyToReg and the dest reg is a vreg, use |
| 427 | // the CopyToReg'd destination register instead of creating a new vreg. |
| 428 | for (SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end(); |
| 429 | UI != E; ++UI) { |
| 430 | SDNode *Use = *UI; |
| 431 | if (Use->getOpcode() == ISD::CopyToReg && |
| 432 | Use->getOperand(2).Val == Node) { |
| 433 | unsigned DestReg = cast<RegisterSDNode>(Use->getOperand(1))->getReg(); |
| 434 | if (MRegisterInfo::isVirtualRegister(DestReg)) { |
| 435 | VRBase = DestReg; |
| 436 | break; |
| 437 | } |
| 438 | } |
| 439 | } |
| 440 | |
Jim Laskey | b6d4c2c | 2005-09-30 19:15:27 +0000 | [diff] [blame] | 441 | // Figure out the register class to create for the destreg. |
| 442 | const TargetRegisterClass *TRC = 0; |
Chris Lattner | a417652 | 2005-10-30 18:54:27 +0000 | [diff] [blame] | 443 | if (VRBase) { |
| 444 | TRC = RegMap->getRegClass(VRBase); |
| 445 | } else { |
Chris Lattner | 089c25c | 2005-10-09 05:58:56 +0000 | [diff] [blame] | 446 | |
Chris Lattner | a417652 | 2005-10-30 18:54:27 +0000 | [diff] [blame] | 447 | // Pick the register class of the right type that contains this physreg. |
Evan Cheng | a9c2091 | 2006-01-21 02:32:06 +0000 | [diff] [blame] | 448 | for (MRegisterInfo::regclass_iterator I = MRI->regclass_begin(), |
| 449 | E = MRI->regclass_end(); I != E; ++I) |
Nate Begeman | 6510b22 | 2005-12-01 04:51:06 +0000 | [diff] [blame] | 450 | if ((*I)->hasType(Node->getValueType(0)) && |
Chris Lattner | a417652 | 2005-10-30 18:54:27 +0000 | [diff] [blame] | 451 | (*I)->contains(SrcReg)) { |
| 452 | TRC = *I; |
| 453 | break; |
| 454 | } |
| 455 | assert(TRC && "Couldn't find register class for reg copy!"); |
Jim Laskey | b6d4c2c | 2005-09-30 19:15:27 +0000 | [diff] [blame] | 456 | |
Chris Lattner | a417652 | 2005-10-30 18:54:27 +0000 | [diff] [blame] | 457 | // Create the reg, emit the copy. |
| 458 | VRBase = RegMap->createVirtualRegister(TRC); |
| 459 | } |
Evan Cheng | a9c2091 | 2006-01-21 02:32:06 +0000 | [diff] [blame] | 460 | MRI->copyRegToReg(*BB, BB->end(), VRBase, SrcReg, TRC); |
Jim Laskey | b6d4c2c | 2005-09-30 19:15:27 +0000 | [diff] [blame] | 461 | break; |
| 462 | } |
Chris Lattner | acc43bf | 2006-01-26 23:28:04 +0000 | [diff] [blame] | 463 | case ISD::INLINEASM: { |
| 464 | unsigned NumOps = Node->getNumOperands(); |
| 465 | if (Node->getOperand(NumOps-1).getValueType() == MVT::Flag) |
| 466 | --NumOps; // Ignore the flag operand. |
| 467 | |
| 468 | // Create the inline asm machine instruction. |
| 469 | MachineInstr *MI = |
| 470 | new MachineInstr(BB, TargetInstrInfo::INLINEASM, (NumOps-2)/2+1); |
| 471 | |
| 472 | // Add the asm string as an external symbol operand. |
| 473 | const char *AsmStr = |
| 474 | cast<ExternalSymbolSDNode>(Node->getOperand(1))->getSymbol(); |
Chris Lattner | ea50fab | 2006-05-04 01:15:02 +0000 | [diff] [blame] | 475 | MI->addExternalSymbolOperand(AsmStr); |
Chris Lattner | acc43bf | 2006-01-26 23:28:04 +0000 | [diff] [blame] | 476 | |
| 477 | // Add all of the operand registers to the instruction. |
Chris Lattner | c3a9f8d | 2006-02-23 19:21:04 +0000 | [diff] [blame] | 478 | for (unsigned i = 2; i != NumOps;) { |
| 479 | unsigned Flags = cast<ConstantSDNode>(Node->getOperand(i))->getValue(); |
Chris Lattner | fd6d282 | 2006-02-24 19:18:20 +0000 | [diff] [blame] | 480 | unsigned NumVals = Flags >> 3; |
Chris Lattner | c3a9f8d | 2006-02-23 19:21:04 +0000 | [diff] [blame] | 481 | |
Chris Lattner | 2d90ac7 | 2006-05-04 18:05:43 +0000 | [diff] [blame] | 482 | MI->addImmOperand(Flags); |
Chris Lattner | c3a9f8d | 2006-02-23 19:21:04 +0000 | [diff] [blame] | 483 | ++i; // Skip the ID value. |
| 484 | |
| 485 | switch (Flags & 7) { |
Chris Lattner | acc43bf | 2006-01-26 23:28:04 +0000 | [diff] [blame] | 486 | default: assert(0 && "Bad flags!"); |
Chris Lattner | c3a9f8d | 2006-02-23 19:21:04 +0000 | [diff] [blame] | 487 | case 1: // Use of register. |
Chris Lattner | fd6d282 | 2006-02-24 19:18:20 +0000 | [diff] [blame] | 488 | for (; NumVals; --NumVals, ++i) { |
Chris Lattner | c3a9f8d | 2006-02-23 19:21:04 +0000 | [diff] [blame] | 489 | unsigned Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg(); |
Chris Lattner | ea50fab | 2006-05-04 01:15:02 +0000 | [diff] [blame] | 490 | MI->addRegOperand(Reg, MachineOperand::Use); |
Chris Lattner | c3a9f8d | 2006-02-23 19:21:04 +0000 | [diff] [blame] | 491 | } |
Chris Lattner | dc19b70 | 2006-02-04 02:26:14 +0000 | [diff] [blame] | 492 | break; |
Chris Lattner | c3a9f8d | 2006-02-23 19:21:04 +0000 | [diff] [blame] | 493 | case 2: // Def of register. |
Chris Lattner | fd6d282 | 2006-02-24 19:18:20 +0000 | [diff] [blame] | 494 | for (; NumVals; --NumVals, ++i) { |
Chris Lattner | c3a9f8d | 2006-02-23 19:21:04 +0000 | [diff] [blame] | 495 | unsigned Reg = cast<RegisterSDNode>(Node->getOperand(i))->getReg(); |
Chris Lattner | ea50fab | 2006-05-04 01:15:02 +0000 | [diff] [blame] | 496 | MI->addRegOperand(Reg, MachineOperand::Def); |
Chris Lattner | c3a9f8d | 2006-02-23 19:21:04 +0000 | [diff] [blame] | 497 | } |
Chris Lattner | dc19b70 | 2006-02-04 02:26:14 +0000 | [diff] [blame] | 498 | break; |
Chris Lattner | dc19b70 | 2006-02-04 02:26:14 +0000 | [diff] [blame] | 499 | case 3: { // Immediate. |
Chris Lattner | fd6d282 | 2006-02-24 19:18:20 +0000 | [diff] [blame] | 500 | assert(NumVals == 1 && "Unknown immediate value!"); |
Chris Lattner | dc19b70 | 2006-02-04 02:26:14 +0000 | [diff] [blame] | 501 | uint64_t Val = cast<ConstantSDNode>(Node->getOperand(i))->getValue(); |
Chris Lattner | 2d90ac7 | 2006-05-04 18:05:43 +0000 | [diff] [blame] | 502 | MI->addImmOperand(Val); |
Chris Lattner | c3a9f8d | 2006-02-23 19:21:04 +0000 | [diff] [blame] | 503 | ++i; |
Chris Lattner | dc19b70 | 2006-02-04 02:26:14 +0000 | [diff] [blame] | 504 | break; |
| 505 | } |
Chris Lattner | fd6d282 | 2006-02-24 19:18:20 +0000 | [diff] [blame] | 506 | case 4: // Addressing mode. |
| 507 | // The addressing mode has been selected, just add all of the |
| 508 | // operands to the machine instruction. |
| 509 | for (; NumVals; --NumVals, ++i) |
Chris Lattner | df37506 | 2006-03-10 07:25:12 +0000 | [diff] [blame] | 510 | AddOperand(MI, Node->getOperand(i), 0, 0, VRBaseMap); |
Chris Lattner | fd6d282 | 2006-02-24 19:18:20 +0000 | [diff] [blame] | 511 | break; |
Chris Lattner | dc19b70 | 2006-02-04 02:26:14 +0000 | [diff] [blame] | 512 | } |
Chris Lattner | acc43bf | 2006-01-26 23:28:04 +0000 | [diff] [blame] | 513 | } |
| 514 | break; |
| 515 | } |
Jim Laskey | b6d4c2c | 2005-09-30 19:15:27 +0000 | [diff] [blame] | 516 | } |
| 517 | } |
| 518 | |
Chris Lattner | df37506 | 2006-03-10 07:25:12 +0000 | [diff] [blame] | 519 | assert(!VRBaseMap.count(Node) && "Node emitted out of order - early"); |
| 520 | VRBaseMap[Node] = VRBase; |
Jim Laskey | b6d4c2c | 2005-09-30 19:15:27 +0000 | [diff] [blame] | 521 | } |
| 522 | |
Chris Lattner | a93dfcd | 2006-03-05 23:51:47 +0000 | [diff] [blame] | 523 | void ScheduleDAG::EmitNoop() { |
| 524 | TII->insertNoop(*BB, BB->end()); |
| 525 | } |
| 526 | |
Evan Cheng | e165a78 | 2006-05-11 23:55:42 +0000 | [diff] [blame^] | 527 | /// EmitSchedule - Emit the machine code in scheduled order. |
| 528 | void ScheduleDAG::EmitSchedule() { |
| 529 | std::map<SDNode*, unsigned> VRBaseMap; |
| 530 | for (unsigned i = 0, e = Sequence.size(); i != e; i++) { |
| 531 | if (SUnit *SU = Sequence[i]) { |
| 532 | for (unsigned j = 0, ee = SU->FlaggedNodes.size(); j != ee; j++) |
| 533 | EmitNode(SU->FlaggedNodes[j], VRBaseMap); |
| 534 | EmitNode(SU->Node, VRBaseMap); |
| 535 | } else { |
| 536 | // Null SUnit* is a noop. |
| 537 | EmitNoop(); |
| 538 | } |
| 539 | } |
| 540 | } |
| 541 | |
| 542 | /// dump - dump the schedule. |
| 543 | void ScheduleDAG::dumpSchedule() const { |
| 544 | for (unsigned i = 0, e = Sequence.size(); i != e; i++) { |
| 545 | if (SUnit *SU = Sequence[i]) |
| 546 | SU->dump(&DAG); |
| 547 | else |
| 548 | std::cerr << "**** NOOP ****\n"; |
| 549 | } |
| 550 | } |
| 551 | |
| 552 | |
Evan Cheng | a9c2091 | 2006-01-21 02:32:06 +0000 | [diff] [blame] | 553 | /// Run - perform scheduling. |
| 554 | /// |
| 555 | MachineBasicBlock *ScheduleDAG::Run() { |
| 556 | TII = TM.getInstrInfo(); |
| 557 | MRI = TM.getRegisterInfo(); |
| 558 | RegMap = BB->getParent()->getSSARegMap(); |
| 559 | ConstPool = BB->getParent()->getConstantPool(); |
Evan Cheng | 4ef1086 | 2006-01-23 07:01:07 +0000 | [diff] [blame] | 560 | |
Evan Cheng | a9c2091 | 2006-01-21 02:32:06 +0000 | [diff] [blame] | 561 | Schedule(); |
| 562 | return BB; |
Chris Lattner | d32b236 | 2005-08-18 18:45:24 +0000 | [diff] [blame] | 563 | } |
Evan Cheng | 4ef1086 | 2006-01-23 07:01:07 +0000 | [diff] [blame] | 564 | |
Evan Cheng | e165a78 | 2006-05-11 23:55:42 +0000 | [diff] [blame^] | 565 | /// SUnit - Scheduling unit. It's an wrapper around either a single SDNode or |
| 566 | /// a group of nodes flagged together. |
| 567 | void SUnit::dump(const SelectionDAG *G) const { |
| 568 | std::cerr << "SU(" << NodeNum << "): "; |
| 569 | Node->dump(G); |
| 570 | std::cerr << "\n"; |
| 571 | if (FlaggedNodes.size() != 0) { |
| 572 | for (unsigned i = 0, e = FlaggedNodes.size(); i != e; i++) { |
| 573 | std::cerr << " "; |
| 574 | FlaggedNodes[i]->dump(G); |
| 575 | std::cerr << "\n"; |
| 576 | } |
| 577 | } |
| 578 | } |
Evan Cheng | 4ef1086 | 2006-01-23 07:01:07 +0000 | [diff] [blame] | 579 | |
Evan Cheng | e165a78 | 2006-05-11 23:55:42 +0000 | [diff] [blame^] | 580 | void SUnit::dumpAll(const SelectionDAG *G) const { |
| 581 | dump(G); |
| 582 | |
| 583 | std::cerr << " # preds left : " << NumPredsLeft << "\n"; |
| 584 | std::cerr << " # succs left : " << NumSuccsLeft << "\n"; |
| 585 | std::cerr << " # chain preds left : " << NumChainPredsLeft << "\n"; |
| 586 | std::cerr << " # chain succs left : " << NumChainSuccsLeft << "\n"; |
| 587 | std::cerr << " Latency : " << Latency << "\n"; |
| 588 | std::cerr << " Depth : " << Depth << "\n"; |
| 589 | std::cerr << " Height : " << Height << "\n"; |
| 590 | |
| 591 | if (Preds.size() != 0) { |
| 592 | std::cerr << " Predecessors:\n"; |
| 593 | for (std::set<std::pair<SUnit*,bool> >::const_iterator I = Preds.begin(), |
| 594 | E = Preds.end(); I != E; ++I) { |
| 595 | if (I->second) |
| 596 | std::cerr << " ch "; |
| 597 | else |
| 598 | std::cerr << " val "; |
| 599 | I->first->dump(G); |
| 600 | } |
| 601 | } |
| 602 | if (Succs.size() != 0) { |
| 603 | std::cerr << " Successors:\n"; |
| 604 | for (std::set<std::pair<SUnit*, bool> >::const_iterator I = Succs.begin(), |
| 605 | E = Succs.end(); I != E; ++I) { |
| 606 | if (I->second) |
| 607 | std::cerr << " ch "; |
| 608 | else |
| 609 | std::cerr << " val "; |
| 610 | I->first->dump(G); |
| 611 | } |
| 612 | } |
| 613 | std::cerr << "\n"; |
| 614 | } |