https://github.com/c8ef updated https://github.com/llvm/llvm-project/pull/112459

>From dfa1585af3f080987cbd15830c45c34bfecc1fca Mon Sep 17 00:00:00 2001
From: c8ef <c...@outlook.com>
Date: Wed, 16 Oct 2024 01:18:13 +0000
Subject: [PATCH 1/7] implement constexpr builtin {l}abs

---
 clang/include/clang/Basic/Builtins.td         |  1 +
 clang/lib/AST/ExprConstant.cpp                | 11 +++++++++++
 clang/test/AST/ByteCode/builtin-functions.cpp |  9 +++++++++
 3 files changed, 21 insertions(+)

diff --git a/clang/include/clang/Basic/Builtins.td 
b/clang/include/clang/Basic/Builtins.td
index bda8a48be92bda..e1b4d5b1fdc0a5 100644
--- a/clang/include/clang/Basic/Builtins.td
+++ b/clang/include/clang/Basic/Builtins.td
@@ -2714,6 +2714,7 @@ def Abs : IntMathTemplate, LibBuiltin<"stdlib.h"> {
   let Attributes = [NoThrow, Const];
   let Prototype = "T(T)";
   let AddBuiltinPrefixedAlias = 1;
+  let OnlyBuiltinPrefixedAliasIsConstexpr = 1;
 }
 
 def Calloc : LibBuiltin<"stdlib.h"> {
diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index 52a7f5778ce6d2..69539a7f03feba 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -13055,6 +13055,17 @@ bool IntExprEvaluator::VisitBuiltinCallExpr(const 
CallExpr *E,
     return Success(Val.popcount() % 2, E);
   }
 
+  case Builtin::BI__builtin_abs:
+  case Builtin::BI__builtin_labs:
+  case Builtin::BI__builtin_llabs: {
+    APSInt Val;
+    if (!EvaluateInteger(E->getArg(0), Val, Info))
+      return false;
+    if (Val.isNegative())
+      Val.negate();
+    return Success(Val, E);
+  }
+
   case Builtin::BI__builtin_popcount:
   case Builtin::BI__builtin_popcountl:
   case Builtin::BI__builtin_popcountll:
diff --git a/clang/test/AST/ByteCode/builtin-functions.cpp 
b/clang/test/AST/ByteCode/builtin-functions.cpp
index 450ff5671314db..46e5b0579bd575 100644
--- a/clang/test/AST/ByteCode/builtin-functions.cpp
+++ b/clang/test/AST/ByteCode/builtin-functions.cpp
@@ -265,6 +265,15 @@ namespace fpclassify {
   char classify_subnorm [__builtin_fpclassify(-1, -1, -1, +1, -1, 1.0e-38f)];
 }
 
+namespace abs {
+static_assert(__builtin_abs(14) == 14, "");
+static_assert(__builtin_labs(14L) == 14L, "");
+static_assert(__builtin_llabs(14LL) == 14LL, "");
+static_assert(__builtin_abs(-14) == 14, "");
+static_assert(__builtin_labs(-14L) == 14L, "");
+static_assert(__builtin_llabs(-14LL) == 14LL, "");
+} // namespace abs
+
 namespace fabs {
   static_assert(__builtin_fabs(-14.0) == 14.0, "");
 }

>From 52e1d070b392ea4651acf5f7f984a1defb035459 Mon Sep 17 00:00:00 2001
From: c8ef <c...@outlook.com>
Date: Wed, 16 Oct 2024 12:32:37 +0800
Subject: [PATCH 2/7] Update InterpBuiltin.cpp

---
 clang/lib/AST/ByteCode/InterpBuiltin.cpp | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)

diff --git a/clang/lib/AST/ByteCode/InterpBuiltin.cpp 
b/clang/lib/AST/ByteCode/InterpBuiltin.cpp
index 65c7b4e5306d72..1e1a75e5514aec 100644
--- a/clang/lib/AST/ByteCode/InterpBuiltin.cpp
+++ b/clang/lib/AST/ByteCode/InterpBuiltin.cpp
@@ -563,6 +563,17 @@ static bool interp__builtin_fabs(InterpState &S, CodePtr 
OpPC,
   return true;
 }
 
