1ab8be96fd30ca9396e6b84fdddf1ac6208984cadEvan Cheng//===-- llvm/MC/MCInstrItineraries.h - Scheduling ---------------*- C++ -*-===//
215517be4bfc7ec7e60ecb25dc0bb46de27d3de37Jim Laskey//
315517be4bfc7ec7e60ecb25dc0bb46de27d3de37Jim Laskey//                     The LLVM Compiler Infrastructure
415517be4bfc7ec7e60ecb25dc0bb46de27d3de37Jim Laskey//
57ed47a13356daed2a34cd2209a31f92552e3bdd8Chris Lattner// This file is distributed under the University of Illinois Open Source
67ed47a13356daed2a34cd2209a31f92552e3bdd8Chris Lattner// License. See LICENSE.TXT for details.
715517be4bfc7ec7e60ecb25dc0bb46de27d3de37Jim Laskey//
815517be4bfc7ec7e60ecb25dc0bb46de27d3de37Jim Laskey//===----------------------------------------------------------------------===//
915517be4bfc7ec7e60ecb25dc0bb46de27d3de37Jim Laskey//
10fac8541dd40e01aa2b52962516f9ae67c99720ccDavid Goodwin// This file describes the structures used for instruction
11fac8541dd40e01aa2b52962516f9ae67c99720ccDavid Goodwin// itineraries, stages, and operand reads/writes.  This is used by
12fac8541dd40e01aa2b52962516f9ae67c99720ccDavid Goodwin// schedulers to determine instruction stages and latencies.
1315517be4bfc7ec7e60ecb25dc0bb46de27d3de37Jim Laskey//
1415517be4bfc7ec7e60ecb25dc0bb46de27d3de37Jim Laskey//===----------------------------------------------------------------------===//
1515517be4bfc7ec7e60ecb25dc0bb46de27d3de37Jim Laskey
16ab8be96fd30ca9396e6b84fdddf1ac6208984cadEvan Cheng#ifndef LLVM_MC_MCINSTRITINERARIES_H
17ab8be96fd30ca9396e6b84fdddf1ac6208984cadEvan Cheng#define LLVM_MC_MCINSTRITINERARIES_H
1815517be4bfc7ec7e60ecb25dc0bb46de27d3de37Jim Laskey
192661b411ccc81b1fe19194d3f43b2630cbef3f28Andrew Trick#include "llvm/MC/MCSchedule.h"
201a8f36e3ce5b9c230781b66600c81536128abfb5David Goodwin#include <algorithm>
211a8f36e3ce5b9c230781b66600c81536128abfb5David Goodwin
2215517be4bfc7ec7e60ecb25dc0bb46de27d3de37Jim Laskeynamespace llvm {
2315517be4bfc7ec7e60ecb25dc0bb46de27d3de37Jim Laskey
2415517be4bfc7ec7e60ecb25dc0bb46de27d3de37Jim Laskey//===----------------------------------------------------------------------===//
251a8f36e3ce5b9c230781b66600c81536128abfb5David Goodwin/// Instruction stage - These values represent a non-pipelined step in
261a8f36e3ce5b9c230781b66600c81536128abfb5David Goodwin/// the execution of an instruction.  Cycles represents the number of
271a8f36e3ce5b9c230781b66600c81536128abfb5David Goodwin/// discrete time slots needed to complete the stage.  Units represent
281a8f36e3ce5b9c230781b66600c81536128abfb5David Goodwin/// the choice of functional units that can be used to complete the
291a8f36e3ce5b9c230781b66600c81536128abfb5David Goodwin/// stage.  Eg. IntUnit1, IntUnit2. NextCycles indicates how many
301a8f36e3ce5b9c230781b66600c81536128abfb5David Goodwin/// cycles should elapse from the start of this stage to the start of
311a8f36e3ce5b9c230781b66600c81536128abfb5David Goodwin/// the next stage in the itinerary. A value of -1 indicates that the
321a8f36e3ce5b9c230781b66600c81536128abfb5David Goodwin/// next stage should start immediately after the current one.
331a8f36e3ce5b9c230781b66600c81536128abfb5David Goodwin/// For example:
341a8f36e3ce5b9c230781b66600c81536128abfb5David Goodwin///
351a8f36e3ce5b9c230781b66600c81536128abfb5David Goodwin///   { 1, x, -1 }
361a8f36e3ce5b9c230781b66600c81536128abfb5David Goodwin///      indicates that the stage occupies FU x for 1 cycle and that
371a8f36e3ce5b9c230781b66600c81536128abfb5David Goodwin///      the next stage starts immediately after this one.
381a8f36e3ce5b9c230781b66600c81536128abfb5David Goodwin///
391a8f36e3ce5b9c230781b66600c81536128abfb5David Goodwin///   { 2, x|y, 1 }
401a8f36e3ce5b9c230781b66600c81536128abfb5David Goodwin///      indicates that the stage occupies either FU x or FU y for 2
411a8f36e3ce5b9c230781b66600c81536128abfb5David Goodwin///      consecuative cycles and that the next stage starts one cycle
421a8f36e3ce5b9c230781b66600c81536128abfb5David Goodwin///      after this stage starts. That is, the stage requirements
431a8f36e3ce5b9c230781b66600c81536128abfb5David Goodwin///      overlap in time.
441a8f36e3ce5b9c230781b66600c81536128abfb5David Goodwin///
451a8f36e3ce5b9c230781b66600c81536128abfb5David Goodwin///   { 1, x, 0 }
461a8f36e3ce5b9c230781b66600c81536128abfb5David Goodwin///      indicates that the stage occupies FU x for 1 cycle and that
471a8f36e3ce5b9c230781b66600c81536128abfb5David Goodwin///      the next stage starts in this same cycle. This can be used to
481a8f36e3ce5b9c230781b66600c81536128abfb5David Goodwin///      indicate that the instruction requires multiple stages at the
491a8f36e3ce5b9c230781b66600c81536128abfb5David Goodwin///      same time.
5033372a18c5e2d66c2b9c4cbe7361cce197bc63b5Dan Gohman///
5196085a36dbb9cf251c81bc150e41ea9c952c99c0Anton Korobeynikov/// FU reservation can be of two different kinds:
5296085a36dbb9cf251c81bc150e41ea9c952c99c0Anton Korobeynikov///  - FUs which instruction actually requires
5396085a36dbb9cf251c81bc150e41ea9c952c99c0Anton Korobeynikov///  - FUs which instruction just reserves. Reserved unit is not available for
5496085a36dbb9cf251c81bc150e41ea9c952c99c0Anton Korobeynikov///    execution of other instruction. However, several instructions can reserve
5596085a36dbb9cf251c81bc150e41ea9c952c99c0Anton Korobeynikov///    the same unit several times.
5696085a36dbb9cf251c81bc150e41ea9c952c99c0Anton Korobeynikov/// Such two types of units reservation is used to model instruction domain
5796085a36dbb9cf251c81bc150e41ea9c952c99c0Anton Korobeynikov/// change stalls, FUs using the same resource (e.g. same register file), etc.
5896085a36dbb9cf251c81bc150e41ea9c952c99c0Anton Korobeynikov
5915517be4bfc7ec7e60ecb25dc0bb46de27d3de37Jim Laskeystruct InstrStage {
6096085a36dbb9cf251c81bc150e41ea9c952c99c0Anton Korobeynikov  enum ReservationKinds {
6196085a36dbb9cf251c81bc150e41ea9c952c99c0Anton Korobeynikov    Required = 0,
6296085a36dbb9cf251c81bc150e41ea9c952c99c0Anton Korobeynikov    Reserved = 1
6396085a36dbb9cf251c81bc150e41ea9c952c99c0Anton Korobeynikov  };
6496085a36dbb9cf251c81bc150e41ea9c952c99c0Anton Korobeynikov
651a8f36e3ce5b9c230781b66600c81536128abfb5David Goodwin  unsigned Cycles_;  ///< Length of stage in machine cycles
66b460a3382961c5be9952a75d46228f624edbd39fHal Finkel  unsigned Units_;   ///< Choice of functional units
6796085a36dbb9cf251c81bc150e41ea9c952c99c0Anton Korobeynikov  int NextCycles_;   ///< Number of machine cycles to next stage
6896085a36dbb9cf251c81bc150e41ea9c952c99c0Anton Korobeynikov  ReservationKinds Kind_; ///< Kind of the FU reservation
691a8f36e3ce5b9c230781b66600c81536128abfb5David Goodwin
701a8f36e3ce5b9c230781b66600c81536128abfb5David Goodwin  /// getCycles - returns the number of cycles the stage is occupied
711a8f36e3ce5b9c230781b66600c81536128abfb5David Goodwin  unsigned getCycles() const {
721a8f36e3ce5b9c230781b66600c81536128abfb5David Goodwin    return Cycles_;
731a8f36e3ce5b9c230781b66600c81536128abfb5David Goodwin  }
741a8f36e3ce5b9c230781b66600c81536128abfb5David Goodwin
751a8f36e3ce5b9c230781b66600c81536128abfb5David Goodwin  /// getUnits - returns the choice of FUs
76b460a3382961c5be9952a75d46228f624edbd39fHal Finkel  unsigned getUnits() const {
771a8f36e3ce5b9c230781b66600c81536128abfb5David Goodwin    return Units_;
781a8f36e3ce5b9c230781b66600c81536128abfb5David Goodwin  }
791a8f36e3ce5b9c230781b66600c81536128abfb5David Goodwin
8096085a36dbb9cf251c81bc150e41ea9c952c99c0Anton Korobeynikov  ReservationKinds getReservationKind() const {
8196085a36dbb9cf251c81bc150e41ea9c952c99c0Anton Korobeynikov    return Kind_;
8296085a36dbb9cf251c81bc150e41ea9c952c99c0Anton Korobeynikov  }
8396085a36dbb9cf251c81bc150e41ea9c952c99c0Anton Korobeynikov
841a8f36e3ce5b9c230781b66600c81536128abfb5David Goodwin  /// getNextCycles - returns the number of cycles from the start of
851a8f36e3ce5b9c230781b66600c81536128abfb5David Goodwin  /// this stage to the start of the next stage in the itinerary
861a8f36e3ce5b9c230781b66600c81536128abfb5David Goodwin  unsigned getNextCycles() const {
871a8f36e3ce5b9c230781b66600c81536128abfb5David Goodwin    return (NextCycles_ >= 0) ? (unsigned)NextCycles_ : Cycles_;
881a8f36e3ce5b9c230781b66600c81536128abfb5David Goodwin  }
8915517be4bfc7ec7e60ecb25dc0bb46de27d3de37Jim Laskey};
9015517be4bfc7ec7e60ecb25dc0bb46de27d3de37Jim Laskey
9115517be4bfc7ec7e60ecb25dc0bb46de27d3de37Jim Laskey
9215517be4bfc7ec7e60ecb25dc0bb46de27d3de37Jim Laskey//===----------------------------------------------------------------------===//
93fac8541dd40e01aa2b52962516f9ae67c99720ccDavid Goodwin/// Instruction itinerary - An itinerary represents the scheduling
94fac8541dd40e01aa2b52962516f9ae67c99720ccDavid Goodwin/// information for an instruction. This includes a set of stages
95fac8541dd40e01aa2b52962516f9ae67c99720ccDavid Goodwin/// occupies by the instruction, and the pipeline cycle in which
96fac8541dd40e01aa2b52962516f9ae67c99720ccDavid Goodwin/// operands are read and written.
9733372a18c5e2d66c2b9c4cbe7361cce197bc63b5Dan Gohman///
9815517be4bfc7ec7e60ecb25dc0bb46de27d3de37Jim Laskeystruct InstrItinerary {
99218ee74a011c0d350099c452810da0bd57a15047Andrew Trick  int      NumMicroOps;        ///< # of micro-ops, -1 means it's variable
100fac8541dd40e01aa2b52962516f9ae67c99720ccDavid Goodwin  unsigned FirstStage;         ///< Index of first stage in itinerary
101fac8541dd40e01aa2b52962516f9ae67c99720ccDavid Goodwin  unsigned LastStage;          ///< Index of last + 1 stage in itinerary
102fac8541dd40e01aa2b52962516f9ae67c99720ccDavid Goodwin  unsigned FirstOperandCycle;  ///< Index of first operand rd/wr
103fac8541dd40e01aa2b52962516f9ae67c99720ccDavid Goodwin  unsigned LastOperandCycle;   ///< Index of last + 1 operand rd/wr
10415517be4bfc7ec7e60ecb25dc0bb46de27d3de37Jim Laskey};
10515517be4bfc7ec7e60ecb25dc0bb46de27d3de37Jim Laskey
10615517be4bfc7ec7e60ecb25dc0bb46de27d3de37Jim Laskey
1076cee630070b1a7183ed56a8404e812629f5ca538Jim Laskey//===----------------------------------------------------------------------===//
10833372a18c5e2d66c2b9c4cbe7361cce197bc63b5Dan Gohman/// Instruction itinerary Data - Itinerary data supplied by a subtarget to be
10933372a18c5e2d66c2b9c4cbe7361cce197bc63b5Dan Gohman/// used by a target.
11033372a18c5e2d66c2b9c4cbe7361cce197bc63b5Dan Gohman///
1115ca96988b01c4c332a7e4a6dba40949c1766465fChris Lattnerclass InstrItineraryData {
1125ca96988b01c4c332a7e4a6dba40949c1766465fChris Lattnerpublic:
1132661b411ccc81b1fe19194d3f43b2630cbef3f28Andrew Trick  const MCSchedModel   *SchedModel;     ///< Basic machine properties.
11433372a18c5e2d66c2b9c4cbe7361cce197bc63b5Dan Gohman  const InstrStage     *Stages;         ///< Array of stages selected
115fac8541dd40e01aa2b52962516f9ae67c99720ccDavid Goodwin  const unsigned       *OperandCycles;  ///< Array of operand cycles selected
1163881cb7a5d54c0011b40997adcd742e1c7b91abdEvan Cheng  const unsigned       *Forwardings;    ///< Array of pipeline forwarding pathes
117064312de8641043b084603aa9a6b409bc794eed2Bob Wilson  const InstrItinerary *Itineraries;    ///< Array of itineraries selected
1186cee630070b1a7183ed56a8404e812629f5ca538Jim Laskey
11933372a18c5e2d66c2b9c4cbe7361cce197bc63b5Dan Gohman  /// Ctors.
12033372a18c5e2d66c2b9c4cbe7361cce197bc63b5Dan Gohman  ///
1212661b411ccc81b1fe19194d3f43b2630cbef3f28Andrew Trick  InstrItineraryData() : SchedModel(&MCSchedModel::DefaultSchedModel),
122dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines                         Stages(nullptr), OperandCycles(nullptr),
123dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines                         Forwardings(nullptr), Itineraries(nullptr) {}
1242da8bc8a5f7705ac131184cd247f48500da0d74eAndrew Trick
1252661b411ccc81b1fe19194d3f43b2630cbef3f28Andrew Trick  InstrItineraryData(const MCSchedModel *SM, const InstrStage *S,
1262661b411ccc81b1fe19194d3f43b2630cbef3f28Andrew Trick                     const unsigned *OS, const unsigned *F)
1272661b411ccc81b1fe19194d3f43b2630cbef3f28Andrew Trick    : SchedModel(SM), Stages(S), OperandCycles(OS), Forwardings(F),
1282661b411ccc81b1fe19194d3f43b2630cbef3f28Andrew Trick      Itineraries(SchedModel->InstrItineraries) {}
1296e8f4c404825b79f9b9176483653f1aa927dfbdeAndrew Trick
13033372a18c5e2d66c2b9c4cbe7361cce197bc63b5Dan Gohman  /// isEmpty - Returns true if there are no itineraries.
13133372a18c5e2d66c2b9c4cbe7361cce197bc63b5Dan Gohman  ///
132dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  bool isEmpty() const { return Itineraries == nullptr; }
133dc4bdcdef1c8dd1a28b82deb08df039e5c0ffc5aDavid Goodwin
1341f528956921561f277a8c697e0202ac1e9a9c1d5David Goodwin  /// isEndMarker - Returns true if the index is for the end marker
1351f528956921561f277a8c697e0202ac1e9a9c1d5David Goodwin  /// itinerary.
1361f528956921561f277a8c697e0202ac1e9a9c1d5David Goodwin  ///
1371f528956921561f277a8c697e0202ac1e9a9c1d5David Goodwin  bool isEndMarker(unsigned ItinClassIndx) const {
138064312de8641043b084603aa9a6b409bc794eed2Bob Wilson    return ((Itineraries[ItinClassIndx].FirstStage == ~0U) &&
139064312de8641043b084603aa9a6b409bc794eed2Bob Wilson            (Itineraries[ItinClassIndx].LastStage == ~0U));
1401f528956921561f277a8c697e0202ac1e9a9c1d5David Goodwin  }
1411f528956921561f277a8c697e0202ac1e9a9c1d5David Goodwin
142fac8541dd40e01aa2b52962516f9ae67c99720ccDavid Goodwin  /// beginStage - Return the first stage of the itinerary.
1436e8f4c404825b79f9b9176483653f1aa927dfbdeAndrew Trick  ///
144fac8541dd40e01aa2b52962516f9ae67c99720ccDavid Goodwin  const InstrStage *beginStage(unsigned ItinClassIndx) const {
145064312de8641043b084603aa9a6b409bc794eed2Bob Wilson    unsigned StageIdx = Itineraries[ItinClassIndx].FirstStage;
1466cee630070b1a7183ed56a8404e812629f5ca538Jim Laskey    return Stages + StageIdx;
1476cee630070b1a7183ed56a8404e812629f5ca538Jim Laskey  }
1486cee630070b1a7183ed56a8404e812629f5ca538Jim Laskey
149fac8541dd40e01aa2b52962516f9ae67c99720ccDavid Goodwin  /// endStage - Return the last+1 stage of the itinerary.
1506e8f4c404825b79f9b9176483653f1aa927dfbdeAndrew Trick  ///
151fac8541dd40e01aa2b52962516f9ae67c99720ccDavid Goodwin  const InstrStage *endStage(unsigned ItinClassIndx) const {
152064312de8641043b084603aa9a6b409bc794eed2Bob Wilson    unsigned StageIdx = Itineraries[ItinClassIndx].LastStage;
1536cee630070b1a7183ed56a8404e812629f5ca538Jim Laskey    return Stages + StageIdx;
1546cee630070b1a7183ed56a8404e812629f5ca538Jim Laskey  }
155c8c2827993204207ca70a93f62f233fbe81b97efDan Gohman
156dc4bdcdef1c8dd1a28b82deb08df039e5c0ffc5aDavid Goodwin  /// getStageLatency - Return the total stage latency of the given
157dc4bdcdef1c8dd1a28b82deb08df039e5c0ffc5aDavid Goodwin  /// class.  The latency is the maximum completion time for any stage
158dc4bdcdef1c8dd1a28b82deb08df039e5c0ffc5aDavid Goodwin  /// in the itinerary.
159c8c2827993204207ca70a93f62f233fbe81b97efDan Gohman  ///
160b86a0cdb674549d8493043331cecd9cbf53b80daAndrew Trick  /// If no stages exist, it defaults to one cycle.
161dc4bdcdef1c8dd1a28b82deb08df039e5c0ffc5aDavid Goodwin  unsigned getStageLatency(unsigned ItinClassIndx) const {
16287896d9368e08d93493427ce7bf8272d1e5cca35Andrew Trick    // If the target doesn't provide itinerary information, use a simple
1632661b411ccc81b1fe19194d3f43b2630cbef3f28Andrew Trick    // non-zero default value for all instructions.
1642661b411ccc81b1fe19194d3f43b2630cbef3f28Andrew Trick    if (isEmpty())
165b86a0cdb674549d8493043331cecd9cbf53b80daAndrew Trick      return 1;
166c8c2827993204207ca70a93f62f233fbe81b97efDan Gohman
167dc4bdcdef1c8dd1a28b82deb08df039e5c0ffc5aDavid Goodwin    // Calculate the maximum completion time for any stage.
1681a8f36e3ce5b9c230781b66600c81536128abfb5David Goodwin    unsigned Latency = 0, StartCycle = 0;
169fac8541dd40e01aa2b52962516f9ae67c99720ccDavid Goodwin    for (const InstrStage *IS = beginStage(ItinClassIndx),
170fac8541dd40e01aa2b52962516f9ae67c99720ccDavid Goodwin           *E = endStage(ItinClassIndx); IS != E; ++IS) {
1711a8f36e3ce5b9c230781b66600c81536128abfb5David Goodwin      Latency = std::max(Latency, StartCycle + IS->getCycles());
1721a8f36e3ce5b9c230781b66600c81536128abfb5David Goodwin      StartCycle += IS->getNextCycles();
1731a8f36e3ce5b9c230781b66600c81536128abfb5David Goodwin    }
174c8c2827993204207ca70a93f62f233fbe81b97efDan Gohman    return Latency;
175c8c2827993204207ca70a93f62f233fbe81b97efDan Gohman  }
176dc4bdcdef1c8dd1a28b82deb08df039e5c0ffc5aDavid Goodwin
177dc4bdcdef1c8dd1a28b82deb08df039e5c0ffc5aDavid Goodwin  /// getOperandCycle - Return the cycle for the given class and
178dc4bdcdef1c8dd1a28b82deb08df039e5c0ffc5aDavid Goodwin  /// operand. Return -1 if no cycle is specified for the operand.
179dc4bdcdef1c8dd1a28b82deb08df039e5c0ffc5aDavid Goodwin  ///
180dc4bdcdef1c8dd1a28b82deb08df039e5c0ffc5aDavid Goodwin  int getOperandCycle(unsigned ItinClassIndx, unsigned OperandIdx) const {
181dc4bdcdef1c8dd1a28b82deb08df039e5c0ffc5aDavid Goodwin    if (isEmpty())
182dc4bdcdef1c8dd1a28b82deb08df039e5c0ffc5aDavid Goodwin      return -1;
183dc4bdcdef1c8dd1a28b82deb08df039e5c0ffc5aDavid Goodwin
184064312de8641043b084603aa9a6b409bc794eed2Bob Wilson    unsigned FirstIdx = Itineraries[ItinClassIndx].FirstOperandCycle;
185064312de8641043b084603aa9a6b409bc794eed2Bob Wilson    unsigned LastIdx = Itineraries[ItinClassIndx].LastOperandCycle;
186dc4bdcdef1c8dd1a28b82deb08df039e5c0ffc5aDavid Goodwin    if ((FirstIdx + OperandIdx) >= LastIdx)
187dc4bdcdef1c8dd1a28b82deb08df039e5c0ffc5aDavid Goodwin      return -1;
188dc4bdcdef1c8dd1a28b82deb08df039e5c0ffc5aDavid Goodwin
189dc4bdcdef1c8dd1a28b82deb08df039e5c0ffc5aDavid Goodwin    return (int)OperandCycles[FirstIdx + OperandIdx];
190dc4bdcdef1c8dd1a28b82deb08df039e5c0ffc5aDavid Goodwin  }
1913ef1c8759a20167457eb7fd82ebcaffe7ccaa1d1Evan Cheng
1923881cb7a5d54c0011b40997adcd742e1c7b91abdEvan Cheng  /// hasPipelineForwarding - Return true if there is a pipeline forwarding
1933881cb7a5d54c0011b40997adcd742e1c7b91abdEvan Cheng  /// between instructions of itinerary classes DefClass and UseClasses so that
1943881cb7a5d54c0011b40997adcd742e1c7b91abdEvan Cheng  /// value produced by an instruction of itinerary class DefClass, operand
1953881cb7a5d54c0011b40997adcd742e1c7b91abdEvan Cheng  /// index DefIdx can be bypassed when it's read by an instruction of
1963881cb7a5d54c0011b40997adcd742e1c7b91abdEvan Cheng  /// itinerary class UseClass, operand index UseIdx.
1973881cb7a5d54c0011b40997adcd742e1c7b91abdEvan Cheng  bool hasPipelineForwarding(unsigned DefClass, unsigned DefIdx,
1983881cb7a5d54c0011b40997adcd742e1c7b91abdEvan Cheng                             unsigned UseClass, unsigned UseIdx) const {
1993881cb7a5d54c0011b40997adcd742e1c7b91abdEvan Cheng    unsigned FirstDefIdx = Itineraries[DefClass].FirstOperandCycle;
2003881cb7a5d54c0011b40997adcd742e1c7b91abdEvan Cheng    unsigned LastDefIdx = Itineraries[DefClass].LastOperandCycle;
2013881cb7a5d54c0011b40997adcd742e1c7b91abdEvan Cheng    if ((FirstDefIdx + DefIdx) >= LastDefIdx)
2023881cb7a5d54c0011b40997adcd742e1c7b91abdEvan Cheng      return false;
2033881cb7a5d54c0011b40997adcd742e1c7b91abdEvan Cheng    if (Forwardings[FirstDefIdx + DefIdx] == 0)
2043881cb7a5d54c0011b40997adcd742e1c7b91abdEvan Cheng      return false;
2053881cb7a5d54c0011b40997adcd742e1c7b91abdEvan Cheng
2063881cb7a5d54c0011b40997adcd742e1c7b91abdEvan Cheng    unsigned FirstUseIdx = Itineraries[UseClass].FirstOperandCycle;
2073881cb7a5d54c0011b40997adcd742e1c7b91abdEvan Cheng    unsigned LastUseIdx = Itineraries[UseClass].LastOperandCycle;
2083881cb7a5d54c0011b40997adcd742e1c7b91abdEvan Cheng    if ((FirstUseIdx + UseIdx) >= LastUseIdx)
2093881cb7a5d54c0011b40997adcd742e1c7b91abdEvan Cheng      return false;
2103881cb7a5d54c0011b40997adcd742e1c7b91abdEvan Cheng
2113881cb7a5d54c0011b40997adcd742e1c7b91abdEvan Cheng    return Forwardings[FirstDefIdx + DefIdx] ==
2123881cb7a5d54c0011b40997adcd742e1c7b91abdEvan Cheng      Forwardings[FirstUseIdx + UseIdx];
2133881cb7a5d54c0011b40997adcd742e1c7b91abdEvan Cheng  }
2143881cb7a5d54c0011b40997adcd742e1c7b91abdEvan Cheng
2153881cb7a5d54c0011b40997adcd742e1c7b91abdEvan Cheng  /// getOperandLatency - Compute and return the use operand latency of a given
2163881cb7a5d54c0011b40997adcd742e1c7b91abdEvan Cheng  /// itinerary class and operand index if the value is produced by an
2173881cb7a5d54c0011b40997adcd742e1c7b91abdEvan Cheng  /// instruction of the specified itinerary class and def operand index.
2183881cb7a5d54c0011b40997adcd742e1c7b91abdEvan Cheng  int getOperandLatency(unsigned DefClass, unsigned DefIdx,
2193881cb7a5d54c0011b40997adcd742e1c7b91abdEvan Cheng                        unsigned UseClass, unsigned UseIdx) const {
2203881cb7a5d54c0011b40997adcd742e1c7b91abdEvan Cheng    if (isEmpty())
2213881cb7a5d54c0011b40997adcd742e1c7b91abdEvan Cheng      return -1;
2223881cb7a5d54c0011b40997adcd742e1c7b91abdEvan Cheng
2233881cb7a5d54c0011b40997adcd742e1c7b91abdEvan Cheng    int DefCycle = getOperandCycle(DefClass, DefIdx);
2243881cb7a5d54c0011b40997adcd742e1c7b91abdEvan Cheng    if (DefCycle == -1)
2253881cb7a5d54c0011b40997adcd742e1c7b91abdEvan Cheng      return -1;
2263881cb7a5d54c0011b40997adcd742e1c7b91abdEvan Cheng
2273881cb7a5d54c0011b40997adcd742e1c7b91abdEvan Cheng    int UseCycle = getOperandCycle(UseClass, UseIdx);
2283881cb7a5d54c0011b40997adcd742e1c7b91abdEvan Cheng    if (UseCycle == -1)
2293881cb7a5d54c0011b40997adcd742e1c7b91abdEvan Cheng      return -1;
2303881cb7a5d54c0011b40997adcd742e1c7b91abdEvan Cheng
2313881cb7a5d54c0011b40997adcd742e1c7b91abdEvan Cheng    UseCycle = DefCycle - UseCycle + 1;
2323881cb7a5d54c0011b40997adcd742e1c7b91abdEvan Cheng    if (UseCycle > 0 &&
2333881cb7a5d54c0011b40997adcd742e1c7b91abdEvan Cheng        hasPipelineForwarding(DefClass, DefIdx, UseClass, UseIdx))
2343881cb7a5d54c0011b40997adcd742e1c7b91abdEvan Cheng      // FIXME: This assumes one cycle benefit for every pipeline forwarding.
2353881cb7a5d54c0011b40997adcd742e1c7b91abdEvan Cheng      --UseCycle;
2363881cb7a5d54c0011b40997adcd742e1c7b91abdEvan Cheng    return UseCycle;
2373881cb7a5d54c0011b40997adcd742e1c7b91abdEvan Cheng  }
2383881cb7a5d54c0011b40997adcd742e1c7b91abdEvan Cheng
2397f8c74cfaebb4b58b283a1875d3205704ce5be07Andrew Trick  /// getNumMicroOps - Return the number of micro-ops that the given class
2407f8c74cfaebb4b58b283a1875d3205704ce5be07Andrew Trick  /// decodes to. Return -1 for classes that require dynamic lookup via
2417f8c74cfaebb4b58b283a1875d3205704ce5be07Andrew Trick  /// TargetInstrInfo.
2427f8c74cfaebb4b58b283a1875d3205704ce5be07Andrew Trick  int getNumMicroOps(unsigned ItinClassIndx) const {
2433ef1c8759a20167457eb7fd82ebcaffe7ccaa1d1Evan Cheng    if (isEmpty())
2447f8c74cfaebb4b58b283a1875d3205704ce5be07Andrew Trick      return 1;
2457f8c74cfaebb4b58b283a1875d3205704ce5be07Andrew Trick    return Itineraries[ItinClassIndx].NumMicroOps;
2463ef1c8759a20167457eb7fd82ebcaffe7ccaa1d1Evan Cheng  }
2476cee630070b1a7183ed56a8404e812629f5ca538Jim Laskey};
2486cee630070b1a7183ed56a8404e812629f5ca538Jim Laskey
24915517be4bfc7ec7e60ecb25dc0bb46de27d3de37Jim Laskey} // End llvm namespace
25015517be4bfc7ec7e60ecb25dc0bb46de27d3de37Jim Laskey
25115517be4bfc7ec7e60ecb25dc0bb46de27d3de37Jim Laskey#endif
252