← Back to board

PR Detail

#206717
[InstCombine]Fold bitcast shuffle ashr v2i64
InstCombine

Description

This patch folds the X86 pre-AVX512 64-bit vector arithmetic shift emulation pattern (bitcast(shuffle(ashr(bitcast(X))))) into a single ashr instruction. The transformation is implemented in InstCombine, the middle-end optimization pass, so it benefits all backends (X86, ARM, RISC-V, etc.) by cleaning up the IR before it reaches any target-specific lowering.

Alive2 proof: https://alive2.llvm.org/ce/z/LM1Eu1

Fixes: #125189

Commit Versions & Reviews

Commit #1reviewing325d0ae725on base8e61b6a20f
Buggy12 bugs (12 patch-specific)565s · 1,948,609 tok · 29 rounds

Analysis & Findings

Bug Triggering Analysis

The new InstCombine fold assumes that bitcast <1 x i64> %x to <2 x i32> maps element 1 to the high/sign-containing half of the i64 lane. That is only true for little-endian layouts. LLVM LangRef defines vector bitcasts as endian-sensitive: on big-endian, element 0 contains the high bits and element 1 contains the low bits.

With the big-endian datalayout above, for %x = <i64 0x0000000080000000>:

  • Source: bitcast yields <i32 0, i32 0x80000000>; ashr by <0,31> yields <0,-1>; shuffle <1,1> yields <-1,-1>; bitcast back yields <i64 -1>.
  • Optimized: InstCombine folds to ashr <1 x i64> %x, splat(63), which yields <i64 0>.

Alive2 reports this exact value mismatch, so the transformation is unsound.

Fix Weakness Analysis

The patch structurally validates the shift vector and shuffle mask but does not validate the datalayout endianness. The recognized mask/shift pattern is the little-endian X86 emulation pattern; applying it on big-endian targets selects the low half rather than the high half. The same issue also applies to multi-lane <N x i64> and to the generalized non-i64 widths accepted by the implementation, such as <1 x i32> via <2 x i16>. The fold should either be restricted to little-endian datalayouts or use an endian-aware mask/shift interpretation.

Bugs (12)

BUG #1Patch-specific
Original IR
target datalayout = "E-m:e-p:64:64-i64:64-n32:64"

define <2 x i64> @test(<2 x i64> %x) {
  %narrow = bitcast <2 x i64> %x to <4 x i32>
  %shifted = ashr <4 x i32> %narrow, <i32 0, i32 31, i32 0, i32 31>
  %shuf = shufflevector <4 x i32> %shifted, <4 x i32> poison, <4 x i32> <i32 1, i32 1, i32 3, i32 3>
  %wide = bitcast <4 x i32> %shuf to <2 x i64>
  ret <2 x i64> %wide
}
Transformed IR
; ModuleID = '/data/tmp/tmpzo91t4b6/orig.ll'
source_filename = "/data/tmp/tmpzo91t4b6/orig.ll"
target datalayout = "E-m:e-p:64:64-i64:64-n32:64"

define <2 x i64> @test(<2 x i64> %x) {
  %wide = ashr <2 x i64> %x, splat (i64 63)
  ret <2 x i64> %wide
}
Output Log
----------------------------------------
define <2 x i64> @test(<2 x i64> %x) {
#0:
  %narrow = bitcast <2 x i64> %x to <4 x i32>
  %shifted = ashr <4 x i32> %narrow, { 0, 31, 0, 31 }
  %shuf = shufflevector <4 x i32> %shifted, <4 x i32> poison, 1, 1, 3, 3
  %wide = bitcast <4 x i32> %shuf to <2 x i64>
  ret <2 x i64> %wide
}
=>
define <2 x i64> @test(<2 x i64> %x) {
#0:
  %wide = ashr <2 x i64> %x, { 63, 63 }
  ret <2 x i64> %wide
}
Transformation doesn't verify!

ERROR: Value mismatch

Example:
<2 x i64> %x = < #x0000000000000000 (0), #x0000000080000000 (2147483648) >

Source:
<4 x i32> %narrow = < #x00000000 (0), #x00000000 (0), #x00000000 (0), #x80000000 (2147483648, -2147483648) >
<4 x i32> %shifted = < #x00000000 (0), #x00000000 (0), #x00000000 (0), #xffffffff (4294967295, -1) >
<4 x i32> %shuf = < #x00000000 (0), #x00000000 (0), #xffffffff (4294967295, -1), #xffffffff (4294967295, -1) >
<2 x i64> %wide = < #x0000000000000000 (0), #xffffffffffffffff (18446744073709551615, -1) >

Target:
<2 x i64> %wide = < #x0000000000000000 (0), #x0000000000000000 (0) >
Source value: < #x0000000000000000 (0), #xffffffffffffffff (18446744073709551615, -1) >
Target value: < #x0000000000000000 (0), #x0000000000000000 (0) >

