| Chris Lattner | d32b236 | 2005-08-18 18:45:24 +0000 | [diff] [blame] | 1 | //===-- ScheduleDAG.cpp - Implement a trivial DAG scheduler ---------------===// | 
 | 2 | // | 
 | 3 | //                     The LLVM Compiler Infrastructure | 
 | 4 | // | 
 | 5 | // This file was developed by Chris Lattner and is distributed under the | 
 | 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 |  | 
 | 16 | #define DEBUG_TYPE "sched" | 
| Chris Lattner | 5839bf2 | 2005-08-26 17:15:30 +0000 | [diff] [blame] | 17 | #include "llvm/CodeGen/MachineConstantPool.h" | 
| Chris Lattner | 4ccd406 | 2005-08-19 20:45:43 +0000 | [diff] [blame] | 18 | #include "llvm/CodeGen/MachineFunction.h" | 
| Chris Lattner | d32b236 | 2005-08-18 18:45:24 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/SelectionDAGISel.h" | 
| Chris Lattner | 2d973e4 | 2005-08-18 20:07:59 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/SelectionDAG.h" | 
| Chris Lattner | 4ccd406 | 2005-08-19 20:45:43 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/SSARegMap.h" | 
| Chris Lattner | 2d973e4 | 2005-08-18 20:07:59 +0000 | [diff] [blame] | 22 | #include "llvm/Target/TargetMachine.h" | 
 | 23 | #include "llvm/Target/TargetInstrInfo.h" | 
| Chris Lattner | 025c39b | 2005-08-26 20:54:47 +0000 | [diff] [blame] | 24 | #include "llvm/Target/TargetLowering.h" | 
| Chris Lattner | 068ca15 | 2005-08-18 20:11:49 +0000 | [diff] [blame] | 25 | #include "llvm/Support/CommandLine.h" | 
| Jim Laskey | e6b90fb | 2005-09-26 21:57:04 +0000 | [diff] [blame] | 26 | #include "llvm/Support/Debug.h" | 
 | 27 | #include <iostream> | 
| Chris Lattner | d32b236 | 2005-08-18 18:45:24 +0000 | [diff] [blame] | 28 | using namespace llvm; | 
 | 29 |  | 
| Jim Laskey | e6b90fb | 2005-09-26 21:57:04 +0000 | [diff] [blame] | 30 | namespace { | 
 | 31 |   // Style of scheduling to use. | 
 | 32 |   enum ScheduleChoices { | 
 | 33 |     noScheduling, | 
 | 34 |     simpleScheduling, | 
 | 35 |   }; | 
 | 36 | } // namespace | 
 | 37 |  | 
 | 38 | cl::opt<ScheduleChoices> ScheduleStyle("sched", | 
 | 39 |   cl::desc("Choose scheduling style"), | 
 | 40 |   cl::init(noScheduling), | 
 | 41 |   cl::values( | 
 | 42 |     clEnumValN(noScheduling, "none", | 
 | 43 |               "Trivial emission with no analysis"), | 
 | 44 |     clEnumValN(simpleScheduling, "simple", | 
 | 45 |               "Minimize critical path and maximize processor utilization"), | 
 | 46 |    clEnumValEnd)); | 
 | 47 |  | 
 | 48 |  | 
| Chris Lattner | da8abb0 | 2005-09-01 18:44:10 +0000 | [diff] [blame] | 49 | #ifndef NDEBUG | 
| Chris Lattner | 068ca15 | 2005-08-18 20:11:49 +0000 | [diff] [blame] | 50 | static cl::opt<bool> | 
 | 51 | ViewDAGs("view-sched-dags", cl::Hidden, | 
 | 52 |          cl::desc("Pop up a window to show sched dags as they are processed")); | 
 | 53 | #else | 
| Chris Lattner | a639a43 | 2005-09-02 07:09:28 +0000 | [diff] [blame] | 54 | static const bool ViewDAGs = 0; | 
| Chris Lattner | 068ca15 | 2005-08-18 20:11:49 +0000 | [diff] [blame] | 55 | #endif | 
 | 56 |  | 
| Chris Lattner | 2d973e4 | 2005-08-18 20:07:59 +0000 | [diff] [blame] | 57 | namespace { | 
| Jim Laskey | e6b90fb | 2005-09-26 21:57:04 +0000 | [diff] [blame] | 58 | //===----------------------------------------------------------------------===// | 
 | 59 | /// | 
 | 60 | /// BitsIterator - Provides iteration through individual bits in a bit vector. | 
 | 61 | /// | 
 | 62 | template<class T> | 
 | 63 | class BitsIterator { | 
 | 64 | private: | 
 | 65 |   T Bits;                               // Bits left to iterate through | 
 | 66 |  | 
 | 67 | public: | 
 | 68 |   /// Ctor. | 
 | 69 |   BitsIterator(T Initial) : Bits(Initial) {} | 
 | 70 |    | 
 | 71 |   /// Next - Returns the next bit set or zero if exhausted. | 
 | 72 |   inline T Next() { | 
 | 73 |     // Get the rightmost bit set | 
 | 74 |     T Result = Bits & -Bits; | 
 | 75 |     // Remove from rest | 
 | 76 |     Bits &= ~Result; | 
 | 77 |     // Return single bit or zero | 
 | 78 |     return Result; | 
 | 79 |   } | 
 | 80 | }; | 
 | 81 |    | 
 | 82 | //===----------------------------------------------------------------------===// | 
 | 83 |  | 
 | 84 |  | 
 | 85 | //===----------------------------------------------------------------------===// | 
 | 86 | /// | 
 | 87 | /// ResourceTally - Manages the use of resources over time intervals.  Each | 
 | 88 | /// item (slot) in the tally vector represents the resources used at a given | 
 | 89 | /// moment.  A bit set to 1 indicates that a resource is in use, otherwise | 
 | 90 | /// available.  An assumption is made that the tally is large enough to schedule  | 
 | 91 | /// all current instructions (asserts otherwise.) | 
 | 92 | /// | 
 | 93 | template<class T> | 
 | 94 | class ResourceTally { | 
 | 95 | private: | 
 | 96 |   std::vector<T> Tally;                 // Resources used per slot | 
 | 97 |   typedef typename std::vector<T>::iterator Iter; | 
 | 98 |                                         // Tally iterator  | 
 | 99 |    | 
 | 100 |   /// AllInUse - Test to see if all of the resources in the slot are busy (set.) | 
 | 101 |   inline bool AllInUse(Iter Cursor, unsigned ResourceSet) { | 
 | 102 |     return (*Cursor & ResourceSet) == ResourceSet; | 
 | 103 |   } | 
 | 104 |  | 
 | 105 |   /// Skip - Skip over slots that use all of the specified resource (all are | 
 | 106 |   /// set.) | 
 | 107 |   Iter Skip(Iter Cursor, unsigned ResourceSet) { | 
 | 108 |     assert(ResourceSet && "At least one resource bit needs to bet set"); | 
| Chris Lattner | 2d973e4 | 2005-08-18 20:07:59 +0000 | [diff] [blame] | 109 |      | 
| Jim Laskey | e6b90fb | 2005-09-26 21:57:04 +0000 | [diff] [blame] | 110 |     // Continue to the end | 
 | 111 |     while (true) { | 
 | 112 |       // Break out if one of the resource bits is not set | 
 | 113 |       if (!AllInUse(Cursor, ResourceSet)) return Cursor; | 
 | 114 |       // Try next slot | 
 | 115 |       Cursor++; | 
 | 116 |       assert(Cursor < Tally.end() && "Tally is not large enough for schedule"); | 
| Chris Lattner | 2d973e4 | 2005-08-18 20:07:59 +0000 | [diff] [blame] | 117 |     } | 
| Jim Laskey | e6b90fb | 2005-09-26 21:57:04 +0000 | [diff] [blame] | 118 |   } | 
 | 119 |    | 
 | 120 |   /// FindSlots - Starting from Begin, locate N consecutive slots where at least  | 
 | 121 |   /// one of the resource bits is available.  Returns the address of first slot. | 
 | 122 |   Iter FindSlots(Iter Begin, unsigned N, unsigned ResourceSet, | 
 | 123 |                                          unsigned &Resource) { | 
 | 124 |     // Track position       | 
 | 125 |     Iter Cursor = Begin; | 
| Chris Lattner | 2d973e4 | 2005-08-18 20:07:59 +0000 | [diff] [blame] | 126 |      | 
| Jim Laskey | e6b90fb | 2005-09-26 21:57:04 +0000 | [diff] [blame] | 127 |     // Try all possible slots forward | 
 | 128 |     while (true) { | 
 | 129 |       // Skip full slots | 
 | 130 |       Cursor = Skip(Cursor, ResourceSet); | 
 | 131 |       // Determine end of interval | 
 | 132 |       Iter End = Cursor + N; | 
 | 133 |       assert(End <= Tally.end() && "Tally is not large enough for schedule"); | 
 | 134 |        | 
 | 135 |       // Iterate thru each resource | 
 | 136 |       BitsIterator<T> Resources(ResourceSet & ~*Cursor); | 
 | 137 |       while (unsigned Res = Resources.Next()) { | 
 | 138 |         // Check if resource is available for next N slots | 
 | 139 |         // Break out if resource is busy | 
 | 140 |         Iter Interval = Cursor; | 
 | 141 |         for (; Interval < End && !(*Interval & Res); Interval++) {} | 
 | 142 |          | 
 | 143 |         // If available for interval, return where and which resource | 
 | 144 |         if (Interval == End) { | 
 | 145 |           Resource = Res; | 
 | 146 |           return Cursor; | 
 | 147 |         } | 
 | 148 |         // Otherwise, check if worth checking other resources | 
 | 149 |         if (AllInUse(Interval, ResourceSet)) { | 
 | 150 |           // Start looking beyond interval | 
 | 151 |           Cursor = Interval; | 
 | 152 |           break; | 
 | 153 |         } | 
 | 154 |       } | 
 | 155 |       Cursor++; | 
| Chris Lattner | 2d973e4 | 2005-08-18 20:07:59 +0000 | [diff] [blame] | 156 |     } | 
| Jim Laskey | e6b90fb | 2005-09-26 21:57:04 +0000 | [diff] [blame] | 157 |   } | 
 | 158 |    | 
 | 159 |   /// Reserve - Mark busy (set) the specified N slots. | 
 | 160 |   void Reserve(Iter Begin, unsigned N, unsigned Resource) { | 
 | 161 |     // Determine end of interval | 
 | 162 |     Iter End = Begin + N; | 
 | 163 |     assert(End <= Tally.end() && "Tally is not large enough for schedule"); | 
 | 164 |   | 
 | 165 |     // Set resource bit in each slot | 
 | 166 |     for (; Begin < End; Begin++) | 
 | 167 |       *Begin |= Resource; | 
 | 168 |   } | 
 | 169 |  | 
 | 170 | public: | 
 | 171 |   /// Initialize - Resize and zero the tally to the specified number of time | 
 | 172 |   /// slots. | 
 | 173 |   inline void Initialize(unsigned N) { | 
 | 174 |     Tally.assign(N, 0);   // Initialize tally to all zeros. | 
 | 175 |   } | 
 | 176 |    | 
 | 177 |   // FindAndReserve - Locate and mark busy (set) N bits started at slot I, using | 
 | 178 |   // ResourceSet for choices. | 
 | 179 |   unsigned FindAndReserve(unsigned I, unsigned N, unsigned ResourceSet) { | 
 | 180 |     // Which resource used | 
 | 181 |     unsigned Resource; | 
 | 182 |     // Find slots for instruction. | 
 | 183 |     Iter Where = FindSlots(Tally.begin() + I, N, ResourceSet, Resource); | 
 | 184 |     // Reserve the slots | 
 | 185 |     Reserve(Where, N, Resource); | 
 | 186 |     // Return time slot (index) | 
 | 187 |     return Where - Tally.begin(); | 
 | 188 |   } | 
 | 189 |  | 
 | 190 | }; | 
 | 191 | //===----------------------------------------------------------------------===// | 
 | 192 |  | 
 | 193 |  | 
 | 194 | //===----------------------------------------------------------------------===// | 
 | 195 | // This struct tracks information used to schedule the a node. | 
 | 196 | struct ScheduleInfo { | 
 | 197 |   SDOperand     Op;                     // Operand information | 
 | 198 |   unsigned      Latency;                // Cycles to complete instruction | 
 | 199 |   unsigned      ResourceSet;            // Bit vector of usable resources | 
| Jim Laskey | e6b90fb | 2005-09-26 21:57:04 +0000 | [diff] [blame] | 200 |   unsigned      Slot;                   // Operand's time slot | 
 | 201 |    | 
 | 202 |   // Ctor. | 
 | 203 |   ScheduleInfo(SDOperand op) | 
 | 204 |   : Op(op) | 
 | 205 |   , Latency(0) | 
 | 206 |   , ResourceSet(0) | 
| Jim Laskey | e6b90fb | 2005-09-26 21:57:04 +0000 | [diff] [blame] | 207 |   , Slot(0) | 
 | 208 |   {} | 
 | 209 | }; | 
 | 210 | //===----------------------------------------------------------------------===// | 
 | 211 |  | 
 | 212 |  | 
 | 213 | //===----------------------------------------------------------------------===// | 
 | 214 | class SimpleSched { | 
 | 215 | private: | 
 | 216 |   // TODO - get ResourceSet from TII | 
 | 217 |   enum { | 
 | 218 |     RSInteger = 0x3,                    // Two integer units | 
 | 219 |     RSFloat = 0xC,                      // Two float units | 
 | 220 |     RSLoadStore = 0x30,                 // Two load store units | 
 | 221 |     RSOther = 0                         // Processing unit independent | 
| Chris Lattner | 2d973e4 | 2005-08-18 20:07:59 +0000 | [diff] [blame] | 222 |   }; | 
| Jim Laskey | e6b90fb | 2005-09-26 21:57:04 +0000 | [diff] [blame] | 223 |    | 
 | 224 |   MachineBasicBlock *BB;                // Current basic block | 
 | 225 |   SelectionDAG &DAG;                    // DAG of the current basic block | 
 | 226 |   const TargetMachine &TM;              // Target processor | 
 | 227 |   const TargetInstrInfo &TII;           // Target instruction information | 
 | 228 |   const MRegisterInfo &MRI;             // Target processor register information | 
 | 229 |   SSARegMap *RegMap;                    // Virtual/real register map | 
 | 230 |   MachineConstantPool *ConstPool;       // Target constant pool | 
 | 231 |   std::vector<ScheduleInfo> Operands;   // All operands to be scheduled | 
 | 232 |   std::vector<ScheduleInfo*> Ordering;  // Emit ordering of operands | 
 | 233 |   std::map<SDNode *, int> Visited;      // Operands that have been visited | 
 | 234 |   ResourceTally<unsigned> Tally;        // Resource usage tally | 
 | 235 |   unsigned NSlots;                      // Total latency | 
 | 236 |   std::map<SDNode *, unsigned>VRMap;    // Operand to VR map | 
 | 237 |   static const unsigned NotFound = ~0U; // Search marker | 
 | 238 |    | 
 | 239 | public: | 
 | 240 |  | 
 | 241 |   // Ctor. | 
 | 242 |   SimpleSched(SelectionDAG &D, MachineBasicBlock *bb) | 
 | 243 |     : BB(bb), DAG(D), TM(D.getTarget()), TII(*TM.getInstrInfo()), | 
 | 244 |       MRI(*TM.getRegisterInfo()), RegMap(BB->getParent()->getSSARegMap()), | 
 | 245 |       ConstPool(BB->getParent()->getConstantPool()), | 
 | 246 |       NSlots(0) { | 
 | 247 |     assert(&TII && "Target doesn't provide instr info?"); | 
 | 248 |     assert(&MRI && "Target doesn't provide register info?"); | 
 | 249 |   } | 
 | 250 |    | 
 | 251 |   // Run - perform scheduling. | 
 | 252 |   MachineBasicBlock *Run() { | 
 | 253 |     Schedule(); | 
 | 254 |     return BB; | 
 | 255 |   } | 
 | 256 |    | 
 | 257 | private: | 
 | 258 |   static bool isFlagDefiner(SDOperand Op) { return isFlagDefiner(Op.Val); } | 
 | 259 |   static bool isFlagUser(SDOperand Op) { return isFlagUser(Op.Val); } | 
 | 260 |   static bool isFlagDefiner(SDNode *A); | 
 | 261 |   static bool isFlagUser(SDNode *A); | 
 | 262 |   static bool isDefiner(SDNode *A, SDNode *B); | 
 | 263 |   static bool isPassiveOperand(SDOperand Op); | 
 | 264 |   void IncludeOperand(SDOperand Op); | 
 | 265 |   void VisitAll(); | 
 | 266 |   void Schedule(); | 
 | 267 |   void GatherOperandInfo(); | 
 | 268 |   bool isStrongDependency(SDOperand A, SDOperand B) { | 
 | 269 |     return isStrongDependency(A.Val, B.Val); | 
 | 270 |   } | 
 | 271 |   bool isWeakDependency(SDOperand A, SDOperand B) { | 
 | 272 |     return isWeakDependency(A.Val, B.Val); | 
 | 273 |   } | 
 | 274 |   static bool isStrongDependency(SDNode *A, SDNode *B); | 
 | 275 |   static bool isWeakDependency(SDNode *A, SDNode *B); | 
 | 276 |   void ScheduleBackward(); | 
 | 277 |   void ScheduleForward(); | 
 | 278 |   void EmitAll(); | 
 | 279 |   void EmitFlagUsers(SDOperand Op); | 
 | 280 |   static unsigned CountResults(SDOperand Op); | 
 | 281 |   static unsigned CountOperands(SDOperand Op); | 
 | 282 |   unsigned CreateVirtualRegisters(SDOperand Op, MachineInstr *MI, | 
 | 283 |                                   unsigned NumResults, | 
 | 284 |                                   const TargetInstrDescriptor &II); | 
 | 285 |   unsigned Emit(SDOperand A); | 
 | 286 |  | 
 | 287 |   void printSI(std::ostream &O, ScheduleInfo *SI) const ; | 
 | 288 |   void print(std::ostream &O) const ; | 
 | 289 |   inline void dump(const char *tag) const { std::cerr << tag; dump(); } | 
 | 290 |   void dump() const; | 
 | 291 | }; | 
 | 292 | //===----------------------------------------------------------------------===// | 
 | 293 |  | 
 | 294 |  | 
 | 295 | //===----------------------------------------------------------------------===// | 
 | 296 | class FlagUserIterator { | 
 | 297 | private: | 
 | 298 |   SDNode               *Definer;        // Node defining flag | 
 | 299 |   SDNode::use_iterator UI;              // User node iterator | 
 | 300 |   SDNode::use_iterator E;               // End of user nodes | 
 | 301 |   unsigned             MinRes;          // Minimum flag result | 
 | 302 |  | 
 | 303 | public: | 
 | 304 |   // Ctor. | 
 | 305 |   FlagUserIterator(SDNode *D) | 
 | 306 |   : Definer(D) | 
 | 307 |   , UI(D->use_begin()) | 
 | 308 |   , E(D->use_end()) | 
 | 309 |   , MinRes(D->getNumValues()) { | 
 | 310 |     // Find minimum flag result. | 
 | 311 |     while (MinRes && D->getValueType(MinRes - 1) == MVT::Flag) --MinRes; | 
 | 312 |   } | 
 | 313 |    | 
 | 314 |   /// isFlagUser - Return true if  node uses definer's flag. | 
 | 315 |   bool isFlagUser(SDNode *U) { | 
 | 316 |     // For each operand (in reverse to only look at flags) | 
 | 317 |     for (unsigned N = U->getNumOperands(); 0 < N--;) { | 
 | 318 |       // Get operand | 
 | 319 |       SDOperand Op = U->getOperand(N); | 
 | 320 |       // Not user if there are no flags | 
 | 321 |       if (Op.getValueType() != MVT::Flag) return false; | 
 | 322 |       // Return true if it is one of the flag results  | 
 | 323 |       if (Op.Val == Definer && Op.ResNo >= MinRes) return true; | 
 | 324 |     } | 
 | 325 |     // Not a flag user | 
 | 326 |     return false; | 
 | 327 |   } | 
 | 328 |    | 
 | 329 |   SDNode *next() { | 
 | 330 |     // Continue to next user | 
 | 331 |     while (UI != E) { | 
 | 332 |       // Next user node | 
 | 333 |       SDNode *User = *UI++; | 
 | 334 |       // Return true if is a flag user | 
 | 335 |       if (isFlagUser(User)) return User; | 
 | 336 |     } | 
 | 337 |      | 
 | 338 |     // No more user nodes | 
 | 339 |     return NULL; | 
 | 340 |   } | 
 | 341 | }; | 
 | 342 |  | 
 | 343 | } // namespace | 
 | 344 |  | 
 | 345 |  | 
 | 346 | //===----------------------------------------------------------------------===// | 
 | 347 | /// isFlagDefiner - Returns true if the operand defines a flag result. | 
 | 348 | bool SimpleSched::isFlagDefiner(SDNode *A) { | 
 | 349 |   unsigned N = A->getNumValues(); | 
 | 350 |   return N && A->getValueType(N - 1) == MVT::Flag; | 
| Chris Lattner | 2d973e4 | 2005-08-18 20:07:59 +0000 | [diff] [blame] | 351 | } | 
 | 352 |  | 
