llvm / llvm/llvm-project

Missed Redundant Loop Elimination Caused by Always-Null Internal Global Initialization

Open
#188,912 1 comment 0 reactions 0 assignees View on GitHub
llvm:optimizations missed-optimization
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

The following reduced IR is derived from https://github.com/torvalds/linux/blob/0138af2472dfdef0d56fc4697416eaa0ff2589bd/drivers/input/serio/i8042.c#L1582.

https://godbolt.org/z/j5jEb36sj

In the reduced IR, the condition `%5 = icmp eq ptr %4, null` is statically always `true`. This is because `@i8042_ports` is an _internal_ global initialized with zeroinitializer, meaning the pointer field is guaranteed to be null at program start and cannot be modified externally.

```llvm
%struct.i8042_port = type { ptr, i32, i8, i8, i8 }
@i8042_ports = internal unnamed_addr global [6 x %struct.i8042_port] zeroinitializer
define void @i8042_remove() local_unnamed_addr #0 {
br label %1

1: ; preds = %7, %0
%2 = phi i64 [ 0, %0 ], [ %8, %7 ]
%3 = getelementptr [6 x %struct.i8042_port], ptr @i8042_ports, i64 0, i64 %2
%4 = load ptr, ptr %3, align 16
%5 = icmp eq ptr %4, null
br i1 %5, label %7, label %6

6: ; preds = %1
store ptr null, ptr @i8042_ports, align 16
br label %7

7: ; preds = %6, %1
%8 = add nuw nsw i64 %2, 1
%9 = icmp eq i64 %8, 6
br i1 %9, label %10, label %1, !llvm.loop !0

10: ; preds = %7
ret void
}
```
Expected:
```llvm
define void @i8042_remove() local_unnamed_addr #0 {
ret void
}
```

Contributor guide

Open the contributing guide

Research direction

Reproduce the reduced LLVM IR in the linked Godbolt example and compare the optimizer output with the expected `ret void`. Use the referenced Linux source location, drivers/input/serio/i8042.c:1582, to understand the originating pattern; done means the redundant loop and conditional work are eliminated for this case.

Written by the indexing model from the issue text.

Assessment

Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.