On Sun, Nov 8, 2026 at 4:16 PM Jeff Law <[email protected]> wrote:
>
> This is another issue spotted by LLM review of assembly code for x264
>
> The basic problem is we have gimple like this:
> _1 = x_22(D) + -1;
> _2 = _1 << 2;
> _3 = (long unsigned int) _2;
> _4 = _3 * 2;
> _5 = p_23(D) + _4;
> _6 = *_5;
> _7 = (int) _6;
> _8 = x_22(D) << 2;
> _9 = (long unsigned int) _8;
> _10 = _9 * 2;
> _11 = p_23(D) + _10;
> _12 = *_11;
> _13 = (int) _12;
> _14 = _7 + _13;
> _15 = x_22(D) + 1;
> _16 = _15 << 2;
>
> Note the repeated forms of x_22 << 2.
>
> We're essentially indexing into an array with [(index - 1) << 2], [(index) <<
> 2], and [(index + 1) << 2] and fail to realize that the shifting is a common
> subexpression.
>
> We get code like this:
>
> slli a5,a1,2 # 50 [c=4 l=4] ashldi3
> slliw a4,a1,2 # 15 [c=8 l=4] ashlsi3_extend
> addiw a5,a5,-4 # 51 [c=8 l=4] addsi3_extended/1
> slli a1,a1,2 # 52 [c=4 l=4] ashldi3
> sh1add a5,a5,a0 # 13 [c=4 l=4] *shNadd
> sh1add a4,a4,a0 # 19 [c=4 l=4] *shNadd
> addiw a1,a1,4 # 53 [c=8 l=4] addsi3_extended/1
> lhu a5,0(a5) # 14 [c=28 l=4]
> *zero_extendhisi2_bitmanip/1
> lhu a4,0(a4) # 20 [c=28 l=4]
> *zero_extendhisi2_bitmanip/1
> sh1add a1,a1,a0 # 29 [c=4 l=4] *shNadd
> lhu a0,0(a1) # 31 [c=28 l=4]
> *zero_extendhidi2_bitmanip/1
> addw a5,a5,a4 # 21 [c=8 l=4] addsi3_extended/0
> add a0,a5,a0 # 38 [c=4 l=4] *adddi3/0
> ret # 56 [c=0 l=4] simple_return
>
> Conceptually this could be solved by treating a left shift as-if it were a
> multiply in slsr and we get:
>
>
>
> _2 = _1 << 2;
> _3 = (long unsigned int) _2;
> _4 = _3 * 2;
> _5 = p_23(D) + _4;
> _6 = *_5;
> _7 = (int) _6;
> _10 = _4 + 8;
> _11 = p_23(D) + _10;
> _12 = *_11;
> _13 = (int) _12;
> _14 = _7 + _13;
> _18 = _4 + 16;
> _19 = p_23(D) + _18;
> _20 = *_19;
> _21 = (int) _20;
> _25 = _14 + _21;
>
>
>
> slli a1,a1,2 # 40 [c=4 l=4] ashldi3
> addiw t0,a1,-4 # 41 [c=8 l=4] addsi3_extended/1
> sh1add t1,t0,a0 # 13 [c=4 l=4] *shNadd
> lhu a5,8(t1) # 14 [c=28 l=4]
> *zero_extendhisi2_bitmanip/1
> lhu a4,0(t1) # 16 [c=28 l=4]
> *zero_extendhisi2_bitmanip/1
> lhu a0,16(t1) # 21 [c=28 l=4]
> *zero_extendhidi2_bitmanip/1
> addw t2,a5,a4 # 17 [c=8 l=4] addsi3_extended/0
> add a0,t2,a0 # 28 [c=4 l=4] *adddi3/0
> ret # 44 [c=0 l=4] simple_return
>
>
> I've go a patch which does this, the problem is LLM review indicated it's
> going to break because of the semantics we've defined for overflowing left
> shifts. Namely that they don't trigger undefined behavior while a multiply
> does trigger undefined behavior.
Correct.
> That matters in legal_cast_p which allows SLSR to look through some casts on
> the assumption that overflow is undefined. But that's no longer a valid
> assumption if we let SLSR treat the shift as-if it were a multiply.
You'd need to treat the shift as unsigned multiply casted back to signed.
> Given an lp64 platform, I suspect those casts are unavoidable and it looks
> like the range information in the key loop from Ranger isn't helpful --
> essentially no information that would allow us to determine the shift doesn't
> overflow.
>
> Any thoughts here?
>
> The minimized testcase looks like this:
>
> typedef unsigned short uint16_t;
>
> int
> f (uint16_t *p, int x)
> {
> return p[(x - 1) << 2] + p[x << 2] + p[(x + 1) << 2];
> }
>
>
>
> But the real context is the cost[] assignments in x264_me_search_ref,
> specifically this in the cpp output:
>
>
> { uint8_t *pix_base = p_fref_w + bmx + bmy*stride;
> h->pixf.fpelcmp_x4[i_pixel]( p_fenc, pix_base + (0) + (-1)*stride, pix_base +
> (0) + (1)*stride, pix_base + (-1) + (0)*stride, pix_base + (1) + (0)*stride,
> stride, costs ); (costs)[0] += (p_cost_mvx[(bmx+(0))<<2] +
> p_cost_mvy[(bmy+(-1))<<2]); (costs)[1] += (p_cost_mvx[(bmx+(0))<<2] +
> p_cost_mvy[(bmy+(1))<<2]); (costs)[2] += (p_cost_mvx[(bmx+(-1))<<2] +
> p_cost_mvy[(bmy+(0))<<2]); (costs)[3] += (p_cost_mvx[(bmx+(1))<<2] +
> p_cost_mvy[(bmy+(0))<<2]);};
> if(((costs[0]<<4)+1)<(bcost)) (bcost)=((costs[0]<<4)+1);;
>
>
>
> Barring any particularly helpful insight, I'm inclined to set this aside as
> unsolvable right now. The WIP patch & testcase are attached if they prove
> useful to someone.
>
> jeff
>
>
>
>