| Jim Laskey | e6b90fb | 2005-09-26 21:57:04 +0000 | [diff] [blame] | 353 | /// isFlagUser - Returns true if the operand uses a flag result. | 
 | 354 | /// | 
 | 355 | bool SimpleSched::isFlagUser(SDNode *A) { | 
 | 356 |   unsigned N = A->getNumOperands(); | 
 | 357 |   return N && A->getOperand(N - 1).getValueType() == MVT::Flag; | 
 | 358 | } | 
 | 359 |  | 
 | 360 | /// isDefiner - Return true if Node A is a definder for B. | 
 | 361 | /// | 
 | 362 | bool SimpleSched::isDefiner(SDNode *A, SDNode *B) { | 
 | 363 |   for (unsigned i = 0, N = B->getNumOperands(); i < N; i++) { | 
 | 364 |     if (B->getOperand(i).Val == A) return true; | 
| Chris Lattner | 2d973e4 | 2005-08-18 20:07:59 +0000 | [diff] [blame] | 365 |   } | 
| Jim Laskey | e6b90fb | 2005-09-26 21:57:04 +0000 | [diff] [blame] | 366 |   return false; | 
 | 367 | } | 
 | 368 |  | 
 | 369 | /// isPassiveOperand - Return true if the operand is a non-scheduled leaf | 
 | 370 | /// operand. | 
 | 371 | bool SimpleSched::isPassiveOperand(SDOperand Op) { | 
 | 372 |   if (isa<ConstantSDNode>(Op))       return true; | 
 | 373 |   if (isa<RegisterSDNode>(Op))       return true; | 
 | 374 |   if (isa<GlobalAddressSDNode>(Op))  return true; | 
 | 375 |   if (isa<BasicBlockSDNode>(Op))     return true; | 
 | 376 |   if (isa<FrameIndexSDNode>(Op))     return true; | 
 | 377 |   if (isa<ConstantPoolSDNode>(Op))   return true; | 
 | 378 |   if (isa<ExternalSymbolSDNode>(Op)) return true; | 
 | 379 |   return false; | 
 | 380 | } | 
 | 381 |  | 
 | 382 | /// IncludeOperand - Add operand to ScheduleInfo vector. | 
 | 383 | /// | 
 | 384 | void SimpleSched::IncludeOperand(SDOperand Op) { | 
 | 385 |   // Ignore entry node | 
 | 386 |   if (Op.getOpcode() == ISD::EntryToken) return; | 
 | 387 |   // Check current count for operand | 
 | 388 |   int Count = Visited[Op.Val]; | 
 | 389 |   // If the operand is already in list | 
 | 390 |   if (Count < 0) return; | 
 | 391 |   // If this the first time then get count  | 
 | 392 |   if (!Count) Count = Op.Val->use_size(); | 
 | 393 |   // Decrement count to indicate a visit | 
 | 394 |   Count--; | 
 | 395 |   // If count has gone to zero then add operand to list | 
 | 396 |   if (!Count) { | 
 | 397 |     // Add operand | 
 | 398 |     Operands.push_back(ScheduleInfo(Op)); | 
 | 399 |     // indicate operand has been added | 
 | 400 |     Count--; | 
 | 401 |   } | 
 | 402 |   // Mark as visited with new count  | 
 | 403 |   Visited[Op.Val] = Count; | 
 | 404 | } | 
 | 405 |  | 
 | 406 | /// VisitAll - Visit each operand breadth-wise to produce an initial ordering. | 
 | 407 | /// Note that the ordering in the Operands vector is reversed. | 
 | 408 | void SimpleSched::VisitAll() { | 
 | 409 |   // Add first element to list | 
 | 410 |   Operands.push_back(DAG.getRoot()); | 
 | 411 |   for (unsigned i = 0; i < Operands.size(); i++) { // note: size() varies | 
 | 412 |     // Get next operand. Need copy because Operands vector is growing and | 
 | 413 |     // addresses can be ScheduleInfo changing. | 
 | 414 |     SDOperand Op = Operands[i].Op; | 
 | 415 |     // Get the number of real operands | 
 | 416 |     unsigned NodeOperands = CountOperands(Op); | 
 | 417 |     // Get the total number of operands | 
 | 418 |     unsigned NumOperands = Op.getNumOperands(); | 
 | 419 |  | 
 | 420 |     // Visit all operands skipping the Other operand if present | 
 | 421 |     for (unsigned i = NumOperands; 0 < i--;) { | 
 | 422 |       SDOperand OpI = Op.getOperand(i); | 
 | 423 |       // Ignore passive operands | 
 | 424 |       if (isPassiveOperand(OpI)) continue; | 
 | 425 |       // Check out operand | 
 | 426 |       IncludeOperand(OpI); | 
 | 427 |     } | 
 | 428 |   } | 
 | 429 |  | 
 | 430 |   // Add entry node last (IncludeOperand filters entry nodes) | 
 | 431 |   if (DAG.getEntryNode().Val != DAG.getRoot().Val) | 
 | 432 |     Operands.push_back(DAG.getEntryNode()); | 
 | 433 | } | 
 | 434 |  | 
 | 435 | /// GatherOperandInfo - Get latency and resource information about each operand. | 
 | 436 | /// | 
 | 437 | void SimpleSched::GatherOperandInfo() { | 
 | 438 |   // Add addresses of operand info to ordering vector | 
 | 439 |   // Get number of operands | 
 | 440 |   unsigned N = Operands.size(); | 
 | 441 |   // FIXME: This is an ugly (but temporary!) hack to test the scheduler before | 
 | 442 |   // we have real target info. | 
 | 443 |    | 
 | 444 |   // For each operand being scheduled | 
 | 445 |   for (unsigned i = 0; i < N; i++) { | 
 | 446 |     ScheduleInfo* SI = &Operands[N - i - 1]; | 
 | 447 |     SDOperand Op = SI->Op; | 
 | 448 |     MVT::ValueType VT = Op.Val->getValueType(0); | 
 | 449 |     if (Op.isTargetOpcode()) { | 
 | 450 |       MachineOpCode TOpc = Op.getTargetOpcode(); | 
 | 451 |       // FIXME SI->Latency = std::max(1, TII.maxLatency(TOpc)); | 
 | 452 |       // FIXME SI->ResourceSet = TII.resources(TOpc); | 
| Jim Laskey | 5324fec | 2005-09-27 17:32:45 +0000 | [diff] [blame] | 453 |       if (TII.isCall(TOpc)) { | 
 | 454 |         SI->ResourceSet = RSInteger; | 
 | 455 |         SI->Latency = 40; | 
 | 456 |       } else if (TII.isLoad(TOpc)) { | 
| Jim Laskey | e6b90fb | 2005-09-26 21:57:04 +0000 | [diff] [blame] | 457 |         SI->ResourceSet = RSLoadStore; | 
 | 458 |         SI->Latency = 5; | 
 | 459 |       } else if (TII.isStore(TOpc)) { | 
 | 460 |         SI->ResourceSet = RSLoadStore; | 
 | 461 |         SI->Latency = 2; | 
 | 462 |       } else if (MVT::isInteger(VT)) { | 
 | 463 |         SI->ResourceSet = RSInteger; | 
 | 464 |         SI->Latency = 2; | 
 | 465 |       } else if (MVT::isFloatingPoint(VT)) { | 
 | 466 |         SI->ResourceSet = RSFloat; | 
 | 467 |         SI->Latency = 3; | 
 | 468 |       } else { | 
 | 469 |         SI->ResourceSet = RSOther; | 
 | 470 |         SI->Latency = 0; | 
 | 471 |       } | 
 | 472 |     } else { | 
 | 473 |       if (MVT::isInteger(VT)) { | 
 | 474 |         SI->ResourceSet = RSInteger; | 
 | 475 |         SI->Latency = 2; | 
 | 476 |       } else if (MVT::isFloatingPoint(VT)) { | 
 | 477 |         SI->ResourceSet = RSFloat; | 
 | 478 |         SI->Latency = 3; | 
 | 479 |       } else { | 
 | 480 |         SI->ResourceSet = RSOther; | 
 | 481 |         SI->Latency = 0; | 
 | 482 |       } | 
 | 483 |     } | 
 | 484 |      | 
 | 485 |     // Add one slot for the instruction itself | 
 | 486 |     SI->Latency++; | 
 | 487 |      | 
 | 488 |     // Sum up all the latencies for max tally size | 
 | 489 |     NSlots += SI->Latency; | 
 | 490 |      | 
 | 491 |     // Place in initial sorted order | 
 | 492 |     // FIXME - PUNT - ignore flag users  | 
 | 493 |     if (!isFlagUser(Op)) Ordering.push_back(SI); | 
 | 494 |   } | 
 | 495 | } | 
 | 496 |  | 
 | 497 | /// isStrongDependency - Return true if operand A has results used by operand B.  | 
 | 498 | /// I.E., B must wait for latency of A. | 
 | 499 | bool SimpleSched::isStrongDependency(SDNode *A, SDNode *B) { | 
 | 500 |   // If A defines for B then it's a strong dependency | 
 | 501 |   if (isDefiner(A, B)) return true; | 
 | 502 |   // If A defines a flag then it's users are part of the dependency | 
 | 503 |   if (isFlagDefiner(A)) { | 
 | 504 |     // Check each flag user | 
 | 505 |     FlagUserIterator FI(A); | 
 | 506 |     while (SDNode *User = FI.next()) { | 
 | 507 |       // If flag user has strong dependency so does B | 
 | 508 |       if (isStrongDependency(User, B)) return true; | 
 | 509 |     } | 
 | 510 |   } | 
 | 511 |   // If B defines a flag then it's users are part of the dependency | 
 | 512 |   if (isFlagDefiner(B)) { | 
 | 513 |     // Check each flag user | 
 | 514 |     FlagUserIterator FI(B); | 
 | 515 |     while (SDNode *User = FI.next()) { | 
 | 516 |       // If flag user has strong dependency so does B | 
 | 517 |       if (isStrongDependency(A, User)) return true; | 
 | 518 |     } | 
 | 519 |   } | 
 | 520 |   return false; | 
 | 521 | } | 
 | 522 |  | 
 | 523 | /// isWeakDependency Return true if operand A produces a result that will | 
 | 524 | /// conflict with operands of B. | 
 | 525 | bool SimpleSched::isWeakDependency(SDNode *A, SDNode *B) { | 
 | 526 |   // TODO check for conflicting real registers and aliases | 
| Jim Laskey | 5324fec | 2005-09-27 17:32:45 +0000 | [diff] [blame] | 527 | #if 0 // Since we are in SSA form and not checking register aliasing | 
| Jim Laskey | e6b90fb | 2005-09-26 21:57:04 +0000 | [diff] [blame] | 528 |   return A->getOpcode() == ISD::EntryToken || isStrongDependency(B, A); | 
| Jim Laskey | 5324fec | 2005-09-27 17:32:45 +0000 | [diff] [blame] | 529 | #else | 
 | 530 |   return A->getOpcode() == ISD::EntryToken; | 
 | 531 | #endif | 
| Jim Laskey | e6b90fb | 2005-09-26 21:57:04 +0000 | [diff] [blame] | 532 | } | 
 | 533 |  | 
 | 534 | /// ScheduleBackward - Schedule instructions so that any long latency | 
 | 535 | /// instructions and the critical path get pushed back in time. Time is run in | 
 | 536 | /// reverse to allow code reuse of the Tally and eliminate the overhead of | 
 | 537 | /// biasing every slot indices against NSlots. | 
 | 538 | void SimpleSched::ScheduleBackward() { | 
 | 539 |   // Size and clear the resource tally | 
 | 540 |   Tally.Initialize(NSlots); | 
 | 541 |   // Get number of operands to schedule | 
 | 542 |   unsigned N = Ordering.size(); | 
 | 543 |    | 
 | 544 |   // For each operand being scheduled | 
 | 545 |   for (unsigned i = N; 0 < i--;) { | 
 | 546 |     ScheduleInfo *SI = Ordering[i]; | 
 | 547 |     // Track insertion | 
 | 548 |     unsigned Slot = NotFound; | 
 | 549 |      | 
 | 550 |     // Compare against those previously scheduled operands | 
| Jeff Cohen | fef80f4 | 2005-09-29 01:59:49 +0000 | [diff] [blame^] | 551 |     unsigned j = i + 1; | 
 | 552 |     for (; j < N; j++) { | 
| Jim Laskey | e6b90fb | 2005-09-26 21:57:04 +0000 | [diff] [blame] | 553 |       // Get following instruction | 
 | 554 |       ScheduleInfo *Other = Ordering[j]; | 
 | 555 |        | 
 | 556 |       // Check dependency against previously inserted operands | 
 | 557 |       if (isStrongDependency(SI->Op, Other->Op)) { | 
 | 558 |         Slot = Other->Slot + Other->Latency; | 
 | 559 |         break; | 
| Jim Laskey | 5324fec | 2005-09-27 17:32:45 +0000 | [diff] [blame] | 560 |       } else if (isWeakDependency(SI->Op, Other->Op)) { | 
| Jim Laskey | e6b90fb | 2005-09-26 21:57:04 +0000 | [diff] [blame] | 561 |         Slot = Other->Slot; | 
 | 562 |         break; | 
 | 563 |       } | 
 | 564 |     } | 
 | 565 |      | 
 | 566 |     // If independent of others (or first entry) | 
 | 567 |     if (Slot == NotFound) Slot = 0; | 
 | 568 |      | 
 | 569 |     // Find a slot where the needed resources are available | 
 | 570 |     if (SI->ResourceSet) | 
 | 571 |       Slot = Tally.FindAndReserve(Slot, SI->Latency, SI->ResourceSet); | 
 | 572 |        | 
 | 573 |     // Set operand slot | 
 | 574 |     SI->Slot = Slot; | 
 | 575 |      | 
 | 576 |     // Insert sort based on slot | 
| Jeff Cohen | fef80f4 | 2005-09-29 01:59:49 +0000 | [diff] [blame^] | 577 |     j = i + 1; | 
| Jim Laskey | e6b90fb | 2005-09-26 21:57:04 +0000 | [diff] [blame] | 578 |     for (; j < N; j++) { | 
 | 579 |       // Get following instruction | 
 | 580 |       ScheduleInfo *Other = Ordering[j]; | 
 | 581 |       // Should we look further | 
 | 582 |       if (Slot >= Other->Slot) break; | 
 | 583 |       // Shuffle other into ordering | 
 | 584 |       Ordering[j - 1] = Other; | 
 | 585 |     } | 
 | 586 |     // Insert operand in proper slot | 
 | 587 |     if (j != i + 1) Ordering[j - 1] = SI; | 
 | 588 |   } | 
 | 589 | } | 
 | 590 |  | 
 | 591 | /// ScheduleForward - Schedule instructions to maximize packing. | 
 | 592 | /// | 
 | 593 | void SimpleSched::ScheduleForward() { | 
 | 594 |   // Size and clear the resource tally | 
 | 595 |   Tally.Initialize(NSlots); | 
 | 596 |   // Get number of operands to schedule | 
 | 597 |   unsigned N = Ordering.size(); | 
 | 598 |    | 
 | 599 |   // For each operand being scheduled | 
 | 600 |   for (unsigned i = 0; i < N; i++) { | 
 | 601 |     ScheduleInfo *SI = Ordering[i]; | 
 | 602 |     // Track insertion | 
 | 603 |     unsigned Slot = NotFound; | 
 | 604 |      | 
 | 605 |     // Compare against those previously scheduled operands | 
| Jeff Cohen | fef80f4 | 2005-09-29 01:59:49 +0000 | [diff] [blame^] | 606 |     unsigned j = i; | 
 | 607 |     for (; 0 < j--;) { | 
| Jim Laskey | e6b90fb | 2005-09-26 21:57:04 +0000 | [diff] [blame] | 608 |       // Get following instruction | 
 | 609 |       ScheduleInfo *Other = Ordering[j]; | 
 | 610 |        | 
 | 611 |       // Check dependency against previously inserted operands | 
 | 612 |       if (isStrongDependency(Other->Op, SI->Op)) { | 
 | 613 |         Slot = Other->Slot + Other->Latency; | 
 | 614 |         break; | 
| Jim Laskey | 5324fec | 2005-09-27 17:32:45 +0000 | [diff] [blame] | 615 |       } else if (isWeakDependency(Other->Op, SI->Op)) { | 
| Jim Laskey | e6b90fb | 2005-09-26 21:57:04 +0000 | [diff] [blame] | 616 |         Slot = Other->Slot; | 
 | 617 |         break; | 
 | 618 |       } | 
 | 619 |     } | 
 | 620 |      | 
 | 621 |     // If independent of others (or first entry) | 
 | 622 |     if (Slot == NotFound) Slot = 0; | 
 | 623 |      | 
 | 624 |     // Find a slot where the needed resources are available | 
 | 625 |     if (SI->ResourceSet) | 
 | 626 |       Slot = Tally.FindAndReserve(Slot, SI->Latency, SI->ResourceSet); | 
 | 627 |        | 
 | 628 |     // Set operand slot | 
 | 629 |     SI->Slot = Slot; | 
 | 630 |      | 
 | 631 |     // Insert sort based on slot | 
| Jeff Cohen | fef80f4 | 2005-09-29 01:59:49 +0000 | [diff] [blame^] | 632 |     j = i; | 
| Jim Laskey | e6b90fb | 2005-09-26 21:57:04 +0000 | [diff] [blame] | 633 |     for (; 0 < j--;) { | 
 | 634 |       // Get following instruction | 
 | 635 |       ScheduleInfo *Other = Ordering[j]; | 
 | 636 |       // Should we look further | 
 | 637 |       if (Slot >= Other->Slot) break; | 
 | 638 |       // Shuffle other into ordering | 
 | 639 |       Ordering[j + 1] = Other; | 
 | 640 |     } | 
 | 641 |     // Insert operand in proper slot | 
 | 642 |     if (j != i) Ordering[j + 1] = SI; | 
 | 643 |   } | 
 | 644 | } | 
 | 645 |  | 
 | 646 | /// EmitAll - Emit all operands in schedule sorted order. | 
 | 647 | /// | 
 | 648 | void SimpleSched::EmitAll() { | 
 | 649 |   // For each operand in the ordering | 
 | 650 |   for (unsigned i = 0, N = Ordering.size(); i < N; i++) { | 
 | 651 |     // Get the scheduling info | 
 | 652 |     ScheduleInfo *SI = Ordering[i]; | 
 | 653 |     // Get the operand | 
 | 654 |     SDOperand Op = SI->Op; | 
 | 655 |     // Emit the operand | 
 | 656 |     Emit(Op); | 
 | 657 |     // FIXME - PUNT - If Op defines a flag then it's users need to be emitted now | 
 | 658 |     if (isFlagDefiner(Op)) EmitFlagUsers(Op); | 
 | 659 |   } | 
 | 660 | } | 
 | 661 |  | 
 | 662 | /// EmitFlagUsers - Emit users of operands flag. | 
 | 663 | /// | 
 | 664 | void SimpleSched::EmitFlagUsers(SDOperand Op) { | 
 | 665 |   // Check each flag user | 
 | 666 |   FlagUserIterator FI(Op.Val); | 
 | 667 |   while (SDNode *User = FI.next()) { | 
 | 668 |     // Construct user node as operand | 
 | 669 |     SDOperand OpU(User, 0); | 
 | 670 |     // Emit  user node | 
 | 671 |     Emit(OpU); | 
 | 672 |     // If user defines a flag then it's users need to be emitted now | 
 | 673 |     if (isFlagDefiner(User)) EmitFlagUsers(OpU); | 
 | 674 |   } | 
 | 675 | } | 
 | 676 |  | 
 | 677 | /// CountResults - The results of target nodes have register or immediate | 
 | 678 | /// operands first, then an optional chain, and optional flag operands (which do | 
 | 679 | /// not go into the machine instrs.) | 
 | 680 | unsigned SimpleSched::CountResults(SDOperand Op) { | 
 | 681 |   unsigned N = Op.Val->getNumValues(); | 
 | 682 |   while (N && Op.Val->getValueType(N - 1) == MVT::Flag) | 
 | 683 |     --N; | 
 | 684 |   if (N && Op.Val->getValueType(N - 1) == MVT::Other) | 
 | 685 |     --N;    // Skip over chain result. | 
 | 686 |   return N; | 
 | 687 | } | 
 | 688 |  | 
 | 689 | /// CountOperands  The inputs to target nodes have any actual inputs first, | 
 | 690 | /// followed by an optional chain operand, then flag operands.  Compute the | 
 | 691 | /// number of actual operands that  will go into the machine instr. | 
 | 692 | unsigned SimpleSched::CountOperands(SDOperand Op) { | 
 | 693 |   unsigned N = Op.getNumOperands(); | 
 | 694 |   while (N && Op.getOperand(N - 1).getValueType() == MVT::Flag) | 
 | 695 |     --N; | 
 | 696 |   if (N && Op.getOperand(N - 1).getValueType() == MVT::Other) | 
 | 697 |     --N; // Ignore chain if it exists. | 
 | 698 |   return N; | 
 | 699 | } | 
 | 700 |  | 
 | 701 | /// CreateVirtualRegisters - Add result register values for things that are | 
 | 702 | /// defined by this instruction. | 
 | 703 | unsigned SimpleSched::CreateVirtualRegisters(SDOperand Op, MachineInstr *MI, | 
 | 704 |                                              unsigned NumResults, | 
 | 705 |                                              const TargetInstrDescriptor &II) { | 
 | 706 |   // Create the result registers for this node and add the result regs to | 
 | 707 |   // the machine instruction. | 
 | 708 |   const TargetOperandInfo *OpInfo = II.OpInfo; | 
 | 709 |   unsigned ResultReg = RegMap->createVirtualRegister(OpInfo[0].RegClass); | 
 | 710 |   MI->addRegOperand(ResultReg, MachineOperand::Def); | 
 | 711 |   for (unsigned i = 1; i != NumResults; ++i) { | 
 | 712 |     assert(OpInfo[i].RegClass && "Isn't a register operand!"); | 
 | 713 |     MI->addRegOperand(RegMap->createVirtualRegister(OpInfo[0].RegClass), | 
 | 714 |                       MachineOperand::Def); | 
 | 715 |   } | 
 | 716 |   return ResultReg; | 
 | 717 | } | 
 | 718 |  | 
 | 719 | /// Emit - Generate machine code for an operand and needed dependencies. | 
 | 720 | /// | 
 | 721 | unsigned SimpleSched::Emit(SDOperand Op) { | 
 | 722 |   std::map<SDNode *, unsigned>::iterator OpI = VRMap.lower_bound(Op.Val); | 
 | 723 |   if (OpI != VRMap.end() && OpI->first == Op.Val) | 
 | 724 |     return OpI->second + Op.ResNo; | 
 | 725 |   unsigned &OpSlot = VRMap.insert(OpI, std::make_pair(Op.Val, 0))->second; | 
| Chris Lattner | 2d973e4 | 2005-08-18 20:07:59 +0000 | [diff] [blame] | 726 |    | 
 | 727 |   unsigned ResultReg = 0; | 
 | 728 |   if (Op.isTargetOpcode()) { | 
 | 729 |     unsigned Opc = Op.getTargetOpcode(); | 
 | 730 |     const TargetInstrDescriptor &II = TII.get(Opc); | 
 | 731 |  | 
| Jim Laskey | e6b90fb | 2005-09-26 21:57:04 +0000 | [diff] [blame] | 732 |     unsigned NumResults = CountResults(Op); | 
 | 733 |     unsigned NodeOperands = CountOperands(Op); | 
 | 734 |     unsigned NumMIOperands = NodeOperands + NumResults; | 
| Chris Lattner | da8abb0 | 2005-09-01 18:44:10 +0000 | [diff] [blame] | 735 | #ifndef NDEBUG | 
| Chris Lattner | 14b392a | 2005-08-24 22:02:41 +0000 | [diff] [blame] | 736 |     assert((unsigned(II.numOperands) == NumMIOperands || II.numOperands == -1)&& | 
| Chris Lattner | 2d973e4 | 2005-08-18 20:07:59 +0000 | [diff] [blame] | 737 |            "#operands for dag node doesn't match .td file!");  | 
| Chris Lattner | ca6aa2f | 2005-08-19 01:01:34 +0000 | [diff] [blame] | 738 | #endif | 
| Chris Lattner | 2d973e4 | 2005-08-18 20:07:59 +0000 | [diff] [blame] | 739 |  | 
 | 740 |     // Create the new machine instruction. | 
| Chris Lattner | 14b392a | 2005-08-24 22:02:41 +0000 | [diff] [blame] | 741 |     MachineInstr *MI = new MachineInstr(Opc, NumMIOperands, true, true); | 
| Chris Lattner | 2d973e4 | 2005-08-18 20:07:59 +0000 | [diff] [blame] | 742 |      | 
 | 743 |     // Add result register values for things that are defined by this | 
 | 744 |     // instruction. | 
| Jim Laskey | e6b90fb | 2005-09-26 21:57:04 +0000 | [diff] [blame] | 745 |     if (NumResults) ResultReg = CreateVirtualRegisters(Op, MI, NumResults, II); | 
| Chris Lattner | 2d973e4 | 2005-08-18 20:07:59 +0000 | [diff] [blame] | 746 |      | 
| Chris Lattner | 82e14db | 2005-08-29 23:21:29 +0000 | [diff] [blame] | 747 |     // If there is a token chain operand, emit it first, as a hack to get avoid | 
 | 748 |     // really bad cases. | 
 | 749 |     if (Op.getNumOperands() > NodeOperands && | 
| Jim Laskey | e6b90fb | 2005-09-26 21:57:04 +0000 | [diff] [blame] | 750 |         Op.getOperand(NodeOperands).getValueType() == MVT::Other) { | 
| Chris Lattner | 82e14db | 2005-08-29 23:21:29 +0000 | [diff] [blame] | 751 |       Emit(Op.getOperand(NodeOperands)); | 
| Jim Laskey | e6b90fb | 2005-09-26 21:57:04 +0000 | [diff] [blame] | 752 |     } | 
| Chris Lattner | 82e14db | 2005-08-29 23:21:29 +0000 | [diff] [blame] | 753 |      | 
 | 754 |     // Emit all of the actual operands of this instruction, adding them to the | 
| Chris Lattner | 2d973e4 | 2005-08-18 20:07:59 +0000 | [diff] [blame] | 755 |     // instruction as appropriate. | 
| Chris Lattner | 82e14db | 2005-08-29 23:21:29 +0000 | [diff] [blame] | 756 |     for (unsigned i = 0; i != NodeOperands; ++i) { | 
| Chris Lattner | 23553cf | 2005-08-22 01:04:32 +0000 | [diff] [blame] | 757 |       if (Op.getOperand(i).isTargetOpcode()) { | 
 | 758 |         // Note that this case is redundant with the final else block, but we | 
 | 759 |         // include it because it is the most common and it makes the logic | 
 | 760 |         // simpler here. | 
| Chris Lattner | 82e14db | 2005-08-29 23:21:29 +0000 | [diff] [blame] | 761 |         assert(Op.getOperand(i).getValueType() != MVT::Other && | 
 | 762 |                Op.getOperand(i).getValueType() != MVT::Flag && | 
 | 763 |                "Chain and flag operands should occur at end of operand list!"); | 
 | 764 |          | 
 | 765 |         MI->addRegOperand(Emit(Op.getOperand(i)), MachineOperand::Use); | 
| Chris Lattner | 23553cf | 2005-08-22 01:04:32 +0000 | [diff] [blame] | 766 |       } else if (ConstantSDNode *C = | 
 | 767 |                                    dyn_cast<ConstantSDNode>(Op.getOperand(i))) { | 
| Chris Lattner | 2d973e4 | 2005-08-18 20:07:59 +0000 | [diff] [blame] | 768 |         MI->addZeroExtImm64Operand(C->getValue()); | 
 | 769 |       } else if (RegisterSDNode*R =dyn_cast<RegisterSDNode>(Op.getOperand(i))) { | 
 | 770 |         MI->addRegOperand(R->getReg(), MachineOperand::Use); | 
| Chris Lattner | 9b78db7 | 2005-08-19 22:38:24 +0000 | [diff] [blame] | 771 |       } else if (GlobalAddressSDNode *TGA = | 
 | 772 |                        dyn_cast<GlobalAddressSDNode>(Op.getOperand(i))) { | 
 | 773 |         MI->addGlobalAddressOperand(TGA->getGlobal(), false, 0); | 
| Chris Lattner | f85ab15 | 2005-08-21 18:49:29 +0000 | [diff] [blame] | 774 |       } else if (BasicBlockSDNode *BB = | 
 | 775 |                        dyn_cast<BasicBlockSDNode>(Op.getOperand(i))) { | 
 | 776 |         MI->addMachineBasicBlockOperand(BB->getBasicBlock()); | 
| Chris Lattner | 81e72b1 | 2005-08-21 19:56:04 +0000 | [diff] [blame] | 777 |       } else if (FrameIndexSDNode *FI = | 
 | 778 |                        dyn_cast<FrameIndexSDNode>(Op.getOperand(i))) { | 
 | 779 |         MI->addFrameIndexOperand(FI->getIndex()); | 
| Chris Lattner | 23553cf | 2005-08-22 01:04:32 +0000 | [diff] [blame] | 780 |       } else if (ConstantPoolSDNode *CP =  | 
 | 781 |                     dyn_cast<ConstantPoolSDNode>(Op.getOperand(i))) { | 
| Chris Lattner | 5839bf2 | 2005-08-26 17:15:30 +0000 | [diff] [blame] | 782 |         unsigned Idx = ConstPool->getConstantPoolIndex(CP->get()); | 
 | 783 |         MI->addConstantPoolIndexOperand(Idx); | 
| Chris Lattner | 14b392a | 2005-08-24 22:02:41 +0000 | [diff] [blame] | 784 |       } else if (ExternalSymbolSDNode *ES =  | 
 | 785 |                  dyn_cast<ExternalSymbolSDNode>(Op.getOperand(i))) { | 
 | 786 |         MI->addExternalSymbolOperand(ES->getSymbol(), false); | 
| Chris Lattner | 2d973e4 | 2005-08-18 20:07:59 +0000 | [diff] [blame] | 787 |       } else { | 
| Chris Lattner | 82e14db | 2005-08-29 23:21:29 +0000 | [diff] [blame] | 788 |         assert(Op.getOperand(i).getValueType() != MVT::Other && | 
 | 789 |                Op.getOperand(i).getValueType() != MVT::Flag && | 
 | 790 |                "Chain and flag operands should occur at end of operand list!"); | 
 | 791 |         MI->addRegOperand(Emit(Op.getOperand(i)), MachineOperand::Use); | 
| Chris Lattner | 2d973e4 | 2005-08-18 20:07:59 +0000 | [diff] [blame] | 792 |       } | 
 | 793 |     } | 
 | 794 |  | 
| Chris Lattner | 82e14db | 2005-08-29 23:21:29 +0000 | [diff] [blame] | 795 |     // Finally, if this node has any flag operands, we *must* emit them last, to | 
 | 796 |     // avoid emitting operations that might clobber the flags. | 
 | 797 |     if (Op.getNumOperands() > NodeOperands) { | 
 | 798 |       unsigned i = NodeOperands; | 
 | 799 |       if (Op.getOperand(i).getValueType() == MVT::Other) | 
 | 800 |         ++i;  // the chain is already selected. | 
| Jim Laskey | e6b90fb | 2005-09-26 21:57:04 +0000 | [diff] [blame] | 801 |       for (unsigned N = Op.getNumOperands(); i < N; i++) { | 
| Chris Lattner | 82e14db | 2005-08-29 23:21:29 +0000 | [diff] [blame] | 802 |         assert(Op.getOperand(i).getValueType() == MVT::Flag && | 
 | 803 |                "Must be flag operands!"); | 
 | 804 |         Emit(Op.getOperand(i)); | 
 | 805 |       } | 
 | 806 |     } | 
 | 807 |      | 
| Chris Lattner | 2d973e4 | 2005-08-18 20:07:59 +0000 | [diff] [blame] | 808 |     // Now that we have emitted all operands, emit this instruction itself. | 
| Chris Lattner | 025c39b | 2005-08-26 20:54:47 +0000 | [diff] [blame] | 809 |     if ((II.Flags & M_USES_CUSTOM_DAG_SCHED_INSERTION) == 0) { | 
 | 810 |       BB->insert(BB->end(), MI); | 
 | 811 |     } else { | 
 | 812 |       // Insert this instruction into the end of the basic block, potentially | 
 | 813 |       // taking some custom action. | 
 | 814 |       BB = DAG.getTargetLoweringInfo().InsertAtEndOfBasicBlock(MI, BB); | 
 | 815 |     } | 
| Chris Lattner | 2d973e4 | 2005-08-18 20:07:59 +0000 | [diff] [blame] | 816 |   } else { | 
 | 817 |     switch (Op.getOpcode()) { | 
| Chris Lattner | ca6aa2f | 2005-08-19 01:01:34 +0000 | [diff] [blame] | 818 |     default: | 
 | 819 |       Op.Val->dump();  | 
 | 820 |       assert(0 && "This target-independent node should have been selected!"); | 
| Chris Lattner | 81e72b1 | 2005-08-21 19:56:04 +0000 | [diff] [blame] | 821 |     case ISD::EntryToken: break; | 
| Chris Lattner | 7ef3304 | 2005-08-19 21:43:53 +0000 | [diff] [blame] | 822 |     case ISD::TokenFactor: | 
| Jim Laskey | e6b90fb | 2005-09-26 21:57:04 +0000 | [diff] [blame] | 823 |       for (unsigned i = 0, N = Op.getNumOperands(); i < N; i++) { | 
| Chris Lattner | 7ef3304 | 2005-08-19 21:43:53 +0000 | [diff] [blame] | 824 |         Emit(Op.getOperand(i)); | 
| Jim Laskey | e6b90fb | 2005-09-26 21:57:04 +0000 | [diff] [blame] | 825 |       } | 
| Chris Lattner | 7ef3304 | 2005-08-19 21:43:53 +0000 | [diff] [blame] | 826 |       break; | 
| Chris Lattner | ca6aa2f | 2005-08-19 01:01:34 +0000 | [diff] [blame] | 827 |     case ISD::CopyToReg: { | 
| Chris Lattner | f155635 | 2005-08-30 01:58:51 +0000 | [diff] [blame] | 828 |       SDOperand FlagOp; | 
| Jim Laskey | e6b90fb | 2005-09-26 21:57:04 +0000 | [diff] [blame] | 829 |       if (Op.getNumOperands() == 4) { | 
| Chris Lattner | f155635 | 2005-08-30 01:58:51 +0000 | [diff] [blame] | 830 |         FlagOp = Op.getOperand(3); | 
| Jim Laskey | e6b90fb | 2005-09-26 21:57:04 +0000 | [diff] [blame] | 831 |       } | 
 | 832 |       if (Op.getOperand(0).Val != FlagOp.Val) { | 
| Chris Lattner | 55334fc | 2005-08-30 01:57:23 +0000 | [diff] [blame] | 833 |         Emit(Op.getOperand(0));   // Emit the chain. | 
| Jim Laskey | e6b90fb | 2005-09-26 21:57:04 +0000 | [diff] [blame] | 834 |       } | 
| Chris Lattner | ca6aa2f | 2005-08-19 01:01:34 +0000 | [diff] [blame] | 835 |       unsigned Val = Emit(Op.getOperand(2)); | 
| Jim Laskey | e6b90fb | 2005-09-26 21:57:04 +0000 | [diff] [blame] | 836 |       if (FlagOp.Val) { | 
 | 837 |         Emit(FlagOp); | 
 | 838 |       } | 
| Chris Lattner | 0189197 | 2005-08-19 20:50:53 +0000 | [diff] [blame] | 839 |       MRI.copyRegToReg(*BB, BB->end(), | 
 | 840 |                        cast<RegisterSDNode>(Op.getOperand(1))->getReg(), Val, | 
 | 841 |                        RegMap->getRegClass(Val)); | 
| Chris Lattner | ca6aa2f | 2005-08-19 01:01:34 +0000 | [diff] [blame] | 842 |       break; | 
 | 843 |     } | 
| Chris Lattner | 7ef3304 | 2005-08-19 21:43:53 +0000 | [diff] [blame] | 844 |     case ISD::CopyFromReg: { | 
 | 845 |       Emit(Op.getOperand(0));   // Emit the chain. | 
 | 846 |       unsigned SrcReg = cast<RegisterSDNode>(Op.getOperand(1))->getReg(); | 
 | 847 |        | 
 | 848 |       // Figure out the register class to create for the destreg. | 
| Chris Lattner | fe0c2c8 | 2005-08-20 18:07:27 +0000 | [diff] [blame] | 849 |       const TargetRegisterClass *TRC = 0; | 
| Chris Lattner | 7ef3304 | 2005-08-19 21:43:53 +0000 | [diff] [blame] | 850 |       if (MRegisterInfo::isVirtualRegister(SrcReg)) { | 
 | 851 |         TRC = RegMap->getRegClass(SrcReg); | 
 | 852 |       } else { | 
 | 853 |         // FIXME: we don't know what register class to generate this for.  Do | 
 | 854 |         // a brute force search and pick the first match. :( | 
 | 855 |         for (MRegisterInfo::regclass_iterator I = MRI.regclass_begin(), | 
 | 856 |                E = MRI.regclass_end(); I != E; ++I) | 
 | 857 |           if ((*I)->contains(SrcReg)) { | 
 | 858 |             TRC = *I; | 
 | 859 |             break; | 
 | 860 |           } | 
 | 861 |         assert(TRC && "Couldn't find register class for reg copy!"); | 
 | 862 |       } | 
 | 863 |        | 
 | 864 |       // Create the reg, emit the copy. | 
 | 865 |       ResultReg = RegMap->createVirtualRegister(TRC); | 
 | 866 |       MRI.copyRegToReg(*BB, BB->end(), ResultReg, SrcReg, TRC); | 
 | 867 |       break; | 
 | 868 |     } | 
| Chris Lattner | 2d973e4 | 2005-08-18 20:07:59 +0000 | [diff] [blame] | 869 |     } | 
 | 870 |   } | 
| Jim Laskey | e6b90fb | 2005-09-26 21:57:04 +0000 | [diff] [blame] | 871 |  | 
 | 872 |   OpSlot = ResultReg; | 
| Chris Lattner | 2d973e4 | 2005-08-18 20:07:59 +0000 | [diff] [blame] | 873 |   return ResultReg+Op.ResNo; | 
 | 874 | } | 
 | 875 |  | 
