[llvm] [NFC][MachineLoopInfo] Consider loads in `isLoopInvariant`. (PR #95632)

Mikhail Gudim via llvm-commits llvm-commits at lists.llvm.org
Fri Jun 14 22:26:16 PDT 2024


https://github.com/mgudim created https://github.com/llvm/llvm-project/pull/95632

Currently `isLoopInvariant` does not consider loads. In fact, if this function is called with a load instruction it may return incorrect result because some store in the loop may alias with the load. In MachineLICM there is separate logic to handle loads.

In this MR we move this logic inside `isLoopInvariant` because this is a more appropriate place for it.

>From c1bbc627d1f7f4fecf83365bf3a14548caca12cf Mon Sep 17 00:00:00 2001
From: Mikhail Gudim <mgudim at gmail.com>
Date: Sat, 15 Jun 2024 00:20:28 -0400
Subject: [PATCH] [NFC][MachineLoopInfo] Consider loads in `isLoopInvariant`.

Currently `isLoopInvariant` does not consider loads. In fact, if this
function is called with a load instruction it may return incorrect
result because some store in the loop may alias with the load. In
MachineLICM there is separate logic to handle loads.

In this MR we move this logic inside `isLoopInvariant` because this
is a more appropriate place for it.
---
 llvm/lib/CodeGen/MachineLICM.cpp     | 3 +--
 llvm/lib/CodeGen/MachineLoopInfo.cpp | 5 +++++
 2 files changed, 6 insertions(+), 2 deletions(-)

diff --git a/llvm/lib/CodeGen/MachineLICM.cpp b/llvm/lib/CodeGen/MachineLICM.cpp
index 86eb259c09015..73604809fee06 100644
--- a/llvm/lib/CodeGen/MachineLICM.cpp
+++ b/llvm/lib/CodeGen/MachineLICM.cpp
@@ -1018,8 +1018,7 @@ bool MachineLICMBase::IsLICMCandidate(MachineInstr &I, MachineLoop *CurLoop) {
   // Loads from constant memory are safe to speculate, for example indexed load
   // from a jump table.
   // Stores and side effects are already checked by isSafeToMove.
-  if (I.mayLoad() && !mayLoadFromGOTOrConstantPool(I) &&
-      !IsGuaranteedToExecute(I.getParent(), CurLoop)) {
+  if (I.mayLoad() && !IsGuaranteedToExecute(I.getParent(), CurLoop)) {
     LLVM_DEBUG(dbgs() << "LICM: Load not guaranteed to execute.\n");
     return false;
   }
diff --git a/llvm/lib/CodeGen/MachineLoopInfo.cpp b/llvm/lib/CodeGen/MachineLoopInfo.cpp
index 1019c53e57c6f..f2844caad8a75 100644
--- a/llvm/lib/CodeGen/MachineLoopInfo.cpp
+++ b/llvm/lib/CodeGen/MachineLoopInfo.cpp
@@ -223,6 +223,11 @@ bool MachineLoop::isLoopInvariant(MachineInstr &I,
   const TargetRegisterInfo *TRI = ST.getRegisterInfo();
   const TargetInstrInfo *TII = ST.getInstrInfo();
 
+  // TODO: If the address of a load is loop-invariant and doesn't alias any store in
+  // the loop then it is loop-invariant. For now only handle constant loads.
+  if (I.mayLoad() && !mayLoadFromGOTOrConstantPool(I))
+    return false;
+
   // The instruction is loop invariant if all of its operands are.
   for (const MachineOperand &MO : I.operands()) {
     if (!MO.isReg())



More information about the llvm-commits mailing list