[clang-tools-extra] d9b660b - [clangd] Remove unused using decls (NFC)

2023-09-24 Thread Kazu Hirata via cfe-commits

Author: Kazu Hirata
Date: 2023-09-24T00:01:05-07:00
New Revision: d9b660b261e25fd9ce28a9a1461a5fee0a388957

URL: 
https://github.com/llvm/llvm-project/commit/d9b660b261e25fd9ce28a9a1461a5fee0a388957
DIFF: 
https://github.com/llvm/llvm-project/commit/d9b660b261e25fd9ce28a9a1461a5fee0a388957.diff

LOG: [clangd] Remove unused using decls (NFC)

Identified with misc-unused-using-decls.

Added: 


Modified: 
clang-tools-extra/clangd/unittests/ClangdLSPServerTests.cpp
clang-tools-extra/clangd/unittests/HeadersTests.cpp
clang-tools-extra/clangd/unittests/ParsedASTTests.cpp
clang-tools-extra/clangd/unittests/PreambleTests.cpp
clang-tools-extra/clangd/unittests/TUSchedulerTests.cpp
clang-tools-extra/clangd/unittests/tweaks/DefineOutlineTests.cpp
clang-tools-extra/clangd/unittests/tweaks/DumpSymbolTests.cpp

Removed: 




