summaryrefslogtreecommitdiffstats
path: root/src/glsl/ast_function.cpp
Commit message (Collapse)AuthorAgeFilesLines
* glsl: Enable GLSL ES 3.00 features inherited from desktop GLSL.Paul Berry2012-12-061-3/+4
| | | | | | | | | | | | | | | | | | | | This patch turns on the following features for GLSL ES 3.00: - Array constructors, whole array assignment, and array comparisons. - Second and third operands of ?: may be arrays. - Use of "in" and "out" qualifiers on globals. - Bitwise and modulus operators. - Integral vertex shader inputs. - Range-checking of literal integers. - array.length method. - Function calls may be constant expressions. - Integral varyings must be qualified with "flat". - Interpolation and centroid qualifiers may not be applied to vertex shader inputs. Reviewed-by: Ian Romanick <[email protected]> Reviewed-by: Kenneth Graunke <[email protected]> Acked-by: Carl Worth <[email protected]>
* glsl: Make use of new _mesa_glsl_parse_state::check_version() function.Paul Berry2012-12-061-8/+7
| | | | | | | | | | | | | | | | | | Previous to this patch, we were not very consistent about the errors we generate when a shader tried to use a feature that is prohibited in the current GLSL version. Some error messages failed to mention the GLSL version currently in use (or did so inaccurately), and some error messages failed to mention the first GLSL version in which the given feature is allowed. This patch reworks all of the error checks to use the check_version() function, which produces error messages in a standard form (approximately "$FEATURE forbidden in $CURRENT_GLSL_VERSION ($REQUIRED_GLSL_VERSION required)."). Reviewed-by: Ian Romanick <[email protected]> Reviewed-by: Kenneth Graunke <[email protected]> Acked-by: Carl Worth <[email protected]>
* glsl: Make use of new _mesa_glsl_parse_state::is_version() function.Paul Berry2012-12-061-1/+1
| | | | | | Reviewed-by: Ian Romanick <[email protected]> Reviewed-by: Kenneth Graunke <[email protected]> Acked-by: Carl Worth <[email protected]>
* glsl: Simplify symbol table version checking.Paul Berry2012-12-061-1/+2
| | | | | | | | | | | Previously, we stored the GLSL language version in the glsl_symbol_table struct. But this was unnecessary--all glsl_symbol_table needs to know is whether functions and variables have separate namespaces (they do in GLSL 1.10 only). Reviewed-by: Ian Romanick <[email protected]> Reviewed-by: Kenneth Graunke <[email protected]> Acked-by: Carl Worth <[email protected]>
* glsl: Use ir_unop_f2u to convert floats to uints.Paul Berry2012-06-151-2/+1
| | | | | | | Fixes piglit tests spec/glsl-1.30/execution/{vs,fs}-float-uint-conversion on i965. Reviewed-by: Kenneth Graunke <[email protected]>
* glsl: Add a variable context to constant_expression_value().Olivier Galibert2012-05-081-1/+1
| | | | | | Signed-off-by: Olivier Galibert <[email protected]> Reviewed-by: Kenneth Graunke <[email protected]> Reviewed-by: Eric Anholt <[email protected]> [v1]
* glsl: Fix regression in function out-parameter lvalue detection.Eric Anholt2012-05-041-14/+14
| | | | | | | | | | When doing the var->assigned change in f2475ca424f7e001be50f64dafa5700f6603d684, I overzealously indented the second block of code into the "if (var)" test. Revert these blocks to the way they were before, just taking advantage of "var" to avoid re-calling variable_referenced(). Bugzilla: https://bugs.freedesktop.org/show_bug.cgi?id=49066
* glsl: Track in each ir_variable whether it was ever assigned.Eric Anholt2012-04-191-13/+16
| | | | | | | This will be used for some compile-and-link-time error checking, where currently we've been doing error checking only at link time. Reviewed-by: Kenneth Graunke <[email protected]>
* glsl: Demote 'type' from ir_instruction to ir_rvalue and ir_variable.Kenneth Graunke2012-04-021-1/+1
| | | | | | | | | | | | | Variables have types, expression trees have types, but statements don't. Rather than have a nonsensical field that stays NULL in the base class, just move it to where it makes sense. Fix up a few places that lazily used ir_instruction even though they actually knew the particular subclass. Signed-off-by: Kenneth Graunke <[email protected]> Reviewed-by: Eric Anholt <[email protected]> Reviewed-by: Ian Romanick <[email protected]>
* glsl: Convert ir_call to be a statement rather than a value.Kenneth Graunke2012-04-021-29/+17
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | Aside from ir_call, our IR is cleanly split into two classes: - Statements (typeless; used for side effects, control flow) - Values (deeply nestable, pure, typed expression trees) Unfortunately, ir_call confused all this: - For void functions, we placed ir_call directly in the instruction stream, treating it as an untyped statement. Yet, it was a subclass of ir_rvalue, and no other ir_rvalue could be used in this way. - For functions with a return value, ir_call could be placed in arbitrary expression trees. While this fit naturally with the source language, it meant that expressions might not be pure, making it difficult to transform and optimize them. To combat this, we always emitted ir_call directly in the RHS of an ir_assignment, only using a temporary variable in expression trees. Many passes relied on this assumption; the acos and atan built-ins violated it. This patch makes ir_call a statement (ir_instruction) rather than a value (ir_rvalue). Non-void calls now take a ir_dereference of a variable, and store the return value there---effectively a call and assignment rolled into one. They cannot be embedded in expressions. All expression trees are now pure, without exception. Signed-off-by: Kenneth Graunke <[email protected]> Reviewed-by: Eric Anholt <[email protected]> Reviewed-by: Ian Romanick <[email protected]>
* glsl: Use ir_rvalue to represent generic error_type values.Kenneth Graunke2012-04-021-16/+16
| | | | | | | | | | | | | | | | | | | | | | Currently, ir_call can be used as either a statement (for void functions) or a value (for non-void functions). This is rather awkward, as it's the only class that can be used in both forms. A number of places use ir_call::get_error_instruction() to construct a generic value of error_type. If ir_call is to become a statement, it can no longer serve this purpose. Unfortunately, none of our classes are particularly well suited for this, and creating a new one would be rather aggrandizing. So, this patch introduces ir_rvalue::error_value(), a static method that creates an instance of the base class, ir_rvalue. This has the nice property that you can't accidentally try and access uninitialized fields (as it doesn't have any). The downside is that the base class is no longer abstract. Signed-off-by: Kenneth Graunke <[email protected]> Reviewed-by: Eric Anholt <[email protected]> Reviewed-by: Ian Romanick <[email protected]>
* glsl: Combine AST-level and IR-level parameter mode checking loops.Kenneth Graunke2012-04-021-82/+85
| | | | | | | | | | | | | generate_call() and ast_function_expression::hir() both tried to verify that 'out' and 'inout' parameters used l-values. Irritatingly, it turned out that this was not redundant; both checks caught -some- cases. This patch combines the two into a single "complete" function that does all the parameter mode checking. It also adds a comment clarifying why AST-level checking is necessary in the first place. Signed-off-by: Kenneth Graunke <[email protected]> Reviewed-by: Ian Romanick <[email protected]>
* glsl: Split up function matching and call generation a bit more.Kenneth Graunke2012-04-021-35/+47
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | We used to have one big function, match_signature_by_name, which found a matching signature, performed out-parameter conversions, and generated the ir_call. As the code for matching against built-in functions became more complicated, I split it internally, creating generate_call(). However, I left the same awkward interface. This patch splits it into three functions: 1. match_signature_by_name() This now takes a name, a list of parameters, the symbol table, and returns an ir_function_signature. Simple and one purpose: matching. 2. no_matching_function_error() Generate the "no matching function" error and list of prototypes. This was complex enough that I felt it deserved its own function. 3. generate_call() Do the out-parameter conversion and generate the ir_call. This could probably use more splitting. The caller now has a more natural workflow: find a matching signature, then either generate an error or a call. Signed-off-by: Kenneth Graunke <[email protected]> Reviewed-by: Eric Anholt <[email protected]> Reviewed-by: Ian Romanick <[email protected]>
* glsl: Emit extra errors for l-value violations in 'out' or 'inout' parametersIan Romanick2012-01-061-4/+59
| | | | | | | | | | Somethings, like pre-increment operations, were not previously caught. After the 8.0 release, this code needs some major refactoring and clean-up. It's a mess. :( Signed-off-by: Ian Romanick <[email protected]> Reviewed-by: Paul Berry <[email protected]> Bugzilla: https://bugs.freedesktop.org/show_bug.cgi?id=42755
* glsl: Always search for an exact function signature match.Kenneth Graunke2011-11-141-29/+63
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | Previously, we would fail to compile the following shader due to a bug in lazy built-in importing: #version 130 void main() { float f = abs(5.0); int i = abs(5); } The first call, abs(5.0), would fail to find a local signature, look through the built-ins, and import "float abs(float)". The second call, abs(5), would find the newly imported float signature in the local shader, and settle for that. Unfortunately, it failed to search the built-ins for the correct/exact signature, "int abs(int)". Thus, abs(5) ended up being a float, causing a bizarre type error when we tried to assign it to an int. Fixes piglit test builtin-overload-matching.frag. This is /not/ a candidate for stable branches, as it should only be possible to trigger this bug using GLSL 1.30's built-in functions that take integer arguments. Plus, the changes are fairly invasive. Signed-off-by: Kenneth Graunke <[email protected]>
* glsl: Split code to generate an ir_call out from match_function_by_name.Kenneth Graunke2011-11-141-165/+173
| | | | | | | | | | | | | | | | | | | | | | match_function_by_name performs two fairly separate tasks: 1. Hunt down the appropriate ir_function_signature for the callee. 2. Generate the actual ir_call (assuming we found the callee). Both of these are complicated. The first has to handle exact/inexact matches, lazy importing of built-in prototypes, different scoping rules for 1.10, 1.20+, and ES. Not to mention printing a user-friendly error message with pretty-printed "maybe you meant this" candidate signatures. The second has to deal with void/non-void functions, pre-call implicit conversions for "in" parmeters, and post-call "out" call conversions. Trying to do both in one function is just too unwieldy. Time to split. This patch purely moves the code to generate an ir_call into a separate function and reindents it. Otherwise, the code is identical. Signed-off-by: Kenneth Graunke <[email protected]> Reviewed-by: Paul Berry <[email protected]>
* glsl: Defer initialization of built-in functions until they're needed.Kenneth Graunke2011-09-231-0/+1
| | | | | | | | | | | | | | | Very simple shaders don't actually use GLSL built-ins. For example: - gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex; - gl_FragColor = vec4(0.0); Both of the shaders used by _mesa_meta_glsl_Clear() also qualify. By waiting to initialize the built-ins until the first time we need to look for a signature, we can avoid the overhead entirely in these cases. Makes piglit run roughly 18% faster (255 vs. 312 seconds). Signed-off-by: Kenneth Graunke <[email protected]> Reviewed-by: Eric Anholt <[email protected]>
* glsl: fix crash when a const is passed to texelFetchOffsetDave Airlie2011-08-251-0/+1
| | | | | | | while debugging texelFetchOffset we kept hitting the assert. Signed-off-by: Dave Airlie <[email protected]> Reviewed-by: Kenneth Graunke <[email protected]>
* glsl: Bail after reporting an error for non-constant const_in parameters.Kenneth Graunke2011-08-251-0/+1
| | | | | | | | | | Otherwise we continue and hit the "Illegal formal parameter mode" assertion. Fixes negative compile test texelFetchOffset.frag in piglit. Signed-off-by: Kenneth Graunke <[email protected]> Signed-off-by: Dave Airlie <[email protected]>
* glsl: Perform implicit type conversions on function call out parameters.Paul Berry2011-08-151-5/+71
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | When an out parameter undergoes an implicit type conversion, we need to store it in a temporary, and then after the call completes, convert the resulting value. In other words, we convert code like the following: void f(out int x); float value; f(value); Into IR that's equivalent to this: void f(out int x); float value; int out_parameter_conversion; f(out_parameter_conversion); value = float(out_parameter_conversion); This transformation needs to happen during ast-to-IR convertion (as opposed to, say, a lowering pass), because it is invalid IR for formal and actual parameters to have types that don't match. Fixes piglit tests spec/glsl-1.20/compiler/qualifiers/out-conversion-int-to-float.vert and spec/glsl-1.20/execution/qualifiers/vs-out-conversion-*.shader_test, and bug 39651. Bugzilla: https://bugs.freedesktop.org/show_bug.cgi?id=39651 Reviewed-by: Chad Versace <[email protected]>
* glsl: Constant-fold built-in functions before outputting IRPaul Berry2011-08-081-2/+14
| | | | | | | | | | | | | | Rearranged the logic for converting the ast for a function call to hir, so that we constant fold before emitting any IR. Previously we would emit some IR, and then only later detect whether we could constant fold. The unnecessary IR would usually get cleaned up by a later optimization step, however in the case of a builtin function being used to compute an array size, it was causing an assertion. Fixes Piglit test array-size-constant-relational.vert. Reviewed-by: Kenneth Graunke <[email protected]> Bugzilla: https://bugs.freedesktop.org/show_bug.cgi?id=38625
* glsl: Emit function signatures at toplevel, even for built-ins.Paul Berry2011-08-081-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | The ast-to-hir conversion needs to emit function signatures in two circumstances: when a function declaration (or definition) is encountered, and when a built-in function is encountered. To avoid emitting a function signature in an illegal place (such as inside a function), emit_function() checked whether we were inside a function definition, and if so, emitted the signature before the function definition. However, this didn't cover the case of emitting function signatures for built-in functions when those built-in functions are called from inside the constant integer expression that specifies the length of a global array. This failed because when processing an array length, we are emitting IR into a dummy exec_list (see process_array_type() in ast_to_hir.cpp). process_array_type() later checks (via an assertion) that no instructions were emitted to the dummy exec_list, based on the reasonable assumption that we shouldn't need to emit instructions to calculate the value of a constant. This patch changes emit_function() so that it emits function signatures at toplevel in all cases. This partially fixes bug 38625 (https://bugs.freedesktop.org/show_bug.cgi?id=38625). The remainder of the fix is in the patch that follows. Reviewed-by: Kenneth Graunke <[email protected]>
* glsl: Fix conversions in array constructorsChad Versace2011-07-301-2/+10
| | | | | | | | | | | | | | | | | | | | | | Array constructors obey narrower conversion rules than other constructors [1] --- they use the implicit conversion rules [2] instead of the scalar constructor conversions [3]. But process_array_constructor() was incorrectly applying the broader rules. [1] GLSL 1.50 spec, Section 5.4.4 Array Constructors, page 52 (58 of pdf) [2] GLSL 1.50 spec, Section 4.1.10 Implicit Conversions, page 25 (31 of pdf) [3] GLSL 1.50 spec, Section 5.4.1 Conversion, page 48 (54 of pdf) To fix this, first check (with glsl_type::can_be_implicitly_converted_to) if an implicit conversion is legal before performing the conversion. Fixes: piglit:spec/glsl-1.20/compiler/structure-and-array-operations/array-ctor-implicit-conversion-bool-float.vert piglit:spec/glsl-1.20/compiler/structure-and-array-operations/array-ctor-implicit-conversion-bvec*-vec*.vert Note: This is a candidate for the 7.10 and 7.11 branches. Reviewed-by: Kenneth Graunke <[email protected]> Signed-off-by: Chad Versace <[email protected]>
* glsl: Make prototype_string publicly availableIan Romanick2011-07-201-2/+4
| | | | | | | | Also clarify the documentation for one of the parameters. Reviewed-by: Paul Berry <[email protected]> Reviewed-by: Eric Anholt <[email protected]> Reviewed-by: Kenneth Graunke <[email protected]>
* glsl: Use i2u and u2i to implement constructor conversions.Kenneth Graunke2011-06-291-6/+29
| | | | | | | | | | | | | | Inspired by a patch from Bryan Cain <[email protected]>. Fixes piglit tests: - ctor-int-uint.vert - ctor-ivec4-uvec4.vert - ctor-uint-int.vert - ctor-uvec4-ivec4.vert Signed-off-by: Kenneth Graunke <[email protected]> Reviewed-by: Ian Romanick <[email protected]> Reviewed-by: Eric Anholt <[email protected]>
* glsl: Remove extra newline from error messageIan Romanick2011-05-031-1/+1
|
* glsl: 80-column wrapping and whitespace fixesIan Romanick2011-04-181-1/+5
|
* glsl: Finish out the reduce/reduce error fixesIan Romanick2011-02-111-48/+61
| | | | | | | | | | | | | Track variables, functions, and types during parsing. Use this information in the lexer to return the currect "type" for identifiers. Change the handling of structure constructors. They will now show up in the AST as constructors (instead of plain function calls). Fixes piglit tests constructor-18.vert, constructor-19.vert, and constructor-20.vert. Also fixes bugzilla #29926. NOTE: This is a candidate for the 7.9 and 7.10 branches.
* glsl: Fix invalid use of ralloc_asprintf in prototype_string.Kenneth Graunke2011-02-011-1/+1
| | | | | | | | This was my mistake when converting from talloc to ralloc. I was confused because the other calls in the function are to asprintf_append and the original code used str as the context rather than NULL. Fixes bug #33823.
* glsl: add cast to silence signed/unsigned comparison warningBrian Paul2011-01-311-1/+1
|
* glsl: Introduce a new "const_in" variable mode.Kenneth Graunke2011-01-311-0/+9
| | | | | | | | This annotation is for an "in" function parameter for which it is only legal to pass constant expressions. The only known example of this, currently, is the textureOffset functions. This should never be used for globals.
* Convert everything from the talloc API to the ralloc API.Kenneth Graunke2011-01-311-10/+10
|
* glsl: Improve error message when read-only vars are writtenChad Versace2011-01-211-6/+21
| | | | | | Improves the cases when: * an explicit assignment references the read-only variable * an 'out' or 'inout' function parameter references the read-only variable
* glsl: Properly add functions during lazy built-in prototype importing.Kenneth Graunke2010-12-061-7/+13
| | | | | | | | | | | | The original lazy built-in importing patch did not add the newly created function to the symbol table, nor actually emit it into the IR stream. Adding it to the symbol table is non-trivial since importing occurs when generating some ir_call in a nested scope. A new add_global_function method, backed by new symbol_table code in the previous patch, handles this. Fixes bug #32030.
* glsl: Lazily import built-in function prototypes.Kenneth Graunke2010-11-301-29/+40
| | | | | | | | This makes a very simple 1.30 shader go from 196k of memory to 9k. NOTE: This -may- be a candidate for the 7.9 branch, as the benefit is substantial. However, it's not a simple change, so it may be wiser to wait for 7.10.
* glsl: Fix matrix constructors with vector parametersIan Romanick2010-11-191-9/+9
| | | | | | | When the semantics of write masks in assignments were changed, this code was not correctly updated. Fixes piglit test glsl-mat-from-vec-ctor-01.
* glsl: Simplify generation of swizzle for vector constructorsIan Romanick2010-11-161-6/+5
|
* glsl: Remove unnecessary "unused variable" warning suppression.Kenneth Graunke2010-11-091-3/+0
| | | | The "instructions" variable -is- used, so the cast to void can go away.
* glsl: Fix constant component count in vector constructor emitting.Kenneth Graunke2010-10-251-1/+1
| | | | | Fixes freedesktop.org bug #31101 as well as piglit test cases assignment-type-mismatch.vert and constructor-28.vert.
* glsl: Rework assignments with write_masks to have LHS chan count match RHS.Eric Anholt2010-09-221-21/+22
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | It turns out that most people new to this IR are surprised when an assignment to (say) 3 components on the LHS takes 4 components on the RHS. It also makes for quite strange IR output: (assign (constant bool (1)) (x) (var_ref color) (swiz x (var_ref v) )) (assign (constant bool (1)) (y) (var_ref color) (swiz yy (var_ref v) )) (assign (constant bool (1)) (z) (var_ref color) (swiz zzz (var_ref v) )) But even worse, even we get it wrong, as shown by this line of our current step(float, vec4): (assign (constant bool (1)) (w) (var_ref t) (expression float b2f (expression bool >= (swiz w (var_ref x))(var_ref edge)))) where we try to assign a float to the writemasked-out x channel and don't supply anything for the actual w channel we're writing. Drivers right now just get lucky since ir_to_mesa spams the float value across all the source channels of a vec4. Instead, the RHS will now have a number of components equal to the number of components actually being written. Hopefully this confuses everyone less, and it also makes codegen for a scalar target simpler. Reviewed-by: Kenneth Graunke <[email protected]> Reviewed-by: Ian Romanick <[email protected]>
* ast_to_hir: Allow matrix-from-matrix constructors in GLSL ES.Kenneth Graunke2010-09-071-1/+1
| | | | Everything but 1.10 supports this, so just change the check to ==.
* glsl: Apply implicit conversions to structure constructor parameters.Kenneth Graunke2010-09-011-2/+9
| | | | | | | The code for handling implicit conversions should probably get refactored, but for now, this is easy. Fixes piglit test constructor-26.vert.
* glsl: Convert constant record constructor parameters to ir_constants.Kenneth Graunke2010-09-011-1/+3
| | | | I'm not sure if this is strictly necessary, but it seems wise.
* glsl: Reject structure constructors that have too many arguments.Kenneth Graunke2010-09-011-0/+6
| | | | Fixes piglit test constructor-27.vert.
* glsl2: Emit structure constructors inlineIan Romanick2010-09-011-37/+70
| | | | Fixes piglit test cases glsl-[fv]s-all-0[12].
* glsl: Fix write mask in matrix-from-matrix constructors.Kenneth Graunke2010-09-011-3/+5
| | | | | | | | | | | If the matrix being constructed was larger than the source matrix, it would overwrite the lower-right part of the matrix with the wrong values, rather than leaving it as the identity matrix. For example, constructing a mat4 from a mat2 should only use a writemask of "xy" when copying from the source, but was using "xyzw". Fixes the code generated by piglit test constructor-23.vert.
* glsl: Move generate_constructor_(matrix|vector) to ir_constant ctor.Kenneth Graunke2010-09-011-99/+1
|
* ast_function: Fix check for "too few components".Kenneth Graunke2010-09-011-1/+2
| | | | | | | | | This was triggering even for matrix-from-matrix constructors. It is perfectly legal to construct a mat3 from a mat2 - the rest will be filled in by the identity matrix. Changes piglit test constructor-23.vert from FAIL to PASS, but the generated code is incorrect.
* ast_function: Remove bogus cases from generate_constructor_matrix.Kenneth Graunke2010-09-011-29/+8
| | | | There are no integer matrix types, so switching on them is silly.
* glsl2: Write vector constructor constants in a single assignmentIan Romanick2010-08-311-11/+72
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Make two passes over the constructor parameters. Write all of the constants in a single write, then write the non-constants one at a time. This causes the fragment shader varying float g; void main() { gl_FragColor = vec4(0.0, g, 0.0, 1.0); } to generate (function main (signature void (parameters ) ( (declare (temporary ) vec4 vec_ctor@0x8580058) (assign (constant bool (1)) (xzw) (var_ref vec_ctor@0x8580058) (constant vec4 (0.000000 0.000000 0.000000 1.000000)) ) (assign (constant bool (1)) (y) (var_ref vec_ctor@0x8580058) (swiz xxxx (var_ref g@0x8580218) )) (assign (constant bool (1)) (xyzw) (var_ref gl_FragColor@0x84d32a0) (var_ref vec_ctor@0x8580058) ) )) ) instead of (function main (signature void (parameters ) ( (declare (temporary ) vec4 vec_ctor@0x8580058) (assign (constant bool (1)) (x) (var_ref vec_ctor@0x8580058) (constant vec4 (0.000000 0.000000 0.000000 1.000000)) ) (assign (constant bool (1)) (y) (var_ref vec_ctor@0x8580058) (swiz xxxx (var_ref g@0x8580218) )) (assign (constant bool (1)) (z) (var_ref vec_ctor@0x8580058) (constant vec4 (0.000000 0.000000 0.000000 1.000000)) ) (assign (constant bool (1)) (w) (var_ref vec_ctor@0x8580058) (constant vec4 (0.000000 0.000000 0.000000 1.000000)) ) (assign (constant bool (1)) (xyzw) (var_ref gl_FragColor@0x84d32a0) (var_ref vec_ctor@0x8580058) ) )) ) A similar optimization could be done for matrix constructors, but it is a little more complicate there.