[PATCH] [CGP] Fix a crash when "Result" is a nullptr.

Joey Gouly joey.gouly at gmail.com
Tue May 13 08:07:17 PDT 2014


r205941 changed the logic, so that a cast happens *before* 'Result' is compared to 'AddrMode.BaseReg'.
In the case that 'AddrMode.BaseReg' is nullptr, 'Result' will also be nullptr, so the cast causes an assertion.
We should use dyn_cast_or_null here to check 'Result' is not null and it is an instruction.

Bug found by Mats Petersson, and I reduced his IR to get a test case.

Also r205941 didn't have a test case, so it'd be good to get one for that commit too.

http://reviews.llvm.org/D3746

Files:
  lib/CodeGen/CodeGenPrepare.cpp
  test/CodeGen/X86/codegen-prepare-crash.ll

Index: lib/CodeGen/CodeGenPrepare.cpp
===================================================================
--- lib/CodeGen/CodeGenPrepare.cpp
+++ lib/CodeGen/CodeGenPrepare.cpp
@@ -2759,7 +2759,7 @@
         // the original IR value was tossed in favor of a constant back when
         // the AddrMode was created we need to bail out gracefully if widths
         // do not match instead of extending it.
-        Instruction *I = dyn_cast<Instruction>(Result);
+        Instruction *I = dyn_cast_or_null<Instruction>(Result);
         if (I && (Result != AddrMode.BaseReg))
           I->eraseFromParent();
         return false;
Index: test/CodeGen/X86/codegen-prepare-crash.ll
===================================================================
--- /dev/null
+++ test/CodeGen/X86/codegen-prepare-crash.ll
@@ -0,0 +1,14 @@
+; RUN: llc < %s
+target triple = "x86_64-unknown-linux-gnu"
+
+ at g = external global [10 x i32]
+
+define void @f(i32 %u) {
+  %1 = add i32 %u, 4
+  br label %P.Proc8.exit
+
+P.Proc8.exit:
+  %valueindex35.i = getelementptr [10 x i32]* @g, i32 0, i32 %1
+  store i32 %u, i32* %valueindex35.i
+  ret void
+}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D3746.9353.patch
Type: text/x-patch
Size: 1131 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20140513/0b0c6ee0/attachment.bin>


More information about the llvm-commits mailing list