Clone of mesa.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ir_function_inlining.cpp 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  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. * \file ir_function_inlining.cpp
  25. *
  26. * Replaces calls to functions with the body of the function.
  27. */
  28. #define NULL 0
  29. #include "ir.h"
  30. #include "ir_visitor.h"
  31. #include "ir_function_inlining.h"
  32. #include "glsl_types.h"
  33. class variable_remap : public exec_node {
  34. public:
  35. variable_remap(const ir_variable *old_var, ir_variable *new_var)
  36. : old_var(old_var), new_var(new_var)
  37. {
  38. /* empty */
  39. }
  40. const ir_variable *old_var;
  41. ir_variable *new_var;
  42. };
  43. class ir_function_cloning_visitor : public ir_visitor {
  44. public:
  45. ir_function_cloning_visitor(ir_variable *retval)
  46. : retval(retval)
  47. {
  48. /* empty */
  49. }
  50. virtual ~ir_function_cloning_visitor()
  51. {
  52. /* empty */
  53. }
  54. void remap_variable(const ir_variable *old_var, ir_variable *new_var) {
  55. variable_remap *remap = new variable_remap(old_var, new_var);
  56. this->remap_list.push_tail(remap);
  57. }
  58. ir_variable *get_remapped_variable(ir_variable *var) {
  59. foreach_iter(exec_list_iterator, iter, this->remap_list) {
  60. variable_remap *remap = (variable_remap *)iter.get();
  61. if (var == remap->old_var)
  62. return remap->new_var;
  63. }
  64. /* Not a reapped variable, so a global scoped reference, for example. */
  65. return var;
  66. }
  67. /* List of variable_remap for mapping from original function body variables
  68. * to inlined function body variables.
  69. */
  70. exec_list remap_list;
  71. /* Return value for the inlined function. */
  72. ir_variable *retval;
  73. /**
  74. * \name Visit methods
  75. *
  76. * As typical for the visitor pattern, there must be one \c visit method for
  77. * each concrete subclass of \c ir_instruction. Virtual base classes within
  78. * the hierarchy should not have \c visit methods.
  79. */
  80. /*@{*/
  81. virtual void visit(ir_variable *);
  82. virtual void visit(ir_label *);
  83. virtual void visit(ir_loop *);
  84. virtual void visit(ir_loop_jump *);
  85. virtual void visit(ir_function_signature *);
  86. virtual void visit(ir_function *);
  87. virtual void visit(ir_expression *);
  88. virtual void visit(ir_swizzle *);
  89. virtual void visit(ir_dereference *);
  90. virtual void visit(ir_assignment *);
  91. virtual void visit(ir_constant *);
  92. virtual void visit(ir_call *);
  93. virtual void visit(ir_return *);
  94. virtual void visit(ir_if *);
  95. /*@}*/
  96. ir_instruction *result;
  97. };
  98. void
  99. ir_function_cloning_visitor::visit(ir_variable *ir)
  100. {
  101. ir_variable *new_var = ir->clone();
  102. this->result = new_var;
  103. this->remap_variable(ir, new_var);
  104. }
  105. void
  106. ir_function_cloning_visitor::visit(ir_label *ir)
  107. {
  108. (void)ir;
  109. this->result = NULL;
  110. }
  111. void
  112. ir_function_cloning_visitor::visit(ir_loop *ir)
  113. {
  114. (void)ir;
  115. this->result = NULL;
  116. }
  117. void
  118. ir_function_cloning_visitor::visit(ir_loop_jump *ir)
  119. {
  120. (void) ir;
  121. this->result = NULL;
  122. }
  123. void
  124. ir_function_cloning_visitor::visit(ir_function_signature *ir)
  125. {
  126. (void)ir;
  127. this->result = NULL;
  128. }
  129. void
  130. ir_function_cloning_visitor::visit(ir_function *ir)
  131. {
  132. (void) ir;
  133. this->result = NULL;
  134. }
  135. void
  136. ir_function_cloning_visitor::visit(ir_expression *ir)
  137. {
  138. unsigned int operand;
  139. ir_rvalue *op[2] = {NULL, NULL};
  140. for (operand = 0; operand < ir->get_num_operands(); operand++) {
  141. ir->operands[operand]->accept(this);
  142. op[operand] = this->result->as_rvalue();
  143. assert(op[operand]);
  144. }
  145. this->result = new ir_expression(ir->operation, ir->type, op[0], op[1]);
  146. }
  147. void
  148. ir_function_cloning_visitor::visit(ir_swizzle *ir)
  149. {
  150. ir->val->accept(this);
  151. this->result = new ir_swizzle(this->result->as_rvalue(), ir->mask);
  152. }
  153. void
  154. ir_function_cloning_visitor::visit(ir_dereference *ir)
  155. {
  156. if (ir->mode == ir_dereference::ir_reference_variable) {
  157. ir_variable *old_var = ir->var->as_variable();
  158. /* If it's a deref of a real variable, then we need to remap it if
  159. * it was local to the function.
  160. */
  161. if (old_var) {
  162. ir_variable *new_var;
  163. new_var = this->get_remapped_variable(old_var);
  164. this->result = new ir_dereference(new_var);
  165. } else {
  166. ir->var->accept(this);
  167. this->result = new ir_dereference(this->result);
  168. }
  169. } else if (ir->mode == ir_dereference::ir_reference_array) {
  170. ir_instruction *variable;
  171. ir_rvalue *index;
  172. ir->var->accept(this);
  173. variable = this->result;
  174. ir->selector.array_index->accept(this);
  175. index = this->result->as_rvalue();
  176. this->result = new ir_dereference(variable, index);
  177. } else {
  178. assert(ir->mode == ir_dereference::ir_reference_record);
  179. /* FINISHME: inlining of structure references */
  180. assert(0);
  181. }
  182. }
  183. void
  184. ir_function_cloning_visitor::visit(ir_assignment *ir)
  185. {
  186. ir_rvalue *lhs, *rhs, *condition;
  187. ir->lhs->accept(this);
  188. lhs = this->result->as_rvalue();
  189. ir->rhs->accept(this);
  190. rhs = this->result->as_rvalue();
  191. ir->condition->accept(this);
  192. condition = this->result->as_rvalue();
  193. this->result = new ir_assignment(lhs, rhs, condition);
  194. }
  195. void
  196. ir_function_cloning_visitor::visit(ir_constant *ir)
  197. {
  198. this->result = ir->clone();
  199. }
  200. void
  201. ir_function_cloning_visitor::visit(ir_call *ir)
  202. {
  203. exec_list parameters;
  204. foreach_iter(exec_list_iterator, iter, *ir) {
  205. ir_rvalue *param = (ir_rvalue *)iter.get();
  206. param->accept(this);
  207. parameters.push_tail(this->result);
  208. }
  209. this->result = new ir_call(ir->get_callee(), &parameters);
  210. }
  211. void
  212. ir_function_cloning_visitor::visit(ir_return *ir)
  213. {
  214. ir_rvalue *rval;
  215. assert(this->retval);
  216. rval = ir->get_value();
  217. rval->accept(this);
  218. rval = this->result->as_rvalue();
  219. assert(rval);
  220. result = new ir_assignment(new ir_dereference(this->retval),
  221. ir->get_value(), NULL);
  222. }
  223. void
  224. ir_function_cloning_visitor::visit(ir_if *ir)
  225. {
  226. (void) ir;
  227. result = NULL;
  228. }
  229. bool
  230. can_inline(ir_call *call)
  231. {
  232. bool found_return = false;
  233. /* FINISHME: Right now we only allow a single statement that is a return.
  234. */
  235. foreach_iter(exec_list_iterator, iter, call->get_callee()->body) {
  236. ir_instruction *ir = (ir_instruction *)iter.get();
  237. if (ir->get_next()->get_next() != NULL)
  238. return false;
  239. if (!ir->as_return())
  240. return false;
  241. found_return = true;
  242. }
  243. return found_return;
  244. }
  245. bool
  246. do_function_inlining(exec_list *instructions)
  247. {
  248. bool progress = false;
  249. foreach_iter(exec_list_iterator, iter, *instructions) {
  250. ir_instruction *ir = (ir_instruction *)iter.get();
  251. ir_assignment *assign = ir->as_assignment();
  252. ir_call *call;
  253. if (assign) {
  254. call = assign->rhs->as_call();
  255. if (!call || !can_inline(call))
  256. continue;
  257. /* generates the parameter setup, function body, and returns the return
  258. * value of the function
  259. */
  260. ir_rvalue *rhs = call->generate_inline(ir);
  261. assert(rhs);
  262. assign->rhs = rhs;
  263. progress = true;
  264. } else if ((call = ir->as_call()) && can_inline(call)) {
  265. (void)call->generate_inline(ir);
  266. ir->remove();
  267. progress = true;
  268. } else {
  269. ir_function_inlining_visitor v;
  270. ir->accept(&v);
  271. }
  272. }
  273. return progress;
  274. }
  275. ir_rvalue *
  276. ir_call::generate_inline(ir_instruction *next_ir)
  277. {
  278. ir_variable **parameters;
  279. int num_parameters;
  280. int i;
  281. ir_variable *retval = NULL;
  282. num_parameters = 0;
  283. foreach_iter(exec_list_iterator, iter_sig, this->callee->parameters)
  284. num_parameters++;
  285. parameters = new ir_variable *[num_parameters];
  286. /* Generate storage for the return value. */
  287. if (this->callee->return_type) {
  288. retval = new ir_variable(this->callee->return_type, "__retval");
  289. next_ir->insert_before(retval);
  290. }
  291. ir_function_cloning_visitor v = ir_function_cloning_visitor(retval);
  292. /* Generate the declarations for the parameters to our inlined code,
  293. * and set up the mapping of real function body variables to ours.
  294. */
  295. i = 0;
  296. exec_list_iterator sig_param_iter = this->callee->parameters.iterator();
  297. exec_list_iterator param_iter = this->actual_parameters.iterator();
  298. for (i = 0; i < num_parameters; i++) {
  299. const ir_variable *const sig_param = (ir_variable *) sig_param_iter.get();
  300. ir_rvalue *param = (ir_rvalue *) param_iter.get();
  301. /* Generate a new variable for the parameter. */
  302. parameters[i] = sig_param->clone();
  303. next_ir->insert_before(parameters[i]);
  304. v.remap_variable(sig_param, parameters[i]);
  305. /* Move the actual param into our param variable if it's an 'in' type. */
  306. if (parameters[i]->mode == ir_var_in ||
  307. parameters[i]->mode == ir_var_inout) {
  308. ir_assignment *assign;
  309. assign = new ir_assignment(new ir_dereference(parameters[i]),
  310. param, NULL);
  311. next_ir->insert_before(assign);
  312. }
  313. sig_param_iter.next();
  314. param_iter.next();
  315. }
  316. /* Generate the inlined body of the function. */
  317. foreach_iter(exec_list_iterator, iter, callee->body) {
  318. ir_instruction *ir = (ir_instruction *)iter.get();
  319. ir->accept(&v);
  320. assert(v.result);
  321. next_ir->insert_before(v.result);
  322. }
  323. /* Generate the declarations for the parameters to our inlined code,
  324. * and set up the mapping of real function body variables to ours.
  325. */
  326. i = 0;
  327. param_iter = this->actual_parameters.iterator();
  328. for (i = 0; i < num_parameters; i++) {
  329. ir_instruction *const param = (ir_instruction *) param_iter.get();
  330. /* Move the actual param into our param variable if it's an 'in' type. */
  331. if (parameters[i]->mode == ir_var_out ||
  332. parameters[i]->mode == ir_var_inout) {
  333. ir_assignment *assign;
  334. assign = new ir_assignment(param->as_rvalue(),
  335. new ir_dereference(parameters[i]),
  336. NULL);
  337. next_ir->insert_before(assign);
  338. }
  339. param_iter.next();
  340. }
  341. delete(parameters);
  342. if (retval)
  343. return new ir_dereference(retval);
  344. else
  345. return NULL;
  346. }
  347. void
  348. ir_function_inlining_visitor::visit(ir_variable *ir)
  349. {
  350. (void) ir;
  351. }
  352. void
  353. ir_function_inlining_visitor::visit(ir_label *ir)
  354. {
  355. ir->signature->accept(this);
  356. }
  357. void
  358. ir_function_inlining_visitor::visit(ir_loop *ir)
  359. {
  360. do_function_inlining(&ir->body_instructions);
  361. }
  362. void
  363. ir_function_inlining_visitor::visit(ir_loop_jump *ir)
  364. {
  365. (void) ir;
  366. }
  367. void
  368. ir_function_inlining_visitor::visit(ir_function_signature *ir)
  369. {
  370. do_function_inlining(&ir->body);
  371. }
  372. void
  373. ir_function_inlining_visitor::visit(ir_function *ir)
  374. {
  375. (void) ir;
  376. }
  377. void
  378. ir_function_inlining_visitor::visit(ir_expression *ir)
  379. {
  380. unsigned int operand;
  381. for (operand = 0; operand < ir->get_num_operands(); operand++) {
  382. ir->operands[operand]->accept(this);
  383. }
  384. }
  385. void
  386. ir_function_inlining_visitor::visit(ir_swizzle *ir)
  387. {
  388. ir->val->accept(this);
  389. }
  390. void
  391. ir_function_inlining_visitor::visit(ir_dereference *ir)
  392. {
  393. if (ir->mode == ir_dereference::ir_reference_array) {
  394. ir->selector.array_index->accept(this);
  395. }
  396. ir->var->accept(this);
  397. }
  398. void
  399. ir_function_inlining_visitor::visit(ir_assignment *ir)
  400. {
  401. ir->rhs->accept(this);
  402. }
  403. void
  404. ir_function_inlining_visitor::visit(ir_constant *ir)
  405. {
  406. (void) ir;
  407. }
  408. void
  409. ir_function_inlining_visitor::visit(ir_call *ir)
  410. {
  411. (void) ir;
  412. }
  413. void
  414. ir_function_inlining_visitor::visit(ir_return *ir)
  415. {
  416. (void) ir;
  417. }
  418. void
  419. ir_function_inlining_visitor::visit(ir_if *ir)
  420. {
  421. ir->condition->accept(this);
  422. do_function_inlining(&ir->then_instructions);
  423. do_function_inlining(&ir->else_instructions);
  424. }