Eric Anholt | 599aac9 | 2012-03-19 13:27:06 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2012 Intel Corporation |
| 3 | * |
| 4 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 5 | * copy of this software and associated documentation files (the "Software"), |
| 6 | * to deal in the Software without restriction, including without limitation |
| 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 8 | * and/or sell copies of the Software, and to permit persons to whom the |
| 9 | * Software is furnished to do so, subject to the following conditions: |
| 10 | * |
| 11 | * The above copyright notice and this permission notice (including the next |
| 12 | * paragraph) shall be included in all copies or substantial portions of the |
| 13 | * Software. |
| 14 | * |
| 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 18 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 20 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 21 | * IN THE SOFTWARE. |
| 22 | */ |
| 23 | |
| 24 | #include "ir.h" |
| 25 | |
| 26 | namespace ir_builder { |
| 27 | |
Chad Versace | a32bc53 | 2013-01-11 14:54:28 -0800 | [diff] [blame] | 28 | #ifndef WRITEMASK_X |
| 29 | enum writemask { |
| 30 | WRITEMASK_X = 0x1, |
| 31 | WRITEMASK_Y = 0x2, |
| 32 | WRITEMASK_Z = 0x4, |
| 33 | WRITEMASK_W = 0x8, |
| 34 | }; |
| 35 | #endif |
| 36 | |
Eric Anholt | d6e6566 | 2012-03-19 14:04:23 -0700 | [diff] [blame] | 37 | /** |
| 38 | * This little class exists to let the helper expression generators |
| 39 | * take either an ir_rvalue * or an ir_variable * to be automatically |
| 40 | * dereferenced, while still providing compile-time type checking. |
| 41 | * |
| 42 | * You don't have to explicitly call the constructor -- C++ will see |
| 43 | * that you passed an ir_variable, and silently call the |
| 44 | * operand(ir_variable *var) constructor behind your back. |
| 45 | */ |
| 46 | class operand { |
| 47 | public: |
| 48 | operand(ir_rvalue *val) |
| 49 | : val(val) |
| 50 | { |
| 51 | } |
| 52 | |
| 53 | operand(ir_variable *var) |
| 54 | { |
| 55 | void *mem_ctx = ralloc_parent(var); |
| 56 | val = new(mem_ctx) ir_dereference_variable(var); |
| 57 | } |
| 58 | |
| 59 | ir_rvalue *val; |
| 60 | }; |
| 61 | |
Eric Anholt | d32780d | 2012-03-19 16:27:34 -0700 | [diff] [blame] | 62 | /** Automatic generator for ir_dereference_variable on assignment LHS. |
| 63 | * |
| 64 | * \sa operand |
| 65 | */ |
| 66 | class deref { |
| 67 | public: |
| 68 | deref(ir_dereference *val) |
| 69 | : val(val) |
| 70 | { |
| 71 | } |
| 72 | |
| 73 | deref(ir_variable *var) |
| 74 | { |
| 75 | void *mem_ctx = ralloc_parent(var); |
| 76 | val = new(mem_ctx) ir_dereference_variable(var); |
| 77 | } |
| 78 | |
| 79 | |
| 80 | ir_dereference *val; |
| 81 | }; |
| 82 | |
Eric Anholt | 7e88f8c | 2012-03-19 16:01:52 -0700 | [diff] [blame] | 83 | class ir_factory { |
| 84 | public: |
Kenneth Graunke | 8d90328 | 2013-09-03 17:07:18 -0700 | [diff] [blame] | 85 | ir_factory(exec_list *instructions = NULL, void *mem_ctx = NULL) |
| 86 | : instructions(instructions), |
| 87 | mem_ctx(mem_ctx) |
Chad Versace | fafcbf5 | 2013-01-15 17:29:21 -0800 | [diff] [blame] | 88 | { |
| 89 | return; |
| 90 | } |
| 91 | |
Eric Anholt | 7e88f8c | 2012-03-19 16:01:52 -0700 | [diff] [blame] | 92 | void emit(ir_instruction *ir); |
Eric Anholt | 8bb0091 | 2012-03-19 16:37:23 -0700 | [diff] [blame] | 93 | ir_variable *make_temp(const glsl_type *type, const char *name); |
Eric Anholt | 7e88f8c | 2012-03-19 16:01:52 -0700 | [diff] [blame] | 94 | |
Chad Versace | a6479ef | 2013-01-11 15:46:24 -0800 | [diff] [blame] | 95 | ir_constant* |
| 96 | constant(float f) |
| 97 | { |
| 98 | return new(mem_ctx) ir_constant(f); |
| 99 | } |
| 100 | |
| 101 | ir_constant* |
| 102 | constant(int i) |
| 103 | { |
| 104 | return new(mem_ctx) ir_constant(i); |
| 105 | } |
| 106 | |
| 107 | ir_constant* |
| 108 | constant(unsigned u) |
| 109 | { |
| 110 | return new(mem_ctx) ir_constant(u); |
| 111 | } |
| 112 | |
| 113 | ir_constant* |
| 114 | constant(bool b) |
| 115 | { |
| 116 | return new(mem_ctx) ir_constant(b); |
| 117 | } |
| 118 | |
Eric Anholt | 7e88f8c | 2012-03-19 16:01:52 -0700 | [diff] [blame] | 119 | exec_list *instructions; |
| 120 | void *mem_ctx; |
| 121 | }; |
| 122 | |
Eric Anholt | d32780d | 2012-03-19 16:27:34 -0700 | [diff] [blame] | 123 | ir_assignment *assign(deref lhs, operand rhs); |
| 124 | ir_assignment *assign(deref lhs, operand rhs, int writemask); |
Kenneth Graunke | f72a849 | 2013-09-03 16:41:42 -0700 | [diff] [blame] | 125 | ir_assignment *assign(deref lhs, operand rhs, operand condition); |
| 126 | ir_assignment *assign(deref lhs, operand rhs, operand condition, int writemask); |
Eric Anholt | d32780d | 2012-03-19 16:27:34 -0700 | [diff] [blame] | 127 | |
Kenneth Graunke | d716b33 | 2013-09-03 16:44:25 -0700 | [diff] [blame] | 128 | ir_return *ret(operand retval); |
| 129 | |
Kenneth Graunke | 0bb3d4b | 2012-07-08 22:27:25 -0700 | [diff] [blame] | 130 | ir_expression *expr(ir_expression_operation op, operand a); |
Eric Anholt | d6e6566 | 2012-03-19 14:04:23 -0700 | [diff] [blame] | 131 | ir_expression *expr(ir_expression_operation op, operand a, operand b); |
Kenneth Graunke | eff2ca1 | 2013-09-03 16:37:39 -0700 | [diff] [blame] | 132 | ir_expression *expr(ir_expression_operation op, operand a, operand b, operand c); |
Eric Anholt | d6e6566 | 2012-03-19 14:04:23 -0700 | [diff] [blame] | 133 | ir_expression *add(operand a, operand b); |
| 134 | ir_expression *sub(operand a, operand b); |
| 135 | ir_expression *mul(operand a, operand b); |
Matt Turner | 06e41a0 | 2013-09-17 21:34:15 -0700 | [diff] [blame] | 136 | ir_expression *imul_high(operand a, operand b); |
Chad Versace | 5790174 | 2013-01-11 09:53:21 -0800 | [diff] [blame] | 137 | ir_expression *div(operand a, operand b); |
Matt Turner | 499d7a7 | 2013-09-19 12:56:10 -0700 | [diff] [blame] | 138 | ir_expression *carry(operand a, operand b); |
| 139 | ir_expression *borrow(operand a, operand b); |
Matt Turner | a5455ab | 2015-01-30 13:50:28 -0800 | [diff] [blame] | 140 | ir_expression *trunc(operand a); |
Chad Versace | 5790174 | 2013-01-11 09:53:21 -0800 | [diff] [blame] | 141 | ir_expression *round_even(operand a); |
Dave Airlie | 41e9adf | 2015-02-05 11:54:39 +0200 | [diff] [blame] | 142 | ir_expression *fract(operand a); |
Eric Anholt | d6e6566 | 2012-03-19 14:04:23 -0700 | [diff] [blame] | 143 | ir_expression *dot(operand a, operand b); |
Chad Versace | 5790174 | 2013-01-11 09:53:21 -0800 | [diff] [blame] | 144 | ir_expression *clamp(operand a, operand b, operand c); |
Eric Anholt | d6e6566 | 2012-03-19 14:04:23 -0700 | [diff] [blame] | 145 | ir_expression *saturate(operand a); |
Matt Turner | 16be629 | 2013-08-04 14:09:09 -0700 | [diff] [blame] | 146 | ir_expression *abs(operand a); |
Kenneth Graunke | 666df56 | 2013-09-03 17:02:07 -0700 | [diff] [blame] | 147 | ir_expression *neg(operand a); |
| 148 | ir_expression *sin(operand a); |
| 149 | ir_expression *cos(operand a); |
| 150 | ir_expression *exp(operand a); |
| 151 | ir_expression *rsq(operand a); |
| 152 | ir_expression *sqrt(operand a); |
| 153 | ir_expression *log(operand a); |
| 154 | ir_expression *sign(operand a); |
Eric Anholt | 599aac9 | 2012-03-19 13:27:06 -0700 | [diff] [blame] | 155 | |
Dave Airlie | 57f2429 | 2015-04-20 10:16:55 +1000 | [diff] [blame] | 156 | ir_expression *subr_to_int(operand a); |
Chad Versace | 5790174 | 2013-01-11 09:53:21 -0800 | [diff] [blame] | 157 | ir_expression *equal(operand a, operand b); |
Matt Turner | 1cf76c7 | 2013-08-04 14:09:35 -0700 | [diff] [blame] | 158 | ir_expression *nequal(operand a, operand b); |
Chad Versace | 5790174 | 2013-01-11 09:53:21 -0800 | [diff] [blame] | 159 | ir_expression *less(operand a, operand b); |
| 160 | ir_expression *greater(operand a, operand b); |
| 161 | ir_expression *lequal(operand a, operand b); |
| 162 | ir_expression *gequal(operand a, operand b); |
| 163 | |
| 164 | ir_expression *logic_not(operand a); |
| 165 | ir_expression *logic_and(operand a, operand b); |
| 166 | ir_expression *logic_or(operand a, operand b); |
| 167 | |
| 168 | ir_expression *bit_not(operand a); |
| 169 | ir_expression *bit_or(operand a, operand b); |
| 170 | ir_expression *bit_and(operand a, operand b); |
Ian Romanick | 7f64041 | 2016-09-06 23:17:51 -0700 | [diff] [blame] | 171 | ir_expression *bit_xor(operand a, operand b); |
Chad Versace | 5790174 | 2013-01-11 09:53:21 -0800 | [diff] [blame] | 172 | ir_expression *lshift(operand a, operand b); |
| 173 | ir_expression *rshift(operand a, operand b); |
| 174 | |
| 175 | ir_expression *f2i(operand a); |
Matt Turner | 6bfb1a8 | 2013-08-03 11:33:49 -0700 | [diff] [blame] | 176 | ir_expression *bitcast_f2i(operand a); |
Chad Versace | 5790174 | 2013-01-11 09:53:21 -0800 | [diff] [blame] | 177 | ir_expression *i2f(operand a); |
Matt Turner | 6bfb1a8 | 2013-08-03 11:33:49 -0700 | [diff] [blame] | 178 | ir_expression *bitcast_i2f(operand a); |
Chad Versace | 5790174 | 2013-01-11 09:53:21 -0800 | [diff] [blame] | 179 | ir_expression *f2u(operand a); |
Matt Turner | 6bfb1a8 | 2013-08-03 11:33:49 -0700 | [diff] [blame] | 180 | ir_expression *bitcast_f2u(operand a); |
Chad Versace | 5790174 | 2013-01-11 09:53:21 -0800 | [diff] [blame] | 181 | ir_expression *u2f(operand a); |
Matt Turner | 6bfb1a8 | 2013-08-03 11:33:49 -0700 | [diff] [blame] | 182 | ir_expression *bitcast_u2f(operand a); |
Chad Versace | 5790174 | 2013-01-11 09:53:21 -0800 | [diff] [blame] | 183 | ir_expression *i2u(operand a); |
| 184 | ir_expression *u2i(operand a); |
Matt Turner | 57a6bcd | 2013-08-07 13:00:48 -0700 | [diff] [blame] | 185 | ir_expression *b2i(operand a); |
| 186 | ir_expression *i2b(operand a); |
Kenneth Graunke | 666df56 | 2013-09-03 17:02:07 -0700 | [diff] [blame] | 187 | ir_expression *f2b(operand a); |
| 188 | ir_expression *b2f(operand a); |
| 189 | |
Dave Airlie | 41e9adf | 2015-02-05 11:54:39 +0200 | [diff] [blame] | 190 | ir_expression *f2d(operand a); |
| 191 | ir_expression *i2d(operand a); |
| 192 | ir_expression *u2d(operand a); |
| 193 | |
Brian Paul | 8d1400f | 2014-01-06 16:11:21 -0700 | [diff] [blame] | 194 | ir_expression *min2(operand a, operand b); |
| 195 | ir_expression *max2(operand a, operand b); |
Maxence Le Doré | 61c450f | 2014-01-03 00:09:50 +0100 | [diff] [blame] | 196 | |
Chris Forbes | 1d5b066 | 2013-11-10 19:13:54 +1300 | [diff] [blame] | 197 | ir_expression *interpolate_at_centroid(operand a); |
| 198 | ir_expression *interpolate_at_offset(operand a, operand b); |
| 199 | ir_expression *interpolate_at_sample(operand a, operand b); |
| 200 | |
Kenneth Graunke | 666df56 | 2013-09-03 17:02:07 -0700 | [diff] [blame] | 201 | ir_expression *fma(operand a, operand b, operand c); |
| 202 | ir_expression *lrp(operand x, operand y, operand a); |
Matt Turner | 7aaa387 | 2013-08-19 10:45:46 -0700 | [diff] [blame] | 203 | ir_expression *csel(operand a, operand b, operand c); |
Ilia Mirkin | 275c581 | 2015-08-20 21:55:52 -0400 | [diff] [blame] | 204 | ir_expression *bitfield_extract(operand a, operand b, operand c); |
Kenneth Graunke | 666df56 | 2013-09-03 17:02:07 -0700 | [diff] [blame] | 205 | ir_expression *bitfield_insert(operand a, operand b, operand c, operand d); |
Chad Versace | 5790174 | 2013-01-11 09:53:21 -0800 | [diff] [blame] | 206 | |
Kenneth Graunke | 1a6c0ef | 2013-09-03 16:55:37 -0700 | [diff] [blame] | 207 | ir_swizzle *swizzle(operand a, int swizzle, int components); |
Kenneth Graunke | d9da350 | 2012-07-09 20:59:29 -0700 | [diff] [blame] | 208 | /** |
| 209 | * Swizzle away later components, but preserve the ordering. |
| 210 | */ |
Brian Paul | c346631 | 2012-11-04 16:43:44 -0700 | [diff] [blame] | 211 | ir_swizzle *swizzle_for_size(operand a, unsigned components); |
Kenneth Graunke | d9da350 | 2012-07-09 20:59:29 -0700 | [diff] [blame] | 212 | |
Eric Anholt | b782352 | 2012-03-19 14:26:04 -0700 | [diff] [blame] | 213 | ir_swizzle *swizzle_xxxx(operand a); |
| 214 | ir_swizzle *swizzle_yyyy(operand a); |
| 215 | ir_swizzle *swizzle_zzzz(operand a); |
| 216 | ir_swizzle *swizzle_wwww(operand a); |
| 217 | ir_swizzle *swizzle_x(operand a); |
| 218 | ir_swizzle *swizzle_y(operand a); |
| 219 | ir_swizzle *swizzle_z(operand a); |
| 220 | ir_swizzle *swizzle_w(operand a); |
| 221 | ir_swizzle *swizzle_xy(operand a); |
| 222 | ir_swizzle *swizzle_xyz(operand a); |
| 223 | ir_swizzle *swizzle_xyzw(operand a); |
| 224 | |
Chad Versace | f859e4f | 2013-01-11 15:53:11 -0800 | [diff] [blame] | 225 | ir_if *if_tree(operand condition, |
| 226 | ir_instruction *then_branch); |
| 227 | ir_if *if_tree(operand condition, |
| 228 | ir_instruction *then_branch, |
| 229 | ir_instruction *else_branch); |
| 230 | |
Eric Anholt | 599aac9 | 2012-03-19 13:27:06 -0700 | [diff] [blame] | 231 | } /* namespace ir_builder */ |