[llvm] [InstCombine] Convert load from LUT into a select (PR #98339)

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Wed Jul 10 08:51:13 PDT 2024


================
@@ -1,5 +1,5 @@
 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
-; RUN: opt -passes=instcombine -S -data-layout="p:32:32:32-p1:16:16:16-p2:128:128:128:32-n8:16:32:64" < %s | FileCheck %s
+; RUN: opt -passes="instcombine<max-iterations=2>" -S -data-layout="p:32:32:32-p1:16:16:16-p2:128:128:128:32-n8:16:32:64" < %s | FileCheck %s
----------------
dtcxzyw wrote:

IRBuilder fails to constant fold `%cmp1 = icmp ne ptr getelementptr (i8, ptr @g, i64 4), null` to `true`.

```
INSTCOMBINE ITERATION #1 on pr93017
ADD:   ret i1 %cmp
ADD:   %cmp = icmp ne ptr %v, null
ADD:   %v = load ptr, ptr %gep, align 8
ADD:   %gep = getelementptr inbounds [2 x ptr], ptr @table, i64 0, i64 %idx
IC: Visiting:   %gep = getelementptr inbounds [2 x ptr], ptr @table, i64 0, i64 %idx
IC: Visiting:   %v = load ptr, ptr %gep, align 8
ADD DEFERRED:   %1 = icmp eq i64 %idx, 0
ADD DEFERRED:   %2 = select i1 %1, ptr @g, ptr getelementptr (i8, ptr @g, i64 4)
IC: Replacing   %v = load ptr, ptr %gep, align 8
    with   %2 = select i1 %1, ptr @g, ptr getelementptr (i8, ptr @g, i64 4)
IC: Mod =   %v = load ptr, ptr %gep, align 8
    New =   %2 = load ptr, ptr %gep, align 8
IC: ERASE   %2 = load ptr, ptr %gep, align 8
ADD DEFERRED:   %gep = getelementptr inbounds [2 x ptr], ptr @table, i64 0, i64 %idx
IC: ERASE   %gep = getelementptr inbounds [2 x ptr], ptr @table, i64 0, i64 %idx
ADD:   %v = select i1 %1, ptr @g, ptr getelementptr (i8, ptr @g, i64 4)
ADD:   %1 = icmp eq i64 %idx, 0
IC: Visiting:   %1 = icmp eq i64 %idx, 0
IC: Visiting:   %v = select i1 %1, ptr @g, ptr getelementptr (i8, ptr @g, i64 4)
IC: Visiting:   %cmp = icmp ne ptr %v, null
ADD DEFERRED:   %cmp1 = icmp ne ptr getelementptr (i8, ptr @g, i64 4), null
IC: Old =   %cmp = icmp ne ptr %v, null
    New =   <badref> = select i1 %1, i1 true, i1 %cmp1
ADD:   %cmp = select i1 %1, i1 true, i1 %cmp1
IC: ERASE   %2 = icmp ne ptr %v, null
ADD DEFERRED:   %v = select i1 %1, ptr @g, ptr getelementptr (i8, ptr @g, i64 4)
IC: ERASE   %v = select i1 %1, ptr @g, ptr getelementptr (i8, ptr @g, i64 4)
ADD DEFERRED:   %1 = icmp eq i64 %idx, 0
ADD DEFERRED:   %cmp = select i1 %1, i1 true, i1 %cmp1
ADD:   %1 = icmp eq i64 %idx, 0
ADD:   %cmp1 = icmp ne ptr getelementptr (i8, ptr @g, i64 4), null
IC: Visiting:   %cmp1 = icmp ne ptr getelementptr (i8, ptr @g, i64 4), null
IC: Visiting:   %1 = icmp eq i64 %idx, 0
IC: Visiting:   %cmp = select i1 %1, i1 true, i1 %cmp1
IC: Old =   %cmp = select i1 %1, i1 true, i1 %cmp1
    New =   <badref> = or i1 %1, %cmp1
ADD:   %cmp = or i1 %1, %cmp1
IC: ERASE   %2 = select i1 %1, i1 true, i1 %cmp1
ADD DEFERRED:   %1 = icmp eq i64 %idx, 0
ADD DEFERRED:   %cmp = or i1 %1, %cmp1
ADD DEFERRED:   %cmp1 = icmp ne ptr getelementptr (i8, ptr @g, i64 4), null
ADD:   %cmp1 = icmp ne ptr getelementptr (i8, ptr @g, i64 4), null
ADD:   %1 = icmp eq i64 %idx, 0
IC: Visiting:   %1 = icmp eq i64 %idx, 0
IC: Visiting:   %cmp1 = icmp ne ptr getelementptr (i8, ptr @g, i64 4), null
IC: Visiting:   %cmp = or i1 %1, %cmp1
IC: Visiting:   ret i1 %cmp


INSTCOMBINE ITERATION #2 on pr93017
IC: ConstFold to: i1 true from:   %cmp1 = icmp ne ptr getelementptr (i8, ptr @g, i64 4), null
ADD:   ret i1 %cmp
ADD:   %cmp = or i1 %1, true
ADD:   %1 = icmp eq i64 %idx, 0
IC: Visiting:   %1 = icmp eq i64 %idx, 0
IC: Visiting:   %cmp = or i1 %1, true
IC: Replacing   %cmp = or i1 %1, true
    with i1 true
IC: Mod =   %cmp = or i1 %1, true
    New =   %cmp = or i1 %1, true
IC: ERASE   %cmp = or i1 %1, true
ADD DEFERRED:   %1 = icmp eq i64 %idx, 0
IC: ERASE   %1 = icmp eq i64 %idx, 0
IC: Visiting:   ret i1 true
LLVM ERROR: Instruction Combining did not reach a fixpoint after 1 iterations
```

https://github.com/llvm/llvm-project/pull/98339


More information about the llvm-commits mailing list