Summary:
  0 correct transformations
  1 incorrect transformations
  0 failed-to-prove transformations
  0 Alive2 errors
BUG #2Patch-specific
Original IR
target datalayout = "E-m:e-p:64:64-i64:64-n32:64"

define <1 x i64> @be_v1_low_sign(<1 x i64> %x) {
  %narrow = bitcast <1 x i64> %x to <2 x i32>
  %shifted = ashr <2 x i32> %narrow, <i32 0, i32 31>
  %shuf = shufflevector <2 x i32> %shifted, <2 x i32> poison, <2 x i32> <i32 1, i32 1>
  %wide = bitcast <2 x i32> %shuf to <1 x i64>
  ret <1 x i64> %wide
}
Transformed IR
; ModuleID = '/data/tmp/tmpzo6h_adv/orig.ll'
source_filename = "/data/tmp/tmpzo6h_adv/orig.ll"
target datalayout = "E-m:e-p:64:64-i64:64-n32:64"

define <1 x i64> @be_v1_low_sign(<1 x i64> %x) {
  %wide = ashr <1 x i64> %x, splat (i64 63)
  ret <1 x i64> %wide
}
Output Log
----------------------------------------
define <1 x i64> @be_v1_low_sign(<1 x i64> %x) {
#0:
  %narrow = bitcast <1 x i64> %x to <2 x i32>
  %shifted = ashr <2 x i32> %narrow, { 0, 31 }
  %shuf = shufflevector <2 x i32> %shifted, <2 x i32> poison, 1, 1
  %wide = bitcast <2 x i32> %shuf to <1 x i64>
  ret <1 x i64> %wide
}
=>
define <1 x i64> @be_v1_low_sign(<1 x i64> %x) {
#0:
  %wide = ashr <1 x i64> %x, { 63 }
  ret <1 x i64> %wide
}
Transformation doesn't verify!

ERROR: Value mismatch

Example:
<1 x i64> %x = < #x0000000080000000 (2147483648) >

Source:
<2 x i32> %narrow = < #x00000000 (0), #x80000000 (2147483648, -2147483648) >
<2 x i32> %shifted = < #x00000000 (0), #xffffffff (4294967295, -1) >
<2 x i32> %shuf = < #xffffffff (4294967295, -1), #xffffffff (4294967295, -1) >
<1 x i64> %wide = < #xffffffffffffffff (18446744073709551615, -1) >

Target:
<1 x i64> %wide = < #x0000000000000000 (0) >
Source value: < #xffffffffffffffff (18446744073709551615, -1) >
Target value: < #x0000000000000000 (0) >

Summary:
  0 correct transformations
  1 incorrect transformations
  0 failed-to-prove transformations
  0 Alive2 errors
BUG #3Patch-specific
Original IR
target datalayout = "E-m:e-p:64:64-i64:64-n32:64"

define <1 x i64> @be_v1_high_sign(<1 x i64> %x) {
  %narrow = bitcast <1 x i64> %x to <2 x i32>
  %shifted = ashr <2 x i32> %narrow, <i32 0, i32 31>
  %shuf = shufflevector <2 x i32> %shifted, <2 x i32> poison, <2 x i32> <i32 1, i32 1>
  %wide = bitcast <2 x i32> %shuf to <1 x i64>
  ret <1 x i64> %wide
}
Transformed IR
; ModuleID = '/data/tmp/tmp7j0jctq3/orig.ll'
source_filename = "/data/tmp/tmp7j0jctq3/orig.ll"
target datalayout = "E-m:e-p:64:64-i64:64-n32:64"

define <1 x i64> @be_v1_high_sign(<1 x i64> %x) {
  %wide = ashr <1 x i64> %x, splat (i64 63)
  ret <1 x i64> %wide
}
Output Log
----------------------------------------
define <1 x i64> @be_v1_high_sign(<1 x i64> %x) {
#0:
  %narrow = bitcast <1 x i64> %x to <2 x i32>
  %shifted = ashr <2 x i32> %narrow, { 0, 31 }
  %shuf = shufflevector <2 x i32> %shifted, <2 x i32> poison, 1, 1
  %wide = bitcast <2 x i32> %shuf to <1 x i64>
  ret <1 x i64> %wide
}
=>
define <1 x i64> @be_v1_high_sign(<1 x i64> %x) {
#0:
  %wide = ashr <1 x i64> %x, { 63 }
  ret <1 x i64> %wide
}
Transformation doesn't verify!

ERROR: Value mismatch

Example:
<1 x i64> %x = < #x0000000080000000 (2147483648) >

Source:
<2 x i32> %narrow = < #x00000000 (0), #x80000000 (2147483648, -2147483648) >
<2 x i32> %shifted = < #x00000000 (0), #xffffffff (4294967295, -1) >
<2 x i32> %shuf = < #xffffffff (4294967295, -1), #xffffffff (4294967295, -1) >
<1 x i64> %wide = < #xffffffffffffffff (18446744073709551615, -1) >

