aboutsummaryrefslogtreecommitdiff
path: root/clang/lib/AST/ExprConstant.cpp
diff options
context:
space:
mode:
authorSimon Pilgrim <llvm-dev@redking.me.uk>2024-10-02 15:45:02 +0100
committerGitHub <noreply@github.com>2024-10-02 15:45:02 +0100
commitc7fb0eed05e768093fc202e94df1c0d88fd7c2f0 (patch)
treecef8e2b146588fadaace9433beab907a40f0715c /clang/lib/AST/ExprConstant.cpp
parent8282c58d9b1cd5b6df89ee3f68438fe0ee672f7f (diff)
downloadllvm-c7fb0eed05e768093fc202e94df1c0d88fd7c2f0.zip
llvm-c7fb0eed05e768093fc202e94df1c0d88fd7c2f0.tar.gz
llvm-c7fb0eed05e768093fc202e94df1c0d88fd7c2f0.tar.bz2
[clang][x86] Add constexpr support for ADC/SBB + ADX intrinsics (#110668)
ADC and ADX use the same internal intrinsics - for testing I've taken the same approach as the generic builtin overflow tests, putting the intrinsics in a constexpr test wrapper and comparing the carry/result value pair. I've added the addcarry/subborrow intrinsics to the clang language extension list - I'm not sure if we want to add all ISA intrinsics to the list (although we can if people think it useful?), but I felt we should at least include the baseline x86 intrinsics.
Diffstat (limited to 'clang/lib/AST/ExprConstant.cpp')
-rw-r--r--clang/lib/AST/ExprConstant.cpp32
1 files changed, 32 insertions, 0 deletions
diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index 48816d3..3a73cea 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -13464,6 +13464,38 @@ bool IntExprEvaluator::VisitBuiltinCallExpr(const CallExpr *E,
return Success(DidOverflow, E);
}
+ case clang::X86::BI__builtin_ia32_addcarryx_u32:
+ case clang::X86::BI__builtin_ia32_addcarryx_u64:
+ case clang::X86::BI__builtin_ia32_subborrow_u32:
+ case clang::X86::BI__builtin_ia32_subborrow_u64: {
+ LValue ResultLValue;
+ APSInt CarryIn, LHS, RHS;
+ QualType ResultType = E->getArg(3)->getType()->getPointeeType();
+ if (!EvaluateInteger(E->getArg(0), CarryIn, Info) ||
+ !EvaluateInteger(E->getArg(1), LHS, Info) ||
+ !EvaluateInteger(E->getArg(2), RHS, Info) ||
+ !EvaluatePointer(E->getArg(3), ResultLValue, Info))
+ return false;
+
+ bool IsAdd = BuiltinOp == clang::X86::BI__builtin_ia32_addcarryx_u32 ||
+ BuiltinOp == clang::X86::BI__builtin_ia32_addcarryx_u64;
+
+ unsigned BitWidth = LHS.getBitWidth();
+ unsigned CarryInBit = CarryIn.ugt(0) ? 1 : 0;
+ APInt ExResult =
+ IsAdd
+ ? (LHS.zext(BitWidth + 1) + (RHS.zext(BitWidth + 1) + CarryInBit))
+ : (LHS.zext(BitWidth + 1) - (RHS.zext(BitWidth + 1) + CarryInBit));
+
+ APInt Result = ExResult.extractBits(BitWidth, 0);
+ uint64_t CarryOut = ExResult.extractBitsAsZExtValue(1, BitWidth);
+
+ APValue APV{APSInt(Result, /*isUnsigned=*/true)};
+ if (!handleAssignment(Info, E, ResultLValue, ResultType, APV))
+ return false;
+ return Success(CarryOut, E);
+ }
+
case clang::X86::BI__builtin_ia32_bextr_u32:
case clang::X86::BI__builtin_ia32_bextr_u64:
case clang::X86::BI__builtin_ia32_bextri_u32: