[llvm] [AArch64][GlobalISel] Legalize Insert vector element (PR #81453)

David Green via llvm-commits llvm-commits at lists.llvm.org
Fri Mar 8 07:03:43 PST 2024


================
@@ -475,6 +476,59 @@ void applyEXT(MachineInstr &MI, ShuffleVectorPseudo &MatchInfo) {
   MI.eraseFromParent();
 }
 
+bool matchNonConstInsert(MachineInstr &MI, MachineRegisterInfo &MRI) {
+  assert(MI.getOpcode() == TargetOpcode::G_INSERT_VECTOR_ELT);
+
+  auto ValAndVReg =
+      getIConstantVRegValWithLookThrough(MI.getOperand(3).getReg(), MRI);
+  return !ValAndVReg;
+}
+
+void applyNonConstInsert(MachineInstr &MI, MachineRegisterInfo &MRI,
+                         MachineIRBuilder &Builder) {
+  assert(MI.getOpcode() == TargetOpcode::G_INSERT_VECTOR_ELT);
+  bool InsertVal = true;
+  Builder.setInstrAndDebugLoc(MI);
+
+  Register Offset = MI.getOperand(3).getReg();
+  LLT VecTy = MRI.getType(MI.getOperand(0).getReg());
+  LLT EltTy = MRI.getType(MI.getOperand(2).getReg());
+  LLT IdxTy = MRI.getType(MI.getOperand(3).getReg());
+
+  // Create a stack slot and store the vector into it
+  MachineFunction &MF = Builder.getMF();
+  int FrameIdx = MF.getFrameInfo().CreateStackObject(VecTy.getSizeInBytes(),
+                                                     Align(8), false);
+  LLT FramePtrTy = LLT::pointer(0, 64);
+  MachinePointerInfo PtrInfo = MachinePointerInfo::getFixedStack(MF, FrameIdx);
+  auto StackTemp = Builder.buildFrameIndex(FramePtrTy, FrameIdx);
+
+  Builder.buildStore(MI.getOperand(1), StackTemp, PtrInfo, Align(8));
+
+  // Get the pointer to the element, and be sure not to hit undefined behavior
+  // if the index is out of bounds.
+  assert(isPowerOf2_64(VecTy.getNumElements()) &&
+         "Expected a power-2 vector size");
+  auto Mask = Builder.buildConstant(IdxTy, VecTy.getNumElements() - 1);
+  Register And = Builder.buildAnd(IdxTy, Offset, Mask).getReg(0);
+  auto EltSize = Builder.buildConstant(IdxTy, EltTy.getSizeInBytes());
+  Register Mul = Builder.buildMul(IdxTy, And, EltSize).getReg(0);
+  Register EltPtr =
+      Builder.buildPtrAdd(MRI.getType(StackTemp.getReg(0)), StackTemp, Mul)
+          .getReg(0);
+
+  if (InsertVal) {
----------------
davemgreen wrote:

I forget where I got this code from now, but it was to allow it to be used with INSERT's and EXTRACT's. There will hopefully be a followup patch to apply to extracts too, if this one can get through review. It was smaller IIRC, but I can remove this for the moment and re-add it there. Thanks.

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


More information about the llvm-commits mailing list