[llvm-branch-commits] [llvm] [AArch64][llvm] Fix constant expressions in CMPBR immediate aliases (PR #230050)

via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Thu Oct 8 03:24:17 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-backend-aarch64

Author: Jonathan Thackray (jthackray)

<details>
<summary>Changes</summary>

Use the standard immediate parser for CMPBR aliases so that parenthesized
expressions and explicit unary plus are accepted. This also simplifies
the amount of code required, since we can remove `tryParseAdjImm0_63`,
and reuse code in `AdjImmAsmOperand` which does the same job.

Check immediates in their original ranges and apply the adjustment
when rendering MC operands. Add coverage for constant expressions in
`cbge`, `cbhs`, `cble` and `cbls`.

---

<sub>Stack created with <a href="https://github.com/github/gh-stack">GitHub Stacks CLI</a> • <a href="https://gh.io/stacks-feedback">Give Feedback 💬</a></sub>

---
Full diff: https://github.com/llvm/llvm-project/pull/230050.diff


3 Files Affected:

- (modified) llvm/lib/Target/AArch64/AArch64InstrFormats.td (+13-23) 
- (modified) llvm/lib/Target/AArch64/AsmParser/AArch64AsmParser.cpp (-30) 
- (modified) llvm/test/MC/AArch64/CMPBR/cmpbr_aliases.s (+13) 


``````````diff
diff --git a/llvm/lib/Target/AArch64/AArch64InstrFormats.td b/llvm/lib/Target/AArch64/AArch64InstrFormats.td
index 754e4444cf0dc..b87bf6ade0acc 100644
--- a/llvm/lib/Target/AArch64/AArch64InstrFormats.td
+++ b/llvm/lib/Target/AArch64/AArch64InstrFormats.td
@@ -470,6 +470,15 @@ def uimm6_32b : Operand<i32>, ImmLeaf<i32, [{ return Imm >= 0 && Imm < 64; }]> {
   let OperandType = "OPERAND_IMMEDIATE";
 }
 
+class AdjImmAsmOperand<string name, string diagnostic, int adjustment,
+                       int low, int high> : AsmOperandClass {
+  let Name = name;
+  let DiagnosticType = diagnostic;
+  let RenderMethod = "addAdjImmOperands<" # adjustment # ">";
+  let PredicateMethod = "isImmInRange<" # !sub(low, adjustment) # "," #
+                        !sub(high, adjustment) # ">";
+}
+
 def CmpBranchUImm6Operand_32b
     : ComplexPattern<i32, 1, "SelectCmpBranchUImm6Operand", [imm]> {
   let WantsParent = true;
@@ -486,21 +495,11 @@ def CmpBranchBExtOperand
 def CmpBranchHExtOperand
     : ComplexPattern<i32, 2, "SelectCmpBranchExtOperand<false>", []> {}
 
-def UImm6Plus1Operand : AsmOperandClass {
-  let Name = "UImm6P1";
-  let DiagnosticType = "InvalidImm1_64";
-  let RenderMethod = "addImmOperands";
-  let ParserMethod = "tryParseAdjImm0_63<-1>";
-  let PredicateMethod = "isImmInRange<0,63>";
-}
+def UImm6Plus1Operand
+    : AdjImmAsmOperand<"UImm6P1", "InvalidImm1_64", -1, 0, 63>;
 
-def UImm6Minus1Operand : AsmOperandClass {
-  let Name = "UImm6M1";
-  let DiagnosticType = "InvalidImmM1_62";
-  let RenderMethod = "addImmOperands";
-  let ParserMethod = "tryParseAdjImm0_63<1>";
-  let PredicateMethod = "isImmInRange<0,63>";
-}
+def UImm6Minus1Operand
+    : AdjImmAsmOperand<"UImm6M1", "InvalidImmM1_62", 1, 0, 63>;
 
 def uimm6p1_32b : Operand<i32> {
   let ParserMatchClass = UImm6Plus1Operand;
@@ -527,15 +526,6 @@ def uimm9 : Operand<i64> {
   let OperandType = "OPERAND_IMMEDIATE";
 }
 
-class AdjImmAsmOperand<string name, string diagnostic, int adjustment,
-                       int low, int high> : AsmOperandClass {
-  let Name = name;
-  let DiagnosticType = diagnostic;
-  let RenderMethod = "addAdjImmOperands<" # adjustment # ">";
-  let PredicateMethod = "isImmInRange<" # !sub(low, adjustment) # "," #
-                        !sub(high, adjustment) # ">";
-}
-
 def uimm9p1 : Operand<i64> {
   let ParserMatchClass =
       AdjImmAsmOperand<"UImm9P1", "InvalidImm1_512", -1, 0, 511>;
diff --git a/llvm/lib/Target/AArch64/AsmParser/AArch64AsmParser.cpp b/llvm/lib/Target/AArch64/AsmParser/AArch64AsmParser.cpp
index 412d4f16bc98b..a904efc8f0612 100644
--- a/llvm/lib/Target/AArch64/AsmParser/AArch64AsmParser.cpp
+++ b/llvm/lib/Target/AArch64/AsmParser/AArch64AsmParser.cpp
@@ -296,7 +296,6 @@ class AArch64AsmParser : public MCTargetAsmParser {
   ParseStatus tryParseSVEVecLenSpecifier(OperandVector &Operands);
   ParseStatus tryParseGPR64x8(OperandVector &Operands);
   ParseStatus tryParseImmRange(OperandVector &Operands);
-  template <int> ParseStatus tryParseAdjImm0_63(OperandVector &Operands);
 
 public:
   enum AArch64MatchResultTy {
@@ -8797,32 +8796,3 @@ ParseStatus AArch64AsmParser::tryParseImmRange(OperandVector &Operands) {
       AArch64Operand::CreateImmRange(ImmFVal, ImmLVal, S, E, getContext()));
   return ParseStatus::Success;
 }
-
-template <int Adj>
-ParseStatus AArch64AsmParser::tryParseAdjImm0_63(OperandVector &Operands) {
-  SMLoc S = getLoc();
-
-  parseOptionalToken(AsmToken::Hash);
-
-  if (getTok().isNot(AsmToken::Integer) && getTok().isNot(AsmToken::Minus))
-    return ParseStatus::NoMatch;
-
-  int64_t Imm;
-  if (parseImmExpr(Imm))
-    return ParseStatus::Failure;
-
-  // We want an adjusted immediate in the range [0, 63]. If we don't have one,
-  // return a value, which is certain to trigger a error message about invalid
-  // immediate range instead of a non-descriptive invalid operand error.
-  static_assert(Adj == 1 || Adj == -1, "Unsafe immediate adjustment");
-  if (Imm == INT64_MIN || Imm == INT64_MAX || Imm + Adj < 0 || Imm + Adj > 63)
-    Imm = -2;
-  else
-    Imm += Adj;
-
-  SMLoc E = SMLoc::getFromPointer(getLoc().getPointer() - 1);
-  Operands.push_back(AArch64Operand::CreateImm(
-      MCConstantExpr::create(Imm, getContext()), S, E, getContext()));
-
-  return ParseStatus::Success;
-}
diff --git a/llvm/test/MC/AArch64/CMPBR/cmpbr_aliases.s b/llvm/test/MC/AArch64/CMPBR/cmpbr_aliases.s
index b7ec31d51a426..8985a574ace80 100644
--- a/llvm/test/MC/AArch64/CMPBR/cmpbr_aliases.s
+++ b/llvm/test/MC/AArch64/CMPBR/cmpbr_aliases.s
@@ -72,3 +72,16 @@
 
 // CHECK: cbgt x5, x3, #-1024                  // encoding: [0x05,0x20,0x03,0xf4]
     cblt x3, x5, #-1024
+// Constant expressions in adjusted immediate aliases.
+
+// CHECK: cbgt w5, #0, #-1024                  // encoding: [0x05,0x20,0x00,0x75]
+    cbge w5, #(2 - 1), #-1024
+
+// CHECK: cbhi x5, #63, #-1024                 // encoding: [0x05,0xa0,0x5f,0xf5]
+    cbhs x5, #+64, #-1024
+
+// CHECK: cblt w5, #0, #-1024                  // encoding: [0x05,0x20,0x20,0x75]
+    cble w5, #(-1), #-1024
+
+// CHECK: cblo x5, #63, #-1024                 // encoding: [0x05,0xa0,0x7f,0xf5]
+    cbls x5, #+62, #-1024

``````````

</details>


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


More information about the llvm-branch-commits mailing list