Skip to content

Commit 33ecef9

Browse files
authored
[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.
1 parent b0763a1 commit 33ecef9

File tree

2 files changed

+23
-21
lines changed

2 files changed

+23
-21
lines changed

llvm/lib/Target/X86/X86InstrInfo.cpp

+14-21
Original file line numberDiff line numberDiff line change
@@ -2326,33 +2326,26 @@ MachineInstr *X86InstrInfo::commuteInstructionImpl(MachineInstr &MI, bool NewMI,
23262326
case X86::VBLENDPSrri:
23272327
// If we're optimizing for size, try to use MOVSD/MOVSS.
23282328
if (MI.getParent()->getParent()->getFunction().hasOptSize()) {
2329-
unsigned Mask;
2330-
switch (Opc) {
2331-
default:
2332-
llvm_unreachable("Unreachable!");
2333-
case X86::BLENDPDrri:
2334-
Opc = X86::MOVSDrr;
2335-
Mask = 0x03;
2336-
break;
2337-
case X86::BLENDPSrri:
2338-
Opc = X86::MOVSSrr;
2339-
Mask = 0x0F;
2340-
break;
2341-
case X86::VBLENDPDrri:
2342-
Opc = X86::VMOVSDrr;
2343-
Mask = 0x03;
2344-
break;
2345-
case X86::VBLENDPSrri:
2346-
Opc = X86::VMOVSSrr;
2347-
Mask = 0x0F;
2348-
break;
2349-
}
2329+
unsigned Mask = (Opc == X86::BLENDPDrri || Opc == X86::VBLENDPDrri) ? 0x03: 0x0F;
23502330
if ((MI.getOperand(3).getImm() ^ Mask) == 1) {
2331+
#define FROM_TO(FROM, TO) \
2332+
case X86::FROM: \
2333+
Opc = X86::TO; \
2334+
break;
2335+
switch (Opc) {
2336+
default:
2337+
llvm_unreachable("Unreachable!");
2338+
FROM_TO(BLENDPDrri, MOVSDrr)
2339+
FROM_TO(BLENDPSrri, MOVSSrr)
2340+
FROM_TO(VBLENDPDrri, VMOVSDrr)
2341+
FROM_TO(VBLENDPSrri, VMOVSSrr)
2342+
}
23512343
WorkingMI = CloneIfNew(MI);
23522344
WorkingMI->setDesc(get(Opc));
23532345
WorkingMI->removeOperand(3);
23542346
break;
23552347
}
2348+
#undef FROM_TO
23562349
}
23572350
[[fallthrough]];
23582351
case X86::PBLENDWrri:

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

+9
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)