@@ -62,38 +62,32 @@ bool RISCVCodeGenPrepare::visitZExtInst(ZExtInst &ZExt) {
62
62
if (!ST->is64Bit ())
63
63
return false ;
64
64
65
+ if (ZExt.hasNonNeg ())
66
+ return false ;
67
+
65
68
Value *Src = ZExt.getOperand (0 );
66
69
67
70
// We only care about ZExt from i32 to i64.
68
71
if (!ZExt.getType ()->isIntegerTy (64 ) || !Src->getType ()->isIntegerTy (32 ))
69
72
return false ;
70
73
71
- // Look for an opportunity to replace (i64 (zext (i32 X))) with a sext if we
72
- // can determine that the sign bit of X is zero via a dominating condition.
73
- // This often occurs with widened induction variables.
74
+ // Look for an opportunity to infer nneg on a zext if we can determine that
75
+ // the sign bit of X is zero via a dominating condition. This often occurs
76
+ // with widened induction variables.
74
77
if (isImpliedByDomCondition (ICmpInst::ICMP_SGE, Src,
75
78
Constant::getNullValue (Src->getType ()), &ZExt,
76
79
*DL).value_or (false )) {
77
- auto *SExt = new SExtInst (Src, ZExt.getType (), " " , &ZExt);
78
- SExt->takeName (&ZExt);
79
- SExt->setDebugLoc (ZExt.getDebugLoc ());
80
-
81
- ZExt.replaceAllUsesWith (SExt);
82
- ZExt.eraseFromParent ();
80
+ ZExt.setNonNeg (true );
83
81
++NumZExtToSExt;
84
82
return true ;
85
83
}
86
84
87
- // Convert (zext (abs(i32 X, i1 1))) -> (sext (abs(i32 X, i1 1))). If abs of
85
+ // Convert (zext (abs(i32 X, i1 1))) -> (zext nneg (abs(i32 X, i1 1))). If abs of
88
86
// INT_MIN is poison, the sign bit is zero.
87
+ // TODO: Move this to instcombine now that we have zext nneg in IR.
89
88
using namespace PatternMatch ;
90
89
if (match (Src, m_Intrinsic<Intrinsic::abs >(m_Value (), m_One ()))) {
91
- auto *SExt = new SExtInst (Src, ZExt.getType (), " " , &ZExt);
92
- SExt->takeName (&ZExt);
93
- SExt->setDebugLoc (ZExt.getDebugLoc ());
94
-
95
- ZExt.replaceAllUsesWith (SExt);
96
- ZExt.eraseFromParent ();
90
+ ZExt.setNonNeg (true );
97
91
++NumZExtToSExt;
98
92
return true ;
99
93
}
@@ -102,19 +96,26 @@ bool RISCVCodeGenPrepare::visitZExtInst(ZExtInst &ZExt) {
102
96
}
103
97
104
98
// Try to optimize (i64 (and (zext/sext (i32 X), C1))) if C1 has bit 31 set,
105
- // but bits 63:32 are zero. If we can prove that bit 31 of X is 0, we can fill
106
- // the upper 32 bits with ones. A separate transform will turn (zext X) into
107
- // (sext X) for the same condition.
99
+ // but bits 63:32 are zero. If we know that bit 31 of X is 0, we can fill
100
+ // the upper 32 bits with ones.
108
101
bool RISCVCodeGenPrepare::visitAnd (BinaryOperator &BO) {
109
102
if (!ST->is64Bit ())
110
103
return false ;
111
104
112
105
if (!BO.getType ()->isIntegerTy (64 ))
113
106
return false ;
114
107
115
- // Left hand side should be sext or zext.
108
+ auto canBeSignExtend = [](Instruction *I) {
109
+ if (isa<SExtInst>(I))
110
+ return true ;
111
+ if (isa<ZExtInst>(I))
112
+ return I->hasNonNeg ();
113
+ return false ;
114
+ };
115
+
116
+ // Left hand side should be a sext or zext nneg.
116
117
Instruction *LHS = dyn_cast<Instruction>(BO.getOperand (0 ));
117
- if (!LHS || (!isa<SExtInst> (LHS) && !isa<ZExtInst>(LHS) ))
118
+ if (!LHS || ! canBeSignExtend (LHS))
118
119
return false ;
119
120
120
121
Value *LHSSrc = LHS->getOperand (0 );
@@ -135,13 +136,6 @@ bool RISCVCodeGenPrepare::visitAnd(BinaryOperator &BO) {
135
136
if (!isUInt<32 >(C) || isInt<12 >(C) || !isInt<12 >(SignExtend64<32 >(C)))
136
137
return false ;
137
138
138
- // If we can determine the sign bit of the input is 0, we can replace the
139
- // And mask constant.
140
- if (!isImpliedByDomCondition (ICmpInst::ICMP_SGE, LHSSrc,
141
- Constant::getNullValue (LHSSrc->getType ()),
142
- LHS, *DL).value_or (false ))
143
- return false ;
144
-
145
139
// Sign extend the constant and replace the And operand.
146
140
C = SignExtend64<32 >(C);
147
141
BO.setOperand (1 , ConstantInt::get (LHS->getType (), C));
0 commit comments