Skip to content

Commit

Permalink
[CodeGen] Emit IR for compound assignment with fixed-point operands.
Browse files Browse the repository at this point in the history
Reviewers: rjmccall, leonardchan

Subscribers: cfe-commits

Tags: #clang

Differential Revision: https://reviews.llvm.org/D73184
  • Loading branch information
bevin-hansson committed Apr 8, 2020
1 parent 39baaab commit 313461f
Show file tree
Hide file tree
Showing 3 changed files with 396 additions and 6 deletions.
20 changes: 14 additions & 6 deletions clang/lib/CodeGen/CGExprScalar.cpp
Expand Up @@ -3537,8 +3537,16 @@ Value *ScalarExprEmitter::EmitFixedPointBinOp(const BinOpInfo &op) {
QualType ResultTy = op.Ty;
QualType LHSTy, RHSTy;
if (const auto *BinOp = dyn_cast<BinaryOperator>(op.E)) {
LHSTy = BinOp->getLHS()->getType();
RHSTy = BinOp->getRHS()->getType();
if (const auto *CAO = dyn_cast<CompoundAssignOperator>(BinOp)) {
// For compound assignment, the effective type of the LHS at this point
// is the computation LHS type, not the actual LHS type, and the final
// result type is not the type of the expression but rather the
// computation result type.
LHSTy = CAO->getComputationLHSType();
ResultTy = CAO->getComputationResultType();
} else
LHSTy = BinOp->getLHS()->getType();
} else if (const auto *UnOp = dyn_cast<UnaryOperator>(op.E)) {
LHSTy = UnOp->getSubExpr()->getType();
RHSTy = UnOp->getSubExpr()->getType();
Expand All @@ -3558,9 +3566,10 @@ Value *ScalarExprEmitter::EmitFixedPointBinOp(const BinOpInfo &op) {
Value *FullRHS = EmitFixedPointConversion(RHS, RHSFixedSema, CommonFixedSema,
op.E->getExprLoc());

// Perform the actual addition.
// Perform the actual operation.
Value *Result;
switch (op.Opcode) {
case BO_AddAssign:
case BO_Add: {
if (ResultFixedSema.isSaturated()) {
llvm::Intrinsic::ID IID = ResultFixedSema.isSigned()
Expand All @@ -3572,6 +3581,7 @@ Value *ScalarExprEmitter::EmitFixedPointBinOp(const BinOpInfo &op) {
}
break;
}
case BO_SubAssign:
case BO_Sub: {
if (ResultFixedSema.isSaturated()) {
llvm::Intrinsic::ID IID = ResultFixedSema.isSigned()
Expand All @@ -3583,6 +3593,7 @@ Value *ScalarExprEmitter::EmitFixedPointBinOp(const BinOpInfo &op) {
}
break;
}
case BO_MulAssign:
case BO_Mul: {
llvm::Intrinsic::ID IID;
if (ResultFixedSema.isSaturated())
Expand All @@ -3597,6 +3608,7 @@ Value *ScalarExprEmitter::EmitFixedPointBinOp(const BinOpInfo &op) {
{FullLHS, FullRHS, Builder.getInt32(CommonFixedSema.getScale())});
break;
}
case BO_DivAssign:
case BO_Div: {
llvm::Intrinsic::ID IID;
if (ResultFixedSema.isSaturated())
Expand Down Expand Up @@ -3633,10 +3645,6 @@ Value *ScalarExprEmitter::EmitFixedPointBinOp(const BinOpInfo &op) {
case BO_Cmp:
case BO_LAnd:
case BO_LOr:
case BO_MulAssign:
case BO_DivAssign:
case BO_AddAssign:
case BO_SubAssign:
case BO_ShlAssign:
case BO_ShrAssign:
llvm_unreachable("Found unimplemented fixed point binary operation");
Expand Down
8 changes: 8 additions & 0 deletions clang/lib/Sema/SemaExpr.cpp
Expand Up @@ -13630,6 +13630,14 @@ ExprResult Sema::CreateBuiltinBinOp(SourceLocation OpLoc,
if (ResultTy.isNull() || LHS.isInvalid() || RHS.isInvalid())
return ExprError();

// The LHS is not converted to the result type for fixed-point compound
// assignment as the common type is computed on demand. Reset the CompLHSTy
// to the LHS type we would have gotten after unary conversions.
if (!CompLHSTy.isNull() &&
(LHS.get()->getType()->isFixedPointType() ||
RHS.get()->getType()->isFixedPointType()))
CompLHSTy = UsualUnaryConversions(LHS.get()).get()->getType();

if (ResultTy->isRealFloatingType() &&
(getLangOpts().getFPRoundingMode() != LangOptions::FPR_ToNearest ||
getLangOpts().getFPExceptionMode() != LangOptions::FPE_Ignore))
Expand Down

0 comments on commit 313461f

Please sign in to comment.