Index: llvm/lib/Transforms/Scalar/GVNHoist.cpp =================================================================== --- llvm/lib/Transforms/Scalar/GVNHoist.cpp +++ llvm/lib/Transforms/Scalar/GVNHoist.cpp @@ -502,12 +502,12 @@ // Return true when it is safe to hoist scalar instructions from all blocks in // WL to HoistBB. - bool safeToHoistScalar(const BasicBlock *HoistBB, + bool safeToHoistScalar(Instruction *I, const BasicBlock *HoistBB, SmallPtrSetImpl &WL, int &NBBsOnAllPaths) { // Enable scalar hoisting at -Oz as it is safe to hoist scalars to a place // where they are partially needed. - if (OptForMinSize) + if (OptForMinSize && isSafeToSpeculativelyExecute(I)) return true; // Check that the hoisted expression is needed on all paths. @@ -574,7 +574,7 @@ WL.insert(BB); if (K == InsKind::Scalar) { - if (safeToHoistScalar(NewHoistBB, WL, NumBBsOnAllPaths)) { + if (safeToHoistScalar(Insn, NewHoistBB, WL, NumBBsOnAllPaths)) { // Extend HoistPt to NewHoistPt. HoistPt = NewHoistPt; HoistBB = NewHoistBB; Index: llvm/test/Transforms/GVNHoist/hoist-unsafe-pr31729.ll =================================================================== --- /dev/null +++ llvm/test/Transforms/GVNHoist/hoist-unsafe-pr31729.ll @@ -0,0 +1,163 @@ +; RUN: opt -tti -tbaa -scoped-noalias -assumption-cache-tracker -targetlibinfo -verify -simplifycfg -domtree -sroa -early-cse -basicaa -aa -memdep -memoryssa -gvn-hoist -S < %s | FileCheck %s + +; CHECK-LABEL: @main +; CHECK: urem +; CHECK: urem +; CHECK: urem + +; ModuleID = 'reduced.c' +source_filename = "reduced.c" +target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" +target triple = "x86_64-unknown-linux-gnu" + +@g_x_s = global i32 -470211272, align 4 +@g_z_s = global i32 2007237709, align 4 +@g_x_u = global i32 282475249, align 4 +@g_z_u = global i32 984943658, align 4 +@g_m = global i32 16807, align 4 +@.str = private unnamed_addr constant [10 x i8] c"All good\0A\00", align 1 +@res = common global i32 0, align 4 + +; Function Attrs: minsize noinline nounwind optsize uwtable +define i64 @func() #0 { +entry: + ret i64 1 +} + +; Function Attrs: minsize nounwind optsize uwtable +define i32 @main() #1 { +entry: + %retval = alloca i32, align 4 + %x_s = alloca i32, align 4 + %z_s = alloca i32, align 4 + %x_u = alloca i32, align 4 + %z_u = alloca i32, align 4 + %m = alloca i32, align 4 + %cleanup.dest.slot = alloca i32 + store i32 0, i32* %retval, align 4 + %0 = bitcast i32* %x_s to i8* + call void @llvm.lifetime.start(i64 4, i8* %0) #4 + %1 = load volatile i32, i32* @g_x_s, align 4, !tbaa !1 + store i32 %1, i32* %x_s, align 4, !tbaa !1 + %2 = bitcast i32* %z_s to i8* + call void @llvm.lifetime.start(i64 4, i8* %2) #4 + %3 = load volatile i32, i32* @g_z_s, align 4, !tbaa !1 + store i32 %3, i32* %z_s, align 4, !tbaa !1 + %4 = bitcast i32* %x_u to i8* + call void @llvm.lifetime.start(i64 4, i8* %4) #4 + %5 = load volatile i32, i32* @g_x_u, align 4, !tbaa !1 + store i32 %5, i32* %x_u, align 4, !tbaa !1 + %6 = bitcast i32* %z_u to i8* + call void @llvm.lifetime.start(i64 4, i8* %6) #4 + %7 = load volatile i32, i32* @g_z_u, align 4, !tbaa !1 + store i32 %7, i32* %z_u, align 4, !tbaa !1 + %8 = bitcast i32* %m to i8* + call void @llvm.lifetime.start(i64 4, i8* %8) #4 + %9 = load volatile i32, i32* @g_m, align 4, !tbaa !1 + store i32 %9, i32* %m, align 4, !tbaa !1 + %call = call i64 @func() #5 + %10 = load i32, i32* %z_s, align 4, !tbaa !1 + %conv = sext i32 %10 to i64 + %cmp = icmp ne i64 %call, %conv + br i1 %cmp, label %if.end, label %lor.lhs.false + +lor.lhs.false: ; preds = %entry + %11 = load i32, i32* %x_s, align 4, !tbaa !1 + %12 = load i32, i32* %m, align 4, !tbaa !1 + %13 = load i32, i32* %z_s, align 4, !tbaa !1 + %div = udiv i32 %12, %13 + %rem = urem i32 %11, %div + %cmp2 = icmp eq i32 %rem, 0 + br i1 %cmp2, label %if.end, label %if.then + +if.then: ; preds = %lor.lhs.false + store i32 1, i32* %retval, align 4 + store i32 1, i32* %cleanup.dest.slot, align 4 + br label %cleanup + +if.end: ; preds = %lor.lhs.false, %entry + %call4 = call i64 @func() #5 + %14 = load i32, i32* %z_u, align 4, !tbaa !1 + %conv5 = zext i32 %14 to i64 + %cmp6 = icmp ne i64 %call4, %conv5 + br i1 %cmp6, label %if.end14, label %lor.lhs.false8 + +lor.lhs.false8: ; preds = %if.end + %15 = load i32, i32* %x_s, align 4, !tbaa !1 + %16 = load i32, i32* %m, align 4, !tbaa !1 + %17 = load i32, i32* %z_u, align 4, !tbaa !1 + %div9 = udiv i32 %16, %17 + %rem10 = urem i32 %15, %div9 + %cmp11 = icmp eq i32 %rem10, 0 + br i1 %cmp11, label %if.end14, label %if.then13 + +if.then13: ; preds = %lor.lhs.false8 + store i32 1, i32* %retval, align 4 + store i32 1, i32* %cleanup.dest.slot, align 4 + br label %cleanup + +if.end14: ; preds = %lor.lhs.false8, %if.end + %call15 = call i64 @func() #5 + %18 = load i32, i32* %z_s, align 4, !tbaa !1 + %conv16 = sext i32 %18 to i64 + %cmp17 = icmp ne i64 %call15, %conv16 + br i1 %cmp17, label %if.end25, label %lor.lhs.false19 + +lor.lhs.false19: ; preds = %if.end14 + %19 = load i32, i32* %x_s, align 4, !tbaa !1 + %20 = load i32, i32* %m, align 4, !tbaa !1 + %21 = load i32, i32* %z_s, align 4, !tbaa !1 + %div20 = udiv i32 %20, %21 + %rem21 = urem i32 %19, %div20 + %cmp22 = icmp eq i32 %rem21, 0 + br i1 %cmp22, label %if.end25, label %if.then24 + +if.then24: ; preds = %lor.lhs.false19 + store i32 1, i32* %retval, align 4 + store i32 1, i32* %cleanup.dest.slot, align 4 + br label %cleanup + +if.end25: ; preds = %lor.lhs.false19, %if.end14 + %call26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i32 0, i32 0)) #5 + store i32 0, i32* %retval, align 4 + store i32 1, i32* %cleanup.dest.slot, align 4 + br label %cleanup + +cleanup: ; preds = %if.end25, %if.then24, %if.then13, %if.then + %22 = bitcast i32* %m to i8* + call void @llvm.lifetime.end(i64 4, i8* %22) #4 + %23 = bitcast i32* %z_u to i8* + call void @llvm.lifetime.end(i64 4, i8* %23) #4 + %24 = bitcast i32* %x_u to i8* + call void @llvm.lifetime.end(i64 4, i8* %24) #4 + %25 = bitcast i32* %z_s to i8* + call void @llvm.lifetime.end(i64 4, i8* %25) #4 + %26 = bitcast i32* %x_s to i8* + call void @llvm.lifetime.end(i64 4, i8* %26) #4 + %27 = load i32, i32* %retval, align 4 + ret i32 %27 +} + +; Function Attrs: argmemonly nounwind +declare void @llvm.lifetime.start(i64, i8* nocapture) #2 + +; Function Attrs: minsize optsize +declare i32 @printf(i8*, ...) #3 + +; Function Attrs: argmemonly nounwind +declare void @llvm.lifetime.end(i64, i8* nocapture) #2 + +attributes #0 = { minsize noinline nounwind optsize uwtable "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="false" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+fxsr,+mmx,+sse,+sse2,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" } +attributes #1 = { minsize nounwind optsize uwtable "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="false" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+fxsr,+mmx,+sse,+sse2,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" } +attributes #2 = { argmemonly nounwind } +attributes #3 = { minsize optsize "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="false" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+fxsr,+mmx,+sse,+sse2,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" } +attributes #4 = { nounwind } +attributes #5 = { minsize optsize } + +!llvm.ident = !{!0} + +!0 = !{!"clang version 5.0.0 "} +!1 = !{!2, !2, i64 0} +!2 = !{!"int", !3, i64 0} +!3 = !{!"omnipotent char", !4, i64 0} +!4 = !{!"Simple C/C++ TBAA"}