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 *); |
| 95 | virtual void visit(ir_label *); |
| 96 | virtual void visit(ir_loop *); |
| 97 | virtual void visit(ir_loop_jump *); |
| 98 | virtual void visit(ir_function_signature *); |
| 99 | virtual void visit(ir_function *); |
| 100 | virtual void visit(ir_expression *); |
| 101 | virtual void visit(ir_swizzle *); |
| 102 | virtual void visit(ir_dereference *); |
| 103 | virtual void visit(ir_assignment *); |
| 104 | virtual void visit(ir_constant *); |
| 105 | virtual void visit(ir_call *); |
| 106 | virtual void visit(ir_return *); |
| 107 | virtual void visit(ir_if *); |
| 108 | /*@}*/ |
| 109 | |
| 110 | ir_instruction *result; |
| 111 | }; |
| 112 | |
| 113 | void |
| 114 | ir_function_cloning_visitor::visit(ir_variable *ir) |
| 115 | { |
| 116 | ir_variable *new_var = ir->clone(); |
| 117 | |
| 118 | this->result = new_var; |
| 119 | |
| 120 | this->remap_variable(ir, new_var); |
| 121 | } |
| 122 | |
| 123 | void |
| 124 | ir_function_cloning_visitor::visit(ir_label *ir) |
| 125 | { |
| 126 | (void)ir; |
| 127 | this->result = NULL; |
| 128 | } |
| 129 | |
| 130 | void |
| 131 | ir_function_cloning_visitor::visit(ir_loop *ir) |
| 132 | { |
| 133 | (void)ir; |
| 134 | this->result = NULL; |
| 135 | } |
| 136 | |
| 137 | void |
| 138 | ir_function_cloning_visitor::visit(ir_loop_jump *ir) |
| 139 | { |
| 140 | (void) ir; |
| 141 | this->result = NULL; |
| 142 | } |
| 143 | |
| 144 | |
| 145 | void |
| 146 | ir_function_cloning_visitor::visit(ir_function_signature *ir) |
| 147 | { |
| 148 | (void)ir; |
| 149 | this->result = NULL; |
| 150 | } |
| 151 | |
| 152 | |
| 153 | void |
| 154 | ir_function_cloning_visitor::visit(ir_function *ir) |
| 155 | { |
| 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 | { |
| 187 | if (ir->mode == ir_dereference::ir_reference_variable) { |
| 188 | ir_variable *old_var = ir->var->as_variable(); |
| 189 | |
| 190 | /* If it's a deref of a real variable, then we need to remap it if |
| 191 | * it was local to the function. |
| 192 | */ |
| 193 | if (old_var) { |
| 194 | ir_variable *new_var; |
| 195 | |
| 196 | new_var = this->get_remapped_variable(old_var); |
| 197 | |
| 198 | this->result = new ir_dereference(new_var); |
| 199 | } else { |
| 200 | ir->var->accept(this); |
| 201 | |
| 202 | this->result = new ir_dereference(this->result); |
| 203 | } |
Eric Anholt | 6192434 | 2010-04-08 13:40:52 -0700 | [diff] [blame] | 204 | } else if (ir->mode == ir_dereference::ir_reference_array) { |
| 205 | ir_instruction *variable; |
| 206 | ir_rvalue *index; |
| 207 | |
| 208 | ir->var->accept(this); |
| 209 | variable = this->result; |
| 210 | |
| 211 | ir->selector.array_index->accept(this); |
| 212 | index = this->result->as_rvalue(); |
| 213 | |
| 214 | this->result = new ir_dereference(variable, index); |
Eric Anholt | cad9766 | 2010-04-07 11:46:26 -0700 | [diff] [blame] | 215 | } else { |
Eric Anholt | 6192434 | 2010-04-08 13:40:52 -0700 | [diff] [blame] | 216 | assert(ir->mode == ir_dereference::ir_reference_record); |
| 217 | /* FINISHME: inlining of structure references */ |
| 218 | assert(0); |
Eric Anholt | cad9766 | 2010-04-07 11:46:26 -0700 | [diff] [blame] | 219 | } |
| 220 | } |
| 221 | |
| 222 | void |
| 223 | ir_function_cloning_visitor::visit(ir_assignment *ir) |
| 224 | { |
| 225 | ir_rvalue *lhs, *rhs, *condition; |
| 226 | |
| 227 | ir->lhs->accept(this); |
| 228 | lhs = this->result->as_rvalue(); |
| 229 | |
| 230 | ir->rhs->accept(this); |
| 231 | rhs = this->result->as_rvalue(); |
| 232 | |
| 233 | ir->condition->accept(this); |
| 234 | condition = this->result->as_rvalue(); |
| 235 | |
| 236 | this->result = new ir_assignment(lhs, rhs, condition); |
| 237 | } |
| 238 | |
| 239 | |
| 240 | void |
| 241 | ir_function_cloning_visitor::visit(ir_constant *ir) |
| 242 | { |
| 243 | this->result = ir->clone(); |
| 244 | } |
| 245 | |
| 246 | |
| 247 | void |
| 248 | ir_function_cloning_visitor::visit(ir_call *ir) |
| 249 | { |
| 250 | exec_list parameters; |
| 251 | |
| 252 | foreach_iter(exec_list_iterator, iter, *ir) { |
| 253 | ir_rvalue *param = (ir_rvalue *)iter.get(); |
| 254 | |
| 255 | param->accept(this); |
| 256 | parameters.push_tail(this->result); |
| 257 | } |
| 258 | |
| 259 | this->result = new ir_call(ir->get_callee(), ¶meters); |
| 260 | } |
| 261 | |
| 262 | |
| 263 | void |
| 264 | ir_function_cloning_visitor::visit(ir_return *ir) |
| 265 | { |
| 266 | ir_rvalue *rval; |
| 267 | |
| 268 | assert(this->retval); |
| 269 | |
| 270 | rval = ir->get_value(); |
| 271 | rval->accept(this); |
| 272 | rval = this->result->as_rvalue(); |
| 273 | assert(rval); |
| 274 | |
| 275 | result = new ir_assignment(new ir_dereference(this->retval), |
| 276 | ir->get_value(), NULL); |
| 277 | } |
| 278 | |
| 279 | |
| 280 | void |
| 281 | ir_function_cloning_visitor::visit(ir_if *ir) |
| 282 | { |
| 283 | (void) ir; |
| 284 | result = NULL; |
| 285 | } |
| 286 | |
| 287 | bool |
| 288 | can_inline(ir_call *call) |
| 289 | { |
| 290 | bool found_return = false; |
| 291 | |
| 292 | /* FINISHME: Right now we only allow a single statement that is a return. |
| 293 | */ |
| 294 | foreach_iter(exec_list_iterator, iter, call->get_callee()->body) { |
| 295 | ir_instruction *ir = (ir_instruction *)iter.get(); |
| 296 | if (ir->get_next()->get_next() != NULL) |
| 297 | return false; |
| 298 | |
| 299 | if (!ir->as_return()) |
| 300 | return false; |
| 301 | |
| 302 | found_return = true; |
| 303 | } |
| 304 | |
| 305 | return found_return; |
| 306 | } |
| 307 | |
| 308 | bool |
Eric Anholt | 0d42321 | 2010-04-16 12:53:46 -0700 | [diff] [blame^] | 309 | automatic_inlining_predicate(ir_instruction *ir) |
| 310 | { |
| 311 | ir_call *call = ir->as_call(); |
| 312 | |
| 313 | if (call && can_inline(call)) |
| 314 | return true; |
| 315 | |
| 316 | return false; |
| 317 | } |
| 318 | |
| 319 | bool |
Eric Anholt | cad9766 | 2010-04-07 11:46:26 -0700 | [diff] [blame] | 320 | do_function_inlining(exec_list *instructions) |
| 321 | { |
Eric Anholt | 2a7b2b2 | 2010-04-08 13:42:48 -0700 | [diff] [blame] | 322 | bool progress = false; |
Eric Anholt | cad9766 | 2010-04-07 11:46:26 -0700 | [diff] [blame] | 323 | |
Eric Anholt | 0d42321 | 2010-04-16 12:53:46 -0700 | [diff] [blame^] | 324 | do_expression_flattening(instructions, automatic_inlining_predicate); |
| 325 | |
Eric Anholt | cad9766 | 2010-04-07 11:46:26 -0700 | [diff] [blame] | 326 | foreach_iter(exec_list_iterator, iter, *instructions) { |
| 327 | ir_instruction *ir = (ir_instruction *)iter.get(); |
| 328 | ir_assignment *assign = ir->as_assignment(); |
| 329 | ir_call *call; |
| 330 | |
| 331 | if (assign) { |
| 332 | call = assign->rhs->as_call(); |
| 333 | if (!call || !can_inline(call)) |
| 334 | continue; |
| 335 | |
| 336 | /* generates the parameter setup, function body, and returns the return |
| 337 | * value of the function |
| 338 | */ |
| 339 | ir_rvalue *rhs = call->generate_inline(ir); |
| 340 | assert(rhs); |
| 341 | |
| 342 | assign->rhs = rhs; |
| 343 | progress = true; |
| 344 | } else if ((call = ir->as_call()) && can_inline(call)) { |
| 345 | (void)call->generate_inline(ir); |
| 346 | ir->remove(); |
| 347 | progress = true; |
| 348 | } else { |
| 349 | ir_function_inlining_visitor v; |
| 350 | ir->accept(&v); |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | return progress; |
| 355 | } |
| 356 | |
| 357 | ir_rvalue * |
| 358 | ir_call::generate_inline(ir_instruction *next_ir) |
| 359 | { |
| 360 | ir_variable **parameters; |
| 361 | int num_parameters; |
| 362 | int i; |
| 363 | ir_variable *retval = NULL; |
| 364 | |
| 365 | num_parameters = 0; |
| 366 | foreach_iter(exec_list_iterator, iter_sig, this->callee->parameters) |
| 367 | num_parameters++; |
| 368 | |
| 369 | parameters = new ir_variable *[num_parameters]; |
| 370 | |
| 371 | /* Generate storage for the return value. */ |
| 372 | if (this->callee->return_type) { |
| 373 | retval = new ir_variable(this->callee->return_type, "__retval"); |
| 374 | next_ir->insert_before(retval); |
| 375 | } |
| 376 | |
| 377 | ir_function_cloning_visitor v = ir_function_cloning_visitor(retval); |
| 378 | |
| 379 | /* Generate the declarations for the parameters to our inlined code, |
| 380 | * and set up the mapping of real function body variables to ours. |
| 381 | */ |
| 382 | i = 0; |
| 383 | exec_list_iterator sig_param_iter = this->callee->parameters.iterator(); |
| 384 | exec_list_iterator param_iter = this->actual_parameters.iterator(); |
| 385 | for (i = 0; i < num_parameters; i++) { |
| 386 | const ir_variable *const sig_param = (ir_variable *) sig_param_iter.get(); |
| 387 | ir_rvalue *param = (ir_rvalue *) param_iter.get(); |
| 388 | |
| 389 | /* Generate a new variable for the parameter. */ |
| 390 | parameters[i] = sig_param->clone(); |
| 391 | next_ir->insert_before(parameters[i]); |
| 392 | |
| 393 | v.remap_variable(sig_param, parameters[i]); |
| 394 | |
| 395 | /* Move the actual param into our param variable if it's an 'in' type. */ |
| 396 | if (parameters[i]->mode == ir_var_in || |
| 397 | parameters[i]->mode == ir_var_inout) { |
| 398 | ir_assignment *assign; |
| 399 | |
| 400 | assign = new ir_assignment(new ir_dereference(parameters[i]), |
| 401 | param, NULL); |
| 402 | next_ir->insert_before(assign); |
| 403 | } |
| 404 | |
| 405 | sig_param_iter.next(); |
| 406 | param_iter.next(); |
| 407 | } |
| 408 | |
| 409 | /* Generate the inlined body of the function. */ |
| 410 | foreach_iter(exec_list_iterator, iter, callee->body) { |
| 411 | ir_instruction *ir = (ir_instruction *)iter.get(); |
| 412 | |
| 413 | ir->accept(&v); |
| 414 | assert(v.result); |
| 415 | next_ir->insert_before(v.result); |
| 416 | } |
| 417 | |
| 418 | /* Generate the declarations for the parameters to our inlined code, |
| 419 | * and set up the mapping of real function body variables to ours. |
| 420 | */ |
| 421 | i = 0; |
| 422 | param_iter = this->actual_parameters.iterator(); |
| 423 | for (i = 0; i < num_parameters; i++) { |
| 424 | ir_instruction *const param = (ir_instruction *) param_iter.get(); |
| 425 | |
| 426 | /* Move the actual param into our param variable if it's an 'in' type. */ |
| 427 | if (parameters[i]->mode == ir_var_out || |
| 428 | parameters[i]->mode == ir_var_inout) { |
| 429 | ir_assignment *assign; |
| 430 | |
| 431 | assign = new ir_assignment(param->as_rvalue(), |
| 432 | new ir_dereference(parameters[i]), |
| 433 | NULL); |
| 434 | next_ir->insert_before(assign); |
| 435 | } |
| 436 | |
| 437 | param_iter.next(); |
| 438 | } |
| 439 | |
| 440 | delete(parameters); |
| 441 | |
| 442 | if (retval) |
| 443 | return new ir_dereference(retval); |
| 444 | else |
| 445 | return NULL; |
| 446 | } |
| 447 | |
| 448 | void |
| 449 | ir_function_inlining_visitor::visit(ir_variable *ir) |
| 450 | { |
| 451 | (void) ir; |
| 452 | } |
| 453 | |
| 454 | |
| 455 | void |
| 456 | ir_function_inlining_visitor::visit(ir_label *ir) |
| 457 | { |
| 458 | ir->signature->accept(this); |
| 459 | } |
| 460 | |
| 461 | void |
| 462 | ir_function_inlining_visitor::visit(ir_loop *ir) |
| 463 | { |
| 464 | do_function_inlining(&ir->body_instructions); |
| 465 | } |
| 466 | |
| 467 | void |
| 468 | ir_function_inlining_visitor::visit(ir_loop_jump *ir) |
| 469 | { |
| 470 | (void) ir; |
| 471 | } |
| 472 | |
| 473 | |
| 474 | void |
| 475 | ir_function_inlining_visitor::visit(ir_function_signature *ir) |
| 476 | { |
| 477 | do_function_inlining(&ir->body); |
| 478 | } |
| 479 | |
| 480 | |
| 481 | void |
| 482 | ir_function_inlining_visitor::visit(ir_function *ir) |
| 483 | { |
| 484 | (void) ir; |
| 485 | } |
| 486 | |
| 487 | void |
| 488 | ir_function_inlining_visitor::visit(ir_expression *ir) |
| 489 | { |
| 490 | unsigned int operand; |
| 491 | |
| 492 | for (operand = 0; operand < ir->get_num_operands(); operand++) { |
| 493 | ir->operands[operand]->accept(this); |
| 494 | } |
| 495 | } |
| 496 | |
| 497 | |
| 498 | void |
| 499 | ir_function_inlining_visitor::visit(ir_swizzle *ir) |
| 500 | { |
| 501 | ir->val->accept(this); |
| 502 | } |
| 503 | |
| 504 | |
| 505 | void |
| 506 | ir_function_inlining_visitor::visit(ir_dereference *ir) |
| 507 | { |
| 508 | if (ir->mode == ir_dereference::ir_reference_array) { |
| 509 | ir->selector.array_index->accept(this); |
| 510 | } |
| 511 | ir->var->accept(this); |
| 512 | } |
| 513 | |
| 514 | void |
| 515 | ir_function_inlining_visitor::visit(ir_assignment *ir) |
| 516 | { |
| 517 | ir->rhs->accept(this); |
| 518 | } |
| 519 | |
| 520 | |
| 521 | void |
| 522 | ir_function_inlining_visitor::visit(ir_constant *ir) |
| 523 | { |
| 524 | (void) ir; |
| 525 | } |
| 526 | |
| 527 | |
| 528 | void |
| 529 | ir_function_inlining_visitor::visit(ir_call *ir) |
| 530 | { |
| 531 | (void) ir; |
| 532 | } |
| 533 | |
| 534 | |
| 535 | void |
| 536 | ir_function_inlining_visitor::visit(ir_return *ir) |
| 537 | { |
| 538 | (void) ir; |
| 539 | } |
| 540 | |
| 541 | |
| 542 | void |
| 543 | ir_function_inlining_visitor::visit(ir_if *ir) |
| 544 | { |
| 545 | ir->condition->accept(this); |
| 546 | |
| 547 | do_function_inlining(&ir->then_instructions); |
| 548 | do_function_inlining(&ir->else_instructions); |
| 549 | } |