[llvm] [InstCombine] Fold comparison of adding two z/sext booleans (PR #67895)
Yingwei Zheng via llvm-commits
llvm-commits at lists.llvm.org
Mon Oct 2 10:01:50 PDT 2023
================
@@ -879,7 +879,8 @@ define i1 @zext_sext_add_icmp_slt_minus_1_no_oneuse(i1 %a, i1 %b) {
; CHECK-NEXT: [[SEXT_B:%.*]] = sext i1 [[B:%.*]] to i8
; CHECK-NEXT: [[ADD:%.*]] = add nsw i8 [[ZEXT_A]], [[SEXT_B]]
; CHECK-NEXT: call void @use(i8 [[ADD]])
-; CHECK-NEXT: ret i1 false
+; CHECK-NEXT: [[R:%.*]] = icmp slt i8 [[ADD]], -1
----------------
dtcxzyw wrote:
```
static Value *createLogicFromTable(const std::bitset<4> &Table, Value *Op0,
Value *Op1, IRBuilderBase &Builder, bool HasOneUse) {
switch (Table.to_ulong()) {
case 0: // 0 0 0 0
return Builder.getFalse();
case 1: // 0 0 0 1
return HasOneUse ? Builder.CreateNot(Builder.CreateOr(Op0, Op1)) : nullptr;
case 2: // 0 0 1 0
return HasOneUse ? Builder.CreateAnd(Builder.CreateNot(Op0), Op1) : nullptr;
case 3: // 0 0 1 1
return Builder.CreateNot(Op0);
case 4: // 0 1 0 0
return HasOneUse ? Builder.CreateAnd(Op0, Builder.CreateNot(Op1)) : nullptr;
case 5: // 0 1 0 1
return Builder.CreateNot(Op1);
case 6: // 0 1 1 0
return Builder.CreateXor(Op0, Op1);
case 7: // 0 1 1 1
return HasOneUse ? Builder.CreateNot(Builder.CreateAnd(Op0, Op1)) : nullptr;
case 8: // 1 0 0 0
return Builder.CreateAnd(Op0, Op1);
case 9: // 1 0 0 1
return HasOneUse ? Builder.CreateNot(Builder.CreateXor(Op0, Op1)) : nullptr;
case 10: // 1 0 1 0
return Op1;
case 11: // 1 0 1 1
return HasOneUse ? Builder.CreateOr(Builder.CreateNot(Op0), Op1) : nullptr;
case 12: // 1 1 0 0
return Op0;
case 13: // 1 1 0 1
return HasOneUse ? Builder.CreateOr(Op0, Builder.CreateNot(Op1)) : nullptr;
case 14: // 1 1 1 0
return Builder.CreateOr(Op0, Op1);
case 15: // 1 1 1 1
return Builder.getTrue();
default:
llvm_unreachable("Invalid Operation");
}
return nullptr;
}
```
```
if (match(Add, m_Add(
m_CombineAnd(m_Instruction(Ext0),
m_OneUse(m_ZExtOrSExt(m_Value(Op0)))),
m_CombineAnd(m_Instruction(Ext1),
m_OneUse(m_ZExtOrSExt(m_Value(Op1)))))) &&
```
```
if (auto *Cond = createLogicFromTable(Table, Op0, Op1, Builder, Add->hasOneUse()))
return replaceInstUsesWith(Cmp, Cond);
```
Then we can apply these optimizations for adds with multi-use without penalty.
https://github.com/llvm/llvm-project/pull/67895
More information about the llvm-commits
mailing list