[llvm] r340434 - [X86] In OptimizeLEAs pass, check that the key is in the LEAs map before accessing

Craig Topper via llvm-commits llvm-commits at lists.llvm.org
Wed Aug 22 11:24:13 PDT 2018


Author: ctopper
Date: Wed Aug 22 11:24:13 2018
New Revision: 340434

URL: http://llvm.org/viewvc/llvm-project?rev=340434&view=rev
Log:
[X86] In OptimizeLEAs pass, check that the key is in the LEAs map before accessing

When the key is not already in the map, the access operator[] creates an empty value and grows the map.
Resizing a map is very slow, so this needs to be avoided.

Found with csmith + asserts.

May help with
https://bugs.llvm.org/show_bug.cgi?id=25843

Patch by Tom Rix.

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

Modified:
    llvm/trunk/lib/Target/X86/X86OptimizeLEAs.cpp

Modified: llvm/trunk/lib/Target/X86/X86OptimizeLEAs.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/X86/X86OptimizeLEAs.cpp?rev=340434&r1=340433&r2=340434&view=diff
==============================================================================
--- llvm/trunk/lib/Target/X86/X86OptimizeLEAs.cpp (original)
+++ llvm/trunk/lib/Target/X86/X86OptimizeLEAs.cpp Wed Aug 22 11:24:13 2018
@@ -510,12 +510,16 @@ bool OptimizeLEAPass::removeRedundantAdd
 
     MemOpNo += X86II::getOperandBias(Desc);
 
+    // Do not call chooseBestLEA if there was no matching LEA
+    auto Insns = LEAs.find(getMemOpKey(MI, MemOpNo));
+    if (Insns == LEAs.end())
+      continue;
+
     // Get the best LEA instruction to replace address calculation.
     MachineInstr *DefMI;
     int64_t AddrDispShift;
     int Dist;
-    if (!chooseBestLEA(LEAs[getMemOpKey(MI, MemOpNo)], MI, DefMI, AddrDispShift,
-                       Dist))
+    if (!chooseBestLEA(Insns->second, MI, DefMI, AddrDispShift, Dist))
       continue;
 
     // If LEA occurs before current instruction, we can freely replace




More information about the llvm-commits mailing list