[PATCH] D17154: Bug fix: check nullptr before call dyn_cast

Lawrence Hu via llvm-commits llvm-commits at lists.llvm.org
Thu Feb 11 10:12:55 PST 2016


hulx2000 created this revision.
hulx2000 added reviewers: joker-eph, atrick, qcolombet, hfinkel.
hulx2000 added a subscriber: llvm-commits.
hulx2000 set the repository for this revision to rL LLVM.

My earlier failed to check null ptr before dyn_cast.

Repository:
  rL LLVM

http://reviews.llvm.org/D17154

Files:
  lib/Transforms/Scalar/SeparateConstOffsetFromGEP.cpp
  test/CodeGen/AArch64/gep-nullptr.ll

Index: test/CodeGen/AArch64/gep-nullptr.ll
===================================================================
--- /dev/null
+++ test/CodeGen/AArch64/gep-nullptr.ll
@@ -0,0 +1,31 @@
+; RUN: llc -O3 -aarch64-gep-opt=true   < %s
+; REQUIRES: asserts
+target datalayout = "e-m:e-i64:64-i128:128-n8:16:32:64-S128"
+target triple = "aarch64--linux-gnu"
+
+%struct.b_ctxt_t.23.226.487.516.545.661.690.719.748.777.1038.1219 = type { i8, i8, i8, i8, i8, i8, [4 x i8], i8, i8, [2 x i32], [2 x %union.MV.9.212.473.502.531.647.676.705.734.763.1024.1218], [4 x [2 x %union.MV.9.212.473.502.531.647.676.705.734.763.1024.1218]], [4 x [2 x %union.MV.9.212.473.502.531.647.676.705.734.763.1024.1218]], [4 x i8], i8*, i8*, i32, i8* }
+%union.MV.9.212.473.502.531.647.676.705.734.763.1024.1218 = type { i32 }
+
+; Function Attrs: argmemonly nounwind
+declare void @llvm.lifetime.end(i64, i8* nocapture)
+
+; Function Attrs: nounwind
+define void @test(%struct.b_ctxt_t.23.226.487.516.545.661.690.719.748.777.1038.1219* %mi_block) #1 {
+entry:
+  br i1 undef, label %for.body13.us, label %if.else
+
+; CHECK:  .text
+for.body13.us:                                    ; preds = %entry
+  %indvars.iv.next40 = or i64 0, 1
+  %packed4.i.us.1 = getelementptr inbounds %struct.b_ctxt_t.23.226.487.516.545.661.690.719.748.777.1038.1219, %struct.b_ctxt_t.23.226.487.516.545.661.690.719.748.777.1038.1219* %mi_block, i64 0, i32 11, i64 0, i64 %indvars.iv.next40, i32 0
+  call void @llvm.lifetime.end(i64 8, i8* nonnull undef) #2
+  unreachable
+
+if.else:                                          ; preds = %entry
+  ret void
+}
+
+attributes #0 = { argmemonly nounwind }
+attributes #1 = { nounwind "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf" "no-infs-fp-math"="true" "no-nans-fp-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="cortex-a57" "target-features"="+crc,+crypto,+neon" "unsafe-fp-math"="true" "use-soft-float"="false" }
+attributes #2 = { nounwind }
+
Index: lib/Transforms/Scalar/SeparateConstOffsetFromGEP.cpp
===================================================================
--- lib/Transforms/Scalar/SeparateConstOffsetFromGEP.cpp
+++ lib/Transforms/Scalar/SeparateConstOffsetFromGEP.cpp
@@ -824,10 +824,12 @@
   // If we created a GEP with constant index, and the base is loop invariant,
   // then we swap the first one with it, so LICM can move constant GEP out
   // later.
-  GetElementPtrInst *FirstGEP = dyn_cast<GetElementPtrInst>(FirstResult);
-  GetElementPtrInst *SecondGEP = dyn_cast<GetElementPtrInst>(ResultPtr);
-  if (isSwapCandidate && isLegalToSwapOperand(FirstGEP, SecondGEP, L))
-    swapGEPOperand(FirstGEP, SecondGEP);
+  if (FirstResult && ResultPtr) {
+    GetElementPtrInst *FirstGEP = dyn_cast<GetElementPtrInst>(FirstResult);
+    GetElementPtrInst *SecondGEP = dyn_cast<GetElementPtrInst>(ResultPtr);
+    if (isSwapCandidate && isLegalToSwapOperand(FirstGEP, SecondGEP, L))
+      swapGEPOperand(FirstGEP, SecondGEP);
+  }
 
   if (ResultPtr->getType() != Variadic->getType())
     ResultPtr = Builder.CreateBitCast(ResultPtr, Variadic->getType());


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D17154.47676.patch
Type: text/x-patch
Size: 3160 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160211/e1fad732/attachment.bin>


More information about the llvm-commits mailing list