[llvm-branch-commits] [llvm] [LoongArch] Load suitable immediates with LU12I.W+SLLI.D (PR #242357)

Rong Mantle Bao via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Sat Nov 14 00:35:39 PST 2026


================
@@ -412,8 +420,20 @@ bool LoongArchMergeBaseOffsetOpt::foldLargeOffset(
       MachineOperand ImmOp = Curr->getOperand(2);
       if (ImmOp.getTargetFlags() != LoongArchII::MO_None || !Hi12)
         return false;
-      Offset += ImmOp.getImm() << 52;
-      Mask &= ~0xFFF0000000000000ULL;
+      const int64_t Imm = ImmOp.getImm();
+      Offset += (static_cast<uint64_t>(Imm) << 52) << Shift;
+      Mask &= ~(0xFFF0000000000000ULL << Shift);
+      Reg = Curr->getOperand(1).getReg();
+      Instrs.push_back(Curr);
+      break;
+    }
+    case LoongArch::SLLI_D: {
----------------
CSharperMantle wrote:

We currently walk up to 4 instructions. In the baseline code, [the SLLI.D is not walked](https://godbolt.org/z/sa754fWG5) and the folding is rejected. Now, with the new code, the trailing, user-supplied SLLI.D could also be walked, making the sequence length 5, and the truncation.

I think the exit condition should be more robust by rejecting any folds that do not end in R0, and optionally increase the instruction length from 4 to 5 if the folding is really desired.

Note that the original implementation can also misfold due to truncation: <https://godbolt.org/z/vvdGdsxs9>.

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


More information about the llvm-branch-commits mailing list