Target:
<1 x i64> %wide = < #x0000000000000000 (0) >
Source value: < #xffffffffffffffff (18446744073709551615, -1) >
Target value: < #x0000000000000000 (0) >

Summary:
  0 correct transformations
  1 incorrect transformations
  0 failed-to-prove transformations
  0 Alive2 errors
BUG #4Patch-specific
Original IR
target datalayout = "E-m:e-p:64:64-i64:64-n32:64"

define i64 @be_v1_extract(<1 x i64> %x) {
  %narrow = bitcast <1 x i64> %x to <2 x i32>
  %shifted = ashr <2 x i32> %narrow, <i32 0, i32 31>
  %shuf = shufflevector <2 x i32> %shifted, <2 x i32> poison, <2 x i32> <i32 1, i32 1>
  %wide = bitcast <2 x i32> %shuf to <1 x i64>
  %r = extractelement <1 x i64> %wide, i32 0
  ret i64 %r
}
Transformed IR
; ModuleID = '/data/tmp/tmpteoei864/orig.ll'
source_filename = "/data/tmp/tmpteoei864/orig.ll"
target datalayout = "E-m:e-p:64:64-i64:64-n32:64"

define i64 @be_v1_extract(<1 x i64> %x) {
  %1 = extractelement <1 x i64> %x, i64 0
  %r = ashr i64 %1, 63
  ret i64 %r
}
Output Log
----------------------------------------
define i64 @be_v1_extract(<1 x i64> %x) {
#0:
  %narrow = bitcast <1 x i64> %x to <2 x i32>
  %shifted = ashr <2 x i32> %narrow, { 0, 31 }
  %shuf = shufflevector <2 x i32> %shifted, <2 x i32> poison, 1, 1
  %wide = bitcast <2 x i32> %shuf to <1 x i64>
  %r = extractelement <1 x i64> %wide, i32 0
  ret i64 %r
}
=>
define i64 @be_v1_extract(<1 x i64> %x) {
#0:
  %#1 = extractelement <1 x i64> %x, i64 0
  %r = ashr i64 %#1, 63
  ret i64 %r
}
Transformation doesn't verify!

ERROR: Value mismatch

Example:
<1 x i64> %x = < #x0000000080000000 (2147483648) >

Source:
<2 x i32> %narrow = < #x00000000 (0), #x80000000 (2147483648, -2147483648) >
<2 x i32> %shifted = < #x00000000 (0), #xffffffff (4294967295, -1) >
<2 x i32> %shuf = < #xffffffff (4294967295, -1), #xffffffff (4294967295, -1) >
<1 x i64> %wide = < #xffffffffffffffff (18446744073709551615, -1) >
i64 %r = #xffffffffffffffff (18446744073709551615, -1)

Target:
i64 %#1 = #x0000000080000000 (2147483648)
i64 %r = #x0000000000000000 (0)
Source value: #xffffffffffffffff (18446744073709551615, -1)
Target value: #x0000000000000000 (0)

Summary:
  0 correct transformations
  1 incorrect transformations
  0 failed-to-prove transformations
  0 Alive2 errors
BUG #5Patch-specific
Original IR
target datalayout = "E-m:e-p:64:64-i64:64-n32:64"

define <1 x i32> @be_i32_i16(<1 x i32> %x) {
  %narrow = bitcast <1 x i32> %x to <2 x i16>
  %shifted = ashr <2 x i16> %narrow, <i16 0, i16 15>
  %shuf = shufflevector <2 x i16> %shifted, <2 x i16> poison, <2 x i32> <i32 1, i32 1>
  %wide = bitcast <2 x i16> %shuf to <1 x i32>
  ret <1 x i32> %wide
}
Transformed IR
; ModuleID = '/data/tmp/tmpf3w2fxmv/orig.ll'
source_filename = "/data/tmp/tmpf3w2fxmv/orig.ll"
target datalayout = "E-m:e-p:64:64-i64:64-n32:64"

define <1 x i32> @be_i32_i16(<1 x i32> %x) {
  %wide = ashr <1 x i32> %x, splat (i32 31)
  ret <1 x i32> %wide
}
Output Log
----------------------------------------
define <1 x i32> @be_i32_i16(<1 x i32> %x) {
#0:
  %narrow = bitcast <1 x i32> %x to <2 x i16>
  %shifted = ashr <2 x i16> %narrow, { 0, 15 }
  %shuf = shufflevector <2 x i16> %shifted, <2 x i16> poison, 1, 1
  %wide = bitcast <2 x i16> %shuf to <1 x i32>
  ret <1 x i32> %wide
}
=>
define <1 x i32> @be_i32_i16(<1 x i32> %x) {
#0:
  %wide = ashr <1 x i32> %x, { 31 }
  ret <1 x i32> %wide
}
Transformation doesn't verify!

ERROR: Value mismatch

Example:
<1 x i32> %x = < #x00008000 (32768) >

