[llvm] [AArch64][GlobalISel] Avoid generating inserts for undefs when selecting G_BUILD_VECTOR (PR #84452)

Dhruv Chawla via llvm-commits llvm-commits at lists.llvm.org
Mon Mar 11 00:57:14 PDT 2024


================
@@ -5963,12 +5966,21 @@ bool AArch64InstructionSelector::selectBuildVector(MachineInstr &I,
     MachineOperand &RegOp = I.getOperand(1);
     RegOp.setReg(Reg);
     RBI.constrainGenericRegister(DstReg, *RC, MRI);
-  } else {
-    // We don't need a subregister copy. Save a copy by re-using the
-    // destination register on the final insert.
-    assert(PrevMI && "PrevMI was null?");
+  } else if (PrevMI) {
+    // PrevMI is not nullptr, so we have generated inserts and don't need a
+    // subregister copy. Save a copy by re-using the destination register on the
+    // final insert.
     PrevMI->getOperand(0).setReg(I.getOperand(0).getReg());
     constrainSelectedInstRegOperands(*PrevMI, TII, TRI, RBI);
+  } else {
----------------
dc03-work wrote:

I did try that at first, however that was giving a crash:
```
LLVM ERROR: VReg has no regclass after selection: %bv:fpr(<4 x s32>) = INSERT_SUBREG %3:fpr128(tied-def 0), %val:gpr32all(s32), %subreg.ssub (in function: undef_elts_different_regbanks)
```
in `llvm/test/CodeGen/AArch64/GlobalISel/select-build-vector.mir`. I think it is best to try and elide the copy wherever possible.

https://github.com/llvm/llvm-project/pull/84452


More information about the llvm-commits mailing list