[PATCH] D157440: [clang][ConstExprEmitter] handle BinaryOperators that don't have signedness or overflow concerns for integrals

2023-08-08 Thread Nick Desaulniers via Phabricator via cfe-commits
nickdesaulniers updated this revision to Diff 548358.
nickdesaulniers edited the summary of this revision.
nickdesaulniers added a comment.

- use const &
- add link to issue tracker


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D157440/new/

https://reviews.llvm.org/D157440

Files:
  clang/lib/CodeGen/CGExprConstant.cpp


Index: clang/lib/CodeGen/CGExprConstant.cpp
===
--- clang/lib/CodeGen/CGExprConstant.cpp
+++ clang/lib/CodeGen/CGExprConstant.cpp
@@ -1401,6 +1401,64 @@
 return nullptr;
   }
 
+  llvm::Constant *VisitBinaryOperator(BinaryOperator *B, QualType T) {
+// Can we attempt to handle this opcode?
+switch (B->getOpcode()) {
+default:
+  return nullptr;
+case BO_And:
+case BO_Xor:
+case BO_Or:
+case BO_EQ:
+case BO_NE:
+  break;
+}
+
+// Constant evaluate LHS
+Expr *LHS = B->getLHS();
+llvm::Constant *LHSC = Visit(LHS, T);
+
+// Only handle integers for now.
+if (!LHSC || !isa(LHSC))
+  return nullptr;
+
+// Constant evaluate RHS
+Expr *RHS = B->getRHS();
+llvm::Constant *RHSC = Visit(RHS, T);
+
+// Only handle integers for now.
+if (!RHSC || !isa(RHSC))
+  return nullptr;
+
+const llvm::APInt  = cast(RHSC)->getValue();
+const llvm::APInt  = cast(LHSC)->getValue();
+llvm::APInt Ret;
+
+// Fold
+switch (B->getOpcode()) {
+default:
+  // Should have return earlier.
+  llvm_unreachable("unhandled BinaryOperator kind");
+case BO_And:
+  Ret = L & R;
+  break;
+case BO_Xor:
+  Ret = L ^ R;
+  break;
+case BO_Or:
+  Ret = L | R;
+  break;
+case BO_EQ:
+  Ret = L == R;
+  break;
+case BO_NE:
+  Ret = L != R;
+  break;
+}
+
+return llvm::ConstantInt::get(CGM.getLLVMContext(), Ret);
+  }
+
   // Utility methods
   llvm::Type *ConvertType(QualType T) {
 return CGM.getTypes().ConvertType(T);


Index: clang/lib/CodeGen/CGExprConstant.cpp
===
--- clang/lib/CodeGen/CGExprConstant.cpp
+++ clang/lib/CodeGen/CGExprConstant.cpp
@@ -1401,6 +1401,64 @@
 return nullptr;
   }
 
+  llvm::Constant *VisitBinaryOperator(BinaryOperator *B, QualType T) {
+// Can we attempt to handle this opcode?
+switch (B->getOpcode()) {
+default:
+  return nullptr;
+case BO_And:
+case BO_Xor:
+case BO_Or:
+case BO_EQ:
+case BO_NE:
+  break;
+}
+
+// Constant evaluate LHS
+Expr *LHS = B->getLHS();
+llvm::Constant *LHSC = Visit(LHS, T);
+
+// Only handle integers for now.
+if (!LHSC || !isa(LHSC))
+  return nullptr;
+
+// Constant evaluate RHS
+Expr *RHS = B->getRHS();
+llvm::Constant *RHSC = Visit(RHS, T);
+
+// Only handle integers for now.
+if (!RHSC || !isa(RHSC))
+  return nullptr;
+
+const llvm::APInt  = cast(RHSC)->getValue();
+const llvm::APInt  = cast(LHSC)->getValue();
+llvm::APInt Ret;
+
+// Fold
+switch (B->getOpcode()) {
+default:
+  // Should have return earlier.
+  llvm_unreachable("unhandled BinaryOperator kind");
+case BO_And:
+  Ret = L & R;
+  break;
+case BO_Xor:
+  Ret = L ^ R;
+  break;
+case BO_Or:
+  Ret = L | R;
+  break;
+case BO_EQ:
+  Ret = L == R;
+  break;
+case BO_NE:
+  Ret = L != R;
+  break;
+}
+
+return llvm::ConstantInt::get(CGM.getLLVMContext(), Ret);
+  }
+
   // Utility methods
   llvm::Type *ConvertType(QualType T) {
 return CGM.getTypes().ConvertType(T);
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D157440: [clang][ConstExprEmitter] handle BinaryOperators that don't have signedness or overflow concerns for integrals

2023-08-08 Thread Nick Desaulniers via Phabricator via cfe-commits
nickdesaulniers created this revision.
Herald added a project: All.
nickdesaulniers requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Handles binary operators: & ^ | == !=

Check that each side evaluates to a ConstantInt then combine the
results.

We probably can reuse DataRecursiveIntBinOpEvaluator or
handleIntIntBinOp from ExprConstant.cpp for the rest, or even these
cases as well.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D157440

Files:
  clang/lib/CodeGen/CGExprConstant.cpp


Index: clang/lib/CodeGen/CGExprConstant.cpp
===
--- clang/lib/CodeGen/CGExprConstant.cpp
+++ clang/lib/CodeGen/CGExprConstant.cpp
@@ -1401,6 +1401,64 @@
 return nullptr;
   }
 
