Skip to content

[SimplifyCFG] Hoist implying condition #155986

@nikic

Description

@nikic

https://alive2.llvm.org/ce/z/Dj_5qY

define i32 @src(ptr %contents.0, i64 %contents.1) {
start:
  %cmp1 = icmp ugt i64 %contents.1, 7
  br i1 %cmp1, label %if, label %exit

if:
  %load = load i64, ptr %contents.0
  %cmp2 = icmp eq i64 %load, 123
  %cmp3 = icmp eq i64 %contents.1, 16
  %and = and i1 %cmp2, %cmp3
  br i1 %and, label %if2, label %exit

if2:
  ret i32 1

exit:
  ret i32 0
}

define i32 @tgt(ptr %contents.0, i64 %contents.1) {
start:
  %cmp1 = icmp eq i64 %contents.1, 16
  br i1 %cmp1, label %if, label %exit

if:
  %load = load i64, ptr %contents.0
  %cmp2 = icmp eq i64 %load, 123
  %cmp3 = icmp eq i64 %contents.1, 16
  %and = and i1 %cmp2, true
  br i1 %and, label %if2, label %exit

if2:
  ret i32 1

exit:
  ret i32 0
}

In this example, %if2 is taken if %cmp1, %cmp2 and %cmp3 are all true, otherwise %exit is taken. However, %cmp3 implies %cmp1. So we can check %cmp3 first. This means that the %if block is not executed anymore in more cases, so it's only possible if it is side-effect free.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions