[llvm] 8b8e870 - [PowerPC] Fix a nullptr dereference

Benjamin Kramer via llvm-commits llvm-commits at lists.llvm.org
Tue Nov 16 14:57:44 PST 2021


Author: Benjamin Kramer
Date: 2021-11-16T23:52:42+01:00
New Revision: 8b8e8704cebe2b989cd04938650945c77baf2e01

URL: https://github.com/llvm/llvm-project/commit/8b8e8704cebe2b989cd04938650945c77baf2e01
DIFF: https://github.com/llvm/llvm-project/commit/8b8e8704cebe2b989cd04938650945c77baf2e01.diff

LOG: [PowerPC] Fix a nullptr dereference

LiMI1/LiMI2 can be null, so don't call a method on them before checking.
Found by ubsan.

Added: 
    

Modified: 
    llvm/lib/Target/PowerPC/PPCMIPeephole.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Target/PowerPC/PPCMIPeephole.cpp b/llvm/lib/Target/PowerPC/PPCMIPeephole.cpp
index bd27f6161546..650798453e17 100644
--- a/llvm/lib/Target/PowerPC/PPCMIPeephole.cpp
+++ b/llvm/lib/Target/PowerPC/PPCMIPeephole.cpp
@@ -1022,15 +1022,15 @@ bool PPCMIPeephole::simplifyCode(void) {
       case PPC::TW: {
         MachineInstr *LiMI1 = getVRegDefOrNull(&MI.getOperand(1), MRI);
         MachineInstr *LiMI2 = getVRegDefOrNull(&MI.getOperand(2), MRI);
-        unsigned Opcode1 = LiMI1->getOpcode();
-        unsigned Opcode2 = LiMI2->getOpcode();
         bool IsOperand2Immediate = MI.getOperand(2).isImm();
         // We can only do the optimization if we can get immediates
         // from both operands
-        if (!(LiMI1 && (Opcode1 == PPC::LI || Opcode1 == PPC::LI8)))
+        if (!(LiMI1 && (LiMI1->getOpcode() == PPC::LI ||
+                        LiMI1->getOpcode() == PPC::LI8)))
           break;
         if (!IsOperand2Immediate &&
-            !(LiMI2 && (Opcode2 == PPC::LI || Opcode2 == PPC::LI8)))
+            !(LiMI2 && (LiMI2->getOpcode() == PPC::LI ||
+                        LiMI2->getOpcode() == PPC::LI8)))
           break;
 
         auto ImmOperand0 = MI.getOperand(0).getImm();


        


More information about the llvm-commits mailing list