Source:
<2 x i16> %narrow = < #x0000 (0), #x8000 (32768, -32768) >
<2 x i16> %shifted = < #x0000 (0), #xffff (65535, -1) >
<2 x i16> %shuf = < #xffff (65535, -1), #xffff (65535, -1) >
<1 x i32> %wide = < #xffffffff (4294967295, -1) >

Target:
<1 x i32> %wide = < #x00000000 (0) >
Source value: < #xffffffff (4294967295, -1) >
Target value: < #x00000000 (0) >

Summary:
  0 correct transformations
  1 incorrect transformations
  0 failed-to-prove transformations
  0 Alive2 errors
BUG #6Patch-specific
Original IR
target datalayout = "E-m:e-p:64:64-i64:64-n32:64"
declare void @llvm.assume(i1)

define <1 x i64> @be_v1_high_sign_assume(<1 x i64> %x) {
  %e = extractelement <1 x i64> %x, i32 0
  %c = icmp eq i64 %e, -9223372036854775808
  call void @llvm.assume(i1 %c)
  %narrow = bitcast <1 x i64> %x to <2 x i32>
  %shifted = ashr <2 x i32> %narrow, <i32 0, i32 31>
  %shuf = shufflevector <2 x i32> %shifted, <2 x i32> poison, <2 x i32> <i32 1, i32 1>
  %wide = bitcast <2 x i32> %shuf to <1 x i64>
  ret <1 x i64> %wide
}
Transformed IR
; ModuleID = '/data/tmp/tmp0jmdm85c/orig.ll'
source_filename = "/data/tmp/tmp0jmdm85c/orig.ll"
target datalayout = "E-m:e-p:64:64-i64:64-n32:64"

; Function Attrs: nocallback nofree nosync nounwind willreturn memory(inaccessiblemem: write)
declare void @llvm.assume(i1 noundef) #0

define <1 x i64> @be_v1_high_sign_assume(<1 x i64> %x) {
  %e = extractelement <1 x i64> %x, i64 0
  %c = icmp eq i64 %e, -9223372036854775808
  call void @llvm.assume(i1 %c)
  %wide = ashr <1 x i64> %x, splat (i64 63)
  ret <1 x i64> %wide
}

attributes #0 = { nocallback nofree nosync nounwind willreturn memory(inaccessiblemem: write) }
Output Log
----------------------------------------
define <1 x i64> @be_v1_high_sign_assume(<1 x i64> %x) {
#0:
  %e = extractelement <1 x i64> %x, i32 0
  %c = icmp eq i64 %e, -9223372036854775808
  assume i1 %c
  %narrow = bitcast <1 x i64> %x to <2 x i32>
  %shifted = ashr <2 x i32> %narrow, { 0, 31 }
  %shuf = shufflevector <2 x i32> %shifted, <2 x i32> poison, 1, 1
  %wide = bitcast <2 x i32> %shuf to <1 x i64>
  ret <1 x i64> %wide
}
=>
define <1 x i64> @be_v1_high_sign_assume(<1 x i64> %x) {
#0:
  %e = extractelement <1 x i64> %x, i64 0
  %c = icmp eq i64 %e, -9223372036854775808
  assume i1 %c
  %wide = ashr <1 x i64> %x, { 63 }
  ret <1 x i64> %wide
}
Transformation doesn't verify!

ERROR: Value mismatch

NOTE: The counterexample is unique.

Example:
<1 x i64> %x = < #x8000000000000000 (9223372036854775808, -9223372036854775808) >

Source:
i64 %e = #x8000000000000000 (9223372036854775808, -9223372036854775808)
i1 %c = #x1 (1)
<2 x i32> %narrow = < #x80000000 (2147483648, -2147483648), #x00000000 (0) >
<2 x i32> %shifted = < #x80000000 (2147483648, -2147483648), #x00000000 (0) >
<2 x i32> %shuf = < #x00000000 (0), #x00000000 (0) >
<1 x i64> %wide = < #x0000000000000000 (0) >

Target:
i64 %e = #x8000000000000000 (9223372036854775808, -9223372036854775808)
i1 %c = #x1 (1)
<1 x i64> %wide = < #xffffffffffffffff (18446744073709551615, -1) >
Source value: < #x0000000000000000 (0) >
Target value: < #xffffffffffffffff (18446744073709551615, -1) >

Summary:
  0 correct transformations
  1 incorrect transformations
  0 failed-to-prove transformations
  0 Alive2 errors
BUG #7Patch-specific
Original IR
target datalayout = "E-m:e-i64:64-n32:64"

