1 /* -*- mesa-c++  -*-
2  *
3  * Copyright (c) 2018-2019 Collabora LTD
4  *
5  * Author: Gert Wollny <gert.wollny@collabora.com>
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a
8  * copy of this software and associated documentation files (the "Software"),
9  * to deal in the Software without restriction, including without limitation
10  * on the rights to use, copy, modify, merge, publish, distribute, sub
11  * license, and/or sell copies of the Software, and to permit persons to whom
12  * the Software is furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice (including the next
15  * paragraph) shall be included in all copies or substantial portions of the
16  * Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21  * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
22  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24  * USE OR OTHER DEALINGS IN THE SOFTWARE.
25  */
26 
27 #ifndef sfn_r600_instr_h
28 #define sfn_r600_instr_h
29 
30 #include "sfn_value_gpr.h"
31 #include "sfn_defines.h"
32 
33 #include "gallium/drivers/r600/r600_isa.h"
34 #include <iostream>
35 #include <memory>
36 #include <vector>
37 #include <set>
38 
39 namespace r600 {
40 
41 
42 struct rename_reg_pair {
43    bool valid;
44    bool used;
45    int new_reg;
46 };
47 
48 class LiverangeEvaluator;
49 class ValueMap;
50 
51 
52 class ValueRemapper {
53 public:
54    ValueRemapper(std::vector<rename_reg_pair>& m,
55                  ValueMap& values);
56 
57    void remap(PValue& v);
58    void remap(GPRVector& v);
59 private:
60    PValue remap_one_registers(PValue& reg);
61 
62    std::vector<rename_reg_pair>& m_map;
63    ValueMap& m_values;
64 };
65 
66 
67 using OutputRegisterMap = std::map<unsigned, const GPRVector *>;
68 
69 class Instruction {
70 public:
71    enum instr_type {
72       alu,
73       exprt,
74       tex,
75       vtx,
76       wait_ack,
77       cond_if,
78       cond_else,
79       cond_endif,
80       lds_atomic,
81       lds_read,
82       lds_write,
83       loop_begin,
84       loop_end,
85       loop_break,
86       loop_continue,
87       phi,
88       streamout,
89       ring,
90       emit_vtx,
91       mem_wr_scratch,
92       gds,
93       rat,
94       tf_write,
95       block,
96       unknown
97    };
98 
99    typedef std::shared_ptr<Instruction> Pointer;
100 
101    friend bool operator == (const Instruction& lhs, const Instruction& rhs);
102 
103    Instruction(instr_type t);
104 
105    virtual ~Instruction();
106 
type()107    instr_type type() const { return m_type;}
108 
109    void print(std::ostream& os) const;
110 
111    virtual void replace_values(const ValueSet& candiates, PValue new_value);
112 
113    void evalue_liveness(LiverangeEvaluator& eval) const;
114 
115    void remap_registers(ValueRemapper& map);
116 
117 protected:
118 
119    void add_remappable_src_value(PValue *v);
120    void add_remappable_src_value(GPRVector *v);
121    void add_remappable_dst_value(PValue *v);
122    void add_remappable_dst_value(GPRVector *v);
123 
124 private:
125 
126    virtual void do_evalue_liveness(LiverangeEvaluator& eval) const;
127 
128    virtual bool is_equal_to(const Instruction& lhs) const = 0;
129 
130    instr_type m_type;
131 
132    virtual void do_print(std::ostream& os) const = 0;
133 
134    std::vector<PValue*> m_mappable_src_registers;
135    std::vector<GPRVector*> m_mappable_src_vectors;
136    std::vector<PValue*> m_mappable_dst_registers;
137    std::vector<GPRVector*> m_mappable_dst_vectors;
138 };
139 
140 using PInstruction=Instruction::Pointer;
141 
142 inline std::ostream& operator << (std::ostream& os, const Instruction& instr)
143 {
144    instr.print(os);
145    return os;
146 }
147 
148 bool operator == (const Instruction& lhs, const Instruction& rhs);
149 
150 }
151 
152 #endif
153