diff  --git a/clang-tools-extra/clangd/unittests/ClangdLSPServerTests.cpp 
b/clang-tools-extra/clangd/unittests/ClangdLSPServerTests.cpp
index 15d4be8dd9ba915..1f58e2c3cb547aa 100644
--- a/clang-tools-extra/clangd/unittests/ClangdLSPServerTests.cpp
+++ b/clang-tools-extra/clangd/unittests/ClangdLSPServerTests.cpp
@@ -44,7 +44,6 @@
 namespace clang {
 namespace clangd {
 namespace {
-using llvm::Succeeded;
 using testing::ElementsAre;
 
 MATCHER_P(diagMessage, M, "") {

diff  --git a/clang-tools-extra/clangd/unittests/HeadersTests.cpp 
b/clang-tools-extra/clangd/unittests/HeadersTests.cpp
index 3a9576ff74e41f0..dc6adaee1125718 100644
--- a/clang-tools-extra/clangd/unittests/HeadersTests.cpp
+++ b/clang-tools-extra/clangd/unittests/HeadersTests.cpp
@@ -33,7 +33,6 @@ namespace {
 using ::testing::AllOf;
 using ::testing::Contains;
 using ::testing::ElementsAre;
-using ::testing::Eq;
 using ::testing::IsEmpty;
 using ::testing::Not;
 using ::testing::UnorderedElementsAre;

diff  --git a/clang-tools-extra/clangd/unittests/ParsedASTTests.cpp 
b/clang-tools-extra/clangd/unittests/ParsedASTTests.cpp
index ec8132645f81fb5..c850d6ed2c0dcd7 100644
--- a/clang-tools-extra/clangd/unittests/ParsedASTTests.cpp
+++ b/clang-tools-extra/clangd/unittests/ParsedASTTests.cpp
@@ -47,7 +47,6 @@ using ::testing::Contains;
 using ::testing::ElementsAre;
 using ::testing::ElementsAreArray;
 using ::testing::IsEmpty;
-using ::testing::UnorderedElementsAreArray;
 
 MATCHER_P(declNamed, Name, "") {
   if (NamedDecl *ND = dyn_cast(arg))

diff  --git a/clang-tools-extra/clangd/unittests/PreambleTests.cpp 
b/clang-tools-extra/clangd/unittests/PreambleTests.cpp
index 3e9ed5304376452..6420516e7855767 100644
--- a/clang-tools-extra/clangd/unittests/PreambleTests.cpp
+++ b/clang-tools-extra/clangd/unittests/PreambleTests.cpp
@@ -43,14 +43,10 @@
 using testing::AllOf;
 using testing::Contains;
 using testing::ElementsAre;
-using testing::ElementsAreArray;
-using testing::Eq;
 using testing::Field;
-using testing::HasSubstr;
 using testing::IsEmpty;
 using testing::Matcher;
 using testing::MatchesRegex;
-using testing::Not;
 using testing::UnorderedElementsAre;
 using testing::UnorderedElementsAreArray;
 

diff  --git a/clang-tools-extra/clangd/unittests/TUSchedulerTests.cpp 
b/clang-tools-extra/clangd/unittests/TUSchedulerTests.cpp
index 4f028f2cbe5a14c..43f38e39c89523f 100644
--- a/clang-tools-extra/clangd/unittests/TUSchedulerTests.cpp
+++ b/clang-tools-extra/clangd/unittests/TUSchedulerTests.cpp
@@ -49,7 +49,6 @@ namespace clang {
 namespace clangd {
 namespace {
 
-using ::testing::_;
 using ::testing::AllOf;
 using ::testing::AnyOf;
 using ::testing::Contains;

diff  --git a/clang-tools-extra/clangd/unittests/tweaks/DefineOutlineTests.cpp 
b/clang-tools-extra/clangd/unittests/tweaks/DefineOutlineTests.cpp
index 576377a658678cc..d1e60b070f20e95 100644
--- a/clang-tools-extra/clangd/unittests/tweaks/DefineOutlineTests.cpp
+++ b/clang-tools-extra/clangd/unittests/tweaks/DefineOutlineTests.cpp
@@ -11,8 +11,6 @@
 #include "gmock/gmock.h"
 #include "gtest/gtest.h"
 
-using ::testing::ElementsAre;
-
 namespace clang {
 namespace clangd {
 namespace {

diff  --git a/clang-tools-extra/clangd/unittests/tweaks/DumpSymbolTests.cpp 
b/clang-tools-extra/clangd/unittests/tweaks/DumpSymbolTests.cpp
index 649eb3d046310b8..047b21abc304ba8 100644
--- a/clang-tools-extra/clangd/unittests/tweaks/DumpSymbolTests.cpp
+++ b/clang-tools-extra/clangd/unittests/tweaks/DumpSymbolTests.cpp
@@ -12,7 +12,6 @@
 #include 
 
 using ::testing::AllOf;
-using ::testing::HasSubstr;
 using ::testing::StartsWith;
 
 namespace clang {



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


[clang] [Clang] Fix CXXRewrittenBinaryOperator::getDecomposedForm to handle case when spaceship operator returns comparison category by reference (PR #66270)

2023-09-24 Thread Timm Baeder via cfe-commits

tbaederr wrote:

Ping @shafik, just needs a rebase :)

https://github.com/llvm/llvm-project/pull/66270
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Driver] Some adjustments for reloctable linking on OpenBSD (PR #67254)

2023-09-24 Thread Raul Tambre via cfe-commits

tambry wrote:

reloctable→relocatable

Just a fly-by, but IMO the commit message ought to describe the change – "some 
changes" doesn't give much insight.

https://github.com/llvm/llvm-project/pull/67254
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] 3935a18 - [clang][Interp][NFC] Use GetPtrThisField intead of two ops

2023-09-24 Thread Timm Bäder via cfe-commits

Author: Timm Bäder
Date: 2023-09-24T09:56:48+02:00
New Revision: 3935a18a5c22e07a3afe31afd007457d3cec8133

URL: 
https://github.com/llvm/llvm-project/commit/3935a18a5c22e07a3afe31afd007457d3cec8133
DIFF: 
https://github.com/llvm/llvm-project/commit/3935a18a5c22e07a3afe31afd007457d3cec8133.diff

LOG: [clang][Interp][NFC] Use GetPtrThisField intead of two ops

Added: 


Modified: 
clang/lib/AST/Interp/ByteCodeStmtGen.cpp

Removed: 




diff  --git a/clang/lib/AST/Interp/ByteCodeStmtGen.cpp 
b/clang/lib/AST/Interp/ByteCodeStmtGen.cpp
index 22a6908daf8b3c2..15eae8e20b3a678 100644
--- a/clang/lib/AST/Interp/ByteCodeStmtGen.cpp
+++ b/clang/lib/AST/Interp/ByteCodeStmtGen.cpp
@@ -171,10 +171,7 @@ bool ByteCodeStmtGen::visitFunc(const 
FunctionDecl *F) {
 } else {
   // Non-primitive case. Get a pointer to the field-to-initialize
   // on the stack and call visitInitialzer() for it.
-  if (!this->emitThis(InitExpr))
-return false;
-
-  if (!this->emitGetPtrField(F->Offset, InitExpr))
+  if (!this->emitGetPtrThisField(F->Offset, InitExpr))
 return false;
 
   if (!this->visitInitializer(InitExpr))



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


[clang] c97c28d - [clang][Interp][NFC] Make a local variable const

2023-09-24 Thread Timm Bäder via cfe-commits

Author: Timm Bäder
Date: 2023-09-24T09:56:49+02:00
New Revision: c97c28dfb3608a78cb70e99ecdc798f65941d46e

URL: 
https://github.com/llvm/llvm-project/commit/c97c28dfb3608a78cb70e99ecdc798f65941d46e
DIFF: 
https://github.com/llvm/llvm-project/commit/c97c28dfb3608a78cb70e99ecdc798f65941d46e.diff

LOG: [clang][Interp][NFC] Make a local variable const

Added: 


Modified: 
clang/lib/AST/Interp/Function.cpp

Removed: 




diff  --git a/clang/lib/AST/Interp/Function.cpp 
b/clang/lib/AST/Interp/Function.cpp
index e409002171c4b73..0b7cfc4e28883f0 100644
--- a/clang/lib/AST/Interp/Function.cpp
+++ b/clang/lib/AST/Interp/Function.cpp
@@ -44,7 +44,7 @@ SourceInfo Function::getSource(CodePtr PC) const {
 }
 
 bool Function::isVirtual() const {
-  if (auto *M = dyn_cast(F))
+  if (const auto *M = dyn_cast(F))
 return M->isVirtual();
   return false;
 }



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


[clang] f4aabc2 - [clang][Interp][NFC] Clean up EvalEmitter includes

2023-09-24 Thread Timm Bäder via cfe-commits

Author: Timm Bäder
Date: 2023-09-24T10:22:34+02:00
New Revision: f4aabc2d9d559decc7a8e0fcfb88e657194595e4

URL: 
https://github.com/llvm/llvm-project/commit/f4aabc2d9d559decc7a8e0fcfb88e657194595e4
DIFF: 
https://github.com/llvm/llvm-project/commit/f4aabc2d9d559decc7a8e0fcfb88e657194595e4.diff

LOG: [clang][Interp][NFC] Clean up EvalEmitter includes

Added: 


Modified: 
clang/lib/AST/Interp/EvalEmitter.cpp
clang/lib/AST/Interp/EvalEmitter.h

Removed: 




diff  --git a/clang/lib/AST/Interp/EvalEmitter.cpp 
b/clang/lib/AST/Interp/EvalEmitter.cpp
index d26ee8e40a437b9..bdf800c60f1723f 100644
--- a/clang/lib/AST/Interp/EvalEmitter.cpp
+++ b/clang/lib/AST/Interp/EvalEmitter.cpp
@@ -7,18 +7,15 @@
 
//===--===//
 
 #include "EvalEmitter.h"
+#include "ByteCodeGenError.h"
 #include "Context.h"
 #include "Interp.h"
 #include "Opcode.h"
-#include "Program.h"
 #include "clang/AST/DeclCXX.h"
 
 using namespace clang;
 using namespace clang::interp;
 
-using APSInt = llvm::APSInt;
-template  using Expected = llvm::Expected;
-
 EvalEmitter::EvalEmitter(Context &Ctx, Program &P, State &Parent,
  InterpStack &Stk, APValue &Result)
 : Ctx(Ctx), P(P), S(Parent, P, Stk, Ctx, this), Result(Result) {

diff  --git a/clang/lib/AST/Interp/EvalEmitter.h 
b/clang/lib/AST/Interp/EvalEmitter.h
index 1f392a13b3ccf73..c63e46bbf347bdf 100644
--- a/clang/lib/AST/Interp/EvalEmitter.h
+++ b/clang/lib/AST/Interp/EvalEmitter.h
@@ -13,12 +13,8 @@
 #ifndef LLVM_CLANG_AST_INTERP_EVALEMITTER_H
 #define LLVM_CLANG_AST_INTERP_EVALEMITTER_H
 
-#include "ByteCodeGenError.h"
-#include "Context.h"
-#include "InterpStack.h"
 #include "InterpState.h"
 #include "PrimType.h"
-#include "Program.h"
 #include "Source.h"
 #include "llvm/Support/Error.h"
 
@@ -26,9 +22,8 @@ namespace clang {
 namespace interp {
 class Context;
 class Function;
-class InterpState;
+class InterpStack;
 class Program;
-class SourceInfo;
 enum Opcode : uint32_t;
 
 /// An emitter which evaluates opcodes as they are emitted.



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


[clang] a7fdbec - [clang][Interp][NFC] Clean up Context includes

2023-09-24 Thread Timm Bäder via cfe-commits

Author: Timm Bäder
Date: 2023-09-24T10:40:17+02:00
New Revision: a7fdbecfb3ce2446e6016a9c868805c97d8f1468

URL: 
https://github.com/llvm/llvm-project/commit/a7fdbecfb3ce2446e6016a9c868805c97d8f1468
DIFF: 
https://github.com/llvm/llvm-project/commit/a7fdbecfb3ce2446e6016a9c868805c97d8f1468.diff

LOG: [clang][Interp][NFC] Clean up Context includes

Added: 


Modified: 
clang/lib/AST/Interp/ByteCodeEmitter.cpp
clang/lib/AST/Interp/ByteCodeEmitter.h
clang/lib/AST/Interp/Context.cpp
clang/lib/AST/Interp/Context.h

Removed: 




diff  --git a/clang/lib/AST/Interp/ByteCodeEmitter.cpp 
b/clang/lib/AST/Interp/ByteCodeEmitter.cpp
index 9b6adcc29bac5f1..14593fbffa0e99d 100644
--- a/clang/lib/AST/Interp/ByteCodeEmitter.cpp
+++ b/clang/lib/AST/Interp/ByteCodeEmitter.cpp
@@ -7,6 +7,7 @@
 
//===--===//
 
 #include "ByteCodeEmitter.h"
+#include "ByteCodeGenError.h"
 #include "Context.h"
 #include "Floating.h"
 #include "Opcode.h"
@@ -18,9 +19,6 @@
 using namespace clang;
 using namespace clang::interp;
 
-using APSInt = llvm::APSInt;
-using Error = llvm::Error;
-
 Expected
 ByteCodeEmitter::compileFunc(const FunctionDecl *FuncDecl) {
   // Set up argument indices.

diff  --git a/clang/lib/AST/Interp/ByteCodeEmitter.h 
b/clang/lib/AST/Interp/ByteCodeEmitter.h
index c917622a1864fac..d184484a9197f93 100644
--- a/clang/lib/AST/Interp/ByteCodeEmitter.h
+++ b/clang/lib/AST/Interp/ByteCodeEmitter.h
@@ -13,10 +13,7 @@
 #ifndef LLVM_CLANG_AST_INTERP_LINKEMITTER_H
 #define LLVM_CLANG_AST_INTERP_LINKEMITTER_H
 
-#include "ByteCodeGenError.h"
 #include "Context.h"
-#include "InterpStack.h"
-#include "InterpState.h"
 #include "PrimType.h"
 #include "Program.h"
 #include "Source.h"
@@ -24,8 +21,6 @@
 
 namespace clang {
 namespace interp {
-class Context;
-class SourceInfo;
 enum Opcode : uint32_t;
 
 /// An emitter which links the program to bytecode for later use.

diff  --git a/clang/lib/AST/Interp/Context.cpp 
b/clang/lib/AST/Interp/Context.cpp
index 1a732b6c1a092ac..1c1aacbc8bb9e7d 100644
--- a/clang/lib/AST/Interp/Context.cpp
+++ b/clang/lib/AST/Interp/Context.cpp
@@ -9,6 +9,7 @@
 #include "Context.h"
 #include "ByteCodeEmitter.h"
 #include "ByteCodeExprGen.h"
+#include "ByteCodeGenError.h"
 #include "ByteCodeStmtGen.h"
 #include "EvalEmitter.h"
 #include "Interp.h"

diff  --git a/clang/lib/AST/Interp/Context.h b/clang/lib/AST/Interp/Context.h
index 617fbaa928f40ee..958b50b1615ad18 100644
--- a/clang/lib/AST/Interp/Context.h
+++ b/clang/lib/AST/Interp/Context.h
@@ -17,13 +17,13 @@
 #define LLVM_CLANG_AST_INTERP_CONTEXT_H
 
 #include "InterpStack.h"
-#include "clang/AST/APValue.h"
 
 namespace clang {
 class ASTContext;
 class LangOptions;
 class FunctionDecl;
 class VarDecl;
+class APValue;
 
 namespace interp {
 class Function;



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


[clang] [InstCombine] Add combines/simplifications for `llvm.ptrmask` (PR #67166)

2023-09-24 Thread Yingwei Zheng via cfe-commits


@@ -1964,18 +1964,78 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst 
&CI) {
 break;
   }
   case Intrinsic::ptrmask: {
+KnownBits Known(DL.getPointerTypeSizeInBits(II->getType()));
+if (SimplifyDemandedInstructionBits(*II, Known))
+  return II;
+
+Value *Op0 = II->getArgOperand(0);
+Value *Op1 = II->getArgOperand(1);
+// Fail loudly in case this is ever changed.
+// TODO: If vector types are supported the merging of (ptrmask (ptrmask))
+// need to ensure we don't merge a vectype with non-vec type.
+assert(!Op0->getType()->isVectorTy() && !Op1->getType()->isVectorTy() &&
+   "These combines where written at a time when ptrmask did not "
+   "support vector types and may not work for vectors");
+
 Value *InnerPtr, *InnerMask;
-if (match(II->getArgOperand(0),
-  m_OneUse(m_Intrinsic(m_Value(InnerPtr),
-   m_Value(InnerMask) {
-  if (II->getArgOperand(1)->getType() == InnerMask->getType()) {
-Value *NewMask = Builder.CreateAnd(II->getArgOperand(1), InnerMask);
-return replaceInstUsesWith(
-*II,
-Builder.CreateIntrinsic(InnerPtr->getType(), Intrinsic::ptrmask,
-{InnerPtr, NewMask}));
+bool Changed = false;
+// Combine:
+// (ptrmask (ptrmask p, A), B)
+//-> (ptrmask p, (and A, B))
+if (match(Op0, m_OneUse(m_Intrinsic(
+   m_Value(InnerPtr), m_Value(InnerMask) {
+  // See if combining the two masks is free.
+  bool OkayToMerge = InnerMask->getType() == Op1->getType();
+  bool NeedsNew = false;
+  if (!OkayToMerge) {
+if (match(InnerMask, m_ImmConstant())) {
+  InnerMask = Builder.CreateZExtOrTrunc(InnerMask, Op1->getType());
+  OkayToMerge = true;
+} else if (match(Op1, m_ImmConstant())) {
+  Op1 = Builder.CreateZExtOrTrunc(Op1, InnerMask->getType());
+  OkayToMerge = true;
+  // Need to create a new one here, as the intrinsic id needs to 
change.
+  NeedsNew = true;
+}
+  }
+  if (InnerMask->getType() == Op1->getType()) {

dtcxzyw wrote:

`OkayToMerge` is unused?

https://github.com/llvm/llvm-project/pull/67166
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clangd] fix compilation errors with newer versions of gRPC (PR #67258)

2023-09-24 Thread via cfe-commits

https://github.com/0xacc created https://github.com/llvm/llvm-project/pull/67258

in recent versions of gRPC, header files with prefix `grpc++` are deprecated.

gRPC headers in include/grpc++ are deprecated since gRPC 1.10.0 
https://github.com/grpc/grpc/releases/tag/v1.10.0 , this PR should close 
https://github.com/clangd/clangd/issues/1603

>From fe6be33d9204f3916bd014c53f0685f04371e2c8 Mon Sep 17 00:00:00 2001
From: 0xacc <82801911+0x...@users.noreply.github.com>
Date: Sun, 24 Sep 2023 11:29:28 +0800
Subject: [PATCH] [clangd] fix compilation errors with newer versions of gRPC

in recent versions of gRPC, header files with prefix `grpc++` are deprecated
---
 clang-tools-extra/clangd/index/remote/Client.cpp  | 2 +-
 clang-tools-extra/clangd/index/remote/monitor/Monitor.cpp | 2 +-
 clang-tools-extra/clangd/index/remote/server/Server.cpp   | 6 +++---
 3 files changed, 5 insertions(+), 5 deletions(-)

diff --git a/clang-tools-extra/clangd/index/remote/Client.cpp 
b/clang-tools-extra/clangd/index/remote/Client.cpp
index 391da3916259c67..bc95d0787fdb74e 100644
--- a/clang-tools-extra/clangd/index/remote/Client.cpp
+++ b/clang-tools-extra/clangd/index/remote/Client.cpp
@@ -6,7 +6,7 @@
 //
 
//===--===//
 
-#include 
+#include 
 
 #include "Client.h"
 #include "Feature.h"
diff --git a/clang-tools-extra/clangd/index/remote/monitor/Monitor.cpp 
b/clang-tools-extra/clangd/index/remote/monitor/Monitor.cpp
index 9a58b5871bfce1a..ad391573804f4da 100644
--- a/clang-tools-extra/clangd/index/remote/monitor/Monitor.cpp
+++ b/clang-tools-extra/clangd/index/remote/monitor/Monitor.cpp
@@ -16,7 +16,7 @@
 
 #include 
 #include 
-#include 
+#include 
 
 namespace clang {
 namespace clangd {
diff --git a/clang-tools-extra/clangd/index/remote/server/Server.cpp 
b/clang-tools-extra/clangd/index/remote/server/Server.cpp
index e108d4d0b057ba2..ad075c4446cad18 100644
--- a/clang-tools-extra/clangd/index/remote/server/Server.cpp
+++ b/clang-tools-extra/clangd/index/remote/server/Server.cpp
@@ -33,8 +33,8 @@
 #include "llvm/Support/VirtualFileSystem.h"
 
 #include 
-#include 
-#include 
+#include 
+#include 
 #include 
 #include 
 #include 
@@ -42,7 +42,7 @@
 #include 
 
 #if ENABLE_GRPC_REFLECTION
-#include 
+#include 
 #endif
 
 #ifdef __GLIBC__

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


[clang] [RISCV] Support floating point VCIX (PR #67094)

2023-09-24 Thread Brandon Wu via cfe-commits


@@ -2441,11 +2441,11 @@ define void @test_sf_vc_fvv_se_e16mf4( %vd, 
 ; CHECK-NEXT:sf.vc.fvv 1, v8, v9, fa0
 ; CHECK-NEXT:ret
 entry:
-  tail call void @llvm.riscv.sf.vc.fvv.se.iXLen.nxv1i16.f16.iXLen(iXLen 1, 
 %vd,  %vs2, half %fs1, iXLen %vl)
+  tail call void @llvm.riscv.sf.vc.fvv.se.iXLen.nxv1i16.i16.iXLen(iXLen 1, 
 %vd,  %vs2, half %fs1, iXLen %vl)

4vtomat wrote:

Oh, I accidentally changed the type, it should be f16 lol~

https://github.com/llvm/llvm-project/pull/67094
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [RISCV] Support floating point VCIX (PR #67094)

2023-09-24 Thread Brandon Wu via cfe-commits

4vtomat wrote:

> The target is support LLVM IR part only, we would like to prevent expose that 
> on the C intrinsic level if possible, because that's intentionally to expose 
> vector with unsigned integer only.

Sure~

https://github.com/llvm/llvm-project/pull/67094
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [ValueTracking] Simplify uaddo pattern (PR #65910)

2023-09-24 Thread Yingwei Zheng via cfe-commits

https://github.com/dtcxzyw updated 
https://github.com/llvm/llvm-project/pull/65910

>From 9bb817aa40dd9bc1bbb18b4cf4bc079145c8ecaa Mon Sep 17 00:00:00 2001
From: Yingwei Zheng 
Date: Mon, 11 Sep 2023 03:58:02 +0800
Subject: [PATCH 1/2] [ValueTracking] Simplify uaddo pattern

---
 llvm/lib/Analysis/ValueTracking.cpp   |  23 
 .../InstSimplify/and-or-implied-cond.ll   | 104 ++
 2 files changed, 127 insertions(+)

diff --git a/llvm/lib/Analysis/ValueTracking.cpp 
b/llvm/lib/Analysis/ValueTracking.cpp
index c4153b824c37e0a..5aaff4ee81be969 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -8291,6 +8291,29 @@ static std::optional isImpliedCondICmps(const 
ICmpInst *LHS,
   if (L0 == R0 && match(L1, m_APInt(LC)) && match(R1, m_APInt(RC)))
 return isImpliedCondCommonOperandWithConstants(LPred, *LC, RPred, *RC);
 
+  // L0 = R0 = L1 + R1, L0 >=u L1 implies R0 >=u R1, L0 From 83fa03f89c69a0a13336f6761023bb1415f92505 Mon Sep 17 00:00:00 2001
From: Yingwei Zheng 
Date: Sun, 24 Sep 2023 17:04:32 +0800
Subject: [PATCH 2/2] fixup! [ValueTracking] Simplify uaddo pattern

---
 llvm/lib/Analysis/ValueTracking.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/llvm/lib/Analysis/ValueTracking.cpp 
b/llvm/lib/Analysis/ValueTracking.cpp
index 5aaff4ee81be969..f2278f3b7097dac 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -8308,7 +8308,7 @@ static std::optional isImpliedCondICmps(const 
ICmpInst *LHS,
   RPred = ICmpInst::getSwappedPredicate(RPred);
 }
 if (L0 == R0 &&
-(LPred == ICmpInst::ICMP_UGE || LPred == ICmpInst::ICMP_ULT) &&
+(LPred == ICmpInst::ICMP_ULT || LPred == ICmpInst::ICMP_UGE) &&
 (RPred == ICmpInst::ICMP_ULT || RPred == ICmpInst::ICMP_UGE) &&
 match(L0, m_c_Add(m_Specific(L1), m_Specific(R1
   return LPred == RPred;

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


[clang-tools-extra] [ValueTracking] Simplify uaddo pattern (PR #65910)

2023-09-24 Thread Yingwei Zheng via cfe-commits

https://github.com/dtcxzyw updated 
https://github.com/llvm/llvm-project/pull/65910

>From 9bb817aa40dd9bc1bbb18b4cf4bc079145c8ecaa Mon Sep 17 00:00:00 2001
From: Yingwei Zheng 
Date: Mon, 11 Sep 2023 03:58:02 +0800
Subject: [PATCH 1/2] [ValueTracking] Simplify uaddo pattern

---
 llvm/lib/Analysis/ValueTracking.cpp   |  23 
 .../InstSimplify/and-or-implied-cond.ll   | 104 ++
 2 files changed, 127 insertions(+)

diff --git a/llvm/lib/Analysis/ValueTracking.cpp 
b/llvm/lib/Analysis/ValueTracking.cpp
index c4153b824c37e0a..5aaff4ee81be969 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -8291,6 +8291,29 @@ static std::optional isImpliedCondICmps(const 
ICmpInst *LHS,
   if (L0 == R0 && match(L1, m_APInt(LC)) && match(R1, m_APInt(RC)))
 return isImpliedCondCommonOperandWithConstants(LPred, *LC, RPred, *RC);
 
+  // L0 = R0 = L1 + R1, L0 >=u L1 implies R0 >=u R1, L0 From 83fa03f89c69a0a13336f6761023bb1415f92505 Mon Sep 17 00:00:00 2001
From: Yingwei Zheng 
Date: Sun, 24 Sep 2023 17:04:32 +0800
Subject: [PATCH 2/2] fixup! [ValueTracking] Simplify uaddo pattern

---
 llvm/lib/Analysis/ValueTracking.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/llvm/lib/Analysis/ValueTracking.cpp 
b/llvm/lib/Analysis/ValueTracking.cpp
index 5aaff4ee81be969..f2278f3b7097dac 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -8308,7 +8308,7 @@ static std::optional isImpliedCondICmps(const 
ICmpInst *LHS,
   RPred = ICmpInst::getSwappedPredicate(RPred);
 }
 if (L0 == R0 &&
-(LPred == ICmpInst::ICMP_UGE || LPred == ICmpInst::ICMP_ULT) &&
+(LPred == ICmpInst::ICMP_ULT || LPred == ICmpInst::ICMP_UGE) &&
 (RPred == ICmpInst::ICMP_ULT || RPred == ICmpInst::ICMP_UGE) &&
 match(L0, m_c_Add(m_Specific(L1), m_Specific(R1
   return LPred == RPred;

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


[clang] [ValueTracking] Simplify uaddo pattern (PR #65910)

2023-09-24 Thread Yingwei Zheng via cfe-commits

https://github.com/dtcxzyw resolved 
https://github.com/llvm/llvm-project/pull/65910
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [ValueTracking] Simplify uaddo pattern (PR #65910)

2023-09-24 Thread Yingwei Zheng via cfe-commits

https://github.com/dtcxzyw resolved 
https://github.com/llvm/llvm-project/pull/65910
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [ValueTracking] Simplify uaddo pattern (PR #65910)

2023-09-24 Thread Yingwei Zheng via cfe-commits


@@ -8291,6 +8291,29 @@ static std::optional isImpliedCondICmps(const 
ICmpInst *LHS,
   if (L0 == R0 && match(L1, m_APInt(LC)) && match(R1, m_APInt(RC)))
 return isImpliedCondCommonOperandWithConstants(LPred, *LC, RPred, *RC);
 
+  // L0 = R0 = L1 + R1, L0 >=u L1 implies R0 >=u R1, L0 u L1 -> R0 >u R1` and  `L0 <=u L1 -> R0 <=u R1`.
Alive2: https://alive2.llvm.org/ce/z/VKtFkD

https://github.com/llvm/llvm-project/pull/65910
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [ValueTracking] Simplify uaddo pattern (PR #65910)

2023-09-24 Thread Yingwei Zheng via cfe-commits


@@ -8291,6 +8291,29 @@ static std::optional isImpliedCondICmps(const 
ICmpInst *LHS,
   if (L0 == R0 && match(L1, m_APInt(LC)) && match(R1, m_APInt(RC)))
 return isImpliedCondCommonOperandWithConstants(LPred, *LC, RPred, *RC);
 
+  // L0 = R0 = L1 + R1, L0 >=u L1 implies R0 >=u R1, L0 u L1 -> R0 >u R1` and  `L0 <=u L1 -> R0 <=u R1`.
Alive2: https://alive2.llvm.org/ce/z/VKtFkD

https://github.com/llvm/llvm-project/pull/65910
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [ValueTracking] Simplify uaddo pattern (PR #65910)

2023-09-24 Thread Yingwei Zheng via cfe-commits

https://github.com/dtcxzyw resolved 
https://github.com/llvm/llvm-project/pull/65910
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [InstCombine] Add combines/simplifications for `llvm.ptrmask` (PR #67166)

2023-09-24 Thread Nikita Popov via cfe-commits


@@ -6397,6 +6397,48 @@ static Value *simplifyBinaryIntrinsic(Function *F, Value 
*Op0, Value *Op1,
   return Constant::getNullValue(ReturnType);
 break;
   }
+  case Intrinsic::ptrmask: {
+// Fail loudly in case this is ever changed.
+// TODO: If vector types are supported the logic that checks if the mask is
+// useless should be updated to use generic constants.
+assert(!Op0->getType()->isVectorTy() && !Op1->getType()->isVectorTy() &&
+   "These simplifications where written at a time when ptrmask did not 
"
+   "support vector types and may not work for vectors");

nikic wrote:

As far as I can see all the code here is already compatible with vectors, so 
not sure this assert makes sense (and I don't understand what your TODO is 
referring to either).

https://github.com/llvm/llvm-project/pull/67166
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [InstCombine] Add combines/simplifications for `llvm.ptrmask` (PR #67166)

2023-09-24 Thread Nikita Popov via cfe-commits


@@ -6397,6 +6397,48 @@ static Value *simplifyBinaryIntrinsic(Function *F, Value 
*Op0, Value *Op1,
   return Constant::getNullValue(ReturnType);
 break;
   }
+  case Intrinsic::ptrmask: {
+// Fail loudly in case this is ever changed.
+// TODO: If vector types are supported the logic that checks if the mask is
+// useless should be updated to use generic constants.
+assert(!Op0->getType()->isVectorTy() && !Op1->getType()->isVectorTy() &&
+   "These simplifications where written at a time when ptrmask did not 
"
+   "support vector types and may not work for vectors");
+
+// NOTE: We can't apply these simplifications based on the value of Op1
+// because we need to preserve provenance.
+if (isa(Op0))

nikic wrote:

You can also return poison if op1 is poison.

https://github.com/llvm/llvm-project/pull/67166
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [InstCombine] Add combines/simplifications for `llvm.ptrmask` (PR #67166)

2023-09-24 Thread Nikita Popov via cfe-commits


@@ -1984,10 +1984,30 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst 
&CI) {
 //-> (ptrmask p, (and A, B))
 if (match(Op0, m_OneUse(m_Intrinsic(
m_Value(InnerPtr), m_Value(InnerMask) {
+  // See if combining the two masks is free.
+  bool OkayToMerge = InnerMask->getType() == Op1->getType();
+  bool NeedsNew = false;
+  if (!OkayToMerge) {
+if (match(InnerMask, m_ImmConstant())) {
+  InnerMask = Builder.CreateZExtOrTrunc(InnerMask, Op1->getType());
+  OkayToMerge = true;
+} else if (match(Op1, m_ImmConstant())) {
+  Op1 = Builder.CreateZExtOrTrunc(Op1, InnerMask->getType());
+  OkayToMerge = true;
+  // Need to create a new one here, as the intrinsic id needs to 
change.
+  NeedsNew = true;
+}
+  }

nikic wrote:

This is the wrong approach. You should be canonicalizing the second operand to 
match the pointer size instead and then let the usual folds handle the rest.

https://github.com/llvm/llvm-project/pull/67166
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [InstCombine] Add combines/simplifications for `llvm.ptrmask` (PR #67166)

2023-09-24 Thread Nikita Popov via cfe-commits


@@ -1985,6 +1985,28 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst 
&CI) {
 {InnerPtr, NewMask}));
   }
 }
+bool Changed = false;
+KnownBits Known = computeKnownBits(II, /*Depth*/ 0, II);
+// See if we can deduce non-null.
+if (!CI.hasRetAttr(Attribute::NonNull) &&
+(Known.isNonZero() ||
+ isKnownNonZero(II, DL, /*Depth*/ 0, &AC, II, &DT))) {
+  CI.addRetAttr(Attribute::NonNull);
+  Changed = true;
+}
+
+// Known bits will capture if we had alignment information assosiated with
+// the pointer argument.
+if (Known.countMinTrailingZeros() > Log2(CI.getRetAlign().valueOrOne())) {
+  if (CI.hasRetAttr(Attribute::Alignment))
+CI.removeRetAttr(Attribute::Alignment);
+  CI.addRetAttr(
+  Attribute::get(CI.getContext(), Attribute::Alignment,
+ uint64_t(1) << Known.countMinTrailingZeros()));

nikic wrote:

Attribute::getWithAlignment(). Also you need to protect against 
MaximumAlignment here.

https://github.com/llvm/llvm-project/pull/67166
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [InstCombine] Add combines/simplifications for `llvm.ptrmask` (PR #67166)

2023-09-24 Thread Nikita Popov via cfe-commits


@@ -1985,6 +1985,28 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst 
&CI) {
 {InnerPtr, NewMask}));
   }
 }
+bool Changed = false;
+KnownBits Known = computeKnownBits(II, /*Depth*/ 0, II);
+// See if we can deduce non-null.
+if (!CI.hasRetAttr(Attribute::NonNull) &&
+(Known.isNonZero() ||
+ isKnownNonZero(II, DL, /*Depth*/ 0, &AC, II, &DT))) {
+  CI.addRetAttr(Attribute::NonNull);
+  Changed = true;
+}
+
+// Known bits will capture if we had alignment information assosiated with

nikic wrote:

associated

https://github.com/llvm/llvm-project/pull/67166
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [InstCombine] Add combines/simplifications for `llvm.ptrmask` (PR #67166)

2023-09-24 Thread Nikita Popov via cfe-commits


@@ -1985,6 +1985,28 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst 
&CI) {
 {InnerPtr, NewMask}));
   }
 }
+bool Changed = false;
+KnownBits Known = computeKnownBits(II, /*Depth*/ 0, II);
+// See if we can deduce non-null.
+if (!CI.hasRetAttr(Attribute::NonNull) &&
+(Known.isNonZero() ||
+ isKnownNonZero(II, DL, /*Depth*/ 0, &AC, II, &DT))) {
+  CI.addRetAttr(Attribute::NonNull);
+  Changed = true;
+}
+
+// Known bits will capture if we had alignment information assosiated with
+// the pointer argument.
+if (Known.countMinTrailingZeros() > Log2(CI.getRetAlign().valueOrOne())) {
+  if (CI.hasRetAttr(Attribute::Alignment))

nikic wrote:

No need to call hasRetAttr

https://github.com/llvm/llvm-project/pull/67166
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [InstCombine] Add combines/simplifications for `llvm.ptrmask` (PR #67166)

2023-09-24 Thread Nikita Popov via cfe-commits


@@ -6397,6 +6397,48 @@ static Value *simplifyBinaryIntrinsic(Function *F, Value 
*Op0, Value *Op1,
   return Constant::getNullValue(ReturnType);
 break;
   }
+  case Intrinsic::ptrmask: {
+// Fail loudly in case this is ever changed.
+// TODO: If vector types are supported the logic that checks if the mask is
+// useless should be updated to use generic constants.
+assert(!Op0->getType()->isVectorTy() && !Op1->getType()->isVectorTy() &&
+   "These simplifications where written at a time when ptrmask did not 
"
+   "support vector types and may not work for vectors");
+
+// NOTE: We can't apply these simplifications based on the value of Op1
+// because we need to preserve provenance.
+if (isa(Op0))
+  return Op0;
+
+if (Q.isUndefValue(Op0))
+  return Constant::getNullValue(Op0->getType());
+
+if (match(Op0, m_Zero()))
+  return Constant::getNullValue(Op0->getType());
+
+if (Op1->getType()->getScalarSizeInBits() ==
+Q.DL.getPointerTypeSizeInBits(Op0->getType())) {
+  if (match(Op1, m_PtrToInt(m_Specific(Op0
+return Op0;
+
+  // TODO: We may have attributes assosiated with the return value of the
+  // llvm.ptrmask intrinsic that will be lost when we just return the
+  // operand. We should try to preserve them.
+  if (match(Op1, m_AllOnes()))
+return Op0;

nikic wrote:

You can also handle undef op1 like -1 here.

https://github.com/llvm/llvm-project/pull/67166
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [InstCombine] Add combines/simplifications for `llvm.ptrmask` (PR #67166)

2023-09-24 Thread Nikita Popov via cfe-commits


@@ -898,6 +910,53 @@ Value *InstCombinerImpl::SimplifyDemandedUseBits(Value *V, 
APInt DemandedMask,
 }
 break;
   }
+  case Intrinsic::ptrmask: {
+// Fail loudly in case this is ever changed.
+// Likely not much needs to be changed here to support vector types.
+assert(!I->getOperand(0)->getType()->isVectorTy() &&
+   !I->getOperand(1)->getType()->isVectorTy() &&
+   "These simplifications where written at a time when ptrmask did 
"
+   "not support vector types and may not work for vectors");
+
+unsigned MaskWidth = 
I->getOperand(1)->getType()->getScalarSizeInBits();
+RHSKnown = KnownBits(MaskWidth);
+// If either the LHS or the RHS are Zero, the result is zero.
+if (SimplifyDemandedBits(I, 0, DemandedMask, LHSKnown, Depth + 1) ||
+SimplifyDemandedBits(
+I, 1, (DemandedMask & ~LHSKnown.Zero).zextOrTrunc(MaskWidth),
+RHSKnown, Depth + 1))
+  return I;
+
+RHSKnown = RHSKnown.zextOrTrunc(BitWidth);
+assert(!RHSKnown.hasConflict() && "Bits known to be one AND zero?");
+assert(!LHSKnown.hasConflict() && "Bits known to be one AND zero?");
+
+Known = LHSKnown & RHSKnown;
+KnownBitsComputed = DemandedMask.isAllOnes();

nikic wrote:

This should always be true.

https://github.com/llvm/llvm-project/pull/67166
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Extension: allow recursive macros (PR #65851)

2023-09-24 Thread via cfe-commits

https://github.com/kelbon updated 
https://github.com/llvm/llvm-project/pull/65851

>From 2f807b312baef8c6038c2452b84232acb6d6d2c2 Mon Sep 17 00:00:00 2001
From: Kelbon Nik 
Date: Sat, 9 Sep 2023 17:51:15 +0400
Subject: [PATCH 1/9] add define2 pp directive

---
 clang/include/clang/Basic/TokenKinds.def |  1 +
 clang/include/clang/Lex/MacroInfo.h  | 19 +--
 clang/include/clang/Lex/Preprocessor.h   |  2 +-
 clang/lib/Basic/IdentifierTable.cpp  |  2 ++
 clang/lib/Format/WhitespaceManager.cpp   |  2 +-
 clang/lib/Lex/MacroInfo.cpp  |  3 ++-
 clang/lib/Lex/PPDirectives.cpp   | 16 +++-
 7 files changed, 27 insertions(+), 18 deletions(-)

diff --git a/clang/include/clang/Basic/TokenKinds.def 
b/clang/include/clang/Basic/TokenKinds.def
index 45ebc200b168986..f059d809823ab42 100644
--- a/clang/include/clang/Basic/TokenKinds.def
+++ b/clang/include/clang/Basic/TokenKinds.def
@@ -115,6 +115,7 @@ PPKEYWORD(__include_macros)
 
 // C99 6.10.3 - Macro Replacement.
 PPKEYWORD(define)
+PPKEYWORD(define2)
 PPKEYWORD(undef)
 
 // C99 6.10.4 - Line Control.
diff --git a/clang/include/clang/Lex/MacroInfo.h 
b/clang/include/clang/Lex/MacroInfo.h
index 00c1c3866bbd9ca..4f0c8e987610e50 100644
--- a/clang/include/clang/Lex/MacroInfo.h
+++ b/clang/include/clang/Lex/MacroInfo.h
@@ -102,6 +102,10 @@ class MacroInfo {
   /// like \#define A A.
   bool IsDisabled : 1;
 
+  // True if 'define2' used,
+  // ignores 'IsDisabled' and enables expansion anyway
+  bool AllowRecurse : 1;
+
   /// True if this macro is either defined in the main file and has
   /// been used, or if it is not defined in the main file.
   ///
@@ -278,18 +282,13 @@ class MacroInfo {
   /// Return true if this macro is enabled.
   ///
   /// In other words, that we are not currently in an expansion of this macro.
-  bool isEnabled() const { return !IsDisabled; }
-
-  void EnableMacro() {
-assert(IsDisabled && "Cannot enable an already-enabled macro!");
-IsDisabled = false;
-  }
+  bool isEnabled() const { return AllowRecurse || !IsDisabled; }
+  void setAllowRecursive(bool Allow) { AllowRecurse = Allow; }
+  bool isAllowRecurse() const { return AllowRecurse; }
 
-  void DisableMacro() {
-assert(!IsDisabled && "Cannot disable an already-disabled macro!");
-IsDisabled = true;
-  }
+  void EnableMacro() { IsDisabled = false; }
 
+  void DisableMacro() { IsDisabled = true; }
   /// Determine whether this macro was used for a header guard.
   bool isUsedForHeaderGuard() const { return UsedForHeaderGuard; }
 
diff --git a/clang/include/clang/Lex/Preprocessor.h 
b/clang/include/clang/Lex/Preprocessor.h
index 9efe439bc5f2192..de121ce82fd1d7b 100644
--- a/clang/include/clang/Lex/Preprocessor.h
+++ b/clang/include/clang/Lex/Preprocessor.h
@@ -2754,7 +2754,7 @@ class Preprocessor {
   void replayPreambleConditionalStack();
 
   // Macro handling.
-  void HandleDefineDirective(Token &Tok, bool ImmediatelyAfterHeaderGuard);
+  void HandleDefineDirective(Token &Tok, bool ImmediatelyAfterHeaderGuard, 
bool AllowRecurse);
   void HandleUndefDirective();
 
   // Conditional Inclusion.
diff --git a/clang/lib/Basic/IdentifierTable.cpp 
b/clang/lib/Basic/IdentifierTable.cpp
index afb30268f2973ce..4de3565c8c6d9a8 100644
--- a/clang/lib/Basic/IdentifierTable.cpp
+++ b/clang/lib/Basic/IdentifierTable.cpp
@@ -433,6 +433,8 @@ tok::PPKeywordKind IdentifierInfo::getPPKeywordID() const {
   unsigned Len = getLength();
   if (Len < 2) return tok::pp_not_keyword;
   const char *Name = getNameStart();
+  if (std::string_view(Name, Len) == "define2")
+return tok::pp_define2;
   switch (HASH(Len, Name[0], Name[2])) {
   default: return tok::pp_not_keyword;
   CASE( 2, 'i', '\0', if);
diff --git a/clang/lib/Format/WhitespaceManager.cpp 
b/clang/lib/Format/WhitespaceManager.cpp
index b7bd8d27dc976b1..d8ab76d6761553e 100644
--- a/clang/lib/Format/WhitespaceManager.cpp
+++ b/clang/lib/Format/WhitespaceManager.cpp
@@ -743,7 +743,7 @@ void WhitespaceManager::alignConsecutiveMacros() {
 if (!Current || Current->isNot(tok::identifier))
   return false;
 
-if (!Current->Previous || Current->Previous->isNot(tok::pp_define))
+if (!Current->Previous || !Current->Previous->isOneOf(tok::pp_define, 
tok::pp_define2))
   return false;
 
 // For a macro function, 0 spaces are required between the
diff --git a/clang/lib/Lex/MacroInfo.cpp b/clang/lib/Lex/MacroInfo.cpp
index 39bb0f44eff25ba..9c3619c7c909304 100644
--- a/clang/lib/Lex/MacroInfo.cpp
+++ b/clang/lib/Lex/MacroInfo.cpp
@@ -50,7 +50,7 @@ static_assert(MacroInfoSizeChecker::AsExpected,
 MacroInfo::MacroInfo(SourceLocation DefLoc)
 : Location(DefLoc), IsDefinitionLengthCached(false), IsFunctionLike(false),
   IsC99Varargs(false), IsGNUVarargs(false), IsBuiltinMacro(false),
-  HasCommaPasting(false), IsDisabled(false), IsUsed(false),
+  HasCommaPasting(false), IsDisabled(false), AllowRecurse(false), 
IsUsed(false),
   IsAllowRedefinitionsWi

[clang] Ensure NoTrapAfterNoreturn is false for the wasm backend (PR #65876)

2023-09-24 Thread Matt Harding via cfe-commits

https://github.com/majaha updated 
https://github.com/llvm/llvm-project/pull/65876

>From 7c43c803764bf9e0256d4e3e9f497d2622bb8f69 Mon Sep 17 00:00:00 2001
From: Matt Harding 
Date: Fri, 25 Aug 2023 06:19:14 +0100
Subject: [PATCH 1/8] Add no-trap-after-noreturn flag and wasm tests

Add the command line flag --no-trap-after-noreturn.
Add and improve tests related to WebAssembly's unreachable instruction.
Also fix various typos.
---
 llvm/include/llvm/CodeGen/MachineFunction.h   |   2 +-
 llvm/include/llvm/CodeGen/MachineInstr.h  |   2 +-
 llvm/lib/CodeGen/LLVMTargetMachine.cpp|   7 +
 .../WebAssembly/WebAssemblyCFGStackify.cpp|   2 +-
 llvm/test/CodeGen/WebAssembly/unreachable.ll  | 178 +++---
 5 files changed, 167 insertions(+), 24 deletions(-)

diff --git a/llvm/include/llvm/CodeGen/MachineFunction.h 
b/llvm/include/llvm/CodeGen/MachineFunction.h
index 6c2da626ea54b4d..8f1651c2958e591 100644
--- a/llvm/include/llvm/CodeGen/MachineFunction.h
+++ b/llvm/include/llvm/CodeGen/MachineFunction.h
@@ -266,7 +266,7 @@ class LLVM_EXTERNAL_VISIBILITY MachineFunction {
   // RegInfo - Information about each register in use in the function.
   MachineRegisterInfo *RegInfo;
 
-  // Used to keep track of target-specific per-machine function information for
+  // Used to keep track of target-specific per-machine-function information for
   // the target implementation.
   MachineFunctionInfo *MFInfo;
 
diff --git a/llvm/include/llvm/CodeGen/MachineInstr.h 
b/llvm/include/llvm/CodeGen/MachineInstr.h
index 03fb15f77c65cbb..8367f999fcc76d3 100644
--- a/llvm/include/llvm/CodeGen/MachineInstr.h
+++ b/llvm/include/llvm/CodeGen/MachineInstr.h
@@ -1276,7 +1276,7 @@ class MachineInstr
   /// eraseFromBundle() to erase individual bundled instructions.
   void eraseFromParent();
 
-  /// Unlink 'this' form its basic block and delete it.
+  /// Unlink 'this' from its basic block and delete it.
   ///
   /// If the instruction is part of a bundle, the other instructions in the
   /// bundle remain bundled.
diff --git a/llvm/lib/CodeGen/LLVMTargetMachine.cpp 
b/llvm/lib/CodeGen/LLVMTargetMachine.cpp
index d02ec1db1165d41..aadc3709b85bfb7 100644
--- a/llvm/lib/CodeGen/LLVMTargetMachine.cpp
+++ b/llvm/lib/CodeGen/LLVMTargetMachine.cpp
@@ -37,6 +37,11 @@ static cl::opt
 EnableTrapUnreachable("trap-unreachable", cl::Hidden,
   cl::desc("Enable generating trap for unreachable"));
 
+static cl::opt
+EnableNoTrapAfterNoreturn("no-trap-after-noreturn", cl::Hidden,
+  cl::desc("Do not emit a trap instruction for 
'unreachable' IR instructions "
+  "after noreturn calls, even if 
--trap-unreachable is set."));
+
 void LLVMTargetMachine::initAsmInfo() {
   MRI.reset(TheTarget.createMCRegInfo(getTargetTriple().str()));
   assert(MRI && "Unable to create reg info");
@@ -95,6 +100,8 @@ LLVMTargetMachine::LLVMTargetMachine(const Target &T,
 
   if (EnableTrapUnreachable)
 this->Options.TrapUnreachable = true;
+  if (EnableNoTrapAfterNoreturn)
+this->Options.NoTrapAfterNoreturn = true;
 }
 
 TargetTransformInfo
diff --git a/llvm/lib/Target/WebAssembly/WebAssemblyCFGStackify.cpp 
b/llvm/lib/Target/WebAssembly/WebAssemblyCFGStackify.cpp
index 131e99c66fa2e5a..d8cbddf74545da6 100644
--- a/llvm/lib/Target/WebAssembly/WebAssemblyCFGStackify.cpp
+++ b/llvm/lib/Target/WebAssembly/WebAssemblyCFGStackify.cpp
@@ -667,7 +667,7 @@ void 
WebAssemblyCFGStackify::removeUnnecessaryInstrs(MachineFunction &MF) {
 
   // When there is an unconditional branch right before a catch instruction and
   // it branches to the end of end_try marker, we don't need the branch, 
because
-  // it there is no exception, the control flow transfers to that point anyway.
+  // if there is no exception, the control flow transfers to that point anyway.
   // bb0:
   //   try
   // ...
diff --git a/llvm/test/CodeGen/WebAssembly/unreachable.ll 
b/llvm/test/CodeGen/WebAssembly/unreachable.ll
index ad1c90090ac58bf..1bac30b842e1e05 100644
--- a/llvm/test/CodeGen/WebAssembly/unreachable.ll
+++ b/llvm/test/CodeGen/WebAssembly/unreachable.ll
@@ -1,33 +1,169 @@
-; RUN: llc < %s -asm-verbose=false -verify-machineinstrs | FileCheck %s
-; RUN: llc < %s -asm-verbose=false -fast-isel -fast-isel-abort=1 
-verify-machineinstrs | FileCheck %s
-
-; Test that LLVM unreachable instruction and trap intrinsic are lowered to
-; wasm unreachable
+; RUN: llc < %s -verify-machineinstrs | FileCheck %s --check-prefixes 
CHECK,NORMAL
+; RUN: llc < %s -fast-isel -fast-isel-abort=1 -verify-machineinstrs | 
FileCheck %s --check-prefixes CHECK,NORMAL
+; RUN: llc < %s -verify-machineinstrs --trap-unreachable | FileCheck %s 
--check-prefixes CHECK,NORMAL
+; RUN: llc < %s -fast-isel -fast-isel-abort=1 -verify-machineinstrs 
--trap-unreachable | FileCheck %s --check-prefixes CHECK,NORMAL
+; RUN: llc < %s -verify-machineinstrs --trap-unreachable 
--no-trap-after-noreturn | FileCheck %s --check-pre

[clang-tools-extra] Ensure NoTrapAfterNoreturn is false for the wasm backend (PR #65876)

2023-09-24 Thread Matt Harding via cfe-commits

https://github.com/majaha updated 
https://github.com/llvm/llvm-project/pull/65876

>From 7c43c803764bf9e0256d4e3e9f497d2622bb8f69 Mon Sep 17 00:00:00 2001
From: Matt Harding 
Date: Fri, 25 Aug 2023 06:19:14 +0100
Subject: [PATCH 1/8] Add no-trap-after-noreturn flag and wasm tests

Add the command line flag --no-trap-after-noreturn.
Add and improve tests related to WebAssembly's unreachable instruction.
Also fix various typos.
---
 llvm/include/llvm/CodeGen/MachineFunction.h   |   2 +-
 llvm/include/llvm/CodeGen/MachineInstr.h  |   2 +-
 llvm/lib/CodeGen/LLVMTargetMachine.cpp|   7 +
 .../WebAssembly/WebAssemblyCFGStackify.cpp|   2 +-
 llvm/test/CodeGen/WebAssembly/unreachable.ll  | 178 +++---
 5 files changed, 167 insertions(+), 24 deletions(-)

diff --git a/llvm/include/llvm/CodeGen/MachineFunction.h 
b/llvm/include/llvm/CodeGen/MachineFunction.h
index 6c2da626ea54b4d..8f1651c2958e591 100644
--- a/llvm/include/llvm/CodeGen/MachineFunction.h
+++ b/llvm/include/llvm/CodeGen/MachineFunction.h
@@ -266,7 +266,7 @@ class LLVM_EXTERNAL_VISIBILITY MachineFunction {
   // RegInfo - Information about each register in use in the function.
   MachineRegisterInfo *RegInfo;
 
-  // Used to keep track of target-specific per-machine function information for
+  // Used to keep track of target-specific per-machine-function information for
   // the target implementation.
   MachineFunctionInfo *MFInfo;
 
diff --git a/llvm/include/llvm/CodeGen/MachineInstr.h 
b/llvm/include/llvm/CodeGen/MachineInstr.h
index 03fb15f77c65cbb..8367f999fcc76d3 100644
--- a/llvm/include/llvm/CodeGen/MachineInstr.h
+++ b/llvm/include/llvm/CodeGen/MachineInstr.h
@@ -1276,7 +1276,7 @@ class MachineInstr
   /// eraseFromBundle() to erase individual bundled instructions.
   void eraseFromParent();
 
-  /// Unlink 'this' form its basic block and delete it.
+  /// Unlink 'this' from its basic block and delete it.
   ///
   /// If the instruction is part of a bundle, the other instructions in the
   /// bundle remain bundled.
diff --git a/llvm/lib/CodeGen/LLVMTargetMachine.cpp 
b/llvm/lib/CodeGen/LLVMTargetMachine.cpp
index d02ec1db1165d41..aadc3709b85bfb7 100644
--- a/llvm/lib/CodeGen/LLVMTargetMachine.cpp
+++ b/llvm/lib/CodeGen/LLVMTargetMachine.cpp
@@ -37,6 +37,11 @@ static cl::opt
 EnableTrapUnreachable("trap-unreachable", cl::Hidden,
   cl::desc("Enable generating trap for unreachable"));
 
+static cl::opt
+EnableNoTrapAfterNoreturn("no-trap-after-noreturn", cl::Hidden,
+  cl::desc("Do not emit a trap instruction for 
'unreachable' IR instructions "
+  "after noreturn calls, even if 
--trap-unreachable is set."));
+
 void LLVMTargetMachine::initAsmInfo() {
   MRI.reset(TheTarget.createMCRegInfo(getTargetTriple().str()));
   assert(MRI && "Unable to create reg info");
@@ -95,6 +100,8 @@ LLVMTargetMachine::LLVMTargetMachine(const Target &T,
 
   if (EnableTrapUnreachable)
 this->Options.TrapUnreachable = true;
+  if (EnableNoTrapAfterNoreturn)
+this->Options.NoTrapAfterNoreturn = true;
 }
 
 TargetTransformInfo
diff --git a/llvm/lib/Target/WebAssembly/WebAssemblyCFGStackify.cpp 
b/llvm/lib/Target/WebAssembly/WebAssemblyCFGStackify.cpp
index 131e99c66fa2e5a..d8cbddf74545da6 100644
--- a/llvm/lib/Target/WebAssembly/WebAssemblyCFGStackify.cpp
+++ b/llvm/lib/Target/WebAssembly/WebAssemblyCFGStackify.cpp
@@ -667,7 +667,7 @@ void 
WebAssemblyCFGStackify::removeUnnecessaryInstrs(MachineFunction &MF) {
 
   // When there is an unconditional branch right before a catch instruction and
   // it branches to the end of end_try marker, we don't need the branch, 
because
-  // it there is no exception, the control flow transfers to that point anyway.
+  // if there is no exception, the control flow transfers to that point anyway.
   // bb0:
   //   try
   // ...
diff --git a/llvm/test/CodeGen/WebAssembly/unreachable.ll 
b/llvm/test/CodeGen/WebAssembly/unreachable.ll
index ad1c90090ac58bf..1bac30b842e1e05 100644
--- a/llvm/test/CodeGen/WebAssembly/unreachable.ll
+++ b/llvm/test/CodeGen/WebAssembly/unreachable.ll
@@ -1,33 +1,169 @@
-; RUN: llc < %s -asm-verbose=false -verify-machineinstrs | FileCheck %s
-; RUN: llc < %s -asm-verbose=false -fast-isel -fast-isel-abort=1 
-verify-machineinstrs | FileCheck %s
-
-; Test that LLVM unreachable instruction and trap intrinsic are lowered to
-; wasm unreachable
+; RUN: llc < %s -verify-machineinstrs | FileCheck %s --check-prefixes 
CHECK,NORMAL
+; RUN: llc < %s -fast-isel -fast-isel-abort=1 -verify-machineinstrs | 
FileCheck %s --check-prefixes CHECK,NORMAL
+; RUN: llc < %s -verify-machineinstrs --trap-unreachable | FileCheck %s 
--check-prefixes CHECK,NORMAL
+; RUN: llc < %s -fast-isel -fast-isel-abort=1 -verify-machineinstrs 
--trap-unreachable | FileCheck %s --check-prefixes CHECK,NORMAL
+; RUN: llc < %s -verify-machineinstrs --trap-unreachable 
--no-trap-after-noreturn | FileCheck %s --check-pre

[clang-tools-extra] [InstCombine] Simplify the pattern `a ne/eq (zext/sext (a ne/eq c))` (PR #65852)

2023-09-24 Thread Yingwei Zheng via cfe-commits

https://github.com/dtcxzyw updated 
https://github.com/llvm/llvm-project/pull/65852

>From d9d8bcbb98e8f5aecb9733329389d61a489bd731 Mon Sep 17 00:00:00 2001
From: Yingwei Zheng 
Date: Sat, 9 Sep 2023 23:07:29 +0800
Subject: [PATCH 1/7] [InstCombine] Simplify the pattern `a ne/eq (zext (a
 ne/eq c))`

---
 .../InstCombine/InstCombineCompares.cpp   |  62 ++
 .../test/Transforms/InstCombine/icmp-range.ll | 181 ++
 2 files changed, 243 insertions(+)

diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp 
b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
index 9fdc46fec631679..837b8e6d2619989 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
@@ -6309,7 +6309,69 @@ Instruction 
*InstCombinerImpl::foldICmpUsingBoolRange(ICmpInst &I) {
   Y->getType()->isIntOrIntVectorTy(1) && Pred == ICmpInst::ICMP_ULE)
 return BinaryOperator::CreateOr(Builder.CreateIsNull(X), Y);
 
+  ICmpInst::Predicate Pred1, Pred2;
   const APInt *C;
+  // icmp eq/ne X, (zext (icmp eq/ne X, C))
+  if (match(&I, m_c_ICmp(Pred1, m_Value(X),
+ m_ZExt(m_ICmp(Pred2, m_Deferred(X), m_APInt(C) &&
+  ICmpInst::isEquality(Pred1) && ICmpInst::isEquality(Pred2)) {
+if (C->isZero()) {
+  if (Pred2 == ICmpInst::ICMP_EQ) {
+// icmp eq X, (zext (icmp eq X, 0)) --> false
+// icmp ne X, (zext (icmp eq X, 0)) --> true
+return replaceInstUsesWith(
+I,
+Constant::getIntegerValue(
+I.getType(),
+APInt(1U, static_cast(Pred1 == ICmpInst::ICMP_NE;
+  } else {
+// icmp eq X, (zext (icmp ne X, 0)) --> icmp ult X, 2
+// icmp ne X, (zext (icmp ne X, 0)) --> icmp ugt X, 1
+return ICmpInst::Create(
+Instruction::ICmp,
+Pred1 == ICmpInst::ICMP_NE ? ICmpInst::ICMP_UGT
+   : ICmpInst::ICMP_ULT,
+X,
+Constant::getIntegerValue(
+X->getType(), APInt(X->getType()->getScalarSizeInBits(),
+Pred1 == ICmpInst::ICMP_NE ? 1 : 2)));
+  }
+} else if (C->isOne()) {
+  if (Pred2 == ICmpInst::ICMP_NE) {
+// icmp eq X, (zext (icmp ne X, 1)) --> false
+// icmp ne X, (zext (icmp ne X, 1)) --> true
+return replaceInstUsesWith(
+I,
+Constant::getIntegerValue(
+I.getType(),
+APInt(1U, static_cast(Pred1 == ICmpInst::ICMP_NE;
+  } else {
+// icmp eq X, (zext (icmp eq X, 1)) --> icmp ult X, 2
+// icmp ne X, (zext (icmp eq X, 1)) --> icmp ugt X, 1
+return ICmpInst::Create(
+Instruction::ICmp,
+Pred1 == ICmpInst::ICMP_NE ? ICmpInst::ICMP_UGT
+   : ICmpInst::ICMP_ULT,
+X,
+Constant::getIntegerValue(
+X->getType(), APInt(X->getType()->getScalarSizeInBits(),
+Pred1 == ICmpInst::ICMP_NE ? 1 : 2)));
+  }
+} else {
+  // C != 0 && C != 1
+  // icmp eq X, (zext (icmp eq X, C)) --> icmp eq X, 0
+  // icmp eq X, (zext (icmp ne X, C)) --> icmp eq X, 1
+  // icmp ne X, (zext (icmp eq X, C)) --> icmp ne X, 0
+  // icmp ne X, (zext (icmp ne X, C)) --> icmp ne X, 1
+  return ICmpInst::Create(
+  Instruction::ICmp, Pred1, X,
+  Constant::getIntegerValue(
+  X->getType(),
+  APInt(X->getType()->getScalarSizeInBits(),
+static_cast(Pred2 == ICmpInst::ICMP_NE;
+}
+  }
+
   if (match(I.getOperand(0), m_c_Add(m_ZExt(m_Value(X)), m_SExt(m_Value(Y 
&&
   match(I.getOperand(1), m_APInt(C)) &&
   X->getType()->isIntOrIntVectorTy(1) &&
diff --git a/llvm/test/Transforms/InstCombine/icmp-range.ll 
b/llvm/test/Transforms/InstCombine/icmp-range.ll
index 4281e09cb0309c8..15424fce33fdeea 100644
--- a/llvm/test/Transforms/InstCombine/icmp-range.ll
+++ b/llvm/test/Transforms/InstCombine/icmp-range.ll
@@ -1034,6 +1034,187 @@ define i1 @icmp_ne_bool_1(ptr %ptr) {
   ret i1 %cmp
 }
 
+define i1 @icmp_ne_zext_eq_zero(i32 %a) {
+; CHECK-LABEL: @icmp_ne_zext_eq_zero(
+; CHECK-NEXT:ret i1 true
+;
+  %cmp = icmp eq i32 %a, 0
+  %conv = zext i1 %cmp to i32
+  %cmp1 = icmp ne i32 %conv, %a
+  ret i1 %cmp1
+}
+
+define i1 @icmp_ne_zext_ne_zero(i32 %a) {
+; CHECK-LABEL: @icmp_ne_zext_ne_zero(
+; CHECK-NEXT:[[CMP1:%.*]] = icmp ugt i32 [[A:%.*]], 1
+; CHECK-NEXT:ret i1 [[CMP1]]
+;
+  %cmp = icmp ne i32 %a, 0
+  %conv = zext i1 %cmp to i32
+  %cmp1 = icmp ne i32 %conv, %a
+  ret i1 %cmp1
+}
+
+define i1 @icmp_eq_zext_eq_zero(i32 %a) {
+; CHECK-LABEL: @icmp_eq_zext_eq_zero(
+; CHECK-NEXT:ret i1 false
+;
+  %cmp = icmp eq i32 %a, 0
+  %conv = zext i1 %cmp to i32
+  %cmp1 = icmp eq i32 %conv, %a
+  ret i1 %cmp1
+}
+
+define i1 @icmp_eq_zext_ne_zero(i32 %

[clang] [InstCombine] Simplify the pattern `a ne/eq (zext/sext (a ne/eq c))` (PR #65852)

2023-09-24 Thread Yingwei Zheng via cfe-commits

https://github.com/dtcxzyw updated 
https://github.com/llvm/llvm-project/pull/65852

>From d9d8bcbb98e8f5aecb9733329389d61a489bd731 Mon Sep 17 00:00:00 2001
From: Yingwei Zheng 
Date: Sat, 9 Sep 2023 23:07:29 +0800
Subject: [PATCH 1/7] [InstCombine] Simplify the pattern `a ne/eq (zext (a
 ne/eq c))`

---
 .../InstCombine/InstCombineCompares.cpp   |  62 ++
 .../test/Transforms/InstCombine/icmp-range.ll | 181 ++
 2 files changed, 243 insertions(+)

diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp 
b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
index 9fdc46fec631679..837b8e6d2619989 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
@@ -6309,7 +6309,69 @@ Instruction 
*InstCombinerImpl::foldICmpUsingBoolRange(ICmpInst &I) {
   Y->getType()->isIntOrIntVectorTy(1) && Pred == ICmpInst::ICMP_ULE)
 return BinaryOperator::CreateOr(Builder.CreateIsNull(X), Y);
 
+  ICmpInst::Predicate Pred1, Pred2;
   const APInt *C;
+  // icmp eq/ne X, (zext (icmp eq/ne X, C))
+  if (match(&I, m_c_ICmp(Pred1, m_Value(X),
+ m_ZExt(m_ICmp(Pred2, m_Deferred(X), m_APInt(C) &&
+  ICmpInst::isEquality(Pred1) && ICmpInst::isEquality(Pred2)) {
+if (C->isZero()) {
+  if (Pred2 == ICmpInst::ICMP_EQ) {
+// icmp eq X, (zext (icmp eq X, 0)) --> false
+// icmp ne X, (zext (icmp eq X, 0)) --> true
+return replaceInstUsesWith(
+I,
+Constant::getIntegerValue(
+I.getType(),
+APInt(1U, static_cast(Pred1 == ICmpInst::ICMP_NE;
+  } else {
+// icmp eq X, (zext (icmp ne X, 0)) --> icmp ult X, 2
+// icmp ne X, (zext (icmp ne X, 0)) --> icmp ugt X, 1
+return ICmpInst::Create(
+Instruction::ICmp,
+Pred1 == ICmpInst::ICMP_NE ? ICmpInst::ICMP_UGT
+   : ICmpInst::ICMP_ULT,
+X,
+Constant::getIntegerValue(
+X->getType(), APInt(X->getType()->getScalarSizeInBits(),
+Pred1 == ICmpInst::ICMP_NE ? 1 : 2)));
+  }
+} else if (C->isOne()) {
+  if (Pred2 == ICmpInst::ICMP_NE) {
+// icmp eq X, (zext (icmp ne X, 1)) --> false
+// icmp ne X, (zext (icmp ne X, 1)) --> true
+return replaceInstUsesWith(
+I,
+Constant::getIntegerValue(
+I.getType(),
+APInt(1U, static_cast(Pred1 == ICmpInst::ICMP_NE;
+  } else {
+// icmp eq X, (zext (icmp eq X, 1)) --> icmp ult X, 2
+// icmp ne X, (zext (icmp eq X, 1)) --> icmp ugt X, 1
+return ICmpInst::Create(
+Instruction::ICmp,
+Pred1 == ICmpInst::ICMP_NE ? ICmpInst::ICMP_UGT
+   : ICmpInst::ICMP_ULT,
+X,
+Constant::getIntegerValue(
+X->getType(), APInt(X->getType()->getScalarSizeInBits(),
+Pred1 == ICmpInst::ICMP_NE ? 1 : 2)));
+  }
+} else {
+  // C != 0 && C != 1
+  // icmp eq X, (zext (icmp eq X, C)) --> icmp eq X, 0
+  // icmp eq X, (zext (icmp ne X, C)) --> icmp eq X, 1
+  // icmp ne X, (zext (icmp eq X, C)) --> icmp ne X, 0
+  // icmp ne X, (zext (icmp ne X, C)) --> icmp ne X, 1
+  return ICmpInst::Create(
+  Instruction::ICmp, Pred1, X,
+  Constant::getIntegerValue(
+  X->getType(),
+  APInt(X->getType()->getScalarSizeInBits(),
+static_cast(Pred2 == ICmpInst::ICMP_NE;
+}
+  }
+
   if (match(I.getOperand(0), m_c_Add(m_ZExt(m_Value(X)), m_SExt(m_Value(Y 
&&
   match(I.getOperand(1), m_APInt(C)) &&
   X->getType()->isIntOrIntVectorTy(1) &&
diff --git a/llvm/test/Transforms/InstCombine/icmp-range.ll 
b/llvm/test/Transforms/InstCombine/icmp-range.ll
index 4281e09cb0309c8..15424fce33fdeea 100644
--- a/llvm/test/Transforms/InstCombine/icmp-range.ll
+++ b/llvm/test/Transforms/InstCombine/icmp-range.ll
@@ -1034,6 +1034,187 @@ define i1 @icmp_ne_bool_1(ptr %ptr) {
   ret i1 %cmp
 }
 
+define i1 @icmp_ne_zext_eq_zero(i32 %a) {
+; CHECK-LABEL: @icmp_ne_zext_eq_zero(
+; CHECK-NEXT:ret i1 true
+;
+  %cmp = icmp eq i32 %a, 0
+  %conv = zext i1 %cmp to i32
+  %cmp1 = icmp ne i32 %conv, %a
+  ret i1 %cmp1
+}
+
+define i1 @icmp_ne_zext_ne_zero(i32 %a) {
+; CHECK-LABEL: @icmp_ne_zext_ne_zero(
+; CHECK-NEXT:[[CMP1:%.*]] = icmp ugt i32 [[A:%.*]], 1
+; CHECK-NEXT:ret i1 [[CMP1]]
+;
+  %cmp = icmp ne i32 %a, 0
+  %conv = zext i1 %cmp to i32
+  %cmp1 = icmp ne i32 %conv, %a
+  ret i1 %cmp1
+}
+
+define i1 @icmp_eq_zext_eq_zero(i32 %a) {
+; CHECK-LABEL: @icmp_eq_zext_eq_zero(
+; CHECK-NEXT:ret i1 false
+;
+  %cmp = icmp eq i32 %a, 0
+  %conv = zext i1 %cmp to i32
+  %cmp1 = icmp eq i32 %conv, %a
+  ret i1 %cmp1
+}
+
+define i1 @icmp_eq_zext_ne_zero(i32 %

[clang] [InstCombine] Simplify the pattern `a ne/eq (zext/sext (a ne/eq c))` (PR #65852)

2023-09-24 Thread Yingwei Zheng via cfe-commits

https://github.com/dtcxzyw resolved 
https://github.com/llvm/llvm-project/pull/65852
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [InstCombine] Simplify the pattern `a ne/eq (zext/sext (a ne/eq c))` (PR #65852)

2023-09-24 Thread Yingwei Zheng via cfe-commits

https://github.com/dtcxzyw resolved 
https://github.com/llvm/llvm-project/pull/65852
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Extension: allow recursive macros (PR #65851)

2023-09-24 Thread via cfe-commits

https://github.com/kelbon updated 
https://github.com/llvm/llvm-project/pull/65851

>From d10ce5dd9e49fe85eac2e1f93a65cb27b511a71f Mon Sep 17 00:00:00 2001
From: Kelbon Nik 
Date: Sat, 9 Sep 2023 17:51:15 +0400
Subject: [PATCH 1/9] add define2 pp directive

---
 clang/include/clang/Basic/TokenKinds.def |  1 +
 clang/include/clang/Lex/MacroInfo.h  | 19 +--
 clang/include/clang/Lex/Preprocessor.h   |  2 +-
 clang/lib/Basic/IdentifierTable.cpp  |  2 ++
 clang/lib/Format/WhitespaceManager.cpp   |  2 +-
 clang/lib/Lex/MacroInfo.cpp  |  3 ++-
 clang/lib/Lex/PPDirectives.cpp   | 16 +++-
 7 files changed, 27 insertions(+), 18 deletions(-)

diff --git a/clang/include/clang/Basic/TokenKinds.def 
b/clang/include/clang/Basic/TokenKinds.def
index 72e8df8c793a7b6..b227a6a5632f496 100644
--- a/clang/include/clang/Basic/TokenKinds.def
+++ b/clang/include/clang/Basic/TokenKinds.def
@@ -115,6 +115,7 @@ PPKEYWORD(__include_macros)
 
 // C99 6.10.3 - Macro Replacement.
 PPKEYWORD(define)
+PPKEYWORD(define2)
 PPKEYWORD(undef)
 
 // C99 6.10.4 - Line Control.
diff --git a/clang/include/clang/Lex/MacroInfo.h 
b/clang/include/clang/Lex/MacroInfo.h
index 00c1c3866bbd9ca..4f0c8e987610e50 100644
--- a/clang/include/clang/Lex/MacroInfo.h
+++ b/clang/include/clang/Lex/MacroInfo.h
@@ -102,6 +102,10 @@ class MacroInfo {
   /// like \#define A A.
   bool IsDisabled : 1;
 
+  // True if 'define2' used,
+  // ignores 'IsDisabled' and enables expansion anyway
+  bool AllowRecurse : 1;
+
   /// True if this macro is either defined in the main file and has
   /// been used, or if it is not defined in the main file.
   ///
@@ -278,18 +282,13 @@ class MacroInfo {
   /// Return true if this macro is enabled.
   ///
   /// In other words, that we are not currently in an expansion of this macro.
-  bool isEnabled() const { return !IsDisabled; }
-
-  void EnableMacro() {
-assert(IsDisabled && "Cannot enable an already-enabled macro!");
-IsDisabled = false;
-  }
+  bool isEnabled() const { return AllowRecurse || !IsDisabled; }
+  void setAllowRecursive(bool Allow) { AllowRecurse = Allow; }
+  bool isAllowRecurse() const { return AllowRecurse; }
 
-  void DisableMacro() {
-assert(!IsDisabled && "Cannot disable an already-disabled macro!");
-IsDisabled = true;
-  }
+  void EnableMacro() { IsDisabled = false; }
 
+  void DisableMacro() { IsDisabled = true; }
   /// Determine whether this macro was used for a header guard.
   bool isUsedForHeaderGuard() const { return UsedForHeaderGuard; }
 
diff --git a/clang/include/clang/Lex/Preprocessor.h 
b/clang/include/clang/Lex/Preprocessor.h
index 575d08b83fd3a02..01eac0939fe9e21 100644
--- a/clang/include/clang/Lex/Preprocessor.h
+++ b/clang/include/clang/Lex/Preprocessor.h
@@ -2754,7 +2754,7 @@ class Preprocessor {
   void replayPreambleConditionalStack();
 
   // Macro handling.
-  void HandleDefineDirective(Token &Tok, bool ImmediatelyAfterHeaderGuard);
+  void HandleDefineDirective(Token &Tok, bool ImmediatelyAfterHeaderGuard, 
bool AllowRecurse);
   void HandleUndefDirective();
 
   // Conditional Inclusion.
diff --git a/clang/lib/Basic/IdentifierTable.cpp 
b/clang/lib/Basic/IdentifierTable.cpp
index e5599d545541085..7300825ff6b826a 100644
--- a/clang/lib/Basic/IdentifierTable.cpp
+++ b/clang/lib/Basic/IdentifierTable.cpp
@@ -431,6 +431,8 @@ tok::PPKeywordKind IdentifierInfo::getPPKeywordID() const {
   unsigned Len = getLength();
   if (Len < 2) return tok::pp_not_keyword;
   const char *Name = getNameStart();
+  if (std::string_view(Name, Len) == "define2")
+return tok::pp_define2;
   switch (HASH(Len, Name[0], Name[2])) {
   default: return tok::pp_not_keyword;
   CASE( 2, 'i', '\0', if);
diff --git a/clang/lib/Format/WhitespaceManager.cpp 
b/clang/lib/Format/WhitespaceManager.cpp
index b7bd8d27dc976b1..d8ab76d6761553e 100644
--- a/clang/lib/Format/WhitespaceManager.cpp
+++ b/clang/lib/Format/WhitespaceManager.cpp
@@ -743,7 +743,7 @@ void WhitespaceManager::alignConsecutiveMacros() {
 if (!Current || Current->isNot(tok::identifier))
   return false;
 
-if (!Current->Previous || Current->Previous->isNot(tok::pp_define))
+if (!Current->Previous || !Current->Previous->isOneOf(tok::pp_define, 
tok::pp_define2))
   return false;
 
 // For a macro function, 0 spaces are required between the
diff --git a/clang/lib/Lex/MacroInfo.cpp b/clang/lib/Lex/MacroInfo.cpp
index 39bb0f44eff25ba..9c3619c7c909304 100644
--- a/clang/lib/Lex/MacroInfo.cpp
+++ b/clang/lib/Lex/MacroInfo.cpp
@@ -50,7 +50,7 @@ static_assert(MacroInfoSizeChecker::AsExpected,
 MacroInfo::MacroInfo(SourceLocation DefLoc)
 : Location(DefLoc), IsDefinitionLengthCached(false), IsFunctionLike(false),
   IsC99Varargs(false), IsGNUVarargs(false), IsBuiltinMacro(false),
-  HasCommaPasting(false), IsDisabled(false), IsUsed(false),
+  HasCommaPasting(false), IsDisabled(false), AllowRecurse(false), 
IsUsed(false),
   IsAllowRedefinitionsWi

[clang] Extension: allow recursive macros (PR #65851)

2023-09-24 Thread via cfe-commits

github-actions[bot] wrote:




:warning: C/C++ code formatter, clang-format found issues in your code. 
:warning:



You can test this locally with the following command:


``bash
git-clang-format --diff 6fbbcb4ee724e23edd0fcd5b51877aff19dabd77 
49d7fa2e638f3903375f6f1de8443c63ff909a5d -- 
clang/test/Preprocessor/macro_infinite_recursion.cpp 
clang/test/Preprocessor/macro_recursion.cpp clang/include/clang/Lex/MacroInfo.h 
clang/include/clang/Lex/Preprocessor.h clang/lib/Basic/IdentifierTable.cpp 
clang/lib/Format/WhitespaceManager.cpp clang/lib/Lex/Lexer.cpp 
clang/lib/Lex/MacroInfo.cpp clang/lib/Lex/PPDirectives.cpp 
clang/lib/Lex/PPMacroExpansion.cpp clang/lib/Lex/Preprocessor.cpp 
clang/lib/Lex/TokenLexer.cpp
``





View the diff from clang-format here.


``diff
diff --git a/clang/include/clang/Lex/MacroInfo.h 
b/clang/include/clang/Lex/MacroInfo.h
index 8b91d33e9..895b15e10 100644
--- a/clang/include/clang/Lex/MacroInfo.h
+++ b/clang/include/clang/Lex/MacroInfo.h
@@ -190,7 +190,7 @@ public:
 return ArrayRef(ParameterList, NumParameters);
   }
   ArrayRef params() const {
-return const_cast(this)->params();
+return const_cast(this)->params();
   }
 
   /// Return the parameter number of the specified identifier,
@@ -300,10 +300,10 @@ public:
 --Depth;
   }
   enum : uint16_t { macro_recursion_depth_limit = 16'000 };
-  
-  [[nodiscard("infinite recursion check ignored")]]
-  bool TryDisableMacro() {
-assert((AllowRecurse || Depth == 0) && "Cannot disable an already-disabled 
macro!");
+
+  [[nodiscard("infinite recursion check ignored")]] bool TryDisableMacro() {
+assert((AllowRecurse || Depth == 0) &&
+   "Cannot disable an already-disabled macro!");
 return ++Depth < macro_recursion_depth_limit;
   }
 
diff --git a/clang/include/clang/Lex/Preprocessor.h 
b/clang/include/clang/Lex/Preprocessor.h
index 356be1d3b..361553e84 100644
--- a/clang/include/clang/Lex/Preprocessor.h
+++ b/clang/include/clang/Lex/Preprocessor.h
@@ -2429,7 +2429,7 @@ private:
   /// If macro definition containts __THIS_MACRO__ creates impl-only recursive
   /// version of macro, and replaces all __THIS_MACRO__ tokens
   /// with new created recusive version
-  void appendRecursiveVersionIfRequired(IdentifierInfo*, MacroInfo*);
+  void appendRecursiveVersionIfRequired(IdentifierInfo *, MacroInfo *);
 
   void PushIncludeMacroStack() {
 assert(CurLexerKind != CLK_CachingLexer && "cannot push a caching lexer");
diff --git a/clang/lib/Format/WhitespaceManager.cpp 
b/clang/lib/Format/WhitespaceManager.cpp
index d8ab76d67..db75e21f8 100644
--- a/clang/lib/Format/WhitespaceManager.cpp
+++ b/clang/lib/Format/WhitespaceManager.cpp
@@ -743,8 +743,10 @@ void WhitespaceManager::alignConsecutiveMacros() {
 if (!Current || Current->isNot(tok::identifier))
   return false;
 
-if (!Current->Previous || !Current->Previous->isOneOf(tok::pp_define, 
tok::pp_define2))
+if (!Current->Previous ||
+!Current->Previous->isOneOf(tok::pp_define, tok::pp_define2)) {
   return false;
+}
 
 // For a macro function, 0 spaces are required between the
 // identifier and the lparen that opens the parameter list.
diff --git a/clang/lib/Lex/Lexer.cpp b/clang/lib/Lex/Lexer.cpp
index 178bcc08a..70ba8f4f9 100644
--- a/clang/lib/Lex/Lexer.cpp
+++ b/clang/lib/Lex/Lexer.cpp
@@ -847,7 +847,7 @@ bool Lexer::isAtEndOfMacroExpansion(SourceLocation loc,
 const SourceManager &SM,
 const LangOptions &LangOpts,
 SourceLocation *MacroEnd) {
-  for(SourceLocation expansionLoc; true;loc = expansionLoc) {
+  for (SourceLocation expansionLoc; true; loc = expansionLoc) {
 assert(loc.isValid() && loc.isMacroID() && "Expected a valid macro loc");
 
 SourceLocation spellLoc = SM.getSpellingLoc(loc);
diff --git a/clang/lib/Lex/MacroInfo.cpp b/clang/lib/Lex/MacroInfo.cpp
index 110e3232b..e042dd38a 100644
--- a/clang/lib/Lex/MacroInfo.cpp
+++ b/clang/lib/Lex/MacroInfo.cpp
@@ -155,9 +155,11 @@ LLVM_DUMP_METHOD void MacroInfo::dump() const {
   // FIXME: Dump locations.
   Out << "MacroInfo " << this;
   if (IsBuiltinMacro) Out << " builtin";
-  if (!isEnabled()) Out << " disabled";
+  if (!isEnabled())
+Out << " disabled";
   if (IsUsed) Out << " used";
-  if (AllowRecurse) Out << " allow_recurse";
+  if (AllowRecurse)
+Out << " allow_recurse";
   if (IsAllowRedefinitionsWithoutWarning)
 Out << " allow_redefinitions_without_warning";
   if (IsWarnIfUnused) Out << " warn_if_unused";
diff --git a/clang/lib/Lex/PPDirectives.cpp b/clang/lib/Lex/PPDirectives.cpp
index 06b819857..6bb1612a9 100644
--- a/clang/lib/Lex/PPDirectives.cpp
+++ b/clang/lib/Lex/PPDirectives.cpp
@@ -1109,7 +1109,8 @@ private:
 void Preprocessor::HandleSkippedDirectiveWhileUsingPCH(Token &Result,
SourceLocation HashLoc) 
{
   if (const Ident

[clang] [Driver] Link Flang runtime on Solaris (PR #65644)

2023-09-24 Thread Andrzej Warzyński via cfe-commits

banach-space wrote:

> I wonder how to proceed with the patch at hand.

I'm not a Solaris expert, but based on this discussion I'd consider adding 
support for `-fuse-ld` in Flang. This sort of changes are often as simple as 
updating Options.td. Same for `-r`.

One important rule of thumb that I'd stick to:
* Do whatever Clang, GFortran and GCC do.

In cases where there's different behavior between these compilers, just go for 
whatever feels most sensible, but please document your design decision.

https://github.com/llvm/llvm-project/pull/65644
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Extension: allow recursive macros (PR #65851)

2023-09-24 Thread via cfe-commits

https://github.com/kelbon updated 
https://github.com/llvm/llvm-project/pull/65851

>From d10ce5dd9e49fe85eac2e1f93a65cb27b511a71f Mon Sep 17 00:00:00 2001
From: Kelbon Nik 
Date: Sat, 9 Sep 2023 17:51:15 +0400
Subject: [PATCH 01/10] add define2 pp directive

---
 clang/include/clang/Basic/TokenKinds.def |  1 +
 clang/include/clang/Lex/MacroInfo.h  | 19 +--
 clang/include/clang/Lex/Preprocessor.h   |  2 +-
 clang/lib/Basic/IdentifierTable.cpp  |  2 ++
 clang/lib/Format/WhitespaceManager.cpp   |  2 +-
 clang/lib/Lex/MacroInfo.cpp  |  3 ++-
 clang/lib/Lex/PPDirectives.cpp   | 16 +++-
 7 files changed, 27 insertions(+), 18 deletions(-)

diff --git a/clang/include/clang/Basic/TokenKinds.def 
b/clang/include/clang/Basic/TokenKinds.def
index 72e8df8c793a7b6..b227a6a5632f496 100644
--- a/clang/include/clang/Basic/TokenKinds.def
+++ b/clang/include/clang/Basic/TokenKinds.def
@@ -115,6 +115,7 @@ PPKEYWORD(__include_macros)
 
 // C99 6.10.3 - Macro Replacement.
 PPKEYWORD(define)
+PPKEYWORD(define2)
 PPKEYWORD(undef)
 
 // C99 6.10.4 - Line Control.
diff --git a/clang/include/clang/Lex/MacroInfo.h 
b/clang/include/clang/Lex/MacroInfo.h
index 00c1c3866bbd9ca..4f0c8e987610e50 100644
--- a/clang/include/clang/Lex/MacroInfo.h
+++ b/clang/include/clang/Lex/MacroInfo.h
@@ -102,6 +102,10 @@ class MacroInfo {
   /// like \#define A A.
   bool IsDisabled : 1;
 
+  // True if 'define2' used,
+  // ignores 'IsDisabled' and enables expansion anyway
+  bool AllowRecurse : 1;
+
   /// True if this macro is either defined in the main file and has
   /// been used, or if it is not defined in the main file.
   ///
@@ -278,18 +282,13 @@ class MacroInfo {
   /// Return true if this macro is enabled.
   ///
   /// In other words, that we are not currently in an expansion of this macro.
-  bool isEnabled() const { return !IsDisabled; }
-
-  void EnableMacro() {
-assert(IsDisabled && "Cannot enable an already-enabled macro!");
-IsDisabled = false;
-  }
+  bool isEnabled() const { return AllowRecurse || !IsDisabled; }
+  void setAllowRecursive(bool Allow) { AllowRecurse = Allow; }
+  bool isAllowRecurse() const { return AllowRecurse; }
 
-  void DisableMacro() {
-assert(!IsDisabled && "Cannot disable an already-disabled macro!");
-IsDisabled = true;
-  }
+  void EnableMacro() { IsDisabled = false; }
 
+  void DisableMacro() { IsDisabled = true; }
   /// Determine whether this macro was used for a header guard.
   bool isUsedForHeaderGuard() const { return UsedForHeaderGuard; }
 
diff --git a/clang/include/clang/Lex/Preprocessor.h 
b/clang/include/clang/Lex/Preprocessor.h
index 575d08b83fd3a02..01eac0939fe9e21 100644
--- a/clang/include/clang/Lex/Preprocessor.h
+++ b/clang/include/clang/Lex/Preprocessor.h
@@ -2754,7 +2754,7 @@ class Preprocessor {
   void replayPreambleConditionalStack();
 
   // Macro handling.
-  void HandleDefineDirective(Token &Tok, bool ImmediatelyAfterHeaderGuard);
+  void HandleDefineDirective(Token &Tok, bool ImmediatelyAfterHeaderGuard, 
bool AllowRecurse);
   void HandleUndefDirective();
 
   // Conditional Inclusion.
diff --git a/clang/lib/Basic/IdentifierTable.cpp 
b/clang/lib/Basic/IdentifierTable.cpp
index e5599d545541085..7300825ff6b826a 100644
--- a/clang/lib/Basic/IdentifierTable.cpp
+++ b/clang/lib/Basic/IdentifierTable.cpp
@@ -431,6 +431,8 @@ tok::PPKeywordKind IdentifierInfo::getPPKeywordID() const {
   unsigned Len = getLength();
   if (Len < 2) return tok::pp_not_keyword;
   const char *Name = getNameStart();
+  if (std::string_view(Name, Len) == "define2")
+return tok::pp_define2;
   switch (HASH(Len, Name[0], Name[2])) {
   default: return tok::pp_not_keyword;
   CASE( 2, 'i', '\0', if);
diff --git a/clang/lib/Format/WhitespaceManager.cpp 
b/clang/lib/Format/WhitespaceManager.cpp
index b7bd8d27dc976b1..d8ab76d6761553e 100644
--- a/clang/lib/Format/WhitespaceManager.cpp
+++ b/clang/lib/Format/WhitespaceManager.cpp
@@ -743,7 +743,7 @@ void WhitespaceManager::alignConsecutiveMacros() {
 if (!Current || Current->isNot(tok::identifier))
   return false;
 
-if (!Current->Previous || Current->Previous->isNot(tok::pp_define))
+if (!Current->Previous || !Current->Previous->isOneOf(tok::pp_define, 
tok::pp_define2))
   return false;
 
 // For a macro function, 0 spaces are required between the
diff --git a/clang/lib/Lex/MacroInfo.cpp b/clang/lib/Lex/MacroInfo.cpp
index 39bb0f44eff25ba..9c3619c7c909304 100644
--- a/clang/lib/Lex/MacroInfo.cpp
+++ b/clang/lib/Lex/MacroInfo.cpp
@@ -50,7 +50,7 @@ static_assert(MacroInfoSizeChecker::AsExpected,
 MacroInfo::MacroInfo(SourceLocation DefLoc)
 : Location(DefLoc), IsDefinitionLengthCached(false), IsFunctionLike(false),
   IsC99Varargs(false), IsGNUVarargs(false), IsBuiltinMacro(false),
-  HasCommaPasting(false), IsDisabled(false), IsUsed(false),
+  HasCommaPasting(false), IsDisabled(false), AllowRecurse(false), 
IsUsed(false),
   IsAllowRedefinitions

[clang] Extension: allow recursive macros (PR #65851)

2023-09-24 Thread via cfe-commits

https://github.com/kelbon updated 
https://github.com/llvm/llvm-project/pull/65851

>From d10ce5dd9e49fe85eac2e1f93a65cb27b511a71f Mon Sep 17 00:00:00 2001
From: Kelbon Nik 
Date: Sat, 9 Sep 2023 17:51:15 +0400
Subject: [PATCH 01/11] add define2 pp directive

---
 clang/include/clang/Basic/TokenKinds.def |  1 +
 clang/include/clang/Lex/MacroInfo.h  | 19 +--
 clang/include/clang/Lex/Preprocessor.h   |  2 +-
 clang/lib/Basic/IdentifierTable.cpp  |  2 ++
 clang/lib/Format/WhitespaceManager.cpp   |  2 +-
 clang/lib/Lex/MacroInfo.cpp  |  3 ++-
 clang/lib/Lex/PPDirectives.cpp   | 16 +++-
 7 files changed, 27 insertions(+), 18 deletions(-)

diff --git a/clang/include/clang/Basic/TokenKinds.def 
b/clang/include/clang/Basic/TokenKinds.def
index 72e8df8c793a7b6..b227a6a5632f496 100644
--- a/clang/include/clang/Basic/TokenKinds.def
+++ b/clang/include/clang/Basic/TokenKinds.def
@@ -115,6 +115,7 @@ PPKEYWORD(__include_macros)
 
 // C99 6.10.3 - Macro Replacement.
 PPKEYWORD(define)
+PPKEYWORD(define2)
 PPKEYWORD(undef)
 
 // C99 6.10.4 - Line Control.
diff --git a/clang/include/clang/Lex/MacroInfo.h 
b/clang/include/clang/Lex/MacroInfo.h
index 00c1c3866bbd9ca..4f0c8e987610e50 100644
--- a/clang/include/clang/Lex/MacroInfo.h
+++ b/clang/include/clang/Lex/MacroInfo.h
@@ -102,6 +102,10 @@ class MacroInfo {
   /// like \#define A A.
   bool IsDisabled : 1;
 
+  // True if 'define2' used,
+  // ignores 'IsDisabled' and enables expansion anyway
+  bool AllowRecurse : 1;
+
   /// True if this macro is either defined in the main file and has
   /// been used, or if it is not defined in the main file.
   ///
@@ -278,18 +282,13 @@ class MacroInfo {
   /// Return true if this macro is enabled.
   ///
   /// In other words, that we are not currently in an expansion of this macro.
-  bool isEnabled() const { return !IsDisabled; }
-
-  void EnableMacro() {
-assert(IsDisabled && "Cannot enable an already-enabled macro!");
-IsDisabled = false;
-  }
+  bool isEnabled() const { return AllowRecurse || !IsDisabled; }
+  void setAllowRecursive(bool Allow) { AllowRecurse = Allow; }
+  bool isAllowRecurse() const { return AllowRecurse; }
 
-  void DisableMacro() {
-assert(!IsDisabled && "Cannot disable an already-disabled macro!");
-IsDisabled = true;
-  }
+  void EnableMacro() { IsDisabled = false; }
 
+  void DisableMacro() { IsDisabled = true; }
   /// Determine whether this macro was used for a header guard.
   bool isUsedForHeaderGuard() const { return UsedForHeaderGuard; }
 
diff --git a/clang/include/clang/Lex/Preprocessor.h 
b/clang/include/clang/Lex/Preprocessor.h
index 575d08b83fd3a02..01eac0939fe9e21 100644
--- a/clang/include/clang/Lex/Preprocessor.h
+++ b/clang/include/clang/Lex/Preprocessor.h
@@ -2754,7 +2754,7 @@ class Preprocessor {
   void replayPreambleConditionalStack();
 
   // Macro handling.
-  void HandleDefineDirective(Token &Tok, bool ImmediatelyAfterHeaderGuard);
+  void HandleDefineDirective(Token &Tok, bool ImmediatelyAfterHeaderGuard, 
bool AllowRecurse);
   void HandleUndefDirective();
 
   // Conditional Inclusion.
diff --git a/clang/lib/Basic/IdentifierTable.cpp 
b/clang/lib/Basic/IdentifierTable.cpp
index e5599d545541085..7300825ff6b826a 100644
--- a/clang/lib/Basic/IdentifierTable.cpp
+++ b/clang/lib/Basic/IdentifierTable.cpp
@@ -431,6 +431,8 @@ tok::PPKeywordKind IdentifierInfo::getPPKeywordID() const {
   unsigned Len = getLength();
   if (Len < 2) return tok::pp_not_keyword;
   const char *Name = getNameStart();
+  if (std::string_view(Name, Len) == "define2")
+return tok::pp_define2;
   switch (HASH(Len, Name[0], Name[2])) {
   default: return tok::pp_not_keyword;
   CASE( 2, 'i', '\0', if);
diff --git a/clang/lib/Format/WhitespaceManager.cpp 
b/clang/lib/Format/WhitespaceManager.cpp
index b7bd8d27dc976b1..d8ab76d6761553e 100644
--- a/clang/lib/Format/WhitespaceManager.cpp
+++ b/clang/lib/Format/WhitespaceManager.cpp
@@ -743,7 +743,7 @@ void WhitespaceManager::alignConsecutiveMacros() {
 if (!Current || Current->isNot(tok::identifier))
   return false;
 
-if (!Current->Previous || Current->Previous->isNot(tok::pp_define))
+if (!Current->Previous || !Current->Previous->isOneOf(tok::pp_define, 
tok::pp_define2))
   return false;
 
 // For a macro function, 0 spaces are required between the
diff --git a/clang/lib/Lex/MacroInfo.cpp b/clang/lib/Lex/MacroInfo.cpp
index 39bb0f44eff25ba..9c3619c7c909304 100644
--- a/clang/lib/Lex/MacroInfo.cpp
+++ b/clang/lib/Lex/MacroInfo.cpp
@@ -50,7 +50,7 @@ static_assert(MacroInfoSizeChecker::AsExpected,
 MacroInfo::MacroInfo(SourceLocation DefLoc)
 : Location(DefLoc), IsDefinitionLengthCached(false), IsFunctionLike(false),
   IsC99Varargs(false), IsGNUVarargs(false), IsBuiltinMacro(false),
-  HasCommaPasting(false), IsDisabled(false), IsUsed(false),
+  HasCommaPasting(false), IsDisabled(false), AllowRecurse(false), 
IsUsed(false),
   IsAllowRedefinitions

[PATCH] D159126: [Clang] Add captures to the instantiation scope of lambda call operators

2023-09-24 Thread Daniel Bertalan via Phabricator via cfe-commits
BertalanD added a comment.

FYI, we found a crash related to this patch: 
https://github.com/llvm/llvm-project/issues/67260


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D159126

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


[clang] Extension: allow recursive macros (PR #65851)

2023-09-24 Thread via cfe-commits

https://github.com/kelbon updated 
https://github.com/llvm/llvm-project/pull/65851

>From d10ce5dd9e49fe85eac2e1f93a65cb27b511a71f Mon Sep 17 00:00:00 2001
From: Kelbon Nik 
Date: Sat, 9 Sep 2023 17:51:15 +0400
Subject: [PATCH 01/12] add define2 pp directive

---
 clang/include/clang/Basic/TokenKinds.def |  1 +
 clang/include/clang/Lex/MacroInfo.h  | 19 +--
 clang/include/clang/Lex/Preprocessor.h   |  2 +-
 clang/lib/Basic/IdentifierTable.cpp  |  2 ++
 clang/lib/Format/WhitespaceManager.cpp   |  2 +-
 clang/lib/Lex/MacroInfo.cpp  |  3 ++-
 clang/lib/Lex/PPDirectives.cpp   | 16 +++-
 7 files changed, 27 insertions(+), 18 deletions(-)

diff --git a/clang/include/clang/Basic/TokenKinds.def 
b/clang/include/clang/Basic/TokenKinds.def
index 72e8df8c793a7b6..b227a6a5632f496 100644
--- a/clang/include/clang/Basic/TokenKinds.def
+++ b/clang/include/clang/Basic/TokenKinds.def
@@ -115,6 +115,7 @@ PPKEYWORD(__include_macros)
 
 // C99 6.10.3 - Macro Replacement.
 PPKEYWORD(define)
+PPKEYWORD(define2)
 PPKEYWORD(undef)
 
 // C99 6.10.4 - Line Control.
diff --git a/clang/include/clang/Lex/MacroInfo.h 
b/clang/include/clang/Lex/MacroInfo.h
index 00c1c3866bbd9ca..4f0c8e987610e50 100644
--- a/clang/include/clang/Lex/MacroInfo.h
+++ b/clang/include/clang/Lex/MacroInfo.h
@@ -102,6 +102,10 @@ class MacroInfo {
   /// like \#define A A.
   bool IsDisabled : 1;
 
+  // True if 'define2' used,
+  // ignores 'IsDisabled' and enables expansion anyway
+  bool AllowRecurse : 1;
+
   /// True if this macro is either defined in the main file and has
   /// been used, or if it is not defined in the main file.
   ///
@@ -278,18 +282,13 @@ class MacroInfo {
   /// Return true if this macro is enabled.
   ///
   /// In other words, that we are not currently in an expansion of this macro.
-  bool isEnabled() const { return !IsDisabled; }
-
-  void EnableMacro() {
-assert(IsDisabled && "Cannot enable an already-enabled macro!");
-IsDisabled = false;
-  }
+  bool isEnabled() const { return AllowRecurse || !IsDisabled; }
+  void setAllowRecursive(bool Allow) { AllowRecurse = Allow; }
+  bool isAllowRecurse() const { return AllowRecurse; }
 
-  void DisableMacro() {
-assert(!IsDisabled && "Cannot disable an already-disabled macro!");
-IsDisabled = true;
-  }
+  void EnableMacro() { IsDisabled = false; }
 
+  void DisableMacro() { IsDisabled = true; }
   /// Determine whether this macro was used for a header guard.
   bool isUsedForHeaderGuard() const { return UsedForHeaderGuard; }
 
diff --git a/clang/include/clang/Lex/Preprocessor.h 
b/clang/include/clang/Lex/Preprocessor.h
index 575d08b83fd3a02..01eac0939fe9e21 100644
--- a/clang/include/clang/Lex/Preprocessor.h
+++ b/clang/include/clang/Lex/Preprocessor.h
@@ -2754,7 +2754,7 @@ class Preprocessor {
   void replayPreambleConditionalStack();
 
   // Macro handling.
-  void HandleDefineDirective(Token &Tok, bool ImmediatelyAfterHeaderGuard);
+  void HandleDefineDirective(Token &Tok, bool ImmediatelyAfterHeaderGuard, 
bool AllowRecurse);
   void HandleUndefDirective();
 
   // Conditional Inclusion.
diff --git a/clang/lib/Basic/IdentifierTable.cpp 
b/clang/lib/Basic/IdentifierTable.cpp
index e5599d545541085..7300825ff6b826a 100644
--- a/clang/lib/Basic/IdentifierTable.cpp
+++ b/clang/lib/Basic/IdentifierTable.cpp
@@ -431,6 +431,8 @@ tok::PPKeywordKind IdentifierInfo::getPPKeywordID() const {
   unsigned Len = getLength();
   if (Len < 2) return tok::pp_not_keyword;
   const char *Name = getNameStart();
+  if (std::string_view(Name, Len) == "define2")
+return tok::pp_define2;
   switch (HASH(Len, Name[0], Name[2])) {
   default: return tok::pp_not_keyword;
   CASE( 2, 'i', '\0', if);
diff --git a/clang/lib/Format/WhitespaceManager.cpp 
b/clang/lib/Format/WhitespaceManager.cpp
index b7bd8d27dc976b1..d8ab76d6761553e 100644
--- a/clang/lib/Format/WhitespaceManager.cpp
+++ b/clang/lib/Format/WhitespaceManager.cpp
@@ -743,7 +743,7 @@ void WhitespaceManager::alignConsecutiveMacros() {
 if (!Current || Current->isNot(tok::identifier))
   return false;
 
-if (!Current->Previous || Current->Previous->isNot(tok::pp_define))
+if (!Current->Previous || !Current->Previous->isOneOf(tok::pp_define, 
tok::pp_define2))
   return false;
 
 // For a macro function, 0 spaces are required between the
diff --git a/clang/lib/Lex/MacroInfo.cpp b/clang/lib/Lex/MacroInfo.cpp
index 39bb0f44eff25ba..9c3619c7c909304 100644
--- a/clang/lib/Lex/MacroInfo.cpp
+++ b/clang/lib/Lex/MacroInfo.cpp
@@ -50,7 +50,7 @@ static_assert(MacroInfoSizeChecker::AsExpected,
 MacroInfo::MacroInfo(SourceLocation DefLoc)
 : Location(DefLoc), IsDefinitionLengthCached(false), IsFunctionLike(false),
   IsC99Varargs(false), IsGNUVarargs(false), IsBuiltinMacro(false),
-  HasCommaPasting(false), IsDisabled(false), IsUsed(false),
+  HasCommaPasting(false), IsDisabled(false), AllowRecurse(false), 
IsUsed(false),
   IsAllowRedefinitions

[clang] Extension: allow recursive macros (PR #65851)

2023-09-24 Thread via cfe-commits

https://github.com/kelbon updated 
https://github.com/llvm/llvm-project/pull/65851

>From d10ce5dd9e49fe85eac2e1f93a65cb27b511a71f Mon Sep 17 00:00:00 2001
From: Kelbon Nik 
Date: Sat, 9 Sep 2023 17:51:15 +0400
Subject: [PATCH 01/13] add define2 pp directive

---
 clang/include/clang/Basic/TokenKinds.def |  1 +
 clang/include/clang/Lex/MacroInfo.h  | 19 +--
 clang/include/clang/Lex/Preprocessor.h   |  2 +-
 clang/lib/Basic/IdentifierTable.cpp  |  2 ++
 clang/lib/Format/WhitespaceManager.cpp   |  2 +-
 clang/lib/Lex/MacroInfo.cpp  |  3 ++-
 clang/lib/Lex/PPDirectives.cpp   | 16 +++-
 7 files changed, 27 insertions(+), 18 deletions(-)

diff --git a/clang/include/clang/Basic/TokenKinds.def 
b/clang/include/clang/Basic/TokenKinds.def
index 72e8df8c793a7b6..b227a6a5632f496 100644
--- a/clang/include/clang/Basic/TokenKinds.def
+++ b/clang/include/clang/Basic/TokenKinds.def
@@ -115,6 +115,7 @@ PPKEYWORD(__include_macros)
 
 // C99 6.10.3 - Macro Replacement.
 PPKEYWORD(define)
+PPKEYWORD(define2)
 PPKEYWORD(undef)
 
 // C99 6.10.4 - Line Control.
diff --git a/clang/include/clang/Lex/MacroInfo.h 
b/clang/include/clang/Lex/MacroInfo.h
index 00c1c3866bbd9ca..4f0c8e987610e50 100644
--- a/clang/include/clang/Lex/MacroInfo.h
+++ b/clang/include/clang/Lex/MacroInfo.h
@@ -102,6 +102,10 @@ class MacroInfo {
   /// like \#define A A.
   bool IsDisabled : 1;
 
+  // True if 'define2' used,
+  // ignores 'IsDisabled' and enables expansion anyway
+  bool AllowRecurse : 1;
+
   /// True if this macro is either defined in the main file and has
   /// been used, or if it is not defined in the main file.
   ///
@@ -278,18 +282,13 @@ class MacroInfo {
   /// Return true if this macro is enabled.
   ///
   /// In other words, that we are not currently in an expansion of this macro.
-  bool isEnabled() const { return !IsDisabled; }
-
-  void EnableMacro() {
-assert(IsDisabled && "Cannot enable an already-enabled macro!");
-IsDisabled = false;
-  }
+  bool isEnabled() const { return AllowRecurse || !IsDisabled; }
+  void setAllowRecursive(bool Allow) { AllowRecurse = Allow; }
+  bool isAllowRecurse() const { return AllowRecurse; }
 
-  void DisableMacro() {
-assert(!IsDisabled && "Cannot disable an already-disabled macro!");
-IsDisabled = true;
-  }
+  void EnableMacro() { IsDisabled = false; }
 
+  void DisableMacro() { IsDisabled = true; }
   /// Determine whether this macro was used for a header guard.
   bool isUsedForHeaderGuard() const { return UsedForHeaderGuard; }
 
diff --git a/clang/include/clang/Lex/Preprocessor.h 
b/clang/include/clang/Lex/Preprocessor.h
index 575d08b83fd3a02..01eac0939fe9e21 100644
--- a/clang/include/clang/Lex/Preprocessor.h
+++ b/clang/include/clang/Lex/Preprocessor.h
@@ -2754,7 +2754,7 @@ class Preprocessor {
   void replayPreambleConditionalStack();
 
   // Macro handling.
-  void HandleDefineDirective(Token &Tok, bool ImmediatelyAfterHeaderGuard);
+  void HandleDefineDirective(Token &Tok, bool ImmediatelyAfterHeaderGuard, 
bool AllowRecurse);
   void HandleUndefDirective();
 
   // Conditional Inclusion.
diff --git a/clang/lib/Basic/IdentifierTable.cpp 
b/clang/lib/Basic/IdentifierTable.cpp
index e5599d545541085..7300825ff6b826a 100644
--- a/clang/lib/Basic/IdentifierTable.cpp
+++ b/clang/lib/Basic/IdentifierTable.cpp
@@ -431,6 +431,8 @@ tok::PPKeywordKind IdentifierInfo::getPPKeywordID() const {
   unsigned Len = getLength();
   if (Len < 2) return tok::pp_not_keyword;
   const char *Name = getNameStart();
+  if (std::string_view(Name, Len) == "define2")
+return tok::pp_define2;
   switch (HASH(Len, Name[0], Name[2])) {
   default: return tok::pp_not_keyword;
   CASE( 2, 'i', '\0', if);
diff --git a/clang/lib/Format/WhitespaceManager.cpp 
b/clang/lib/Format/WhitespaceManager.cpp
index b7bd8d27dc976b1..d8ab76d6761553e 100644
--- a/clang/lib/Format/WhitespaceManager.cpp
+++ b/clang/lib/Format/WhitespaceManager.cpp
@@ -743,7 +743,7 @@ void WhitespaceManager::alignConsecutiveMacros() {
 if (!Current || Current->isNot(tok::identifier))
   return false;
 
-if (!Current->Previous || Current->Previous->isNot(tok::pp_define))
+if (!Current->Previous || !Current->Previous->isOneOf(tok::pp_define, 
tok::pp_define2))
   return false;
 
 // For a macro function, 0 spaces are required between the
diff --git a/clang/lib/Lex/MacroInfo.cpp b/clang/lib/Lex/MacroInfo.cpp
index 39bb0f44eff25ba..9c3619c7c909304 100644
--- a/clang/lib/Lex/MacroInfo.cpp
+++ b/clang/lib/Lex/MacroInfo.cpp
@@ -50,7 +50,7 @@ static_assert(MacroInfoSizeChecker::AsExpected,
 MacroInfo::MacroInfo(SourceLocation DefLoc)
 : Location(DefLoc), IsDefinitionLengthCached(false), IsFunctionLike(false),
   IsC99Varargs(false), IsGNUVarargs(false), IsBuiltinMacro(false),
-  HasCommaPasting(false), IsDisabled(false), IsUsed(false),
+  HasCommaPasting(false), IsDisabled(false), AllowRecurse(false), 
IsUsed(false),
   IsAllowRedefinitions

[libunwind] [docs] Minor fix to outdated example in LibTooling tutorial (PR #67262)

2023-09-24 Thread Ivan Ho via cfe-commits

https://github.com/hhkit created https://github.com/llvm/llvm-project/pull/67262

Came across this while using said tutorial. 

I grabbed this example from [ASTMatchers 
tutorial](https://clang.llvm.org/docs/LibASTMatchersTutorial.html) for 
consistency, and it works with the latest LLVM version.

>From d9b18db1a38e6e9002582a34b510a3d3fdd4f996 Mon Sep 17 00:00:00 2001
From: hhkit <38537881+hh...@users.noreply.github.com>
Date: Fri, 22 Sep 2023 05:17:39 +0800
Subject: [PATCH] [docs] Minor fix; outdated interface used

---
 clang/docs/LibTooling.rst | 21 ++---
 1 file changed, 18 insertions(+), 3 deletions(-)

diff --git a/clang/docs/LibTooling.rst b/clang/docs/LibTooling.rst
index df50dcebf9b83c7..e552ed81fe14edb 100644
--- a/clang/docs/LibTooling.rst
+++ b/clang/docs/LibTooling.rst
@@ -69,9 +69,17 @@ and automatic location of the compilation database using 
source files paths.
   static llvm::cl::OptionCategory MyToolCategory("my-tool options");
 
   int main(int argc, const char **argv) {
-// CommonOptionsParser constructor will parse arguments and create a
+
+// CommonOptionsParser::create will parse arguments and create a
 // CompilationDatabase.  In case of error it will terminate the program.
-CommonOptionsParser OptionsParser(argc, argv, MyToolCategory);
+auto ExpectedParser = CommonOptionsParser::create(argc, argv, 
MyToolCategory);
+if (!ExpectedParser) {
+  // Fail gracefully for unsupported options.
+  llvm::errs() << ExpectedParser.takeError();
+  return 1;
+}
+
+CommonOptionsParser& OptionsParser = ExpectedParser.get();
 
 // Use OptionsParser.getCompilations() and 
OptionsParser.getSourcePathList()
 // to retrieve CompilationDatabase and the list of input file paths.
@@ -133,7 +141,14 @@ version of this example tool is also checked into the 
clang tree at
   static cl::extrahelp MoreHelp("\nMore help text...\n");
 
   int main(int argc, const char **argv) {
-CommonOptionsParser OptionsParser(argc, argv, MyToolCategory);
+auto ExpectedParser = CommonOptionsParser::create(argc, argv, 
MyToolCategory);
+if (!ExpectedParser) {
+  // Fail gracefully for unsupported options.
+  llvm::errs() << ExpectedParser.takeError();
+  return 1;
+}
+
+CommonOptionsParser& OptionsParser = ExpectedParser.get();
 ClangTool Tool(OptionsParser.getCompilations(),
OptionsParser.getSourcePathList());
 return Tool.run(newFrontendActionFactory().get());

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


[libunwind] [docs] Minor fix to outdated example in LibTooling tutorial (PR #67262)

2023-09-24 Thread via cfe-commits

llvmbot wrote:




@llvm/pr-subscribers-clang


Changes

Came across this while using said tutorial. 

I grabbed this example from [ASTMatchers 
tutorial](https://clang.llvm.org/docs/LibASTMatchersTutorial.html) for 
consistency, and it works with the latest LLVM version.

---
Full diff: https://github.com/llvm/llvm-project/pull/67262.diff


1 Files Affected:

- (modified) clang/docs/LibTooling.rst (+18-3) 


``diff
diff --git a/clang/docs/LibTooling.rst b/clang/docs/LibTooling.rst
index df50dcebf9b83c7..e552ed81fe14edb 100644
--- a/clang/docs/LibTooling.rst
+++ b/clang/docs/LibTooling.rst
@@ -69,9 +69,17 @@ and automatic location of the compilation database using 
source files paths.
   static llvm::cl::OptionCategory MyToolCategory("my-tool options");
 
   int main(int argc, const char **argv) {
-// CommonOptionsParser constructor will parse arguments and create a
+
+// CommonOptionsParser::create will parse arguments and create a
 // CompilationDatabase.  In case of error it will terminate the program.
-CommonOptionsParser OptionsParser(argc, argv, MyToolCategory);
+auto ExpectedParser = CommonOptionsParser::create(argc, argv, 
MyToolCategory);
+if (!ExpectedParser) {
+  // Fail gracefully for unsupported options.
+  llvm::errs() << ExpectedParser.takeError();
+  return 1;
+}
+
+CommonOptionsParser& OptionsParser = ExpectedParser.get();
 
 // Use OptionsParser.getCompilations() and 
OptionsParser.getSourcePathList()
 // to retrieve CompilationDatabase and the list of input file paths.
@@ -133,7 +141,14 @@ version of this example tool is also checked into the 
clang tree at
   static cl::extrahelp MoreHelp("\nMore help text...\n");
 
   int main(int argc, const char **argv) {
-CommonOptionsParser OptionsParser(argc, argv, MyToolCategory);
+auto ExpectedParser = CommonOptionsParser::create(argc, argv, 
MyToolCategory);
+if (!ExpectedParser) {
+  // Fail gracefully for unsupported options.
+  llvm::errs() << ExpectedParser.takeError();
+  return 1;
+}
+
+CommonOptionsParser& OptionsParser = ExpectedParser.get();
 ClangTool Tool(OptionsParser.getCompilations(),
OptionsParser.getSourcePathList());
 return Tool.run(newFrontendActionFactory().get());

``




https://github.com/llvm/llvm-project/pull/67262
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Extension: allow recursive macros (PR #65851)

2023-09-24 Thread via cfe-commits

https://github.com/kelbon updated 
https://github.com/llvm/llvm-project/pull/65851

>From d10ce5dd9e49fe85eac2e1f93a65cb27b511a71f Mon Sep 17 00:00:00 2001
From: Kelbon Nik 
Date: Sat, 9 Sep 2023 17:51:15 +0400
Subject: [PATCH 01/14] add define2 pp directive

---
 clang/include/clang/Basic/TokenKinds.def |  1 +
 clang/include/clang/Lex/MacroInfo.h  | 19 +--
 clang/include/clang/Lex/Preprocessor.h   |  2 +-
 clang/lib/Basic/IdentifierTable.cpp  |  2 ++
 clang/lib/Format/WhitespaceManager.cpp   |  2 +-
 clang/lib/Lex/MacroInfo.cpp  |  3 ++-
 clang/lib/Lex/PPDirectives.cpp   | 16 +++-
 7 files changed, 27 insertions(+), 18 deletions(-)

diff --git a/clang/include/clang/Basic/TokenKinds.def 
b/clang/include/clang/Basic/TokenKinds.def
index 72e8df8c793a7b6..b227a6a5632f496 100644
--- a/clang/include/clang/Basic/TokenKinds.def
+++ b/clang/include/clang/Basic/TokenKinds.def
@@ -115,6 +115,7 @@ PPKEYWORD(__include_macros)
 
 // C99 6.10.3 - Macro Replacement.
 PPKEYWORD(define)
+PPKEYWORD(define2)
 PPKEYWORD(undef)
 
 // C99 6.10.4 - Line Control.
diff --git a/clang/include/clang/Lex/MacroInfo.h 
b/clang/include/clang/Lex/MacroInfo.h
index 00c1c3866bbd9ca..4f0c8e987610e50 100644
--- a/clang/include/clang/Lex/MacroInfo.h
+++ b/clang/include/clang/Lex/MacroInfo.h
@@ -102,6 +102,10 @@ class MacroInfo {
   /// like \#define A A.
   bool IsDisabled : 1;
 
+  // True if 'define2' used,
+  // ignores 'IsDisabled' and enables expansion anyway
+  bool AllowRecurse : 1;
+
   /// True if this macro is either defined in the main file and has
   /// been used, or if it is not defined in the main file.
   ///
@@ -278,18 +282,13 @@ class MacroInfo {
   /// Return true if this macro is enabled.
   ///
   /// In other words, that we are not currently in an expansion of this macro.
-  bool isEnabled() const { return !IsDisabled; }
-
-  void EnableMacro() {
-assert(IsDisabled && "Cannot enable an already-enabled macro!");
-IsDisabled = false;
-  }
+  bool isEnabled() const { return AllowRecurse || !IsDisabled; }
+  void setAllowRecursive(bool Allow) { AllowRecurse = Allow; }
+  bool isAllowRecurse() const { return AllowRecurse; }
 
-  void DisableMacro() {
-assert(!IsDisabled && "Cannot disable an already-disabled macro!");
-IsDisabled = true;
-  }
+  void EnableMacro() { IsDisabled = false; }
 
+  void DisableMacro() { IsDisabled = true; }
   /// Determine whether this macro was used for a header guard.
   bool isUsedForHeaderGuard() const { return UsedForHeaderGuard; }
 
diff --git a/clang/include/clang/Lex/Preprocessor.h 
b/clang/include/clang/Lex/Preprocessor.h
index 575d08b83fd3a02..01eac0939fe9e21 100644
--- a/clang/include/clang/Lex/Preprocessor.h
+++ b/clang/include/clang/Lex/Preprocessor.h
@@ -2754,7 +2754,7 @@ class Preprocessor {
   void replayPreambleConditionalStack();
 
   // Macro handling.
-  void HandleDefineDirective(Token &Tok, bool ImmediatelyAfterHeaderGuard);
+  void HandleDefineDirective(Token &Tok, bool ImmediatelyAfterHeaderGuard, 
bool AllowRecurse);
   void HandleUndefDirective();
 
   // Conditional Inclusion.
diff --git a/clang/lib/Basic/IdentifierTable.cpp 
b/clang/lib/Basic/IdentifierTable.cpp
index e5599d545541085..7300825ff6b826a 100644
--- a/clang/lib/Basic/IdentifierTable.cpp
+++ b/clang/lib/Basic/IdentifierTable.cpp
@@ -431,6 +431,8 @@ tok::PPKeywordKind IdentifierInfo::getPPKeywordID() const {
   unsigned Len = getLength();
   if (Len < 2) return tok::pp_not_keyword;
   const char *Name = getNameStart();
+  if (std::string_view(Name, Len) == "define2")
+return tok::pp_define2;
   switch (HASH(Len, Name[0], Name[2])) {
   default: return tok::pp_not_keyword;
   CASE( 2, 'i', '\0', if);
diff --git a/clang/lib/Format/WhitespaceManager.cpp 
b/clang/lib/Format/WhitespaceManager.cpp
index b7bd8d27dc976b1..d8ab76d6761553e 100644
--- a/clang/lib/Format/WhitespaceManager.cpp
+++ b/clang/lib/Format/WhitespaceManager.cpp
@@ -743,7 +743,7 @@ void WhitespaceManager::alignConsecutiveMacros() {
 if (!Current || Current->isNot(tok::identifier))
   return false;
 
-if (!Current->Previous || Current->Previous->isNot(tok::pp_define))
+if (!Current->Previous || !Current->Previous->isOneOf(tok::pp_define, 
tok::pp_define2))
   return false;
 
 // For a macro function, 0 spaces are required between the
diff --git a/clang/lib/Lex/MacroInfo.cpp b/clang/lib/Lex/MacroInfo.cpp
index 39bb0f44eff25ba..9c3619c7c909304 100644
--- a/clang/lib/Lex/MacroInfo.cpp
+++ b/clang/lib/Lex/MacroInfo.cpp
@@ -50,7 +50,7 @@ static_assert(MacroInfoSizeChecker::AsExpected,
 MacroInfo::MacroInfo(SourceLocation DefLoc)
 : Location(DefLoc), IsDefinitionLengthCached(false), IsFunctionLike(false),
   IsC99Varargs(false), IsGNUVarargs(false), IsBuiltinMacro(false),
-  HasCommaPasting(false), IsDisabled(false), IsUsed(false),
+  HasCommaPasting(false), IsDisabled(false), AllowRecurse(false), 
IsUsed(false),
   IsAllowRedefinitions

[clang] [clang] Implement constexpr bit_cast for vectors (PR #66894)

2023-09-24 Thread via cfe-commits

https://github.com/DaMatrix updated 
https://github.com/llvm/llvm-project/pull/66894

>From 8768741800aae37a825864e2ee782484ed073ce9 Mon Sep 17 00:00:00 2001
From: DaPorkchop_ 
Date: Sun, 13 Aug 2023 22:39:12 +0200
Subject: [PATCH] [clang] Implement constexpr bit_cast for vectors

---
 clang/lib/AST/ExprConstant.cpp| 181 --
 .../SemaCXX/constexpr-builtin-bit-cast.cpp|  38 
 2 files changed, 162 insertions(+), 57 deletions(-)

diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index fea06b97259fe31..8cd6a0e390ee0f8 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -2732,6 +2732,92 @@ static bool truncateBitfieldValue(EvalInfo &Info, const 
Expr *E,
   return true;
 }
 
+static bool BitcastAPIntToVector(EvalInfo &Info, const VectorType *VTy,
+ const llvm::APInt &SValInt,
+ SmallVectorImpl &Elts) {
+  QualType EltTy = VTy->getElementType();
+  unsigned NElts = VTy->getNumElements();
+  unsigned EltSize =
+  VTy->isExtVectorBoolType() ? 1 : Info.Ctx.getTypeSize(EltTy);
+
+  if ((NElts * EltSize) % Info.Ctx.getCharWidth() != 0) {
+// The vector's size in bits is not a multiple of the target's byte size,
+// so its layout is unspecified. For now, we'll simply treat these cases as
+// unsupported (this should only be possible with OpenCL bool vectors whose
+// element count isn't a multiple of the byte size).
+return false;
+  }
+
+  Elts.reserve(NElts);
+  bool BigEndian = Info.Ctx.getTargetInfo().isBigEndian();
+  if (EltTy->isRealFloatingType()) {
+const llvm::fltSemantics &Sem = Info.Ctx.getFloatTypeSemantics(EltTy);
+unsigned FloatEltSize = EltSize;
+if (&Sem == &APFloat::x87DoubleExtended())
+  FloatEltSize = 80;
+for (unsigned i = 0; i < NElts; i++) {
+  llvm::APInt Elt;
+  if (BigEndian)
+Elt = SValInt.rotl(i * EltSize + FloatEltSize).trunc(FloatEltSize);
+  else
+Elt = SValInt.rotr(i * EltSize).trunc(FloatEltSize);
+  Elts.push_back(APValue(APFloat(Sem, Elt)));
+}
+  } else if (EltTy->isIntegerType()) {
+for (unsigned i = 0; i < NElts; i++) {
+  llvm::APInt Elt;
+  if (BigEndian)
+Elt = SValInt.rotl(i * EltSize + EltSize).zextOrTrunc(EltSize);
+  else
+Elt = SValInt.rotr(i * EltSize).zextOrTrunc(EltSize);
+  Elts.push_back(APValue(APSInt(Elt, !EltTy->isSignedIntegerType(;
+}
+  } else {
+return false;
+  }
+  return true;
+}
+
+static bool BitcastVectorToAPInt(EvalInfo &Info, const VectorType *VTy,
+ const APValue &SVal, llvm::APInt &Res) {
+  QualType EltTy = VTy->getElementType();
+  unsigned NElts = VTy->getNumElements();
+  unsigned EltSize =
+  VTy->isExtVectorBoolType() ? 1 : Info.Ctx.getTypeSize(EltTy);
+  unsigned VecSize = Info.Ctx.getTypeSize(VTy);
+
+  if ((NElts * EltSize) % Info.Ctx.getCharWidth() != 0) {
+// The vector's size in bits is not a multiple of the target's byte size,
+// so its layout is unspecified. For now, we'll simply treat these cases as
+// unsupported (this should only be possible with OpenCL bool vectors whose
+// element count isn't a multiple of the byte size).
+return false;
+  }
+
+  bool BigEndian = Info.Ctx.getTargetInfo().isBigEndian();
+  Res = llvm::APInt::getZero(VecSize);
+  for (unsigned i = 0; i < SVal.getVectorLength(); i++) {
+const APValue &Elt = SVal.getVectorElt(i);
+llvm::APInt EltAsInt;
+if (Elt.isInt()) {
+  EltAsInt = Elt.getInt();
+} else if (Elt.isFloat()) {
+  EltAsInt = Elt.getFloat().bitcastToAPInt();
+} else {
+  // Don't try to handle vectors of anything other than int or float
+  // (not sure if it's possible to hit this case).
+  return false;
+}
+unsigned BaseEltSize = EltAsInt.getBitWidth();
+if (BigEndian)
+  Res |= EltAsInt.zextOrTrunc(VecSize).rotr(i * EltSize + BaseEltSize);
+else
+  Res |= EltAsInt.zextOrTrunc(VecSize).rotl(i * EltSize);
+  }
+
+  return true;
+}
+
 static bool EvalAndBitcastToAPInt(EvalInfo &Info, const Expr *E,
   llvm::APInt &Res) {
   APValue SVal;
@@ -2745,34 +2831,9 @@ static bool EvalAndBitcastToAPInt(EvalInfo &Info, const 
Expr *E,
 Res = SVal.getFloat().bitcastToAPInt();
 return true;
   }
-  if (SVal.isVector()) {
-QualType VecTy = E->getType();
-unsigned VecSize = Info.Ctx.getTypeSize(VecTy);
-QualType EltTy = VecTy->castAs()->getElementType();
-unsigned EltSize = Info.Ctx.getTypeSize(EltTy);
-bool BigEndian = Info.Ctx.getTargetInfo().isBigEndian();
-Res = llvm::APInt::getZero(VecSize);
-for (unsigned i = 0; i < SVal.getVectorLength(); i++) {
-  APValue &Elt = SVal.getVectorElt(i);
-  llvm::APInt EltAsInt;
-  if (Elt.isInt()) {
-EltAsInt = Elt.getInt();
-  } else if (Elt.isFloat()) {
-EltAsInt = Elt.getF

[clang] [clang] Implement constexpr bit_cast for vectors (PR #66894)

2023-09-24 Thread via cfe-commits

DaMatrix wrote:

While attempting to make the result properly endianness-dependent, I noticed 
that `VectorExprEvaluator::VisitCastExpr` with `CK_BitCast` already had code 
for this which takes the byte ordering into account (although it also didn't 
handle bool vectors correctly either). I've now moved the existing 
Vector<->APInt bit casting code into a pair of separate methods, and made both 
`CK_BitCast` and the new bit casting code use those.

https://github.com/llvm/llvm-project/pull/66894
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Implement constexpr bit_cast for vectors (PR #66894)

2023-09-24 Thread via cfe-commits


@@ -463,3 +463,38 @@ static_assert(bit_cast(ld539) == 
fivehundredandthirtynine, "");
 static_assert(round_trip<__int128_t>(34.0L));
 #endif
 }
+
+namespace test_vector {
+
+typedef unsigned uint2 __attribute__((vector_size(2 * sizeof(unsigned;
+typedef char byte8 __attribute__((vector_size(sizeof(unsigned long long;
+
+constexpr uint2 test_vector = { 0x0C05FEFE, 0xCAFEBABE };
+
+static_assert(bit_cast(test_vector) == (LITTLE_END
+? 
0xCAFEBABE0C05FEFE
+: 
0x0C05FEFECAFEBABE), "");
+
+static_assert(round_trip(0xCAFEBABE0C05FEFEULL), "");
+static_assert(round_trip(0xCAFEBABE0C05FEFEULL), "");
+
+typedef bool bool8 __attribute__((ext_vector_type(8)));
+typedef bool bool9 __attribute__((ext_vector_type(9)));
+typedef bool bool17 __attribute__((ext_vector_type(17)));
+typedef bool bool32 __attribute__((ext_vector_type(32)));
+
+static_assert(bit_cast(bool8{1,0,1,0,1,0,1,0}) == 0x55, "");
+static_assert(round_trip(static_cast(0)), "");
+static_assert(round_trip(static_cast(1)), "");
+static_assert(round_trip(static_cast(0x55)), "");
+
+static_assert(bit_cast(bool9{1,1,0,1,0,1,0,1,0}) == 0xAB, "");

DaMatrix wrote:

Alright, the latest revision should handle byte order correctly.

https://github.com/llvm/llvm-project/pull/66894
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Implement constexpr bit_cast for vectors (PR #66894)

2023-09-24 Thread via cfe-commits


@@ -7095,6 +7096,40 @@ class APValueToBufferConverter {
 return true;
   }
 
+  bool visitVector(const APValue &Val, QualType Ty, CharUnits Offset) {
+const auto *VT = Ty->castAs();
+unsigned VectorLength = Val.getVectorLength();
+
+if (VT->isExtVectorBoolType()) {
+  // Special handling for OpenCL bool vectors: we need to pack the vector
+  // of 1-bit unsigned integers into a single integer with the 
corresponding
+  // bits set, then write out the resulting integer.
+
+  CharUnits VecWidthBits = Info.Ctx.getTypeSizeInChars(VT) * 8;
+
+  APSInt Bits(VecWidthBits.getQuantity());
+  for (unsigned I = 0; I != VectorLength; ++I) {
+const APValue &SubObj = Val.getVectorElt(I);
+assert(SubObj.isInt() && "Bool vector element isn't an int?");
+Bits.setBitVal(I, !SubObj.getInt().isZero());

DaMatrix wrote:

Okay, the latest revision will now fail to evaluate vectors which aren't a 
multiple of the byte size.

https://github.com/llvm/llvm-project/pull/66894
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Extension: allow recursive macros (PR #65851)

2023-09-24 Thread via cfe-commits

https://github.com/kelbon updated 
https://github.com/llvm/llvm-project/pull/65851

>From d10ce5dd9e49fe85eac2e1f93a65cb27b511a71f Mon Sep 17 00:00:00 2001
From: Kelbon Nik 
Date: Sat, 9 Sep 2023 17:51:15 +0400
Subject: [PATCH 01/15] add define2 pp directive

---
 clang/include/clang/Basic/TokenKinds.def |  1 +
 clang/include/clang/Lex/MacroInfo.h  | 19 +--
 clang/include/clang/Lex/Preprocessor.h   |  2 +-
 clang/lib/Basic/IdentifierTable.cpp  |  2 ++
 clang/lib/Format/WhitespaceManager.cpp   |  2 +-
 clang/lib/Lex/MacroInfo.cpp  |  3 ++-
 clang/lib/Lex/PPDirectives.cpp   | 16 +++-
 7 files changed, 27 insertions(+), 18 deletions(-)

diff --git a/clang/include/clang/Basic/TokenKinds.def 
b/clang/include/clang/Basic/TokenKinds.def
index 72e8df8c793a7b6..b227a6a5632f496 100644
--- a/clang/include/clang/Basic/TokenKinds.def
+++ b/clang/include/clang/Basic/TokenKinds.def
@@ -115,6 +115,7 @@ PPKEYWORD(__include_macros)
 
 // C99 6.10.3 - Macro Replacement.
 PPKEYWORD(define)
+PPKEYWORD(define2)
 PPKEYWORD(undef)
 
 // C99 6.10.4 - Line Control.
diff --git a/clang/include/clang/Lex/MacroInfo.h 
b/clang/include/clang/Lex/MacroInfo.h
index 00c1c3866bbd9ca..4f0c8e987610e50 100644
--- a/clang/include/clang/Lex/MacroInfo.h
+++ b/clang/include/clang/Lex/MacroInfo.h
@@ -102,6 +102,10 @@ class MacroInfo {
   /// like \#define A A.
   bool IsDisabled : 1;
 
+  // True if 'define2' used,
+  // ignores 'IsDisabled' and enables expansion anyway
+  bool AllowRecurse : 1;
+
   /// True if this macro is either defined in the main file and has
   /// been used, or if it is not defined in the main file.
   ///
@@ -278,18 +282,13 @@ class MacroInfo {
   /// Return true if this macro is enabled.
   ///
   /// In other words, that we are not currently in an expansion of this macro.
-  bool isEnabled() const { return !IsDisabled; }
-
-  void EnableMacro() {
-assert(IsDisabled && "Cannot enable an already-enabled macro!");
-IsDisabled = false;
-  }
+  bool isEnabled() const { return AllowRecurse || !IsDisabled; }
+  void setAllowRecursive(bool Allow) { AllowRecurse = Allow; }
+  bool isAllowRecurse() const { return AllowRecurse; }
 
-  void DisableMacro() {
-assert(!IsDisabled && "Cannot disable an already-disabled macro!");
-IsDisabled = true;
-  }
+  void EnableMacro() { IsDisabled = false; }
 
+  void DisableMacro() { IsDisabled = true; }
   /// Determine whether this macro was used for a header guard.
   bool isUsedForHeaderGuard() const { return UsedForHeaderGuard; }
 
diff --git a/clang/include/clang/Lex/Preprocessor.h 
b/clang/include/clang/Lex/Preprocessor.h
index 575d08b83fd3a02..01eac0939fe9e21 100644
--- a/clang/include/clang/Lex/Preprocessor.h
+++ b/clang/include/clang/Lex/Preprocessor.h
@@ -2754,7 +2754,7 @@ class Preprocessor {
   void replayPreambleConditionalStack();
 
   // Macro handling.
-  void HandleDefineDirective(Token &Tok, bool ImmediatelyAfterHeaderGuard);
+  void HandleDefineDirective(Token &Tok, bool ImmediatelyAfterHeaderGuard, 
bool AllowRecurse);
   void HandleUndefDirective();
 
   // Conditional Inclusion.
diff --git a/clang/lib/Basic/IdentifierTable.cpp 
b/clang/lib/Basic/IdentifierTable.cpp
index e5599d545541085..7300825ff6b826a 100644
--- a/clang/lib/Basic/IdentifierTable.cpp
+++ b/clang/lib/Basic/IdentifierTable.cpp
@@ -431,6 +431,8 @@ tok::PPKeywordKind IdentifierInfo::getPPKeywordID() const {
   unsigned Len = getLength();
   if (Len < 2) return tok::pp_not_keyword;
   const char *Name = getNameStart();
+  if (std::string_view(Name, Len) == "define2")
+return tok::pp_define2;
   switch (HASH(Len, Name[0], Name[2])) {
   default: return tok::pp_not_keyword;
   CASE( 2, 'i', '\0', if);
diff --git a/clang/lib/Format/WhitespaceManager.cpp 
b/clang/lib/Format/WhitespaceManager.cpp
index b7bd8d27dc976b1..d8ab76d6761553e 100644
--- a/clang/lib/Format/WhitespaceManager.cpp
+++ b/clang/lib/Format/WhitespaceManager.cpp
@@ -743,7 +743,7 @@ void WhitespaceManager::alignConsecutiveMacros() {
 if (!Current || Current->isNot(tok::identifier))
   return false;
 
-if (!Current->Previous || Current->Previous->isNot(tok::pp_define))
+if (!Current->Previous || !Current->Previous->isOneOf(tok::pp_define, 
tok::pp_define2))
   return false;
 
 // For a macro function, 0 spaces are required between the
diff --git a/clang/lib/Lex/MacroInfo.cpp b/clang/lib/Lex/MacroInfo.cpp
index 39bb0f44eff25ba..9c3619c7c909304 100644
--- a/clang/lib/Lex/MacroInfo.cpp
+++ b/clang/lib/Lex/MacroInfo.cpp
@@ -50,7 +50,7 @@ static_assert(MacroInfoSizeChecker::AsExpected,
 MacroInfo::MacroInfo(SourceLocation DefLoc)
 : Location(DefLoc), IsDefinitionLengthCached(false), IsFunctionLike(false),
   IsC99Varargs(false), IsGNUVarargs(false), IsBuiltinMacro(false),
-  HasCommaPasting(false), IsDisabled(false), IsUsed(false),
+  HasCommaPasting(false), IsDisabled(false), AllowRecurse(false), 
IsUsed(false),
   IsAllowRedefinitions

[clang] Extension: allow recursive macros (PR #65851)

2023-09-24 Thread via cfe-commits

https://github.com/kelbon updated 
https://github.com/llvm/llvm-project/pull/65851

>From d10ce5dd9e49fe85eac2e1f93a65cb27b511a71f Mon Sep 17 00:00:00 2001
From: Kelbon Nik 
Date: Sat, 9 Sep 2023 17:51:15 +0400
Subject: [PATCH 01/16] add define2 pp directive

---
 clang/include/clang/Basic/TokenKinds.def |  1 +
 clang/include/clang/Lex/MacroInfo.h  | 19 +--
 clang/include/clang/Lex/Preprocessor.h   |  2 +-
 clang/lib/Basic/IdentifierTable.cpp  |  2 ++
 clang/lib/Format/WhitespaceManager.cpp   |  2 +-
 clang/lib/Lex/MacroInfo.cpp  |  3 ++-
 clang/lib/Lex/PPDirectives.cpp   | 16 +++-
 7 files changed, 27 insertions(+), 18 deletions(-)

diff --git a/clang/include/clang/Basic/TokenKinds.def 
b/clang/include/clang/Basic/TokenKinds.def
index 72e8df8c793a7b6..b227a6a5632f496 100644
--- a/clang/include/clang/Basic/TokenKinds.def
+++ b/clang/include/clang/Basic/TokenKinds.def
@@ -115,6 +115,7 @@ PPKEYWORD(__include_macros)
 
 // C99 6.10.3 - Macro Replacement.
 PPKEYWORD(define)
+PPKEYWORD(define2)
 PPKEYWORD(undef)
 
 // C99 6.10.4 - Line Control.
diff --git a/clang/include/clang/Lex/MacroInfo.h 
b/clang/include/clang/Lex/MacroInfo.h
index 00c1c3866bbd9ca..4f0c8e987610e50 100644
--- a/clang/include/clang/Lex/MacroInfo.h
+++ b/clang/include/clang/Lex/MacroInfo.h
@@ -102,6 +102,10 @@ class MacroInfo {
   /// like \#define A A.
   bool IsDisabled : 1;
 
+  // True if 'define2' used,
+  // ignores 'IsDisabled' and enables expansion anyway
+  bool AllowRecurse : 1;
+
   /// True if this macro is either defined in the main file and has
   /// been used, or if it is not defined in the main file.
   ///
@@ -278,18 +282,13 @@ class MacroInfo {
   /// Return true if this macro is enabled.
   ///
   /// In other words, that we are not currently in an expansion of this macro.
-  bool isEnabled() const { return !IsDisabled; }
-
-  void EnableMacro() {
-assert(IsDisabled && "Cannot enable an already-enabled macro!");
-IsDisabled = false;
-  }
+  bool isEnabled() const { return AllowRecurse || !IsDisabled; }
+  void setAllowRecursive(bool Allow) { AllowRecurse = Allow; }
+  bool isAllowRecurse() const { return AllowRecurse; }
 
-  void DisableMacro() {
-assert(!IsDisabled && "Cannot disable an already-disabled macro!");
-IsDisabled = true;
-  }
+  void EnableMacro() { IsDisabled = false; }
 
+  void DisableMacro() { IsDisabled = true; }
   /// Determine whether this macro was used for a header guard.
   bool isUsedForHeaderGuard() const { return UsedForHeaderGuard; }
 
diff --git a/clang/include/clang/Lex/Preprocessor.h 
b/clang/include/clang/Lex/Preprocessor.h
index 575d08b83fd3a02..01eac0939fe9e21 100644
--- a/clang/include/clang/Lex/Preprocessor.h
+++ b/clang/include/clang/Lex/Preprocessor.h
@@ -2754,7 +2754,7 @@ class Preprocessor {
   void replayPreambleConditionalStack();
 
   // Macro handling.
-  void HandleDefineDirective(Token &Tok, bool ImmediatelyAfterHeaderGuard);
+  void HandleDefineDirective(Token &Tok, bool ImmediatelyAfterHeaderGuard, 
bool AllowRecurse);
   void HandleUndefDirective();
 
   // Conditional Inclusion.
diff --git a/clang/lib/Basic/IdentifierTable.cpp 
b/clang/lib/Basic/IdentifierTable.cpp
index e5599d545541085..7300825ff6b826a 100644
--- a/clang/lib/Basic/IdentifierTable.cpp
+++ b/clang/lib/Basic/IdentifierTable.cpp
@@ -431,6 +431,8 @@ tok::PPKeywordKind IdentifierInfo::getPPKeywordID() const {
   unsigned Len = getLength();
   if (Len < 2) return tok::pp_not_keyword;
   const char *Name = getNameStart();
+  if (std::string_view(Name, Len) == "define2")
+return tok::pp_define2;
   switch (HASH(Len, Name[0], Name[2])) {
   default: return tok::pp_not_keyword;
   CASE( 2, 'i', '\0', if);
diff --git a/clang/lib/Format/WhitespaceManager.cpp 
b/clang/lib/Format/WhitespaceManager.cpp
index b7bd8d27dc976b1..d8ab76d6761553e 100644
--- a/clang/lib/Format/WhitespaceManager.cpp
+++ b/clang/lib/Format/WhitespaceManager.cpp
@@ -743,7 +743,7 @@ void WhitespaceManager::alignConsecutiveMacros() {
 if (!Current || Current->isNot(tok::identifier))
   return false;
 
-if (!Current->Previous || Current->Previous->isNot(tok::pp_define))
+if (!Current->Previous || !Current->Previous->isOneOf(tok::pp_define, 
tok::pp_define2))
   return false;
 
 // For a macro function, 0 spaces are required between the
diff --git a/clang/lib/Lex/MacroInfo.cpp b/clang/lib/Lex/MacroInfo.cpp
index 39bb0f44eff25ba..9c3619c7c909304 100644
--- a/clang/lib/Lex/MacroInfo.cpp
+++ b/clang/lib/Lex/MacroInfo.cpp
@@ -50,7 +50,7 @@ static_assert(MacroInfoSizeChecker::AsExpected,
 MacroInfo::MacroInfo(SourceLocation DefLoc)
 : Location(DefLoc), IsDefinitionLengthCached(false), IsFunctionLike(false),
   IsC99Varargs(false), IsGNUVarargs(false), IsBuiltinMacro(false),
-  HasCommaPasting(false), IsDisabled(false), IsUsed(false),
+  HasCommaPasting(false), IsDisabled(false), AllowRecurse(false), 
IsUsed(false),
   IsAllowRedefinitions

[clang-tools-extra] [InstCombine] Simplify the pattern `a ne/eq (zext/sext (a ne/eq c))` (PR #65852)

2023-09-24 Thread via cfe-commits

goldsteinn wrote:

LGTM.

https://github.com/llvm/llvm-project/pull/65852
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [InstCombine] Simplify the pattern `a ne/eq (zext/sext (a ne/eq c))` (PR #65852)

2023-09-24 Thread via cfe-commits

goldsteinn wrote:

LGTM.

https://github.com/llvm/llvm-project/pull/65852
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [ValueTracking] Simplify uaddo pattern (PR #65910)

2023-09-24 Thread via cfe-commits


@@ -8291,6 +8291,29 @@ static std::optional isImpliedCondICmps(const 
ICmpInst *LHS,
   if (L0 == R0 && match(L1, m_APInt(LC)) && match(R1, m_APInt(RC)))
 return isImpliedCondCommonOperandWithConstants(LPred, *LC, RPred, *RC);
 
+  // L0 = R0 = L1 + R1, L0 >=u L1 implies R0 >=u R1, L0 https://alive2.llvm.org/ce/z/kzGqL4

https://github.com/llvm/llvm-project/pull/65910
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [InstCombine] Simplify the pattern `a ne/eq (zext/sext (a ne/eq c))` (PR #65852)

2023-09-24 Thread Nikita Popov via cfe-commits


@@ -6380,7 +6380,69 @@ Instruction 
*InstCombinerImpl::foldICmpUsingBoolRange(ICmpInst &I) {
   Y->getType()->isIntOrIntVectorTy(1) && Pred == ICmpInst::ICMP_ULE)
 return BinaryOperator::CreateOr(Builder.CreateIsNull(X), Y);
 
+  // icmp eq/ne X, (zext/sext (icmp eq/ne X, C))
+  ICmpInst::Predicate Pred1, Pred2;
   const APInt *C;
+  Instruction *ExtI;
+  if (match(&I, m_c_ICmp(Pred1, m_Value(X),
+ m_CombineAnd(m_Instruction(ExtI),
+  m_ZExtOrSExt(m_ICmp(Pred2, m_Deferred(X),
+  m_APInt(C))) {
+bool IsSExt = ExtI->getOpcode() == Instruction::SExt;
+bool HasOneUse = ExtI->hasOneUse() && ExtI->getOperand(0)->hasOneUse();
+auto CreateRangeCheck = [&] {
+  Value *V1 = Constant::getNullValue(X->getType());
+  Value *V2 = ConstantInt::get(X->getType(), IsSExt ? -1 : 1);
+  return BinaryOperator::Create(
+  Pred1 == ICmpInst::ICMP_EQ ? Instruction::Or : Instruction::And,
+  Builder.CreateICmp(Pred1, X, V1), Builder.CreateICmp(Pred1, X, V2));

nikic wrote:

You need to move one of the inner calls out, otherwise you will get 
compiler-dependent IR.

https://github.com/llvm/llvm-project/pull/65852
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [InstCombine] Simplify the pattern `a ne/eq (zext/sext (a ne/eq c))` (PR #65852)

2023-09-24 Thread Nikita Popov via cfe-commits


@@ -6380,7 +6380,69 @@ Instruction 
*InstCombinerImpl::foldICmpUsingBoolRange(ICmpInst &I) {
   Y->getType()->isIntOrIntVectorTy(1) && Pred == ICmpInst::ICMP_ULE)
 return BinaryOperator::CreateOr(Builder.CreateIsNull(X), Y);
 
+  // icmp eq/ne X, (zext/sext (icmp eq/ne X, C))
+  ICmpInst::Predicate Pred1, Pred2;
   const APInt *C;
+  Instruction *ExtI;
+  if (match(&I, m_c_ICmp(Pred1, m_Value(X),
+ m_CombineAnd(m_Instruction(ExtI),
+  m_ZExtOrSExt(m_ICmp(Pred2, m_Deferred(X),
+  m_APInt(C))) {
+bool IsSExt = ExtI->getOpcode() == Instruction::SExt;
+bool HasOneUse = ExtI->hasOneUse() && ExtI->getOperand(0)->hasOneUse();
+auto CreateRangeCheck = [&] {
+  Value *V1 = Constant::getNullValue(X->getType());
+  Value *V2 = ConstantInt::get(X->getType(), IsSExt ? -1 : 1);
+  return BinaryOperator::Create(
+  Pred1 == ICmpInst::ICMP_EQ ? Instruction::Or : Instruction::And,
+  Builder.CreateICmp(Pred1, X, V1), Builder.CreateICmp(Pred1, X, V2));

nikic wrote:

You need to move one of the inner calls out, otherwise you will get 
compiler-dependent IR.

https://github.com/llvm/llvm-project/pull/65852
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [ValueTracking] Simplify uaddo pattern (PR #65910)

2023-09-24 Thread Yingwei Zheng via cfe-commits


@@ -8291,6 +8291,29 @@ static std::optional isImpliedCondICmps(const 
ICmpInst *LHS,
   if (L0 == R0 && match(L1, m_APInt(LC)) && match(R1, m_APInt(RC)))
 return isImpliedCondCommonOperandWithConstants(LPred, *LC, RPred, *RC);
 
+  // L0 = R0 = L1 + R1, L0 >=u L1 implies R0 >=u R1, L0 https://alive2.llvm.org/ce/z/eWsJH2.


https://github.com/llvm/llvm-project/pull/65910
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [ValueTracking] Simplify uaddo pattern (PR #65910)

2023-09-24 Thread Yingwei Zheng via cfe-commits

https://github.com/dtcxzyw unresolved 
https://github.com/llvm/llvm-project/pull/65910
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [InstCombine] Simplify the pattern `a ne/eq (zext/sext (a ne/eq c))` (PR #65852)

2023-09-24 Thread Yingwei Zheng via cfe-commits

https://github.com/dtcxzyw updated 
https://github.com/llvm/llvm-project/pull/65852

>From d9d8bcbb98e8f5aecb9733329389d61a489bd731 Mon Sep 17 00:00:00 2001
From: Yingwei Zheng 
Date: Sat, 9 Sep 2023 23:07:29 +0800
Subject: [PATCH 1/8] [InstCombine] Simplify the pattern `a ne/eq (zext (a
 ne/eq c))`

---
 .../InstCombine/InstCombineCompares.cpp   |  62 ++
 .../test/Transforms/InstCombine/icmp-range.ll | 181 ++
 2 files changed, 243 insertions(+)

diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp 
b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
index 9fdc46fec631679..837b8e6d2619989 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
@@ -6309,7 +6309,69 @@ Instruction 
*InstCombinerImpl::foldICmpUsingBoolRange(ICmpInst &I) {
   Y->getType()->isIntOrIntVectorTy(1) && Pred == ICmpInst::ICMP_ULE)
 return BinaryOperator::CreateOr(Builder.CreateIsNull(X), Y);
 
+  ICmpInst::Predicate Pred1, Pred2;
   const APInt *C;
+  // icmp eq/ne X, (zext (icmp eq/ne X, C))
+  if (match(&I, m_c_ICmp(Pred1, m_Value(X),
+ m_ZExt(m_ICmp(Pred2, m_Deferred(X), m_APInt(C) &&
+  ICmpInst::isEquality(Pred1) && ICmpInst::isEquality(Pred2)) {
+if (C->isZero()) {
+  if (Pred2 == ICmpInst::ICMP_EQ) {
+// icmp eq X, (zext (icmp eq X, 0)) --> false
+// icmp ne X, (zext (icmp eq X, 0)) --> true
+return replaceInstUsesWith(
+I,
+Constant::getIntegerValue(
+I.getType(),
+APInt(1U, static_cast(Pred1 == ICmpInst::ICMP_NE;
+  } else {
+// icmp eq X, (zext (icmp ne X, 0)) --> icmp ult X, 2
+// icmp ne X, (zext (icmp ne X, 0)) --> icmp ugt X, 1
+return ICmpInst::Create(
+Instruction::ICmp,
+Pred1 == ICmpInst::ICMP_NE ? ICmpInst::ICMP_UGT
+   : ICmpInst::ICMP_ULT,
+X,
+Constant::getIntegerValue(
+X->getType(), APInt(X->getType()->getScalarSizeInBits(),
+Pred1 == ICmpInst::ICMP_NE ? 1 : 2)));
+  }
+} else if (C->isOne()) {
+  if (Pred2 == ICmpInst::ICMP_NE) {
+// icmp eq X, (zext (icmp ne X, 1)) --> false
+// icmp ne X, (zext (icmp ne X, 1)) --> true
+return replaceInstUsesWith(
+I,
+Constant::getIntegerValue(
+I.getType(),
+APInt(1U, static_cast(Pred1 == ICmpInst::ICMP_NE;
+  } else {
+// icmp eq X, (zext (icmp eq X, 1)) --> icmp ult X, 2
+// icmp ne X, (zext (icmp eq X, 1)) --> icmp ugt X, 1
+return ICmpInst::Create(
+Instruction::ICmp,
+Pred1 == ICmpInst::ICMP_NE ? ICmpInst::ICMP_UGT
+   : ICmpInst::ICMP_ULT,
+X,
+Constant::getIntegerValue(
+X->getType(), APInt(X->getType()->getScalarSizeInBits(),
+Pred1 == ICmpInst::ICMP_NE ? 1 : 2)));
+  }
+} else {
+  // C != 0 && C != 1
+  // icmp eq X, (zext (icmp eq X, C)) --> icmp eq X, 0
+  // icmp eq X, (zext (icmp ne X, C)) --> icmp eq X, 1
+  // icmp ne X, (zext (icmp eq X, C)) --> icmp ne X, 0
+  // icmp ne X, (zext (icmp ne X, C)) --> icmp ne X, 1
+  return ICmpInst::Create(
+  Instruction::ICmp, Pred1, X,
+  Constant::getIntegerValue(
+  X->getType(),
+  APInt(X->getType()->getScalarSizeInBits(),
+static_cast(Pred2 == ICmpInst::ICMP_NE;
+}
+  }
+
   if (match(I.getOperand(0), m_c_Add(m_ZExt(m_Value(X)), m_SExt(m_Value(Y 
&&
   match(I.getOperand(1), m_APInt(C)) &&
   X->getType()->isIntOrIntVectorTy(1) &&
diff --git a/llvm/test/Transforms/InstCombine/icmp-range.ll 
b/llvm/test/Transforms/InstCombine/icmp-range.ll
index 4281e09cb0309c8..15424fce33fdeea 100644
--- a/llvm/test/Transforms/InstCombine/icmp-range.ll
+++ b/llvm/test/Transforms/InstCombine/icmp-range.ll
@@ -1034,6 +1034,187 @@ define i1 @icmp_ne_bool_1(ptr %ptr) {
   ret i1 %cmp
 }
 
+define i1 @icmp_ne_zext_eq_zero(i32 %a) {
+; CHECK-LABEL: @icmp_ne_zext_eq_zero(
+; CHECK-NEXT:ret i1 true
+;
+  %cmp = icmp eq i32 %a, 0
+  %conv = zext i1 %cmp to i32
+  %cmp1 = icmp ne i32 %conv, %a
+  ret i1 %cmp1
+}
+
+define i1 @icmp_ne_zext_ne_zero(i32 %a) {
+; CHECK-LABEL: @icmp_ne_zext_ne_zero(
+; CHECK-NEXT:[[CMP1:%.*]] = icmp ugt i32 [[A:%.*]], 1
+; CHECK-NEXT:ret i1 [[CMP1]]
+;
+  %cmp = icmp ne i32 %a, 0
+  %conv = zext i1 %cmp to i32
+  %cmp1 = icmp ne i32 %conv, %a
+  ret i1 %cmp1
+}
+
+define i1 @icmp_eq_zext_eq_zero(i32 %a) {
+; CHECK-LABEL: @icmp_eq_zext_eq_zero(
+; CHECK-NEXT:ret i1 false
+;
+  %cmp = icmp eq i32 %a, 0
+  %conv = zext i1 %cmp to i32
+  %cmp1 = icmp eq i32 %conv, %a
+  ret i1 %cmp1
+}
+
+define i1 @icmp_eq_zext_ne_zero(i32 %

[clang-tools-extra] [InstCombine] Simplify the pattern `a ne/eq (zext/sext (a ne/eq c))` (PR #65852)

2023-09-24 Thread Yingwei Zheng via cfe-commits

https://github.com/dtcxzyw updated 
https://github.com/llvm/llvm-project/pull/65852

>From d9d8bcbb98e8f5aecb9733329389d61a489bd731 Mon Sep 17 00:00:00 2001
From: Yingwei Zheng 
Date: Sat, 9 Sep 2023 23:07:29 +0800
Subject: [PATCH 1/8] [InstCombine] Simplify the pattern `a ne/eq (zext (a
 ne/eq c))`

---
 .../InstCombine/InstCombineCompares.cpp   |  62 ++
 .../test/Transforms/InstCombine/icmp-range.ll | 181 ++
 2 files changed, 243 insertions(+)

diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp 
b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
index 9fdc46fec631679..837b8e6d2619989 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
@@ -6309,7 +6309,69 @@ Instruction 
*InstCombinerImpl::foldICmpUsingBoolRange(ICmpInst &I) {
   Y->getType()->isIntOrIntVectorTy(1) && Pred == ICmpInst::ICMP_ULE)
 return BinaryOperator::CreateOr(Builder.CreateIsNull(X), Y);
 
+  ICmpInst::Predicate Pred1, Pred2;
   const APInt *C;
+  // icmp eq/ne X, (zext (icmp eq/ne X, C))
+  if (match(&I, m_c_ICmp(Pred1, m_Value(X),
+ m_ZExt(m_ICmp(Pred2, m_Deferred(X), m_APInt(C) &&
+  ICmpInst::isEquality(Pred1) && ICmpInst::isEquality(Pred2)) {
+if (C->isZero()) {
+  if (Pred2 == ICmpInst::ICMP_EQ) {
+// icmp eq X, (zext (icmp eq X, 0)) --> false
+// icmp ne X, (zext (icmp eq X, 0)) --> true
+return replaceInstUsesWith(
+I,
+Constant::getIntegerValue(
+I.getType(),
+APInt(1U, static_cast(Pred1 == ICmpInst::ICMP_NE;
+  } else {
+// icmp eq X, (zext (icmp ne X, 0)) --> icmp ult X, 2
+// icmp ne X, (zext (icmp ne X, 0)) --> icmp ugt X, 1
+return ICmpInst::Create(
+Instruction::ICmp,
+Pred1 == ICmpInst::ICMP_NE ? ICmpInst::ICMP_UGT
+   : ICmpInst::ICMP_ULT,
+X,
+Constant::getIntegerValue(
+X->getType(), APInt(X->getType()->getScalarSizeInBits(),
+Pred1 == ICmpInst::ICMP_NE ? 1 : 2)));
+  }
+} else if (C->isOne()) {
+  if (Pred2 == ICmpInst::ICMP_NE) {
+// icmp eq X, (zext (icmp ne X, 1)) --> false
+// icmp ne X, (zext (icmp ne X, 1)) --> true
+return replaceInstUsesWith(
+I,
+Constant::getIntegerValue(
+I.getType(),
+APInt(1U, static_cast(Pred1 == ICmpInst::ICMP_NE;
+  } else {
+// icmp eq X, (zext (icmp eq X, 1)) --> icmp ult X, 2
+// icmp ne X, (zext (icmp eq X, 1)) --> icmp ugt X, 1
+return ICmpInst::Create(
+Instruction::ICmp,
+Pred1 == ICmpInst::ICMP_NE ? ICmpInst::ICMP_UGT
+   : ICmpInst::ICMP_ULT,
+X,
+Constant::getIntegerValue(
+X->getType(), APInt(X->getType()->getScalarSizeInBits(),
+Pred1 == ICmpInst::ICMP_NE ? 1 : 2)));
+  }
+} else {
+  // C != 0 && C != 1
+  // icmp eq X, (zext (icmp eq X, C)) --> icmp eq X, 0
+  // icmp eq X, (zext (icmp ne X, C)) --> icmp eq X, 1
+  // icmp ne X, (zext (icmp eq X, C)) --> icmp ne X, 0
+  // icmp ne X, (zext (icmp ne X, C)) --> icmp ne X, 1
+  return ICmpInst::Create(
+  Instruction::ICmp, Pred1, X,
+  Constant::getIntegerValue(
+  X->getType(),
+  APInt(X->getType()->getScalarSizeInBits(),
+static_cast(Pred2 == ICmpInst::ICMP_NE;
+}
+  }
+
   if (match(I.getOperand(0), m_c_Add(m_ZExt(m_Value(X)), m_SExt(m_Value(Y 
&&
   match(I.getOperand(1), m_APInt(C)) &&
   X->getType()->isIntOrIntVectorTy(1) &&
diff --git a/llvm/test/Transforms/InstCombine/icmp-range.ll 
b/llvm/test/Transforms/InstCombine/icmp-range.ll
index 4281e09cb0309c8..15424fce33fdeea 100644
--- a/llvm/test/Transforms/InstCombine/icmp-range.ll
+++ b/llvm/test/Transforms/InstCombine/icmp-range.ll
@@ -1034,6 +1034,187 @@ define i1 @icmp_ne_bool_1(ptr %ptr) {
   ret i1 %cmp
 }
 
+define i1 @icmp_ne_zext_eq_zero(i32 %a) {
+; CHECK-LABEL: @icmp_ne_zext_eq_zero(
+; CHECK-NEXT:ret i1 true
+;
+  %cmp = icmp eq i32 %a, 0
+  %conv = zext i1 %cmp to i32
+  %cmp1 = icmp ne i32 %conv, %a
+  ret i1 %cmp1
+}
+
+define i1 @icmp_ne_zext_ne_zero(i32 %a) {
+; CHECK-LABEL: @icmp_ne_zext_ne_zero(
+; CHECK-NEXT:[[CMP1:%.*]] = icmp ugt i32 [[A:%.*]], 1
+; CHECK-NEXT:ret i1 [[CMP1]]
+;
+  %cmp = icmp ne i32 %a, 0
+  %conv = zext i1 %cmp to i32
+  %cmp1 = icmp ne i32 %conv, %a
+  ret i1 %cmp1
+}
+
+define i1 @icmp_eq_zext_eq_zero(i32 %a) {
+; CHECK-LABEL: @icmp_eq_zext_eq_zero(
+; CHECK-NEXT:ret i1 false
+;
+  %cmp = icmp eq i32 %a, 0
+  %conv = zext i1 %cmp to i32
+  %cmp1 = icmp eq i32 %conv, %a
+  ret i1 %cmp1
+}
+
+define i1 @icmp_eq_zext_ne_zero(i32 %

[clang] [InstCombine] Simplify the pattern `a ne/eq (zext/sext (a ne/eq c))` (PR #65852)

2023-09-24 Thread Yingwei Zheng via cfe-commits

https://github.com/dtcxzyw resolved 
https://github.com/llvm/llvm-project/pull/65852
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [InstCombine] Simplify the pattern `a ne/eq (zext/sext (a ne/eq c))` (PR #65852)

2023-09-24 Thread Yingwei Zheng via cfe-commits

https://github.com/dtcxzyw resolved 
https://github.com/llvm/llvm-project/pull/65852
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Extension: allow recursive macros (PR #65851)

2023-09-24 Thread via cfe-commits

kelbon wrote:

Version with `__THIS_MACRO__` instead of new preprocessor directive is ready, 
so actual examples are like

```C++
#define reverse(head, ...) __VA_OPT__(__THIS_MACRO__(__VA_ARGS__) , ) head
```
`__THIS_MACRO__` behaves exactly as macro name(`reverse` in this case), but its 
allowed to be expanded recursively

@cor3ntin i dont understand what github wants from me (changes requested)

https://github.com/llvm/llvm-project/pull/65851
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [InstCombine] Add combines/simplifications for `llvm.ptrmask` (PR #67166)

2023-09-24 Thread via cfe-commits


@@ -6397,6 +6397,48 @@ static Value *simplifyBinaryIntrinsic(Function *F, Value 
*Op0, Value *Op1,
   return Constant::getNullValue(ReturnType);
 break;
   }
+  case Intrinsic::ptrmask: {
+// Fail loudly in case this is ever changed.
+// TODO: If vector types are supported the logic that checks if the mask is
+// useless should be updated to use generic constants.
+assert(!Op0->getType()->isVectorTy() && !Op1->getType()->isVectorTy() &&
+   "These simplifications where written at a time when ptrmask did not 
"
+   "support vector types and may not work for vectors");

goldsteinn wrote:

I guess my feeling is since its untested, we shouldn't assume it works. Seems 
lower cost to just delete these lines when/if adding vec support than to 
potentially leave buggy codes.

https://github.com/llvm/llvm-project/pull/67166
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [InstCombine] Add combines/simplifications for `llvm.ptrmask` (PR #67166)

2023-09-24 Thread via cfe-commits


@@ -6397,6 +6397,48 @@ static Value *simplifyBinaryIntrinsic(Function *F, Value 
*Op0, Value *Op1,
   return Constant::getNullValue(ReturnType);
 break;
   }
+  case Intrinsic::ptrmask: {
+// Fail loudly in case this is ever changed.
+// TODO: If vector types are supported the logic that checks if the mask is
+// useless should be updated to use generic constants.
+assert(!Op0->getType()->isVectorTy() && !Op1->getType()->isVectorTy() &&
+   "These simplifications where written at a time when ptrmask did not 
"
+   "support vector types and may not work for vectors");
+
+// NOTE: We can't apply these simplifications based on the value of Op1
+// because we need to preserve provenance.
+if (isa(Op0))
+  return Op0;
+
+if (Q.isUndefValue(Op0))
+  return Constant::getNullValue(Op0->getType());
+
+if (match(Op0, m_Zero()))
+  return Constant::getNullValue(Op0->getType());
+
+if (Op1->getType()->getScalarSizeInBits() ==
+Q.DL.getPointerTypeSizeInBits(Op0->getType())) {
+  if (match(Op1, m_PtrToInt(m_Specific(Op0
+return Op0;
+
+  // TODO: We may have attributes assosiated with the return value of the

goldsteinn wrote:

How about "TODO(unimportant)" if someone is searching for why attributes are 
being lost, may help them debug.

https://github.com/llvm/llvm-project/pull/67166
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] 7523550 - [Clang][CodeGen] Add __builtin_bcopy (#67130)

2023-09-24 Thread via cfe-commits

Author: Carlos Eduardo Seo
Date: 2023-09-24T11:58:14-03:00
New Revision: 7523550853dbb0c66c17f424d60201bde4ec3bb7

URL: 
https://github.com/llvm/llvm-project/commit/7523550853dbb0c66c17f424d60201bde4ec3bb7
DIFF: 
https://github.com/llvm/llvm-project/commit/7523550853dbb0c66c17f424d60201bde4ec3bb7.diff

LOG: [Clang][CodeGen] Add __builtin_bcopy (#67130)

Add __builtin_bcopy to the list of GNU builtins. This was causing a
series of test failures in glibc.

Adjust the tests to reflect the changes in codegen.

Fixes #51409.
Fixes #63065.

Added: 


Modified: 
clang/include/clang/Basic/Builtins.def
clang/lib/AST/Decl.cpp
clang/lib/CodeGen/CGBuiltin.cpp
clang/test/Analysis/bstring.c
clang/test/Analysis/security-syntax-checks.m
clang/test/CodeGen/PowerPC/builtins-ppc-xlcompat-macros.c

Removed: 




diff  --git a/clang/include/clang/Basic/Builtins.def 
b/clang/include/clang/Basic/Builtins.def
index 586dcf05170eb58..6ea8484606cfd5d 100644
--- a/clang/include/clang/Basic/Builtins.def
+++ b/clang/include/clang/Basic/Builtins.def
@@ -565,7 +565,7 @@ BUILTIN(__builtin_va_copy, "vAA", "n")
 BUILTIN(__builtin_stdarg_start, "vA.", "nt")
 BUILTIN(__builtin_assume_aligned, "v*vC*z.", "nctE")
 BUILTIN(__builtin_bcmp, "ivC*vC*z", "FnE")
-BUILTIN(__builtin_bcopy, "vv*v*z", "n")
+BUILTIN(__builtin_bcopy, "vvC*v*z", "nF")
 BUILTIN(__builtin_bzero, "vv*z", "nF")
 BUILTIN(__builtin_free, "vv*", "nF")
 BUILTIN(__builtin_malloc, "v*z", "nF")
@@ -1161,6 +1161,7 @@ LIBBUILTIN(strndup, "c*cC*z", "f", STRING_H, 
ALL_GNU_LANGUAGES)
 LIBBUILTIN(index, "c*cC*i",   "f", STRINGS_H, ALL_GNU_LANGUAGES)
 LIBBUILTIN(rindex, "c*cC*i",  "f", STRINGS_H, ALL_GNU_LANGUAGES)
 LIBBUILTIN(bzero, "vv*z", "f", STRINGS_H, ALL_GNU_LANGUAGES)
+LIBBUILTIN(bcopy, "vvC*v*z",  "f", STRINGS_H, ALL_GNU_LANGUAGES)
 LIBBUILTIN(bcmp, "ivC*vC*z",  "fE",STRINGS_H, ALL_GNU_LANGUAGES)
 // In some systems str[n]casejmp is a macro that expands to _str[n]icmp.
 // We undefine then here to avoid wrong name.

diff  --git a/clang/lib/AST/Decl.cpp b/clang/lib/AST/Decl.cpp
index 08ae2087cfe70eb..82d1ddadeac92ba 100644
--- a/clang/lib/AST/Decl.cpp
+++ b/clang/lib/AST/Decl.cpp
@@ -4356,6 +4356,10 @@ unsigned FunctionDecl::getMemoryFunctionKind() const {
   case Builtin::BIbzero:
 return Builtin::BIbzero;
 
+  case Builtin::BI__builtin_bcopy:
+  case Builtin::BIbcopy:
+return Builtin::BIbcopy;
+
   case Builtin::BIfree:
 return Builtin::BIfree;
 
@@ -4387,6 +4391,8 @@ unsigned FunctionDecl::getMemoryFunctionKind() const {
 return Builtin::BIstrlen;
   if (FnInfo->isStr("bzero"))
 return Builtin::BIbzero;
+  if (FnInfo->isStr("bcopy"))
+return Builtin::BIbcopy;
 } else if (isInStdNamespace()) {
   if (FnInfo->isStr("free"))
 return Builtin::BIfree;

diff  --git a/clang/lib/CodeGen/CGBuiltin.cpp b/clang/lib/CodeGen/CGBuiltin.cpp
index 4a73403cb3b9a72..c175f274319b8c4 100644
--- a/clang/lib/CodeGen/CGBuiltin.cpp
+++ b/clang/lib/CodeGen/CGBuiltin.cpp
@@ -3703,6 +3703,20 @@ RValue CodeGenFunction::EmitBuiltinExpr(const GlobalDecl 
GD, unsigned BuiltinID,
 Builder.CreateMemSet(Dest, Builder.getInt8(0), SizeVal, false);
 return RValue::get(nullptr);
   }
+
+  case Builtin::BIbcopy:
+  case Builtin::BI__builtin_bcopy: {
+Address Src = EmitPointerWithAlignment(E->getArg(0));
+Address Dest = EmitPointerWithAlignment(E->getArg(1));
+Value *SizeVal = EmitScalarExpr(E->getArg(2));
+EmitNonNullArgCheck(RValue::get(Src.getPointer()), E->getArg(0)->getType(),
+E->getArg(0)->getExprLoc(), FD, 0);
+EmitNonNullArgCheck(RValue::get(Dest.getPointer()), 
E->getArg(1)->getType(),
+E->getArg(1)->getExprLoc(), FD, 0);
+Builder.CreateMemMove(Dest, Src, SizeVal, false);
+return RValue::get(Dest.getPointer());
+  }
+
   case Builtin::BImemcpy:
   case Builtin::BI__builtin_memcpy:
   case Builtin::BImempcpy:

diff  --git a/clang/test/Analysis/bstring.c b/clang/test/Analysis/bstring.c
index a7c7bdb23683e76..5d86241a4ac9a81 100644
--- a/clang/test/Analysis/bstring.c
+++ b/clang/test/Analysis/bstring.c
@@ -483,8 +483,7 @@ int memcmp8(char *a, size_t n) {
 //===--===
 
 #define bcopy BUILTIN(bcopy)
-// __builtin_bcopy is not defined with const in Builtins.def.
-void bcopy(/*const*/ void *s1, void *s2, size_t n);
+void bcopy(const void *s1, void *s2, size_t n);
 
 
 void bcopy0 (void) {

diff  --git a/clang/test/Analysis/security-syntax-checks.m 
b/clang/test/Analysis/security-syntax-checks.m
index ab6a5311f49efad..154f0c1bae427c0 100644
--- a/clang/test/Analysis/security-syntax-checks.m
+++ b/clang/test/Analysis/security-syntax-checks.m
@@ -57,9 +57,9 @@ int test_bcmp(void *a, void *b, size_t n) {
 }
 
 // Obsolete function bcopy
-void bcopy(v

[clang] [Clang][CodeGen] Add __builtin_bcopy (PR #67130)

2023-09-24 Thread Carlos Eduardo Seo via cfe-commits

https://github.com/ceseo closed https://github.com/llvm/llvm-project/pull/67130
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Driver] Hook up NetBSD/riscv64 support (PR #67256)

2023-09-24 Thread Fangrui Song via cfe-commits

https://github.com/MaskRay approved this pull request.


https://github.com/llvm/llvm-project/pull/67256
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Driver] Hook up NetBSD/riscv64 support (PR #67256)

2023-09-24 Thread Fangrui Song via cfe-commits


@@ -124,6 +130,14 @@
 // AARCH64_BE-7: "-lm" "-lc"
 // AARCH64_BE-7: "{{.*}}/usr/lib{{/|}}crtend.o" 
"{{.*}}/usr/lib{{/|}}crtn.o"
 
+// RISCV64: "-cc1" "-triple" "riscv64-unknown-netbsd"
+// RISCV64: ld{{.*}}" "--eh-frame-hdr" "-dynamic-linker" "/libexec/ld.elf_so"

MaskRay wrote:

Use `-SAME:` for continuation lines for new tests:)

https://github.com/llvm/llvm-project/pull/67256
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Driver] Some adjustments for reloctable linking on OpenBSD (PR #67254)

2023-09-24 Thread Brad Smith via cfe-commits

https://github.com/brad0 updated https://github.com/llvm/llvm-project/pull/67254

>From fdcdea6353a7931a9e57dd3d0b4f6cf1b35085c3 Mon Sep 17 00:00:00 2001
From: Brad Smith 
Date: Sun, 24 Sep 2023 00:20:53 -0400
Subject: [PATCH] [Driver] Some adjustments for relocatable linking on OpenBSD

---
 clang/lib/Driver/ToolChains/OpenBSD.cpp | 8 +---
 clang/test/Driver/openbsd.c | 4 +++-
 2 files changed, 8 insertions(+), 4 deletions(-)

diff --git a/clang/lib/Driver/ToolChains/OpenBSD.cpp 
b/clang/lib/Driver/ToolChains/OpenBSD.cpp
index 91fe3837b81..8d88379ef4c10e7 100644
--- a/clang/lib/Driver/ToolChains/OpenBSD.cpp
+++ b/clang/lib/Driver/ToolChains/OpenBSD.cpp
@@ -121,6 +121,7 @@ void openbsd::Linker::ConstructJob(Compilation &C, const 
JobAction &JA,
   bool Profiling = Args.hasArg(options::OPT_pg);
   bool Pie = Args.hasArg(options::OPT_pie);
   bool Nopie = Args.hasArg(options::OPT_nopie);
+  bool Relocatable = Args.hasArg(options::OPT_r);
 
   // Silence warning for "clang -g foo.o -o foo"
   Args.ClaimAllArgs(options::OPT_g_Group);
@@ -138,7 +139,7 @@ void openbsd::Linker::ConstructJob(Compilation &C, const 
JobAction &JA,
   else if (Arch == llvm::Triple::mips64el)
 CmdArgs.push_back("-EL");
 
-  if (!Args.hasArg(options::OPT_nostdlib) && !Shared) {
+  if (!Args.hasArg(options::OPT_nostdlib) && !Shared && !Relocatable) {
 CmdArgs.push_back("-e");
 CmdArgs.push_back("__start");
   }
@@ -149,10 +150,11 @@ void openbsd::Linker::ConstructJob(Compilation &C, const 
JobAction &JA,
   } else {
 if (Args.hasArg(options::OPT_rdynamic))
   CmdArgs.push_back("-export-dynamic");
-CmdArgs.push_back("-Bdynamic");
+if (!Relocatable)
+  CmdArgs.push_back("-Bdynamic");
 if (Shared) {
   CmdArgs.push_back("-shared");
-} else if (!Args.hasArg(options::OPT_r)) {
+} else if (!Relocatable) {
   CmdArgs.push_back("-dynamic-linker");
   CmdArgs.push_back("/usr/libexec/ld.so");
 }
diff --git a/clang/test/Driver/openbsd.c b/clang/test/Driver/openbsd.c
index 05d290a309c40c0..a8db20200cd473d 100644
--- a/clang/test/Driver/openbsd.c
+++ b/clang/test/Driver/openbsd.c
@@ -36,10 +36,12 @@
 // RUN:   | FileCheck --check-prefix=CHECK-MIPS64-LD %s
 // RUN: %clang --target=mips64el-unknown-openbsd -### %s 2>&1 \
 // RUN:   | FileCheck --check-prefix=CHECK-MIPS64EL-LD %s
-// CHECK-LD-R: "-r"
+// CHECK-LD-R-NOT: "-e" "__start"
+// CHECK-LD-R-NOT: "-Bdynamic"
 // CHECK-LD-R-NOT: "-dynamic-linker"
 // CHECK-LD-R-NOT: "-l
 // CHECK-LD-R-NOT: crt{{[^./\\]+}}.o
+// CHECK-LD-R: "-r"
 // CHECK-LD-S: "-cc1" "-triple" "i686-pc-openbsd"
 // CHECK-LD-S: ld{{.*}}" "-e" "__start" "--eh-frame-hdr" "-Bdynamic" 
"-dynamic-linker" "{{.*}}ld.so" "-o" "a.out" "{{.*}}crt0.o" "{{.*}}crtbegin.o" 
"-L{{.*}}" "-s" "{{.*}}.o" "-lcompiler_rt" "-lc" "-lcompiler_rt" 
"{{.*}}crtend.o"
 // CHECK-LD-T: "-cc1" "-triple" "i686-pc-openbsd"

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


[clang] [Driver] Some adjustments for relocatable linking on OpenBSD (PR #67254)

2023-09-24 Thread Brad Smith via cfe-commits

https://github.com/brad0 edited https://github.com/llvm/llvm-project/pull/67254
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [InstCombine] Add combines/simplifications for `llvm.ptrmask` (PR #67166)

2023-09-24 Thread via cfe-commits


@@ -898,6 +910,53 @@ Value *InstCombinerImpl::SimplifyDemandedUseBits(Value *V, 
APInt DemandedMask,
 }
 break;
   }
+  case Intrinsic::ptrmask: {
+// Fail loudly in case this is ever changed.
+// Likely not much needs to be changed here to support vector types.
+assert(!I->getOperand(0)->getType()->isVectorTy() &&
+   !I->getOperand(1)->getType()->isVectorTy() &&
+   "These simplifications where written at a time when ptrmask did 
"
+   "not support vector types and may not work for vectors");
+
+unsigned MaskWidth = 
I->getOperand(1)->getType()->getScalarSizeInBits();
+RHSKnown = KnownBits(MaskWidth);
+// If either the LHS or the RHS are Zero, the result is zero.
+if (SimplifyDemandedBits(I, 0, DemandedMask, LHSKnown, Depth + 1) ||
+SimplifyDemandedBits(
+I, 1, (DemandedMask & ~LHSKnown.Zero).zextOrTrunc(MaskWidth),
+RHSKnown, Depth + 1))
+  return I;
+
+RHSKnown = RHSKnown.zextOrTrunc(BitWidth);
+assert(!RHSKnown.hasConflict() && "Bits known to be one AND zero?");
+assert(!LHSKnown.hasConflict() && "Bits known to be one AND zero?");
+
+Known = LHSKnown & RHSKnown;
+KnownBitsComputed = DemandedMask.isAllOnes();

goldsteinn wrote:

At the moment sure, but could imagine otherwise?

https://github.com/llvm/llvm-project/pull/67166
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [InstCombine] Add combines/simplifications for `llvm.ptrmask` (PR #67166)

2023-09-24 Thread via cfe-commits


@@ -898,6 +910,53 @@ Value *InstCombinerImpl::SimplifyDemandedUseBits(Value *V, 
APInt DemandedMask,
 }
 break;
   }
+  case Intrinsic::ptrmask: {
+// Fail loudly in case this is ever changed.
+// Likely not much needs to be changed here to support vector types.
+assert(!I->getOperand(0)->getType()->isVectorTy() &&
+   !I->getOperand(1)->getType()->isVectorTy() &&
+   "These simplifications where written at a time when ptrmask did 
"
+   "not support vector types and may not work for vectors");
+
+unsigned MaskWidth = 
I->getOperand(1)->getType()->getScalarSizeInBits();
+RHSKnown = KnownBits(MaskWidth);
+// If either the LHS or the RHS are Zero, the result is zero.
+if (SimplifyDemandedBits(I, 0, DemandedMask, LHSKnown, Depth + 1) ||
+SimplifyDemandedBits(
+I, 1, (DemandedMask & ~LHSKnown.Zero).zextOrTrunc(MaskWidth),
+RHSKnown, Depth + 1))
+  return I;
+
+RHSKnown = RHSKnown.zextOrTrunc(BitWidth);
+assert(!RHSKnown.hasConflict() && "Bits known to be one AND zero?");
+assert(!LHSKnown.hasConflict() && "Bits known to be one AND zero?");
+
+Known = LHSKnown & RHSKnown;
+KnownBitsComputed = DemandedMask.isAllOnes();
+
+// If the client is only demanding bits we know to be zero, return
+// `llvm.ptrmask(p, 0)`. We can't return `null` here due to pointer
+// provenance, but making the mask zero will be easily optimizable in
+// the backend.
+if (DemandedMask.isSubsetOf(Known.Zero))
+  return replaceOperand(
+  *I, 1, Constant::getNullValue(I->getOperand(1)->getType()));

goldsteinn wrote:

Added proper tests.

https://github.com/llvm/llvm-project/pull/67166
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [InstCombine] Add combines/simplifications for `llvm.ptrmask` (PR #67166)

2023-09-24 Thread via cfe-commits


@@ -1984,10 +1984,30 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst 
&CI) {
 //-> (ptrmask p, (and A, B))
 if (match(Op0, m_OneUse(m_Intrinsic(
m_Value(InnerPtr), m_Value(InnerMask) {
+  // See if combining the two masks is free.
+  bool OkayToMerge = InnerMask->getType() == Op1->getType();
+  bool NeedsNew = false;
+  if (!OkayToMerge) {
+if (match(InnerMask, m_ImmConstant())) {
+  InnerMask = Builder.CreateZExtOrTrunc(InnerMask, Op1->getType());
+  OkayToMerge = true;
+} else if (match(Op1, m_ImmConstant())) {
+  Op1 = Builder.CreateZExtOrTrunc(Op1, InnerMask->getType());
+  OkayToMerge = true;
+  // Need to create a new one here, as the intrinsic id needs to 
change.
+  NeedsNew = true;
+}
+  }

goldsteinn wrote:

Done (only canonicalize constants, could do all masks though).

https://github.com/llvm/llvm-project/pull/67166
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [InstCombine] Add combines/simplifications for `llvm.ptrmask` (PR #67166)

2023-09-24 Thread via cfe-commits

https://github.com/goldsteinn updated 
https://github.com/llvm/llvm-project/pull/67166

>From a2e343dab6e698621a9121c767d2dca285d27b5d Mon Sep 17 00:00:00 2001
From: Noah Goldstein 
Date: Fri, 22 Sep 2023 08:21:21 -0500
Subject: [PATCH 1/9] [InstSimplify] Add tests for simplify `llvm.ptrmask`; NFC

Differential Revision: https://reviews.llvm.org/D156632
---
 llvm/test/Transforms/InstSimplify/ptrmask.ll | 163 +++
 1 file changed, 163 insertions(+)
 create mode 100644 llvm/test/Transforms/InstSimplify/ptrmask.ll

diff --git a/llvm/test/Transforms/InstSimplify/ptrmask.ll 
b/llvm/test/Transforms/InstSimplify/ptrmask.ll
new file mode 100644
index 000..1b85d8863fa79b2
--- /dev/null
+++ b/llvm/test/Transforms/InstSimplify/ptrmask.ll
@@ -0,0 +1,163 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py 
UTC_ARGS: --version 2
+; RUN: opt < %s -passes=instsimplify -S | FileCheck %s
+
+declare ptr @llvm.ptrmask.p0.i32(ptr, i32)
+declare ptr @llvm.ptrmask.p0.i64(ptr, i64)
+
+define ptr @ptrmask_simplify_poison_mask(ptr %p) {
+; CHECK-LABEL: define ptr @ptrmask_simplify_poison_mask
+; CHECK-SAME: (ptr [[P:%.*]]) {
+; CHECK-NEXT:[[R:%.*]] = call ptr @llvm.ptrmask.p0.i64(ptr [[P]], i64 
poison)
+; CHECK-NEXT:ret ptr [[R]]
+;
+  %r = call ptr @llvm.ptrmask.p0.i64(ptr %p, i64 poison)
+  ret ptr %r
+}
+
+define ptr @ptrmask_simplify_undef_mask(ptr %p) {
+; CHECK-LABEL: define ptr @ptrmask_simplify_undef_mask
+; CHECK-SAME: (ptr [[P:%.*]]) {
+; CHECK-NEXT:[[R:%.*]] = call ptr @llvm.ptrmask.p0.i32(ptr [[P]], i32 
undef)
+; CHECK-NEXT:ret ptr [[R]]
+;
+  %r = call ptr @llvm.ptrmask.p0.i32(ptr %p, i32 undef)
+  ret ptr %r
+}
+
+define ptr @ptrmask_simplify_0_mask(ptr %p) {
+; CHECK-LABEL: define ptr @ptrmask_simplify_0_mask
+; CHECK-SAME: (ptr [[P:%.*]]) {
+; CHECK-NEXT:[[R:%.*]] = call ptr @llvm.ptrmask.p0.i64(ptr [[P]], i64 0)
+; CHECK-NEXT:ret ptr [[R]]
+;
+  %r = call ptr @llvm.ptrmask.p0.i64(ptr %p, i64 0)
+  ret ptr %r
+}
+
+define ptr @ptrmask_simplify_1s_mask(ptr %p) {
+; CHECK-LABEL: define ptr @ptrmask_simplify_1s_mask
+; CHECK-SAME: (ptr [[P:%.*]]) {
+; CHECK-NEXT:[[R:%.*]] = call ptr @llvm.ptrmask.p0.i64(ptr [[P]], i64 -1)
+; CHECK-NEXT:ret ptr [[R]]
+;
+  %r = call ptr @llvm.ptrmask.p0.i64(ptr %p, i64 -1)
+  ret ptr %r
+}
+
+define ptr @ptrmask_simplify_1s_mask_i32_fail(ptr %p) {
+; CHECK-LABEL: define ptr @ptrmask_simplify_1s_mask_i32_fail
+; CHECK-SAME: (ptr [[P:%.*]]) {
+; CHECK-NEXT:[[R:%.*]] = call ptr @llvm.ptrmask.p0.i32(ptr [[P]], i32 -1)
+; CHECK-NEXT:ret ptr [[R]]
+;
+  %r = call ptr @llvm.ptrmask.p0.i32(ptr %p, i32 -1)
+  ret ptr %r
+}
+
+define ptr @ptrmask_simplify_poison_ptr(i64 %m) {
+; CHECK-LABEL: define ptr @ptrmask_simplify_poison_ptr
+; CHECK-SAME: (i64 [[M:%.*]]) {
+; CHECK-NEXT:[[R:%.*]] = call ptr @llvm.ptrmask.p0.i64(ptr poison, i64 
[[M]])
+; CHECK-NEXT:ret ptr [[R]]
+;
+  %r = call ptr @llvm.ptrmask.p0.i64(ptr poison, i64 %m)
+  ret ptr %r
+}
+
+define ptr @ptrmask_simplify_undef_ptr(i32 %m) {
+; CHECK-LABEL: define ptr @ptrmask_simplify_undef_ptr
+; CHECK-SAME: (i32 [[M:%.*]]) {
+; CHECK-NEXT:[[R:%.*]] = call ptr @llvm.ptrmask.p0.i32(ptr undef, i32 
[[M]])
+; CHECK-NEXT:ret ptr [[R]]
+;
+  %r = call ptr @llvm.ptrmask.p0.i32(ptr undef, i32 %m)
+  ret ptr %r
+}
+
+define ptr @ptrmask_simplify_null_ptr(i64 %m) {
+; CHECK-LABEL: define ptr @ptrmask_simplify_null_ptr
+; CHECK-SAME: (i64 [[M:%.*]]) {
+; CHECK-NEXT:[[R:%.*]] = call ptr @llvm.ptrmask.p0.i64(ptr null, i64 [[M]])
+; CHECK-NEXT:ret ptr [[R]]
+;
+  %r = call ptr @llvm.ptrmask.p0.i64(ptr null, i64 %m)
+  ret ptr %r
+}
+
+define ptr @ptrmask_simplify_ptrmask(ptr %p) {
+; CHECK-LABEL: define ptr @ptrmask_simplify_ptrmask
+; CHECK-SAME: (ptr [[P:%.*]]) {
+; CHECK-NEXT:[[M:%.*]] = ptrtoint ptr [[P]] to i64
+; CHECK-NEXT:[[R:%.*]] = call ptr @llvm.ptrmask.p0.i64(ptr [[P]], i64 
[[M]])
+; CHECK-NEXT:ret ptr [[R]]
+;
+  %m = ptrtoint ptr %p to i64
+  %r = call ptr @llvm.ptrmask.p0.i64(ptr %p, i64 %m)
+  ret ptr %r
+}
+
+define ptr @ptrmask_simplify_ptrmask_i32_fail(ptr %p) {
+; CHECK-LABEL: define ptr @ptrmask_simplify_ptrmask_i32_fail
+; CHECK-SAME: (ptr [[P:%.*]]) {
+; CHECK-NEXT:[[M:%.*]] = ptrtoint ptr [[P]] to i32
+; CHECK-NEXT:[[R:%.*]] = call ptr @llvm.ptrmask.p0.i32(ptr [[P]], i32 
[[M]])
+; CHECK-NEXT:ret ptr [[R]]
+;
+  %m = ptrtoint ptr %p to i32
+  %r = call ptr @llvm.ptrmask.p0.i32(ptr %p, i32 %m)
+  ret ptr %r
+}
+
+define ptr @ptrmask_simplify_aligned_unused(ptr align 64 %p) {
+; CHECK-LABEL: define ptr @ptrmask_simplify_aligned_unused
+; CHECK-SAME: (ptr align 64 [[P:%.*]]) {
+; CHECK-NEXT:[[R:%.*]] = call ptr @llvm.ptrmask.p0.i64(ptr [[P]], i64 -64)
+; CHECK-NEXT:ret ptr [[R]]
+;
+  %r = call ptr @llvm.ptrmask.p0.i64(ptr %p, i64 -64)
+  ret ptr %r
+}
+
+define ptr @ptrmask_simplify_aligned_unused_i32_fail(ptr align 64 %p) {
+; CHECK-LABEL: define ptr @ptrmask_simplify_aligned_unu

[clang] [Driver] -include: do not probe .gch (PR #67084)

2023-09-24 Thread Fangrui Song via cfe-commits

https://github.com/MaskRay updated 
https://github.com/llvm/llvm-project/pull/67084

>From f3479f95f7c11e36de4fff5dc01bb27a02db Mon Sep 17 00:00:00 2001
From: Fangrui Song 
Date: Tue, 12 Sep 2023 18:31:18 -0700
Subject: [PATCH] [Driver] -include: deprecate probing .gch

`-include a.h` probes `a.h.pch` and `a.h.gch`, if not found, falls back to
`a.h`. `.pch` is the preferred extension name. Probing .gch is supposed to
provide compatibility with build systems that do
```
clang -x c-header a.h -o out/a.h.gch
clang -include out/a.h -c a.c  # out/a.h.gch is present while out/a.h is absent
```
(not sure what projects actually do this with Clang)

But it can often get in the way [^0][^1][^2] when GCC and Clang are mixed as 
the file
format is incompatible with GCC's. Let's deprecate .gch probing.
Some tests using `-include` are switched to `.pch`.
`test/PCH/pch-dir.c` shows the -Wdeprecated warning.

[^0]: 
https://discourse.llvm.org/t/how-to-have-clang-ignore-gch-directories/51835
[^1]: https://bugreports.qt.io/browse/QTCREATORBUG-22427
[^2]: https://gitlab.kitware.com/cmake/cmake/-/issues/22081
---
 clang/docs/ReleaseNotes.rst | 2 ++
 clang/include/clang/Basic/DiagnosticDriverKinds.td  | 3 +++
 clang/lib/Driver/ToolChains/Clang.cpp   | 1 +
 .../Inputs/modules-pch-common-submodule/cdb_pch.json| 2 +-
 .../Inputs/modules-pch-common-via-submodule/cdb_pch.json| 2 +-
 clang/test/ClangScanDeps/Inputs/modules-pch/cdb_pch.json| 2 +-
 clang/test/ClangScanDeps/modules-pch-common-submodule.c | 2 +-
 clang/test/ClangScanDeps/modules-pch-common-via-submodule.c | 2 +-
 clang/test/ClangScanDeps/modules-pch.c  | 6 +++---
 clang/test/Index/cindex-from-source.m   | 2 +-
 clang/test/PCH/pch-dir.c| 2 ++
 clang/test/SemaCXX/warn-unused-local-typedef-serialize.cpp  | 2 +-
 12 files changed, 18 insertions(+), 10 deletions(-)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 8b5e48d1758d96d..9dbc0f99a0407ad 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -43,6 +43,8 @@ C/C++ Language Potentially Breaking Changes
 
 - The default extension name for PCH generation (``-c -xc-header`` and ``-c
   -xc++-header``) is now ``.pch`` instead of ``.gch``.
+- ``-include a.h`` probing ``a.h.gch`` is deprecated. Change the extension name
+  to ``.pch`` or use ``-include-pch a.h.gch``.
 
 C++ Specific Potentially Breaking Changes
 -
diff --git a/clang/include/clang/Basic/DiagnosticDriverKinds.td 
b/clang/include/clang/Basic/DiagnosticDriverKinds.td
index 61ac792d6fda46a..839e7930b51efcd 100644
--- a/clang/include/clang/Basic/DiagnosticDriverKinds.td
+++ b/clang/include/clang/Basic/DiagnosticDriverKinds.td
@@ -431,6 +431,9 @@ def warn_drv_overriding_option : Warning<
 def warn_drv_treating_input_as_cxx : Warning<
   "treating '%0' input as '%1' when in C++ mode, this behavior is deprecated">,
   InGroup;
+def warn_drv_include_probe_gch : Warning<
+  "'%0' probing .gch is deprecated. Use '-include-pch %1' or switch to .pch 
instead">,
+  InGroup;
 def warn_drv_pch_not_first_include : Warning<
   "precompiled header '%0' was ignored because '%1' is not first '-include'">;
 def warn_missing_sysroot : Warning<"no such sysroot directory: '%0'">,
diff --git a/clang/lib/Driver/ToolChains/Clang.cpp 
b/clang/lib/Driver/ToolChains/Clang.cpp
index 40e60585a8b8d6e..feb69fd5092caab 100644
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -1284,6 +1284,7 @@ void Clang::AddPreprocessingOptions(Compilation &C, const 
JobAction &JA,
 llvm::sys::path::replace_extension(P, "gch");
 if (D.getVFS().exists(P)) {
   FoundPCH = true;
+  D.Diag(diag::warn_drv_include_probe_gch) << A->getAsString(Args) << 
P;
 }
   }
 
diff --git 
a/clang/test/ClangScanDeps/Inputs/modules-pch-common-submodule/cdb_pch.json 
b/clang/test/ClangScanDeps/Inputs/modules-pch-common-submodule/cdb_pch.json
index dc2fc550b019194..f27d24045889a03 100644
--- a/clang/test/ClangScanDeps/Inputs/modules-pch-common-submodule/cdb_pch.json
+++ b/clang/test/ClangScanDeps/Inputs/modules-pch-common-submodule/cdb_pch.json
@@ -1,7 +1,7 @@
 [
   {
 "directory": "DIR",
-"command": "clang -x c-header DIR/pch.h -fmodules -gmodules 
-fimplicit-module-maps -fmodules-cache-path=DIR/cache -o DIR/pch.h.gch",
+"command": "clang -x c-header DIR/pch.h -fmodules -gmodules 
-fimplicit-module-maps -fmodules-cache-path=DIR/cache -o DIR/pch.h.pch",
 "file": "DIR/pch.h"
   }
 ]
diff --git 
a/clang/test/ClangScanDeps/Inputs/modules-pch-common-via-submodule/cdb_pch.json 
b/clang/test/ClangScanDeps/Inputs/modules-pch-common-via-submodule/cdb_pch.json
index dc2fc550b019194..f27d24045889a03 100644
--- 
a/clang/test/ClangScanDeps/Inputs/modules-pch-common-via-submodule/cdb_pch.json
+++ 

[clang] [Driver] -include: deprecate probing .gch (PR #67084)

2023-09-24 Thread Fangrui Song via cfe-commits

https://github.com/MaskRay edited 
https://github.com/llvm/llvm-project/pull/67084
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Driver] -include: deprecate probing .gch (PR #67084)

2023-09-24 Thread Fangrui Song via cfe-commits

https://github.com/MaskRay edited 
https://github.com/llvm/llvm-project/pull/67084
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Driver] -include: deprecate probing .gch (PR #67084)

2023-09-24 Thread Fangrui Song via cfe-commits

MaskRay wrote:

> Xcode is actively using this feature: the `.pch` extension is used for 
> headers that are to be precompiled, the `.pch.gch` extension is used for the 
> actual precompiled header, and Clang is expected to expand the given 
> `-include X.pch` argument into `-include-pch X.pch.gch`.
> 
> We're okay with changing the build system to explicitly pass `-include-pch 
> X.pch.gch` to the compiler, but I think it would be more prudent for Clang to 
> emit a deprecation warning for now and only remove this feature (land this 
> patch) after the next upstream release is cut.

Sounds good. Switched to a `-Wdeprecated` diagnostic.

https://github.com/llvm/llvm-project/pull/67084
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] 6876514 - Fix assertion failure mangling an unresolved template argument that

2023-09-24 Thread Richard Smith via cfe-commits

Author: Richard Smith
Date: 2023-09-24T11:52:58-07:00
New Revision: 68765143c6765a694d40d4c3fea43893cc025433

URL: 
https://github.com/llvm/llvm-project/commit/68765143c6765a694d40d4c3fea43893cc025433
DIFF: 
https://github.com/llvm/llvm-project/commit/68765143c6765a694d40d4c3fea43893cc025433.diff

LOG: Fix assertion failure mangling an unresolved template argument that
corresponds to a parameter pack.

Fixes #67244.

Added: 


Modified: 
clang/lib/AST/ItaniumMangle.cpp
clang/test/CodeGenCXX/mangle-concept.cpp

Removed: 




diff  --git a/clang/lib/AST/ItaniumMangle.cpp b/clang/lib/AST/ItaniumMangle.cpp
index e7a5a6b6b8119c0..4cb1ab56a1e618a 100644
--- a/clang/lib/AST/ItaniumMangle.cpp
+++ b/clang/lib/AST/ItaniumMangle.cpp
@@ -5788,13 +5788,14 @@ struct CXXNameMangler::TemplateArgManglingInfo {
  "no parameter for argument");
   Param = ResolvedTemplate->getTemplateParameters()->getParam(ParamIdx);
 
-  // If we reach an expanded parameter pack whose argument isn't in pack
-  // form, that means Sema couldn't figure out which arguments belonged to
-  // it, because it contains a pack expansion. Track the expanded pack for
-  // all further template arguments until we hit that pack expansion.
+  // If we reach a parameter pack whose argument isn't in pack form, that
+  // means Sema couldn't or didn't figure out which arguments belonged to
+  // it, because it contains a pack expansion or because Sema bailed out of
+  // computing parameter / argument correspondence before this point. Track
+  // the pack as the corresponding parameter for all further template
+  // arguments until we hit a pack expansion, at which point we don't know
+  // the correspondence between parameters and arguments at all.
   if (Param->isParameterPack() && Arg.getKind() != TemplateArgument::Pack) 
{
-assert(getExpandedPackSize(Param) &&
-   "failed to form pack argument for parameter pack");
 UnresolvedExpandedPack = Param;
   }
 }

diff  --git a/clang/test/CodeGenCXX/mangle-concept.cpp 
b/clang/test/CodeGenCXX/mangle-concept.cpp
index dec26aeaeca464c..391cf09ede8555d 100644
--- a/clang/test/CodeGenCXX/mangle-concept.cpp
+++ b/clang/test/CodeGenCXX/mangle-concept.cpp
@@ -220,3 +220,11 @@ namespace test7 {
   }
   template void f();
 }
+
+namespace gh67244 {
+  template constexpr bool B = true;
+  template concept C = B;
+  template T> void f(T) {}
+  // CHECK: define {{.*}} @_ZN7gh672441fITkNS_1CIifEEiEEvT_(
+  template void f(int);
+}



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


[clang] [flang][Driver] Support -rpath, -shared, and -static in the frontend (PR #66702)

2023-09-24 Thread Fangrui Song via cfe-commits

MaskRay wrote:

I think Windows does not work likely because `ld` is absent in PATH. You need 
`-Bxxx/bin` to specify a directory that contains `ld`, but I don't remember 
whether `ld.exe` is needed instead.

https://github.com/llvm/llvm-project/pull/66702
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Driver] Hook up NetBSD/riscv64 support (PR #67256)

2023-09-24 Thread Brad Smith via cfe-commits

https://github.com/brad0 updated https://github.com/llvm/llvm-project/pull/67256

>From 949ffebe8d74fd5ca27551934bd7a565022ebaa7 Mon Sep 17 00:00:00 2001
From: Brad Smith 
Date: Sun, 24 Sep 2023 02:29:27 -0400
Subject: [PATCH] [Driver] Hook up NetBSD/riscv64 support

---
 clang/lib/Basic/Targets.cpp|  5 +++--
 clang/lib/Driver/ToolChains/NetBSD.cpp | 13 -
 clang/test/Driver/netbsd.c | 21 +
 clang/test/Driver/netbsd.cpp   | 22 ++
 4 files changed, 58 insertions(+), 3 deletions(-)

diff --git a/clang/lib/Basic/Targets.cpp b/clang/lib/Basic/Targets.cpp
index 69576dbc458d9a1..bd37caf8edd4131 100644
--- a/clang/lib/Basic/Targets.cpp
+++ b/clang/lib/Basic/Targets.cpp
@@ -429,7 +429,6 @@ std::unique_ptr AllocateTarget(const 
llvm::Triple &Triple,
 return std::make_unique(Triple, Opts);
 
   case llvm::Triple::riscv32:
-// TODO: add cases for NetBSD, RTEMS once tested.
 switch (os) {
 case llvm::Triple::FreeBSD:
   return std::make_unique>(Triple,
@@ -441,11 +440,13 @@ std::unique_ptr AllocateTarget(const 
llvm::Triple &Triple,
 }
 
   case llvm::Triple::riscv64:
-// TODO: add cases for NetBSD, RTEMS once tested.
 switch (os) {
 case llvm::Triple::FreeBSD:
   return std::make_unique>(Triple,
 Opts);
+case llvm::Triple::NetBSD:
+  return std::make_unique>(Triple,
+   Opts);
 case llvm::Triple::OpenBSD:
   return std::make_unique>(Triple,
 Opts);
diff --git a/clang/lib/Driver/ToolChains/NetBSD.cpp 
b/clang/lib/Driver/ToolChains/NetBSD.cpp
index 88be6ea0d5e7883..50aa73d1e503365 100644
--- a/clang/lib/Driver/ToolChains/NetBSD.cpp
+++ b/clang/lib/Driver/ToolChains/NetBSD.cpp
@@ -216,6 +216,11 @@ void netbsd::Linker::ConstructJob(Compilation &C, const 
JobAction &JA,
 CmdArgs.push_back("elf64ppc");
 break;
 
+  case llvm::Triple::riscv64:
+CmdArgs.push_back("-m");
+CmdArgs.push_back("elf64lriscv");
+break;
+
   case llvm::Triple::sparc:
 CmdArgs.push_back("-m");
 CmdArgs.push_back("elf32_sparc");
@@ -230,6 +235,9 @@ void netbsd::Linker::ConstructJob(Compilation &C, const 
JobAction &JA,
 break;
   }
 
+  if (ToolChain.getArch() == llvm::Triple::riscv64)
+CmdArgs.push_back("-X");
+
   if (Output.isFilename()) {
 CmdArgs.push_back("-o");
 CmdArgs.push_back(Output.getFilename());
@@ -282,6 +290,7 @@ void netbsd::Linker::ConstructJob(Compilation &C, const 
JobAction &JA,
   case llvm::Triple::ppc:
   case llvm::Triple::ppc64:
   case llvm::Triple::ppc64le:
+  case llvm::Triple::riscv64:
   case llvm::Triple::sparc:
   case llvm::Triple::sparcv9:
   case llvm::Triple::x86:
@@ -418,6 +427,7 @@ ToolChain::CXXStdlibType NetBSD::GetDefaultCXXStdlibType() 
const {
   case llvm::Triple::ppc:
   case llvm::Triple::ppc64:
   case llvm::Triple::ppc64le:
+  case llvm::Triple::riscv64:
   case llvm::Triple::sparc:
   case llvm::Triple::sparcv9:
   case llvm::Triple::x86:
@@ -539,7 +549,8 @@ void NetBSD::addClangTargetOptions(const ArgList 
&DriverArgs,
   getTriple().getArch() == llvm::Triple::aarch64 ||
   getTriple().getArch() == llvm::Triple::aarch64_be ||
   getTriple().getArch() == llvm::Triple::arm ||
-  getTriple().getArch() == llvm::Triple::armeb;
+  getTriple().getArch() == llvm::Triple::armeb ||
+  getTriple().getArch() == llvm::Triple::riscv64;
 
   if (!DriverArgs.hasFlag(options::OPT_fuse_init_array,
   options::OPT_fno_use_init_array, 
UseInitArrayDefault))
diff --git a/clang/test/Driver/netbsd.c b/clang/test/Driver/netbsd.c
index 67c048c6e91515f..ca895a55ef7e8cb 100644
--- a/clang/test/Driver/netbsd.c
+++ b/clang/test/Driver/netbsd.c
@@ -59,6 +59,9 @@
 // RUN: %clang --target=arm-unknown-netbsd7.0.0-eabi \
 // RUN: --sysroot=%S/Inputs/basic_netbsd_tree -### %s 2>&1 \
 // RUN: | FileCheck -check-prefix=ARM-7 %s
+// RUN: %clang --target=riscv64-unknown-netbsd \
+// RUN: -no-integrated-as --sysroot=%S/Inputs/basic_netbsd_tree -### %s 2>&1 \
+// RUN: | FileCheck -check-prefix=RISCV64 %s
 // RUN: %clang --target=sparc-unknown-netbsd \
 // RUN: -no-integrated-as --sysroot=%S/Inputs/basic_netbsd_tree -### %s 2>&1 \
 // RUN: | FileCheck -check-prefix=SPARC %s
@@ -99,6 +102,9 @@
 // RUN: %clang --target=arm-unknown-netbsd7.0.0-eabi -static \
 // RUN: --sysroot=%S/Inputs/basic_netbsd_tree -### %s 2>&1 \
 // RUN: | FileCheck -check-prefix=S-ARM-7 %s
+// RUN: %clang --target=riscv64-unknown-netbsd7.0.0 -static \
+// RUN: --sysroot=%S/Inputs/basic_netbsd_tree -### %s 2>&1 \
+// RUN: | FileCheck -check-prefix=S-RISCV64-7 %s
 // RUN: %clang --target=sparc-unknown-netbsd7.0.0 -static \
 // RUN: --sysroot=%S/Inputs/basic_netbsd_tree -### %s 2>&1 \
 // RUN: | FileCheck -check-prefix=S-SPARC-7 %s
@@ -264,6 +270,14 

[clang] [Driver] Hook up NetBSD/riscv64 support (PR #67256)

2023-09-24 Thread Brad Smith via cfe-commits

https://github.com/brad0 resolved 
https://github.com/llvm/llvm-project/pull/67256
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] Ensure NoTrapAfterNoreturn is false for the wasm backend (PR #65876)

2023-09-24 Thread Heejin Ahn via cfe-commits

aheejin wrote:

> > Can you point out what are the unsupported options here?
> 
> All of them, they are all options that translate to TargetOptions, and they 
> do nothing for a wasm target triple: ` --trap-unreachable=false 
> --xcoff-traceback-table=true --relax-elf-relocations=false --vec-extabi=true`

I don't think these options are the same case as this PR. These are just 
non-wasm options, so they mean nothing to wasm. Wasm simply doesn't do anything 
with respect to these options and wasm is not _overriding_ them. But with this 
PR, even if a user gives `--no-trap-after-noreturn` in the command line, they 
wouldn't know that their request was silently rejected/overridden.

To say it another way, the difference is, Wasm doesn't understand 
`--xcoff-traceback-table=true` and will do nothing with respect to it. But Wasm 
understands `--no-trap-after-noreturn` and will silently reject it after this 
PR.

> > I'm suggesting a warning only when an incompatible option is explicitly 
> > given in the command line.
> 
> I'm just trying to fix a bug and match how it currently works. If you want to 
> change the behaviour to give more warnings, and especially only when an 
> option is given on the command line, that's a more involved change that 
> belongs in a separate issue.

I have been kind of confused by your characterization of this as "bugfix", 
given that this does not change anything functionality-wise. I agree this PR 
can be an improvement being a failsafe mechanism if someone chooses to override 
the default option or explicitly chooses to override it in the command line. 
But I think, at least in the latter case, the user should be informed that even 
though they requested `--no-trap-after-noreturn`, we will do 
`--no-trap-after-noreturn=0` regardless.

So I'm not sure if I agree that this PR is a bugfix. I consider this as an 
improvement adding a failsafe mechanism.

> > If you grep with report_fatal_error in all target's ***TargetMachine.cpp, 
> > there are many instances of reporting that some options are not supported. 
> > (I'm not necessarily suggesting erroring out though)
> 
> Most of these are hard errors, where there's a real contradiction between two 
> requested features. Not so with TrapUnreachable and NoTrapAfterNoreturn, they 
> are more like hints or suggestions, and can be ignored without breaking 
> anything.

Didn't you provide a counterexample that `--no-trap-after-noreturn` produces 
invalid code? Then don't we have "a real contradiction"? What's your 
distinction criteria for "real contradiction" and "hints or suggestions"?


https://github.com/llvm/llvm-project/pull/65876
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Ensure NoTrapAfterNoreturn is false for the wasm backend (PR #65876)

2023-09-24 Thread Heejin Ahn via cfe-commits

aheejin wrote:

> > Can you point out what are the unsupported options here?
> 
> All of them, they are all options that translate to TargetOptions, and they 
> do nothing for a wasm target triple: ` --trap-unreachable=false 
> --xcoff-traceback-table=true --relax-elf-relocations=false --vec-extabi=true`

I don't think these options are the same case as this PR. These are just 
non-wasm options, so they mean nothing to wasm. Wasm simply doesn't do anything 
with respect to these options and wasm is not _overriding_ them. But with this 
PR, even if a user gives `--no-trap-after-noreturn` in the command line, they 
wouldn't know that their request was silently rejected/overridden.

To say it another way, the difference is, Wasm doesn't understand 
`--xcoff-traceback-table=true` and will do nothing with respect to it. But Wasm 
understands `--no-trap-after-noreturn` and will silently reject it after this 
PR.

> > I'm suggesting a warning only when an incompatible option is explicitly 
> > given in the command line.
> 
> I'm just trying to fix a bug and match how it currently works. If you want to 
> change the behaviour to give more warnings, and especially only when an 
> option is given on the command line, that's a more involved change that 
> belongs in a separate issue.

I have been kind of confused by your characterization of this as "bugfix", 
given that this does not change anything functionality-wise. I agree this PR 
can be an improvement being a failsafe mechanism if someone chooses to override 
the default option or explicitly chooses to override it in the command line. 
But I think, at least in the latter case, the user should be informed that even 
though they requested `--no-trap-after-noreturn`, we will do 
`--no-trap-after-noreturn=0` regardless.

So I'm not sure if I agree that this PR is a bugfix. I consider this as an 
improvement adding a failsafe mechanism.

> > If you grep with report_fatal_error in all target's ***TargetMachine.cpp, 
> > there are many instances of reporting that some options are not supported. 
> > (I'm not necessarily suggesting erroring out though)
> 
> Most of these are hard errors, where there's a real contradiction between two 
> requested features. Not so with TrapUnreachable and NoTrapAfterNoreturn, they 
> are more like hints or suggestions, and can be ignored without breaking 
> anything.

Didn't you provide a counterexample that `--no-trap-after-noreturn` produces 
invalid code? Then don't we have "a real contradiction"? What's your 
distinction criteria for "real contradiction" and "hints or suggestions"?


https://github.com/llvm/llvm-project/pull/65876
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format] Disable OuterScope lambda indentation behaviour for constructor initializers (PR #66755)

2023-09-24 Thread Björn Schäpers via cfe-commits

HazardyKnusperkeks wrote:

You should adapt the documentation.

https://github.com/llvm/llvm-project/pull/66755
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format] Fix a bug in aligning trailing comments (PR #67221)

2023-09-24 Thread Björn Schäpers via cfe-commits

https://github.com/HazardyKnusperkeks approved this pull request.


https://github.com/llvm/llvm-project/pull/67221
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format][NFC] Clean up alignTrailingComments() (PR #67218)

2023-09-24 Thread Björn Schäpers via cfe-commits

https://github.com/HazardyKnusperkeks approved this pull request.


https://github.com/llvm/llvm-project/pull/67218
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Driver] Hook up NetBSD/riscv64 support (PR #67256)

2023-09-24 Thread Brad Smith via cfe-commits

https://github.com/brad0 updated https://github.com/llvm/llvm-project/pull/67256

>From 2c5311b844f9ad08b0357ea2b79d0d8bee922296 Mon Sep 17 00:00:00 2001
From: Brad Smith 
Date: Sun, 24 Sep 2023 02:29:27 -0400
Subject: [PATCH] [Driver] Hook up NetBSD/riscv support

---
 clang/lib/Basic/Targets.cpp|  8 +++--
 clang/lib/Driver/ToolChains/NetBSD.cpp | 21 +++-
 clang/test/Driver/netbsd.c | 42 
 clang/test/Driver/netbsd.cpp   | 44 ++
 4 files changed, 112 insertions(+), 3 deletions(-)

diff --git a/clang/lib/Basic/Targets.cpp b/clang/lib/Basic/Targets.cpp
index 69576dbc458d9a1..35d1d0d2c45c530 100644
--- a/clang/lib/Basic/Targets.cpp
+++ b/clang/lib/Basic/Targets.cpp
@@ -429,11 +429,13 @@ std::unique_ptr AllocateTarget(const 
llvm::Triple &Triple,
 return std::make_unique(Triple, Opts);
 
   case llvm::Triple::riscv32:
-// TODO: add cases for NetBSD, RTEMS once tested.
 switch (os) {
 case llvm::Triple::FreeBSD:
   return std::make_unique>(Triple,
 Opts);
+case llvm::Triple::NetBSD:
+  return std::make_unique>(Triple,
+   Opts);
 case llvm::Triple::Linux:
   return std::make_unique>(Triple, 
Opts);
 default:
@@ -441,11 +443,13 @@ std::unique_ptr AllocateTarget(const 
llvm::Triple &Triple,
 }
 
   case llvm::Triple::riscv64:
-// TODO: add cases for NetBSD, RTEMS once tested.
 switch (os) {
 case llvm::Triple::FreeBSD:
   return std::make_unique>(Triple,
 Opts);
+case llvm::Triple::NetBSD:
+  return std::make_unique>(Triple,
+   Opts);
 case llvm::Triple::OpenBSD:
   return std::make_unique>(Triple,
 Opts);
diff --git a/clang/lib/Driver/ToolChains/NetBSD.cpp 
b/clang/lib/Driver/ToolChains/NetBSD.cpp
index 88be6ea0d5e7883..0bd15434898828a 100644
--- a/clang/lib/Driver/ToolChains/NetBSD.cpp
+++ b/clang/lib/Driver/ToolChains/NetBSD.cpp
@@ -216,6 +216,16 @@ void netbsd::Linker::ConstructJob(Compilation &C, const 
JobAction &JA,
 CmdArgs.push_back("elf64ppc");
 break;
 
+  case llvm::Triple::riscv32:
+CmdArgs.push_back("-m");
+CmdArgs.push_back("elf32lriscv");
+break;
+
+  case llvm::Triple::riscv64:
+CmdArgs.push_back("-m");
+CmdArgs.push_back("elf64lriscv");
+break;
+
   case llvm::Triple::sparc:
 CmdArgs.push_back("-m");
 CmdArgs.push_back("elf32_sparc");
@@ -230,6 +240,9 @@ void netbsd::Linker::ConstructJob(Compilation &C, const 
JobAction &JA,
 break;
   }
 
+  if (Triple.isRISCV())
+CmdArgs.push_back("-X");
+
   if (Output.isFilename()) {
 CmdArgs.push_back("-o");
 CmdArgs.push_back(Output.getFilename());
@@ -282,6 +295,8 @@ void netbsd::Linker::ConstructJob(Compilation &C, const 
JobAction &JA,
   case llvm::Triple::ppc:
   case llvm::Triple::ppc64:
   case llvm::Triple::ppc64le:
+  case llvm::Triple::riscv32:
+  case llvm::Triple::riscv64:
   case llvm::Triple::sparc:
   case llvm::Triple::sparcv9:
   case llvm::Triple::x86:
@@ -418,6 +433,8 @@ ToolChain::CXXStdlibType NetBSD::GetDefaultCXXStdlibType() 
const {
   case llvm::Triple::ppc:
   case llvm::Triple::ppc64:
   case llvm::Triple::ppc64le:
+  case llvm::Triple::riscv32:
+  case llvm::Triple::riscv64:
   case llvm::Triple::sparc:
   case llvm::Triple::sparcv9:
   case llvm::Triple::x86:
@@ -539,7 +556,9 @@ void NetBSD::addClangTargetOptions(const ArgList 
&DriverArgs,
   getTriple().getArch() == llvm::Triple::aarch64 ||
   getTriple().getArch() == llvm::Triple::aarch64_be ||
   getTriple().getArch() == llvm::Triple::arm ||
-  getTriple().getArch() == llvm::Triple::armeb;
+  getTriple().getArch() == llvm::Triple::armeb ||
+  getTriple().getArch() == llvm::Triple::riscv32 ||
+  getTriple().getArch() == llvm::Triple::riscv64;
 
   if (!DriverArgs.hasFlag(options::OPT_fuse_init_array,
   options::OPT_fno_use_init_array, 
UseInitArrayDefault))
diff --git a/clang/test/Driver/netbsd.c b/clang/test/Driver/netbsd.c
index 67c048c6e91515f..760cba4ac4e0c9f 100644
--- a/clang/test/Driver/netbsd.c
+++ b/clang/test/Driver/netbsd.c
@@ -59,6 +59,12 @@
 // RUN: %clang --target=arm-unknown-netbsd7.0.0-eabi \
 // RUN: --sysroot=%S/Inputs/basic_netbsd_tree -### %s 2>&1 \
 // RUN: | FileCheck -check-prefix=ARM-7 %s
+// RUN: %clang --target=riscv32-unknown-netbsd \
+// RUN: -no-integrated-as --sysroot=%S/Inputs/basic_netbsd_tree -### %s 2>&1 \
+// RUN: | FileCheck -check-prefix=RISCV32 %s
+// RUN: %clang --target=riscv64-unknown-netbsd \
+// RUN: -no-integrated-as --sysroot=%S/Inputs/basic_netbsd_tree -### %s 2>&1 \
+// RUN: | FileCheck -check-prefix=RISCV64 %s
 // RUN: %clang --targ

[clang] [Driver] Hook up NetBSD/riscv support (PR #67256)

2023-09-24 Thread Brad Smith via cfe-commits

https://github.com/brad0 edited https://github.com/llvm/llvm-project/pull/67256
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Driver] Hook up NetBSD/riscv support (PR #67256)

2023-09-24 Thread Brad Smith via cfe-commits

https://github.com/brad0 closed https://github.com/llvm/llvm-project/pull/67256
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] 62ffbe0 - [Driver] Hook up NetBSD/riscv support (#67256)

2023-09-24 Thread via cfe-commits

Author: Brad Smith
Date: 2023-09-24T16:29:46-04:00
New Revision: 62ffbe0dcca9f394f9dbfda160e0f0a1e96948ae

URL: 
https://github.com/llvm/llvm-project/commit/62ffbe0dcca9f394f9dbfda160e0f0a1e96948ae
DIFF: 
https://github.com/llvm/llvm-project/commit/62ffbe0dcca9f394f9dbfda160e0f0a1e96948ae.diff

LOG: [Driver] Hook up NetBSD/riscv support (#67256)

Added: 


Modified: 
clang/lib/Basic/Targets.cpp
clang/lib/Driver/ToolChains/NetBSD.cpp
clang/test/Driver/netbsd.c
clang/test/Driver/netbsd.cpp

Removed: 




diff  --git a/clang/lib/Basic/Targets.cpp b/clang/lib/Basic/Targets.cpp
index 69576dbc458d9a1..35d1d0d2c45c530 100644
--- a/clang/lib/Basic/Targets.cpp
+++ b/clang/lib/Basic/Targets.cpp
@@ -429,11 +429,13 @@ std::unique_ptr AllocateTarget(const 
llvm::Triple &Triple,
 return std::make_unique(Triple, Opts);
 
   case llvm::Triple::riscv32:
-// TODO: add cases for NetBSD, RTEMS once tested.
 switch (os) {
 case llvm::Triple::FreeBSD:
   return std::make_unique>(Triple,
 Opts);
+case llvm::Triple::NetBSD:
+  return std::make_unique>(Triple,
+   Opts);
 case llvm::Triple::Linux:
   return std::make_unique>(Triple, 
Opts);
 default:
@@ -441,11 +443,13 @@ std::unique_ptr AllocateTarget(const 
llvm::Triple &Triple,
 }
 
   case llvm::Triple::riscv64:
-// TODO: add cases for NetBSD, RTEMS once tested.
 switch (os) {
 case llvm::Triple::FreeBSD:
   return std::make_unique>(Triple,
 Opts);
+case llvm::Triple::NetBSD:
+  return std::make_unique>(Triple,
+   Opts);
 case llvm::Triple::OpenBSD:
   return std::make_unique>(Triple,
 Opts);

diff  --git a/clang/lib/Driver/ToolChains/NetBSD.cpp 
b/clang/lib/Driver/ToolChains/NetBSD.cpp
index 88be6ea0d5e7883..0bd15434898828a 100644
--- a/clang/lib/Driver/ToolChains/NetBSD.cpp
+++ b/clang/lib/Driver/ToolChains/NetBSD.cpp
@@ -216,6 +216,16 @@ void netbsd::Linker::ConstructJob(Compilation &C, const 
JobAction &JA,
 CmdArgs.push_back("elf64ppc");
 break;
 
+  case llvm::Triple::riscv32:
+CmdArgs.push_back("-m");
+CmdArgs.push_back("elf32lriscv");
+break;
+
+  case llvm::Triple::riscv64:
+CmdArgs.push_back("-m");
+CmdArgs.push_back("elf64lriscv");
+break;
+
   case llvm::Triple::sparc:
 CmdArgs.push_back("-m");
 CmdArgs.push_back("elf32_sparc");
@@ -230,6 +240,9 @@ void netbsd::Linker::ConstructJob(Compilation &C, const 
JobAction &JA,
 break;
   }
 
+  if (Triple.isRISCV())
+CmdArgs.push_back("-X");
+
   if (Output.isFilename()) {
 CmdArgs.push_back("-o");
 CmdArgs.push_back(Output.getFilename());
@@ -282,6 +295,8 @@ void netbsd::Linker::ConstructJob(Compilation &C, const 
JobAction &JA,
   case llvm::Triple::ppc:
   case llvm::Triple::ppc64:
   case llvm::Triple::ppc64le:
+  case llvm::Triple::riscv32:
+  case llvm::Triple::riscv64:
   case llvm::Triple::sparc:
   case llvm::Triple::sparcv9:
   case llvm::Triple::x86:
@@ -418,6 +433,8 @@ ToolChain::CXXStdlibType NetBSD::GetDefaultCXXStdlibType() 
const {
   case llvm::Triple::ppc:
   case llvm::Triple::ppc64:
   case llvm::Triple::ppc64le:
+  case llvm::Triple::riscv32:
+  case llvm::Triple::riscv64:
   case llvm::Triple::sparc:
   case llvm::Triple::sparcv9:
   case llvm::Triple::x86:
@@ -539,7 +556,9 @@ void NetBSD::addClangTargetOptions(const ArgList 
&DriverArgs,
   getTriple().getArch() == llvm::Triple::aarch64 ||
   getTriple().getArch() == llvm::Triple::aarch64_be ||
   getTriple().getArch() == llvm::Triple::arm ||
-  getTriple().getArch() == llvm::Triple::armeb;
+  getTriple().getArch() == llvm::Triple::armeb ||
+  getTriple().getArch() == llvm::Triple::riscv32 ||
+  getTriple().getArch() == llvm::Triple::riscv64;
 
   if (!DriverArgs.hasFlag(options::OPT_fuse_init_array,
   options::OPT_fno_use_init_array, 
UseInitArrayDefault))

diff  --git a/clang/test/Driver/netbsd.c b/clang/test/Driver/netbsd.c
index 67c048c6e91515f..760cba4ac4e0c9f 100644
--- a/clang/test/Driver/netbsd.c
+++ b/clang/test/Driver/netbsd.c
@@ -59,6 +59,12 @@
 // RUN: %clang --target=arm-unknown-netbsd7.0.0-eabi \
 // RUN: --sysroot=%S/Inputs/basic_netbsd_tree -### %s 2>&1 \
 // RUN: | FileCheck -check-prefix=ARM-7 %s
+// RUN: %clang --target=riscv32-unknown-netbsd \
+// RUN: -no-integrated-as --sysroot=%S/Inputs/basic_netbsd_tree -### %s 2>&1 \
+// RUN: | FileCheck -check-prefix=RISCV32 %s
+// RUN: %clang --target=riscv64-unknown-netbsd \
+// RUN: -no-integrated-as --sysroot=%S/Inputs/basic_netbsd_tree -### %s 2>&1 \
+// RUN: | FileCheck -che

[clang] [Driver] Hook up Haiku ARM support (PR #67222)

2023-09-24 Thread Brad Smith via cfe-commits

https://github.com/brad0 updated https://github.com/llvm/llvm-project/pull/67222

>From b9ad72d918c663f7b5ee782979ce5704b23aa370 Mon Sep 17 00:00:00 2001
From: Brad Smith 
Date: Sat, 23 Sep 2023 03:22:18 -0400
Subject: [PATCH] [Driver] Hook up Haiku ARM support

---
 clang/lib/Basic/Targets.cpp   | 2 ++
 clang/lib/Basic/Targets/ARM.cpp   | 3 ++-
 clang/lib/Driver/ToolChains/Arch/ARM.cpp  | 1 +
 clang/test/Driver/arm-abi.c   | 2 ++
 clang/test/Driver/haiku.c | 5 +
 llvm/lib/TargetParser/ARMTargetParser.cpp | 6 +-
 6 files changed, 17 insertions(+), 2 deletions(-)

diff --git a/clang/lib/Basic/Targets.cpp b/clang/lib/Basic/Targets.cpp
index 35d1d0d2c45c530..9e3543c6d5ff81c 100644
--- a/clang/lib/Basic/Targets.cpp
+++ b/clang/lib/Basic/Targets.cpp
@@ -219,6 +219,8 @@ std::unique_ptr AllocateTarget(const 
llvm::Triple &Triple,
   return std::make_unique>(Triple, 
Opts);
 case llvm::Triple::RTEMS:
   return std::make_unique>(Triple, Opts);
+case llvm::Triple::Haiku:
+  return std::make_unique>(Triple, Opts);
 case llvm::Triple::NaCl:
   return std::make_unique>(Triple, Opts);
 case llvm::Triple::Win32:
diff --git a/clang/lib/Basic/Targets/ARM.cpp b/clang/lib/Basic/Targets/ARM.cpp
index 06e99e67c875584..1e809283748b66c 100644
--- a/clang/lib/Basic/Targets/ARM.cpp
+++ b/clang/lib/Basic/Targets/ARM.cpp
@@ -257,6 +257,7 @@ ARMTargetInfo::ARMTargetInfo(const llvm::Triple &Triple,
   bool IsFreeBSD = Triple.isOSFreeBSD();
   bool IsOpenBSD = Triple.isOSOpenBSD();
   bool IsNetBSD = Triple.isOSNetBSD();
+  bool IsHaiku = Triple.isOSHaiku();
 
   // FIXME: the isOSBinFormatMachO is a workaround for identifying a 
Darwin-like
   // environment where size_t is `unsigned long` rather than `unsigned int`
@@ -323,7 +324,7 @@ ARMTargetInfo::ARMTargetInfo(const llvm::Triple &Triple,
 default:
   if (IsNetBSD)
 setABI("apcs-gnu");
-  else if (IsFreeBSD || IsOpenBSD)
+  else if (IsFreeBSD || IsOpenBSD || IsHaiku)
 setABI("aapcs-linux");
   else
 setABI("aapcs");
diff --git a/clang/lib/Driver/ToolChains/Arch/ARM.cpp 
b/clang/lib/Driver/ToolChains/Arch/ARM.cpp
index bb66db5feae8c3b..8e1cff0b443eeeb 100644
--- a/clang/lib/Driver/ToolChains/Arch/ARM.cpp
+++ b/clang/lib/Driver/ToolChains/Arch/ARM.cpp
@@ -404,6 +404,7 @@ arm::FloatABI arm::getDefaultFloatABI(const llvm::Triple 
&Triple) {
 }
 break;
 
+  case llvm::Triple::Haiku:
   case llvm::Triple::OpenBSD:
 return FloatABI::SoftFP;
 
diff --git a/clang/test/Driver/arm-abi.c b/clang/test/Driver/arm-abi.c
index 7bf5977992f65a2..139456cf98e1478 100644
--- a/clang/test/Driver/arm-abi.c
+++ b/clang/test/Driver/arm-abi.c
@@ -33,6 +33,8 @@
 // RUN:   | FileCheck -check-prefix=CHECK-AAPCS-LINUX %s
 // RUN: %clang -target arm--openbsd- %s -### -o %t.o 2>&1 \
 // RUN:   | FileCheck -check-prefix=CHECK-AAPCS-LINUX %s
+// RUN: %clang -target arm--haiku- %s -### -o %t.o 2>&1 \
+// RUN:   | FileCheck -check-prefix=CHECK-AAPCS-LINUX %s
 
 // Otherwise, ABI is selected based on environment
 // RUN: %clang -target arm---android %s -### -o %t.o 2>&1 \
diff --git a/clang/test/Driver/haiku.c b/clang/test/Driver/haiku.c
index 021ab522be06e5c..3888c6732923228 100644
--- a/clang/test/Driver/haiku.c
+++ b/clang/test/Driver/haiku.c
@@ -65,3 +65,8 @@
 // CHECK-X86_64-SHARED-SAME: "-isysroot" "[[SYSROOT:[^"]+]]"
 // CHECK-X86_64-SHARED: "{{.*}}ld{{(.exe)?}}"
 // CHECK-X86_64-SHARED-NOT: "[[SYSROOT]]/boot/system/develop/lib/start_dyn.o"
+
+// Check default ARM CPU, ARMv6
+// RUN: %clang -### %s 2>&1 --target=arm-unknown-haiku \
+// RUN:   | FileCheck --check-prefix=CHECK-ARM-CPU %s
+// CHECK-ARM-CPU: "-target-cpu" "arm1176jzf-s"
diff --git a/llvm/lib/TargetParser/ARMTargetParser.cpp 
b/llvm/lib/TargetParser/ARMTargetParser.cpp
index c84928eeb07b140..20225232b3cccb7 100644
--- a/llvm/lib/TargetParser/ARMTargetParser.cpp
+++ b/llvm/lib/TargetParser/ARMTargetParser.cpp
@@ -526,7 +526,8 @@ StringRef ARM::computeDefaultTargetABI(const Triple &TT, 
StringRef CPU) {
   default:
 if (TT.isOSNetBSD())
   return "apcs-gnu";
-if (TT.isOSFreeBSD() || TT.isOSOpenBSD() || TT.isOHOSFamily())
+if (TT.isOSFreeBSD() || TT.isOSOpenBSD() || TT.isOSHaiku() ||
+TT.isOHOSFamily())
   return "aapcs-linux";
 return "aapcs";
   }
@@ -542,6 +543,7 @@ StringRef ARM::getARMCPUForArch(const llvm::Triple &Triple, 
StringRef MArch) {
   case llvm::Triple::FreeBSD:
   case llvm::Triple::NetBSD:
   case llvm::Triple::OpenBSD:
+  case llvm::Triple::Haiku:
 if (!MArch.empty() && MArch == "v6")
   return "arm1176jzf-s";
 if (!MArch.empty() && MArch == "v7")
@@ -574,6 +576,8 @@ StringRef ARM::getARMCPUForArch(const llvm::Triple &Triple, 
StringRef MArch) {
   // If no specific architecture version is requested, return the minimum CPU
   // required by the OS and environment.
   switch (Triple.getOS()) {
+  case llvm::Triple::Haiku:
+return "arm1176jzf-s";

[clang] [NFC][Clang][CodeGen] Improve performance for vtable metadata generation (PR #67066)

2023-09-24 Thread Fangrui Song via cfe-commits

https://github.com/MaskRay approved this pull request.


https://github.com/llvm/llvm-project/pull/67066
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [RISCV] Eliminate dead li after emitting VSETVLIs (PR #65934)

2023-09-24 Thread Yingwei Zheng via cfe-commits

https://github.com/dtcxzyw updated 
https://github.com/llvm/llvm-project/pull/65934

>From 2fe5756dd4d49580d3a23b0ff1b72535f725915e Mon Sep 17 00:00:00 2001
From: Yingwei Zheng 
Date: Mon, 11 Sep 2023 15:51:46 +0800
Subject: [PATCH 1/4] [RISCV] Eliminate dead li after emitting VSETVLIs

---
 llvm/lib/Target/RISCV/RISCVInsertVSETVLI.cpp  | 22 ++---
 .../RISCV/rvv/fixed-vectors-masked-gather.ll  | 24 ---
 2 files changed, 19 insertions(+), 27 deletions(-)

diff --git a/llvm/lib/Target/RISCV/RISCVInsertVSETVLI.cpp 
b/llvm/lib/Target/RISCV/RISCVInsertVSETVLI.cpp
index b42ad269c18de6f..918c96beb29afca 100644
--- a/llvm/lib/Target/RISCV/RISCVInsertVSETVLI.cpp
+++ b/llvm/lib/Target/RISCV/RISCVInsertVSETVLI.cpp
@@ -757,7 +757,8 @@ class RISCVInsertVSETVLI : public MachineFunctionPass {
   bool computeVLVTYPEChanges(const MachineBasicBlock &MBB,
  VSETVLIInfo &Info) const;
   void computeIncomingVLVTYPE(const MachineBasicBlock &MBB);
-  void emitVSETVLIs(MachineBasicBlock &MBB);
+  void emitVSETVLIs(MachineBasicBlock &MBB,
+SmallVectorImpl &DeadVLInstrs);
   void doLocalPostpass(MachineBasicBlock &MBB);
   void doPRE(MachineBasicBlock &MBB);
   void insertReadVL(MachineBasicBlock &MBB);
@@ -1216,7 +1217,8 @@ bool RISCVInsertVSETVLI::needVSETVLIPHI(const VSETVLIInfo 
&Require,
   return false;
 }
 
-void RISCVInsertVSETVLI::emitVSETVLIs(MachineBasicBlock &MBB) {
+void RISCVInsertVSETVLI::emitVSETVLIs(
+MachineBasicBlock &MBB, SmallVectorImpl &DeadVLInstrs) {
   VSETVLIInfo CurInfo = BlockInfo[MBB.getNumber()].Pred;
   // Track whether the prefix of the block we've scanned is transparent
   // (meaning has not yet changed the abstract state).
@@ -1255,6 +1257,13 @@ void RISCVInsertVSETVLI::emitVSETVLIs(MachineBasicBlock 
&MBB) {
 MachineOperand &VLOp = MI.getOperand(getVLOpNum(MI));
 if (VLOp.isReg()) {
   // Erase the AVL operand from the instruction.
+  if (MachineInstr *MI = MRI->getVRegDef(VLOp.getReg());
+  MI && MI->getOpcode() == RISCV::ADDI &&
+  MI->getOperand(1).isReg() && MI->getOperand(2).isImm() &&
+  MI->getOperand(1).getReg() == RISCV::X0 &&
+  MI->getOperand(2).getImm() != 0)
+DeadVLInstrs.push_back(MI);
+
   VLOp.setReg(RISCV::NoRegister);
   VLOp.setIsKill(false);
 }
@@ -1580,8 +1589,9 @@ bool 
RISCVInsertVSETVLI::runOnMachineFunction(MachineFunction &MF) {
   // Phase 2 information to avoid adding vsetvlis before the first vector
   // instruction in the block if the VL/VTYPE is satisfied by its
   // predecessors.
+  SmallVector DeadVLInstrs;
   for (MachineBasicBlock &MBB : MF)
-emitVSETVLIs(MBB);
+emitVSETVLIs(MBB, DeadVLInstrs);
 
   // Now that all vsetvlis are explicit, go through and do block local
   // DSE and peephole based demanded fields based transforms.  Note that
@@ -1592,6 +1602,12 @@ bool 
RISCVInsertVSETVLI::runOnMachineFunction(MachineFunction &MF) {
   for (MachineBasicBlock &MBB : MF)
 doLocalPostpass(MBB);
 
+  // Remove dead LI instructions that set VL.
+  for (MachineInstr *MI : DeadVLInstrs) {
+if (MRI->use_nodbg_empty(MI->getOperand(0).getReg()))
+  MI->eraseFromParent();
+  }
+
   // Once we're fully done rewriting all the instructions, do a final pass
   // through to check for VSETVLIs which write to an unused destination.
   // For the non X0, X0 variant, we can replace the destination register
diff --git a/llvm/test/CodeGen/RISCV/rvv/fixed-vectors-masked-gather.ll 
b/llvm/test/CodeGen/RISCV/rvv/fixed-vectors-masked-gather.ll
index f7352b4659e5a9b..0d306775528ed86 100644
--- a/llvm/test/CodeGen/RISCV/rvv/fixed-vectors-masked-gather.ll
+++ b/llvm/test/CodeGen/RISCV/rvv/fixed-vectors-masked-gather.ll
@@ -12393,7 +12393,6 @@ define <32 x i8> @mgather_baseidx_v32i8(ptr %base, <32 
x i8> %idxs, <32 x i1> %m
 ; RV64ZVE32F-NEXT:vmv.x.s a2, v12
 ; RV64ZVE32F-NEXT:add a2, a0, a2
 ; RV64ZVE32F-NEXT:lbu a2, 0(a2)
-; RV64ZVE32F-NEXT:li a3, 32
 ; RV64ZVE32F-NEXT:vmv.s.x v12, a2
 ; RV64ZVE32F-NEXT:vsetivli zero, 2, e8, m2, tu, ma
 ; RV64ZVE32F-NEXT:vslideup.vi v10, v12, 1
@@ -12431,7 +12430,6 @@ define <32 x i8> @mgather_baseidx_v32i8(ptr %base, <32 
x i8> %idxs, <32 x i1> %m
 ; RV64ZVE32F-NEXT:vmv.x.s a2, v14
 ; RV64ZVE32F-NEXT:add a2, a0, a2
 ; RV64ZVE32F-NEXT:lbu a2, 0(a2)
-; RV64ZVE32F-NEXT:li a3, 32
 ; RV64ZVE32F-NEXT:vmv.s.x v14, a2
 ; RV64ZVE32F-NEXT:vsetivli zero, 6, e8, m2, tu, ma
 ; RV64ZVE32F-NEXT:vslideup.vi v10, v14, 5
@@ -12455,7 +12453,6 @@ define <32 x i8> @mgather_baseidx_v32i8(ptr %base, <32 
x i8> %idxs, <32 x i1> %m
 ; RV64ZVE32F-NEXT:vmv.x.s a2, v13
 ; RV64ZVE32F-NEXT:add a2, a0, a2
 ; RV64ZVE32F-NEXT:lbu a2, 0(a2)
-; RV64ZVE32F-NEXT:li a3, 32
 ; RV64ZVE32F-NEXT:vmv.s.x v14, a2
 ; RV64ZVE32F-NEXT:vsetivli zero, 10, e8, m2, tu, ma
 ; RV64ZVE32F-NEXT:vslideup.vi v10, v

[clang] [RISCV] Eliminate dead li after emitting VSETVLIs (PR #65934)

2023-09-24 Thread Yingwei Zheng via cfe-commits

https://github.com/dtcxzyw updated 
https://github.com/llvm/llvm-project/pull/65934

>From 2fe5756dd4d49580d3a23b0ff1b72535f725915e Mon Sep 17 00:00:00 2001
From: Yingwei Zheng 
Date: Mon, 11 Sep 2023 15:51:46 +0800
Subject: [PATCH 1/4] [RISCV] Eliminate dead li after emitting VSETVLIs

---
 llvm/lib/Target/RISCV/RISCVInsertVSETVLI.cpp  | 22 ++---
 .../RISCV/rvv/fixed-vectors-masked-gather.ll  | 24 ---
 2 files changed, 19 insertions(+), 27 deletions(-)

diff --git a/llvm/lib/Target/RISCV/RISCVInsertVSETVLI.cpp 
b/llvm/lib/Target/RISCV/RISCVInsertVSETVLI.cpp
index b42ad269c18de6f..918c96beb29afca 100644
--- a/llvm/lib/Target/RISCV/RISCVInsertVSETVLI.cpp
+++ b/llvm/lib/Target/RISCV/RISCVInsertVSETVLI.cpp
@@ -757,7 +757,8 @@ class RISCVInsertVSETVLI : public MachineFunctionPass {
   bool computeVLVTYPEChanges(const MachineBasicBlock &MBB,
  VSETVLIInfo &Info) const;
   void computeIncomingVLVTYPE(const MachineBasicBlock &MBB);
-  void emitVSETVLIs(MachineBasicBlock &MBB);
+  void emitVSETVLIs(MachineBasicBlock &MBB,
+SmallVectorImpl &DeadVLInstrs);
   void doLocalPostpass(MachineBasicBlock &MBB);
   void doPRE(MachineBasicBlock &MBB);
   void insertReadVL(MachineBasicBlock &MBB);
@@ -1216,7 +1217,8 @@ bool RISCVInsertVSETVLI::needVSETVLIPHI(const VSETVLIInfo 
&Require,
   return false;
 }
 
-void RISCVInsertVSETVLI::emitVSETVLIs(MachineBasicBlock &MBB) {
+void RISCVInsertVSETVLI::emitVSETVLIs(
+MachineBasicBlock &MBB, SmallVectorImpl &DeadVLInstrs) {
   VSETVLIInfo CurInfo = BlockInfo[MBB.getNumber()].Pred;
   // Track whether the prefix of the block we've scanned is transparent
   // (meaning has not yet changed the abstract state).
@@ -1255,6 +1257,13 @@ void RISCVInsertVSETVLI::emitVSETVLIs(MachineBasicBlock 
&MBB) {
 MachineOperand &VLOp = MI.getOperand(getVLOpNum(MI));
 if (VLOp.isReg()) {
   // Erase the AVL operand from the instruction.
+  if (MachineInstr *MI = MRI->getVRegDef(VLOp.getReg());
+  MI && MI->getOpcode() == RISCV::ADDI &&
+  MI->getOperand(1).isReg() && MI->getOperand(2).isImm() &&
+  MI->getOperand(1).getReg() == RISCV::X0 &&
+  MI->getOperand(2).getImm() != 0)
+DeadVLInstrs.push_back(MI);
+
   VLOp.setReg(RISCV::NoRegister);
   VLOp.setIsKill(false);
 }
@@ -1580,8 +1589,9 @@ bool 
RISCVInsertVSETVLI::runOnMachineFunction(MachineFunction &MF) {
   // Phase 2 information to avoid adding vsetvlis before the first vector
   // instruction in the block if the VL/VTYPE is satisfied by its
   // predecessors.
+  SmallVector DeadVLInstrs;
   for (MachineBasicBlock &MBB : MF)
-emitVSETVLIs(MBB);
+emitVSETVLIs(MBB, DeadVLInstrs);
 
   // Now that all vsetvlis are explicit, go through and do block local
   // DSE and peephole based demanded fields based transforms.  Note that
@@ -1592,6 +1602,12 @@ bool 
RISCVInsertVSETVLI::runOnMachineFunction(MachineFunction &MF) {
   for (MachineBasicBlock &MBB : MF)
 doLocalPostpass(MBB);
 
+  // Remove dead LI instructions that set VL.
+  for (MachineInstr *MI : DeadVLInstrs) {
+if (MRI->use_nodbg_empty(MI->getOperand(0).getReg()))
+  MI->eraseFromParent();
+  }
+
   // Once we're fully done rewriting all the instructions, do a final pass
   // through to check for VSETVLIs which write to an unused destination.
   // For the non X0, X0 variant, we can replace the destination register
diff --git a/llvm/test/CodeGen/RISCV/rvv/fixed-vectors-masked-gather.ll 
b/llvm/test/CodeGen/RISCV/rvv/fixed-vectors-masked-gather.ll
index f7352b4659e5a9b..0d306775528ed86 100644
--- a/llvm/test/CodeGen/RISCV/rvv/fixed-vectors-masked-gather.ll
+++ b/llvm/test/CodeGen/RISCV/rvv/fixed-vectors-masked-gather.ll
@@ -12393,7 +12393,6 @@ define <32 x i8> @mgather_baseidx_v32i8(ptr %base, <32 
x i8> %idxs, <32 x i1> %m
 ; RV64ZVE32F-NEXT:vmv.x.s a2, v12
 ; RV64ZVE32F-NEXT:add a2, a0, a2
 ; RV64ZVE32F-NEXT:lbu a2, 0(a2)
-; RV64ZVE32F-NEXT:li a3, 32
 ; RV64ZVE32F-NEXT:vmv.s.x v12, a2
 ; RV64ZVE32F-NEXT:vsetivli zero, 2, e8, m2, tu, ma
 ; RV64ZVE32F-NEXT:vslideup.vi v10, v12, 1
@@ -12431,7 +12430,6 @@ define <32 x i8> @mgather_baseidx_v32i8(ptr %base, <32 
x i8> %idxs, <32 x i1> %m
 ; RV64ZVE32F-NEXT:vmv.x.s a2, v14
 ; RV64ZVE32F-NEXT:add a2, a0, a2
 ; RV64ZVE32F-NEXT:lbu a2, 0(a2)
-; RV64ZVE32F-NEXT:li a3, 32
 ; RV64ZVE32F-NEXT:vmv.s.x v14, a2
 ; RV64ZVE32F-NEXT:vsetivli zero, 6, e8, m2, tu, ma
 ; RV64ZVE32F-NEXT:vslideup.vi v10, v14, 5
@@ -12455,7 +12453,6 @@ define <32 x i8> @mgather_baseidx_v32i8(ptr %base, <32 
x i8> %idxs, <32 x i1> %m
 ; RV64ZVE32F-NEXT:vmv.x.s a2, v13
 ; RV64ZVE32F-NEXT:add a2, a0, a2
 ; RV64ZVE32F-NEXT:lbu a2, 0(a2)
-; RV64ZVE32F-NEXT:li a3, 32
 ; RV64ZVE32F-NEXT:vmv.s.x v14, a2
 ; RV64ZVE32F-NEXT:vsetivli zero, 10, e8, m2, tu, ma
 ; RV64ZVE32F-NEXT:vslideup.vi v10, v

[clang-tools-extra] [RISCV] Eliminate dead li after emitting VSETVLIs (PR #65934)

2023-09-24 Thread Yingwei Zheng via cfe-commits

https://github.com/dtcxzyw resolved 
https://github.com/llvm/llvm-project/pull/65934
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [RISCV] Eliminate dead li after emitting VSETVLIs (PR #65934)

2023-09-24 Thread Yingwei Zheng via cfe-commits

https://github.com/dtcxzyw resolved 
https://github.com/llvm/llvm-project/pull/65934
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [RISCV] Eliminate dead li after emitting VSETVLIs (PR #65934)

2023-09-24 Thread Yingwei Zheng via cfe-commits


@@ -757,7 +757,8 @@ class RISCVInsertVSETVLI : public MachineFunctionPass {
   bool computeVLVTYPEChanges(const MachineBasicBlock &MBB,
  VSETVLIInfo &Info) const;
   void computeIncomingVLVTYPE(const MachineBasicBlock &MBB);
-  void emitVSETVLIs(MachineBasicBlock &MBB);
+  void emitVSETVLIs(MachineBasicBlock &MBB,
+SmallVectorImpl &PossiblyDeadVLInstrs);

dtcxzyw wrote:

I don't think it is a typo.


https://github.com/llvm/llvm-project/pull/65934
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [RISCV] Eliminate dead li after emitting VSETVLIs (PR #65934)

2023-09-24 Thread Yingwei Zheng via cfe-commits

https://github.com/dtcxzyw resolved 
https://github.com/llvm/llvm-project/pull/65934
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [RISCV] Eliminate dead li after emitting VSETVLIs (PR #65934)

2023-09-24 Thread Yingwei Zheng via cfe-commits


@@ -757,7 +757,8 @@ class RISCVInsertVSETVLI : public MachineFunctionPass {
   bool computeVLVTYPEChanges(const MachineBasicBlock &MBB,
  VSETVLIInfo &Info) const;
   void computeIncomingVLVTYPE(const MachineBasicBlock &MBB);
-  void emitVSETVLIs(MachineBasicBlock &MBB);
+  void emitVSETVLIs(MachineBasicBlock &MBB,
+SmallVectorImpl &PossiblyDeadVLInstrs);

dtcxzyw wrote:

I don't think it is a typo.


https://github.com/llvm/llvm-project/pull/65934
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


  1   2   >