@@ -252,15 +252,18 @@ static Value *foldThreeVarBoolExpr(Value *Root,
252
252
if (!isa<BinaryOperator>(Root))
253
253
return nullptr ;
254
254
255
+ // Early bailout for expressions with too many uses (avoid expensive analysis
256
+ // andorxor.ll)
257
+ if (!Root->hasOneUse ())
258
+ return nullptr ;
259
+
255
260
// Skip transformation if expression is already simple (at most 2 levels
256
261
// deep).
257
- if (Root->hasOneUse () && isa<BinaryOperator>(Root)) {
258
- if (auto *BO = dyn_cast<BinaryOperator>(Root)) {
259
- bool IsSimple = !isa<BinaryOperator>(BO->getOperand (0 )) ||
260
- !isa<BinaryOperator>(BO->getOperand (1 ));
261
- if (IsSimple)
262
- return nullptr ;
263
- }
262
+ if (auto *BO = dyn_cast<BinaryOperator>(Root)) {
263
+ bool IsSimple = !isa<BinaryOperator>(BO->getOperand (0 )) ||
264
+ !isa<BinaryOperator>(BO->getOperand (1 ));
265
+ if (IsSimple)
266
+ return nullptr ;
264
267
}
265
268
266
269
auto [Op0, Op1, Op2] = extractThreeVariables (Root);
@@ -271,10 +274,6 @@ static Value *foldThreeVarBoolExpr(Value *Root,
271
274
if (!Table)
272
275
return nullptr ;
273
276
274
- // Only transform expressions with single use to avoid code growth.
275
- if (!Root->hasOneUse ())
276
- return nullptr ;
277
-
278
277
return createLogicFromTable3Var (*Table, Op0, Op1, Op2, Root, Builder, true );
279
278
}
280
279
@@ -2628,6 +2627,9 @@ Instruction *InstCombinerImpl::visitAnd(BinaryOperator &I) {
2628
2627
if (Instruction *Phi = foldBinopWithPhiOperands (I))
2629
2628
return Phi;
2630
2629
2630
+ if (Value *Canonical = foldThreeVarBoolExpr (&I, Builder))
2631
+ return replaceInstUsesWith (I, Canonical);
2632
+
2631
2633
// See if we can simplify any instructions used by the instruction whose sole
2632
2634
// purpose is to compute bits we don't care about.
2633
2635
if (SimplifyDemandedInstructionBits (I))
@@ -3985,6 +3987,9 @@ Instruction *InstCombinerImpl::visitOr(BinaryOperator &I) {
3985
3987
if (Instruction *Phi = foldBinopWithPhiOperands (I))
3986
3988
return Phi;
3987
3989
3990
+ if (Value *Canonical = foldThreeVarBoolExpr (&I, Builder))
3991
+ return replaceInstUsesWith (I, Canonical);
3992
+
3988
3993
// See if we can simplify any instructions used by the instruction whose sole
3989
3994
// purpose is to compute bits we don't care about.
3990
3995
if (SimplifyDemandedInstructionBits (I))
@@ -4008,9 +4013,6 @@ Instruction *InstCombinerImpl::visitOr(BinaryOperator &I) {
4008
4013
4009
4014
Value *Op0 = I.getOperand (0 ), *Op1 = I.getOperand (1 );
4010
4015
4011
- if (Value *Canonical = foldThreeVarBoolExpr (&I, Builder))
4012
- return replaceInstUsesWith (I, Canonical);
4013
-
4014
4016
Type *Ty = I.getType ();
4015
4017
if (Ty->isIntOrIntVectorTy (1 )) {
4016
4018
if (auto *SI0 = dyn_cast<SelectInst>(Op0)) {
@@ -5136,6 +5138,9 @@ Instruction *InstCombinerImpl::visitXor(BinaryOperator &I) {
5136
5138
if (Instruction *Phi = foldBinopWithPhiOperands (I))
5137
5139
return Phi;
5138
5140
5141
+ if (Value *Canonical = foldThreeVarBoolExpr (&I, Builder))
5142
+ return replaceInstUsesWith (I, Canonical);
5143
+
5139
5144
if (Instruction *NewXor = foldXorToXor (I, Builder))
5140
5145
return NewXor;
5141
5146
@@ -5417,9 +5422,6 @@ Instruction *InstCombinerImpl::visitXor(BinaryOperator &I) {
5417
5422
}
5418
5423
}
5419
5424
5420
- if (Value *Canonical = foldThreeVarBoolExpr (&I, Builder))
5421
- return replaceInstUsesWith (I, Canonical);
5422
-
5423
5425
if (auto *LHS = dyn_cast<ICmpInst>(I.getOperand (0 )))
5424
5426
if (auto *RHS = dyn_cast<ICmpInst>(I.getOperand (1 )))
5425
5427
if (Value *V = foldXorOfICmps (LHS, RHS, I))
0 commit comments