Skip to content

Commit 2d4b677

Browse files
committedAug 28, 2019
[InstCombine] clean up wrap propagation for reassociated ops; NFCI
Always true/false checks were flagged by static analysis; https://bugs.llvm.org/show_bug.cgi?id=43143 I have not confirmed the logic difference in propagating nsw vs. nuw, but presumably we would have noticed a bug by now if that was wrong. llvm-svn: 370248
1 parent ead98ea commit 2d4b677

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed
 

‎llvm/lib/Transforms/InstCombine/InstructionCombining.cpp

+15-11
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,8 @@ bool InstCombiner::shouldChangeType(Type *From, Type *To) const {
200200
// where both B and C should be ConstantInts, results in a constant that does
201201
// not overflow. This function only handles the Add and Sub opcodes. For
202202
// 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);
205205
if (!OBO || !OBO->hasNoSignedWrap())
206206
return false;
207207

@@ -224,10 +224,15 @@ static bool MaintainNoSignedWrap(BinaryOperator &I, Value *B, Value *C) {
224224
}
225225

226226
static bool hasNoUnsignedWrap(BinaryOperator &I) {
227-
OverflowingBinaryOperator *OBO = dyn_cast<OverflowingBinaryOperator>(&I);
227+
auto *OBO = dyn_cast<OverflowingBinaryOperator>(&I);
228228
return OBO && OBO->hasNoUnsignedWrap();
229229
}
230230

231+
static bool hasNoSignedWrap(BinaryOperator &I) {
232+
auto *OBO = dyn_cast<OverflowingBinaryOperator>(&I);
233+
return OBO && OBO->hasNoSignedWrap();
234+
}
235+
231236
/// Conservatively clears subclassOptionalData after a reassociation or
232237
/// commutation. We preserve fast-math flags when applicable as they can be
233238
/// preserved.
@@ -332,22 +337,21 @@ bool InstCombiner::SimplifyAssociativeOrCommutative(BinaryOperator &I) {
332337
// It simplifies to V. Form "A op V".
333338
I.setOperand(0, A);
334339
I.setOperand(1, V);
335-
// Conservatively clear the optional flags, since they may not be
336-
// preserved by the reassociation.
337340
bool IsNUW = hasNoUnsignedWrap(I) && hasNoUnsignedWrap(*Op0);
338-
bool IsNSW = MaintainNoSignedWrap(I, B, C);
341+
bool IsNSW = maintainNoSignedWrap(I, B, C) && hasNoSignedWrap(*Op0);
339342

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.
340346
ClearSubclassDataAfterReassociation(I);
341347

348+
// Note: this is only valid because SimplifyBinOp doesn't look at
349+
// the operands to Op0.
342350
if (IsNUW)
343351
I.setHasNoUnsignedWrap(true);
344352

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)
349354
I.setHasNoSignedWrap(true);
350-
}
351355

352356
Changed = true;
353357
++NumReassoc;

0 commit comments

Comments
 (0)