Skip to content

Commit 629a48f

Browse files
committed
[CVP] Convert sitofp -> uitofp nneg and add nneg flag to uitofp
Similiar to the `InstCombine` changes, just furthering the scope of the canonicalization/`uitofp nneg` support
1 parent e31cbc4 commit 629a48f

File tree

3 files changed

+47
-11
lines changed

3 files changed

+47
-11
lines changed

llvm/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ STATISTIC(NumAShrsConverted, "Number of ashr converted to lshr");
6262
STATISTIC(NumAShrsRemoved, "Number of ashr removed");
6363
STATISTIC(NumSRems, "Number of srem converted to urem");
6464
STATISTIC(NumSExt, "Number of sext converted to zext");
65+
STATISTIC(NumSIToFP, "Number of sitofp converted to uitofp");
6566
STATISTIC(NumSICmps, "Number of signed icmp preds simplified to unsigned");
6667
STATISTIC(NumAnd, "Number of ands removed");
6768
STATISTIC(NumNW, "Number of no-wrap deductions");
@@ -89,7 +90,7 @@ STATISTIC(NumSMinMax,
8990
"Number of llvm.s{min,max} intrinsics simplified to unsigned");
9091
STATISTIC(NumUDivURemsNarrowedExpanded,
9192
"Number of bound udiv's/urem's expanded");
92-
STATISTIC(NumZExt, "Number of non-negative deductions");
93+
STATISTIC(NumNNeg, "Number of zext/uitofp non-negative deductions");
9394