+static bool interp__builtin_abs(InterpState &S, CodePtr OpPC,
+                                const InterpFrame *Frame, const Function *Func,
+                                const CallExpr *Call) {
+  PrimType ArgT = *S.getContext().classify(Call->getArg(0)->getType());
+  APSInt Val = peekToAPSInt(S.Stk, ArgT);
+  if (Val.isNegative())
+    Val.negate();
+  pushInteger(S, Val, Call->getType());
+  return true;
+}
+
 static bool interp__builtin_popcount(InterpState &S, CodePtr OpPC,
                                      const InterpFrame *Frame,
                                      const Function *Func,
@@ -1808,6 +1819,13 @@ bool InterpretBuiltin(InterpState &S, CodePtr OpPC, 
const Function *F,
       return false;
     break;
 
+  case Builtin::BI__builtin_abs:
+  case Builtin::BI__builtin_labs:
+  case Builtin::BI__builtin_llabs:
+  if (!interp__builtin_abs(S, OpPC, Frame, F, Call))
+    return false;
+  break;
+
   case Builtin::BI__builtin_popcount:
   case Builtin::BI__builtin_popcountl:
   case Builtin::BI__builtin_popcountll:

>From 227193529178a1c1484c0da42a06289a26c75114 Mon Sep 17 00:00:00 2001
From: c8ef <c...@outlook.com>
Date: Wed, 16 Oct 2024 12:33:29 +0800
Subject: [PATCH 3/7] Update ExprConstant.cpp

---
 clang/lib/AST/ExprConstant.cpp | 11 -----------
 1 file changed, 11 deletions(-)

diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index 69539a7f03feba..52a7f5778ce6d2 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -13055,17 +13055,6 @@ bool IntExprEvaluator::VisitBuiltinCallExpr(const 
CallExpr *E,
     return Success(Val.popcount() % 2, E);
   }
 
-  case Builtin::BI__builtin_abs:
-  case Builtin::BI__builtin_labs:
-  case Builtin::BI__builtin_llabs: {
-    APSInt Val;
-    if (!EvaluateInteger(E->getArg(0), Val, Info))
-      return false;
-    if (Val.isNegative())
-      Val.negate();
-    return Success(Val, E);
-  }
-
   case Builtin::BI__builtin_popcount:
   case Builtin::BI__builtin_popcountl:
   case Builtin::BI__builtin_popcountll:

>From 5f45e1feba15266662ce171366c0450c86b58b64 Mon Sep 17 00:00:00 2001
From: c8ef <c...@outlook.com>
Date: Wed, 16 Oct 2024 12:38:09 +0800
Subject: [PATCH 4/7] Update InterpBuiltin.cpp

---
 clang/lib/AST/ByteCode/InterpBuiltin.cpp | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/clang/lib/AST/ByteCode/InterpBuiltin.cpp 
b/clang/lib/AST/ByteCode/InterpBuiltin.cpp
index 1e1a75e5514aec..8a27658d7fb6a4 100644
--- a/clang/lib/AST/ByteCode/InterpBuiltin.cpp
+++ b/clang/lib/AST/ByteCode/InterpBuiltin.cpp
@@ -1822,9 +1822,9 @@ bool InterpretBuiltin(InterpState &S, CodePtr OpPC, const 
Function *F,
   case Builtin::BI__builtin_abs:
   case Builtin::BI__builtin_labs:
   case Builtin::BI__builtin_llabs:
-  if (!interp__builtin_abs(S, OpPC, Frame, F, Call))
-    return false;
-  break;
+    if (!interp__builtin_abs(S, OpPC, Frame, F, Call))
+      return false;
+    break;
 
   case Builtin::BI__builtin_popcount:
   case Builtin::BI__builtin_popcountl:

>From 05b6cd41661c347ef9932dcb651851ef95e67379 Mon Sep 17 00:00:00 2001
From: c8ef <c...@outlook.com>
Date: Wed, 16 Oct 2024 13:55:34 +0800
Subject: [PATCH 5/7] Update ExprConstant.cpp

---
 clang/lib/AST/ExprConstant.cpp | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index 52a7f5778ce6d2..69539a7f03feba 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -13055,6 +13055,17 @@ bool IntExprEvaluator::VisitBuiltinCallExpr(const 
CallExpr *E,
     return Success(Val.popcount() % 2, E);
   }
 
+  case Builtin::BI__builtin_abs:
+  case Builtin::BI__builtin_labs:
+  case Builtin::BI__builtin_llabs: {
+    APSInt Val;
+    if (!EvaluateInteger(E->getArg(0), Val, Info))
+      return false;
+    if (Val.isNegative())
+      Val.negate();
+    return Success(Val, E);
+  }
+
   case Builtin::BI__builtin_popcount:
   case Builtin::BI__builtin_popcountl:
   case Builtin::BI__builtin_popcountll:

>From 12e545f87482a2965804057eda3821e5946be4b8 Mon Sep 17 00:00:00 2001
From: c8ef <c...@outlook.com>
Date: Wed, 16 Oct 2024 18:10:48 +0800
Subject: [PATCH 6/7] Update ExprConstant.cpp

---
 clang/lib/AST/ExprConstant.cpp | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index 69539a7f03feba..256bc7454e5f46 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -13061,6 +13061,8 @@ bool IntExprEvaluator::VisitBuiltinCallExpr(const 
CallExpr *E,
     APSInt Val;
     if (!EvaluateInteger(E->getArg(0), Val, Info))
       return false;
+    if (Val == APSInt::getSignedMinValue(Val.getBitWidth()))
+      return false;
     if (Val.isNegative())
       Val.negate();
     return Success(Val, E);

>From 0adef1074cc3982fdef4166b4913fd57858ea09d Mon Sep 17 00:00:00 2001
From: c8ef <c...@outlook.com>
Date: Wed, 16 Oct 2024 18:23:36 +0800
Subject: [PATCH 7/7] Update ExprConstant.cpp

---
 clang/lib/AST/ExprConstant.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index 256bc7454e5f46..d1d6790e152527 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -13061,7 +13061,7 @@ bool IntExprEvaluator::VisitBuiltinCallExpr(const 
CallExpr *E,
     APSInt Val;
     if (!EvaluateInteger(E->getArg(0), Val, Info))
       return false;
-    if (Val == APSInt::getSignedMinValue(Val.getBitWidth()))
+    if (Val == APSInt(APSInt::getSignedMinValue(Val.getBitWidth()), false))
       return false;
     if (Val.isNegative())
       Val.negate();

_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to