[clang] d082f1f - [clang][bytecode] Only booleans can be inverted
Timm Bäder via cfe-commits
cfe-commits at lists.llvm.org
Sun Aug 18 07:02:46 PDT 2024
Author: Timm Bäder
Date: 2024-08-18T16:02:32+02:00
New Revision: d082f1f37d8cb7a0c6875537ba873a631b154d53
URL: https://github.com/llvm/llvm-project/commit/d082f1f37d8cb7a0c6875537ba873a631b154d53
DIFF: https://github.com/llvm/llvm-project/commit/d082f1f37d8cb7a0c6875537ba873a631b154d53.diff
LOG: [clang][bytecode] Only booleans can be inverted
No need to have the Inv() function be templated.
Added:
Modified:
clang/lib/AST/ByteCode/Boolean.h
clang/lib/AST/ByteCode/Compiler.cpp
clang/lib/AST/ByteCode/Interp.h
clang/lib/AST/ByteCode/Opcodes.td
Removed:
################################################################################
diff --git a/clang/lib/AST/ByteCode/Boolean.h b/clang/lib/AST/ByteCode/Boolean.h
index 0a2e0fe332110e..f1914ddb9970dc 100644
--- a/clang/lib/AST/ByteCode/Boolean.h
+++ b/clang/lib/AST/ByteCode/Boolean.h
@@ -44,6 +44,7 @@ class Boolean final {
Boolean operator-() const { return Boolean(V); }
Boolean operator-(const Boolean &Other) const { return Boolean(V - Other.V); }
Boolean operator~() const { return Boolean(true); }
+ Boolean operator!() const { return Boolean(!V); }
template <typename Ty, typename = std::enable_if_t<std::is_integral_v<Ty>>>
explicit operator Ty() const {
diff --git a/clang/lib/AST/ByteCode/Compiler.cpp b/clang/lib/AST/ByteCode/Compiler.cpp
index c3ecaa40b86f2a..53144a50ccf4a9 100644
--- a/clang/lib/AST/ByteCode/Compiler.cpp
+++ b/clang/lib/AST/ByteCode/Compiler.cpp
@@ -5112,7 +5112,7 @@ bool Compiler<Emitter>::VisitUnaryOperator(const UnaryOperator *E) {
if (!this->visitBool(SubExpr))
return false;
- if (!this->emitInvBool(E))
+ if (!this->emitInv(E))
return false;
if (PrimType ET = classifyPrim(E->getType()); ET != PT_Bool)
@@ -5231,7 +5231,7 @@ bool Compiler<Emitter>::VisitComplexUnaryOperator(const UnaryOperator *E) {
return false;
if (!this->emitComplexBoolCast(SubExpr))
return false;
- if (!this->emitInvBool(E))
+ if (!this->emitInv(E))
return false;
if (PrimType ET = classifyPrim(E->getType()); ET != PT_Bool)
return this->emitCast(PT_Bool, ET, E);
diff --git a/clang/lib/AST/ByteCode/Interp.h b/clang/lib/AST/ByteCode/Interp.h
index 6cb7a42482ab25..5aa3d24ebc7582 100644
--- a/clang/lib/AST/ByteCode/Interp.h
+++ b/clang/lib/AST/ByteCode/Interp.h
@@ -656,15 +656,9 @@ inline bool Divf(InterpState &S, CodePtr OpPC, llvm::RoundingMode RM) {
// Inv
//===----------------------------------------------------------------------===//
-template <PrimType Name, class T = typename PrimConv<Name>::T>
-bool Inv(InterpState &S, CodePtr OpPC) {
- using BoolT = PrimConv<PT_Bool>::T;
- const T &Val = S.Stk.pop<T>();
- const unsigned Bits = Val.bitWidth();
- Boolean R;
- Boolean::inv(BoolT::from(Val, Bits), &R);
-
- S.Stk.push<BoolT>(R);
+inline bool Inv(InterpState &S, CodePtr OpPC) {
+ const auto &Val = S.Stk.pop<Boolean>();
+ S.Stk.push<Boolean>(!Val);
return true;
}
diff --git a/clang/lib/AST/ByteCode/Opcodes.td b/clang/lib/AST/ByteCode/Opcodes.td
index 3478eb174e518e..61319e1633d9ad 100644
--- a/clang/lib/AST/ByteCode/Opcodes.td
+++ b/clang/lib/AST/ByteCode/Opcodes.td
@@ -103,10 +103,6 @@ def PtrTypeClass : TypeClass {
let Types = [Ptr, FnPtr, MemberPtr];
}
-def BoolTypeClass : TypeClass {
- let Types = [Bool];
-}
-
def NonPtrTypeClass : TypeClass {
let Types = !listconcat(IntegerTypeClass.Types, [Bool], [Float]);
}
@@ -574,11 +570,8 @@ def Shr : Opcode {
// Unary operators.
//===----------------------------------------------------------------------===//
-// [Real] -> [Real]
-def Inv: Opcode {
- let Types = [BoolTypeClass];
- let HasGroup = 1;
-}
+// [Bool] -> [Bool]
+def Inv: Opcode;
// Increment and decrement.
def Inc: AluOpcode;
More information about the cfe-commits
mailing list