9495
static Constant *getConstantAt(Value *V, Instruction *At, LazyValueInfo *LVI) {
9596
if (Constant *C = LVI->getConstant(V, At))
@@ -1075,20 +1076,49 @@ static bool processSExt(SExtInst *SDI, LazyValueInfo *LVI) {
10751076
return true;
10761077
}
10771078

1078-
static bool processZExt(ZExtInst *ZExt, LazyValueInfo *LVI) {
1079-
if (ZExt->getType()->isVectorTy())
1079+
static bool processPossibleNonNeg(PossiblyNonNegInst *I, LazyValueInfo *LVI) {
1080+
if (I->getType()->isVectorTy())
10801081
return false;
10811082

1082-
if (ZExt->hasNonNeg())
1083+
if (I->hasNonNeg())
10831084
return false;
10841085

1085-
const Use &Base = ZExt->getOperandUse(0);
1086+
const Use &Base = I->getOperandUse(0);
10861087
if (!LVI->getConstantRangeAtUse(Base, /*UndefAllowed*/ false)
10871088
.isAllNonNegative())
10881089
return false;
10891090

1090-
++NumZExt;
1091-
ZExt->setNonNeg();
1091+
++NumNNeg;
1092+
I->setNonNeg();
1093+
1094+
return true;
1095+
}
1096+
1097+
static bool processZExt(ZExtInst *ZExt, LazyValueInfo *LVI) {
1098+
return processPossibleNonNeg(cast<PossiblyNonNegInst>(ZExt), LVI);
1099+
}
1100+
1101+
static bool processUIToFP(UIToFPInst *UIToFP, LazyValueInfo *LVI) {
1102+
return processPossibleNonNeg(cast<PossiblyNonNegInst>(UIToFP), LVI);
1103+
}
1104+
1105+
static bool processSIToFP(SIToFPInst *SIToFP, LazyValueInfo *LVI) {
1106+
if (SIToFP->getType()->isVectorTy())
1107+
return false;
1108+
1109+
const Use &Base = SIToFP->getOperandUse(0);
1110+
if (!LVI->getConstantRangeAtUse(Base, /*UndefAllowed*/ false)
1111+
.isAllNonNegative())
1112+
return false;
1113+
1114+
++NumSIToFP;
1115+
auto *UIToFP = CastInst::Create(Instruction::UIToFP, Base, SIToFP->getType(),
1116+
"", SIToFP->getIterator());
1117+
UIToFP->takeName(SIToFP);
1118+
UIToFP->setDebugLoc(SIToFP->getDebugLoc());
1119+
UIToFP->setNonNeg();
1120+
SIToFP->replaceAllUsesWith(UIToFP);
1121+
SIToFP->eraseFromParent();
10921122

10931123
return true;
10941124
}
@@ -1197,6 +1227,12 @@ static bool runImpl(Function &F, LazyValueInfo *LVI, DominatorTree *DT,
11971227
case Instruction::ZExt:
11981228
BBChanged |= processZExt(cast<ZExtInst>(&II), LVI);
11991229
break;
1230+
case Instruction::UIToFP:
1231+
BBChanged |= processUIToFP(cast<UIToFPInst>(&II), LVI);
1232+
break;
1233+
case Instruction::SIToFP:
1234+
BBChanged |= processSIToFP(cast<SIToFPInst>(&II), LVI);
1235+
break;
12001236
case Instruction::Add:
12011237
case Instruction::Sub:
12021238
case Instruction::Mul:

llvm/test/Transforms/CorrelatedValuePropagation/sitofp.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ define void @test1(i32 %n) {
99
; CHECK-NEXT: [[CMP:%.*]] = icmp sgt i32 [[N:%.*]], -1
1010
; CHECK-NEXT: br i1 [[CMP]], label [[BB:%.*]], label [[EXIT:%.*]]
1111
; CHECK: bb:
12-
; CHECK-NEXT: [[EXT_WIDE:%.*]] = sitofp i32 [[N]] to float
12+
; CHECK-NEXT: [[EXT_WIDE:%.*]] = uitofp nneg i32 [[N]] to float
1313
; CHECK-NEXT: call void @use.f32(float [[EXT_WIDE]])
1414
; CHECK-NEXT: br label [[EXIT]]
1515
; CHECK: exit:
@@ -88,7 +88,7 @@ exit:
8888
define double @test_infer_at_use(i32 noundef %n) {
8989
; CHECK-LABEL: @test_infer_at_use(
9090
; CHECK-NEXT: [[CMP:%.*]] = icmp sgt i32 [[N:%.*]], -1
91-
; CHECK-NEXT: [[EXT:%.*]] = sitofp i32 [[N]] to double
91+
; CHECK-NEXT: [[EXT:%.*]] = uitofp nneg i32 [[N]] to double
9292
; CHECK-NEXT: [[SELECT:%.*]] = select i1 [[CMP]], double [[EXT]], double 0.000000e+00
9393
; CHECK-NEXT: ret double [[SELECT]]
9494
;

llvm/test/Transforms/CorrelatedValuePropagation/uitofp.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ define void @test1(i32 %n) {
99
; CHECK-NEXT: [[CMP:%.*]] = icmp sgt i32 [[N:%.*]], -1
1010
; CHECK-NEXT: br i1 [[CMP]], label [[BB:%.*]], label [[EXIT:%.*]]
1111
; CHECK: bb:
12-
; CHECK-NEXT: [[EXT_WIDE:%.*]] = uitofp i32 [[N]] to float
12+
; CHECK-NEXT: [[EXT_WIDE:%.*]] = uitofp nneg i32 [[N]] to float
1313
; CHECK-NEXT: call void @use.f32(float [[EXT_WIDE]])
1414
; CHECK-NEXT: br label [[EXIT]]
1515
; CHECK: exit:
@@ -87,7 +87,7 @@ exit:
8787
define double @test_infer_at_use(i32 noundef %n) {
8888
; CHECK-LABEL: @test_infer_at_use(
8989
; CHECK-NEXT: [[CMP:%.*]] = icmp sgt i32 [[N:%.*]], -1
90-
; CHECK-NEXT: [[EXT:%.*]] = uitofp i32 [[N]] to double
90+
; CHECK-NEXT: [[EXT:%.*]] = uitofp nneg i32 [[N]] to double
9191
; CHECK-NEXT: [[SELECT:%.*]] = select i1 [[CMP]], double [[EXT]], double 0.000000e+00
9292
; CHECK-NEXT: ret double [[SELECT]]
9393
;

0 commit comments

Comments
 (0)