define <2 x i64> @be_counter(<2 x i64> %x) {
  %narrow = bitcast <2 x i64> %x to <4 x i32>
  %shifted = ashr <4 x i32> %narrow, <i32 0, i32 31, i32 0, i32 31>
  %shuf = shufflevector <4 x i32> %shifted, <4 x i32> poison, <4 x i32> <i32 1, i32 1, i32 3, i32 3>
  %wide = bitcast <4 x i32> %shuf to <2 x i64>
  ret <2 x i64> %wide
}
Transformed IR
; ModuleID = '/data/tmp/tmpr79gciv1/orig.ll'
source_filename = "/data/tmp/tmpr79gciv1/orig.ll"
target datalayout = "E-m:e-i64:64-n32:64"

define <2 x i64> @be_counter(<2 x i64> %x) {
  %wide = ashr <2 x i64> %x, splat (i64 63)
  ret <2 x i64> %wide
}
Output Log
----------------------------------------
define <2 x i64> @be_counter(<2 x i64> %x) {
#0:
  %narrow = bitcast <2 x i64> %x to <4 x i32>
  %shifted = ashr <4 x i32> %narrow, { 0, 31, 0, 31 }
  %shuf = shufflevector <4 x i32> %shifted, <4 x i32> poison, 1, 1, 3, 3
  %wide = bitcast <4 x i32> %shuf to <2 x i64>
  ret <2 x i64> %wide
}
=>
define <2 x i64> @be_counter(<2 x i64> %x) {
#0:
  %wide = ashr <2 x i64> %x, { 63, 63 }
  ret <2 x i64> %wide
}
Transformation doesn't verify!

ERROR: Value mismatch

Example:
<2 x i64> %x = < #x0000000000000000 (0), #x0000000080000000 (2147483648) >

Source:
<4 x i32> %narrow = < #x00000000 (0), #x00000000 (0), #x00000000 (0), #x80000000 (2147483648, -2147483648) >
<4 x i32> %shifted = < #x00000000 (0), #x00000000 (0), #x00000000 (0), #xffffffff (4294967295, -1) >
<4 x i32> %shuf = < #x00000000 (0), #x00000000 (0), #xffffffff (4294967295, -1), #xffffffff (4294967295, -1) >
<2 x i64> %wide = < #x0000000000000000 (0), #xffffffffffffffff (18446744073709551615, -1) >

Target:
<2 x i64> %wide = < #x0000000000000000 (0), #x0000000000000000 (0) >
Source value: < #x0000000000000000 (0), #xffffffffffffffff (18446744073709551615, -1) >
Target value: < #x0000000000000000 (0), #x0000000000000000 (0) >

Summary:
  0 correct transformations
  1 incorrect transformations
  0 failed-to-prove transformations
  0 Alive2 errors
BUG #8Patch-specific
Original IR
target datalayout = "E"

define <1 x i64> @big_endian_exact(<1 x i64> %x) {
  %narrow = bitcast <1 x i64> %x to <2 x i32>
  %shifted = ashr <2 x i32> %narrow, <i32 0, i32 31>
  %shuf = shufflevector <2 x i32> %shifted, <2 x i32> poison, <2 x i32> <i32 1, i32 1>
  %wide = bitcast <2 x i32> %shuf to <1 x i64>
  ret <1 x i64> %wide
}
Transformed IR
; ModuleID = '/data/tmp/tmpusxsjbfj/orig.ll'
source_filename = "/data/tmp/tmpusxsjbfj/orig.ll"
target datalayout = "E"

define <1 x i64> @big_endian_exact(<1 x i64> %x) {
  %wide = ashr <1 x i64> %x, splat (i64 63)
  ret <1 x i64> %wide
}
Output Log
----------------------------------------
define <1 x i64> @big_endian_exact(<1 x i64> %x) {
#0:
  %narrow = bitcast <1 x i64> %x to <2 x i32>
  %shifted = ashr <2 x i32> %narrow, { 0, 31 }
  %shuf = shufflevector <2 x i32> %shifted, <2 x i32> poison, 1, 1
  %wide = bitcast <2 x i32> %shuf to <1 x i64>
  ret <1 x i64> %wide
}
=>
define <1 x i64> @big_endian_exact(<1 x i64> %x) {
#0:
  %wide = ashr <1 x i64> %x, { 63 }
  ret <1 x i64> %wide
}
Transformation doesn't verify!

ERROR: Value mismatch

Example:
<1 x i64> %x = < #x0000000080000000 (2147483648) >

Source:
<2 x i32> %narrow = < #x00000000 (0), #x80000000 (2147483648, -2147483648) >
<2 x i32> %shifted = < #x00000000 (0), #xffffffff (4294967295, -1) >
<2 x i32> %shuf = < #xffffffff (4294967295, -1), #xffffffff (4294967295, -1) >
<1 x i64> %wide = < #xffffffffffffffff (18446744073709551615, -1) >

Target:
<1 x i64> %wide = < #x0000000000000000 (0) >
Source value: < #xffffffffffffffff (18446744073709551615, -1) >
Target value: < #x0000000000000000 (0) >

Summary:
  0 correct transformations
  1 incorrect transformations
  0 failed-to-prove transformations
  0 Alive2 errors
BUG #9Patch-specific
Original IR
target datalayout = "E"

