Path: blob/21.2-virgl/src/compiler/glsl/ir_builder.cpp
4545 views
/*1* Copyright © 2012 Intel Corporation2*3* Permission is hereby granted, free of charge, to any person obtaining a4* copy of this software and associated documentation files (the "Software"),5* to deal in the Software without restriction, including without limitation6* the rights to use, copy, modify, merge, publish, distribute, sublicense,7* and/or sell copies of the Software, and to permit persons to whom the8* Software is furnished to do so, subject to the following conditions:9*10* The above copyright notice and this permission notice (including the next11* paragraph) shall be included in all copies or substantial portions of the12* Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL17* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER18* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING19* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS20* IN THE SOFTWARE.21*/2223#include "ir_builder.h"24#include "program/prog_instruction.h"2526using namespace ir_builder;2728namespace ir_builder {2930void31ir_factory::emit(ir_instruction *ir)32{33instructions->push_tail(ir);34}3536ir_variable *37ir_factory::make_temp(const glsl_type *type, const char *name)38{39ir_variable *var;4041var = new(mem_ctx) ir_variable(type, name, ir_var_temporary);42emit(var);4344return var;45}4647ir_assignment *48assign(deref lhs, operand rhs, operand condition, int writemask)49{50void *mem_ctx = ralloc_parent(lhs.val);5152ir_assignment *assign = new(mem_ctx) ir_assignment(lhs.val,53rhs.val,54condition.val,55writemask);5657return assign;58}5960ir_assignment *61assign(deref lhs, operand rhs)62{63return assign(lhs, rhs, (1 << lhs.val->type->vector_elements) - 1);64}6566ir_assignment *67assign(deref lhs, operand rhs, int writemask)68{69return assign(lhs, rhs, (ir_rvalue *) NULL, writemask);70}7172ir_assignment *73assign(deref lhs, operand rhs, operand condition)74{75return assign(lhs, rhs, condition, (1 << lhs.val->type->vector_elements) - 1);76}7778ir_return *79ret(operand retval)80{81void *mem_ctx = ralloc_parent(retval.val);82return new(mem_ctx) ir_return(retval.val);83}8485ir_swizzle *86swizzle(operand a, int swizzle, int components)87{88void *mem_ctx = ralloc_parent(a.val);8990return new(mem_ctx) ir_swizzle(a.val,91GET_SWZ(swizzle, 0),92GET_SWZ(swizzle, 1),93GET_SWZ(swizzle, 2),94GET_SWZ(swizzle, 3),95components);96}9798ir_swizzle *99swizzle_for_size(operand a, unsigned components)100{101void *mem_ctx = ralloc_parent(a.val);102103if (a.val->type->vector_elements < components)104components = a.val->type->vector_elements;105106unsigned s[4] = { 0, 1, 2, 3 };107for (int i = components; i < 4; i++)108s[i] = components - 1;109110return new(mem_ctx) ir_swizzle(a.val, s, components);111}112113ir_swizzle *114swizzle_xxxx(operand a)115{116return swizzle(a, SWIZZLE_XXXX, 4);117}118119ir_swizzle *120swizzle_yyyy(operand a)121{122return swizzle(a, SWIZZLE_YYYY, 4);123}124125ir_swizzle *126swizzle_zzzz(operand a)127{128return swizzle(a, SWIZZLE_ZZZZ, 4);129}130131ir_swizzle *132swizzle_wwww(operand a)133{134return swizzle(a, SWIZZLE_WWWW, 4);135}136137ir_swizzle *138swizzle_x(operand a)139{140return swizzle(a, SWIZZLE_XXXX, 1);141}142143ir_swizzle *144swizzle_y(operand a)145{146return swizzle(a, SWIZZLE_YYYY, 1);147}148149ir_swizzle *150swizzle_z(operand a)151{152return swizzle(a, SWIZZLE_ZZZZ, 1);153}154155ir_swizzle *156swizzle_w(operand a)157{158return swizzle(a, SWIZZLE_WWWW, 1);159}160161ir_swizzle *162swizzle_xy(operand a)163{164return swizzle(a, SWIZZLE_XYZW, 2);165}166167ir_swizzle *168swizzle_xyz(operand a)169{170return swizzle(a, SWIZZLE_XYZW, 3);171}172173ir_swizzle *174swizzle_xyzw(operand a)175{176return swizzle(a, SWIZZLE_XYZW, 4);177}178179ir_expression *180expr(ir_expression_operation op, operand a)181{182void *mem_ctx = ralloc_parent(a.val);183184return new(mem_ctx) ir_expression(op, a.val);185}186187ir_expression *188expr(ir_expression_operation op, operand a, operand b)189{190void *mem_ctx = ralloc_parent(a.val);191192return new(mem_ctx) ir_expression(op, a.val, b.val);193}194195ir_expression *196expr(ir_expression_operation op, operand a, operand b, operand c)197{198void *mem_ctx = ralloc_parent(a.val);199200return new(mem_ctx) ir_expression(op, a.val, b.val, c.val);201}202203ir_expression *add(operand a, operand b)204{205return expr(ir_binop_add, a, b);206}207208ir_expression *sub(operand a, operand b)209{210return expr(ir_binop_sub, a, b);211}212213ir_expression *min2(operand a, operand b)214{215return expr(ir_binop_min, a, b);216}217218ir_expression *max2(operand a, operand b)219{220return expr(ir_binop_max, a, b);221}222223ir_expression *mul(operand a, operand b)224{225return expr(ir_binop_mul, a, b);226}227228ir_expression *imul_high(operand a, operand b)229{230return expr(ir_binop_imul_high, a, b);231}232233ir_expression *div(operand a, operand b)234{235return expr(ir_binop_div, a, b);236}237238ir_expression *carry(operand a, operand b)239{240return expr(ir_binop_carry, a, b);241}242243ir_expression *borrow(operand a, operand b)244{245return expr(ir_binop_borrow, a, b);246}247248ir_expression *trunc(operand a)249{250return expr(ir_unop_trunc, a);251}252253ir_expression *round_even(operand a)254{255return expr(ir_unop_round_even, a);256}257258ir_expression *fract(operand a)259{260return expr(ir_unop_fract, a);261}262263/* dot for vectors, mul for scalars */264ir_expression *dot(operand a, operand b)265{266assert(a.val->type == b.val->type);267268if (a.val->type->vector_elements == 1)269return expr(ir_binop_mul, a, b);270271return expr(ir_binop_dot, a, b);272}273274ir_expression*275clamp(operand a, operand b, operand c)276{277return expr(ir_binop_min, expr(ir_binop_max, a, b), c);278}279280ir_expression *281saturate(operand a)282{283return expr(ir_unop_saturate, a);284}285286ir_expression *287abs(operand a)288{289return expr(ir_unop_abs, a);290}291292ir_expression *293neg(operand a)294{295return expr(ir_unop_neg, a);296}297298ir_expression *299sin(operand a)300{301return expr(ir_unop_sin, a);302}303304ir_expression *305cos(operand a)306{307return expr(ir_unop_cos, a);308}309310ir_expression *311exp(operand a)312{313return expr(ir_unop_exp, a);314}315316ir_expression *317rcp(operand a)318{319return expr(ir_unop_rcp, a);320}321322ir_expression *323rsq(operand a)324{325return expr(ir_unop_rsq, a);326}327328ir_expression *329sqrt(operand a)330{331return expr(ir_unop_sqrt, a);332}333334ir_expression *335log(operand a)336{337return expr(ir_unop_log, a);338}339340ir_expression *341sign(operand a)342{343return expr(ir_unop_sign, a);344}345346ir_expression *347subr_to_int(operand a)348{349return expr(ir_unop_subroutine_to_int, a);350}351352ir_expression*353equal(operand a, operand b)354{355return expr(ir_binop_equal, a, b);356}357358ir_expression*359nequal(operand a, operand b)360{361return expr(ir_binop_nequal, a, b);362}363364ir_expression*365less(operand a, operand b)366{367return expr(ir_binop_less, a, b);368}369370ir_expression*371greater(operand a, operand b)372{373return expr(ir_binop_less, b, a);374}375376ir_expression*377lequal(operand a, operand b)378{379return expr(ir_binop_gequal, b, a);380}381382ir_expression*383gequal(operand a, operand b)384{385return expr(ir_binop_gequal, a, b);386}387388ir_expression*389logic_not(operand a)390{391return expr(ir_unop_logic_not, a);392}393394ir_expression*395logic_and(operand a, operand b)396{397return expr(ir_binop_logic_and, a, b);398}399400ir_expression*401logic_or(operand a, operand b)402{403return expr(ir_binop_logic_or, a, b);404}405406ir_expression*407bit_not(operand a)408{409return expr(ir_unop_bit_not, a);410}411412ir_expression*413bit_and(operand a, operand b)414{415return expr(ir_binop_bit_and, a, b);416}417418ir_expression*419bit_or(operand a, operand b)420{421return expr(ir_binop_bit_or, a, b);422}423424ir_expression*425bit_xor(operand a, operand b)426{427return expr(ir_binop_bit_xor, a, b);428}429430ir_expression*431lshift(operand a, operand b)432{433return expr(ir_binop_lshift, a, b);434}435436ir_expression*437rshift(operand a, operand b)438{439return expr(ir_binop_rshift, a, b);440}441442ir_expression*443f2i(operand a)444{445return expr(ir_unop_f2i, a);446}447448ir_expression*449bitcast_f2i(operand a)450{451return expr(ir_unop_bitcast_f2i, a);452}453454ir_expression*455i2f(operand a)456{457return expr(ir_unop_i2f, a);458}459460ir_expression*461bitcast_i2f(operand a)462{463return expr(ir_unop_bitcast_i2f, a);464}465466ir_expression*467i2u(operand a)468{469return expr(ir_unop_i2u, a);470}471472ir_expression*473u2i(operand a)474{475return expr(ir_unop_u2i, a);476}477478ir_expression*479f2u(operand a)480{481return expr(ir_unop_f2u, a);482}483484ir_expression*485bitcast_f2u(operand a)486{487return expr(ir_unop_bitcast_f2u, a);488}489490ir_expression*491u2f(operand a)492{493return expr(ir_unop_u2f, a);494}495496ir_expression*497bitcast_u2f(operand a)498{499return expr(ir_unop_bitcast_u2f, a);500}501502ir_expression*503i2b(operand a)504{505return expr(ir_unop_i2b, a);506}507508ir_expression*509b2i(operand a)510{511return expr(ir_unop_b2i, a);512}513514ir_expression *515f2b(operand a)516{517return expr(ir_unop_f2b, a);518}519520ir_expression *521b2f(operand a)522{523return expr(ir_unop_b2f, a);524}525526ir_expression*527bitcast_d2i64(operand a)528{529return expr(ir_unop_bitcast_d2i64, a);530}531532ir_expression*533bitcast_d2u64(operand a)534{535return expr(ir_unop_bitcast_d2u64, a);536}537538ir_expression*539bitcast_i642d(operand a)540{541return expr(ir_unop_bitcast_i642d, a);542}543544ir_expression*545bitcast_u642d(operand a)546{547return expr(ir_unop_bitcast_u642d, a);548}549550ir_expression *551interpolate_at_centroid(operand a)552{553return expr(ir_unop_interpolate_at_centroid, a);554}555556ir_expression *557interpolate_at_offset(operand a, operand b)558{559return expr(ir_binop_interpolate_at_offset, a, b);560}561562ir_expression *563interpolate_at_sample(operand a, operand b)564{565return expr(ir_binop_interpolate_at_sample, a, b);566}567568ir_expression *569f2d(operand a)570{571return expr(ir_unop_f2d, a);572}573574ir_expression *575i2d(operand a)576{577return expr(ir_unop_i2d, a);578}579580ir_expression *581u2d(operand a)582{583return expr(ir_unop_u2d, a);584}585586ir_expression *587fma(operand a, operand b, operand c)588{589return expr(ir_triop_fma, a, b, c);590}591592ir_expression *593lrp(operand x, operand y, operand a)594{595return expr(ir_triop_lrp, x, y, a);596}597598ir_expression *599csel(operand a, operand b, operand c)600{601return expr(ir_triop_csel, a, b, c);602}603604ir_expression *605bitfield_extract(operand a, operand b, operand c)606{607return expr(ir_triop_bitfield_extract, a, b, c);608}609610ir_expression *611bitfield_insert(operand a, operand b, operand c, operand d)612{613void *mem_ctx = ralloc_parent(a.val);614return new(mem_ctx) ir_expression(ir_quadop_bitfield_insert,615a.val->type, a.val, b.val, c.val, d.val);616}617618ir_if*619if_tree(operand condition,620ir_instruction *then_branch)621{622assert(then_branch != NULL);623624void *mem_ctx = ralloc_parent(condition.val);625626ir_if *result = new(mem_ctx) ir_if(condition.val);627result->then_instructions.push_tail(then_branch);628return result;629}630631ir_if*632if_tree(operand condition,633ir_instruction *then_branch,634ir_instruction *else_branch)635{636assert(then_branch != NULL);637assert(else_branch != NULL);638639void *mem_ctx = ralloc_parent(condition.val);640641ir_if *result = new(mem_ctx) ir_if(condition.val);642result->then_instructions.push_tail(then_branch);643result->else_instructions.push_tail(else_branch);644return result;645}646647} /* namespace ir_builder */648649650