[llvm] [NFC][AArch64] Extract MOVaddr* expansion model into common header (PR #183503)

Guy David via llvm-commits llvm-commits at lists.llvm.org
Fri May 8 09:34:14 PDT 2026


https://github.com/guy-david updated https://github.com/llvm/llvm-project/pull/183503

>From 1430e8341b6745c82951ec07f1c62cc4fc6e0f9e Mon Sep 17 00:00:00 2001
From: Guy David <guyda96 at gmail.com>
Date: Thu, 26 Feb 2026 11:34:40 +0200
Subject: [PATCH] [NFC][AArch64] Extract MOVaddr* expansion model into common
 header

This makes the expansion logic reusable by getInstSizeInBytes in a
follow-up patch.
---
 llvm/lib/Target/AArch64/AArch64ExpandImm.cpp  |  72 ++++++----
 llvm/lib/Target/AArch64/AArch64ExpandImm.h    |   9 +-
 .../AArch64/AArch64ExpandPseudoInsts.cpp      | 127 ++++++++++--------
 llvm/lib/Target/AArch64/AArch64InstrInfo.cpp  |   1 +
 4 files changed, 128 insertions(+), 81 deletions(-)

diff --git a/llvm/lib/Target/AArch64/AArch64ExpandImm.cpp b/llvm/lib/Target/AArch64/AArch64ExpandImm.cpp
index f44cb8a0628d7..096803fe07dfd 100644
--- a/llvm/lib/Target/AArch64/AArch64ExpandImm.cpp
+++ b/llvm/lib/Target/AArch64/AArch64ExpandImm.cpp
@@ -1,4 +1,4 @@
-//===- AArch64ExpandImm.h - AArch64 Immediate Expansion -------------------===//
+//===- AArch64ExpandImm.cpp - AArch64 Immediate Expansion -----------------===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
@@ -6,12 +6,12 @@
 //
 //===----------------------------------------------------------------------===//
 //
-// This file implements the AArch64ExpandImm stuff.
+// This file implements the AArch64 immediate expansion stuff.
 //
 //===----------------------------------------------------------------------===//
 
-#include "AArch64.h"
 #include "AArch64ExpandImm.h"
+#include "AArch64.h"
 #include "MCTargetDesc/AArch64AddressingModes.h"
 
 using namespace llvm;
@@ -41,7 +41,7 @@ static bool canUseOrr(uint64_t Chunk, uint64_t &Encoding) {
 /// of the chunks doesn't matter), assuming |A|A|A|A| can be materialized with
 /// an ORR instruction.
 static bool tryToreplicateChunks(uint64_t UImm,
-				 SmallVectorImpl<ImmInsnModel> &Insn) {
+                                 SmallVectorImpl<ImmInsnModel> &Insn) {
   using CountMap = DenseMap<uint64_t, unsigned>;
 
   CountMap Counts;
@@ -64,7 +64,7 @@ static bool tryToreplicateChunks(uint64_t UImm,
 
     const bool CountThree = Count == 3;
 
-    Insn.push_back({ AArch64::ORRXri, 0, Encoding });
+    Insn.push_back({AArch64::ORRXri, 0, Encoding});
 
     unsigned ShiftAmt = 0;
     uint64_t Imm16 = 0;
@@ -77,8 +77,8 @@ static bool tryToreplicateChunks(uint64_t UImm,
     }
 
     // Create the first MOVK instruction.
-    Insn.push_back({ AArch64::MOVKXi, Imm16,
-		     AArch64_AM::getShifterImm(AArch64_AM::LSL, ShiftAmt) });
+    Insn.push_back({AArch64::MOVKXi, Imm16,
+                    AArch64_AM::getShifterImm(AArch64_AM::LSL, ShiftAmt)});
 
     // In case we have three instances the whole constant is now materialized
     // and we can exit.
@@ -92,8 +92,8 @@ static bool tryToreplicateChunks(uint64_t UImm,
       if (Imm16 != ChunkVal)
         break;
     }
-    Insn.push_back({ AArch64::MOVKXi, Imm16,
-                     AArch64_AM::getShifterImm(AArch64_AM::LSL, ShiftAmt) });
+    Insn.push_back({AArch64::MOVKXi, Imm16,
+                    AArch64_AM::getShifterImm(AArch64_AM::LSL, ShiftAmt)});
     return true;
   }
 
@@ -220,21 +220,21 @@ static bool trySequenceOfOnes(uint64_t UImm,
   // Create the ORR-immediate instruction.
   uint64_t Encoding = 0;
   AArch64_AM::processLogicalImmediate(OrrImm, 64, Encoding);
-  Insn.push_back({ AArch64::ORRXri, 0, Encoding });
+  Insn.push_back({AArch64::ORRXri, 0, Encoding});
 
   const bool SingleMovk = SecondMovkIdx == NotSet;
-  Insn.push_back({ AArch64::MOVKXi, getChunk(UImm, FirstMovkIdx),
-                   AArch64_AM::getShifterImm(AArch64_AM::LSL,
-                                             FirstMovkIdx * 16) });
+  Insn.push_back(
+      {AArch64::MOVKXi, getChunk(UImm, FirstMovkIdx),
+       AArch64_AM::getShifterImm(AArch64_AM::LSL, FirstMovkIdx * 16)});
 
   // Early exit in case we only need to emit a single MOVK instruction.
   if (SingleMovk)
     return true;
 
   // Create the second MOVK instruction.
-  Insn.push_back({ AArch64::MOVKXi, getChunk(UImm, SecondMovkIdx),
-	           AArch64_AM::getShifterImm(AArch64_AM::LSL,
-                                             SecondMovkIdx * 16) });
+  Insn.push_back(
+      {AArch64::MOVKXi, getChunk(UImm, SecondMovkIdx),
+       AArch64_AM::getShifterImm(AArch64_AM::LSL, SecondMovkIdx * 16)});
 
   return true;
 }
@@ -526,11 +526,31 @@ static bool tryEorOfLogicalImmediates(uint64_t Imm,
   return false;
 }
 
+/// Describe the expansion of a MOVaddr-family pseudo instruction.
+/// Returns a sequence of opcodes that the pseudo expands into.
+void AArch64_IMM::expandMOVAddr(unsigned Opcode, unsigned TargetFlags,
+                                bool IsTargetMachO,
+                                SmallVectorImpl<AddrInsnModel> &Insn) {
+  if (Opcode == AArch64::MOVaddrBA && IsTargetMachO) {
+    // Block address on MachO goes through a constant pool.
+    Insn.push_back({AArch64::ADRP});
+    Insn.push_back({AArch64::LDRXui});
+    return;
+  }
+
+  Insn.push_back({AArch64::ADRP});
+
+  if (TargetFlags & AArch64II::MO_TAGGED)
+    Insn.push_back({AArch64::MOVKXi});
+
+  Insn.push_back({AArch64::ADDXri});
+}
+
 /// \brief Expand a MOVi32imm or MOVi64imm pseudo instruction to a
 /// MOVZ or MOVN of width BitSize followed by up to 3 MOVK instructions.
 static inline void expandMOVImmSimple(uint64_t Imm, unsigned BitSize,
-				      unsigned OneChunks, unsigned ZeroChunks,
-				      SmallVectorImpl<ImmInsnModel> &Insn) {
+                                      unsigned OneChunks, unsigned ZeroChunks,
+                                      SmallVectorImpl<ImmInsnModel> &Insn) {
   const unsigned Mask = 0xFFFF;
 
   // Use a MOVZ or MOVN instruction to set the high bits, followed by one or
@@ -562,8 +582,8 @@ static inline void expandMOVImmSimple(uint64_t Imm, unsigned BitSize,
   }
   unsigned Imm16 = (Imm >> Shift) & Mask;
 
-  Insn.push_back({ FirstOpc, Imm16,
-                   AArch64_AM::getShifterImm(AArch64_AM::LSL, Shift) });
+  Insn.push_back(
+      {FirstOpc, Imm16, AArch64_AM::getShifterImm(AArch64_AM::LSL, Shift)});
 
   if (Shift == LastShift)
     return;
@@ -580,8 +600,8 @@ static inline void expandMOVImmSimple(uint64_t Imm, unsigned BitSize,
     if (Imm16 == (isNeg ? Mask : 0))
       continue; // This 16-bit portion is already set correctly.
 
-    Insn.push_back({ Opc, Imm16,
-                     AArch64_AM::getShifterImm(AArch64_AM::LSL, Shift) });
+    Insn.push_back(
+        {Opc, Imm16, AArch64_AM::getShifterImm(AArch64_AM::LSL, Shift)});
   }
 
   // Now, we get 16-bit divided Imm. If high and low bits are same in
@@ -624,7 +644,7 @@ void AArch64_IMM::expandMOVImm(uint64_t Imm, unsigned BitSize,
   uint64_t Encoding;
   if (AArch64_AM::processLogicalImmediate(UImm, BitSize, Encoding)) {
     unsigned Opc = (BitSize == 32 ? AArch64::ORRWri : AArch64::ORRXri);
-    Insn.push_back({ Opc, 0, Encoding });
+    Insn.push_back({Opc, 0, Encoding});
     return;
   }
 
@@ -659,12 +679,12 @@ void AArch64_IMM::expandMOVImm(uint64_t Imm, unsigned BitSize,
         AArch64_AM::processLogicalImmediate(ReplicateChunk, BitSize,
                                             Encoding)) {
       // Create the ORR-immediate instruction.
-      Insn.push_back({ AArch64::ORRXri, 0, Encoding });
+      Insn.push_back({AArch64::ORRXri, 0, Encoding});
 
       // Create the MOVK instruction.
       const unsigned Imm16 = getChunk(UImm, Shift / 16);
-      Insn.push_back({ AArch64::MOVKXi, Imm16,
-		       AArch64_AM::getShifterImm(AArch64_AM::LSL, Shift) });
+      Insn.push_back({AArch64::MOVKXi, Imm16,
+                      AArch64_AM::getShifterImm(AArch64_AM::LSL, Shift)});
       return;
     }
   }
diff --git a/llvm/lib/Target/AArch64/AArch64ExpandImm.h b/llvm/lib/Target/AArch64/AArch64ExpandImm.h
index 42c97d2c3e9b5..c3376520eb410 100644
--- a/llvm/lib/Target/AArch64/AArch64ExpandImm.h
+++ b/llvm/lib/Target/AArch64/AArch64ExpandImm.h
@@ -25,8 +25,15 @@ struct ImmInsnModel {
   uint64_t Op2;
 };
 
+struct AddrInsnModel {
+  unsigned Opcode;
+};
+
 void expandMOVImm(uint64_t Imm, unsigned BitSize,
-		  SmallVectorImpl<ImmInsnModel> &Insn);
+                  SmallVectorImpl<ImmInsnModel> &Insn);
+
+void expandMOVAddr(unsigned Opcode, unsigned TargetFlags, bool IsTargetMachO,
+                   SmallVectorImpl<AddrInsnModel> &Insn);
 
 } // end namespace AArch64_IMM
 
diff --git a/llvm/lib/Target/AArch64/AArch64ExpandPseudoInsts.cpp b/llvm/lib/Target/AArch64/AArch64ExpandPseudoInsts.cpp
index 535f9e4fac8c5..82023c6460f74 100644
--- a/llvm/lib/Target/AArch64/AArch64ExpandPseudoInsts.cpp
+++ b/llvm/lib/Target/AArch64/AArch64ExpandPseudoInsts.cpp
@@ -1539,70 +1539,89 @@ bool AArch64ExpandPseudoImpl::expandMI(MachineBasicBlock &MBB,
     MI.eraseFromParent();
     return true;
   }
-  case AArch64::MOVaddrBA: {
-    MachineFunction &MF = *MI.getParent()->getParent();
-    if (MF.getSubtarget<AArch64Subtarget>().isTargetMachO()) {
-      // blockaddress expressions have to come from a constant pool because the
-      // largest addend (and hence offset within a function) allowed for ADRP is
-      // only 8MB.
-      const BlockAddress *BA = MI.getOperand(1).getBlockAddress();
-      assert(MI.getOperand(1).getOffset() == 0 && "unexpected offset");
-
-      MachineConstantPool *MCP = MF.getConstantPool();
-      unsigned CPIdx = MCP->getConstantPoolIndex(BA, Align(8));
-
-      Register DstReg = MI.getOperand(0).getReg();
-      auto MIB1 =
-          BuildMI(MBB, MBBI, MI.getDebugLoc(), TII->get(AArch64::ADRP), DstReg)
-              .addConstantPoolIndex(CPIdx, 0, AArch64II::MO_PAGE);
-      auto MIB2 = BuildMI(MBB, MBBI, MI.getDebugLoc(),
-                          TII->get(AArch64::LDRXui), DstReg)
-                      .addUse(DstReg)
-                      .addConstantPoolIndex(
-                          CPIdx, 0, AArch64II::MO_PAGEOFF | AArch64II::MO_NC);
-      transferImpOps(MI, MIB1, MIB2);
-      MI.eraseFromParent();
-      return true;
-    }
-  }
-    [[fallthrough]];
+  case AArch64::MOVaddrBA:
   case AArch64::MOVaddr:
   case AArch64::MOVaddrJT:
   case AArch64::MOVaddrCP:
   case AArch64::MOVaddrTLS:
   case AArch64::MOVaddrEXT: {
-    // Expand into ADRP + ADD.
+    MachineFunction &MF = *MI.getParent()->getParent();
     Register DstReg = MI.getOperand(0).getReg();
     assert(DstReg != AArch64::XZR);
-    MachineInstrBuilder MIB1 =
-        BuildMI(MBB, MBBI, MI.getDebugLoc(), TII->get(AArch64::ADRP), DstReg)
-            .add(MI.getOperand(1));
-
-    if (MI.getOperand(1).getTargetFlags() & AArch64II::MO_TAGGED) {
-      // MO_TAGGED on the page indicates a tagged address. Set the tag now.
-      // We do so by creating a MOVK that sets bits 48-63 of the register to
-      // (global address + 0x100000000 - PC) >> 48. This assumes that we're in
-      // the small code model so we can assume a binary size of <= 4GB, which
-      // makes the untagged PC relative offset positive. The binary must also be
-      // loaded into address range [0, 2^48). Both of these properties need to
-      // be ensured at runtime when using tagged addresses.
-      auto Tag = MI.getOperand(1);
-      Tag.setTargetFlags(AArch64II::MO_PREL | AArch64II::MO_G3);
-      Tag.setOffset(0x100000000);
-      BuildMI(MBB, MBBI, MI.getDebugLoc(), TII->get(AArch64::MOVKXi), DstReg)
-          .addReg(DstReg)
-          .add(Tag)
-          .addImm(48);
+
+    SmallVector<AArch64_IMM::AddrInsnModel, 3> Insn;
+    AArch64_IMM::expandMOVAddr(
+        MI.getOpcode(), MI.getOperand(1).getTargetFlags(),
+        MF.getSubtarget<AArch64Subtarget>().isTargetMachO(), Insn);
+
+    // For MachO block addresses, we need to set up the constant pool entry
+    // before emitting instructions.
+    unsigned CPIdx = 0;
+    if (Insn.size() == 2 && Insn[1].Opcode == AArch64::LDRXui) {
+      // blockaddress expressions have to come from a constant pool because the
+      // largest addend (and hence offset within a function) allowed for ADRP is
+      // only 8MB.
+      const BlockAddress *BA = MI.getOperand(1).getBlockAddress();
+      assert(MI.getOperand(1).getOffset() == 0 && "unexpected offset");
+      MachineConstantPool *MCP = MF.getConstantPool();
+      CPIdx = MCP->getConstantPoolIndex(BA, Align(8));
     }
 
-    MachineInstrBuilder MIB2 =
-        BuildMI(MBB, MBBI, MI.getDebugLoc(), TII->get(AArch64::ADDXri))
-            .add(MI.getOperand(0))
-            .addReg(DstReg)
-            .add(MI.getOperand(2))
-            .addImm(0);
+    MachineInstrBuilder FirstMIB;
+    MachineInstrBuilder LastMIB;
+    for (const auto &I : Insn) {
+      MachineInstrBuilder MIB;
+      switch (I.Opcode) {
+      case AArch64::ADRP:
+        MIB = BuildMI(MBB, MBBI, MI.getDebugLoc(), TII->get(AArch64::ADRP),
+                      DstReg);
+        if (Insn[1].Opcode == AArch64::LDRXui)
+          MIB.addConstantPoolIndex(CPIdx, 0, AArch64II::MO_PAGE);
+        else
+          MIB.add(MI.getOperand(1));
+        break;
+      case AArch64::LDRXui:
+        MIB = BuildMI(MBB, MBBI, MI.getDebugLoc(), TII->get(AArch64::LDRXui),
+                      DstReg)
+                  .addUse(DstReg)
+                  .addConstantPoolIndex(
+                      CPIdx, 0, AArch64II::MO_PAGEOFF | AArch64II::MO_NC);
+        break;
+      case AArch64::MOVKXi: {
+        // MO_TAGGED on the page indicates a tagged address. Set the tag now.
+        // We do so by creating a MOVK that sets bits 48-63 of the register to
+        // (global address + 0x100000000 - PC) >> 48. This assumes that we're in
+        // the small code model so we can assume a binary size of <= 4GB, which
+        // makes the untagged PC relative offset positive. The binary must also
+        // be loaded into address range [0, 2^48). Both of these properties need
+        // to be ensured at runtime when using tagged addresses.
+        auto Tag = MI.getOperand(1);
+        Tag.setTargetFlags(AArch64II::MO_PREL | AArch64II::MO_G3);
+        Tag.setOffset(0x100000000);
+        MIB = BuildMI(MBB, MBBI, MI.getDebugLoc(), TII->get(AArch64::MOVKXi),
+                      DstReg)
+                  .addReg(DstReg)
+                  .add(Tag)
+                  .addImm(48);
+        break;
+      }
+      case AArch64::ADDXri:
+        MIB = BuildMI(MBB, MBBI, MI.getDebugLoc(), TII->get(AArch64::ADDXri))
+                  .add(MI.getOperand(0))
+                  .addReg(DstReg)
+                  .add(MI.getOperand(2))
+                  .addImm(0);
+        break;
+      default:
+        llvm_unreachable("unexpected opcode in MOVaddr expansion");
+      }
+
+      if (!FirstMIB.getInstr())
+        FirstMIB = MIB;
+      LastMIB = MIB;
+    }
 
-    transferImpOps(MI, MIB1, MIB2);
+    transferImpOps(MI, FirstMIB, LastMIB);
     MI.eraseFromParent();
     return true;
   }
diff --git a/llvm/lib/Target/AArch64/AArch64InstrInfo.cpp b/llvm/lib/Target/AArch64/AArch64InstrInfo.cpp
index c0a389f9b2d93..e8a5d6e258202 100644
--- a/llvm/lib/Target/AArch64/AArch64InstrInfo.cpp
+++ b/llvm/lib/Target/AArch64/AArch64InstrInfo.cpp
@@ -236,6 +236,7 @@ unsigned AArch64InstrInfo::getInstSizeInBytes(const MachineInstr &MI) const {
   case AArch64::SPACE:
     NumBytes = MI.getOperand(1).getImm();
     break;
+
   case TargetOpcode::BUNDLE:
     NumBytes = getInstBundleSize(MI);
     break;



More information about the llvm-commits mailing list