[llvm] [CodeGen] Fix off-by-one in RegAllocHints grow (PR #102273)

Alexis Engelke via llvm-commits llvm-commits at lists.llvm.org
Tue Aug 6 23:26:27 PDT 2024


https://github.com/aengelke created https://github.com/llvm/llvm-project/pull/102273

grow() expects a value in the range, not the size. Fix this off-by-one. Fixup of #102186.

>From f9568e619af5e4fa51dafce58d795914210aed9a Mon Sep 17 00:00:00 2001
From: Alexis Engelke <engelke at in.tum.de>
Date: Wed, 7 Aug 2024 06:15:07 +0000
Subject: [PATCH] [CodeGen] Fix off-by-one in RegAllocHints grow

grow() expects a value in the range, not the size. Fix this off-by-one.
---
 llvm/include/llvm/CodeGen/MachineRegisterInfo.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/llvm/include/llvm/CodeGen/MachineRegisterInfo.h b/llvm/include/llvm/CodeGen/MachineRegisterInfo.h
index 2367d8d04787d..0f641b9e8e902 100644
--- a/llvm/include/llvm/CodeGen/MachineRegisterInfo.h
+++ b/llvm/include/llvm/CodeGen/MachineRegisterInfo.h
@@ -801,7 +801,7 @@ class MachineRegisterInfo {
   /// of an earlier hint it will be overwritten.
   void setRegAllocationHint(Register VReg, unsigned Type, Register PrefReg) {
     assert(VReg.isVirtual());
-    RegAllocHints.grow(Register::index2VirtReg(getNumVirtRegs()));
+    RegAllocHints.grow(Register::index2VirtReg(getNumVirtRegs() - 1));
     RegAllocHints[VReg].first  = Type;
     RegAllocHints[VReg].second.clear();
     RegAllocHints[VReg].second.push_back(PrefReg);
@@ -811,7 +811,7 @@ class MachineRegisterInfo {
   /// vector for VReg.
   void addRegAllocationHint(Register VReg, Register PrefReg) {
     assert(VReg.isVirtual());
-    RegAllocHints.grow(Register::index2VirtReg(getNumVirtRegs()));
+    RegAllocHints.grow(Register::index2VirtReg(getNumVirtRegs() - 1));
     RegAllocHints[VReg].second.push_back(PrefReg);
   }
 



More information about the llvm-commits mailing list