+  llvm::Constant *VisitBinaryOperator(BinaryOperator *B, QualType T) {
+// Can we attempt to handle this opcode?
+switch (B->getOpcode()) {
+default:
+  return nullptr;
+case BO_And:
+case BO_Xor:
+case BO_Or:
+case BO_EQ:
+case BO_NE:
+  break;
+}
+
+// Constant evaluate LHS
+Expr *LHS = B->getLHS();
+llvm::Constant *LHSC = Visit(LHS, T);
+
+// Only handle integers for now.
+if (!LHSC || !isa(LHSC))
+  return nullptr;
+
+// Constant evaluate RHS
+Expr *RHS = B->getRHS();
+llvm::Constant *RHSC = Visit(RHS, T);
+
+// Only handle integers for now.
+if (!RHSC || !isa(RHSC))
+  return nullptr;
+
+const llvm::APInt R = cast(RHSC)->getValue();
+const llvm::APInt L = cast(LHSC)->getValue();
+llvm::APInt Ret;
+
+// Fold
+switch (B->getOpcode()) {
+default:
+  // Should have return earlier.
+  llvm_unreachable("unhandled BinaryOperator kind");
+case BO_And:
+  Ret = L & R;
+  break;
+case BO_Xor:
+  Ret = L ^ R;
+  break;
+case BO_Or:
+  Ret = L | R;
+  break;
+case BO_EQ:
+  Ret = L == R;
+  break;
+case BO_NE:
+  Ret = L != R;
+  break;
+}
+
+return llvm::ConstantInt::get(CGM.getLLVMContext(), Ret);
+  }
+
   // Utility methods
   llvm::Type *ConvertType(QualType T) {
 return CGM.getTypes().ConvertType(T);


Index: clang/lib/CodeGen/CGExprConstant.cpp
===
--- clang/lib/CodeGen/CGExprConstant.cpp
+++ clang/lib/CodeGen/CGExprConstant.cpp
@@ -1401,6 +1401,64 @@
 return nullptr;
   }
 
+  llvm::Constant *VisitBinaryOperator(BinaryOperator *B, QualType T) {
+// Can we attempt to handle this opcode?
+switch (B->getOpcode()) {
+default:
+  return nullptr;
+case BO_And:
+case BO_Xor:
+case BO_Or:
+case BO_EQ:
+case BO_NE:
+  break;
+}
+
+// Constant evaluate LHS
+Expr *LHS = B->getLHS();
+llvm::Constant *LHSC = Visit(LHS, T);
+
+// Only handle integers for now.
+if (!LHSC || !isa(LHSC))
+  return nullptr;
+
+// Constant evaluate RHS
+Expr *RHS = B->getRHS();
+llvm::Constant *RHSC = Visit(RHS, T);
+
+// Only handle integers for now.
+if (!RHSC || !isa(RHSC))
+  return nullptr;
+
+const llvm::APInt R = cast(RHSC)->getValue();
+const llvm::APInt L = cast(LHSC)->getValue();
+llvm::APInt Ret;
+
+// Fold
+switch (B->getOpcode()) {
+default:
+  // Should have return earlier.
+  llvm_unreachable("unhandled BinaryOperator kind");
+case BO_And:
+  Ret = L & R;
+  break;
+case BO_Xor:
+  Ret = L ^ R;
+  break;
+case BO_Or:
+  Ret = L | R;
+  break;
+case BO_EQ:
+  Ret = L == R;
+  break;
+case BO_NE:
+  Ret = L != R;
+  break;
+}
+
+return llvm::ConstantInt::get(CGM.getLLVMContext(), Ret);
+  }
+
   // Utility methods
   llvm::Type *ConvertType(QualType T) {
 return CGM.getTypes().ConvertType(T);
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits