@@ -200,8 +200,8 @@ bool InstCombiner::shouldChangeType(Type *From, Type *To) const {
200
200
// where both B and C should be ConstantInts, results in a constant that does
201
201
// not overflow. This function only handles the Add and Sub opcodes. For
202
202
// all other opcodes, the function conservatively returns false.
203
- static bool MaintainNoSignedWrap (BinaryOperator &I, Value *B, Value *C) {
204
- OverflowingBinaryOperator *OBO = dyn_cast<OverflowingBinaryOperator>(&I);
203
+ static bool maintainNoSignedWrap (BinaryOperator &I, Value *B, Value *C) {
204
+ auto *OBO = dyn_cast<OverflowingBinaryOperator>(&I);
205
205
if (!OBO || !OBO->hasNoSignedWrap ())
206
206
return false ;
207
207
@@ -224,10 +224,15 @@ static bool MaintainNoSignedWrap(BinaryOperator &I, Value *B, Value *C) {
224
224
}
225
225
226
226
static bool hasNoUnsignedWrap (BinaryOperator &I) {
227
- OverflowingBinaryOperator *OBO = dyn_cast<OverflowingBinaryOperator>(&I);
227
+ auto *OBO = dyn_cast<OverflowingBinaryOperator>(&I);
228
228
return OBO && OBO->hasNoUnsignedWrap ();
229
229
}
230
230
231
+ static bool hasNoSignedWrap (BinaryOperator &I) {
232
+ auto *OBO = dyn_cast<OverflowingBinaryOperator>(&I);
233
+ return OBO && OBO->hasNoSignedWrap ();
234
+ }
235
+
231
236
// / Conservatively clears subclassOptionalData after a reassociation or
232
237
// / commutation. We preserve fast-math flags when applicable as they can be
233
238
// / preserved.
@@ -332,22 +337,21 @@ bool InstCombiner::SimplifyAssociativeOrCommutative(BinaryOperator &I) {
332
337
// It simplifies to V. Form "A op V".
333
338
I.setOperand (0 , A);
334
339
I.setOperand (1 , V);
335
- // Conservatively clear the optional flags, since they may not be
336
- // preserved by the reassociation.
337
340
bool IsNUW = hasNoUnsignedWrap (I) && hasNoUnsignedWrap (*Op0);
338
- bool IsNSW = MaintainNoSignedWrap (I, B, C);
341
+ bool IsNSW = maintainNoSignedWrap (I, B, C) && hasNoSignedWrap (*Op0 );
339
342
343
+ // Conservatively clear all optional flags since they may not be
344
+ // preserved by the reassociation. Reset nsw/nuw based on the above
345
+ // analysis.
340
346
ClearSubclassDataAfterReassociation (I);
341
347
348
+ // Note: this is only valid because SimplifyBinOp doesn't look at
349
+ // the operands to Op0.
342
350
if (IsNUW)
343
351
I.setHasNoUnsignedWrap (true );
344
352
345
- if (IsNSW &&
346
- (!Op0 || (isa<BinaryOperator>(Op0) && Op0->hasNoSignedWrap ()))) {
347
- // Note: this is only valid because SimplifyBinOp doesn't look at
348
- // the operands to Op0.
353
+ if (IsNSW)
349
354
I.setHasNoSignedWrap (true );
350
- }
351
355
352
356
Changed = true ;
353
357
++NumReassoc;
0 commit comments