llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[DSE] Case of invalid dead store elimination

Open mariannems opened this issue 4 years ago • 8 comments

See the following IR. When running opt -dse, the second store inside the loop is eliminated. That is not valid, since the second loop loads and uses the value.

target datalayout = "E-m:e-p:32:32-i64:32-f64:32:64-a:0:32-n32-S32"

%struct.ilist = type { i32, %struct.ilist* }

@array = dso_local local_unnamed_addr global [100 x i32] [i32 14, i32 66, i32 12, i32 41, i32 86, i32 69, i32 19, i32 77, i32 68, i32 38, i32 26, i32 42, i32 37, i32 23, i32 17, i32 29, i32 55, i32 13, i32 90, i32 92, i32 76, i32 99, i32 10, i32 54, i32 57, i32 83, i32 40, i32 44, i32 75, i32 33, i32 24, i32 28, i32 80, i32 18, i32 78, i32 32, i32 93, i32 89, i32 52, i32 11, i32 21, i32 96, i32 50, i32 15, i32 48, i32 63, i32 87, i32 20, i32 8, i32 85, i32 43, i32 16, i32 94, i32 88, i32 53, i32 84, i32 74, i32 91, i32 67, i32 36, i32 95, i32 61, i32 64, i32 5, i32 30, i32 82, i32 72, i32 46, i32 59, i32 9, i32 7, i32 3, i32 39, i32 31, i32 4, i32 73, i32 70, i32 60, i32 58, i32 81, i32 56, i32 51, i32 45, i32 1, i32 6, i32 49, i32 27, i32 47, i32 34, i32 35, i32 62, i32 97, i32 2, i32 79, i32 98, i32 25, i32 22, i32 65, i32 71, i32 0], align 4

; Function Attrs: nofree nounwind optsize
define dso_local void @main() local_unnamed_addr #0 {
  br label %5

1:                                                ; preds = %5
  %2 = bitcast i8* %8 to %struct.ilist*
  %3 = getelementptr inbounds %struct.ilist, %struct.ilist* %2, i32 0, i32 1
  store %struct.ilist* null, %struct.ilist** %3, align 4
  %4 = icmp eq %struct.ilist* %7, null
  br i1 %4, label %23, label %16

5:                                                ; preds = %5, %0
  %6 = phi i32 [ 0, %0 ], [ %14, %5 ]
  %7 = phi %struct.ilist* [ null, %0 ], [ %9, %5 ]
  %8 = tail call align 8 dereferenceable_or_null(8) i8* @malloc(i32 8) #2
  %9 = bitcast i8* %8 to %struct.ilist*
  %10 = getelementptr inbounds [100 x i32], [100 x i32]* @array, i32 0, i32 %6
  %11 = load i32, i32* %10, align 4
  %12 = getelementptr inbounds %struct.ilist, %struct.ilist* %9, i32 0, i32 0
  store i32 %11, i32* %12, align 8
  %13 = getelementptr inbounds %struct.ilist, %struct.ilist* %9, i32 0, i32 1
  store %struct.ilist* %7, %struct.ilist** %13, align 4
  %14 = add nuw nsw i32 %6, 1
  %15 = icmp eq i32 %14, 100
  br i1 %15, label %1, label %5

16:                                               ; preds = %16, %1
  %17 = phi %struct.ilist* [ %18, %16 ], [ %2, %1 ]
  %18 = phi %struct.ilist* [ %20, %16 ], [ %7, %1 ]
  %19 = getelementptr inbounds %struct.ilist, %struct.ilist* %18, i32 0, i32 1
  %20 = load %struct.ilist*, %struct.ilist** %19, align 4
  %21 = getelementptr inbounds %struct.ilist, %struct.ilist* %18, i32 0, i32 1
  store %struct.ilist* %17, %struct.ilist** %21, align 4
  %22 = icmp eq %struct.ilist* %20, null
  br i1 %22, label %23, label %16

23:                                               ; preds = %16, %1
  ret void
}

; Function Attrs: inaccessiblememonly mustprogress nofree nounwind optsize willreturn
declare dso_local noalias noundef align 8 i8* @malloc(i32 noundef) local_unnamed_addr #1

attributes #0 = { nofree nounwind optsize "frame-pointer"="all" "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" }
attributes #1 = { inaccessiblememonly mustprogress nofree nounwind optsize willreturn "frame-pointer"="all" "no-trapping-math"="true" "stack-protector-buffer-size"="8" }
attributes #2 = { optsize }

mariannems avatar Dec 17 '21 13:12 mariannems

Patch for review: https://reviews.llvm.org/D115965

fhahn avatar Dec 21 '21 20:12 fhahn

Fixed by df59056 and 90d1786.

mariannems avatar Jan 03 '22 15:01 mariannems

This bug was first discovered in release 13 branch. Should the commits be merged there ? @tstellar ?

mariannems avatar Jan 03 '22 15:01 mariannems

Yes, a backport would probably make sense if there is still time.

nikic avatar Jan 05 '22 07:01 nikic

/cherry-pick df59056 90d1786

Not sure if that works or not...

nikic avatar Jan 05 '22 07:01 nikic

This fix does not apply cleanly to the release branch. Could someone resolve the merge conflicts and push it to a github fork.

tstellar avatar Jan 05 '22 18:01 tstellar

It seems to apply correctly on branch 13.x when I apply the 2 commits in order. Here it is a fork with the cherry-picks applied : https://github.com/mariannems/llvm-project/tree/release/13.x

mariannems avatar Jan 05 '22 19:01 mariannems

Merged: 426297cce9d5 33f7aa65f5d7

tstellar avatar Jan 07 '22 05:01 tstellar