Skip to content

[Feature] Optimizing ternary operators #28697

@mohammadfawaz

Description

@mohammadfawaz

🚀 Feature

Experimenting with code like this:

program foo.aleo {
    transition test(arr: [u8; 20]) -> [u8; 20] {
        for i in 0u32..20u32 {
            if i < 12 {
                arr[i] = 0;
            }
        }
        
        return arr;
    }
}

The output assembly is:

program foo.aleo;

function test:
    input r0 as [u8; 20u32].private;
    ternary true 0u8 r0[0u32] into r1;
    ternary true 0u8 r0[1u32] into r2;
    ternary true 0u8 r0[2u32] into r3;
    ternary true 0u8 r0[3u32] into r4;
    ternary true 0u8 r0[4u32] into r5;
    ternary true 0u8 r0[5u32] into r6;
    ternary true 0u8 r0[6u32] into r7;
    ternary true 0u8 r0[7u32] into r8;
    ternary true 0u8 r0[8u32] into r9;
    ternary true 0u8 r0[9u32] into r10;
    ternary true 0u8 r0[10u32] into r11;
    ternary true 0u8 r0[11u32] into r12;
    ternary false 0u8 r0[12u32] into r13;
    ternary false 0u8 r0[13u32] into r14;
    ternary false 0u8 r0[14u32] into r15;
    ternary false 0u8 r0[15u32] into r16;
    ternary false 0u8 r0[16u32] into r17;
    ternary false 0u8 r0[17u32] into r18;
    ternary false 0u8 r0[18u32] into r19;
    ternary false 0u8 r0[19u32] into r20;
    cast r1 r2 r3 r4 r5 r6 r7 r8 r9 r10 r11 r12 r13 r14 r15 r16 r17 r18 r19 r20 into r21 as [u8; 20u32];
    output r21 as [u8; 20u32].private;

All the ternary operators are unnecessary since their conditions are know at compile time.

Metadata

Metadata

Assignees

Labels

featureA new feature.🧱 Core CompilerAnything related to the core compiler including parsing, analysis, transforms, codegen, etc.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions