LatencyPriorityQueue.h revision 208599
1129198Scognet//===---- LatencyPriorityQueue.h - A latency-oriented priority queue ------===//
2129198Scognet//
3139735Simp//                     The LLVM Compiler Infrastructure
4129198Scognet//
5129198Scognet// This file is distributed under the University of Illinois Open Source
6129198Scognet// License. See LICENSE.TXT for details.
7129198Scognet//
8129198Scognet//===----------------------------------------------------------------------===//
9129198Scognet//
10129198Scognet// This file declares the LatencyPriorityQueue class, which is a
11129198Scognet// SchedulingPriorityQueue that schedules using latency information to
12129198Scognet// reduce the length of the critical path through the basic block.
13129198Scognet//
14129198Scognet//===----------------------------------------------------------------------===//
15129198Scognet
16129198Scognet#ifndef LATENCY_PRIORITY_QUEUE_H
17129198Scognet#define LATENCY_PRIORITY_QUEUE_H
18129198Scognet
19129198Scognet#include "llvm/CodeGen/ScheduleDAG.h"
20129198Scognet
21129198Scognetnamespace llvm {
22129198Scognet  class LatencyPriorityQueue;
23129198Scognet
24129198Scognet  /// Sorting functions for the Available queue.
25129198Scognet  struct latency_sort : public std::binary_function<SUnit*, SUnit*, bool> {
26129198Scognet    LatencyPriorityQueue *PQ;
27129198Scognet    explicit latency_sort(LatencyPriorityQueue *pq) : PQ(pq) {}
28129198Scognet
29129198Scognet    bool operator()(const SUnit* left, const SUnit* right) const;
30129198Scognet  };
31129198Scognet
32129198Scognet  class LatencyPriorityQueue : public SchedulingPriorityQueue {
33129198Scognet    // SUnits - The SUnits for the current graph.
34129198Scognet    std::vector<SUnit> *SUnits;
35129198Scognet
36129198Scognet    /// NumNodesSolelyBlocking - This vector contains, for every node in the
37129198Scognet    /// Queue, the number of nodes that the node is the sole unscheduled
38129198Scognet    /// predecessor for.  This is used as a tie-breaker heuristic for better
39129198Scognet    /// mobility.
40129198Scognet    std::vector<unsigned> NumNodesSolelyBlocking;
41129198Scognet
42129198Scognet    /// Queue - The queue.
43129198Scognet    std::vector<SUnit*> Queue;
44129198Scognet    latency_sort Picker;
45129198Scognet
46129198Scognet  public:
47129198Scognet    LatencyPriorityQueue() : Picker(this) {
48129198Scognet    }
49129198Scognet
50129198Scognet    void initNodes(std::vector<SUnit> &sunits) {
51129198Scognet      SUnits = &sunits;
52129198Scognet      NumNodesSolelyBlocking.resize(SUnits->size(), 0);
53129198Scognet    }
54129198Scognet
55129198Scognet    void addNode(const SUnit *SU) {
56129198Scognet      NumNodesSolelyBlocking.resize(SUnits->size(), 0);
57129198Scognet    }
58129198Scognet
59129198Scognet    void updateNode(const SUnit *SU) {
60129198Scognet    }
61129198Scognet
62129198Scognet    void releaseState() {
63129198Scognet      SUnits = 0;
64129198Scognet    }
65129198Scognet
66129198Scognet    unsigned getLatency(unsigned NodeNum) const {
67129198Scognet      assert(NodeNum < (*SUnits).size());
68129198Scognet      return (*SUnits)[NodeNum].getHeight();
69129198Scognet    }
70129198Scognet
71129198Scognet    unsigned getNumSolelyBlockNodes(unsigned NodeNum) const {
72      assert(NodeNum < NumNodesSolelyBlocking.size());
73      return NumNodesSolelyBlocking[NodeNum];
74    }
75
76    bool empty() const { return Queue.empty(); }
77
78    virtual void push(SUnit *U);
79
80    virtual SUnit *pop();
81
82    virtual void remove(SUnit *SU);
83
84    // ScheduledNode - As nodes are scheduled, we look to see if there are any
85    // successor nodes that have a single unscheduled predecessor.  If so, that
86    // single predecessor has a higher priority, since scheduling it will make
87    // the node available.
88    void ScheduledNode(SUnit *Node);
89
90private:
91    void AdjustPriorityOfUnscheduledPreds(SUnit *SU);
92    SUnit *getSingleUnscheduledPred(SUnit *SU);
93  };
94}
95
96#endif
97