| Jim Laskey | e6b90fb | 2005-09-26 21:57:04 +0000 | [diff] [blame] | 876 | /// Schedule - Order operands according to selected style. | 
 | 877 | /// | 
 | 878 | void SimpleSched::Schedule() { | 
 | 879 |   switch (ScheduleStyle) { | 
 | 880 |   case simpleScheduling: | 
 | 881 |     // Breadth first walk of DAG | 
 | 882 |     VisitAll(); | 
 | 883 |     // Get latency and resource requirements | 
 | 884 |     GatherOperandInfo(); | 
 | 885 |     // Don't waste time if is only entry and return | 
 | 886 |     if (Operands.size() > 2) { | 
 | 887 |       DEBUG(dump("Pre-")); | 
 | 888 |       // Push back long instructions and critical path | 
 | 889 |       ScheduleBackward(); | 
 | 890 |       DEBUG(dump("Mid-")); | 
 | 891 |       // Pack instructions to maximize resource utilization | 
 | 892 |       ScheduleForward(); | 
 | 893 |       DEBUG(dump("Post-")); | 
 | 894 |       // Emit in scheduled order | 
 | 895 |       EmitAll(); | 
 | 896 |       break; | 
 | 897 |     } // fall thru | 
 | 898 |   case noScheduling: | 
 | 899 |     // Emit instructions in using a DFS from the exit root | 
 | 900 |     Emit(DAG.getRoot()); | 
 | 901 |     break; | 
 | 902 |   } | 
 | 903 | } | 
