[clang] [CIR] Fold constant add and sub operations (PR #218398)
via cfe-commits
cfe-commits at lists.llvm.org
Mon Aug 24 23:13:28 PDT 2026
================
@@ -3185,6 +3185,71 @@ OpFoldResult cir::NotOp::fold(FoldAdaptor adaptor) {
return {};
}
+//===----------------------------------------------------------------------===//
+// AddOp & SubOp
+//===----------------------------------------------------------------------===//
+
+// Constant-fold integer add/sub. Honors nsw/nuw by folding to poison on the
+// corresponding overflow, and folds saturated arithmetic by clamping.
+static OpFoldResult foldAddSubConst(mlir::Type ty, const APInt &lhs,
+ const APInt &rhs, bool isSub, bool nsw,
+ bool nuw, bool sat) {
+ bool isSigned = mlir::cast<cir::IntType>(ty).isSigned();
+ if (sat) {
+ APInt res = isSub ? (isSigned ? lhs.ssub_sat(rhs) : lhs.usub_sat(rhs))
+ : (isSigned ? lhs.sadd_sat(rhs) : lhs.uadd_sat(rhs));
+ return cir::IntAttr::get(ty, res);
+ }
+
+ bool poison = false;
+ if (nsw) {
----------------
prometheusfma-llvm wrote:
Thanks for taking a look. What about fixups added in the recent commit? Does that look fine?
https://github.com/llvm/llvm-project/pull/218398
More information about the cfe-commits
mailing list