<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
    <title>Cem Akgok</title>
    <subtitle>.</subtitle>
    <link rel="self" type="application/atom+xml" href="https://cakgok.github.io/atom.xml"/>
    <link rel="alternate" type="text/html" href="https://cakgok.github.io"/>
    <generator uri="https://www.getzola.org/">Zola</generator>
    <updated>2026-11-09T00:00:00+00:00</updated>
    <id>https://cakgok.github.io/atom.xml</id>
    <entry xml:lang="en">
        <title>What if an emergency spill slot needs an emergency spill slot?</title>
        <published>2026-11-09T00:00:00+00:00</published>
        <updated>2026-11-09T00:00:00+00:00</updated>
        
        <author>
          <name>Unknown</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://cakgok.github.io/posts/spill-slot/"/>
        <id>https://cakgok.github.io/posts/spill-slot/</id>
        
        <content type="html" xml:base="https://cakgok.github.io/posts/spill-slot/">&lt;p&gt;Storing a local variable on the stack is a relatively simple operation.&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: light-dark(#24292E, #E1E4E8); background-color: light-dark(#FFFFFF, #24292E);&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;store value, [base + offset]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;A stack store ordinarily uses a base register that holds a memory address, and the offset helps us find the required address.
For a nearby local variable, this is straightforward indeed:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: light-dark(#24292E, #E1E4E8); background-color: light-dark(#FFFFFF, #24292E);&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;store value, [sp + 64]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;But what if the local variable is farther away?&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: light-dark(#24292E, #E1E4E8); background-color: light-dark(#FFFFFF, #24292E);&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;store value, [sp + 1536]&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The problem here is that, depending on the target architecture, this instruction may not be possible in a single step.&lt;/p&gt;
&lt;p&gt;Consider the Xtensa ISA, used in ESP32 microcontrollers.&lt;br /&gt;
Its 32-bit store instruction &lt;code&gt;S32I&lt;/code&gt; has an 8-bit immediate field that is also scaled by 4.&lt;/p&gt;
&lt;p&gt;What this means is that the instruction can directly address offsets of 0, 4, 8, …, 1020 bytes relative to a base register. If the offset is outside that, the compiler must construct the address separately, by using another register.&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: light-dark(#24292E, #E1E4E8); background-color: light-dark(#FFFFFF, #24292E);&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;movi  r_scratch, 1536&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;add   r_scratch, sp, r_scratch&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;s32i  r_val, r_scratch, 0&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;span class=&quot;aside&quot;&gt; &lt;code&gt;MOVI&lt;/code&gt;, on the other hand, encodes a signed 12-bit immediate field, so any value between -2048 and 2047 works fine. &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Now we need a &lt;strong&gt;scratch register&lt;/strong&gt; to calculate the address. The problem is that ordinary register allocation has already finished. Why not allocate this register earlier?&lt;/p&gt;
&lt;p&gt;&lt;span class=&quot;aside&quot;&gt; A scratch register is basically a register that you use for a temporary, throwaway calculation. &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Because by the time we know we need it, it’s already too late. The need for a scratch register comes up during &lt;strong&gt;frame-index elimination&lt;/strong&gt;, where the compiler assigns real register+offset values to abstract stack slots (frame indices). The offsets are only known once the frame layout is finalized, and the layout can’t be final before register allocation. So, the order is: register allocation, then finalize the frame layout, learn the offsets, and only then will we know which accesses need a scratch register.&lt;/p&gt;
&lt;p&gt;At that point, LLVM can still create a new virtual register to hold the temporary value. However, it cannot go through the normal register allocation. Instead, LLVM uses &lt;strong&gt;register scavenging&lt;/strong&gt; to find a physical register for it.&lt;/p&gt;
&lt;p&gt;Now, if you have a usable register at that point, business as usual and all is well. We can use that empty register as the scratch register.&lt;/p&gt;
&lt;h2 id=&quot;under-register-pressure&quot;&gt;Under Register Pressure&lt;/h2&gt;
&lt;p&gt;With high register pressure, every general-purpose register may already hold a live value. To get the scratch register, the scavenger has to temporarily borrow an occupied register. To do this, we take whatever is in the register, store it in an &lt;strong&gt;emergency spill slot&lt;/strong&gt;, and free up the register. Now, the register is usable for temporary calculation. When we are done with it, we take the saved value from the spill slot and put it back into the register.&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: light-dark(#24292E, #E1E4E8); background-color: light-dark(#FFFFFF, #24292E);&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;s32i  r_tmp, sp, emergency_offset   ; 1. Save a live register to the emergency slot&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;movi  r_tmp, 1536                   ; 2. Use that register to calculate the address&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;add   r_tmp, sp, r_tmp&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;s32i  r_val, r_tmp, 0               ; 3. Perform the original store&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;l32i  r_tmp, sp, emergency_offset   ; 4. Restore the saved register&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now, this is all good, but what if your emergency spill slot is also out of reach of your immediate?&lt;/p&gt;
&lt;h2 id=&quot;the-emergency-slot-is-too-far&quot;&gt;The Emergency Slot Is Too Far&lt;/h2&gt;
&lt;p&gt;If we apply the same reasoning, you can see how we’ll have a problem.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;We have an offset that needs to be calculated.&lt;/li&gt;
&lt;li&gt;We borrow a register and save its value in the emergency slot.&lt;/li&gt;
&lt;li&gt;If the slot is also out of our immediate’s reach, we need an emergency spill slot to calculate the offset.&lt;/li&gt;
&lt;li&gt;???&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;This circular logic leads to a crash in LLVM: &lt;strong&gt;Incomplete scavenging after 2nd pass&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;The code makes it obvious in &lt;a rel=&quot;external&quot; href=&quot;https://github.com/llvm/llvm-project/blob/4c22a9f755be90fa8ce385f589761033609fe66f/llvm/lib/CodeGen/RegisterScavenging.cpp#L473-L498&quot;&gt;&lt;code&gt;RegisterScavenging.cpp&lt;/code&gt;&lt;/a&gt; (simplified):&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: light-dark(#24292E, #E1E4E8); background-color: light-dark(#FFFFFF, #24292E);&quot; &gt;&lt;code data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;bool&lt;/span&gt;&lt;span&gt; Again&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #B392F0);&quot;&gt; scavengeFrameVirtualRegsInBlock&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;MRI&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; RS&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; MBB&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;if&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;Again&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#6A737D, #6A737D);&quot;&gt;  //&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6A737D, #6A737D);&quot;&gt; &amp;quot;Warning: Required two scavenging passes&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  Again&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #B392F0);&quot;&gt; scavengeFrameVirtualRegsInBlock&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;MRI&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; RS&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; MBB&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#6A737D, #6A737D);&quot;&gt;  //&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6A737D, #6A737D);&quot;&gt; The target required a 2nd run (because it created new vregs while&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#6A737D, #6A737D);&quot;&gt;  //&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6A737D, #6A737D);&quot;&gt; spilling). Refuse to do another pass to keep compiletime in check.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;  if&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;Again&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #B392F0);&quot;&gt;    report_fatal_error&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#032F62, #9ECBFF);&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#032F62, #9ECBFF);&quot;&gt;Incomplete scavenging after 2nd pass&lt;/span&gt;&lt;span style=&quot;color: light-dark(#032F62, #9ECBFF);&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The comment basically says that this is a stopgap to keep compile times in check. But I think a third pass wouldn’t have helped anyway, since each spill creates another out-of-range access.&lt;/p&gt;
&lt;p&gt;This problem is not specific to Xtensa. If your target has small immediate fields, the problem becomes easier to hit.&lt;/p&gt;
&lt;h2 id=&quot;why-is-the-spill-slot-too-far&quot;&gt;Why Is the Spill Slot Too Far?&lt;/h2&gt;
&lt;p&gt;Before we look at the failure modes, let’s look at how the emergency spill slot is addressed. We need a stable base register that can reach the spill slot. It can be the &lt;strong&gt;stack pointer (SP)&lt;/strong&gt;, the &lt;strong&gt;frame pointer (FP)&lt;/strong&gt; or a separate &lt;strong&gt;base pointer (BP)&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;We first have to make some decisions, and some of these decisions are made by the target ABI, while others are made by &lt;code&gt;TargetFrameLowering&lt;/code&gt; / &lt;code&gt;TargetRegisterInfo&lt;/code&gt; hooks in the compiler.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Where FP points: The ABI usually makes this decision. On x86, AArch64 and RISC-V, FP usually sits near the incoming SP, exceptions notwithstanding. On Xtensa, Mips and SystemZ, the FP is just a copy of SP right after the prologue, so it sits at the bottom. The decision LLVM makes here is whether there is an FP at all (&lt;code&gt;hasFP()&lt;/code&gt;), plus whether a separate base pointer is needed.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Where the emergency slot goes: This one really is a layout choice. PEI asks the target, through a hook called &lt;code&gt;allocateScavengingFrameIndexesNearIncomingSP()&lt;/code&gt;, whether to place scavenging slots first (near incoming SP) or last (near SP). The hook can of course be overridden.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class=&quot;svg-block svg-position-center svg-layout-beside svg-text-valign-center svg-text-align-left&quot; data-panels=&quot;FP at the top | FP == SP | With VLAs&quot;&gt;
&lt;svg id=&quot;fig-frames&quot; viewBox=&quot;0 0 1000 465&quot; role=&quot;img&quot; font-family=&quot;&#39;IBM Plex Sans&#39;, system-ui, sans-serif&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot;&gt;
&lt;defs&gt;
&lt;marker id=&quot;ff-arrow&quot; viewBox=&quot;0 0 10 10&quot; refX=&quot;9&quot; refY=&quot;5&quot; markerWidth=&quot;7&quot; markerHeight=&quot;7&quot; orient=&quot;auto-start-reverse&quot;&gt;&lt;path d=&quot;M0 0L10 5L0 10z&quot; fill=&quot;currentColor&quot;/&gt;&lt;/marker&gt;
&lt;pattern id=&quot;ff-hatch&quot; width=&quot;8&quot; height=&quot;8&quot; patternUnits=&quot;userSpaceOnUse&quot; patternTransform=&quot;rotate(45)&quot;&gt;&lt;line x1=&quot;0&quot; y1=&quot;0&quot; x2=&quot;0&quot; y2=&quot;8&quot; stroke=&quot;currentColor&quot; stroke-opacity=&quot;0.11&quot; stroke-width=&quot;3&quot;/&gt;&lt;/pattern&gt;
&lt;/defs&gt;
&lt;!-- ============ 1: FP at the top, fixed frame ============ --&gt;
&lt;g&gt;
&lt;g text-anchor=&quot;middle&quot; fill=&quot;currentColor&quot;&gt;
&lt;text x=&quot;225&quot; y=&quot;30&quot; font-size=&quot;15&quot; font-weight=&quot;600&quot;&gt;FP at the top&lt;/text&gt;
&lt;text x=&quot;225&quot; y=&quot;48&quot; font-size=&quot;12&quot; fill-opacity=&quot;0.65&quot;&gt;x86, RISC-V, AArch64&lt;/text&gt;
&lt;/g&gt;
&lt;rect x=&quot;140&quot; y=&quot;70&quot; width=&quot;170&quot; height=&quot;40&quot; fill=&quot;currentColor&quot; fill-opacity=&quot;0.03&quot; stroke=&quot;currentColor&quot; stroke-opacity=&quot;0.4&quot; stroke-dasharray=&quot;4 3&quot;/&gt;
&lt;g fill=&quot;currentColor&quot; fill-opacity=&quot;0.06&quot; stroke=&quot;currentColor&quot; stroke-opacity=&quot;0.6&quot;&gt;
&lt;rect x=&quot;140&quot; y=&quot;110&quot; width=&quot;170&quot; height=&quot;60&quot;/&gt;
&lt;rect x=&quot;140&quot; y=&quot;194&quot; width=&quot;170&quot; height=&quot;166&quot;/&gt;
&lt;rect x=&quot;140&quot; y=&quot;360&quot; width=&quot;170&quot; height=&quot;50&quot;/&gt;
&lt;/g&gt;
&lt;rect x=&quot;140&quot; y=&quot;170&quot; width=&quot;170&quot; height=&quot;24&quot; fill=&quot;#2a9d7f&quot; fill-opacity=&quot;0.25&quot; stroke=&quot;#2a9d7f&quot; stroke-width=&quot;1.5&quot;/&gt;
&lt;rect x=&quot;144&quot; y=&quot;335&quot; width=&quot;162&quot; height=&quot;21&quot; fill=&quot;none&quot; stroke=&quot;#2a9d7f&quot; stroke-width=&quot;1.2&quot; stroke-dasharray=&quot;4 3&quot;/&gt;
&lt;g text-anchor=&quot;middle&quot; fill=&quot;currentColor&quot; font-size=&quot;12.5&quot;&gt;
&lt;text x=&quot;225&quot; y=&quot;94&quot; fill-opacity=&quot;0.65&quot;&gt;incoming stack arguments&lt;/text&gt;
&lt;text x=&quot;225&quot; y=&quot;136&quot;&gt;return address,&lt;/text&gt;
&lt;text x=&quot;225&quot; y=&quot;152&quot;&gt;callee-saved registers&lt;/text&gt;
&lt;text x=&quot;225&quot; y=&quot;186&quot; font-weight=&quot;600&quot;&gt;emergency slot&lt;/text&gt;
&lt;text x=&quot;225&quot; y=&quot;262&quot;&gt;local variables,&lt;/text&gt;
&lt;text x=&quot;225&quot; y=&quot;278&quot;&gt;spill slots&lt;/text&gt;
&lt;text x=&quot;225&quot; y=&quot;350&quot; font-size=&quot;11.5&quot; fill-opacity=&quot;0.75&quot;&gt;slot here if there is no FP&lt;/text&gt;
&lt;text x=&quot;225&quot; y=&quot;382&quot;&gt;outgoing arguments&lt;/text&gt;
&lt;text x=&quot;225&quot; y=&quot;399&quot; font-size=&quot;11.5&quot; fill-opacity=&quot;0.65&quot;&gt;reserved call frame&lt;/text&gt;
&lt;/g&gt;
&lt;g stroke=&quot;currentColor&quot; stroke-width=&quot;1.3&quot; marker-end=&quot;url(#ff-arrow)&quot;&gt;
&lt;line x1=&quot;86&quot; y1=&quot;110&quot; x2=&quot;136&quot; y2=&quot;110&quot;/&gt;
&lt;line x1=&quot;86&quot; y1=&quot;170&quot; x2=&quot;136&quot; y2=&quot;170&quot;/&gt;
&lt;line x1=&quot;86&quot; y1=&quot;410&quot; x2=&quot;136&quot; y2=&quot;410&quot;/&gt;
&lt;/g&gt;
&lt;g text-anchor=&quot;end&quot; fill=&quot;currentColor&quot; font-size=&quot;12.5&quot;&gt;
&lt;text x=&quot;80&quot; y=&quot;114&quot; fill-opacity=&quot;0.7&quot;&gt;incoming SP&lt;/text&gt;
&lt;text x=&quot;80&quot; y=&quot;174&quot; font-weight=&quot;600&quot;&gt;FP&lt;/text&gt;
&lt;text x=&quot;80&quot; y=&quot;414&quot; font-weight=&quot;600&quot;&gt;SP&lt;/text&gt;
&lt;/g&gt;
&lt;g text-anchor=&quot;middle&quot; fill=&quot;currentColor&quot; fill-opacity=&quot;0.6&quot; font-size=&quot;12&quot;&gt;
&lt;text x=&quot;225&quot; y=&quot;436&quot;&gt;with VLAs: the same picture,&lt;/text&gt;
&lt;text x=&quot;225&quot; y=&quot;452&quot;&gt;they are allocated below the slot&lt;/text&gt;
&lt;/g&gt;
&lt;/g&gt;
&lt;!-- ============ 2: FP == SP, fixed frame ============ --&gt;
&lt;g transform=&quot;translate(335 0)&quot;&gt;
&lt;g text-anchor=&quot;middle&quot; fill=&quot;currentColor&quot;&gt;
&lt;text x=&quot;225&quot; y=&quot;30&quot; font-size=&quot;15&quot; font-weight=&quot;600&quot;&gt;FP == SP after the prologue&lt;/text&gt;
&lt;text x=&quot;225&quot; y=&quot;48&quot; font-size=&quot;12&quot; fill-opacity=&quot;0.65&quot;&gt;Mips, SystemZ&lt;/text&gt;
&lt;/g&gt;
&lt;rect x=&quot;140&quot; y=&quot;70&quot; width=&quot;170&quot; height=&quot;40&quot; fill=&quot;currentColor&quot; fill-opacity=&quot;0.03&quot; stroke=&quot;currentColor&quot; stroke-opacity=&quot;0.4&quot; stroke-dasharray=&quot;4 3&quot;/&gt;
&lt;g fill=&quot;currentColor&quot; fill-opacity=&quot;0.06&quot; stroke=&quot;currentColor&quot; stroke-opacity=&quot;0.6&quot;&gt;
&lt;rect x=&quot;140&quot; y=&quot;110&quot; width=&quot;170&quot; height=&quot;60&quot;/&gt;
&lt;rect x=&quot;140&quot; y=&quot;170&quot; width=&quot;170&quot; height=&quot;166&quot;/&gt;
&lt;rect x=&quot;140&quot; y=&quot;360&quot; width=&quot;170&quot; height=&quot;50&quot;/&gt;
&lt;/g&gt;
&lt;rect x=&quot;140&quot; y=&quot;336&quot; width=&quot;170&quot; height=&quot;24&quot; fill=&quot;#2a9d7f&quot; fill-opacity=&quot;0.25&quot; stroke=&quot;#2a9d7f&quot; stroke-width=&quot;1.5&quot;/&gt;
&lt;g text-anchor=&quot;middle&quot; fill=&quot;currentColor&quot; font-size=&quot;12.5&quot;&gt;
&lt;text x=&quot;225&quot; y=&quot;94&quot; fill-opacity=&quot;0.65&quot;&gt;incoming stack arguments&lt;/text&gt;
&lt;text x=&quot;225&quot; y=&quot;136&quot;&gt;return address,&lt;/text&gt;
&lt;text x=&quot;225&quot; y=&quot;152&quot;&gt;callee-saved registers&lt;/text&gt;
&lt;text x=&quot;225&quot; y=&quot;249&quot;&gt;local variables,&lt;/text&gt;
&lt;text x=&quot;225&quot; y=&quot;265&quot;&gt;spill slots&lt;/text&gt;
&lt;text x=&quot;225&quot; y=&quot;352&quot; font-weight=&quot;600&quot;&gt;emergency slot&lt;/text&gt;
&lt;text x=&quot;225&quot; y=&quot;382&quot;&gt;outgoing arguments&lt;/text&gt;
&lt;text x=&quot;225&quot; y=&quot;399&quot; font-size=&quot;11.5&quot; fill-opacity=&quot;0.65&quot;&gt;call frame, if reserved&lt;/text&gt;
&lt;/g&gt;
&lt;g stroke=&quot;currentColor&quot; stroke-width=&quot;1.3&quot; marker-end=&quot;url(#ff-arrow)&quot;&gt;
&lt;line x1=&quot;86&quot; y1=&quot;110&quot; x2=&quot;136&quot; y2=&quot;110&quot;/&gt;
&lt;line x1=&quot;86&quot; y1=&quot;410&quot; x2=&quot;136&quot; y2=&quot;410&quot;/&gt;
&lt;/g&gt;
&lt;g text-anchor=&quot;end&quot; fill=&quot;currentColor&quot; font-size=&quot;12.5&quot;&gt;
&lt;text x=&quot;80&quot; y=&quot;114&quot; fill-opacity=&quot;0.7&quot;&gt;incoming SP&lt;/text&gt;
&lt;text x=&quot;80&quot; y=&quot;414&quot; font-weight=&quot;600&quot;&gt;SP == FP&lt;/text&gt;
&lt;/g&gt;
&lt;/g&gt;
&lt;!-- ============ 3: FP == SP, with VLAs ============ --&gt;
&lt;g transform=&quot;translate(670 0)&quot;&gt;
&lt;g text-anchor=&quot;middle&quot; fill=&quot;currentColor&quot;&gt;
&lt;text x=&quot;225&quot; y=&quot;30&quot; font-size=&quot;15&quot; font-weight=&quot;600&quot;&gt;If there are VLAs&lt;/text&gt;
&lt;text x=&quot;225&quot; y=&quot;48&quot; font-size=&quot;12&quot; fill-opacity=&quot;0.65&quot;&gt;SP moves, FP is stable&lt;/text&gt;
&lt;/g&gt;
&lt;rect x=&quot;140&quot; y=&quot;70&quot; width=&quot;170&quot; height=&quot;40&quot; fill=&quot;currentColor&quot; fill-opacity=&quot;0.03&quot; stroke=&quot;currentColor&quot; stroke-opacity=&quot;0.4&quot; stroke-dasharray=&quot;4 3&quot;/&gt;
&lt;g fill=&quot;currentColor&quot; fill-opacity=&quot;0.06&quot; stroke=&quot;currentColor&quot; stroke-opacity=&quot;0.6&quot;&gt;
&lt;rect x=&quot;140&quot; y=&quot;110&quot; width=&quot;170&quot; height=&quot;60&quot;/&gt;
&lt;rect x=&quot;140&quot; y=&quot;170&quot; width=&quot;170&quot; height=&quot;106&quot;/&gt;
&lt;rect x=&quot;140&quot; y=&quot;370&quot; width=&quot;170&quot; height=&quot;40&quot;/&gt;
&lt;/g&gt;
&lt;rect x=&quot;140&quot; y=&quot;300&quot; width=&quot;170&quot; height=&quot;70&quot; fill=&quot;url(#ff-hatch)&quot; stroke=&quot;currentColor&quot; stroke-opacity=&quot;0.6&quot;/&gt;
&lt;rect x=&quot;140&quot; y=&quot;276&quot; width=&quot;170&quot; height=&quot;24&quot; fill=&quot;#2a9d7f&quot; fill-opacity=&quot;0.25&quot; stroke=&quot;#2a9d7f&quot; stroke-width=&quot;1.5&quot;/&gt;
&lt;g text-anchor=&quot;middle&quot; fill=&quot;currentColor&quot; font-size=&quot;12.5&quot;&gt;
&lt;text x=&quot;225&quot; y=&quot;94&quot; fill-opacity=&quot;0.65&quot;&gt;incoming stack arguments&lt;/text&gt;
&lt;text x=&quot;225&quot; y=&quot;136&quot;&gt;return address,&lt;/text&gt;
&lt;text x=&quot;225&quot; y=&quot;152&quot;&gt;callee-saved registers&lt;/text&gt;
&lt;text x=&quot;225&quot; y=&quot;219&quot;&gt;local variables,&lt;/text&gt;
&lt;text x=&quot;225&quot; y=&quot;235&quot;&gt;spill slots&lt;/text&gt;
&lt;text x=&quot;225&quot; y=&quot;292&quot; font-weight=&quot;600&quot;&gt;emergency slot&lt;/text&gt;
&lt;text x=&quot;225&quot; y=&quot;331&quot; font-weight=&quot;600&quot;&gt;VLAs, alloca&lt;/text&gt;
&lt;text x=&quot;225&quot; y=&quot;348&quot; font-size=&quot;11.5&quot; fill-opacity=&quot;0.7&quot;&gt;size known only at run time&lt;/text&gt;
&lt;text x=&quot;225&quot; y=&quot;394&quot;&gt;outgoing arguments&lt;/text&gt;
&lt;/g&gt;
&lt;g stroke=&quot;currentColor&quot; stroke-width=&quot;1.3&quot; marker-end=&quot;url(#ff-arrow)&quot;&gt;
&lt;line x1=&quot;86&quot; y1=&quot;110&quot; x2=&quot;136&quot; y2=&quot;110&quot;/&gt;
&lt;line x1=&quot;86&quot; y1=&quot;300&quot; x2=&quot;136&quot; y2=&quot;300&quot;/&gt;
&lt;line x1=&quot;86&quot; y1=&quot;410&quot; x2=&quot;136&quot; y2=&quot;410&quot;/&gt;
&lt;/g&gt;
&lt;g text-anchor=&quot;end&quot; fill=&quot;currentColor&quot; font-size=&quot;12.5&quot;&gt;
&lt;text x=&quot;80&quot; y=&quot;114&quot; fill-opacity=&quot;0.7&quot;&gt;incoming SP&lt;/text&gt;
&lt;text x=&quot;80&quot; y=&quot;304&quot; font-weight=&quot;600&quot;&gt;FP&lt;/text&gt;
&lt;text x=&quot;80&quot; y=&quot;414&quot; font-weight=&quot;600&quot;&gt;SP&lt;/text&gt;
&lt;/g&gt;
&lt;/g&gt;
&lt;/svg&gt;
&lt;/div&gt;
&lt;ol start=&quot;3&quot;&gt;
&lt;li&gt;
&lt;p&gt;How a slot is addressed: This is decided per frame index, in &lt;code&gt;getFrameIndexReference()&lt;/code&gt; / &lt;code&gt;eliminateFrameIndex()&lt;/code&gt;, and it’s likely that this decision is often forced by the first two. When there are variable-length arrays (VLAs) in the stack frame, you can’t use SP-relative access.&lt;br /&gt;
&lt;br /&gt;
On the other hand, if the stack frame needs realignment, FP can no longer be used to address the emergency slot. The reason is that the FP is copied from the SP &lt;em&gt;before&lt;/em&gt; the realignment. Realignment moves SP down by an amount known only at run time, so the offset is not a compile-time constant.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Whether to reserve the call frame: Reserving it means the outgoing-argument area is allocated once in the prologue, sized for the largest call, instead of moving SP around each call. Not reserving means there’s no gap, but the SP moves, and that movement needs to be accounted for.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;All these decisions have to be considered in tandem such that they align with each other. Let’s look at what happens if they don’t.&lt;/p&gt;
&lt;h3 id=&quot;a-it-s-on-the-wrong-end-of-the-stack-frame&quot;&gt;A. It’s on the Wrong End of the Stack Frame&lt;/h3&gt;
&lt;div class=&quot;svg-block svg-position-left svg-layout-beside svg-text-valign-center svg-text-align-left svg-has-text&quot;&gt;
&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; id=&quot;fig-modes-wrong-end&quot; viewBox=&quot;0 38 280 316&quot; width=&quot;280&quot; height=&quot;316&quot; style=&quot;width: 280px; max-width: 100%; height: auto;&quot; role=&quot;img&quot; font-family=&quot;&#39;IBM Plex Sans&#39;, system-ui, sans-serif&quot;&gt;
  &lt;defs&gt;
    &lt;marker id=&quot;fm-wrong-end-arrow&quot; viewBox=&quot;0 0 10 10&quot; refX=&quot;9&quot; refY=&quot;5&quot; markerWidth=&quot;7&quot; markerHeight=&quot;7&quot; orient=&quot;auto-start-reverse&quot;&gt;
      &lt;path d=&quot;M0 0L10 5L0 10z&quot; fill=&quot;currentColor&quot; /&gt;
    &lt;/marker&gt;
  &lt;/defs&gt;
  &lt;rect x=&quot;65&quot; y=&quot;96&quot; width=&quot;150&quot; height=&quot;194&quot; fill=&quot;currentColor&quot; fill-opacity=&quot;0.06&quot; stroke=&quot;currentColor&quot; stroke-opacity=&quot;0.6&quot; /&gt;
  &lt;rect x=&quot;65&quot; y=&quot;70&quot; width=&quot;150&quot; height=&quot;26&quot; fill=&quot;#d1495b&quot; fill-opacity=&quot;0.14&quot; stroke=&quot;#d1495b&quot; stroke-width=&quot;1.5&quot; /&gt;
  &lt;g text-anchor=&quot;middle&quot; fill=&quot;currentColor&quot; font-size=&quot;12.5&quot;&gt;
    &lt;text x=&quot;140&quot; y=&quot;58&quot; font-size=&quot;11.5&quot; fill-opacity=&quot;0.65&quot;&gt;incoming SP&lt;/text&gt;
    &lt;text x=&quot;140&quot; y=&quot;87&quot; font-weight=&quot;600&quot;&gt;slot&lt;/text&gt;
    &lt;text x=&quot;140&quot; y=&quot;198&quot;&gt;locals&lt;/text&gt;
  &lt;/g&gt;
  &lt;path d=&quot;M219 290h8V83h-8&quot; fill=&quot;none&quot; stroke=&quot;#d1495b&quot; stroke-width=&quot;1.5&quot; /&gt;
  &lt;text transform=&quot;rotate(-90 242 186)&quot; x=&quot;242&quot; y=&quot;186&quot; text-anchor=&quot;middle&quot; font-size=&quot;12&quot; fill=&quot;#d1495b&quot; font-weight=&quot;600&quot;&gt;&amp;gt; 1020 B&lt;/text&gt;
  &lt;line x1=&quot;10&quot; y1=&quot;290&quot; x2=&quot;61&quot; y2=&quot;290&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.3&quot; marker-end=&quot;url(#fm-wrong-end-arrow)&quot; /&gt;
  &lt;text x=&quot;12&quot; y=&quot;282&quot; font-size=&quot;12.5&quot; font-weight=&quot;600&quot; fill=&quot;currentColor&quot;&gt;SP == FP&lt;/text&gt;
  &lt;g fill=&quot;currentColor&quot; font-size=&quot;12&quot; text-anchor=&quot;middle&quot;&gt;
    &lt;text x=&quot;140&quot; y=&quot;324&quot; fill=&quot;#2a9d7f&quot; font-weight=&quot;600&quot;&gt;Fix: put the slot next to SP&lt;/text&gt;
    &lt;text x=&quot;140&quot; y=&quot;342&quot; fill-opacity=&quot;0.7&quot;&gt;(Mips and SystemZ do this)&lt;/text&gt;
  &lt;/g&gt;
&lt;/svg&gt;
&lt;div class=&quot;svg-text&quot;&gt;&lt;p&gt;LLVM’s default puts the slot near the &lt;em&gt;incoming SP&lt;/em&gt; whenever there’s an FP. This assumes the FP is up there too. If your FP is near the SP instead, you can see how this can lead to some issues.&lt;/p&gt;
&lt;p&gt;If your frame is big enough, you just crashed the compiler.&lt;/p&gt;
&lt;p&gt;With Xtensa, this is quite easy due to the limited range of the immediate, as we talked about.&lt;/p&gt;
&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;Here’s a simple reproducer to crash the compiler:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: light-dark(#24292E, #E1E4E8); background-color: light-dark(#FFFFFF, #24292E);&quot; &gt;&lt;code data-lang=&quot;c&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;static&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; int&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #B392F0);&quot;&gt; min&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #FFAB70);&quot;&gt; a&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; int&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #FFAB70);&quot;&gt; b&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; return&lt;/span&gt;&lt;span&gt; a &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span&gt; b &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;?&lt;/span&gt;&lt;span&gt; a &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;:&lt;/span&gt;&lt;span&gt; b&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;void&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #B392F0);&quot;&gt; callee&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;int&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; int&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; int&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; int&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; int&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; int&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; int&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;void&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #B392F0);&quot;&gt; use&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;char&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; *&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;void&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #B392F0);&quot;&gt; f&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; int&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; *&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #FFAB70);&quot;&gt;p&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;  char&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #FFAB70);&quot;&gt; big&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;2100&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #B392F0);&quot;&gt;  use&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;big&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #B392F0);&quot;&gt;  callee&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #B392F0);&quot;&gt;min&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #FFAB70);&quot;&gt;p&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;0&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #FFAB70);&quot;&gt; p&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;1&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #B392F0);&quot;&gt; min&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #FFAB70);&quot;&gt;p&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;2&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #FFAB70);&quot;&gt; p&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;3&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #B392F0);&quot;&gt; min&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #FFAB70);&quot;&gt;p&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;4&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #FFAB70);&quot;&gt; p&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;5&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #B392F0);&quot;&gt; min&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #FFAB70);&quot;&gt;p&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;6&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #FFAB70);&quot;&gt; p&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;7&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #B392F0);&quot;&gt;         min&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #FFAB70);&quot;&gt;p&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;8&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #FFAB70);&quot;&gt; p&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;9&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #B392F0);&quot;&gt; min&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #FFAB70);&quot;&gt;p&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;10&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #FFAB70);&quot;&gt; p&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;11&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt; 7&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;giallo&quot; style=&quot;color: light-dark(#24292E, #E1E4E8); background-color: light-dark(#FFFFFF, #24292E);&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;$ clang --target=xtensa -O2 -S -emit-llvm repro.c -o - | llc -mtriple=xtensa -mcpu=esp32s3 -o -&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;LLVM ERROR: Incomplete scavenging after 2nd pass&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;span class=&quot;aside&quot;&gt; Upstream only reserves the slot when the estimated frame size is at least 2048 bytes. Smaller frames get a different error, due to another bug: “Cannot scavenge register without an emergency spill slot”. &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;But what exactly makes this code need a scratch register?&lt;br /&gt;
First, this shows up with an FP, and clang happens to add one by default at this version.&lt;/p&gt;
&lt;p&gt;Second, it’s the call.&lt;br /&gt;
The 7th argument doesn’t fit in registers, so it goes on the stack, and Xtensa makes room for it by moving SP down right before the call.
Moving SP takes a temporary register:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: light-dark(#24292E, #E1E4E8); background-color: light-dark(#FFFFFF, #24292E);&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;addi  a8, a1, -4    ; a8 = SP - 4&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;movsp a1, a8        ; SP = a8&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Xtensa has 16 general-purpose registers. The return address, SP and FP occupy 3 of them. Twelve values loaded from &lt;code&gt;p&lt;/code&gt; and the constant 7 occupy the remaining 13. So the scavenger has to borrow one, and the emergency slot it saves it to sits more than 2 KiB above FP due to &lt;code&gt;big[2100]&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;&lt;span class=&quot;aside&quot;&gt; Xtensa only reserves space for stack arguments in advance when there is no FP. We’ll come back to this in section C. &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The fix is simple. We just override the placement of the slot (&lt;a rel=&quot;external&quot; href=&quot;https://github.com/llvm/llvm-project/pull/237791&quot;&gt;#237791&lt;/a&gt;). This is what SystemZ and Mips do as well. &lt;a rel=&quot;external&quot; href=&quot;https://github.com/llvm/llvm-project/blob/4c22a9f755be90fa8ce385f589761033609fe66f/llvm/lib/Target/SystemZ/SystemZFrameLowering.h#L35-L39&quot;&gt;SystemZ’s override&lt;/a&gt; comes with a comment that describes this exact case:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;The default implementation assumes an x86-like layout, where the frame pointer is at the opposite end of the frame from the stack pointer. This meant that when frame pointer elimination was disabled, the slots ended up being as close as possible to the incoming stack pointer, which is the opposite of what we want on SystemZ.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Here is the exact hook that does it:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: light-dark(#24292E, #E1E4E8); background-color: light-dark(#FFFFFF, #24292E);&quot; &gt;&lt;code data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;bool&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #B392F0);&quot;&gt; allocateScavengingFrameIndexesNearIncomingSP&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;    const&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #B392F0);&quot;&gt; MachineFunction&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #FFAB70);&quot;&gt;MF&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; const&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; override&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;  return&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt; false&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;hr /&gt;
&lt;p&gt;There is another way the slot ends up on the wrong side, and that’s with an over-aligned local. The gap between FP and the locals is unknown at compile time, so locals have to be addressed from SP (or another base pointer). A slot placed near FP would be addressed from SP, across the whole frame.&lt;/p&gt;
&lt;p&gt;This case has come up before: when the stack is realigned, PEI places the slot near SP instead (&lt;a rel=&quot;external&quot; href=&quot;https://github.com/llvm/llvm-project/commit/cad78a763455&quot;&gt;&lt;code&gt;cad78a763455&lt;/code&gt;&lt;/a&gt;). A base pointer can cause the same mismatch. In AArch64, functions with VLAs kept the slot at the top of the frame but addressed it from the base pointer at the other end, and the fix was to reach it from the FP (&lt;a rel=&quot;external&quot; href=&quot;https://github.com/llvm/llvm-project/commit/f2c22050e821&quot;&gt;&lt;code&gt;f2c22050e821&lt;/code&gt;&lt;/a&gt;).&lt;/p&gt;
&lt;p&gt;The default implementation of &lt;a rel=&quot;external&quot; href=&quot;https://github.com/llvm/llvm-project/blob/4c22a9f755be90fa8ce385f589761033609fe66f/llvm/lib/CodeGen/TargetFrameLoweringImpl.cpp#L172-L180&quot;&gt;&lt;code&gt;allocateScavengingFrameIndexesNearIncomingSP()&lt;/code&gt;&lt;/a&gt; accounts for this. It only places the slot near the incoming SP when there is an FP and the stack is not realigned:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: light-dark(#24292E, #E1E4E8); background-color: light-dark(#FFFFFF, #24292E);&quot; &gt;&lt;code data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;bool&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #B392F0);&quot;&gt; TargetFrameLowering&lt;/span&gt;&lt;span&gt;::&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #B392F0);&quot;&gt;allocateScavengingFrameIndexesNearIncomingSP&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;  const&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #B392F0);&quot;&gt; MachineFunction&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #FFAB70);&quot;&gt;MF&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; const&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;  if&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;!&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #B392F0);&quot;&gt;hasFP&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;MF&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt; false&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;  const&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #B392F0);&quot;&gt; TargetRegisterInfo&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; *&lt;/span&gt;&lt;span&gt;RegInfo&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; =&lt;/span&gt;&lt;span&gt; MF&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #B392F0);&quot;&gt;getSubtarget&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #B392F0);&quot;&gt;getRegisterInfo&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;  return&lt;/span&gt;&lt;span&gt; RegInfo&lt;/span&gt;&lt;span&gt;-&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #B392F0);&quot;&gt;useFPForScavengingIndex&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;MF&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; &amp;amp;&amp;amp;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;         !&lt;/span&gt;&lt;span&gt;RegInfo&lt;/span&gt;&lt;span&gt;-&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #B392F0);&quot;&gt;hasStackRealignment&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;MF&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;span class=&quot;aside&quot;&gt; The hook is fairly new: it dates from 2021 and replaced an older knob. Funnily enough, the &lt;a rel=&quot;external&quot; href=&quot;https://github.com/llvm/llvm-project/commit/273a0c8bc9c7&quot;&gt;commit&lt;/a&gt; even says: “I don’t understand what the existing control &lt;code&gt;isFPCloseToIncomingSP&lt;/code&gt; is supposed to mean”. &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;In the end, &lt;code&gt;useFPForScavengingIndex()&lt;/code&gt; only declares a preference on whether to use FP or not for the access. The hook uses this preference to decide where to place the slot. Frame-index elimination still has to pick the correct register separately.&lt;/p&gt;
&lt;hr /&gt;
&lt;h3 id=&quot;b-offset-between-the-base-and-the-slot&quot;&gt;B. Offset Between the Base and the Slot&lt;/h3&gt;
&lt;p&gt;The slot is at the right end, but something is laid out between it and the base register. This usually happens when there is a large reserved outgoing call frame, because the slot is normally allocated right above the outgoing call area.&lt;/p&gt;
&lt;div class=&quot;svg-block svg-position-center svg-layout-beside svg-text-valign-center svg-text-align-left&quot; data-panels=&quot;Before @ 0-318 | After @ 318-720&quot;&gt;
&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot;
     id=&quot;fig-call-frame-before-after&quot;
     viewBox=&quot;34 42 663 395&quot; width=&quot;663&quot; height=&quot;395&quot;
     style=&quot;width: 663px; max-width: 100%; height: auto;&quot;
     role=&quot;img&quot;
     font-family=&quot;&#39;IBM Plex Sans&#39;, system-ui, sans-serif&quot;&gt;
  &lt;title id=&quot;cfba-title&quot;&gt;Emergency spill slot before and after enabling a frame pointer&lt;/title&gt;
  &lt;defs&gt;
    &lt;marker id=&quot;cfba-arrow&quot; viewBox=&quot;0 0 10 10&quot; refX=&quot;9&quot; refY=&quot;5&quot;
            markerWidth=&quot;7&quot; markerHeight=&quot;7&quot; orient=&quot;auto-start-reverse&quot;&gt;
      &lt;path d=&quot;M0 0L10 5L0 10z&quot; fill=&quot;currentColor&quot; /&gt;
    &lt;/marker&gt;
  &lt;/defs&gt;
  &lt;g fill=&quot;currentColor&quot; text-anchor=&quot;middle&quot;&gt;
    &lt;text x=&quot;177&quot; y=&quot;64&quot; font-size=&quot;13&quot; font-weight=&quot;600&quot;&gt;Before: no frame pointer&lt;/text&gt;
    &lt;text x=&quot;537&quot; y=&quot;64&quot; font-size=&quot;13&quot; font-weight=&quot;600&quot;&gt;After: frame pointer enabled&lt;/text&gt;
    &lt;text x=&quot;177&quot; y=&quot;88&quot; font-size=&quot;11.5&quot; fill-opacity=&quot;0.65&quot;&gt;incoming SP&lt;/text&gt;
    &lt;text x=&quot;537&quot; y=&quot;88&quot; font-size=&quot;11.5&quot; fill-opacity=&quot;0.65&quot;&gt;incoming SP&lt;/text&gt;
  &lt;/g&gt;
  &lt;!-- Before: locals, emergency slot, reserved outgoing call area. --&gt;
  &lt;g fill=&quot;currentColor&quot; fill-opacity=&quot;0.06&quot; stroke=&quot;currentColor&quot; stroke-opacity=&quot;0.6&quot;&gt;
    &lt;rect x=&quot;92&quot; y=&quot;104&quot; width=&quot;170&quot; height=&quot;112&quot; /&gt;
    &lt;rect x=&quot;92&quot; y=&quot;244&quot; width=&quot;170&quot; height=&quot;96&quot; /&gt;
  &lt;/g&gt;
  &lt;rect x=&quot;92&quot; y=&quot;216&quot; width=&quot;170&quot; height=&quot;28&quot;
        fill=&quot;#d1495b&quot; fill-opacity=&quot;0.14&quot; stroke=&quot;#d1495b&quot; stroke-width=&quot;1.5&quot; /&gt;
  &lt;g fill=&quot;currentColor&quot; text-anchor=&quot;middle&quot; font-size=&quot;12.5&quot;&gt;
    &lt;text x=&quot;177&quot; y=&quot;164&quot;&gt;locals&lt;/text&gt;
    &lt;text x=&quot;177&quot; y=&quot;234&quot; font-weight=&quot;600&quot;&gt;emergency slot&lt;/text&gt;
    &lt;text x=&quot;177&quot; y=&quot;285&quot;&gt;reserved outgoing&lt;/text&gt;
    &lt;text x=&quot;177&quot; y=&quot;303&quot;&gt;call area&lt;/text&gt;
  &lt;/g&gt;
  &lt;line x1=&quot;42&quot; y1=&quot;340&quot; x2=&quot;88&quot; y2=&quot;340&quot;
        stroke=&quot;currentColor&quot; stroke-width=&quot;1.3&quot; marker-end=&quot;url(#cfba-arrow)&quot; /&gt;
  &lt;text x=&quot;44&quot; y=&quot;332&quot; fill=&quot;currentColor&quot; font-size=&quot;12.5&quot; font-weight=&quot;600&quot;&gt;SP&lt;/text&gt;
  &lt;path d=&quot;M266 340h8V244h-8&quot; fill=&quot;none&quot; stroke=&quot;#d1495b&quot; stroke-width=&quot;1.5&quot; /&gt;
  &lt;text x=&quot;293&quot; y=&quot;292&quot; transform=&quot;rotate(-90 293 292)&quot;
        text-anchor=&quot;middle&quot; fill=&quot;#d1495b&quot; font-size=&quot;12&quot; font-weight=&quot;600&quot;&gt;&amp;gt; 2047 B&lt;/text&gt;
  &lt;!-- Enabling FP also selects the existing upper-slot placement policy. --&gt;
  &lt;g fill=&quot;currentColor&quot; text-anchor=&quot;middle&quot; font-size=&quot;11.5&quot;&gt;
    &lt;text x=&quot;357&quot; y=&quot;185&quot;&gt;enable FP&lt;/text&gt;
    &lt;text x=&quot;357&quot; y=&quot;203&quot;&gt;AND&lt;/text&gt;
    &lt;text x=&quot;357&quot; y=&quot;217&quot;&gt;move slot&lt;/text&gt;
  &lt;/g&gt;
  &lt;line x1=&quot;321&quot; y1=&quot;226&quot; x2=&quot;393&quot; y2=&quot;226&quot;
        stroke=&quot;currentColor&quot; stroke-opacity=&quot;0.7&quot; stroke-width=&quot;1.3&quot;
        marker-end=&quot;url(#cfba-arrow)&quot; /&gt;
  &lt;!-- After: emergency slot, locals, unchanged reserved outgoing call area. --&gt;
  &lt;rect x=&quot;452&quot; y=&quot;104&quot; width=&quot;170&quot; height=&quot;28&quot;
        fill=&quot;#2a9d7f&quot; fill-opacity=&quot;0.2&quot; stroke=&quot;#2a9d7f&quot; stroke-width=&quot;1.5&quot; /&gt;
  &lt;g fill=&quot;currentColor&quot; fill-opacity=&quot;0.06&quot; stroke=&quot;currentColor&quot; stroke-opacity=&quot;0.6&quot;&gt;
    &lt;rect x=&quot;452&quot; y=&quot;132&quot; width=&quot;170&quot; height=&quot;112&quot; /&gt;
    &lt;rect x=&quot;452&quot; y=&quot;244&quot; width=&quot;170&quot; height=&quot;96&quot; /&gt;
  &lt;/g&gt;
  &lt;g fill=&quot;currentColor&quot; text-anchor=&quot;middle&quot; font-size=&quot;12.5&quot;&gt;
    &lt;text x=&quot;537&quot; y=&quot;122&quot; font-weight=&quot;600&quot;&gt;emergency slot&lt;/text&gt;
    &lt;text x=&quot;537&quot; y=&quot;192&quot;&gt;locals&lt;/text&gt;
    &lt;text x=&quot;537&quot; y=&quot;285&quot;&gt;reserved outgoing&lt;/text&gt;
    &lt;text x=&quot;537&quot; y=&quot;303&quot;&gt;call area&lt;/text&gt;
  &lt;/g&gt;
  &lt;g stroke=&quot;currentColor&quot; stroke-width=&quot;1.3&quot; marker-end=&quot;url(#cfba-arrow)&quot;&gt;
    &lt;line x1=&quot;402&quot; y1=&quot;104&quot; x2=&quot;448&quot; y2=&quot;104&quot; /&gt;
    &lt;line x1=&quot;402&quot; y1=&quot;340&quot; x2=&quot;448&quot; y2=&quot;340&quot; /&gt;
  &lt;/g&gt;
  &lt;g fill=&quot;currentColor&quot; font-size=&quot;12.5&quot; font-weight=&quot;600&quot;&gt;
    &lt;text x=&quot;404&quot; y=&quot;96&quot;&gt;FP&lt;/text&gt;
    &lt;text x=&quot;404&quot; y=&quot;332&quot;&gt;SP&lt;/text&gt;
  &lt;/g&gt;
  &lt;path d=&quot;M626 104h8V132h-8&quot; fill=&quot;none&quot; stroke=&quot;#2a9d7f&quot; stroke-width=&quot;1.5&quot; /&gt;
  &lt;text x=&quot;644&quot; y=&quot;122&quot; fill=&quot;#2a9d7f&quot; font-size=&quot;12&quot; font-weight=&quot;600&quot;&gt;near FP&lt;/text&gt;
  &lt;g text-anchor=&quot;middle&quot; font-size=&quot;12&quot;&gt;
    &lt;text x=&quot;177&quot; y=&quot;374&quot; fill=&quot;#d1495b&quot; font-weight=&quot;600&quot;&gt;Slot is beyond SP&#39;s reach&lt;/text&gt;
    &lt;text x=&quot;537&quot; y=&quot;374&quot; fill=&quot;#2a9d7f&quot; font-weight=&quot;600&quot;&gt;Slot is within FP&#39;s reach&lt;/text&gt;
    &lt;g fill=&quot;currentColor&quot; fill-opacity=&quot;0.7&quot;&gt;
      &lt;text x=&quot;177&quot; y=&quot;394&quot;&gt;Outgoing area separates SP and slot.&lt;/text&gt;
      &lt;text x=&quot;537&quot; y=&quot;394&quot;&gt;Locals grow below the slot.&lt;/text&gt;
      &lt;text x=&quot;360&quot; y=&quot;426&quot; font-size=&quot;11.5&quot;&gt;Call area stays reserved · no realignment&lt;/text&gt;
    &lt;/g&gt;
  &lt;/g&gt;
&lt;/svg&gt;
&lt;/div&gt;
&lt;p&gt;With bottom placement, the slot comes after all locals, with the max call frame size below it.&lt;br /&gt;
So, the slot sits exactly &lt;code&gt;MaxCallFrameSize&lt;/code&gt; above SP.&lt;br /&gt;
Above 2047 bytes on RISC-V (&lt;a rel=&quot;external&quot; href=&quot;https://github.com/llvm/llvm-project/issues/180199&quot;&gt;#180199&lt;/a&gt;) or 1020 on Xtensa, it’s out of reach.&lt;/p&gt;
&lt;p&gt;Two things are happening in the reproducer below. First, the 2100-byte struct goes into the reserved outgoing call area, which sits between SP and everything else, including the slot. Second, &lt;code&gt;flag = v12&lt;/code&gt; stores to a local more than 2100 bytes above SP while all 14 values are live, so it needs a scratch register.&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: light-dark(#24292E, #E1E4E8); background-color: light-dark(#FFFFFF, #24292E);&quot; &gt;&lt;code data-lang=&quot;c&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;struct&lt;/span&gt;&lt;span&gt; big &lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;    char&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #FFAB70);&quot;&gt; bytes&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;2100&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;void&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #B392F0);&quot;&gt; callee&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;struct&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #FFAB70);&quot;&gt; big&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;void&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #B392F0);&quot;&gt; use&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;char&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; *&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; char&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; *&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #B392F0);&quot;&gt; f&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; int&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; *&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #FFAB70);&quot;&gt;in&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; int&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; *&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #FFAB70);&quot;&gt;out&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; const&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; struct&lt;/span&gt;&lt;span&gt; big &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #FFAB70);&quot;&gt;p&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;  char&lt;/span&gt;&lt;span&gt; flag&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;  char&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #FFAB70);&quot;&gt; buf&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;300&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;  int&lt;/span&gt;&lt;span&gt; v0 &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #FFAB70);&quot;&gt; in&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;0&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; int&lt;/span&gt;&lt;span&gt; v1 &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #FFAB70);&quot;&gt; in&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;1&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; int&lt;/span&gt;&lt;span&gt; v2 &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #FFAB70);&quot;&gt; in&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;2&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;  int&lt;/span&gt;&lt;span&gt; v3 &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #FFAB70);&quot;&gt; in&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;3&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; int&lt;/span&gt;&lt;span&gt; v4 &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #FFAB70);&quot;&gt; in&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;4&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; int&lt;/span&gt;&lt;span&gt; v5 &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #FFAB70);&quot;&gt; in&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;5&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;  int&lt;/span&gt;&lt;span&gt; v6 &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #FFAB70);&quot;&gt; in&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;6&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; int&lt;/span&gt;&lt;span&gt; v7 &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #FFAB70);&quot;&gt; in&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;7&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; int&lt;/span&gt;&lt;span&gt; v8 &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #FFAB70);&quot;&gt; in&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;8&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;  int&lt;/span&gt;&lt;span&gt; v9 &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #FFAB70);&quot;&gt; in&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;9&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; int&lt;/span&gt;&lt;span&gt; v10 &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #FFAB70);&quot;&gt; in&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;10&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; int&lt;/span&gt;&lt;span&gt; v11 &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #FFAB70);&quot;&gt; in&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;11&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;  int&lt;/span&gt;&lt;span&gt; v12 &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #FFAB70);&quot;&gt; in&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;12&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; int&lt;/span&gt;&lt;span&gt; v13 &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #FFAB70);&quot;&gt; in&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;13&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  flag &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;=&lt;/span&gt;&lt;span&gt; v12&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;  int&lt;/span&gt;&lt;span&gt; r &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;=&lt;/span&gt;&lt;span&gt; v0 &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;+&lt;/span&gt;&lt;span&gt; v1 &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;+&lt;/span&gt;&lt;span&gt; v2 &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;+&lt;/span&gt;&lt;span&gt; v3 &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;+&lt;/span&gt;&lt;span&gt; v4 &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;+&lt;/span&gt;&lt;span&gt; v5 &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;+&lt;/span&gt;&lt;span&gt; v6 &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;+&lt;/span&gt;&lt;span&gt; v7 &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;+&lt;/span&gt;&lt;span&gt; v8 &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;+&lt;/span&gt;&lt;span&gt; v9 &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;+&lt;/span&gt;&lt;span&gt; v10 &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;+&lt;/span&gt;&lt;span&gt; v11 &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;+&lt;/span&gt;&lt;span&gt; v13&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;  *&lt;/span&gt;&lt;span&gt;out &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;=&lt;/span&gt;&lt;span&gt; r&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #B392F0);&quot;&gt;  use&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span&gt;flag&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; buf&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #B392F0);&quot;&gt;  callee&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;*&lt;/span&gt;&lt;span&gt;p&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;  return&lt;/span&gt;&lt;span&gt; r&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Here we omit the frame pointer to force Xtensa to reserve the outgoing argument area in the fixed frame. That puts the large call area between SP and the emergency slot.&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: light-dark(#24292E, #E1E4E8); background-color: light-dark(#FFFFFF, #24292E);&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;$ clang --target=xtensa -O2 -fomit-frame-pointer -S repro.c&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;fatal error: error in backend: Incomplete scavenging after 2nd pass&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;RISC-V fixes this by forcing an FP (&lt;a rel=&quot;external&quot; href=&quot;https://github.com/llvm/llvm-project/pull/182124&quot;&gt;#182124&lt;/a&gt;). Since on RISC-V the FP points near the incoming SP (at the top of the frame), it won’t be affected by the large reserved call frame.&lt;/p&gt;
&lt;p&gt;&lt;span class=&quot;aside&quot;&gt; The RISC-V fix says “This idea is borrowed from AArch64”. &lt;a rel=&quot;external&quot; href=&quot;https://github.com/llvm/llvm-project/commit/5c290dc20601&quot;&gt;AArch64&lt;/a&gt; hit the same bug and fixed it in January 2018, eight years earlier. &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Interestingly, an FP at the top doesn’t make you immune either: on AArch64, SME hazard padding between the FP and the locals pushed the slot out of reach (&lt;a rel=&quot;external&quot; href=&quot;https://github.com/llvm/llvm-project/pull/142190&quot;&gt;#142190&lt;/a&gt;). This is quite specific, though; I don’t know of other backends that insert padding there in a similar fashion.&lt;/p&gt;
&lt;p&gt;Anyway, this approach won’t work if you are also putting the frame pointer on the stack pointer, like Xtensa does, so we need something else.&lt;/p&gt;
&lt;hr /&gt;
&lt;p&gt;In that case, another possible solution is to stop reserving the call frame above a certain size (e.g., 1020 for Xtensa), force an FP, and let the SP move around function calls. This would make the FP the anchor, and since there would be no reserved space, the slot would always be reachable.&lt;/p&gt;
&lt;p&gt;A minor upside of this approach is a smaller fixed stack frame. But you do generate some extra instructions to move the SP, so it’s not without a trade-off. Amusingly, these stack adjustments may also need temporary registers, creating another kind of register pressure.&lt;/p&gt;
&lt;p&gt;&lt;span class=&quot;aside&quot;&gt; This is basically the reason behind the reproducer in section A. &lt;/span&gt;&lt;/p&gt;
&lt;hr /&gt;
&lt;h3 id=&quot;c-the-base-register-has-moved-by-the-time-of-the-spill&quot;&gt;C. The Base Register Has Moved by the Time of the Spill&lt;/h3&gt;
&lt;p&gt;In this case, the static layout may be fine, but the stack pointer isn’t where it was when the layout was finalized.&lt;/p&gt;
&lt;p&gt;SP moves for two reasons:&lt;/p&gt;
&lt;div class=&quot;svg-block svg-position-left svg-layout-beside svg-text-valign-center svg-text-align-left svg-has-text&quot;&gt;
&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; id=&quot;fig-modes-base-moved&quot; viewBox=&quot;0 12 280 322&quot; width=&quot;280&quot; height=&quot;322&quot; style=&quot;width: 280px; max-width: 100%; height: auto;&quot; role=&quot;img&quot; font-family=&quot;&#39;IBM Plex Sans&#39;, system-ui, sans-serif&quot;&gt;
  &lt;defs&gt;
    &lt;marker id=&quot;fm-base-moved-arrow&quot; viewBox=&quot;0 0 10 10&quot; refX=&quot;9&quot; refY=&quot;5&quot; markerWidth=&quot;7&quot; markerHeight=&quot;7&quot; orient=&quot;auto-start-reverse&quot;&gt;
      &lt;path d=&quot;M0 0L10 5L0 10z&quot; fill=&quot;currentColor&quot; /&gt;
    &lt;/marker&gt;
  &lt;/defs&gt;
  &lt;g transform=&quot;translate(-520 0)&quot;&gt;
    &lt;g text-anchor=&quot;middle&quot; fill=&quot;currentColor&quot;&gt;
      &lt;text x=&quot;660&quot; y=&quot;36&quot; font-size=&quot;14.5&quot; font-weight=&quot;600&quot;&gt;SP-relative, but SP moved&lt;/text&gt;
      &lt;text x=&quot;660&quot; y=&quot;58&quot; font-size=&quot;11.5&quot; fill-opacity=&quot;0.65&quot;&gt;incoming SP&lt;/text&gt;
    &lt;/g&gt;
    &lt;g fill=&quot;currentColor&quot; fill-opacity=&quot;0.06&quot; stroke=&quot;currentColor&quot; stroke-opacity=&quot;0.6&quot;&gt;
      &lt;rect x=&quot;585&quot; y=&quot;70&quot; width=&quot;150&quot; height=&quot;100&quot; /&gt;
    &lt;/g&gt;
    &lt;rect x=&quot;585&quot; y=&quot;196&quot; width=&quot;150&quot; height=&quot;94&quot; fill=&quot;currentColor&quot; fill-opacity=&quot;0.03&quot; stroke=&quot;currentColor&quot; stroke-opacity=&quot;0.5&quot; stroke-dasharray=&quot;4 3&quot; /&gt;
    &lt;rect x=&quot;585&quot; y=&quot;170&quot; width=&quot;150&quot; height=&quot;26&quot; fill=&quot;#d1495b&quot; fill-opacity=&quot;0.14&quot; stroke=&quot;#d1495b&quot; stroke-width=&quot;1.5&quot; /&gt;
    &lt;g text-anchor=&quot;middle&quot; fill=&quot;currentColor&quot; font-size=&quot;12.5&quot;&gt;
      &lt;text x=&quot;660&quot; y=&quot;124&quot;&gt;locals&lt;/text&gt;
      &lt;text x=&quot;660&quot; y=&quot;187&quot; font-weight=&quot;600&quot;&gt;slot&lt;/text&gt;
      &lt;text x=&quot;660&quot; y=&quot;240&quot; fill-opacity=&quot;0.75&quot;&gt;arguments pushed&lt;/text&gt;
      &lt;text x=&quot;660&quot; y=&quot;256&quot; fill-opacity=&quot;0.75&quot;&gt;for this call&lt;/text&gt;
    &lt;/g&gt;
    &lt;path d=&quot;M739 290h8V183h-8&quot; fill=&quot;none&quot; stroke=&quot;#d1495b&quot; stroke-width=&quot;1.5&quot; /&gt;
    &lt;text transform=&quot;rotate(-90 762 236)&quot; x=&quot;762&quot; y=&quot;236&quot; text-anchor=&quot;middle&quot; font-size=&quot;12&quot; fill=&quot;#d1495b&quot; font-weight=&quot;600&quot;&gt;offset + call size&lt;/text&gt;
    &lt;line x1=&quot;540&quot; y1=&quot;196&quot; x2=&quot;581&quot; y2=&quot;196&quot; stroke=&quot;currentColor&quot; stroke-opacity=&quot;0.55&quot; stroke-width=&quot;1.3&quot; stroke-dasharray=&quot;3 3&quot; /&gt;
    &lt;text x=&quot;579&quot; y=&quot;214&quot; text-anchor=&quot;end&quot; font-size=&quot;11.5&quot; fill=&quot;currentColor&quot; fill-opacity=&quot;0.65&quot;&gt;SP before&lt;/text&gt;
    &lt;line x1=&quot;540&quot; y1=&quot;290&quot; x2=&quot;581&quot; y2=&quot;290&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;1.3&quot; marker-end=&quot;url(#fm-base-moved-arrow)&quot; /&gt;
    &lt;text x=&quot;579&quot; y=&quot;282&quot; text-anchor=&quot;end&quot; font-size=&quot;12.5&quot; font-weight=&quot;600&quot; fill=&quot;currentColor&quot;&gt;SP&lt;/text&gt;
    &lt;g fill=&quot;currentColor&quot; font-size=&quot;12&quot; text-anchor=&quot;middle&quot;&gt;
      &lt;text x=&quot;660&quot; y=&quot;322&quot; fill-opacity=&quot;0.7&quot;&gt;no FP means SP must not move&lt;/text&gt;
    &lt;/g&gt;
  &lt;/g&gt;
&lt;/svg&gt;
&lt;div class=&quot;svg-text&quot;&gt;&lt;p&gt;First, VLAs move it by an amount known only at run time, so the backend cannot address the frame from SP at all.&lt;/p&gt;
&lt;p&gt;Second, calls move it by a fixed amount: the call-frame size. Frame-index elimination tracks that amount so the compiler can correct SP-relative accesses inside the call sequence. This might make it seem like the SP is still usable as a base. And it actually is, except for the scavenger’s own spill and reload.&lt;/p&gt;
&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;A slot that was close to SP in the static layout is now farther away by the call-frame size. Moreover, all outgoing arguments are live at that point, filling up the registers, which is exactly when the scavenger needs one.&lt;/p&gt;
&lt;p&gt;I can’t really show a C reproducer here for Xtensa, because in the current implementation, functions without a frame pointer reserve their call frame. Consequently, this particular problem (SP moving around calls while frame objects remain SP-relative) doesn’t materialize.&lt;/p&gt;
&lt;p&gt;However, ARM can still hit it, and they guard against it with an assertion. You can see it fire in &lt;a rel=&quot;external&quot; href=&quot;https://github.com/llvm/llvm-project/issues/98113&quot;&gt;#98113&lt;/a&gt;. The issue has been open since 2024.&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: light-dark(#24292E, #E1E4E8); background-color: light-dark(#FFFFFF, #24292E);&quot; &gt;&lt;code data-lang=&quot;c&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;struct&lt;/span&gt;&lt;span&gt; big &lt;/span&gt;&lt;span&gt;{&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; int&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #FFAB70);&quot;&gt; w&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;1100&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;void&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #B392F0);&quot;&gt; callee&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;int&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; int&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; int&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; int&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; struct&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #FFAB70);&quot;&gt; big&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; int&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; int&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; int&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; int&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; int&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; int&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; int&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; int&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; int&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; int&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; int&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; int&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;void&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #B392F0);&quot;&gt; f&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;volatile&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; int&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; *&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #FFAB70);&quot;&gt;q&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; const&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; struct&lt;/span&gt;&lt;span&gt; big &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #FFAB70);&quot;&gt;p&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;  int&lt;/span&gt;&lt;span&gt; v0 &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #FFAB70);&quot;&gt; q&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;0&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; v1 &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #FFAB70);&quot;&gt; q&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;1&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; v2 &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #FFAB70);&quot;&gt; q&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;2&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; v3 &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #FFAB70);&quot;&gt; q&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;3&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; v4 &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #FFAB70);&quot;&gt; q&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;4&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; v5 &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #FFAB70);&quot;&gt; q&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;5&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; v6 &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #FFAB70);&quot;&gt; q&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;6&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; v7 &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #FFAB70);&quot;&gt; q&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;7&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; v8 &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #FFAB70);&quot;&gt; q&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;8&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; v9 &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #FFAB70);&quot;&gt; q&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;9&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #B392F0);&quot;&gt;  callee&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;v0&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; v1&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; v2&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; v3&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; *&lt;/span&gt;&lt;span&gt;p&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; v3&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; v0&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; v7&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; v4&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; v1&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; v8&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; v5&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; v2&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; v9&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; v6&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; v3&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; v0&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#E36209, #FFAB70);&quot;&gt;  q&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;0&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; =&lt;/span&gt;&lt;span&gt; v0&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #FFAB70);&quot;&gt; q&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;1&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; =&lt;/span&gt;&lt;span&gt; v1&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #FFAB70);&quot;&gt; q&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;2&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; =&lt;/span&gt;&lt;span&gt; v2&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #FFAB70);&quot;&gt; q&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;3&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; =&lt;/span&gt;&lt;span&gt; v3&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #FFAB70);&quot;&gt; q&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;4&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; =&lt;/span&gt;&lt;span&gt; v4&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #FFAB70);&quot;&gt; q&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;5&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; =&lt;/span&gt;&lt;span&gt; v5&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #FFAB70);&quot;&gt; q&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;6&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; =&lt;/span&gt;&lt;span&gt; v6&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #FFAB70);&quot;&gt; q&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;7&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; =&lt;/span&gt;&lt;span&gt; v7&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #FFAB70);&quot;&gt; q&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;8&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; =&lt;/span&gt;&lt;span&gt; v8&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #FFAB70);&quot;&gt; q&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;9&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; =&lt;/span&gt;&lt;span&gt; v9&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;pre class=&quot;giallo&quot; style=&quot;color: light-dark(#24292E, #E1E4E8); background-color: light-dark(#FFFFFF, #24292E);&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;$ clang --target=thumbv7m-none-eabi -O2 -S repro.c     # LLVM built with assertions&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Assertion `TFI-&amp;gt;hasReservedCallFrame(MF) &amp;amp;&amp;amp; &amp;quot;Cannot use SP to access the emergency spill slot in &amp;quot; &amp;quot;functions without a reserved call frame&amp;quot;&amp;#39; failed.&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The assertion sits right below a comment that explains it, in &lt;a rel=&quot;external&quot; href=&quot;https://github.com/llvm/llvm-project/blob/4c22a9f755be90fa8ce385f589761033609fe66f/llvm/lib/Target/ARM/ARMBaseRegisterInfo.cpp#L864-L867&quot;&gt;&lt;code&gt;ARMBaseRegisterInfo.cpp&lt;/code&gt;&lt;/a&gt;:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;code&gt;PEI::scavengeFrameVirtualRegs()&lt;/code&gt; cannot accurately track &lt;code&gt;SPAdj&lt;/code&gt; because the call frame setup/destroy instructions have already been eliminated.
That means the stack pointer cannot be used to access the emergency spill slot when &lt;code&gt;!hasReservedCallFrame()&lt;/code&gt;.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;&lt;code&gt;SPAdj&lt;/code&gt; is exactly how movement of the SP is tracked. Frame-index elimination uses it to correct the offsets, but in &lt;a rel=&quot;external&quot; href=&quot;https://github.com/llvm/llvm-project/blob/4c22a9f755be90fa8ce385f589761033609fe66f/llvm/lib/CodeGen/RegisterScavenging.cpp#L389&quot;&gt;&lt;code&gt;RegisterScavenging.cpp&lt;/code&gt;&lt;/a&gt; it’s hard-coded as &lt;code&gt;int SPAdj = 0&lt;/code&gt;. This means that the scavenger assumes SP hasn’t moved.&lt;/p&gt;
&lt;p&gt;Since it cannot account for the movement of the SP, the scavenger keeps using the old &lt;code&gt;[sp + offset]&lt;/code&gt;. Instead of a crash, we can get a silent miscompilation. Whatever lives at that wrong address has a chance to get silently clobbered.&lt;/p&gt;
&lt;p&gt;&lt;span class=&quot;aside&quot;&gt; It’s rare in practice. It needs an out-of-range offset with all registers live around a call. &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;In this particular ARM example, without assertions, the resulting code looks just fine. No outgoing argument gets clobbered; I think it’s by pure luck in the instruction ordering. You can, however, hand-roll MIR that exposes the issue.&lt;/p&gt;
&lt;h2 id=&quot;reserve-the-call-frame-or-not&quot;&gt;Reserve the Call Frame or Not?&lt;/h2&gt;
&lt;p&gt;So without a reserved call frame, SP can’t anchor the slot. Then why does ARM stop reserving it? Here’s the reason, in &lt;a rel=&quot;external&quot; href=&quot;https://github.com/llvm/llvm-project/blob/4c22a9f755be90fa8ce385f589761033609fe66f/llvm/lib/Target/ARM/ARMFrameLowering.cpp#L363-L374&quot;&gt;&lt;code&gt;ARMFrameLowering.cpp&lt;/code&gt;&lt;/a&gt;:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: light-dark(#24292E, #E1E4E8); background-color: light-dark(#FFFFFF, #24292E);&quot; &gt;&lt;code data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#6A737D, #6A737D);&quot;&gt;//&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6A737D, #6A737D);&quot;&gt; It&amp;#39;s not always a good idea to include the call frame as part of the&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#6A737D, #6A737D);&quot;&gt;//&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6A737D, #6A737D);&quot;&gt; stack frame. ARM (especially Thumb) has small immediate offset to&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#6A737D, #6A737D);&quot;&gt;//&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6A737D, #6A737D);&quot;&gt; address the stack frame. So a large call frame can cause poor codegen&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#6A737D, #6A737D);&quot;&gt;//&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6A737D, #6A737D);&quot;&gt; and may even makes it impossible to scavenge a register.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;if&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;CFSize&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; &amp;gt;=&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; &amp;lt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt; 12&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; -&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt; 1&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; /&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt; 2&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6A737D, #6A737D);&quot;&gt;  //&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6A737D, #6A737D);&quot;&gt; Half of imm12&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;  return&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt; false&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Wait a minute, that’s part of the fix from section B? ARM stops reserving large call frames so the slot stays in reach, but it doesn’t force an FP. It’s such a recurring problem. We fixed one issue by not reserving the call frame, and now we’re hitting the same problem because we &lt;em&gt;didn’t&lt;/em&gt; reserve it. The gist of it is that we can’t overload the stack pointer with two jobs. We can either let it move around calls or use it as an anchor for the slot.&lt;/p&gt;
&lt;p&gt;The proper fix is to use another register as the anchor. LLVM’s default already does this: &lt;a rel=&quot;external&quot; href=&quot;https://github.com/llvm/llvm-project/blob/4c22a9f755be90fa8ce385f589761033609fe66f/llvm/include/llvm/CodeGen/TargetFrameLowering.h#L315-L317&quot;&gt;&lt;code&gt;hasReservedCallFrame()&lt;/code&gt;&lt;/a&gt; returns &lt;code&gt;!hasFP(MF)&lt;/code&gt;. SP only moves when there’s also an FP. That’s also why the second fix in B forces an FP. ARM overrides the default to fix B and loses that guarantee.&lt;/p&gt;
&lt;p&gt;&lt;span class=&quot;aside&quot;&gt; This was corrected in Thumb1, where it &lt;a rel=&quot;external&quot; href=&quot;https://github.com/llvm/llvm-project/blob/4c22a9f755be90fa8ce385f589761033609fe66f/llvm/lib/Target/ARM/ARMBaseRegisterInfo.cpp#L484-L489&quot;&gt;forces a base pointer&lt;/a&gt; whenever SP moves, “for correctness in cases where we need an emergency spill slot.” &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Why not always force a stable reference from an FP/BP then? Well, the cost is a register. If your target has many registers, losing one to the FP may not matter much. On Xtensa, with only 14 usable general-purpose registers, you probably don’t want it in every function, considering you only need it where the call frame is too big for SP to reach across.&lt;/p&gt;
&lt;p&gt;AArch64 and RISC-V already work this way. They force an FP only above 255 and 2047 bytes of call frame, respectively. On Xtensa, that would be functions with more than 1020 bytes of outgoing arguments.&lt;/p&gt;
&lt;hr /&gt;
&lt;h2 id=&quot;takeaways&quot;&gt;Takeaways&lt;/h2&gt;
&lt;p&gt;My understanding is that it all comes down to a few connected decisions.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Place the emergency slot close to the base register used in addressing.&lt;/li&gt;
&lt;li&gt;That register must be a stable reference point.&lt;/li&gt;
&lt;li&gt;Reserving the outgoing call area, using an FP, or introducing a base pointer are ways to achieve this, depending on the target’s layout.&lt;/li&gt;
&lt;li&gt;Unknowns at compile time force you to use an FP/BP, as the stack pointer becomes unreliable.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;It all seems so obvious when I look at it like this in the end. But it was quite difficult debugging and understanding it because there are many little things that can go wrong. Nice thing is we can see that ARM, Aarch, RISC etc. already went through these problems over the years, so we get to shamelessly borrow their solutions.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;I only briefly touched on other mechanisms like hazard padding, alignment and base pointers to keep the post in focus. There might be still plenty of small edge cases left to explore.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Next up, I’ll look at how to decide whether to reserve an emergency spill slot at all.&lt;/p&gt;
&lt;hr /&gt;
&lt;p&gt;Disclaimer: All reproducers were tested with LLVM main at &lt;a rel=&quot;external&quot; href=&quot;https://github.com/llvm/llvm-project/commit/4c22a9f755be90fa8ce385f589761033609fe66f&quot;&gt;&lt;code&gt;4c22a9f755be&lt;/code&gt;&lt;/a&gt; (24.0.0git, built with assertions). Xtensa is an experimental target, so builds need it explicitly enabled.&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Hunting a wrong-code bug in GCC&#39;s vectorizer</title>
        <published>2026-10-18T00:00:00+00:00</published>
        <updated>2026-10-18T00:00:00+00:00</updated>
        
        <author>
          <name>Unknown</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://cakgok.github.io/posts/gcc-slp-induction-reuse/"/>
        <id>https://cakgok.github.io/posts/gcc-slp-induction-reuse/</id>
        
        <content type="html" xml:base="https://cakgok.github.io/posts/gcc-slp-induction-reuse/">&lt;p&gt;I encountered a bug during a coding competition of all places. You know the kind: you solve an algorithm problem, send your code to an online judge, and you get a verdict back. What do you do when you get back a “Wrong Answer” though? You blame &lt;del&gt;the compiler&lt;/del&gt; yourself, of course. But what if Clang and GCC give you different outputs, which one do you trust?&lt;/p&gt;
&lt;p&gt;In general, with a bug like this, most of the time it’s undefined behavior. Not this time though.&lt;/p&gt;
&lt;p&gt;It became &lt;a rel=&quot;external&quot; href=&quot;https://gcc.gnu.org/bugzilla/show_bug.cgi?id=126225&quot;&gt;PR126225&lt;/a&gt;. I ended up writing the &lt;a rel=&quot;external&quot; href=&quot;https://gcc.gnu.org/cgit/gcc/commit/?id=6c1a23f4bfe2403c12439c299064af557b317737&quot;&gt;fix&lt;/a&gt; myself. It’s been merged into trunk and backported to 16.2.&lt;/p&gt;
&lt;h2 id=&quot;reproducer&quot;&gt;Reproducer&lt;/h2&gt;
&lt;p&gt;The starting code already wasn’t that complicated, so I stripped it down until I could get something minimal.&lt;/p&gt;
&lt;p&gt;Here is the loop.
The &lt;code&gt;fill&lt;/code&gt; function is also the first of the regression tests that went in with the fix.&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: light-dark(#24292E, #E1E4E8); background-color: light-dark(#FFFFFF, #24292E);&quot; &gt;&lt;code data-lang=&quot;c&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;#&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;include&lt;/span&gt;&lt;span style=&quot;color: light-dark(#032F62, #9ECBFF);&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#032F62, #9ECBFF);&quot;&gt;assert.h&lt;/span&gt;&lt;span style=&quot;color: light-dark(#032F62, #9ECBFF);&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #B392F0);&quot;&gt;__attribute__&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;noipa&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;void&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #B392F0);&quot;&gt; fill&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; *&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #FFAB70);&quot;&gt;ans&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; int&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #FFAB70);&quot;&gt; rounds&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;  int&lt;/span&gt;&lt;span&gt; x &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt; 1&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;  int&lt;/span&gt;&lt;span&gt; y &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt; 2&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;  int&lt;/span&gt;&lt;span&gt; i &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt; 0&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;  while&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;rounds &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt; 0&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#E36209, #FFAB70);&quot;&gt;      ans&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;i&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;++&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; =&lt;/span&gt;&lt;span&gt; x&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #FFAB70);&quot;&gt; ans&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;i&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;++&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; =&lt;/span&gt;&lt;span&gt; x&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #FFAB70);&quot;&gt; ans&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;i&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;++&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; =&lt;/span&gt;&lt;span&gt; x&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #FFAB70);&quot;&gt; ans&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;i&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;++&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; =&lt;/span&gt;&lt;span&gt; x&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#E36209, #FFAB70);&quot;&gt;      ans&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;i&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;++&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; =&lt;/span&gt;&lt;span&gt; y&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #FFAB70);&quot;&gt; ans&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;i&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;++&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; =&lt;/span&gt;&lt;span&gt; y&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #FFAB70);&quot;&gt; ans&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;i&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;++&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; =&lt;/span&gt;&lt;span&gt; y&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #FFAB70);&quot;&gt; ans&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;i&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;++&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; =&lt;/span&gt;&lt;span&gt; y&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;      --&lt;/span&gt;&lt;span&gt;rounds&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      x &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;+=&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt; 2&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      y &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;+=&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt; 2&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #B392F0);&quot;&gt; main&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;void&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;    int&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #FFAB70);&quot;&gt; ans&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;16&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; =&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;0&lt;/span&gt;&lt;span&gt;}&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #B392F0);&quot;&gt;    fill&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;ans&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt; 2&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;    const&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; int&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #FFAB70);&quot;&gt; expected&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;16&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; =&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;        1&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt; 1&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt; 1&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt; 1&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt; 2&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt; 2&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt; 2&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt; 2&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;        3&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt; 3&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt; 3&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt; 3&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt; 4&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt; 4&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt; 4&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt; 4&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    }&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;    for&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;int&lt;/span&gt;&lt;span&gt; i &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt; 0&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span&gt; i &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt; 16&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; ++&lt;/span&gt;&lt;span&gt;i&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #B392F0);&quot;&gt; assert&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #FFAB70);&quot;&gt;ans&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;i&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; ==&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #FFAB70);&quot;&gt; expected&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;i&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;With &lt;code&gt;rounds = 2&lt;/code&gt; the answer should be:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: light-dark(#24292E, #E1E4E8); background-color: light-dark(#FFFFFF, #24292E);&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;expected  1 1 1 1 2 2 2 2 3 3 3 3 4 4 4 4&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;-O2       1 1 1 1 1 1 1 1 3 3 3 3 3 3 3 3&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h2 id=&quot;experimenting-with-flags&quot;&gt;Experimenting With Flags&lt;/h2&gt;
&lt;p&gt;I tried a few other shapes.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;With &lt;code&gt;int64&lt;/code&gt;, it still produced wrong code.&lt;/li&gt;
&lt;li&gt;With &lt;code&gt;-mavx2&lt;/code&gt; and &lt;code&gt;int32&lt;/code&gt;s, it produced correct code, because AVX2 makes it use its wider 256-bit &lt;code&gt;ymm&lt;/code&gt; registers.&lt;/li&gt;
&lt;li&gt;With &lt;code&gt;-mavx2&lt;/code&gt; and &lt;code&gt;int64&lt;/code&gt;, it’s wrong again, because a 256-bit register only holds four 64-bit values, and the bug comes back.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;span class=&quot;aside&quot;&gt;&lt;em&gt;Vectorization&lt;/em&gt; means one instruction works on several values at once. A 128-bit SSE register (&lt;code&gt;xmm&lt;/code&gt;) holds four &lt;code&gt;int32&lt;/code&gt;s, a 256-bit AVX2 register (&lt;code&gt;ymm&lt;/code&gt;) holds eight. Each slot is a &lt;em&gt;lane&lt;/em&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Flags&lt;/th&gt;&lt;th&gt;Element&lt;/th&gt;&lt;th&gt;Result&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;-O1&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;int32&lt;/code&gt;&lt;/td&gt;&lt;td&gt;correct&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;-O2&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;int32&lt;/code&gt;&lt;/td&gt;&lt;td&gt;wrong&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;-O2 -fno-tree-vectorize&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;int32&lt;/code&gt;&lt;/td&gt;&lt;td&gt;correct&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;-O2 -mavx2&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;int32&lt;/code&gt;&lt;/td&gt;&lt;td&gt;correct&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;-O2&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;int64&lt;/code&gt;&lt;/td&gt;&lt;td&gt;wrong&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;-O2 -mavx2&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;int64&lt;/code&gt;&lt;/td&gt;&lt;td&gt;wrong&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;-O2 -fno-tree-slp-vectorize&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;int32&lt;/code&gt;&lt;/td&gt;&lt;td&gt;wrong&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;-O2 -fno-tree-loop-vectorize&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;int32&lt;/code&gt;&lt;/td&gt;&lt;td&gt;correct&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;I also realized if you put a print statement inside the loop, the bug goes away. Moreover, at &lt;code&gt;-O1&lt;/code&gt; it was correct. &lt;code&gt;-O2&lt;/code&gt; was wrong, and &lt;code&gt;-O3&lt;/code&gt; was correct again.&lt;/p&gt;
&lt;p&gt;One thing that confused me was &lt;code&gt;-fno-tree-slp-vectorize&lt;/code&gt;. Apparently, it only disables the separate basic-block SLP pass. The loop vectorizer’s own SLP still runs, so it doesn’t help.&lt;/p&gt;
&lt;p&gt;&lt;span class=&quot;aside&quot;&gt;Two ways of vectorizing: the first is Superword-Level Parallelism (SLP), also called straight-line. It looks at side-by-side operations, and packs them into a vector operation if it can.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;-O3&lt;/code&gt; threw me off too, but GCC has an attribute, &lt;code&gt;__attribute__((noipa))&lt;/code&gt;, for exactly this purpose. It lets us disable interprocedural optimizations between functions, such as inlining. That’s why &lt;code&gt;fill&lt;/code&gt; has it in the reproducer above: with it, &lt;code&gt;-O3&lt;/code&gt; is wrong too. Note that &lt;code&gt;__attribute__((noinline))&lt;/code&gt; was not enough to bring the bug back at &lt;code&gt;-O3&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;&lt;span class=&quot;aside&quot;&gt;The other is loop vectorization. It packs the same statement from different iterations into a single vector. In this example, the loop vectorizer uses SLP to pack operations within one iteration.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The bug was present in all the major releases from 11.1 up to 16.1, which was the latest at that point. At &lt;code&gt;-O2&lt;/code&gt; it only shows up from 12.1, because GCC 12 is when &lt;code&gt;-O2&lt;/code&gt; started vectorizing.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;-fno-tree-loop-vectorize&lt;/code&gt; already pointed me to something happening in the loop vectorizer.&lt;/p&gt;
&lt;h2 id=&quot;more-experiments&quot;&gt;More Experiments&lt;/h2&gt;
&lt;p&gt;If you work with compilers a lot, at this point you probably know where to look. Source is there after all. But I didn’t. Instead, I wrote a template program that tested the compiler output with different shapes. The template replaces the body with different patterns such as &lt;code&gt;xxxyyyyy&lt;/code&gt;, signifying one letter per store. Every case is compiled at &lt;code&gt;-O1&lt;/code&gt; as a reference and &lt;code&gt;-O2&lt;/code&gt;, and the outputs are compared.&lt;/p&gt;
&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Lane pattern&lt;/th&gt;&lt;th&gt;Flags&lt;/th&gt;&lt;th&gt;Result&lt;/th&gt;&lt;th&gt;-O2 output (wrong cases)&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;xxxxyyyy&lt;/code&gt;&lt;/td&gt;&lt;td&gt;SSE&lt;/td&gt;&lt;td&gt;wrong&lt;/td&gt;&lt;td&gt;&lt;code&gt;11111111 33333333&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;xxxyyyyy&lt;/code&gt;&lt;/td&gt;&lt;td&gt;SSE&lt;/td&gt;&lt;td&gt;wrong&lt;/td&gt;&lt;td&gt;&lt;code&gt;11121112 33343334&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;xxxxxxxy&lt;/code&gt;&lt;/td&gt;&lt;td&gt;SSE&lt;/td&gt;&lt;td&gt;wrong&lt;/td&gt;&lt;td&gt;&lt;code&gt;11111111 33333333&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;xyxyxyxy&lt;/code&gt;&lt;/td&gt;&lt;td&gt;SSE&lt;/td&gt;&lt;td&gt;correct&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;xxyyxxyy&lt;/code&gt;&lt;/td&gt;&lt;td&gt;SSE&lt;/td&gt;&lt;td&gt;correct&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;xxxxyyyy&lt;/code&gt;&lt;/td&gt;&lt;td&gt;AVX2&lt;/td&gt;&lt;td&gt;correct&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;So, it’s not really about the uniformity of the pattern.
The passing patterns (&lt;code&gt;xyxyxyxy&lt;/code&gt;, &lt;code&gt;xxyyxxyy&lt;/code&gt;) repeat every 4 elements, which ended up playing a part in understanding the bug. In hindsight, 4 is the number of &lt;code&gt;int32&lt;/code&gt;s that fit in a 128-bit SSE register, which is why the register width and the element size mattered. I didn’t see that yet, though.&lt;/p&gt;
&lt;h2 id=&quot;looking-at-the-assembly&quot;&gt;Looking at the Assembly&lt;/h2&gt;
&lt;p&gt;So, I checked the generated assembly. &lt;em&gt;(This is with 16.1, -O2)&lt;/em&gt;&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: light-dark(#24292E, #E1E4E8); background-color: light-dark(#FFFFFF, #24292E);&quot; &gt;&lt;code data-lang=&quot;asm&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;&amp;quot;fill&amp;quot;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;    test&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;    esi&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;esi&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;    jle&lt;/span&gt;&lt;span&gt;     .L1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;    lea&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;     eax&lt;/span&gt;&lt;span&gt;, [&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;rsi&lt;/span&gt;&lt;span&gt;-&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;1&lt;/span&gt;&lt;span&gt;]             &lt;/span&gt;&lt;span style=&quot;color: light-dark(#6A737D, #6A737D);&quot;&gt; # rounds - 1, for the end pointer&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;    pcmpeqd&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt; xmm0&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;xmm0&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6A737D, #6A737D);&quot;&gt;                # xmm0 = {-1,-1,-1,-1}, all ones&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;    mov&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;     edx&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;2&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;    sal&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;     rax&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;5&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;    psrld&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;   xmm0&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;31&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6A737D, #6A737D);&quot;&gt;                  # xmm0 = {1,1,1,1} &amp;lt;- x&amp;#39;s start&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;    movd&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;    xmm1&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;edx&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;    lea&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;     rax&lt;/span&gt;&lt;span&gt;, [&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;rdi&lt;/span&gt;&lt;span&gt;+&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;32&lt;/span&gt;&lt;span&gt;+&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;rax&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;    pshufd&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;  xmm1&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;xmm1&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6A737D, #6A737D);&quot;&gt;             # xmm1 = {2,2,2,2} &amp;lt;- the STEP, not y&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #B392F0);&quot;&gt;L3&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #B392F0);&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;    movups&lt;/span&gt;&lt;span&gt;  XMMWORD PTR [&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;rdi&lt;/span&gt;&lt;span&gt;], &lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;xmm0&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6A737D, #6A737D);&quot;&gt;   # ans[i+0..3] = xmm0&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;    add&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;     rdi&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;32&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;    movups&lt;/span&gt;&lt;span&gt;  XMMWORD PTR [&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;rdi&lt;/span&gt;&lt;span&gt;-&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;16&lt;/span&gt;&lt;span&gt;], &lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;xmm0&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6A737D, #6A737D);&quot;&gt;  # ans[i+4..7] = xmm0 &amp;lt;- should be y!&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;    paddd&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;   xmm0&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;xmm1&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6A737D, #6A737D);&quot;&gt;                # xmm0 += {2,2,2,2}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;    cmp&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;     rax&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;rdi&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;    jne&lt;/span&gt;&lt;span&gt;     .L3&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;If you check it at &lt;code&gt;-O1&lt;/code&gt;, you will see that it doesn’t use &lt;code&gt;xmm&lt;/code&gt; registers at all. It does scalar operations on general purpose registers.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Apparently, &lt;code&gt;pcmpeqd&lt;/code&gt; + &lt;code&gt;psrld xmm0, 31&lt;/code&gt; is a trick for building {1,1,1,1} without loading anything from memory. Compare a register with itself to get all-ones bits, then shift each 32-bit lane right by 31. Pretty cool.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;mov edx, 2&lt;/code&gt; + &lt;code&gt;movd&lt;/code&gt; + &lt;code&gt;pshufd xmm1, xmm1, 0&lt;/code&gt; fills &lt;code&gt;xmm1&lt;/code&gt; with [2, 2, 2, 2].&lt;br /&gt;
At first glance I thought this was y (which also starts at 2), but &lt;code&gt;xmm1&lt;/code&gt; is only ever used to increment &lt;code&gt;xmm0&lt;/code&gt; via &lt;code&gt;paddd&lt;/code&gt;. So, it’s the step instead.&lt;/li&gt;
&lt;li&gt;Looking at the stores in the loop body, we see that it writes &lt;code&gt;xmm0&lt;/code&gt; to memory, shifts 16 bytes, and writes &lt;code&gt;xmm0&lt;/code&gt; again before adding the step. (Well, actually, it shifts 32 bytes and writes to -16, but I digress.)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;span class=&quot;aside&quot;&gt;There is also a future lesson here: I would have had an easier time if I had chosen more distinct values to investigate. It would have been so much easier to just dump everything and &lt;code&gt;grep&lt;/code&gt; for that distinct value. &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The point is, there should be a second vector but it’s using &lt;code&gt;xmm0&lt;/code&gt; for both halves of the array.&lt;/p&gt;
&lt;p&gt;Suspicious.&lt;/p&gt;
&lt;h2 id=&quot;checking-the-gimple-dumps&quot;&gt;Checking the GIMPLE Dumps&lt;/h2&gt;
&lt;p&gt;Since I already suspect that the bug is in the vectorizer, the plan is to check both sides: the GIMPLE given to the backend, and the GIMPLE going into the vectorizer.&lt;/p&gt;
&lt;p&gt;&lt;span class=&quot;aside&quot;&gt;GIMPLE is the intermediate representation (IR) of GCC where most of the optimizations happen. It’s then passed off to the backend for code generation.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;First question to answer is this: Is the GIMPLE handed to the backend already wrong? If it is, this will clear the backend.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;-fdump-tree-optimized&lt;/code&gt; is the flag we need to see what is passed.&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: light-dark(#24292E, #E1E4E8); background-color: light-dark(#FFFFFF, #24292E);&quot; &gt;&lt;code data-lang=&quot;c&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;bb &lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  # vect_vec_iv_.&lt;/span&gt;&lt;span style=&quot;color: light-dark(#B31D28, #FDAEB7);font-style: italic;&quot;&gt;15_29&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; =&lt;/span&gt;&lt;span&gt; PHI &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #B392F0);&quot;&gt;_28&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;4&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt; 1&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt; 1&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt; 1&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt; 1&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;3&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  # ivtmp.&lt;/span&gt;&lt;span style=&quot;color: light-dark(#B31D28, #FDAEB7);font-style: italic;&quot;&gt;23_53&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; =&lt;/span&gt;&lt;span&gt; PHI &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;ivtmp.&lt;/span&gt;&lt;span style=&quot;color: light-dark(#B31D28, #FDAEB7);font-style: italic;&quot;&gt;23_60&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;4&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; ivtmp.&lt;/span&gt;&lt;span style=&quot;color: light-dark(#B31D28, #FDAEB7);font-style: italic;&quot;&gt;23_59&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;3&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  _58 &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;=&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;void&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; *&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; ivtmp.&lt;/span&gt;&lt;span style=&quot;color: light-dark(#B31D28, #FDAEB7);font-style: italic;&quot;&gt;23_53&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  MEM &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #B392F0);&quot;&gt;vector&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;4&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; int&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; *&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;_58&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;       =&lt;/span&gt;&lt;span&gt; vect_vec_iv_.&lt;/span&gt;&lt;span style=&quot;color: light-dark(#B31D28, #FDAEB7);font-style: italic;&quot;&gt;15_29&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  MEM &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #B392F0);&quot;&gt;vector&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;4&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; int&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; *&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;_58 &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: light-dark(#B31D28, #FDAEB7);font-style: italic;&quot;&gt; 16B&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; =&lt;/span&gt;&lt;span&gt; vect_vec_iv_.&lt;/span&gt;&lt;span style=&quot;color: light-dark(#B31D28, #FDAEB7);font-style: italic;&quot;&gt;15_29&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  _28 &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;=&lt;/span&gt;&lt;span&gt; vect_vec_iv_.&lt;/span&gt;&lt;span style=&quot;color: light-dark(#B31D28, #FDAEB7);font-style: italic;&quot;&gt;15_29&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; +&lt;/span&gt;&lt;span&gt; {&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt; 2&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt; 2&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt; 2&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt; 2&lt;/span&gt;&lt;span&gt; }&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  ivtmp.&lt;/span&gt;&lt;span style=&quot;color: light-dark(#B31D28, #FDAEB7);font-style: italic;&quot;&gt;23_60&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; =&lt;/span&gt;&lt;span&gt; ivtmp.&lt;/span&gt;&lt;span style=&quot;color: light-dark(#B31D28, #FDAEB7);font-style: italic;&quot;&gt;23_53&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; +&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt; 32&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;  if&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;_4 &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;!=&lt;/span&gt;&lt;span&gt; ivtmp.&lt;/span&gt;&lt;span style=&quot;color: light-dark(#B31D28, #FDAEB7);font-style: italic;&quot;&gt;23_60&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; goto&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span&gt;bb &lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;span class=&quot;aside&quot;&gt;In Static Single Assignment (SSA) form, every variable can only be assigned once. Each assignment gets a new numbered name: &lt;code&gt;x_58&lt;/code&gt; and &lt;code&gt;x_50&lt;/code&gt; are versions of &lt;code&gt;x&lt;/code&gt;, and &lt;code&gt;_28&lt;/code&gt; is a temporary.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span class=&quot;aside&quot;&gt;A &lt;em&gt;PHI&lt;/em&gt; node is how the compiler handles a variable whose value depends on which path the code took to get here. In a loop, that’s either entering it for the first time or jumping back from the previous iteration. The numbers in brackets are the blocks the values come from: &lt;code&gt;(3)&lt;/code&gt; is before the loop, &lt;code&gt;(4)&lt;/code&gt; is the loop itself.&lt;/span&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The PHI takes its initial value {1,1,1,1}, which is stored to bytes 0–15 and again to bytes 16–31, giving &lt;code&gt;1 1 1 1 1 1 1 1&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;_28 = {3,3,3,3} and the pointer advances 32 bytes.&lt;/li&gt;
&lt;li&gt;Second iteration gives &lt;code&gt;3 3 3 3 3 3 3 3&lt;/code&gt;. That’s the wrong output from the reproducer.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;As expected, it lines up with the assembly: the PHI is xmm0, the step is xmm1.&lt;/p&gt;
&lt;h2 id=&quot;gimple-entering-the-vectorizer&quot;&gt;GIMPLE Entering the Vectorizer&lt;/h2&gt;
&lt;p&gt;&lt;code&gt;-fno-tree-vectorize&lt;/code&gt; eliminates the bug. However it doesn’t prove the vectorizer is faulty. Maybe an earlier pass merged x and y and the vectorizer merely exposed it? So, we check if the IR that the vectorizer receives is correct. If it is, that narrows the search to the vectorizer or a later pass.&lt;/p&gt;
&lt;p&gt;The last dump before the vectorizer runs can be checked with: &lt;code&gt;-fdump-tree-ifcvt&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;&lt;span class=&quot;aside&quot;&gt; &lt;code&gt;-fdump-passes&lt;/code&gt; dumps the list of optimization passes, and you can check the ordering. &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Trimmed:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: light-dark(#24292E, #E1E4E8); background-color: light-dark(#FFFFFF, #24292E);&quot; &gt;&lt;code data-lang=&quot;c&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;bb &lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  # rounds_57 &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;=&lt;/span&gt;&lt;span&gt; PHI &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #B392F0);&quot;&gt;rounds_49&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;6&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #B392F0);&quot;&gt; rounds_30&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;D&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;5&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  # x_58 &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;=&lt;/span&gt;&lt;span&gt; PHI &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #B392F0);&quot;&gt;x_50&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;6&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt; 1&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;5&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  # y_59 &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;=&lt;/span&gt;&lt;span&gt; PHI &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #B392F0);&quot;&gt;y_51&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;6&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt; 2&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;5&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  # i_60 &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;=&lt;/span&gt;&lt;span&gt; PHI &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #B392F0);&quot;&gt;i_47&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;6&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt; 0&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;5&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  _3 &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #B392F0);&quot;&gt; ans_33&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;D&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; +&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;i_60 &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt; 4&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;    *&lt;/span&gt;&lt;span&gt;_3  &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;=&lt;/span&gt;&lt;span&gt; x_58&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  _6 &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;=&lt;/span&gt;&lt;span&gt; ...      &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt; 4&lt;/span&gt;&lt;span&gt; bytes&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;        *&lt;/span&gt;&lt;span&gt;_6  &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;=&lt;/span&gt;&lt;span&gt; x_58&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  _9 &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;=&lt;/span&gt;&lt;span&gt; ...      &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt; 8&lt;/span&gt;&lt;span&gt; bytes&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;        *&lt;/span&gt;&lt;span&gt;_9  &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;=&lt;/span&gt;&lt;span&gt; x_58&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  _12 &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;=&lt;/span&gt;&lt;span&gt; ...     &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt; 12&lt;/span&gt;&lt;span&gt; bytes&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;       *&lt;/span&gt;&lt;span&gt;_12 &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;=&lt;/span&gt;&lt;span&gt; x_58&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  _15 &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;=&lt;/span&gt;&lt;span&gt; ...     &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt; 16&lt;/span&gt;&lt;span&gt; bytes&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;       *&lt;/span&gt;&lt;span&gt;_15 &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;=&lt;/span&gt;&lt;span&gt; y_59&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  _18 &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;=&lt;/span&gt;&lt;span&gt; ...     &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt; 20&lt;/span&gt;&lt;span&gt; bytes&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;       *&lt;/span&gt;&lt;span&gt;_18 &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;=&lt;/span&gt;&lt;span&gt; y_59&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  _21 &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;=&lt;/span&gt;&lt;span&gt; ...     &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt; 24&lt;/span&gt;&lt;span&gt; bytes&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;       *&lt;/span&gt;&lt;span&gt;_21 &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;=&lt;/span&gt;&lt;span&gt; y_59&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  _24 &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;=&lt;/span&gt;&lt;span&gt; ...     &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt; 28&lt;/span&gt;&lt;span&gt; bytes&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;       *&lt;/span&gt;&lt;span&gt;_24 &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;=&lt;/span&gt;&lt;span&gt; y_59&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  rounds_49 &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;=&lt;/span&gt;&lt;span&gt; rounds_57 &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; -&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;1&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  x_50 &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;=&lt;/span&gt;&lt;span&gt; x_58 &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt; 2&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  y_51 &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;=&lt;/span&gt;&lt;span&gt; y_59 &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt; 2&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  i_47 &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;=&lt;/span&gt;&lt;span&gt; i_60 &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt; 8&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;There are four PHI nodes (four scalar induction variables: &lt;code&gt;rounds&lt;/code&gt;, &lt;code&gt;x&lt;/code&gt;, &lt;code&gt;y&lt;/code&gt;, &lt;code&gt;i&lt;/code&gt;). &lt;code&gt;x_58&lt;/code&gt; starts at 1 and &lt;code&gt;y_59&lt;/code&gt; starts at 2. Eight consecutive stores form a contiguous 32-byte group. This proves that everything the vectorizer needs is here and correct.&lt;/p&gt;
&lt;p&gt;&lt;span class=&quot;aside&quot;&gt;An &lt;em&gt;induction variable&lt;/em&gt; (IV) is a variable that changes by a fixed step every iteration, like &lt;code&gt;x += 2&lt;/code&gt;.&lt;/span&gt;&lt;/p&gt;
&lt;h2 id=&quot;what-happens-inside-the-vectorizer&quot;&gt;What Happens Inside the Vectorizer&lt;/h2&gt;
&lt;p&gt;We can check the whole vectorization pass with &lt;code&gt;-fdump-tree-vect-details&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;First, we look at the plan. This shows that the vectorizer’s analysis is correct. Early in the pass (&lt;code&gt;vect_analyze_scalar_cycles&lt;/code&gt;), the scalar evolution (SCEV) analysis recognizes each PHI as an induction. It separates x and y as two distinct IVs with different initial values.&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: light-dark(#24292E, #E1E4E8); background-color: light-dark(#FFFFFF, #24292E);&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;step: -1,  init: rounds_30(D)   Detected induction.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;step: 2,   init: 1              Detected induction.   &amp;lt;- x&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;step: 2,   init: 2              Detected induction.   &amp;lt;- y&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;step: 8,   init: 0              Detected induction.   &amp;lt;- i&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;span class=&quot;aside&quot;&gt;Scalar Evolution (SCEV) is how GCC summarizes a loop variable’s behavior into a &lt;code&gt;{init, +, step}&lt;/code&gt; progression, rather than chasing individual assignments around the loop body.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The store group becomes a single SLP node (0x…4798) with 8 lanes. Its only child node is an induction-PHI node with 8 lanes (0x…4838), one per stored value:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: light-dark(#24292E, #E1E4E8); background-color: light-dark(#FFFFFF, #24292E);&quot; &gt;&lt;code data-lang=&quot;c&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;node &lt;/span&gt;&lt;span style=&quot;color: light-dark(#B31D28, #FDAEB7);font-style: italic;&quot;&gt;0x...4798&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;max_nunits&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;4&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; refcnt&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;2&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #B392F0);&quot;&gt; vector&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;4&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; int&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;op template: &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;*&lt;/span&gt;&lt;span&gt;_3 &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;=&lt;/span&gt;&lt;span&gt; x_58&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    stmt &lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; *&lt;/span&gt;&lt;span&gt;_3 &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;=&lt;/span&gt;&lt;span&gt; x_58&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span&gt;    stmt &lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;4&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; *&lt;/span&gt;&lt;span&gt;_15 &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;=&lt;/span&gt;&lt;span&gt; y_59&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    stmt &lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; *&lt;/span&gt;&lt;span&gt;_6 &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;=&lt;/span&gt;&lt;span&gt; x_58&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span&gt;    stmt &lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;5&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; *&lt;/span&gt;&lt;span&gt;_18 &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;=&lt;/span&gt;&lt;span&gt; y_59&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    stmt &lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; *&lt;/span&gt;&lt;span&gt;_9 &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;=&lt;/span&gt;&lt;span&gt; x_58&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span&gt;    stmt &lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;6&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; *&lt;/span&gt;&lt;span&gt;_21 &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;=&lt;/span&gt;&lt;span&gt; y_59&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    stmt &lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; *&lt;/span&gt;&lt;span&gt;_12 &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;=&lt;/span&gt;&lt;span&gt; x_58&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span&gt;   stmt &lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;7&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; *&lt;/span&gt;&lt;span&gt;_24 &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;=&lt;/span&gt;&lt;span&gt; y_59&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    children &lt;/span&gt;&lt;span style=&quot;color: light-dark(#B31D28, #FDAEB7);font-style: italic;&quot;&gt;0x...4838&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;node &lt;/span&gt;&lt;span style=&quot;color: light-dark(#B31D28, #FDAEB7);font-style: italic;&quot;&gt;0x...4838&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;max_nunits&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;4&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; refcnt&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;2&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #B392F0);&quot;&gt; vector&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;4&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; int&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;op template: x_58 &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;=&lt;/span&gt;&lt;span&gt; PHI &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #B392F0);&quot;&gt;x_50&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;6&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt; 1&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;5&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    stmt &lt;/span&gt;&lt;span style=&quot;color: light-dark(#B31D28, #FDAEB7);font-style: italic;&quot;&gt;0..3&lt;/span&gt;&lt;span&gt; x_58 &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;=&lt;/span&gt;&lt;span&gt; PHI &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #B392F0);&quot;&gt;x_50&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;6&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt; 1&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;5&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    stmt &lt;/span&gt;&lt;span style=&quot;color: light-dark(#B31D28, #FDAEB7);font-style: italic;&quot;&gt;4..7&lt;/span&gt;&lt;span&gt; y_59 &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;=&lt;/span&gt;&lt;span&gt; PHI &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #B392F0);&quot;&gt;y_51&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;6&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt; 2&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;5&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #B392F0);&quot;&gt;    children&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;nil&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;nil&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;vectorization factor &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt; 1&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In SLP terms:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;group_size = 8&lt;/code&gt;. The node has 8 &lt;em&gt;lanes&lt;/em&gt; [x x x x y y y y].&lt;/li&gt;
&lt;li&gt;&lt;code&gt;nunits = 4&lt;/code&gt;. It targets 128-bit xmm registers that hold four 32-bit ints.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;VF = 1&lt;/code&gt;. Since a single scalar iteration already gives 8 values - enough to fill two full registers - it doesn’t need unrolling.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;nvects = 2&lt;/code&gt;. Number of vectors. Each SLP node simply needs to emit 2 of them: lanes 0–3 and lanes 4–7.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This tree is correct. Lanes 4–7 are explicitly y_59 with init 2.&lt;br /&gt;
Up until this point, the compiler hasn’t lost track of y_59.&lt;/p&gt;
&lt;p&gt;If we look at the transform:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: light-dark(#24292E, #E1E4E8); background-color: light-dark(#FFFFFF, #24292E);&quot; &gt;&lt;code data-lang=&quot;c&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;--&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;--&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;--&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;vectorizing SLP node starting from: x_58 &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;=&lt;/span&gt;&lt;span&gt; PHI &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #B392F0);&quot;&gt;x_50&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;6&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt; 1&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;8&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;transform induction phi.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;--&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;--&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;--&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span&gt;vectorizing SLP node starting from: &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;*&lt;/span&gt;&lt;span&gt;_3 &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;=&lt;/span&gt;&lt;span&gt; x_58&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;transform store.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;add new stmt: MEM &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #B392F0);&quot;&gt;vector&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;4&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; int&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; *&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;vectp_ans.&lt;/span&gt;&lt;span style=&quot;color: light-dark(#B31D28, #FDAEB7);font-style: italic;&quot;&gt;16_27&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; =&lt;/span&gt;&lt;span&gt; vect_vec_iv_.&lt;/span&gt;&lt;span style=&quot;color: light-dark(#B31D28, #FDAEB7);font-style: italic;&quot;&gt;15_29&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;add new stmt: vectp_ans.&lt;/span&gt;&lt;span style=&quot;color: light-dark(#B31D28, #FDAEB7);font-style: italic;&quot;&gt;16_77&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; =&lt;/span&gt;&lt;span&gt; vectp_ans.&lt;/span&gt;&lt;span style=&quot;color: light-dark(#B31D28, #FDAEB7);font-style: italic;&quot;&gt;16_27&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; +&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt; 16&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;add new stmt: MEM &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #B392F0);&quot;&gt;vector&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;4&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; int&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; [&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; *&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;vectp_ans.&lt;/span&gt;&lt;span style=&quot;color: light-dark(#B31D28, #FDAEB7);font-style: italic;&quot;&gt;16_77&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; =&lt;/span&gt;&lt;span&gt; vect_vec_iv_.&lt;/span&gt;&lt;span style=&quot;color: light-dark(#B31D28, #FDAEB7);font-style: italic;&quot;&gt;15_29&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This dump narrows the problem down.&lt;/p&gt;
&lt;p&gt;Looking at what &lt;code&gt;transform store&lt;/code&gt; and &lt;code&gt;transform induction phi&lt;/code&gt; just emitted, we can see that it generates two 16-byte vector stores (ans[0..3] at vectp_ans.16_27, and ans[4..7] at + 16), which is expected. However, both stores write the exact same &lt;code&gt;vect_vec_iv_.15_29&lt;/code&gt; variable.&lt;/p&gt;
&lt;p&gt;&lt;span class=&quot;aside&quot;&gt;A &lt;em&gt;vector IV&lt;/em&gt; is the same thing in vector form: &lt;code&gt;vect_vec_iv_&lt;/code&gt; starts at {1,1,1,1} and gets {2,2,2,2} added each iteration.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;There is no IV starting at { 2, 2, 2, 2 } anywhere. The only { 2, 2, 2, 2 } is the step: the IV for y was never built.&lt;/p&gt;
&lt;h2 id=&quot;bisecting&quot;&gt;Bisecting&lt;/h2&gt;
&lt;p&gt;If you are familiar with GCC internals, you probably know that only two functions print &lt;code&gt;transform induction phi.&lt;/code&gt;: &lt;code&gt;vectorizable_induction&lt;/code&gt; and &lt;code&gt;vectorizable_nonlinear_induction&lt;/code&gt;. There is nothing &lt;em&gt;nonlinear&lt;/em&gt; about the reproducer.&lt;/p&gt;
&lt;p&gt;If you are like me and you don’t know where this happens, you go ahead and bisect. With 10~15 steps, I was able to bisect it to this commit: &lt;a rel=&quot;external&quot; href=&quot;https://gcc.gnu.org/cgit/gcc/commit/?id=092cdbd9198497&quot;&gt;092cdbd&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Perfect.&lt;/p&gt;
&lt;p&gt;The commit only touches &lt;code&gt;vectorizable_induction&lt;/code&gt;, in &lt;code&gt;tree-vect-loop.c&lt;/code&gt; (renamed to &lt;code&gt;tree-vect-loop.cc&lt;/code&gt; in 2022).&lt;/p&gt;
&lt;p&gt;The commit is called “Re-instantiate SLP induction IV CSE”. It puts back in a shortcut that was previously removed. It’s from November 2020, so it first shipped in GCC 11.1, which matches the versions above.&lt;/p&gt;
&lt;p&gt;&lt;span class=&quot;aside&quot;&gt;CSE: Common subexpression elimination. Reusing a value instead of computing it twice, which is exactly what went wrong.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The analysis and transform halves of &lt;code&gt;vectorizable_induction&lt;/code&gt; are separated in the function. This also matches with our previous findings, because the analysis was already correct, and the commit only changed the transform.&lt;/p&gt;
&lt;h2 id=&quot;reading-the-source&quot;&gt;Reading the Source&lt;/h2&gt;
&lt;p&gt;To vectorize the loop, &lt;code&gt;vectorizable_induction&lt;/code&gt; builds a vector IV for
each chunk (one vector’s worth of lanes). Here that means two vector IVs, one for the &lt;code&gt;x&lt;/code&gt; lanes and one for the &lt;code&gt;y&lt;/code&gt; lanes. Each vector IV costs a register and an add per iteration, so the vectorizer tries not to build more than it needs. Before the fix, the count was computed like this (in &lt;code&gt;gcc/tree-vect-loop.cc&lt;/code&gt;):&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: light-dark(#24292E, #E1E4E8); background-color: light-dark(#FFFFFF, #24292E);&quot; &gt;&lt;code data-lang=&quot;c&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#6A737D, #6A737D);&quot;&gt;/*&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6A737D, #6A737D);&quot;&gt; Compute the number of distinct IVs we need.  First reduce&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#6A737D, #6A737D);&quot;&gt;   group_size if it is a multiple of const_nunits so we get&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#6A737D, #6A737D);&quot;&gt;   one IV for a group_size of 4 but const_nunits 2.  &lt;/span&gt;&lt;span style=&quot;color: light-dark(#6A737D, #6A737D);&quot;&gt;*/&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;unsigned&lt;/span&gt;&lt;span&gt; group_sizep &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;=&lt;/span&gt;&lt;span&gt; group_size&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;if&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;group_sizep &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;%&lt;/span&gt;&lt;span&gt; const_nunits &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;==&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt; 0&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  group_sizep &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;=&lt;/span&gt;&lt;span&gt; group_sizep &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;/&lt;/span&gt;&lt;span&gt; const_nunits&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;nivs &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #B392F0);&quot;&gt; least_common_multiple&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;group_sizep&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; const_nunits&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; /&lt;/span&gt;&lt;span&gt; const_nunits&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;As the comment says, this is the number of distinct IVs to build.
For our loop, &lt;code&gt;group_size&lt;/code&gt; is 8 and &lt;code&gt;const_nunits&lt;/code&gt; (lanes per vector) is 4.&lt;br /&gt;
&lt;code&gt;group_sizep&lt;/code&gt; becomes 2.&lt;br /&gt;
&lt;code&gt;least_common_multiple (2, 4) / 4&lt;/code&gt; is &lt;strong&gt;1&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;&lt;span class=&quot;aside&quot;&gt;&lt;code&gt;nivs&lt;/code&gt; is basically the “Number of Induction Vectors”. &lt;code&gt;ivn&lt;/code&gt; is just the loop counter.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;GCC decided one vector IV was enough. Later, when it filled in the vector definitions for the remaining chunks, it copied the first IV into every slot:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: light-dark(#24292E, #E1E4E8); background-color: light-dark(#FFFFFF, #24292E);&quot; &gt;&lt;code data-lang=&quot;c&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#6A737D, #6A737D);&quot;&gt;/*&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6A737D, #6A737D);&quot;&gt; Fill up to the number of vectors we need for the whole group.  &lt;/span&gt;&lt;span style=&quot;color: light-dark(#6A737D, #6A737D);&quot;&gt;*/&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;nivs &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #B392F0);&quot;&gt; least_common_multiple&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;group_size&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; const_nunits&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; /&lt;/span&gt;&lt;span&gt; const_nunits&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;for&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span&gt; ivn &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span&gt; nivs&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; ++&lt;/span&gt;&lt;span&gt;ivn&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    slp_node&lt;/span&gt;&lt;span&gt;-&amp;gt;&lt;/span&gt;&lt;span&gt;push_vec_def&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #B392F0);&quot;&gt;SLP_TREE_VEC_DEFS&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;slp_node&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;0&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    vec_steps&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;quick_push&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #FFAB70);&quot;&gt;vec_steps&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt;0&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Here &lt;code&gt;nivs&lt;/code&gt; is recalculated from the full &lt;code&gt;group_size&lt;/code&gt;, not &lt;code&gt;group_sizep&lt;/code&gt;, so it’s 2. &lt;code&gt;ivn&lt;/code&gt; is 1 after building the one IV, so the loop runs once and fills the second slot.&lt;/p&gt;
&lt;p&gt;So the second vector, with the &lt;code&gt;y&lt;/code&gt; lanes, got the IV that was built for the first vector, with the &lt;code&gt;x&lt;/code&gt; lanes.&lt;/p&gt;
&lt;p&gt;However, this reduction is only correct when every chunk is a copy of an earlier one. The same initial values and the same steps, lane for lane. The earlier comment (a group of 4 with 2 lanes per vector) works when the second pair of lanes actually repeats but nothing was checking that it did.&lt;/p&gt;
&lt;p&gt;But wait, there is a second problem in the fill: it always copies IV 0 (&lt;code&gt;vec_steps[0]&lt;/code&gt;). Which is ok when only one IV is built. When there are more though, chunks that should reuse the second or third IV get the first one instead. So even a valid reduction can give wrong code, whenever a later chunk needs an IV other than the first. So this becomes another piece to patch.&lt;/p&gt;
&lt;h2 id=&quot;the-fix&quot;&gt;The Fix&lt;/h2&gt;
&lt;p&gt;We have to change two things.&lt;/p&gt;
&lt;p&gt;First, compute the unreduced count - which is always correct - and treat the reduced count only as a &lt;em&gt;candidate&lt;/em&gt;:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: light-dark(#24292E, #E1E4E8); background-color: light-dark(#FFFFFF, #24292E);&quot; &gt;&lt;code data-lang=&quot;c&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;nivs &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #B392F0);&quot;&gt; least_common_multiple&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;group_size&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; const_nunits&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; /&lt;/span&gt;&lt;span&gt; const_nunits&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;unsigned&lt;/span&gt;&lt;span&gt; group_sizep &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;=&lt;/span&gt;&lt;span&gt; group_size&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;if&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;group_sizep &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;%&lt;/span&gt;&lt;span&gt; const_nunits &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;==&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt; 0&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    group_sizep &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;=&lt;/span&gt;&lt;span&gt; group_sizep &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;/&lt;/span&gt;&lt;span&gt; const_nunits&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;    unsigned&lt;/span&gt;&lt;span&gt; candidate_nivs&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;      =&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #B392F0);&quot;&gt; least_common_multiple&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;group_sizep&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; const_nunits&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; /&lt;/span&gt;&lt;span&gt; const_nunits&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;    if&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;candidate_nivs &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span&gt; nivs&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;        &amp;amp;&amp;amp;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #B392F0);&quot;&gt; vect_slp_induction_reuse_p&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;steps&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; inits&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; group_size&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                                       const_nunits&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; candidate_nivs&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; nivs&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      nivs &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;=&lt;/span&gt;&lt;span&gt; candidate_nivs&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We create a helper: &lt;code&gt;vect_slp_induction_reuse_p&lt;/code&gt; that checks the shortcut is valid. &lt;br /&gt;
For every chunk past the candidate count, it compares each lane’s initial value and step against the lane at the same position in the chunk it would reuse
(&lt;code&gt;ivn % candidate_nivs&lt;/code&gt;):&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: light-dark(#24292E, #E1E4E8); background-color: light-dark(#FFFFFF, #24292E);&quot; &gt;&lt;code data-lang=&quot;c&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;for&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;unsigned&lt;/span&gt;&lt;span&gt; ivn &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;=&lt;/span&gt;&lt;span&gt; candidate_nivs&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span&gt; ivn &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span&gt; nivs&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; ++&lt;/span&gt;&lt;span&gt;ivn&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;    unsigned&lt;/span&gt;&lt;span&gt; reuse_ivn &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;=&lt;/span&gt;&lt;span&gt; ivn &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;%&lt;/span&gt;&lt;span&gt; candidate_nivs&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;    for&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;unsigned&lt;/span&gt;&lt;span&gt; HOST_WIDE_INT eltn &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt; 0&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span&gt; eltn &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span&gt; const_nunits&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; ++&lt;/span&gt;&lt;span&gt;eltn&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;        unsigned&lt;/span&gt;&lt;span&gt; HOST_WIDE_INT elt &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;=&lt;/span&gt;&lt;span&gt; ivn &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;*&lt;/span&gt;&lt;span&gt; const_nunits &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;+&lt;/span&gt;&lt;span&gt; eltn&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;        unsigned&lt;/span&gt;&lt;span&gt; HOST_WIDE_INT reused_elt &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;=&lt;/span&gt;&lt;span&gt; reuse_ivn &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;*&lt;/span&gt;&lt;span&gt; const_nunits &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;+&lt;/span&gt;&lt;span&gt; eltn&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;        if&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;!&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #B392F0);&quot;&gt;operand_equal_p&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #FFAB70);&quot;&gt;steps&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;elt&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #FFAB70);&quot;&gt; steps&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;reused_elt&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt; 0&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;            ||&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; !&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #B392F0);&quot;&gt;operand_equal_p&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #FFAB70);&quot;&gt;inits&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;elt&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #FFAB70);&quot;&gt; inits&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;reused_elt&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt; 0&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;          return&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt; false&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;      }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;return&lt;/span&gt;&lt;span style=&quot;color: light-dark(#005CC5, #79B8FF);&quot;&gt; true&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;span class=&quot;aside&quot;&gt;“Wait, it’s all modular arithmetic?”&lt;br&gt;“Always has been.”&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;If any lane differs, we throw away the candidate, keep the full count and build a separate IV for each chunk.&lt;/p&gt;
&lt;p&gt;Secondly, the loop that fills in the remaining chunks now reuses IVs
&lt;em&gt;cyclically&lt;/em&gt; rather than always taking the first one:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: light-dark(#24292E, #E1E4E8); background-color: light-dark(#FFFFFF, #24292E);&quot; &gt;&lt;code data-lang=&quot;c&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#6A737D, #6A737D);&quot;&gt;/*&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6A737D, #6A737D);&quot;&gt; Fill up to the number of vectors we need for the whole group.  &lt;/span&gt;&lt;span style=&quot;color: light-dark(#6A737D, #6A737D);&quot;&gt;*/&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;nivs &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #B392F0);&quot;&gt; least_common_multiple&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;group_size&lt;/span&gt;&lt;span&gt;,&lt;/span&gt;&lt;span&gt; const_nunits&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; /&lt;/span&gt;&lt;span&gt; const_nunits&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;unsigned&lt;/span&gt;&lt;span&gt; generated_nivs &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;=&lt;/span&gt;&lt;span&gt; ivn&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6A737D, #6A737D);&quot;&gt;   /*&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6A737D, #6A737D);&quot;&gt; IVs actually built &lt;/span&gt;&lt;span style=&quot;color: light-dark(#6A737D, #6A737D);&quot;&gt;*/&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;for&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span&gt; ivn &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span&gt; nivs&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt; ++&lt;/span&gt;&lt;span&gt;ivn&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;    unsigned&lt;/span&gt;&lt;span&gt; reuse_ivn &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;=&lt;/span&gt;&lt;span&gt; ivn &lt;/span&gt;&lt;span style=&quot;color: light-dark(#D73A49, #F97583);&quot;&gt;%&lt;/span&gt;&lt;span&gt; generated_nivs&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    slp_node&lt;/span&gt;&lt;span&gt;-&amp;gt;&lt;/span&gt;&lt;span&gt;push_vec_def&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span style=&quot;color: light-dark(#6F42C1, #B392F0);&quot;&gt;SLP_TREE_VEC_DEFS&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span&gt;slp_node&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;reuse_ivn&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    vec_steps&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;&lt;span&gt;quick_push&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span style=&quot;color: light-dark(#E36209, #FFAB70);&quot;&gt;vec_steps&lt;/span&gt;&lt;span&gt;[&lt;/span&gt;&lt;span&gt;reuse_ivn&lt;/span&gt;&lt;span&gt;]&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;  }&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This matters once the reduced count is more than one. We have to fill it cyclically. Take a group of 24 lanes laid out as &lt;code&gt;X Y X X Y X&lt;/code&gt;, four lanes per chunk. The pattern repeats every three chunks, so three IVs cover all six chunks, but chunk 4 has to reuse IV 1 (the &lt;code&gt;Y&lt;/code&gt; one), not IV 0.&lt;/p&gt;
&lt;p&gt;With a single IV, &lt;code&gt;ivn % 1&lt;/code&gt; is always 0, so the old loop and the new one do the same thing.&lt;/p&gt;
&lt;h2 id=&quot;regression-tests&quot;&gt;Regression Tests&lt;/h2&gt;
&lt;p&gt;Three new tests went into the patch.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;vect-iv-12.c&lt;/code&gt;: chunks with &lt;strong&gt;different inits&lt;/strong&gt; (the reproducer).&lt;/li&gt;
&lt;li&gt;&lt;code&gt;vect-iv-13.c&lt;/code&gt;: chunks with the same inits but &lt;strong&gt;different steps&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;In both, the IV reuse must be rejected.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;vect-iv-14.c&lt;/code&gt;: the &lt;code&gt;X Y X X Y X&lt;/code&gt; checks the cyclic reuse pattern when reuse is &lt;em&gt;valid&lt;/em&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;We check if it takes the shortcut, through a new dump message (&lt;code&gt;reusing 3 SLP induction IVs for 6 vector chunks&lt;/code&gt;), and that the cyclic reuse gives the right values.&lt;/p&gt;
&lt;h2 id=&quot;missed-optimization-and-beyond&quot;&gt;Missed Optimization and Beyond&lt;/h2&gt;
&lt;p&gt;Is there missed optimization here? Maybe. Generally speaking, we could do better with &lt;code&gt;nivs = 2&lt;/code&gt; since we only have X and Y shapes. However, the code can only reuse chunk &lt;code&gt;ivn % candidate_nivs&lt;/code&gt;, so it has no way of expressing that.&lt;/p&gt;
&lt;p&gt;Even then, the code at that point is in such a weird shape: an SLP group of IVs at least two vectors long, where the chunks repeat every three but only come in two distinct shapes. Even reading that is a handful. Probably not worth dwelling too much on.&lt;/p&gt;
&lt;hr /&gt;
&lt;p&gt;Once I sent the patch, the turnaround was extremely quick. After a small back and forth, it has been merged, and is currently being backported to all supported releases. All in all, a fun debugging experience. Interesting to see how this bug survived such a long time, seeing that the eventual reproducer is extremely simple.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Bootstrapped and regtested on x86_64-pc-linux-gnu.&lt;/em&gt;&lt;/p&gt;
</content>
        
    </entry>
</feed>
