================
@@ -287,43 +287,77 @@ static std::pair<SymbolRef, APSIntPtr> 
decomposeSymbol(SymbolRef Sym,
   return std::make_pair(Sym, BV.getValue(0, Sym->getType()));
 }
 
-// Simplify "(LSym + LInt) Op (RSym + RInt)" assuming all values are of the
-// same signed integral type and no overflows occur (which should be checked
-// by the caller). May return UnknownVal if the symbol complexity threshold is
-// reached.
-static SVal doRearrangeUnchecked(ProgramStateRef State,
-                                 BinaryOperator::Opcode Op, SymbolRef LSym,
-                                 llvm::APSInt LInt, SymbolRef RSym,
-                                 llvm::APSInt RInt) {
-  SValBuilder &SVB = State->getStateManager().getSValBuilder();
-  BasicValueFactory &BV = SVB.getBasicValueFactory();
+// Simplify "(LSym + LInt) Op (RSym + RInt)" when all values are of the
+// same signed integral type and no overflows occurs. Returns the simplified
+// value or std::nullopt when the the preconditions are not satisfied or the
+// symbol complexity limit is reached.
+static std::optional<NonLoc> tryRearrange(ProgramStateRef State,
+                                          BinaryOperator::Opcode Op, NonLoc 
Lhs,
+                                          NonLoc Rhs, QualType ResultTy) {
+  ProgramStateManager &StateMgr = State->getStateManager();
+  SValBuilder &SVB = StateMgr.getSValBuilder();
+
+  // FIXME: This should be enabled by default, at least for additive 
operations.
+  if (!SVB.getAnalyzerOptions().ShouldAggressivelySimplifyBinaryOperation)
+    return std::nullopt;
 
-  QualType SymTy = LSym->getType();
-  assert(SymTy == RSym->getType() &&
-         "Symbols are not of the same type!");
-  assert(APSIntType(LInt) == BV.getAPSIntType(SymTy) &&
+  SymbolRef LSym = Lhs.getAsSymbol();
+  if (!LSym)
+    return std::nullopt;
+
+  // We expect everything to be of the same type - this type.
+  QualType CommonTy = LSym->getType();
+
+  if (BinaryOperator::isComparisonOp(Op)) {
+    if (ResultTy != SVB.getConditionType())
+      return std::nullopt;
+  } else if (BinaryOperator::isAdditiveOp(Op)) {
+    if (ResultTy != CommonTy)
+      return std::nullopt;
+  } else {
+    // Don't rearrange other operations.
+    return std::nullopt;
+  }
+
+  // Rearrange signed symbolic expressions only
----------------
steakhal wrote:

```suggestion
  // Rearrange signed symbolic expressions only.
```

https://github.com/llvm/llvm-project/pull/233441
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to