[llvm] [ARM] Take advantage of built-in mod of shift amount in variable-shift rotations (PR #157208)

via llvm-commits llvm-commits at lists.llvm.org
Wed Apr 8 16:11:33 PDT 2026


================
@@ -3155,6 +3157,108 @@ bool ARMDAGToDAGISel::tryInsertVectorElt(SDNode *N) {
   return false;
 }
 
+/// tryShiftAmountMod - Take advantage of built-in mod of shift amount in
+/// variable shift/rotate instructions.
+bool ARMDAGToDAGISel::tryShiftAmountMod(SDNode *N) {
+  EVT VT = N->getValueType(0);
+  if (VT != MVT::i32)
+    return false;
+  // On ARM we intentionally do this only for ROTR. Unlike AArch64, variable
+  // SHL/SRL/SRA do not all have the same modulo-shift semantics we can exploit.
+  // Select ROR by register; in ARM state this is modeled as MOVsr with a ROR
+  // shifter operand, while in Thumb we use tROR/t2RORrr directly.
+
+  SDValue ShiftAmt = N->getOperand(1);
+  SDLoc DL(N);
+  SDValue NewShiftAmt;
+  auto emitUnary = [&](unsigned Opc, SDValue Src, bool IsRSB) {
+    if (Subtarget->isThumb2() || !Subtarget->isThumb()) {
+      SDValue Ops[] = {Src};
+      if (IsRSB) {
+        SDValue ZeroImm = CurDAG->getTargetConstant(0, DL, MVT::i32);
+        SDValue FullOps[] = {Src, ZeroImm, getAL(CurDAG, DL),
+                             CurDAG->getRegister(0, MVT::i32),
+                             CurDAG->getRegister(0, MVT::i32)};
+        MachineSDNode *Unary =
+            CurDAG->getMachineNode(Opc, DL, MVT::i32, FullOps);
+        return SDValue(Unary, 0);
+      }
+      SDValue FullOps[] = {Ops[0], getAL(CurDAG, DL),
+                           CurDAG->getRegister(0, MVT::i32),
+                           CurDAG->getRegister(0, MVT::i32)};
+      MachineSDNode *Unary = CurDAG->getMachineNode(Opc, DL, MVT::i32, FullOps);
+      return SDValue(Unary, 0);
+    }
+    SDValue Thumb1Ops[] = {CurDAG->getRegister(ARM::CPSR, MVT::i32), Src,
+                           getAL(CurDAG, DL), CurDAG->getRegister(0, MVT::i32)};
+    MachineSDNode *Unary = CurDAG->getMachineNode(Opc, DL, MVT::i32, Thumb1Ops);
+    return SDValue(Unary, 0);
+  };
+
+  if (ShiftAmt->getOpcode() == ISD::ADD || ShiftAmt->getOpcode() == ISD::SUB) {
+    SDValue Add0 = ShiftAmt->getOperand(0);
+    SDValue Add1 = ShiftAmt->getOperand(1);
+    unsigned Add0Imm;
+    unsigned Add1Imm;
+    if (isInt32Immediate(Add1, Add1Imm) && ((Add1Imm & 31) == 0)) {
+      NewShiftAmt = Add0;
+    } else if (ShiftAmt->getOpcode() == ISD::SUB &&
+               isInt32Immediate(Add0, Add0Imm) && Add0Imm != 0 &&
+               ((Add0Imm & 31) == 0)) {
+      unsigned NegOpc =
+          Subtarget->isThumb()
+              ? (Subtarget->hasThumb2() ? ARM::t2RSBri : ARM::tRSB)
+              : ARM::RSBri;
+      NewShiftAmt = emitUnary(NegOpc, Add1, /*IsRSB=*/true);
+    } else if (ShiftAmt->getOpcode() == ISD::SUB &&
+               isInt32Immediate(Add0, Add0Imm) && ((Add0Imm & 31) == 31)) {
+      unsigned NotOpc = Subtarget->isThumb()
+                            ? (Subtarget->isThumb2() ? ARM::t2MVNr : ARM::tMVN)
+                            : ARM::MVNr;
+      NewShiftAmt = emitUnary(NotOpc, Add1, /*IsRSB=*/false);
+    } else {
+      return false;
+    }
+  } else {
+    // Check if shift amount is masked with AND covering low 5 bits
----------------
SiliconA-Z wrote:

Fixed. Leftover from aarch64, where lsr, lsl, etc, do not have this fold.

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


More information about the llvm-commits mailing list