Skip to content

Commit 453ff4b

Browse files
KanRoberttstellar
authored andcommitted
[X86][CodeGen] Fix crash when commute operands of Instruction for code size (#79245)
Reported in 134fcc6 Incorrect opcode is used b/c there is a `[[fallthrough]]` at line 2386. (cherry picked from commit 33ecef9)
1 parent c9e73cd commit 453ff4b

File tree

2 files changed

+23
-21
lines changed

2 files changed

+23
-21
lines changed

llvm/lib/Target/X86/X86InstrInfo.cpp

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2354,33 +2354,26 @@ MachineInstr *X86InstrInfo::commuteInstructionImpl(MachineInstr &MI, bool NewMI,
23542354
case X86::VBLENDPSrri:
23552355
// If we're optimizing for size, try to use MOVSD/MOVSS.
23562356
if (MI.getParent()->getParent()->getFunction().hasOptSize()) {
2357-
unsigned Mask;
2358-
switch (Opc) {
2359-
default:
2360-
llvm_unreachable("Unreachable!");
2361-
case X86::BLENDPDrri:
2362-
Opc = X86::MOVSDrr;
2363-
Mask = 0x03;
2364-
break;
2365-
case X86::BLENDPSrri:
2366-
Opc = X86::MOVSSrr;
2367-
Mask = 0x0F;
2368-
break;
2369-
case X86::VBLENDPDrri:
2370-
Opc = X86::VMOVSDrr;
2371-
Mask = 0x03;
2372-
break;
2373-
case X86::VBLENDPSrri:
2374-
Opc = X86::VMOVSSrr;
2375-
Mask = 0x0F;
2376-
break;
2377-
}
2357+
unsigned Mask = (Opc == X86::BLENDPDrri || Opc == X86::VBLENDPDrri) ? 0x03: 0x0F;
23782358
if ((MI.getOperand(3).getImm() ^ Mask) == 1) {
2359+
#define FROM_TO(FROM, TO) \
2360+
case X86::FROM: \
2361+
Opc = X86::TO; \
2362+
break;
2363+
switch (Opc) {
2364+
default:
2365+
llvm_unreachable("Unreachable!");
2366+
FROM_TO(BLENDPDrri, MOVSDrr)
2367+
FROM_TO(BLENDPSrri, MOVSSrr)
2368+
FROM_TO(VBLENDPDrri, VMOVSDrr)
2369+
FROM_TO(VBLENDPSrri, VMOVSSrr)
2370+
}
23792371
WorkingMI = CloneIfNew(MI);
23802372
WorkingMI->setDesc(get(Opc));
23812373
WorkingMI->removeOperand(3);
23822374
break;
23832375
}
2376+
#undef FROM_TO
23842377
}
23852378
[[fallthrough]];
23862379
case X86::PBLENDWrri:

llvm/test/CodeGen/X86/commute-blend-avx2.ll

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,12 @@ define <4 x double> @commute_fold_vblendpd_256(<4 x double> %a, ptr %b) #0 {
8888
ret <4 x double> %2
8989
}
9090
declare <4 x double> @llvm.x86.avx.blend.pd.256(<4 x double>, <4 x double>, i8) nounwind readnone
91+
92+
define <4 x float> @commute_vblendpd_128_for_code_size(<4 x float> %a, <4 x float> %b) optsize {
93+
; CHECK-LABEL: commute_vblendpd_128_for_code_size:
94+
; CHECK: # %bb.0:
95+
; CHECK-NEXT: vblendps {{.*#+}} xmm0 = xmm1[0],xmm0[1],xmm1[2,3]
96+
; CHECK-NEXT: retq
97+
%r = shufflevector <4 x float> %b, <4 x float> %a, <4 x i32> <i32 0, i32 5, i32 2, i32 3>
98+
ret <4 x float> %r
99+
}

0 commit comments

Comments
 (0)