| Chris Lattner | 2d973e4 | 2005-08-18 20:07:59 +0000 | [diff] [blame] | 904 |  | 
| Jim Laskey | e6b90fb | 2005-09-26 21:57:04 +0000 | [diff] [blame] | 905 | /// printSI - Print schedule info. | 
 | 906 | /// | 
 | 907 | void SimpleSched::printSI(std::ostream &O, ScheduleInfo *SI) const { | 
 | 908 | #ifndef NDEBUG | 
 | 909 |   using namespace std; | 
 | 910 |   SDOperand Op = SI->Op; | 
 | 911 |   O << " " | 
 | 912 |     << hex << Op.Val | 
 | 913 |     << ", RS=" << SI->ResourceSet | 
 | 914 |     << ", Lat=" << SI->Latency | 
 | 915 |     << ", Slot=" << SI->Slot | 
 | 916 |     << ", ARITY=(" << Op.getNumOperands() << "," | 
 | 917 |                    << Op.Val->getNumValues() << ")" | 
 | 918 |     << " " << Op.Val->getOperationName(&DAG); | 
 | 919 |   if (isFlagDefiner(Op)) O << "<#"; | 
 | 920 |   if (isFlagUser(Op)) O << ">#"; | 
 | 921 | #endif | 
 | 922 | } | 
 | 923 |  | 
 | 924 | /// print - Print ordering to specified output stream. | 
 | 925 | /// | 
 | 926 | void SimpleSched::print(std::ostream &O) const { | 
 | 927 | #ifndef NDEBUG | 
 | 928 |   using namespace std; | 
 | 929 |   O << "Ordering\n"; | 
 | 930 |   for (unsigned i = 0, N = Ordering.size(); i < N; i++) { | 
 | 931 |     printSI(O, Ordering[i]); | 
 | 932 |     O << "\n"; | 
 | 933 |   } | 
 | 934 | #endif | 
 | 935 | } | 
 | 936 |  | 
 | 937 | /// dump - Print ordering to std::cerr. | 
 | 938 | /// | 
 | 939 | void SimpleSched::dump() const { | 
 | 940 |   print(std::cerr); | 
 | 941 | } | 
 | 942 | //===----------------------------------------------------------------------===// | 
 | 943 |  | 
 | 944 |  | 
 | 945 | //===----------------------------------------------------------------------===// | 
 | 946 | /// ScheduleAndEmitDAG - Pick a safe ordering and emit instructions for each | 
 | 947 | /// target node in the graph. | 
| Chris Lattner | d32b236 | 2005-08-18 18:45:24 +0000 | [diff] [blame] | 948 | void SelectionDAGISel::ScheduleAndEmitDAG(SelectionDAG &SD) { | 
| Chris Lattner | 068ca15 | 2005-08-18 20:11:49 +0000 | [diff] [blame] | 949 |   if (ViewDAGs) SD.viewGraph(); | 
| Chris Lattner | 620c93c | 2005-08-27 00:58:02 +0000 | [diff] [blame] | 950 |   BB = SimpleSched(SD, BB).Run();   | 
| Chris Lattner | d32b236 | 2005-08-18 18:45:24 +0000 | [diff] [blame] | 951 | } |