{"id":37093,"date":"2026-11-17T18:23:46","date_gmt":"2026-11-17T18:23:46","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/cppblog\/?p=37093"},"modified":"2026-11-17T18:23:46","modified_gmt":"2026-11-17T18:23:46","slug":"c-optimizations-in-msvc-build-tools-14-52","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/cppblog\/c-optimizations-in-msvc-build-tools-14-52\/","title":{"rendered":"C++ Optimizations in MSVC Build Tools 14.52"},"content":{"rendered":"<h2 id=\"c-optimizations-in-msvc-build-tools-14.52\">C++ Optimizations in MSVC Build Tools 14.52<\/h2>\n<p>The Microsoft C++ team implemented new optimizations in MSVC Build Tools 14.52.\nLet&#8217;s walk through examples of some of these optimizations. We will focus on\nreassociation, loop optimizations, SIMD code generation, inlining, and patterns. Where\nnoted, we&#8217;ll also include a few optimizations that appeared in MSVC 14.51 but\nwhich were not covered in previous blog posts.<\/p>\n<h2>Improved Reassociation<\/h2>\n<p>We improved arithmetic reassociation to make shared calculations easier to\nidentify and eliminate. The compiler is free to reassociate integer expressions\nand can reassociate floating-point expressions with <code>\/fp:fast<\/code>.\nPreviously, the compiler reassociated terms that would simplify, such as\n<code>x + y - x<\/code>, and then rebuilt a balanced expression tree. For example:<\/p>\n<pre><code class=\"language-text\">(((((a + b) + x) + y) - x) + z) =&gt; (a + b) + (y + z)\r\n<\/code><\/pre>\n<p>Although this is good, simplification and balancing alone without regard for\ncommon expressions can prevent other optimizations.<\/p>\n<p>Consider this function:<\/p>\n<pre><code class=\"language-cpp\">void foo(double x, double y, double z, double d, double* out) {\r\n    out[0] = 1.0 + 2.5 * x * x - d;\r\n    out[1] = 1.0 + 2.5 * y * y - d;\r\n    out[2] = 1.0 + 2.5 * z * z - d;\r\n}\r\n<\/code><\/pre>\n<p>Nothing directly simplifies and there are three addition\/subtraction terms so\nbalancing does not do anything, but all three expressions contain <code>1.0 - d<\/code>.\nUnder <code>\/fp:fast<\/code>, the compiler should arrange for <code>1.0 - d<\/code> to occur once:<\/p>\n<pre><code class=\"language-cpp\">void foo(double x, double y, double z, double d, double* out) {\r\n    double temp = 1.0 - d;\r\n    out[0] = temp + 2.5 * x * x;\r\n    out[1] = temp + 2.5 * y * y;\r\n    out[2] = temp + 2.5 * z * z;\r\n}\r\n<\/code><\/pre>\n<p>The updated compiler performs this reassociation. It groups subexpressions in a\nranked order where literals come first, followed by loop invariants, then\nvariants. Grouping invariants next to literals, which are also invariant, helps\nthe compiler to hoist larger expressions from loops. Within the groups, the\nglobal frequency of each term is considered, including instances where the term\noccurs with a different sign. Our algorithm is very similar to the one\ndescribed in the global reassociation section of the paper Effective Partial\nRedundancy Elimination by Briggs and Cooper.<\/p>\n<p>For example:<\/p>\n<pre><code class=\"language-cpp\">void foo2(int n, int x, int* out, int* out2) {\r\n    for (int i = 0; i &lt; n; ++i) {\r\n        out[i] = 1 + 2 * i + x;\r\n        out2[i] = 1 - 2 * i + x;\r\n    }\r\n}\r\n<\/code><\/pre>\n<p>Applying reassociation, hoisting, and common subexpression elimination yields:<\/p>\n<pre><code class=\"language-cpp\">void foo2(int n, int x, int* out, int* out2) {\r\n    int hoisted = 1 + x;\r\n    for (int i = 0; i &lt; n; ++i) {\r\n        int temp = 2 * i;\r\n        out[i] = hoisted + temp;\r\n        out2[i] = hoisted - temp;\r\n    }\r\n}\r\n<\/code><\/pre>\n<h2 id=\"loop-optimizations\">Loop Optimizations<\/h2>\n<h3 id=\"loop-final-value-optimization\">Loop final value optimization<\/h3>\n<p>The loop optimizer can replace a repeated induction variable update with a\ncalculation of its final value. When the intermediate values are not needed\ninside the loop, the updates can be removed. If nothing else remains in the\nloop, then the loop itself can be removed.<\/p>\n<p>For example, a constant increment repeated a known number of times:<\/p>\n<pre><code class=\"language-cpp\">result = 0;\r\nfor (int i = 0; i &lt; 10; i++) {\r\n    result += 5;\r\n}\r\n<\/code><\/pre>\n<p>can become:<\/p>\n<pre><code class=\"language-cpp\">result = 50;\r\n<\/code><\/pre>\n<p>Other work in the loop does not necessarily prevent final-value optimization.\nIn this example, printing still requires iteration, but updating <code>result<\/code> does\nnot:<\/p>\n<pre><code class=\"language-cpp\">result = 0;\r\nfor (int i = 0; i &lt; 10; i++) {\r\n    print(i);\r\n    result += 5;\r\n}\r\n<\/code><\/pre>\n<p>The transformed code keeps the printing loop and materializes the result afterward:<\/p>\n<pre><code class=\"language-cpp\">for (int i = 0; i &lt; 10; i++) {\r\n    print(i);\r\n}\r\nresult = 50;\r\n<\/code><\/pre>\n<p>A runtime trip count can also be handled:<\/p>\n<pre><code class=\"language-cpp\">result = 0;\r\nfor (int i = 0; i &lt; n; i++) {\r\n    result += 5;\r\n}\r\n<\/code><\/pre>\n<p>becomes:<\/p>\n<pre><code class=\"language-cpp\">result = n &gt; 0 ? n * 5 : 0;\r\n<\/code><\/pre>\n<p>The zero-trip case is preserved. These transformations assume that the compiler\ncan establish the conditions needed to preserve the original program&#8217;s\nbehavior.<\/p>\n<h3 id=\"better-address-modes-in-loops\">Better address modes in loops<\/h3>\n<p>Address calculations are now optimized with more information about the entire\nloop, rather than choosing the largest addressing mode for each instruction in\nisolation. The work spans arithmetic-expression reordering, loop-invariant code\nmotion, common-subexpression elimination, and address-mode construction. The\noptimization was motivated by unrolled loops where nearby memory accesses can\nshare a common base address. It is especially useful on ARM64, where\nneighboring loads with a common base can be combined into load-pair (<code>LDP<\/code>)\ninstructions. For example, consider this sequence of instructions within a loop:<\/p>\n<pre><code class=\"language-asm\">add x28, x8, #1\r\nadd x24, x8, #2\r\nadd x27, x8, #3\r\nadd x20, x8, #4\r\nadd x19, x8, #5\r\nadd x15, x8, #6\r\nadd x23, x8, #7\r\nadd x26, x8, #8\r\nadd x22, x8, #9\r\nadd x14, x8, #10\r\nadd x0, x8, #11\r\nadd x13, x8, #12\r\nadd x1, x8, #13\r\nadd x12, x8, #14\r\nadd x7, x8, #15\r\nadd x11, x8, #16\r\nadd x21, x8, #17\r\nadd x25, x8, #18\r\n<\/code><\/pre>\n<p>The loop then used 19 individual indexed loads:<\/p>\n<pre><code class=\"language-asm\">ldr d26, [x10, x28, lsl #3]\r\nldr d25, [x10, x8, lsl #3]\r\nldr d23, [x10, x24, lsl #3]\r\nldr d15, [x10, x27, lsl #3]\r\nldr d14, [x10, x20, lsl #3]\r\nldr d13, [x10, x19, lsl #3]\r\nldr d12, [x10, x15, lsl #3]\r\nldr d0, [x10, x23, lsl #3]\r\nldr d1, [x10, x26, lsl #3]\r\nldr d30, [x10, x22, lsl #3]\r\nldr d29, [x10, x14, lsl #3]\r\nldr d2, [x10, x0, lsl #3]\r\nldr d3, [x10, x13, lsl #3]\r\nldr d4, [x10, x1, lsl #3]\r\nldr d28, [x10, x12, lsl #3]\r\nldr d5, [x10, x7, lsl #3]\r\nldr d6, [x10, x11, lsl #3]\r\nldr d7, [x10, x21, lsl #3]\r\nldr d31, [x10, x25, lsl #3]\r\n<\/code><\/pre>\n<p>The improved code calculates a shared base before the loop:<\/p>\n<pre><code class=\"language-asm\">lsl x24, x8, #3\r\nadd x28, x24, x10\r\n<\/code><\/pre>\n<p>The loads in the loop use constant offsets from that base, allowing nine pairs and one remaining individual load:<\/p>\n<pre><code class=\"language-asm\">ldp d25, d26, [x28]\r\nldp d23, d15, [x28, #16]\r\nldp d14, d13, [x28, #32]\r\nldp d12, d0, [x28, #48]\r\nldp d1, d30, [x28, #64]\r\nldp d29, d2, [x28, #80]\r\nldp d3, d4, [x28, #96]\r\nldp d28, d5, [x28, #112]\r\nldp d6, d7, [x28, #128]\r\nldr d31, [x28, #144]\r\n<\/code><\/pre>\n<h3 id=\"sharing-arm64-address-calculations-across-indexed-accesses\">Sharing ARM64 address calculations across indexed accesses<\/h3>\n<p>Another address-mode improvement targets accesses that share a fixed member\noffset but use different array indices, such as as neighborhood-pattern\ncomputation that loads eight elements from a member array. Each address has the\nform:<\/p>\n<pre><code class=\"language-text\">(basePtr + varyingIndex * elementSize) + memberOffset\r\n<\/code><\/pre>\n<p>Previously, the compiler calculated <code>basePtr + varyingIndex * elementSize<\/code>\nseparately for each access and folded <code>memberOffset<\/code> into the load. Although\neach address mode was efficient in isolation, the sequence repeated work.<\/p>\n<p>The improved code instead computes a shared base:<\/p>\n<pre><code class=\"language-text\">sharedBase = basePtr + memberOffset\r\naddress = sharedBase + varyingIndex * elementSize\r\n<\/code><\/pre>\n<p>For example, the old ARM64 sequence contained eight address calculations and\neight loads. The first two accesses looked like this:<\/p>\n<pre><code class=\"language-asm\">add x8, x19, x10, lsl #2\r\nldr w0, [x8, #0x500]\r\nadd x8, x19, x11, lsl #2\r\nldr w1, [x8, #0x500]\r\n<\/code><\/pre>\n<p>With the member offset included in a shared base, all eight loads can use\nscaled-indexed addressing directly:<\/p>\n<pre><code class=\"language-asm\">add x20, x19, #0x500\r\nldr w0, [x20, x10, lsl #2]\r\nldr w1, [x20, x11, lsl #2]\r\nldr w2, [x20, x12, lsl #2]\r\nldr w3, [x20, x13, lsl #2]\r\nldr w4, [x20, x14, lsl #2]\r\nldr w5, [x20, x15, lsl #2]\r\nldr w6, [x20, x16, lsl #2]\r\nldr w7, [x20, x17, lsl #2]\r\n<\/code><\/pre>\n<p>This reduces the sequence from 16 instructions to nine. Unlike the preceding\nload-pair example, the indices can differ; the opportunity is to share the\nfixed part of the address, not to combine adjacent loads.<\/p>\n<h3 id=\"hoisting-conditionally-executed-loop-invariant-operations\">Hoisting conditionally executed loop-invariant operations<\/h3>\n<p>Loop-invariant code motion can now hoist eligible pure operations even when\nthey originally appeared in a conditional block. The inputs do not change\nbetween iterations, so the calculation need not be repeated whenever the\ncondition is true.<\/p>\n<p>For example:<\/p>\n<pre><code class=\"language-cpp\">for (int i = 0; i &lt; n; i++) {\r\n    if (i % 2) {\r\n        sum += a * b;\r\n    }\r\n}\r\n<\/code><\/pre>\n<p>can be transformed into:<\/p>\n<pre><code class=\"language-cpp\">c = a * b;\r\nfor (int i = 0; i &lt; n; i++) {\r\n    if (i % 2) {\r\n        sum += c;\r\n    }\r\n}\r\n<\/code><\/pre>\n<p>The multiplication moves out of the loop and the conditional accumulation stays\nin place. This is not a general license to move side effects or unsafe\noperations out of conditional execution.<\/p>\n<h2 id=\"simd-code-generation-and-vectorization\">SIMD Code Generation and Vectorization<\/h2>\n<h3 id=\"small-loop-vectorization-on-arm64\">Small-loop vectorization on ARM64<\/h3>\n<p>Starting in MSVC 14.51, the ARM64 loop vectorizer accepts small loops that\nexactly fill a vector register, including 64-bit vectors. Previously, a loop\nsuch as this was rejected as too small:<\/p>\n<pre><code class=\"language-cpp\">void small_loop(int* __restrict a, const int* __restrict b) {\r\n    for (int i = 0; i &lt; 2; i++) {\r\n        a[i] += b[i];\r\n    }\r\n}\r\n<\/code><\/pre>\n<p>Now the loop body vectorizes as:<\/p>\n<pre><code class=\"language-asm\">ldr d17, [x0]\r\nldr d16, [x1]\r\nadd v16.2s, v17.2s, v16.2s\r\nstr d16, [x0]\r\n<\/code><\/pre>\n<h3 id=\"arm64-neon-bitmask-patterns\">ARM64 NEON bitmask patterns<\/h3>\n<p>The first pattern combines negation with a sign test when range information\nproves the input to the negation cannot be <code>INT_MIN<\/code>. For example:<\/p>\n<pre><code class=\"language-cpp\">#include &lt;arm_neon.h&gt;\r\n\r\nint32x4_t shift_sign_bit_s32(int32x4_t a) {\r\n    return vshrq_n_s32(vnegq_s32(vshrq_n_s32(a, 1)), 31);\r\n}\r\n<\/code><\/pre>\n<p>In MSVC 14.50, the sequence was:<\/p>\n<pre><code class=\"language-asm\">sshr v16.4s, v0.4s, #1\r\nneg v16.4s, v16.4s\r\ncmlt v0.4s, v16.4s, #0\r\n<\/code><\/pre>\n<p>In MSVC 14.51, the initial shift supplies the range information needed to\nreplace the last two instructions with a positive comparison:<\/p>\n<pre><code class=\"language-asm\">sshr v16.4s, v0.4s, #1\r\ncmgt v0.4s, v16.4s, #0\r\n<\/code><\/pre>\n<p>The second pattern tests whether all bits above the low byte are zero:<\/p>\n<pre><code class=\"language-cpp\">#include &lt;arm_neon.h&gt;\r\n\r\nint32x4_t test_int32_t(int32x4_t a) {\r\n    return vceqq_s32(vandq_s32(a, vdupq_n_s32(-256)), vdupq_n_s32(0));\r\n}\r\n<\/code><\/pre>\n<p>The literal mask-and-compare implementation in MSVC 14.50:<\/p>\n<pre><code class=\"language-asm\">mvni v16.4s, #0xFF\r\nmovi v17.4s, #0\r\nand v16.16b, v0.16b, v16.16b\r\ncmeq v0.4s, v16.4s, v17.4s\r\n<\/code><\/pre>\n<p>is replaced in MSVC 14.51 with an unsigned comparison against 256:<\/p>\n<pre><code class=\"language-asm\">movi v16.4s, #1, lsl #8\r\ncmhi v0.4s, v16.4s, v0.4s\r\n<\/code><\/pre>\n<h3 id=\"arm64-neon-pairwise-add-and-accumulate-patterns\">ARM64 NEON pairwise add-and-accumulate patterns<\/h3>\n<p>The compiler recognizes a widening pairwise addition followed by accumulation:<\/p>\n<pre><code class=\"language-cpp\">#include &lt;arm_neon.h&gt;\r\n\r\nuint32x4_t test_uint32x4(uint32x4_t acc, uint16x8_t a) {\r\n    const auto pairs = vpaddlq_u16(a);\r\n    return vaddq_u32(acc, pairs);\r\n}\r\n<\/code><\/pre>\n<p>Instead of the two instructions emitted by MSVC 14.50:<\/p>\n<pre><code class=\"language-asm\">uaddlp v16.4s, v1.8h\r\nadd v0.4s, v0.4s, v16.4s\r\n<\/code><\/pre>\n<p>14.51 emits:<\/p>\n<pre><code class=\"language-asm\">uadalp v0.4s, v1.8h\r\n<\/code><\/pre>\n<p>The signed variant similarly uses <code>SADALP<\/code>.<\/p>\n<h3 id=\"slp-recognition-of-scalar-reductions\">SLP recognition of scalar reductions<\/h3>\n<p>In MSVC 14.51, superword-level parallelism (SLP) vectorization can recognize scalar reductions such as:<\/p>\n<pre><code class=\"language-cpp\">int reduce(const int* a) {\r\n    return a[0] + a[1] + a[2] + a[3];\r\n}\r\n<\/code><\/pre>\n<p>ARM64 has a horizontal sum instruction that makes this compact:<\/p>\n<pre><code class=\"language-asm\">ldr q16, [x0]\r\naddv s16, v16.4s\r\n<\/code><\/pre>\n<p>On x64, reducing vector lanes requires a larger sequence. The profitability\ndecision therefore also considers whether other vectorizable operations can\noffset the reduction cost.<\/p>\n<h3 id=\"slp-recognition-of-phi-nodes\">SLP recognition of phi nodes<\/h3>\n<p>In MSVC 14.52, SLP can vectorize phi nodes, extending its scope beyond straight-line\nbasic blocks to patterns involving loops. For example, consider two neighboring\naccumulations:<\/p>\n<pre><code class=\"language-cpp\">#include &lt;stdint.h&gt;\r\n\r\nvoid slp_phi(int64_t* dst, const int64_t* src, int n) {\r\n    int64_t a = 0;\r\n    int64_t b = 0;\r\n    for (int i = 0; i &lt; n; i += 2) {\r\n        a += src[i];\r\n        b += src[i + 1];\r\n    }\r\n    dst[0] = a;\r\n    dst[1] = b;\r\n}\r\n<\/code><\/pre>\n<p>These can be processed as two vector lanes. In static single assignment form, phi\nnodes merge the initial accumulator values with the values produced by previous\nloop iterations. Recognizing those nodes allows SLP to follow the accumulations\nthrough the loop, rather than stopping at the merge.<\/p>\n<p>The transformation can be illustrated with vectorized SSA pseudocode:<\/p>\n<pre><code class=\"language-text\">a_0 = broadcast(0)\r\nfor (i = 0; i &lt; n; i += 2):\r\n    a_1 = phi(a_0, a_2)\r\n    a_2 = a_1 + load(&amp;src[i])\r\na_3 = phi(a_0, a_2)\r\nstore(&amp;dst[0], a_3)\r\n<\/code><\/pre>\n<h3 id=\"recognizing-adjacent-data-through-address-arithmetic\">Recognizing adjacent data through address arithmetic<\/h3>\n<p>In MSVC 14.52, SLP can recognize more neighboring scalar loads and stores even when\ntheir addresses are written differently. A motivating example accessed pairs of\n<code>double<\/code> values using an index calculated at runtime:<\/p>\n<pre><code class=\"language-cpp\">int j = 3 * indices[k];\r\ndouble dx = xi - x[j];\r\ndouble dy = yi - x[j + 1];\r\ndouble r2 = dx * dx + dy * dy;\r\ndouble scale = 1.0 \/ (r2 * r2);\r\ndouble fx = scale * dx;\r\ndouble fy = scale * dy;\r\nf[j] -= fx;\r\nf[j + 1] -= fy;\r\n<\/code><\/pre>\n<p>To vectorize the neighboring operations, SLP must establish that their memory\naccesses are adjacent. For an eight-byte <code>double<\/code>, it can analyze the second\naddress as:<\/p>\n<pre><code class=\"language-text\">x + (j + 1) * 8  -&gt;  x + j * 8 + 8\r\n<\/code><\/pre>\n<p>Both accesses then have the same variable part, <code>x + j * 8<\/code>, with byte offsets\nof zero and eight.<\/p>\n<p>The address builder already supported this distribution when optimizing address\nexpressions. The change makes it available in analysis-only mode too, so SLP\ncan record the constant byte offset and prove adjacency without rewriting the\nintermediate representation. This applies to both the <code>x<\/code> loads and the <code>f<\/code>\nupdates.<\/p>\n<p>For example, updating the two force components separately on ARM64 requires two\nloads, two subtractions, and two stores:<\/p>\n<pre><code class=\"language-asm\">; x0 = &amp;f[j], d0 = fx, d1 = fy\r\nldr d2, [x0]\r\nldr d3, [x0, #8]\r\nfsub d2, d2, d0\r\nfsub d3, d3, d1\r\nstr d2, [x0]\r\nstr d3, [x0, #8]\r\n<\/code><\/pre>\n<p>With both components in a vector register, the update becomes:<\/p>\n<pre><code class=\"language-asm\">; x0 = &amp;f[j], v0.2d = {fx, fy}\r\nldr q2, [x0]\r\nfsub v2.2d, v2.2d, v0.2d\r\nstr q2, [x0]\r\n<\/code><\/pre>\n<h3 id=\"optimizing-explicit-neon-loads-and-stores\">Optimizing explicit NEON loads and stores<\/h3>\n<p>In MSVC 14.52, explicit NEON load and store intrinsics are canonicalized into\ngeneric memory operations. Previously, the compiler kept the specific intrinsics\nthroughout the optimizer, but they did not match existing optimization patterns\nthat expected normal loads and stores. Canonicalizing the NEON loads and stores\nallowed existing optimizations to apply to them. Ultimately, if the loads and\nstores persist after optimization, they may become NEON loads and stores again.<\/p>\n<h3 id=\"combining-scalar-stores-when-constructing-vectors\">Combining scalar stores when constructing vectors<\/h3>\n<p>MSVC 14.52 extends scalar-store combining to more data types and ARM64 NEON.\nWhen scalar assignments construct a vector that is subsequently loaded as a\nwhole, the compiler can build the value directly in registers rather than\nstoring each component to the stack and loading it back.<\/p>\n<p>For example:<\/p>\n<pre><code class=\"language-cpp\">#include &lt;arm_neon.h&gt;\r\n\r\nfloat32x4_t make_vector(float a, float b, float c, float d) {\r\n    float32x4_t result;\r\n    result.n128_f32[0] = a;\r\n    result.n128_f32[1] = b;\r\n    result.n128_f32[2] = c;\r\n    result.n128_f32[3] = d;\r\n    return result;\r\n}\r\n<\/code><\/pre>\n<p>The register-only construction can use lane inserts:<\/p>\n<pre><code class=\"language-asm\">ins v0.s[1], v1.s[0]\r\nins v0.s[2], v2.s[0]\r\nins v0.s[3], v3.s[0]\r\n<\/code><\/pre>\n<p>This small example already stayed in registers in MSVC 14.51. The improvement\nis in larger functions, where control flow and register pressure previously\nprevented the register allocator from eliminating the temporary stack storage.\nCombining the stores earlier also exposes the vector to later SIMD optimizations.<\/p>\n<h3 id=\"more-robust-bit-pattern-recognition\">More robust bit-pattern recognition<\/h3>\n<p>MSVC 14.52 improves the cost model for recognizing scalar bit-manipulation\npatterns when their inputs are defined in another basic block. For example,\nthis sequence reverses the bits of a 32-bit value:<\/p>\n<pre><code class=\"language-cpp\">#include &lt;intrin.h&gt;\r\n\r\nunsigned int reverse_bits(unsigned int x) {\r\n    x = ((x &amp; 0x55555555u) &lt;&lt; 1) | ((x &amp; 0xAAAAAAAAu) &gt;&gt; 1);\r\n    x = ((x &amp; 0x33333333u) &lt;&lt; 2) | ((x &amp; 0xCCCCCCCCu) &gt;&gt; 2);\r\n    x = ((x &amp; 0x0F0F0F0Fu) &lt;&lt; 4) | ((x &amp; 0xF0F0F0F0u) &gt;&gt; 4);\r\n    return _byteswap_ulong(x);\r\n}\r\n<\/code><\/pre>\n<p>On ARM64, the calculation can become:<\/p>\n<pre><code class=\"language-asm\">rbit w0, w0\r\n<\/code><\/pre>\n<p>The isolated pattern was already recognized. The new behavior makes recognition\nmore reliable inside large functions with more complicated control flow.<\/p>\n<h3 id=\"arm64-neon-arithmetic-with-a-selected-lane\">ARM64 NEON arithmetic with a selected lane<\/h3>\n<p>A vector multiplication by a broadcast lane no longer needs a separate\nduplicate instruction when the arithmetic instruction can select that lane\ndirectly:<\/p>\n<pre><code class=\"language-cpp\">#include &lt;arm_neon.h&gt;\r\n\r\nfloat32x4_t multiply_by_lane(float32x4_t a, float32x4_t b) {\r\n    return vmulq_f32(a, vdupq_laneq_f32(b, 1));\r\n}\r\n<\/code><\/pre>\n<p>MSVC 14.51 emitted:<\/p>\n<pre><code class=\"language-asm\">dup v16.4s, v1.s[1]\r\nfmul v0.4s, v0.4s, v16.4s\r\n<\/code><\/pre>\n<p>MSVC 14.52 folds the broadcast into the multiply:<\/p>\n<pre><code class=\"language-asm\">fmul v0.4s, v0.4s, v1.s[1]\r\n<\/code><\/pre>\n<p>The same recognition applies to fused multiply-add and multiply-subtract,\nusing the by-element forms of <code>FMLA<\/code> and <code>FMLS<\/code>. With <code>\/fp:fast<\/code>, the compiler\ncan also exchange the multiplicands to expose these forms when the broadcast\nappears on the other side of the multiplication.<\/p>\n<h3 id=\"arm64-neon-shifted-bit-clear-patterns\">ARM64 NEON shifted bit-clear patterns<\/h3>\n<p>The compiler can combine a mask and shift by moving the mask after the shift\nand expressing it as an immediate bit-clear operation:<\/p>\n<pre><code class=\"language-cpp\">#include &lt;arm_neon.h&gt;\r\n\r\nint32x4_t mask_and_shift(int32x4_t a) {\r\n    return vshlq_n_s32(vandq_s32(a, vdupq_n_s32(2)), 30);\r\n}\r\n<\/code><\/pre>\n<p>Previously, this required loading the mask, applying it, and shifting:<\/p>\n<pre><code class=\"language-asm\">movi v16.4s, #2\r\nand v16.16b, v0.16b, v16.16b\r\nshl v0.4s, v16.4s, #30\r\n<\/code><\/pre>\n<p>MSVC 14.52 instead emits:<\/p>\n<pre><code class=\"language-asm\">shl v0.4s, v0.4s, #30\r\nbic v0.4s, #0x40, lsl #24\r\n<\/code><\/pre>\n<p>The shifted result can contain only bits 30 and 31. Clearing bit 30 leaves\nexactly the bit selected by the original mask, without materializing a vector\nconstant.<\/p>\n<h3 id=\"removing-masks-made-redundant-by-a-shift\">Removing masks made redundant by a shift<\/h3>\n<p>In related cases, the mask can disappear entirely:<\/p>\n<pre><code class=\"language-cpp\">#include &lt;arm_neon.h&gt;\r\n\r\nint32x4_t move_low_bit(int32x4_t a) {\r\n    return vshlq_n_s32(vandq_s32(a, vdupq_n_s32(1)), 31);\r\n}\r\n<\/code><\/pre>\n<p>Only the original low bit survives a 31-bit left shift, so masking off the\nother bits beforehand does not affect the result. Instead of a <code>MOVI<\/code>, <code>AND<\/code>,\nand <code>SHL<\/code>, MSVC 14.52 needs only:<\/p>\n<pre><code class=\"language-asm\">shl v0.4s, v0.4s, #31\r\n<\/code><\/pre>\n<h3 id=\"removing-work-in-unused-neon-lanes\">Removing work in unused NEON lanes<\/h3>\n<p>MSVC 14.52 tracks which vector lanes are observed and can remove instructions\nthat define only unused lanes. For example, if code inserts zero into lane 3\nbefore an integer vector addition, but subsequently consumes only lanes 0\nthrough 2, the insertion is unnecessary:<\/p>\n<pre><code class=\"language-asm\">; Before\r\nins v0.s[3], wzr\r\nadd v16.4s, v0.4s, v1.4s\r\n\r\n; After\r\nadd v16.4s, v0.4s, v1.4s\r\n<\/code><\/pre>\n<p>This requires that the remaining operations can safely compute values in the\nunused lanes.<\/p>\n<p>The compiler also takes advantage of scalar floating-point and NEON values\nsharing the same physical registers. A broadcast is unnecessary if only the\noriginal low lane is used:<\/p>\n<pre><code class=\"language-cpp\">#include &lt;arm_neon.h&gt;\r\n\r\nfloat add_low_lane(float a, float b) {\r\n    float32x4_t broadcast = vdupq_n_f32(a);\r\n    return vgetq_lane_f32(broadcast, 0) + b;\r\n}\r\n<\/code><\/pre>\n<p>The previous <code>DUP<\/code> followed by a scalar addition becomes simply:<\/p>\n<pre><code class=\"language-asm\">fadd s0, s0, s1\r\n<\/code><\/pre>\n<h3 id=\"arm64-neon-per-lane-rotations\">ARM64 NEON per-lane rotations<\/h3>\n<p>Two complementary shifts combined with a bitwise OR express a rotation within\neach vector lane:<\/p>\n<pre><code class=\"language-cpp\">#include &lt;arm_neon.h&gt;\r\n\r\nuint32x4_t rotate_lanes(uint32x4_t unused, uint32x4_t a) {\r\n    return vorrq_u32(vshrq_n_u32(a, 18), vshlq_n_u32(a, 14));\r\n}\r\n<\/code><\/pre>\n<p>The unused parameter puts <code>a<\/code> in <code>v1<\/code>, leaving the return register available\nfor an instruction that updates its destination. Previously, the calculation\nused two shifts and an OR:<\/p>\n<pre><code class=\"language-asm\">shl v17.4s, v1.4s, #14\r\nushr v16.4s, v1.4s, #18\r\norr v0.16b, v16.16b, v17.16b\r\n<\/code><\/pre>\n<p>MSVC 14.52 can use unsigned shift-right-and-accumulate:<\/p>\n<pre><code class=\"language-asm\">shl v0.4s, v1.4s, #14\r\nusra v0.4s, v1.4s, #18\r\n<\/code><\/pre>\n<p>The two shifted values occupy disjoint bits, so addition gives the same result\nas OR. When both shifts are 16, swapping the two halfwords in each 32-bit lane\nperforms the whole rotation with one instruction:<\/p>\n<pre><code class=\"language-asm\">rev32 v0.8h, v1.8h\r\n<\/code><\/pre>\n<h2 id=\"inlining-improvements\">Inlining Improvements<\/h2>\n<p>Inlining removes call overhead, but its larger benefit is often exposing\noptimizations across a function boundary. MSVC 14.52 improves several decisions\nthat previously left useful helper functions out of line. The examples below\nillustrate the opportunities; they are not minimal reproductions of a missed\ninline, and small versions may already inline with earlier compilers.<\/p>\n<h3 id=\"single-call-site-helpers-inside-force-inline-functions\">Single-call-site helpers inside force-inline functions<\/h3>\n<p>The inliner can give special consideration to eligible functions with a single\ncall site. Previously, this analysis was disabled while optimizing a\n<code>__forceinline<\/code> root function, even when its helper was explicitly marked\n<code>inline<\/code>.<\/p>\n<p>Consider this call structure:<\/p>\n<pre><code class=\"language-cpp\">static inline unsigned mix(unsigned x) {\r\n    x ^= x &gt;&gt; 16;\r\n    x *= 0x7FEB352Du;\r\n    x ^= x &gt;&gt; 15;\r\n    return x;\r\n}\r\n\r\n__forceinline unsigned bucket(unsigned x) {\r\n    return mix(x) &amp; 255u;\r\n}\r\n\r\nunsigned lookup_bucket(unsigned x) {\r\n    return bucket(x);\r\n}\r\n<\/code><\/pre>\n<p>The <code>__forceinline<\/code> on <code>bucket<\/code> does not itself request that <code>mix<\/code> be inlined.\nThe improvement allows the single-call-site heuristic to consider a\nuser-marked <code>inline<\/code> helper such as <code>mix<\/code> while optimizing <code>bucket<\/code>.\nThe result can expose the helper&#8217;s arithmetic together with its caller&#8217;s\nmasking and other surrounding work.<\/p>\n<h3 id=\"keeping-slightly-larger-small-helpers-inline\">Keeping slightly larger small helpers inline<\/h3>\n<p>MSVC 14.52 raises the small-function allowance from 40 to 50 instructions. The\nmotivating NEON shuffle helpers were roughly 48 nodes, just outside the old\nallowance.<\/p>\n<p>For example, a SIMD abstraction can wrap lane selection in a template:<\/p>\n<pre><code class=\"language-cpp\">#include &lt;arm_neon.h&gt;\r\n\r\ntemplate&lt;int Lane&gt;\r\ninline float32x4_t broadcast_lane(float32x4_t value) {\r\n    return vdupq_laneq_f32(value, Lane);\r\n}\r\n\r\nfloat32x4_t scale_vector(float32x4_t a, float32x4_t b) {\r\n    return vmulq_f32(a, broadcast_lane&lt;1&gt;(b));\r\n}\r\n<\/code><\/pre>\n<p>Keeping such wrappers inline exposes the constant lane selection to the SIMD\noptimizer, allowing opportunities such as the by-element multiply described\nabove. The example is deliberately small; the allowance change matters for\nlarger wrappers whose intermediate representation falls between the old and\nnew limits. These are compiler-internal size estimates, not source-line counts\nor a new unconditional inlining guarantee.<\/p>\n<h3 id=\"accounting-for-scalar-replacement-of-local-aggregates\">Accounting for scalar replacement of local aggregates<\/h3>\n<p>The inliner now recognizes more calls whose removal can enable scalar\nreplacement of aggregates (SROA) and dead-store elimination. Passing the address\nof a local struct to a function forces it to have a memory location, such\nthat the entire struct is written to memory in the caller. The called function\nmight use only a few fields of the struct. Inlining exposes the fields that\nare actually needed, allowing the optimizer to keep those useful values in\nregisters and skip copying the struct to memory.<\/p>\n<p>For example:<\/p>\n<pre><code class=\"language-cpp\">#include &lt;stdint.h&gt;\r\n\r\nstruct Values {\r\n    uint64_t a, b, c, d;\r\n};\r\n\r\nvoid adjust(Values* values, uint64_t delta) {\r\n    values-&gt;a += delta;\r\n    values-&gt;b += delta;\r\n    values-&gt;c += delta;\r\n    values-&gt;d += delta;\r\n}\r\n\r\nuint64_t calculate(\r\n    uint64_t a, uint64_t c, uint64_t delta) {\r\n    Values values = {a, 0, c, 0};\r\n    adjust(&amp;values, delta);\r\n    return values.a + values.c;\r\n}\r\n<\/code><\/pre>\n<p>Once <code>adjust<\/code> is inlined, the struct and the updates to its unused <code>b<\/code> and\n<code>d<\/code> fields can disappear. The useful calculation is equivalent to:<\/p>\n<pre><code class=\"language-cpp\">return (a + delta) + (c + delta);\r\n<\/code><\/pre>\n<p>The new heuristic gives a bonus to eligible calls that the normal\nheuristic would otherwise decline. It combines a summary of the\ncallee&#8217;s fixed-offset pointer accesses with caller-side evidence that the\nargument is a suitable local aggregate. Pointer escapes, variable-offset\naccesses, and other cases the analysis cannot safely characterize do not\nreceive this benefit.<\/p>\n<h2 id=\"pattern-based-optimizations\">Pattern-based Optimizations<\/h2>\n<p>MSVC 14.52 recognizes additional profitable arithmetic, selection, comparison,\nand bitwise-expression patterns. The examples in this section use simplified\nx64 assembly for optimized builds with <code>\/O2<\/code>; the reciprocal transformation\nadditionally requires <code>\/fp:fast<\/code>.<\/p>\n<h3 id=\"sharing-a-reciprocal-across-repeated-divisions\">Sharing a reciprocal across repeated divisions<\/h3>\n<p>Several divisions by the same floating-point value can share a reciprocal:<\/p>\n<pre><code class=\"language-cpp\">void divide_by_common_value(\r\n    double a, double b, double c, double divisor, double* out) {\r\n    out[0] = a \/ divisor;\r\n    out[1] = b \/ divisor;\r\n    out[2] = c \/ divisor;\r\n}\r\n<\/code><\/pre>\n<p>Instead of three separate divisions:<\/p>\n<pre><code class=\"language-asm\">divsd xmm0, xmm3\r\ndivsd xmm1, xmm3\r\ndivsd xmm2, xmm3\r\n<\/code><\/pre>\n<p>the compiler can perform one division followed by three multiplications:<\/p>\n<pre><code class=\"language-asm\">movsd xmm4, QWORD PTR [one]\r\ndivsd xmm4, xmm3\r\nmulsd xmm0, xmm4\r\nmulsd xmm1, xmm4\r\nmulsd xmm2, xmm4\r\n<\/code><\/pre>\n<p>This transformation requires <code>\/fp:fast<\/code>. It is cost-based rather than\nunconditional: with only a few uses, direct division may be preferable because\nintroducing a reciprocal adds a temporary and can lengthen live ranges.<\/p>\n<h3 id=\"more-branchless-selections-on-x64\">More branchless selections on x64<\/h3>\n<p>The x64 if-conversion profitability decision now accepts nonzero integer\nconstants in a pattern already supported on ARM64. A small selection such as:<\/p>\n<pre><code class=\"language-cpp\">int select_value(int a, int b) {\r\n    return a &lt; b ? a + b : 7;\r\n}\r\n<\/code><\/pre>\n<p>might previously use a branch:<\/p>\n<pre><code class=\"language-asm\">lea eax, DWORD PTR [rcx+rdx]\r\ncmp ecx, edx\r\njl selected_sum\r\nmov eax, 7\r\nselected_sum:\r\n<\/code><\/pre>\n<p>In MSVC 14.52, it instead can select between register values with a conditional move:<\/p>\n<pre><code class=\"language-asm\">cmp ecx, edx\r\nlea eax, DWORD PTR [rcx+rdx]\r\nmov r8d, 7\r\ncmovge eax, r8d\r\n<\/code><\/pre>\n<p>This removes the control-flow branch and its potential misprediction cost.<\/p>\n<h3 id=\"collapsing-boolean-expressions-to-xor\">Collapsing Boolean expressions to XOR<\/h3>\n<p>Some multi-operation Boolean expressions compute a simple exclusive OR:<\/p>\n<pre><code class=\"language-cpp\">unsigned symmetric_difference(unsigned a, unsigned b) {\r\n    return (a | b) &amp; ~(a &amp; b);\r\n}\r\n<\/code><\/pre>\n<p>A literal implementation might use:<\/p>\n<pre><code class=\"language-asm\">mov eax, ecx\r\nor ecx, edx\r\nand eax, edx\r\nnot eax\r\nand eax, ecx\r\n<\/code><\/pre>\n<p>Recognizing the identity reduces the sequence to:<\/p>\n<pre><code class=\"language-asm\">xor ecx, edx\r\nmov eax, ecx\r\n<\/code><\/pre>\n<p>Related forms, including XOR combinations of the AND and OR of the same\noperands, can simplify in the same way.<\/p>\n<h3 id=\"reusing-equivalent-reversed-comparisons\">Reusing equivalent reversed comparisons<\/h3>\n<p>Reversing both comparison operands and the condition can express the same test.\nRecognizing that equivalence avoids computing identical Boolean values\nindependently:<\/p>\n<pre><code class=\"language-cpp\">int equivalent_compare_difference(int a, int b) {\r\n    const int first = a &gt;= b ? 1 : 0;\r\n    const int second = b &lt;= a ? 1 : 0;\r\n    return first - second;\r\n}\r\n<\/code><\/pre>\n<p>Instead of:<\/p>\n<pre><code class=\"language-asm\">xor r8d, r8d\r\ncmp edx, ecx\r\nsetle r8b\r\nxor eax, eax\r\ncmp ecx, edx\r\nsetge al\r\nsub eax, r8d\r\n<\/code><\/pre>\n<p>the entire expression becomes:<\/p>\n<pre><code class=\"language-asm\">xor eax, eax\r\n<\/code><\/pre>\n<p>Canonicalizing the comparison both enables reuse and exposes the constant result of subtracting equal values.<\/p>\n<h3 id=\"recognizing-equivalent-minimum-and-maximum-selections\">Recognizing equivalent minimum and maximum selections<\/h3>\n<p>Two differently written selections can also compute the same minimum or maximum:<\/p>\n<pre><code class=\"language-cpp\">int equivalent_max_difference(int a, int b) {\r\n    const int first = a &gt; b ? a : b;\r\n    const int second = b &gt; a ? b : a;\r\n    return first - second;\r\n}\r\n<\/code><\/pre>\n<p>Without recognizing their equivalence, the compiler might emit:<\/p>\n<pre><code class=\"language-asm\">cmp edx, ecx\r\nmov eax, ecx\r\ncmovg eax, edx\r\ncmovl edx, ecx\r\nsub edx, eax\r\nmov eax, edx\r\n<\/code><\/pre>\n<p>Both selections return the same value, so the optimized result is simply:<\/p>\n<pre><code class=\"language-asm\">xor eax, eax\r\n<\/code><\/pre>\n<p>The recognition covers signed minimum and maximum forms with strict or\nnon-strict comparisons, including equivalent results combined with XOR. Besides\nsaving instructions directly, these simplifications reduce temporary values and\ncan expose further opportunities in later optimization passes.<\/p>\n<h2 id=\"conclusion\">Conclusion<\/h2>\n<p>The Microsoft C++ team released many new optimizations in MSVC Build Tools\n14.52. We will continue focusing on performance while developing MSVC 14.53.\nMany thanks to the compiler engineers who implemented the optimizations and\nhelped write sections of this blog post, including Alex Wong, Aman Arora,\nChris Pulido, Reka Kovacs, and Swaroop Sridhar.<\/p>\n<p>MSVC Build Tools 14.52 is available as part of <a href=\"https:\/\/visualstudio.microsoft.com\/downloads\/\">Visual Studio 2026<\/a>.\nTry it out today and share your feedback on <a href=\"https:\/\/developercommunity.visualstudio.com\/cpp\">Visual Studio Developer Community<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>C++ Optimizations in MSVC Build Tools 14.52 The Microsoft C++ team implemented new optimizations in MSVC Build Tools 14.52. Let&#8217;s walk through examples of some of these optimizations. We will focus on reassociation, loop optimizations, SIMD code generation, inlining, and patterns. Where noted, we&#8217;ll also include a few optimizations that appeared in MSVC 14.51 but [&hellip;]<\/p>\n","protected":false},"author":129662,"featured_media":35994,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1,218],"tags":[],"class_list":["post-37093","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-cplusplus","category-performance"],"acf":[],"blog_post_summary":"<p>C++ Optimizations in MSVC Build Tools 14.52 The Microsoft C++ team implemented new optimizations in MSVC Build Tools 14.52. Let&#8217;s walk through examples of some of these optimizations. We will focus on reassociation, loop optimizations, SIMD code generation, inlining, and patterns. Where noted, we&#8217;ll also include a few optimizations that appeared in MSVC 14.51 but [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/posts\/37093","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/users\/129662"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/comments?post=37093"}],"version-history":[{"count":1,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/posts\/37093\/revisions"}],"predecessor-version":[{"id":37101,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/posts\/37093\/revisions\/37101"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/media\/35994"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/media?parent=37093"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/categories?post=37093"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/tags?post=37093"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}