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(FirstResult); - GetElementPtrInst *SecondGEP = dyn_cast(ResultPtr); - if (isSwapCandidate && isLegalToSwapOperand(FirstGEP, SecondGEP, L)) - swapGEPOperand(FirstGEP, SecondGEP); + if (FirstResult && ResultPtr) { + GetElementPtrInst *FirstGEP = dyn_cast(FirstResult); + GetElementPtrInst *SecondGEP = dyn_cast(ResultPtr); + if (isSwapCandidate && isLegalToSwapOperand(FirstGEP, SecondGEP, L)) + swapGEPOperand(FirstGEP, SecondGEP); + } if (ResultPtr->getType() != Variadic->getType()) ResultPtr = Builder.CreateBitCast(ResultPtr, Variadic->getType()); 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 } +