Eric Anholt | cad9766 | 2010-04-07 11:46:26 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2010 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 |
| 21 | * DEALINGS IN THE SOFTWARE. |
| 22 | */ |
| 23 | |
| 24 | /** |
| 25 | * \file ir_function_inlining.cpp |
| 26 | * |
| 27 | * Replaces calls to functions with the body of the function. |
| 28 | */ |
| 29 | |
| 30 | #define NULL 0 |
| 31 | #include "ir.h" |
| 32 | #include "ir_visitor.h" |
| 33 | #include "ir_function_inlining.h" |
Eric Anholt | 0d42321 | 2010-04-16 12:53:46 -0700 | [diff] [blame] | 34 | #include "ir_expression_flattening.h" |
Eric Anholt | cad9766 | 2010-04-07 11:46:26 -0700 | [diff] [blame] | 35 | #include "glsl_types.h" |
| 36 | |
| 37 | class variable_remap : public exec_node { |
| 38 | public: |
| 39 | variable_remap(const ir_variable *old_var, ir_variable *new_var) |
| 40 | : old_var(old_var), new_var(new_var) |
| 41 | { |
| 42 | /* empty */ |
| 43 | } |
| 44 | const ir_variable *old_var; |
| 45 | ir_variable *new_var; |
| 46 | }; |
| 47 | |
| 48 | class ir_function_cloning_visitor : public ir_visitor { |
| 49 | public: |
| 50 | ir_function_cloning_visitor(ir_variable *retval) |
| 51 | : retval(retval) |
| 52 | { |
| 53 | /* empty */ |
| 54 | } |
| 55 | |
| 56 | virtual ~ir_function_cloning_visitor() |
| 57 | { |
| 58 | /* empty */ |
| 59 | } |
| 60 | |
| 61 | void remap_variable(const ir_variable *old_var, ir_variable *new_var) { |
| 62 | variable_remap *remap = new variable_remap(old_var, new_var); |
| 63 | this->remap_list.push_tail(remap); |
| 64 | } |
| 65 | |
| 66 | ir_variable *get_remapped_variable(ir_variable *var) { |
| 67 | foreach_iter(exec_list_iterator, iter, this->remap_list) { |
| 68 | variable_remap *remap = (variable_remap *)iter.get(); |
| 69 | |
| 70 | if (var == remap->old_var) |
| 71 | return remap->new_var; |
| 72 | } |
| 73 | |
| 74 | /* Not a reapped variable, so a global scoped reference, for example. */ |
| 75 | return var; |
| 76 | } |
| 77 | |
| 78 | /* List of variable_remap for mapping from original function body variables |
| 79 | * to inlined function body variables. |
| 80 | */ |
| 81 | exec_list remap_list; |
| 82 | |
| 83 | /* Return value for the inlined function. */ |
| 84 | ir_variable *retval; |
| 85 | |
| 86 | /** |
| 87 | * \name Visit methods |
| 88 | * |
| 89 | * As typical for the visitor pattern, there must be one \c visit method for |
| 90 | * each concrete subclass of \c ir_instruction. Virtual base classes within |
| 91 | * the hierarchy should not have \c visit methods. |
| 92 | */ |
| 93 | /*@{*/ |
| 94 | virtual void visit(ir_variable *); |
Eric Anholt | cad9766 | 2010-04-07 11:46:26 -0700 | [diff] [blame] | 95 | virtual void visit(ir_loop *); |
| 96 | virtual void visit(ir_loop_jump *); |
| 97 | virtual void visit(ir_function_signature *); |
| 98 | virtual void visit(ir_function *); |
| 99 | virtual void visit(ir_expression *); |
| 100 | virtual void visit(ir_swizzle *); |
| 101 | virtual void visit(ir_dereference *); |
| 102 | virtual void visit(ir_assignment *); |
| 103 | virtual void visit(ir_constant *); |
| 104 | virtual void visit(ir_call *); |
| 105 | virtual void visit(ir_return *); |
| 106 | virtual void visit(ir_if *); |
| 107 | /*@}*/ |
| 108 | |
| 109 | ir_instruction *result; |
| 110 | }; |
| 111 | |
| 112 | void |
| 113 | ir_function_cloning_visitor::visit(ir_variable *ir) |
| 114 | { |
| 115 | ir_variable *new_var = ir->clone(); |
| 116 | |
| 117 | this->result = new_var; |
| 118 | |
| 119 | this->remap_variable(ir, new_var); |
| 120 | } |
| 121 | |
| 122 | void |
Eric Anholt | cad9766 | 2010-04-07 11:46:26 -0700 | [diff] [blame] | 123 | ir_function_cloning_visitor::visit(ir_loop *ir) |
| 124 | { |
Eric Anholt | e8e9748 | 2010-04-22 17:52:59 -0700 | [diff] [blame] | 125 | /* FINISHME: Implement loop cloning. */ |
| 126 | assert(0); |
| 127 | |
Eric Anholt | cad9766 | 2010-04-07 11:46:26 -0700 | [diff] [blame] | 128 | (void)ir; |
| 129 | this->result = NULL; |
| 130 | } |
| 131 | |
| 132 | void |
| 133 | ir_function_cloning_visitor::visit(ir_loop_jump *ir) |
| 134 | { |
Eric Anholt | e8e9748 | 2010-04-22 17:52:59 -0700 | [diff] [blame] | 135 | /* FINISHME: Implement loop cloning. */ |
| 136 | assert(0); |
| 137 | |
Eric Anholt | cad9766 | 2010-04-07 11:46:26 -0700 | [diff] [blame] | 138 | (void) ir; |
| 139 | this->result = NULL; |
| 140 | } |
| 141 | |
| 142 | |
| 143 | void |
| 144 | ir_function_cloning_visitor::visit(ir_function_signature *ir) |
| 145 | { |
Eric Anholt | e8e9748 | 2010-04-22 17:52:59 -0700 | [diff] [blame] | 146 | assert(0); |
Eric Anholt | cad9766 | 2010-04-07 11:46:26 -0700 | [diff] [blame] | 147 | (void)ir; |
| 148 | this->result = NULL; |
| 149 | } |
| 150 | |
| 151 | |
| 152 | void |
| 153 | ir_function_cloning_visitor::visit(ir_function *ir) |
| 154 | { |
Eric Anholt | e8e9748 | 2010-04-22 17:52:59 -0700 | [diff] [blame] | 155 | assert(0); |
Eric Anholt | cad9766 | 2010-04-07 11:46:26 -0700 | [diff] [blame] | 156 | (void) ir; |
| 157 | this->result = NULL; |
| 158 | } |
| 159 | |
| 160 | void |
| 161 | ir_function_cloning_visitor::visit(ir_expression *ir) |
| 162 | { |
| 163 | unsigned int operand; |
| 164 | ir_rvalue *op[2] = {NULL, NULL}; |
| 165 | |
| 166 | for (operand = 0; operand < ir->get_num_operands(); operand++) { |
| 167 | ir->operands[operand]->accept(this); |
| 168 | op[operand] = this->result->as_rvalue(); |
| 169 | assert(op[operand]); |
| 170 | } |
| 171 | |
| 172 | this->result = new ir_expression(ir->operation, ir->type, op[0], op[1]); |
| 173 | } |
| 174 | |
| 175 | |
| 176 | void |
| 177 | ir_function_cloning_visitor::visit(ir_swizzle *ir) |
| 178 | { |
| 179 | ir->val->accept(this); |
| 180 | |
| 181 | this->result = new ir_swizzle(this->result->as_rvalue(), ir->mask); |
| 182 | } |
| 183 | |
| 184 | void |
| 185 | ir_function_cloning_visitor::visit(ir_dereference *ir) |
| 186 | { |
Eric Anholt | c0bfe87 | 2010-04-26 15:01:50 -0700 | [diff] [blame] | 187 | ir_variable *old_var = ir->var->as_variable(); |
| 188 | ir_instruction *var; |
Eric Anholt | cad9766 | 2010-04-07 11:46:26 -0700 | [diff] [blame] | 189 | |
Eric Anholt | c0bfe87 | 2010-04-26 15:01:50 -0700 | [diff] [blame] | 190 | if (old_var) |
| 191 | var = this->get_remapped_variable(old_var); |
| 192 | else { |
Eric Anholt | 6192434 | 2010-04-08 13:40:52 -0700 | [diff] [blame] | 193 | ir->var->accept(this); |
Eric Anholt | c0bfe87 | 2010-04-26 15:01:50 -0700 | [diff] [blame] | 194 | var = this->result; |
| 195 | } |
| 196 | |
| 197 | if (ir->mode == ir_dereference::ir_reference_variable) { |
| 198 | this->result = new ir_dereference(var); |
| 199 | } else if (ir->mode == ir_dereference::ir_reference_array) { |
| 200 | ir_rvalue *index; |
Eric Anholt | 6192434 | 2010-04-08 13:40:52 -0700 | [diff] [blame] | 201 | |
| 202 | ir->selector.array_index->accept(this); |
| 203 | index = this->result->as_rvalue(); |
| 204 | |
Eric Anholt | c0bfe87 | 2010-04-26 15:01:50 -0700 | [diff] [blame] | 205 | this->result = new ir_dereference(var, index); |
Eric Anholt | cad9766 | 2010-04-07 11:46:26 -0700 | [diff] [blame] | 206 | } else { |
Eric Anholt | 6192434 | 2010-04-08 13:40:52 -0700 | [diff] [blame] | 207 | assert(ir->mode == ir_dereference::ir_reference_record); |
Eric Anholt | 35e8e46 | 2010-04-26 15:02:40 -0700 | [diff] [blame^] | 208 | this->result = new ir_dereference(var, strdup(ir->selector.field)); |
Eric Anholt | cad9766 | 2010-04-07 11:46:26 -0700 | [diff] [blame] | 209 | } |
| 210 | } |
| 211 | |
| 212 | void |
| 213 | ir_function_cloning_visitor::visit(ir_assignment *ir) |
| 214 | { |
Eric Anholt | 22147be | 2010-04-22 18:41:32 -0700 | [diff] [blame] | 215 | ir_rvalue *lhs, *rhs, *condition = NULL; |
Eric Anholt | cad9766 | 2010-04-07 11:46:26 -0700 | [diff] [blame] | 216 | |
| 217 | ir->lhs->accept(this); |
| 218 | lhs = this->result->as_rvalue(); |
| 219 | |
| 220 | ir->rhs->accept(this); |
| 221 | rhs = this->result->as_rvalue(); |
| 222 | |
Eric Anholt | 22147be | 2010-04-22 18:41:32 -0700 | [diff] [blame] | 223 | if (ir->condition) { |
| 224 | ir->condition->accept(this); |
| 225 | condition = this->result->as_rvalue(); |
| 226 | } |
Eric Anholt | cad9766 | 2010-04-07 11:46:26 -0700 | [diff] [blame] | 227 | |
| 228 | this->result = new ir_assignment(lhs, rhs, condition); |
| 229 | } |
| 230 | |
| 231 | |
| 232 | void |
| 233 | ir_function_cloning_visitor::visit(ir_constant *ir) |
| 234 | { |
| 235 | this->result = ir->clone(); |
| 236 | } |
| 237 | |
| 238 | |
| 239 | void |
| 240 | ir_function_cloning_visitor::visit(ir_call *ir) |
| 241 | { |
| 242 | exec_list parameters; |
| 243 | |
| 244 | foreach_iter(exec_list_iterator, iter, *ir) { |
| 245 | ir_rvalue *param = (ir_rvalue *)iter.get(); |
| 246 | |
| 247 | param->accept(this); |
| 248 | parameters.push_tail(this->result); |
| 249 | } |
| 250 | |
| 251 | this->result = new ir_call(ir->get_callee(), ¶meters); |
| 252 | } |
| 253 | |
| 254 | |
| 255 | void |
| 256 | ir_function_cloning_visitor::visit(ir_return *ir) |
| 257 | { |
| 258 | ir_rvalue *rval; |
| 259 | |
| 260 | assert(this->retval); |
| 261 | |
| 262 | rval = ir->get_value(); |
| 263 | rval->accept(this); |
| 264 | rval = this->result->as_rvalue(); |
| 265 | assert(rval); |
| 266 | |
| 267 | result = new ir_assignment(new ir_dereference(this->retval), |
| 268 | ir->get_value(), NULL); |
| 269 | } |
| 270 | |
| 271 | |
| 272 | void |
| 273 | ir_function_cloning_visitor::visit(ir_if *ir) |
| 274 | { |
Eric Anholt | e8e9748 | 2010-04-22 17:52:59 -0700 | [diff] [blame] | 275 | /* FINISHME: Implement if cloning. */ |
| 276 | assert(0); |
| 277 | |
Eric Anholt | cad9766 | 2010-04-07 11:46:26 -0700 | [diff] [blame] | 278 | (void) ir; |
| 279 | result = NULL; |
| 280 | } |
| 281 | |
| 282 | bool |
Eric Anholt | 0d42321 | 2010-04-16 12:53:46 -0700 | [diff] [blame] | 283 | automatic_inlining_predicate(ir_instruction *ir) |
| 284 | { |
| 285 | ir_call *call = ir->as_call(); |
| 286 | |
| 287 | if (call && can_inline(call)) |
| 288 | return true; |
| 289 | |
| 290 | return false; |
| 291 | } |
| 292 | |
| 293 | bool |
Eric Anholt | cad9766 | 2010-04-07 11:46:26 -0700 | [diff] [blame] | 294 | do_function_inlining(exec_list *instructions) |
| 295 | { |
Eric Anholt | 2a7b2b2 | 2010-04-08 13:42:48 -0700 | [diff] [blame] | 296 | bool progress = false; |
Eric Anholt | cad9766 | 2010-04-07 11:46:26 -0700 | [diff] [blame] | 297 | |
Eric Anholt | 0d42321 | 2010-04-16 12:53:46 -0700 | [diff] [blame] | 298 | do_expression_flattening(instructions, automatic_inlining_predicate); |
| 299 | |
Eric Anholt | cad9766 | 2010-04-07 11:46:26 -0700 | [diff] [blame] | 300 | foreach_iter(exec_list_iterator, iter, *instructions) { |
| 301 | ir_instruction *ir = (ir_instruction *)iter.get(); |
| 302 | ir_assignment *assign = ir->as_assignment(); |
| 303 | ir_call *call; |
| 304 | |
| 305 | if (assign) { |
| 306 | call = assign->rhs->as_call(); |
| 307 | if (!call || !can_inline(call)) |
| 308 | continue; |
| 309 | |
| 310 | /* generates the parameter setup, function body, and returns the return |
| 311 | * value of the function |
| 312 | */ |
| 313 | ir_rvalue *rhs = call->generate_inline(ir); |
| 314 | assert(rhs); |
| 315 | |
| 316 | assign->rhs = rhs; |
| 317 | progress = true; |
| 318 | } else if ((call = ir->as_call()) && can_inline(call)) { |
| 319 | (void)call->generate_inline(ir); |
| 320 | ir->remove(); |
| 321 | progress = true; |
| 322 | } else { |
| 323 | ir_function_inlining_visitor v; |
| 324 | ir->accept(&v); |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | return progress; |
| 329 | } |
| 330 | |
| 331 | ir_rvalue * |
| 332 | ir_call::generate_inline(ir_instruction *next_ir) |
| 333 | { |
| 334 | ir_variable **parameters; |
| 335 | int num_parameters; |
| 336 | int i; |
| 337 | ir_variable *retval = NULL; |
| 338 | |
| 339 | num_parameters = 0; |
| 340 | foreach_iter(exec_list_iterator, iter_sig, this->callee->parameters) |
| 341 | num_parameters++; |
| 342 | |
| 343 | parameters = new ir_variable *[num_parameters]; |
| 344 | |
| 345 | /* Generate storage for the return value. */ |
| 346 | if (this->callee->return_type) { |
| 347 | retval = new ir_variable(this->callee->return_type, "__retval"); |
| 348 | next_ir->insert_before(retval); |
| 349 | } |
| 350 | |
| 351 | ir_function_cloning_visitor v = ir_function_cloning_visitor(retval); |
| 352 | |
| 353 | /* Generate the declarations for the parameters to our inlined code, |
| 354 | * and set up the mapping of real function body variables to ours. |
| 355 | */ |
| 356 | i = 0; |
| 357 | exec_list_iterator sig_param_iter = this->callee->parameters.iterator(); |
| 358 | exec_list_iterator param_iter = this->actual_parameters.iterator(); |
| 359 | for (i = 0; i < num_parameters; i++) { |
| 360 | const ir_variable *const sig_param = (ir_variable *) sig_param_iter.get(); |
| 361 | ir_rvalue *param = (ir_rvalue *) param_iter.get(); |
| 362 | |
| 363 | /* Generate a new variable for the parameter. */ |
| 364 | parameters[i] = sig_param->clone(); |
| 365 | next_ir->insert_before(parameters[i]); |
| 366 | |
| 367 | v.remap_variable(sig_param, parameters[i]); |
| 368 | |
| 369 | /* Move the actual param into our param variable if it's an 'in' type. */ |
| 370 | if (parameters[i]->mode == ir_var_in || |
| 371 | parameters[i]->mode == ir_var_inout) { |
| 372 | ir_assignment *assign; |
| 373 | |
| 374 | assign = new ir_assignment(new ir_dereference(parameters[i]), |
| 375 | param, NULL); |
| 376 | next_ir->insert_before(assign); |
| 377 | } |
| 378 | |
| 379 | sig_param_iter.next(); |
| 380 | param_iter.next(); |
| 381 | } |
| 382 | |
| 383 | /* Generate the inlined body of the function. */ |
| 384 | foreach_iter(exec_list_iterator, iter, callee->body) { |
| 385 | ir_instruction *ir = (ir_instruction *)iter.get(); |
| 386 | |
| 387 | ir->accept(&v); |
| 388 | assert(v.result); |
| 389 | next_ir->insert_before(v.result); |
| 390 | } |
| 391 | |
| 392 | /* Generate the declarations for the parameters to our inlined code, |
| 393 | * and set up the mapping of real function body variables to ours. |
| 394 | */ |
| 395 | i = 0; |
| 396 | param_iter = this->actual_parameters.iterator(); |
| 397 | for (i = 0; i < num_parameters; i++) { |
| 398 | ir_instruction *const param = (ir_instruction *) param_iter.get(); |
| 399 | |
| 400 | /* Move the actual param into our param variable if it's an 'in' type. */ |
| 401 | if (parameters[i]->mode == ir_var_out || |
| 402 | parameters[i]->mode == ir_var_inout) { |
| 403 | ir_assignment *assign; |
| 404 | |
| 405 | assign = new ir_assignment(param->as_rvalue(), |
| 406 | new ir_dereference(parameters[i]), |
| 407 | NULL); |
| 408 | next_ir->insert_before(assign); |
| 409 | } |
| 410 | |
| 411 | param_iter.next(); |
| 412 | } |
| 413 | |
| 414 | delete(parameters); |
| 415 | |
| 416 | if (retval) |
| 417 | return new ir_dereference(retval); |
| 418 | else |
| 419 | return NULL; |
| 420 | } |
| 421 | |
| 422 | void |
| 423 | ir_function_inlining_visitor::visit(ir_variable *ir) |
| 424 | { |
| 425 | (void) ir; |
| 426 | } |
| 427 | |
| 428 | |
| 429 | void |
Eric Anholt | cad9766 | 2010-04-07 11:46:26 -0700 | [diff] [blame] | 430 | ir_function_inlining_visitor::visit(ir_loop *ir) |
| 431 | { |
| 432 | do_function_inlining(&ir->body_instructions); |
| 433 | } |
| 434 | |
| 435 | void |
| 436 | ir_function_inlining_visitor::visit(ir_loop_jump *ir) |
| 437 | { |
| 438 | (void) ir; |
| 439 | } |
| 440 | |
| 441 | |
| 442 | void |
| 443 | ir_function_inlining_visitor::visit(ir_function_signature *ir) |
| 444 | { |
| 445 | do_function_inlining(&ir->body); |
| 446 | } |
| 447 | |
| 448 | |
| 449 | void |
| 450 | ir_function_inlining_visitor::visit(ir_function *ir) |
| 451 | { |
Kenneth Graunke | 9fa99f3 | 2010-04-21 12:30:22 -0700 | [diff] [blame] | 452 | foreach_iter(exec_list_iterator, iter, *ir) { |
| 453 | ir_function_signature *const sig = (ir_function_signature *) iter.get(); |
| 454 | sig->accept(this); |
| 455 | } |
Eric Anholt | cad9766 | 2010-04-07 11:46:26 -0700 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | void |
| 459 | ir_function_inlining_visitor::visit(ir_expression *ir) |
| 460 | { |
| 461 | unsigned int operand; |
| 462 | |
| 463 | for (operand = 0; operand < ir->get_num_operands(); operand++) { |
| 464 | ir->operands[operand]->accept(this); |
| 465 | } |
| 466 | } |
| 467 | |
| 468 | |
| 469 | void |
| 470 | ir_function_inlining_visitor::visit(ir_swizzle *ir) |
| 471 | { |
| 472 | ir->val->accept(this); |
| 473 | } |
| 474 | |
| 475 | |
| 476 | void |
| 477 | ir_function_inlining_visitor::visit(ir_dereference *ir) |
| 478 | { |
| 479 | if (ir->mode == ir_dereference::ir_reference_array) { |
| 480 | ir->selector.array_index->accept(this); |
| 481 | } |
| 482 | ir->var->accept(this); |
| 483 | } |
| 484 | |
| 485 | void |
| 486 | ir_function_inlining_visitor::visit(ir_assignment *ir) |
| 487 | { |
| 488 | ir->rhs->accept(this); |
| 489 | } |
| 490 | |
| 491 | |
| 492 | void |
| 493 | ir_function_inlining_visitor::visit(ir_constant *ir) |
| 494 | { |
| 495 | (void) ir; |
| 496 | } |
| 497 | |
| 498 | |
| 499 | void |
| 500 | ir_function_inlining_visitor::visit(ir_call *ir) |
| 501 | { |
| 502 | (void) ir; |
| 503 | } |
| 504 | |
| 505 | |
| 506 | void |
| 507 | ir_function_inlining_visitor::visit(ir_return *ir) |
| 508 | { |
| 509 | (void) ir; |
| 510 | } |
| 511 | |
| 512 | |
| 513 | void |
| 514 | ir_function_inlining_visitor::visit(ir_if *ir) |
| 515 | { |
| 516 | ir->condition->accept(this); |
| 517 | |
| 518 | do_function_inlining(&ir->then_instructions); |
| 519 | do_function_inlining(&ir->else_instructions); |
| 520 | } |