[llvm] 4aed098 - Expand Div/Rem: consider the case where the dividend is zero

Nuno Lopes via llvm-commits llvm-commits at lists.llvm.org
Thu Sep 1 04:00:21 PDT 2022


Author: Nuno Lopes
Date: 2022-09-01T12:00:03+01:00
New Revision: 4aed09868b5a51a29aade11d9d412c3313310f29

URL: https://github.com/llvm/llvm-project/commit/4aed09868b5a51a29aade11d9d412c3313310f29
DIFF: https://github.com/llvm/llvm-project/commit/4aed09868b5a51a29aade11d9d412c3313310f29.diff

LOG: Expand Div/Rem: consider the case where the dividend is zero
So we can't use ctlz in poison-producing mode

Added: 
    

Modified: 
    llvm/lib/Transforms/Utils/IntegerDivision.cpp
    llvm/test/Transforms/ExpandLargeDivRem/sdiv129.ll
    llvm/test/Transforms/ExpandLargeDivRem/srem129.ll
    llvm/test/Transforms/ExpandLargeDivRem/udiv129.ll
    llvm/test/Transforms/ExpandLargeDivRem/urem129.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/Utils/IntegerDivision.cpp b/llvm/lib/Transforms/Utils/IntegerDivision.cpp
index 1267360b34b18..52a175b4c87c8 100644
--- a/llvm/lib/Transforms/Utils/IntegerDivision.cpp
+++ b/llvm/lib/Transforms/Utils/IntegerDivision.cpp
@@ -148,6 +148,7 @@ static Value *generateUnsignedDivisionCode(Value *Dividend, Value *Divisor,
   ConstantInt *MSB = ConstantInt::get(DivTy, BitWidth - 1);
 
   ConstantInt *True = Builder.getTrue();
+  ConstantInt *False = Builder.getFalse();
 
   BasicBlock *IBB = Builder.GetInsertBlock();
   Function *F = IBB->getParent();
@@ -211,8 +212,8 @@ static Value *generateUnsignedDivisionCode(Value *Dividend, Value *Divisor,
   // ;   %ret0_2      = icmp eq i32 %dividend, 0
   // ;   %ret0_3      = or i1 %ret0_1, %ret0_2
   // ;   %tmp0        = tail call i32 @llvm.ctlz.i32(i32 %divisor, i1 true)
-  // ;   %tmp1        = tail call i32 @llvm.ctlz.i32(i32 %dividend, i1 true)
-  // ;   %sr          = sub nsw i32 %tmp0, %tmp1
+  // ;   %tmp1        = tail call i32 @llvm.ctlz.i32(i32 %dividend, i1 false)
+  // ;   %sr          = sub i32 %tmp0, %tmp1
   // ;   %ret0_4      = icmp ugt i32 %sr, 31
   // ;   %ret0        = or i1 %ret0_3, %ret0_4
   // ;   %retDividend = icmp eq i32 %sr, 31
@@ -224,7 +225,7 @@ static Value *generateUnsignedDivisionCode(Value *Dividend, Value *Divisor,
   Value *Ret0_2      = Builder.CreateICmpEQ(Dividend, Zero);
   Value *Ret0_3      = Builder.CreateOr(Ret0_1, Ret0_2);
   Value *Tmp0 = Builder.CreateCall(CTLZ, {Divisor, True});
-  Value *Tmp1 = Builder.CreateCall(CTLZ, {Dividend, True});
+  Value *Tmp1 = Builder.CreateCall(CTLZ, {Dividend, False});
   Value *SR          = Builder.CreateSub(Tmp0, Tmp1);
   Value *Ret0_4      = Builder.CreateICmpUGT(SR, MSB);
   Value *Ret0        = Builder.CreateOr(Ret0_3, Ret0_4);

diff  --git a/llvm/test/Transforms/ExpandLargeDivRem/sdiv129.ll b/llvm/test/Transforms/ExpandLargeDivRem/sdiv129.ll
index 27ab1e8eb9ed6..2bfad7b6fa839 100644
--- a/llvm/test/Transforms/ExpandLargeDivRem/sdiv129.ll
+++ b/llvm/test/Transforms/ExpandLargeDivRem/sdiv129.ll
@@ -12,7 +12,7 @@ define void @sdiv129(i129* %ptr, i129* %out) nounwind {
 ; CHECK-NEXT:    [[TMP4:%.*]] = icmp eq i129 [[TMP2]], 0
 ; CHECK-NEXT:    [[TMP5:%.*]] = or i1 false, [[TMP4]]
 ; CHECK-NEXT:    [[TMP6:%.*]] = call i129 @llvm.ctlz.i129(i129 3, i1 true)
-; CHECK-NEXT:    [[TMP7:%.*]] = call i129 @llvm.ctlz.i129(i129 [[TMP2]], i1 true)
+; CHECK-NEXT:    [[TMP7:%.*]] = call i129 @llvm.ctlz.i129(i129 [[TMP2]], i1 false)
 ; CHECK-NEXT:    [[TMP8:%.*]] = sub i129 [[TMP6]], [[TMP7]]
 ; CHECK-NEXT:    [[TMP9:%.*]] = icmp ugt i129 [[TMP8]], 128
 ; CHECK-NEXT:    [[TMP10:%.*]] = or i1 [[TMP5]], [[TMP9]]

diff  --git a/llvm/test/Transforms/ExpandLargeDivRem/srem129.ll b/llvm/test/Transforms/ExpandLargeDivRem/srem129.ll
index 08df750a13663..c4326333786fe 100644
--- a/llvm/test/Transforms/ExpandLargeDivRem/srem129.ll
+++ b/llvm/test/Transforms/ExpandLargeDivRem/srem129.ll
@@ -11,7 +11,7 @@ define void @test(i129* %ptr, i129* %out) nounwind {
 ; CHECK-NEXT:    [[TMP3:%.*]] = icmp eq i129 [[TMP2]], 0
 ; CHECK-NEXT:    [[TMP4:%.*]] = or i1 false, [[TMP3]]
 ; CHECK-NEXT:    [[TMP5:%.*]] = call i129 @llvm.ctlz.i129(i129 3, i1 true)
-; CHECK-NEXT:    [[TMP6:%.*]] = call i129 @llvm.ctlz.i129(i129 [[TMP2]], i1 true)
+; CHECK-NEXT:    [[TMP6:%.*]] = call i129 @llvm.ctlz.i129(i129 [[TMP2]], i1 false)
 ; CHECK-NEXT:    [[TMP7:%.*]] = sub i129 [[TMP5]], [[TMP6]]
 ; CHECK-NEXT:    [[TMP8:%.*]] = icmp ugt i129 [[TMP7]], 128
 ; CHECK-NEXT:    [[TMP9:%.*]] = or i1 [[TMP4]], [[TMP8]]

diff  --git a/llvm/test/Transforms/ExpandLargeDivRem/udiv129.ll b/llvm/test/Transforms/ExpandLargeDivRem/udiv129.ll
index 31705a9a35c10..1929963331913 100644
--- a/llvm/test/Transforms/ExpandLargeDivRem/udiv129.ll
+++ b/llvm/test/Transforms/ExpandLargeDivRem/udiv129.ll
@@ -8,7 +8,7 @@ define void @test(i129* %ptr, i129* %out) nounwind {
 ; CHECK-NEXT:    [[TMP0:%.*]] = icmp eq i129 [[A]], 0
 ; CHECK-NEXT:    [[TMP1:%.*]] = or i1 false, [[TMP0]]
 ; CHECK-NEXT:    [[TMP2:%.*]] = call i129 @llvm.ctlz.i129(i129 3, i1 true)
-; CHECK-NEXT:    [[TMP3:%.*]] = call i129 @llvm.ctlz.i129(i129 [[A]], i1 true)
+; CHECK-NEXT:    [[TMP3:%.*]] = call i129 @llvm.ctlz.i129(i129 [[A]], i1 false)
 ; CHECK-NEXT:    [[TMP4:%.*]] = sub i129 [[TMP2]], [[TMP3]]
 ; CHECK-NEXT:    [[TMP5:%.*]] = icmp ugt i129 [[TMP4]], 128
 ; CHECK-NEXT:    [[TMP6:%.*]] = or i1 [[TMP1]], [[TMP5]]

diff  --git a/llvm/test/Transforms/ExpandLargeDivRem/urem129.ll b/llvm/test/Transforms/ExpandLargeDivRem/urem129.ll
index 4e91eef6d2aa6..36ecb990e52f5 100644
--- a/llvm/test/Transforms/ExpandLargeDivRem/urem129.ll
+++ b/llvm/test/Transforms/ExpandLargeDivRem/urem129.ll
@@ -8,7 +8,7 @@ define void @test(i129* %ptr, i129* %out) nounwind {
 ; CHECK-NEXT:    [[TMP0:%.*]] = icmp eq i129 [[A]], 0
 ; CHECK-NEXT:    [[TMP1:%.*]] = or i1 false, [[TMP0]]
 ; CHECK-NEXT:    [[TMP2:%.*]] = call i129 @llvm.ctlz.i129(i129 3, i1 true)
-; CHECK-NEXT:    [[TMP3:%.*]] = call i129 @llvm.ctlz.i129(i129 [[A]], i1 true)
+; CHECK-NEXT:    [[TMP3:%.*]] = call i129 @llvm.ctlz.i129(i129 [[A]], i1 false)
 ; CHECK-NEXT:    [[TMP4:%.*]] = sub i129 [[TMP2]], [[TMP3]]
 ; CHECK-NEXT:    [[TMP5:%.*]] = icmp ugt i129 [[TMP4]], 128
 ; CHECK-NEXT:    [[TMP6:%.*]] = or i1 [[TMP1]], [[TMP5]]


        


More information about the llvm-commits mailing list