define <2 x i128> @big_endian_wider(<2 x i128> %x) {
  %narrow = bitcast <2 x i128> %x to <4 x i64>
  %shifted = ashr <4 x i64> %narrow, <i64 0, i64 63, i64 0, i64 63>
  %shuf = shufflevector <4 x i64> %shifted, <4 x i64> poison, <4 x i32> <i32 1, i32 1, i32 3, i32 3>
  %wide = bitcast <4 x i64> %shuf to <2 x i128>
  ret <2 x i128> %wide
}
Transformed IR
; ModuleID = '/data/tmp/tmpmt5wcft3/orig.ll'
source_filename = "/data/tmp/tmpmt5wcft3/orig.ll"
target datalayout = "E"

define <2 x i128> @big_endian_wider(<2 x i128> %x) {
  %wide = ashr <2 x i128> %x, splat (i128 127)
  ret <2 x i128> %wide
}
Output Log
----------------------------------------
define <2 x i128> @big_endian_wider(<2 x i128> %x) {
#0:
  %narrow = bitcast <2 x i128> %x to <4 x i64>
  %shifted = ashr <4 x i64> %narrow, { 0, 63, 0, 63 }
  %shuf = shufflevector <4 x i64> %shifted, <4 x i64> poison, 1, 1, 3, 3
  %wide = bitcast <4 x i64> %shuf to <2 x i128>
  ret <2 x i128> %wide
}
=>
define <2 x i128> @big_endian_wider(<2 x i128> %x) {
#0:
  %wide = ashr <2 x i128> %x, { 127, 127 }
  ret <2 x i128> %wide
}
Transformation doesn't verify!

ERROR: Value mismatch

Example:
<2 x i128> %x = < #x00000000000000000000000000000000 (0), #x00000000000000008000000000000000 (9223372036854775808) >

Source:
<4 x i64> %narrow = < #x0000000000000000 (0), #x0000000000000000 (0), #x0000000000000000 (0), #x8000000000000000 (9223372036854775808, -9223372036854775808) >
<4 x i64> %shifted = < #x0000000000000000 (0), #x0000000000000000 (0), #x0000000000000000 (0), #xffffffffffffffff (18446744073709551615, -1) >
<4 x i64> %shuf = < #x0000000000000000 (0), #x0000000000000000 (0), #xffffffffffffffff (18446744073709551615, -1), #xffffffffffffffff (18446744073709551615, -1) >
<2 x i128> %wide = < #x00000000000000000000000000000000 (0), #xffffffffffffffffffffffffffffffff (340282366920938463463374607431768211455, -1) >

Target:
<2 x i128> %wide = < #x00000000000000000000000000000000 (0), #x00000000000000000000000000000000 (0) >
Source value: < #x00000000000000000000000000000000 (0), #xffffffffffffffffffffffffffffffff (340282366920938463463374607431768211455, -1) >
Target value: < #x00000000000000000000000000000000 (0), #x00000000000000000000000000000000 (0) >

Summary:
  0 correct transformations
  1 incorrect transformations
  0 failed-to-prove transformations
  0 Alive2 errors
BUG #10Patch-specific
Original IR
target datalayout = "E"

define <1 x i64> @be_min(<1 x i64> %x) {
  %narrow = bitcast <1 x i64> %x to <2 x i32>
  %shifted = ashr <2 x i32> %narrow, <i32 0, i32 31>
  %shuf = shufflevector <2 x i32> %shifted, <2 x i32> poison, <2 x i32> <i32 1, i32 1>
  %wide = bitcast <2 x i32> %shuf to <1 x i64>
  ret <1 x i64> %wide
}
Transformed IR
; ModuleID = '/data/tmp/tmp_djwj964/orig.ll'
source_filename = "/data/tmp/tmp_djwj964/orig.ll"
target datalayout = "E"

define <1 x i64> @be_min(<1 x i64> %x) {
  %wide = ashr <1 x i64> %x, splat (i64 63)
  ret <1 x i64> %wide
}
Output Log
----------------------------------------
define <1 x i64> @be_min(<1 x i64> %x) {
#0:
  %narrow = bitcast <1 x i64> %x to <2 x i32>
  %shifted = ashr <2 x i32> %narrow, { 0, 31 }
  %shuf = shufflevector <2 x i32> %shifted, <2 x i32> poison, 1, 1
  %wide = bitcast <2 x i32> %shuf to <1 x i64>
  ret <1 x i64> %wide
}
=>
define <1 x i64> @be_min(<1 x i64> %x) {
#0:
  %wide = ashr <1 x i64> %x, { 63 }
  ret <1 x i64> %wide
}
Transformation doesn't verify!

ERROR: Value mismatch

Example:
<1 x i64> %x = < #x0000000080000000 (2147483648) >

