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_builder.h" 25#include "program/prog_instruction.h" 26 27using namespace ir_builder; 28 29namespace ir_builder { 30 31void 32ir_factory::emit(ir_instruction *ir) 33{ 34 instructions->push_tail(ir); 35} 36 37ir_variable * 38ir_factory::make_temp(const glsl_type *type, const char *name) 39{ 40 ir_variable *var; 41 42 var = new(mem_ctx) ir_variable(type, name, ir_var_temporary); 43 emit(var); 44 45 return var; 46} 47 48ir_assignment * 49assign(deref lhs, operand rhs) 50{ 51 return assign(lhs, rhs, (1 << lhs.val->type->vector_elements) - 1); 52} 53 54ir_assignment * 55assign(deref lhs, operand rhs, int writemask) 56{ 57 void *mem_ctx = ralloc_parent(lhs.val); 58 59 ir_assignment *assign = new(mem_ctx) ir_assignment(lhs.val, 60 rhs.val, 61 writemask); 62 63 return assign; 64} 65 66ir_return * 67ret(operand retval) 68{ 69 void *mem_ctx = ralloc_parent(retval.val); 70 return new(mem_ctx) ir_return(retval.val); 71} 72 73ir_swizzle * 74swizzle(operand a, int swizzle, int components) 75{ 76 void *mem_ctx = ralloc_parent(a.val); 77 78 return new(mem_ctx) ir_swizzle(a.val, 79 GET_SWZ(swizzle, 0), 80 GET_SWZ(swizzle, 1), 81 GET_SWZ(swizzle, 2), 82 GET_SWZ(swizzle, 3), 83 components); 84} 85 86ir_swizzle * 87swizzle_for_size(operand a, unsigned components) 88{ 89 void *mem_ctx = ralloc_parent(a.val); 90 91 if (a.val->type->vector_elements < components) 92 components = a.val->type->vector_elements; 93 94 unsigned s[4] = { 0, 1, 2, 3 }; 95 for (int i = components; i < 4; i++) 96 s[i] = components - 1; 97 98 return new(mem_ctx) ir_swizzle(a.val, s, components); 99} 100 101ir_swizzle * 102swizzle_xxxx(operand a) 103{ 104 return swizzle(a, SWIZZLE_XXXX, 4); 105} 106 107ir_swizzle * 108swizzle_yyyy(operand a) 109{ 110 return swizzle(a, SWIZZLE_YYYY, 4); 111} 112 113ir_swizzle * 114swizzle_zzzz(operand a) 115{ 116 return swizzle(a, SWIZZLE_ZZZZ, 4); 117} 118 119ir_swizzle * 120swizzle_wwww(operand a) 121{ 122 return swizzle(a, SWIZZLE_WWWW, 4); 123} 124 125ir_swizzle * 126swizzle_x(operand a) 127{ 128 return swizzle(a, SWIZZLE_XXXX, 1); 129} 130 131ir_swizzle * 132swizzle_y(operand a) 133{ 134 return swizzle(a, SWIZZLE_YYYY, 1); 135} 136 137ir_swizzle * 138swizzle_z(operand a) 139{ 140 return swizzle(a, SWIZZLE_ZZZZ, 1); 141} 142 143ir_swizzle * 144swizzle_w(operand a) 145{ 146 return swizzle(a, SWIZZLE_WWWW, 1); 147} 148 149ir_swizzle * 150swizzle_xy(operand a) 151{ 152 return swizzle(a, SWIZZLE_XYZW, 2); 153} 154 155ir_swizzle * 156swizzle_xyz(operand a) 157{ 158 return swizzle(a, SWIZZLE_XYZW, 3); 159} 160 161ir_swizzle * 162swizzle_xyzw(operand a) 163{ 164 return swizzle(a, SWIZZLE_XYZW, 4); 165} 166 167ir_expression * 168expr(ir_expression_operation op, operand a) 169{ 170 void *mem_ctx = ralloc_parent(a.val); 171 172 return new(mem_ctx) ir_expression(op, a.val); 173} 174 175ir_expression * 176expr(ir_expression_operation op, operand a, operand b) 177{ 178 void *mem_ctx = ralloc_parent(a.val); 179 180 return new(mem_ctx) ir_expression(op, a.val, b.val); 181} 182 183ir_expression * 184expr(ir_expression_operation op, operand a, operand b, operand c) 185{ 186 void *mem_ctx = ralloc_parent(a.val); 187 188 return new(mem_ctx) ir_expression(op, a.val, b.val, c.val); 189} 190 191ir_expression *add(operand a, operand b) 192{ 193 return expr(ir_binop_add, a, b); 194} 195 196ir_expression *sub(operand a, operand b) 197{ 198 return expr(ir_binop_sub, a, b); 199} 200 201ir_expression *min2(operand a, operand b) 202{ 203 return expr(ir_binop_min, a, b); 204} 205 206ir_expression *max2(operand a, operand b) 207{ 208 return expr(ir_binop_max, a, b); 209} 210 211ir_expression *mul(operand a, operand b) 212{ 213 return expr(ir_binop_mul, a, b); 214} 215 216ir_expression *imul_high(operand a, operand b) 217{ 218 return expr(ir_binop_imul_high, a, b); 219} 220 221ir_expression *div(operand a, operand b) 222{ 223 return expr(ir_binop_div, a, b); 224} 225 226ir_expression *carry(operand a, operand b) 227{ 228 return expr(ir_binop_carry, a, b); 229} 230 231ir_expression *borrow(operand a, operand b) 232{ 233 return expr(ir_binop_borrow, a, b); 234} 235 236ir_expression *trunc(operand a) 237{ 238 return expr(ir_unop_trunc, a); 239} 240 241ir_expression *round_even(operand a) 242{ 243 return expr(ir_unop_round_even, a); 244} 245 246ir_expression *fract(operand a) 247{ 248 return expr(ir_unop_fract, a); 249} 250 251/* dot for vectors, mul for scalars */ 252ir_expression *dot(operand a, operand b) 253{ 254 assert(a.val->type == b.val->type); 255 256 if (a.val->type->vector_elements == 1) 257 return expr(ir_binop_mul, a, b); 258 259 return expr(ir_binop_dot, a, b); 260} 261 262ir_expression* 263clamp(operand a, operand b, operand c) 264{ 265 return expr(ir_binop_min, expr(ir_binop_max, a, b), c); 266} 267 268ir_expression * 269saturate(operand a) 270{ 271 return expr(ir_unop_saturate, a); 272} 273 274ir_expression * 275abs(operand a) 276{ 277 return expr(ir_unop_abs, a); 278} 279 280ir_expression * 281neg(operand a) 282{ 283 return expr(ir_unop_neg, a); 284} 285 286ir_expression * 287sin(operand a) 288{ 289 return expr(ir_unop_sin, a); 290} 291 292ir_expression * 293cos(operand a) 294{ 295 return expr(ir_unop_cos, a); 296} 297 298ir_expression * 299exp(operand a) 300{ 301 return expr(ir_unop_exp, a); 302} 303 304ir_expression * 305rcp(operand a) 306{ 307 return expr(ir_unop_rcp, a); 308} 309 310ir_expression * 311rsq(operand a) 312{ 313 return expr(ir_unop_rsq, a); 314} 315 316ir_expression * 317sqrt(operand a) 318{ 319 return expr(ir_unop_sqrt, a); 320} 321 322ir_expression * 323log(operand a) 324{ 325 return expr(ir_unop_log, a); 326} 327 328ir_expression * 329sign(operand a) 330{ 331 return expr(ir_unop_sign, a); 332} 333 334ir_expression * 335subr_to_int(operand a) 336{ 337 return expr(ir_unop_subroutine_to_int, a); 338} 339 340ir_expression* 341equal(operand a, operand b) 342{ 343 return expr(ir_binop_equal, a, b); 344} 345 346ir_expression* 347nequal(operand a, operand b) 348{ 349 return expr(ir_binop_nequal, a, b); 350} 351 352ir_expression* 353less(operand a, operand b) 354{ 355 return expr(ir_binop_less, a, b); 356} 357 358ir_expression* 359greater(operand a, operand b) 360{ 361 return expr(ir_binop_less, b, a); 362} 363 364ir_expression* 365lequal(operand a, operand b) 366{ 367 return expr(ir_binop_gequal, b, a); 368} 369 370ir_expression* 371gequal(operand a, operand b) 372{ 373 return expr(ir_binop_gequal, a, b); 374} 375 376ir_expression* 377logic_not(operand a) 378{ 379 return expr(ir_unop_logic_not, a); 380} 381 382ir_expression* 383logic_and(operand a, operand b) 384{ 385 return expr(ir_binop_logic_and, a, b); 386} 387 388ir_expression* 389logic_or(operand a, operand b) 390{ 391 return expr(ir_binop_logic_or, a, b); 392} 393 394ir_expression* 395bit_not(operand a) 396{ 397 return expr(ir_unop_bit_not, a); 398} 399 400ir_expression* 401bit_and(operand a, operand b) 402{ 403 return expr(ir_binop_bit_and, a, b); 404} 405 406ir_expression* 407bit_or(operand a, operand b) 408{ 409 return expr(ir_binop_bit_or, a, b); 410} 411 412ir_expression* 413bit_xor(operand a, operand b) 414{ 415 return expr(ir_binop_bit_xor, a, b); 416} 417 418ir_expression* 419lshift(operand a, operand b) 420{ 421 return expr(ir_binop_lshift, a, b); 422} 423 424ir_expression* 425rshift(operand a, operand b) 426{ 427 return expr(ir_binop_rshift, a, b); 428} 429 430ir_expression* 431f2i(operand a) 432{ 433 return expr(ir_unop_f2i, a); 434} 435 436ir_expression* 437bitcast_f2i(operand a) 438{ 439 return expr(ir_unop_bitcast_f2i, a); 440} 441 442ir_expression* 443i2f(operand a) 444{ 445 return expr(ir_unop_i2f, a); 446} 447 448ir_expression* 449bitcast_i2f(operand a) 450{ 451 return expr(ir_unop_bitcast_i2f, a); 452} 453 454ir_expression* 455i2u(operand a) 456{ 457 return expr(ir_unop_i2u, a); 458} 459 460ir_expression* 461u2i(operand a) 462{ 463 return expr(ir_unop_u2i, a); 464} 465 466ir_expression* 467f2u(operand a) 468{ 469 return expr(ir_unop_f2u, a); 470} 471 472ir_expression* 473bitcast_f2u(operand a) 474{ 475 return expr(ir_unop_bitcast_f2u, a); 476} 477 478ir_expression* 479u2f(operand a) 480{ 481 return expr(ir_unop_u2f, a); 482} 483 484ir_expression* 485bitcast_u2f(operand a) 486{ 487 return expr(ir_unop_bitcast_u2f, a); 488} 489 490ir_expression* 491i2b(operand a) 492{ 493 return expr(ir_unop_i2b, a); 494} 495 496ir_expression* 497b2i(operand a) 498{ 499 return expr(ir_unop_b2i, a); 500} 501 502ir_expression * 503f2b(operand a) 504{ 505 return expr(ir_unop_f2b, a); 506} 507 508ir_expression * 509b2f(operand a) 510{ 511 return expr(ir_unop_b2f, a); 512} 513 514ir_expression* 515bitcast_d2i64(operand a) 516{ 517 return expr(ir_unop_bitcast_d2i64, a); 518} 519 520ir_expression* 521bitcast_d2u64(operand a) 522{ 523 return expr(ir_unop_bitcast_d2u64, a); 524} 525 526ir_expression* 527bitcast_i642d(operand a) 528{ 529 return expr(ir_unop_bitcast_i642d, a); 530} 531 532ir_expression* 533bitcast_u642d(operand a) 534{ 535 return expr(ir_unop_bitcast_u642d, a); 536} 537 538ir_expression * 539interpolate_at_centroid(operand a) 540{ 541 return expr(ir_unop_interpolate_at_centroid, a); 542} 543 544ir_expression * 545interpolate_at_offset(operand a, operand b) 546{ 547 return expr(ir_binop_interpolate_at_offset, a, b); 548} 549 550ir_expression * 551interpolate_at_sample(operand a, operand b) 552{ 553 return expr(ir_binop_interpolate_at_sample, a, b); 554} 555 556ir_expression * 557f2d(operand a) 558{ 559 return expr(ir_unop_f2d, a); 560} 561 562ir_expression * 563i2d(operand a) 564{ 565 return expr(ir_unop_i2d, a); 566} 567 568ir_expression * 569u2d(operand a) 570{ 571 return expr(ir_unop_u2d, a); 572} 573 574ir_expression * 575fma(operand a, operand b, operand c) 576{ 577 return expr(ir_triop_fma, a, b, c); 578} 579 580ir_expression * 581lrp(operand x, operand y, operand a) 582{ 583 return expr(ir_triop_lrp, x, y, a); 584} 585 586ir_expression * 587csel(operand a, operand b, operand c) 588{ 589 return expr(ir_triop_csel, a, b, c); 590} 591 592ir_expression * 593bitfield_extract(operand a, operand b, operand c) 594{ 595 return expr(ir_triop_bitfield_extract, a, b, c); 596} 597 598ir_expression * 599bitfield_insert(operand a, operand b, operand c, operand d) 600{ 601 void *mem_ctx = ralloc_parent(a.val); 602 return new(mem_ctx) ir_expression(ir_quadop_bitfield_insert, 603 a.val->type, a.val, b.val, c.val, d.val); 604} 605 606ir_if* 607if_tree(operand condition, 608 ir_instruction *then_branch) 609{ 610 assert(then_branch != NULL); 611 612 void *mem_ctx = ralloc_parent(condition.val); 613 614 ir_if *result = new(mem_ctx) ir_if(condition.val); 615 result->then_instructions.push_tail(then_branch); 616 return result; 617} 618 619ir_if* 620if_tree(operand condition, 621 ir_instruction *then_branch, 622 ir_instruction *else_branch) 623{ 624 assert(then_branch != NULL); 625 assert(else_branch != NULL); 626 627 void *mem_ctx = ralloc_parent(condition.val); 628 629 ir_if *result = new(mem_ctx) ir_if(condition.val); 630 result->then_instructions.push_tail(then_branch); 631 result->else_instructions.push_tail(else_branch); 632 return result; 633} 634 635} /* namespace ir_builder */ 636