[llvm] 0a35232 - [MC] Assert that MCRegUnitIterator operates over MCRegisters

Mircea Trofin via llvm-commits llvm-commits at lists.llvm.org
Sat Oct 3 13:18:39 PDT 2020


Author: Mircea Trofin
Date: 2020-10-03T13:18:25-07:00
New Revision: 0a3523299dec61f2e6eb2a28fdecd25360e8b6d8

URL: https://github.com/llvm/llvm-project/commit/0a3523299dec61f2e6eb2a28fdecd25360e8b6d8
DIFF: https://github.com/llvm/llvm-project/commit/0a3523299dec61f2e6eb2a28fdecd25360e8b6d8.diff

LOG: [MC] Assert that MCRegUnitIterator operates over MCRegisters

The signature of the ctor expects a MCRegister, but currently any
unsigned value can be converted to a MCRegister.

This patch checks that indeed the provided value is a physical register
only. We want to eventually stop implicitly converting unsigned or
Register to MCRegister (which is incorrect). The next step after this
patch is changing uses of MCRegUnitIterator to explicitly cast Register
or unsigned values to MCRegister. To that end, this patch also
introduces 2 APIs that make that conversion checked and explicit.

Differential Revision: https://reviews.llvm.org/D88705

Added: 
    

Modified: 
    llvm/include/llvm/CodeGen/Register.h
    llvm/include/llvm/MC/MCRegister.h
    llvm/include/llvm/MC/MCRegisterInfo.h

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/CodeGen/Register.h b/llvm/include/llvm/CodeGen/Register.h
index 884c8bc7dc2e..86dde8ed2903 100644
--- a/llvm/include/llvm/CodeGen/Register.h
+++ b/llvm/include/llvm/CodeGen/Register.h
@@ -110,6 +110,15 @@ class Register {
     return MCRegister(Reg);
   }
 
+  /// Utility to check-convert this value to a MCRegister. The caller is
+  /// expected to have already validated that this Register is, indeed,
+  /// physical.
+  MCRegister asMCReg() const {
+    assert(Reg == MCRegister::NoRegister ||
+           MCRegister::isPhysicalRegister(Reg));
+    return MCRegister(Reg);
+  }
+
   bool isValid() const { return Reg != MCRegister::NoRegister; }
 
   /// Comparisons between register objects

diff  --git a/llvm/include/llvm/MC/MCRegister.h b/llvm/include/llvm/MC/MCRegister.h
index 5f2e31b70fd8..21ffe28ef6a7 100644
--- a/llvm/include/llvm/MC/MCRegister.h
+++ b/llvm/include/llvm/MC/MCRegister.h
@@ -68,6 +68,12 @@ class MCRegister {
     return Reg;
   }
 
+  /// Check the provided unsigned value is a valid MCRegister.
+  static MCRegister from(unsigned Val) {
+    assert(Val == NoRegister || isPhysicalRegister(Val));
+    return MCRegister(Val);
+  }
+
   unsigned id() const {
     return Reg;
   }

diff  --git a/llvm/include/llvm/MC/MCRegisterInfo.h b/llvm/include/llvm/MC/MCRegisterInfo.h
index 9864d95d19e0..0c1ac6254ec1 100644
--- a/llvm/include/llvm/MC/MCRegisterInfo.h
+++ b/llvm/include/llvm/MC/MCRegisterInfo.h
@@ -675,6 +675,7 @@ class MCRegUnitIterator : public MCRegisterInfo::DiffListIterator {
 
   MCRegUnitIterator(MCRegister Reg, const MCRegisterInfo *MCRI) {
     assert(Reg && "Null register has no regunits");
+    assert(MCRegister::isPhysicalRegister(Reg.id()));
     // Decode the RegUnits MCRegisterDesc field.
     unsigned RU = MCRI->get(Reg).RegUnits;
     unsigned Scale = RU & 15;


        


More information about the llvm-commits mailing list