Source:
<2 x i32> %narrow = < #x00000000 (0), #x80000000 (2147483648, -2147483648) >
<2 x i32> %shifted = < #x00000000 (0), #xffffffff (4294967295, -1) >
<2 x i32> %shuf = < #xffffffff (4294967295, -1), #xffffffff (4294967295, -1) >
<1 x i64> %wide = < #xffffffffffffffff (18446744073709551615, -1) >

Target:
<1 x i64> %wide = < #x0000000000000000 (0) >
Source value: < #xffffffffffffffff (18446744073709551615, -1) >
Target value: < #x0000000000000000 (0) >

Summary:
  0 correct transformations
  1 incorrect transformations
  0 failed-to-prove transformations
  0 Alive2 errors
BUG #11Patch-specific
Original IR
target datalayout = "E"

define i64 @be_v2i64_lane0(<2 x i64> %x) {
  %narrow = bitcast <2 x i64> %x to <4 x i32>
  %shifted = ashr <4 x i32> %narrow, <i32 0, i32 31, i32 0, i32 31>
  %shuf = shufflevector <4 x i32> %shifted, <4 x i32> poison, <4 x i32> <i32 1, i32 1, i32 3, i32 3>
  %wide = bitcast <4 x i32> %shuf to <2 x i64>
  %r = extractelement <2 x i64> %wide, i32 0
  ret i64 %r
}
Transformed IR
; ModuleID = '/data/tmp/tmpo9nnn_zp/orig.ll'
source_filename = "/data/tmp/tmpo9nnn_zp/orig.ll"
target datalayout = "E"

define i64 @be_v2i64_lane0(<2 x i64> %x) {
  %1 = extractelement <2 x i64> %x, i64 0
  %r = ashr i64 %1, 63
  ret i64 %r
}
Output Log
----------------------------------------
define i64 @be_v2i64_lane0(<2 x i64> %x) {
#0:
  %narrow = bitcast <2 x i64> %x to <4 x i32>
  %shifted = ashr <4 x i32> %narrow, { 0, 31, 0, 31 }
  %shuf = shufflevector <4 x i32> %shifted, <4 x i32> poison, 1, 1, 3, 3
  %wide = bitcast <4 x i32> %shuf to <2 x i64>
  %r = extractelement <2 x i64> %wide, i32 0
  ret i64 %r
}
=>
define i64 @be_v2i64_lane0(<2 x i64> %x) {
#0:
  %#1 = extractelement <2 x i64> %x, i64 0
  %r = ashr i64 %#1, 63
  ret i64 %r
}
Transformation doesn't verify!

ERROR: Value mismatch

Example:
<2 x i64> %x = < #x0000000080000000 (2147483648), poison >

Source:
<4 x i32> %narrow = < #x00000000 (0), #x80000000 (2147483648, -2147483648), poison, poison >
<4 x i32> %shifted = < #x00000000 (0), #xffffffff (4294967295, -1), poison, poison >
<4 x i32> %shuf = < #xffffffff (4294967295, -1), #xffffffff (4294967295, -1), poison, poison >
<2 x i64> %wide = < #xffffffffffffffff (18446744073709551615, -1), poison >
i64 %r = #xffffffffffffffff (18446744073709551615, -1)

Target:
i64 %#1 = #x0000000080000000 (2147483648)
i64 %r = #x0000000000000000 (0)
Source value: #xffffffffffffffff (18446744073709551615, -1)
Target value: #x0000000000000000 (0)

Summary:
  0 correct transformations
  1 incorrect transformations
  0 failed-to-prove transformations
  0 Alive2 errors
BUG #12Patch-specific
Original IR
target datalayout = "E"

define i32 @be_i32_i16(<1 x i32> %x) {
  %narrow = bitcast <1 x i32> %x to <2 x i16>
  %shifted = ashr <2 x i16> %narrow, <i16 0, i16 15>
  %shuf = shufflevector <2 x i16> %shifted, <2 x i16> poison, <2 x i32> <i32 1, i32 1>
  %wide = bitcast <2 x i16> %shuf to <1 x i32>
  %r = extractelement <1 x i32> %wide, i32 0
  ret i32 %r
}
Transformed IR
; ModuleID = '/data/tmp/tmpsw409pqg/orig.ll'
source_filename = "/data/tmp/tmpsw409pqg/orig.ll"
target datalayout = "E"

define i32 @be_i32_i16(<1 x i32> %x) {
  %1 = extractelement <1 x i32> %x, i64 0
  %r = ashr i32 %1, 31
  ret i32 %r
}
Output Log
----------------------------------------
define i32 @be_i32_i16(<1 x i32> %x) {
#0:
  %narrow = bitcast <1 x i32> %x to <2 x i16>
  %shifted = ashr <2 x i16> %narrow, { 0, 15 }
  %shuf = shufflevector <2 x i16> %shifted, <2 x i16> poison, 1, 1
  %wide = bitcast <2 x i16> %shuf to <1 x i32>
  %r = extractelement <1 x i32> %wide, i32 0
  ret i32 %r
}
=>
define i32 @be_i32_i16(<1 x i32> %x) {
#0:
  %#1 = extractelement <1 x i32> %x, i64 0
  %r = ashr i32 %#1, 31
  ret i32 %r
}
Transformation doesn't verify!

