+bool NaryReassociate::isKnownNonNegative(Value *V, Instruction *Ctxt) {
+ bool NonNegative, Negative;
+ // TODO: ComputeSignBits is expensive. Consider caching the results.
+ ComputeSignBit(V, NonNegative, Negative, *DL, 0, AC, Ctxt, DT);
+ return NonNegative;
+}
+
+bool NaryReassociate::maySignOverflow(AddOperator *AO, Instruction *Ctxt) {
+ if (AO->hasNoSignedWrap())
+ return false;
+
+ Value *LHS = AO->getOperand(0), *RHS = AO->getOperand(1);
+ // If LHS or RHS has the same sign as the sum, AO doesn't sign overflow.
+ // TODO: handle the negative case as well.
+ if (isKnownNonNegative(AO, Ctxt) &&
+ (isKnownNonNegative(LHS, Ctxt) || isKnownNonNegative(RHS, Ctxt)))
+ return false;
+
+ return true;
+}
+