ERROR: Value mismatch

Example:
<1 x i32> %x = < #x00008000 (32768) >

Source:
<2 x i16> %narrow = < #x0000 (0), #x8000 (32768, -32768) >
<2 x i16> %shifted = < #x0000 (0), #xffff (65535, -1) >
<2 x i16> %shuf = < #xffff (65535, -1), #xffff (65535, -1) >
<1 x i32> %wide = < #xffffffff (4294967295, -1) >
i32 %r = #xffffffff (4294967295, -1)

Target:
i32 %#1 = #x00008000 (32768)
i32 %r = #x00000000 (0)
Source value: #xffffffff (4294967295, -1)
Target value: #x00000000 (0)

Summary:
  0 correct transformations
  1 incorrect transformations
  0 failed-to-prove transformations
  0 Alive2 errors

Test Strategies

1Big-endian v1i64 low-half sign-bit false positive
Target
Mutate the canonical v1i64/i32 pattern by adding a big-endian datalayout and a constant or testable input with high 32 bits zero and low 32-bit sign bit set; expect no fold or correct endian-adjusted fold.
Rationale
The fold assumes that after `bitcast <1 x i64> -> <2 x i32>`, element 1 is the high 32-bit half. That is true on little-endian, but on big-endian element 1 is the low half. If the low half has bit 31 set while the i64 sign bit is clear, the original pattern computes all-ones but the wide `ashr` computes zero.
Expected Issue
On a big-endian datalayout, InstCombine may fold to `ashr <1 x i64> %x, <i64 63>` and change the result from all-ones to zero for inputs such as `0x0000000080000000`.
2Big-endian v1i64 high-sign lost through low-half selection
Target
Use the same fold shape with big-endian datalayout and input/constant whose high half is negative and low half is non-negative; check that the fold is not applied.
Rationale
For big-endian vector bitcasts, mask `<1,1>` duplicates the low 32-bit half, not the high 32-bit half. If the i64 sign bit is set but the low half sign bit is clear, the original pattern produces zero while the replacement `ashr i64, 63` produces all-ones.
Expected Issue
On big-endian targets, InstCombine may change a negative i64 result from zero to all-ones, e.g. for `0x8000000000000000`.
3Big-endian multi-lane v2i64 lane-wise mismatch
Target
Mutate the canonical test from `<1 x i64>` to `<2 x i64>` with mask `<1,1,3,3>`, shifts `<0,31,0,31>`, and big-endian datalayout; use constants or FileCheck to ensure no wide `ashr <2 x i64>, splat(63)` appears.
Rationale
The loop checks masks per wide element but does not consult datalayout. With `<2 x i64> -> <4 x i32>`, each pair `<2k,2k+1>` has the same endian inversion problem. A mixed vector such as lane0 low-half sign set/high sign clear and lane1 high sign set/low sign clear exposes both directions simultaneously.
Expected Issue
The fold may incorrectly replace a per-lane big-endian computation with a wide per-lane sign splat, causing mixed lanes to be wrong independently.
4Big-endian generalized width mismatch vNi32/i16
Target
Create a big-endian `<1 x i32>` or `<2 x i32>` variant with inner `ashr <2 x i16>/<4 x i16>` shifts `<0,15,...>` and mask `<1,1,...>`; check that InstCombine does not form `ashr <N x i32>, splat(31)`.
Rationale
The implementation is generic for any even integer width `W >= 4`, not only the stated X86 `i64` emulation. The same high-half assumption fails for `bitcast <N x i32> -> <2N x i16>` on big-endian systems; mask `<1,1>` duplicates the low 16 bits rather than the sign-containing high 16 bits.
Expected Issue
The generalized fold may be wrong for big-endian non-i64 widths, e.g. `i32` widened from two `i16` halves, returning sign of the wrong half.
5Inner exact ashr poison relaxation regression guard
Target
Mutate the canonical little-endian pattern by adding `exact` to the narrow `ashr` and using a high half with low bits set; verify the intended behavior/check line is stable and no invalid `exact` flag is propagated to the new wide `ashr`.
Rationale
`m_AShr` matches an `ashr` regardless of the `exact` flag, and the replacement creates a non-exact wide `ashr`. For inputs where the selected high half has shifted-out one bits, the original `ashr exact` lane is poison while the replacement is defined. Although making poison more defined is generally allowed, this is a notable semantic relaxation and should be covered so reviewers consciously accept it.
Expected Issue
If the inner narrow `ashr` is marked `exact`, the fold may remove the `exact` poison condition and make formerly poison cases defined. This is likely a legal refinement, but it should be explicitly tested to avoid accidental expectations about preserving exactness or later transforms relying on it.