| 1 |
#include "CIRGenBuilder.h" |
| 2 |
#include "CIRGenConstantEmitter.h" |
| 3 |
#include "CIRGenFunction.h" |
| 4 |
|
| 5 |
#include "clang/AST/StmtVisitor.h" |
| 6 |
|
| 7 |
using namespace clang; |
| 8 |
using namespace clang::CIRGen; |
| 9 |
|
| 10 |
#ifndef NDEBUG |
| 11 |
/// Return the complex type that we are meant to emit. |
| 12 |
static const ComplexType *getComplexType(QualType type) { |
| 13 |
type = type.getCanonicalType(); |
| 14 |
if (const ComplexType *comp = dyn_cast<ComplexType>(type)) |
| 15 |
return comp; |
| 16 |
return cast<ComplexType>(cast<AtomicType>(type)->getValueType()); |
| 17 |
} |
| 18 |
#endif // NDEBUG |
| 19 |
|
| 20 |
namespace { |
| 21 |
class ComplexExprEmitter : public StmtVisitor<ComplexExprEmitter, mlir::Value> { |
| 22 |
CIRGenFunction &cgf; |
| 23 |
CIRGenBuilderTy &builder; |
| 24 |
|
| 25 |
public: |
| 26 |
explicit ComplexExprEmitter(CIRGenFunction &cgf) |
| 27 |
: cgf(cgf), builder(cgf.getBuilder()) {} |
| 28 |
|
| 29 |
//===--------------------------------------------------------------------===// |
| 30 |
// Utilities |
| 31 |
//===--------------------------------------------------------------------===// |
| 32 |
|
| 33 |
/// Given an expression with complex type that represents a value l-value, |
| 34 |
/// this method emits the address of the l-value, then loads and returns the |
| 35 |
/// result. |
| 36 |
mlir::Value emitLoadOfLValue(const Expr *e) { |
| 37 |
return emitLoadOfLValue(cgf.emitLValue(e), e->getExprLoc()); |
| 38 |
} |
| 39 |
|
| 40 |
mlir::Value emitLoadOfLValue(LValue lv, SourceLocation loc); |
| 41 |
|
| 42 |
/// Store the specified real/imag parts into the |
| 43 |
/// specified value pointer. |
| 44 |
void emitStoreOfComplex(mlir::Location loc, mlir::Value val, LValue lv, |
| 45 |
bool isInit); |
| 46 |
|
| 47 |
/// Emit a cast from complex value Val to DestType. |
| 48 |
mlir::Value emitComplexToComplexCast(mlir::Value value, QualType srcType, |
| 49 |
QualType destType, SourceLocation loc); |
| 50 |
|
| 51 |
/// Emit a cast from scalar value Val to DestType. |
| 52 |
mlir::Value emitScalarToComplexCast(mlir::Value value, QualType srcType, |
| 53 |
QualType destType, SourceLocation loc); |
| 54 |
|
| 55 |
//===--------------------------------------------------------------------===// |
| 56 |
// Visitor Methods |
| 57 |
//===--------------------------------------------------------------------===// |
| 58 |
|
| 59 |
mlir::Value Visit(Expr *e) { |
| 60 |
return StmtVisitor<ComplexExprEmitter, mlir::Value>::Visit(e); |
| 61 |
} |
| 62 |
|
| 63 |
mlir::Value VisitStmt(Stmt *s) { |
| 64 |
cgf.cgm.errorNYI(s->getBeginLoc(), "ComplexExprEmitter VisitStmt"); |
| 65 |
return {}; |
| 66 |
} |
| 67 |
|
| 68 |
mlir::Value VisitExpr(Expr *e); |
| 69 |
mlir::Value VisitConstantExpr(ConstantExpr *e) { |
| 70 |
if (mlir::Attribute result = ConstantEmitter(cgf).tryEmitConstantExpr(e)) |
| 71 |
return builder.getConstant(cgf.getLoc(e->getSourceRange()), |
| 72 |
mlir::cast<mlir::TypedAttr>(result)); |
| 73 |
|
| 74 |
cgf.cgm.errorNYI(e->getExprLoc(), |
| 75 |
"ComplexExprEmitter VisitConstantExpr non constantexpr"); |
| 76 |
return {}; |
| 77 |
} |
| 78 |
|
| 79 |
mlir::Value VisitParenExpr(ParenExpr *pe) { return Visit(pe->getSubExpr()); } |
| 80 |
mlir::Value VisitGenericSelectionExpr(GenericSelectionExpr *ge) { |
| 81 |
return Visit(ge->getResultExpr()); |
| 82 |
} |
| 83 |
mlir::Value VisitImaginaryLiteral(const ImaginaryLiteral *il); |
| 84 |
mlir::Value |
| 85 |
VisitSubstNonTypeTemplateParmExpr(SubstNonTypeTemplateParmExpr *pe) { |
| 86 |
return Visit(pe->getReplacement()); |
| 87 |
} |
| 88 |
mlir::Value VisitCoawaitExpr(CoawaitExpr *s) { |
| 89 |
cgf.cgm.errorNYI(s->getExprLoc(), "ComplexExprEmitter VisitCoawaitExpr"); |
| 90 |
return {}; |
| 91 |
} |
| 92 |
mlir::Value VisitCoyieldExpr(CoyieldExpr *s) { |
| 93 |
cgf.cgm.errorNYI(s->getExprLoc(), "ComplexExprEmitter VisitCoyieldExpr"); |
| 94 |
return {}; |
| 95 |
} |
| 96 |
mlir::Value VisitUnaryCoawait(const UnaryOperator *e) { |
| 97 |
cgf.cgm.errorNYI(e->getExprLoc(), "ComplexExprEmitter VisitUnaryCoawait"); |
| 98 |
return {}; |
| 99 |
} |
| 100 |
|
| 101 |
mlir::Value emitConstant(const CIRGenFunction::ConstantEmission &constant, |
| 102 |
Expr *e) { |
| 103 |
assert(constant && "not a constant"); |
| 104 |
if (constant.isReference()) |
| 105 |
return emitLoadOfLValue(constant.getReferenceLValue(cgf, e), |
| 106 |
e->getExprLoc()); |
| 107 |
|
| 108 |
mlir::TypedAttr valueAttr = constant.getValue(); |
| 109 |
return builder.getConstant(cgf.getLoc(e->getSourceRange()), valueAttr); |
| 110 |
} |
| 111 |
|
| 112 |
// l-values. |
| 113 |
mlir::Value VisitDeclRefExpr(DeclRefExpr *e) { |
| 114 |
if (CIRGenFunction::ConstantEmission constant = cgf.tryEmitAsConstant(e)) |
| 115 |
return emitConstant(constant, e); |
| 116 |
return emitLoadOfLValue(e); |
| 117 |
} |
| 118 |
mlir::Value VisitObjCIvarRefExpr(ObjCIvarRefExpr *e) { |
| 119 |
cgf.cgm.errorNYI(e->getExprLoc(), |
| 120 |
"ComplexExprEmitter VisitObjCIvarRefExpr"); |
| 121 |
return {}; |
| 122 |
} |
| 123 |
mlir::Value VisitObjCMessageExpr(ObjCMessageExpr *e) { |
| 124 |
cgf.cgm.errorNYI(e->getExprLoc(), |
| 125 |
"ComplexExprEmitter VisitObjCMessageExpr"); |
| 126 |
return {}; |
| 127 |
} |
| 128 |
mlir::Value VisitArraySubscriptExpr(Expr *e) { return emitLoadOfLValue(e); } |
| 129 |
mlir::Value VisitMemberExpr(MemberExpr *me) { |
| 130 |
if (CIRGenFunction::ConstantEmission constant = cgf.tryEmitAsConstant(me)) { |
| 131 |
cgf.emitIgnoredExpr(me->getBase()); |
| 132 |
return emitConstant(constant, me); |
| 133 |
} |
| 134 |
return emitLoadOfLValue(me); |
| 135 |
} |
| 136 |
mlir::Value VisitOpaqueValueExpr(OpaqueValueExpr *e) { |
| 137 |
if (e->isGLValue()) |
| 138 |
return emitLoadOfLValue(cgf.getOrCreateOpaqueLValueMapping(e), |
| 139 |
e->getExprLoc()); |
| 140 |
return cgf.getOrCreateOpaqueRValueMapping(e).getComplexValue(); |
| 141 |
} |
| 142 |
|
| 143 |
mlir::Value VisitPseudoObjectExpr(PseudoObjectExpr *e) { |
| 144 |
cgf.cgm.errorNYI(e->getExprLoc(), |
| 145 |
"ComplexExprEmitter VisitPseudoObjectExpr"); |
| 146 |
return {}; |
| 147 |
} |
| 148 |
|
| 149 |
mlir::Value emitCast(CastKind ck, Expr *op, QualType destTy); |
| 150 |
mlir::Value VisitImplicitCastExpr(ImplicitCastExpr *e) { |
| 151 |
// Unlike for scalars, we don't have to worry about function->ptr demotion |
| 152 |
// here. |
| 153 |
if (e->changesVolatileQualification()) |
| 154 |
return emitLoadOfLValue(e); |
| 155 |
return emitCast(e->getCastKind(), e->getSubExpr(), e->getType()); |
| 156 |
} |
| 157 |
mlir::Value VisitCastExpr(CastExpr *e) { |
| 158 |
if (const auto *ece = dyn_cast<ExplicitCastExpr>(e)) { |
| 159 |
// Bind VLAs in the cast type. |
| 160 |
if (ece->getType()->isVariablyModifiedType()) { |
| 161 |
cgf.cgm.errorNYI(e->getExprLoc(), |
| 162 |
"VisitCastExpr Bind VLAs in the cast type"); |
| 163 |
return {}; |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
if (e->changesVolatileQualification()) |
| 168 |
return emitLoadOfLValue(e); |
| 169 |
|
| 170 |
return emitCast(e->getCastKind(), e->getSubExpr(), e->getType()); |
| 171 |
} |
| 172 |
mlir::Value VisitCallExpr(const CallExpr *e); |
| 173 |
mlir::Value VisitStmtExpr(const StmtExpr *e); |
| 174 |
|
| 175 |
// Operators. |
| 176 |
mlir::Value VisitPrePostIncDec(const UnaryOperator *e, cir::UnaryOpKind op, |
| 177 |
bool isPre) { |
| 178 |
LValue lv = cgf.emitLValue(e->getSubExpr()); |
| 179 |
return cgf.emitComplexPrePostIncDec(e, lv, op, isPre); |
| 180 |
} |
| 181 |
mlir::Value VisitUnaryPostDec(const UnaryOperator *e) { |
| 182 |
return VisitPrePostIncDec(e, cir::UnaryOpKind::Dec, false); |
| 183 |
} |
| 184 |
mlir::Value VisitUnaryPostInc(const UnaryOperator *e) { |
| 185 |
return VisitPrePostIncDec(e, cir::UnaryOpKind::Inc, false); |
| 186 |
} |
| 187 |
mlir::Value VisitUnaryPreDec(const UnaryOperator *e) { |
| 188 |
return VisitPrePostIncDec(e, cir::UnaryOpKind::Dec, true); |
| 189 |
} |
| 190 |
mlir::Value VisitUnaryPreInc(const UnaryOperator *e) { |
| 191 |
return VisitPrePostIncDec(e, cir::UnaryOpKind::Inc, true); |
| 192 |
} |
| 193 |
mlir::Value VisitUnaryDeref(const Expr *e) { return emitLoadOfLValue(e); } |
| 194 |
|
| 195 |
mlir::Value VisitUnaryPlus(const UnaryOperator *e); |
| 196 |
mlir::Value VisitUnaryMinus(const UnaryOperator *e); |
| 197 |
mlir::Value VisitPlusMinus(const UnaryOperator *e, cir::UnaryOpKind kind, |
| 198 |
QualType promotionType); |
| 199 |
mlir::Value VisitUnaryNot(const UnaryOperator *e); |
| 200 |
// LNot,Real,Imag never return complex. |
| 201 |
mlir::Value VisitUnaryExtension(const UnaryOperator *e) { |
| 202 |
return Visit(e->getSubExpr()); |
| 203 |
} |
| 204 |
mlir::Value VisitCXXDefaultArgExpr(CXXDefaultArgExpr *dae) { |
| 205 |
CIRGenFunction::CXXDefaultArgExprScope scope(cgf, dae); |
| 206 |
return Visit(dae->getExpr()); |
| 207 |
} |
| 208 |
mlir::Value VisitCXXDefaultInitExpr(CXXDefaultInitExpr *die) { |
| 209 |
CIRGenFunction::CXXDefaultInitExprScope scope(cgf, die); |
| 210 |
return Visit(die->getExpr()); |
| 211 |
} |
| 212 |
mlir::Value VisitExprWithCleanups(ExprWithCleanups *e) { |
| 213 |
cgf.cgm.errorNYI(e->getExprLoc(), |
| 214 |
"ComplexExprEmitter VisitExprWithCleanups"); |
| 215 |
return {}; |
| 216 |
} |
| 217 |
mlir::Value VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *e) { |
| 218 |
mlir::Location loc = cgf.getLoc(e->getExprLoc()); |
| 219 |
mlir::Type complexTy = cgf.convertType(e->getType()); |
| 220 |
return builder.getNullValue(complexTy, loc); |
| 221 |
} |
| 222 |
mlir::Value VisitImplicitValueInitExpr(ImplicitValueInitExpr *e) { |
| 223 |
cgf.cgm.errorNYI(e->getExprLoc(), |
| 224 |
"ComplexExprEmitter VisitImplicitValueInitExpr"); |
| 225 |
return {}; |
| 226 |
} |
| 227 |
|
| 228 |
struct BinOpInfo { |
| 229 |
mlir::Location loc; |
| 230 |
mlir::Value lhs{}; |
| 231 |
mlir::Value rhs{}; |
| 232 |
QualType ty{}; // Computation Type. |
| 233 |
FPOptions fpFeatures{}; |
| 234 |
}; |
| 235 |
|
| 236 |
BinOpInfo emitBinOps(const BinaryOperator *e, |
| 237 |
QualType promotionTy = QualType()); |
| 238 |
|
| 239 |
mlir::Value emitPromoted(const Expr *e, QualType promotionTy); |
| 240 |
mlir::Value emitPromotedComplexOperand(const Expr *e, QualType promotionTy); |
| 241 |
LValue emitCompoundAssignLValue( |
| 242 |
const CompoundAssignOperator *e, |
| 243 |
mlir::Value (ComplexExprEmitter::*func)(const BinOpInfo &), |
| 244 |
RValue &value); |
| 245 |
mlir::Value emitCompoundAssign( |
| 246 |
const CompoundAssignOperator *e, |
| 247 |
mlir::Value (ComplexExprEmitter::*func)(const BinOpInfo &)); |
| 248 |
|
| 249 |
mlir::Value emitBinAdd(const BinOpInfo &op); |
| 250 |
mlir::Value emitBinSub(const BinOpInfo &op); |
| 251 |
mlir::Value emitBinMul(const BinOpInfo &op); |
| 252 |
mlir::Value emitBinDiv(const BinOpInfo &op); |
| 253 |
|
| 254 |
QualType getPromotionType(QualType ty, bool isDivOpCode = false) { |
| 255 |
if (auto *complexTy = ty->getAs<ComplexType>()) { |
| 256 |
QualType elementTy = complexTy->getElementType(); |
| 257 |
if (elementTy.UseExcessPrecision(cgf.getContext())) |
| 258 |
return cgf.getContext().getComplexType(cgf.getContext().FloatTy); |
| 259 |
} |
| 260 |
|
| 261 |
if (ty.UseExcessPrecision(cgf.getContext())) |
| 262 |
return cgf.getContext().FloatTy; |
| 263 |
return QualType(); |
| 264 |
} |
| 265 |
|
| 266 |
#define HANDLEBINOP(OP) \ |
| 267 |
mlir::Value VisitBin##OP(const BinaryOperator *e) { \ |
| 268 |
QualType promotionTy = getPromotionType( \ |
| 269 |
e->getType(), e->getOpcode() == BinaryOperatorKind::BO_Div); \ |
| 270 |
mlir::Value result = emitBin##OP(emitBinOps(e, promotionTy)); \ |
| 271 |
if (!promotionTy.isNull()) \ |
| 272 |
result = cgf.emitUnPromotedValue(result, e->getType()); \ |
| 273 |
return result; \ |
| 274 |
} |
| 275 |
|
| 276 |
HANDLEBINOP(Add) |
| 277 |
HANDLEBINOP(Sub) |
| 278 |
HANDLEBINOP(Mul) |
| 279 |
HANDLEBINOP(Div) |
| 280 |
#undef HANDLEBINOP |
| 281 |
|
| 282 |
mlir::Value VisitCXXRewrittenBinaryOperator(CXXRewrittenBinaryOperator *e) { |
| 283 |
cgf.cgm.errorNYI(e->getExprLoc(), |
| 284 |
"ComplexExprEmitter VisitCXXRewrittenBinaryOperator"); |
| 285 |
return {}; |
| 286 |
} |
| 287 |
|
| 288 |
// Compound assignments. |
| 289 |
mlir::Value VisitBinAddAssign(const CompoundAssignOperator *e) { |
| 290 |
return emitCompoundAssign(e, &ComplexExprEmitter::emitBinAdd); |
| 291 |
} |
| 292 |
mlir::Value VisitBinSubAssign(const CompoundAssignOperator *e) { |
| 293 |
return emitCompoundAssign(e, &ComplexExprEmitter::emitBinSub); |
| 294 |
} |
| 295 |
mlir::Value VisitBinMulAssign(const CompoundAssignOperator *e) { |
| 296 |
return emitCompoundAssign(e, &ComplexExprEmitter::emitBinMul); |
| 297 |
} |
| 298 |
mlir::Value VisitBinDivAssign(const CompoundAssignOperator *e) { |
| 299 |
return emitCompoundAssign(e, &ComplexExprEmitter::emitBinDiv); |
| 300 |
} |
| 301 |
|
| 302 |
// GCC rejects rem/and/or/xor for integer complex. |
| 303 |
// Logical and/or always return int, never complex. |
| 304 |
|
| 305 |
// No comparisons produce a complex result. |
| 306 |
|
| 307 |
LValue emitBinAssignLValue(const BinaryOperator *e, mlir::Value &val); |
| 308 |
mlir::Value VisitBinAssign(const BinaryOperator *e); |
| 309 |
mlir::Value VisitBinComma(const BinaryOperator *e); |
| 310 |
|
| 311 |
mlir::Value |
| 312 |
VisitAbstractConditionalOperator(const AbstractConditionalOperator *e); |
| 313 |
mlir::Value VisitChooseExpr(ChooseExpr *e); |
| 314 |
|
| 315 |
mlir::Value VisitInitListExpr(InitListExpr *e); |
| 316 |
|
| 317 |
mlir::Value VisitCompoundLiteralExpr(CompoundLiteralExpr *e) { |
| 318 |
return emitLoadOfLValue(e); |
| 319 |
} |
| 320 |
|
| 321 |
mlir::Value VisitVAArgExpr(VAArgExpr *e); |
| 322 |
|
| 323 |
mlir::Value VisitAtomicExpr(AtomicExpr *e) { |
| 324 |
return cgf.emitAtomicExpr(e).getComplexValue(); |
| 325 |
} |
| 326 |
|
| 327 |
mlir::Value VisitPackIndexingExpr(PackIndexingExpr *e) { |
| 328 |
cgf.cgm.errorNYI(e->getExprLoc(), |
| 329 |
"ComplexExprEmitter VisitPackIndexingExpr"); |
| 330 |
return {}; |
| 331 |
} |
| 332 |
}; |
| 333 |
} // namespace |
| 334 |
|
| 335 |
//===----------------------------------------------------------------------===// |
| 336 |
// Utilities |
| 337 |
//===----------------------------------------------------------------------===// |
| 338 |
|
| 339 |
/// EmitLoadOfLValue - Given an RValue reference for a complex, emit code to |
| 340 |
/// load the real and imaginary pieces, returning them as Real/Imag. |
| 341 |
mlir::Value ComplexExprEmitter::emitLoadOfLValue(LValue lv, |
| 342 |
SourceLocation loc) { |
| 343 |
assert(lv.isSimple() && "non-simple complex l-value?"); |
| 344 |
if (lv.getType()->isAtomicType()) |
| 345 |
cgf.cgm.errorNYI(loc, "emitLoadOfLValue with Atomic LV"); |
| 346 |
|
| 347 |
const Address srcAddr = lv.getAddress(); |
| 348 |
return builder.createLoad(cgf.getLoc(loc), srcAddr, lv.isVolatileQualified()); |
| 349 |
} |
| 350 |
|
| 351 |
/// EmitStoreOfComplex - Store the specified real/imag parts into the |
| 352 |
/// specified value pointer. |
| 353 |
void ComplexExprEmitter::emitStoreOfComplex(mlir::Location loc, mlir::Value val, |
| 354 |
LValue lv, bool isInit) { |
| 355 |
if (lv.getType()->isAtomicType() || |
| 356 |
(!isInit && cgf.isLValueSuitableForInlineAtomic(lv))) { |
| 357 |
cgf.cgm.errorNYI(loc, "StoreOfComplex with Atomic LV"); |
| 358 |
return; |
| 359 |
} |
| 360 |
|
| 361 |
const Address destAddr = lv.getAddress(); |
| 362 |
builder.createStore(loc, val, destAddr, lv.isVolatileQualified()); |
| 363 |
} |
| 364 |
|
| 365 |
//===----------------------------------------------------------------------===// |
| 366 |
// Visitor Methods |
| 367 |
//===----------------------------------------------------------------------===// |
| 368 |
|
| 369 |
mlir::Value ComplexExprEmitter::VisitExpr(Expr *e) { |
| 370 |
cgf.cgm.errorNYI(e->getExprLoc(), "ComplexExprEmitter VisitExpr"); |
| 371 |
return {}; |
| 372 |
} |
| 373 |
|
| 374 |
mlir::Value |
| 375 |
ComplexExprEmitter::VisitImaginaryLiteral(const ImaginaryLiteral *il) { |
| 376 |
auto ty = mlir::cast<cir::ComplexType>(cgf.convertType(il->getType())); |
| 377 |
mlir::Type elementTy = ty.getElementType(); |
| 378 |
mlir::Location loc = cgf.getLoc(il->getExprLoc()); |
| 379 |
|
| 380 |
mlir::TypedAttr realValueAttr; |
| 381 |
mlir::TypedAttr imagValueAttr; |
| 382 |
|
| 383 |
if (mlir::isa<cir::IntType>(elementTy)) { |
| 384 |
llvm::APInt imagValue = cast<IntegerLiteral>(il->getSubExpr())->getValue(); |
| 385 |
realValueAttr = cir::IntAttr::get(elementTy, 0); |
| 386 |
imagValueAttr = cir::IntAttr::get(elementTy, imagValue); |
| 387 |
} else { |
| 388 |
assert(mlir::isa<cir::FPTypeInterface>(elementTy) && |
| 389 |
"Expected complex element type to be floating-point"); |
| 390 |
|
| 391 |
llvm::APFloat imagValue = |
| 392 |
cast<FloatingLiteral>(il->getSubExpr())->getValue(); |
| 393 |
realValueAttr = cir::FPAttr::get( |
| 394 |
elementTy, llvm::APFloat::getZero(imagValue.getSemantics())); |
| 395 |
imagValueAttr = cir::FPAttr::get(elementTy, imagValue); |
| 396 |
} |
| 397 |
|
| 398 |
auto complexAttr = cir::ConstComplexAttr::get(realValueAttr, imagValueAttr); |
| 399 |
return cir::ConstantOp::create(builder, loc, complexAttr); |
| 400 |
} |
| 401 |
|
| 402 |
mlir::Value ComplexExprEmitter::VisitCallExpr(const CallExpr *e) { |
| 403 |
if (e->getCallReturnType(cgf.getContext())->isReferenceType()) |
| 404 |
return emitLoadOfLValue(e); |
| 405 |
return cgf.emitCallExpr(e).getComplexValue(); |
| 406 |
} |
| 407 |
|
| 408 |
mlir::Value ComplexExprEmitter::VisitStmtExpr(const StmtExpr *e) { |
| 409 |
CIRGenFunction::StmtExprEvaluation eval(cgf); |
| 410 |
Address retAlloca = |
| 411 |
cgf.createMemTemp(e->getType(), cgf.getLoc(e->getSourceRange())); |
| 412 |
(void)cgf.emitCompoundStmt(*e->getSubStmt(), &retAlloca); |
| 413 |
assert(retAlloca.isValid() && "Expected complex return value"); |
| 414 |
return emitLoadOfLValue(cgf.makeAddrLValue(retAlloca, e->getType()), |
| 415 |
e->getExprLoc()); |
| 416 |
} |
| 417 |
|
| 418 |
mlir::Value ComplexExprEmitter::emitComplexToComplexCast(mlir::Value val, |
| 419 |
QualType srcType, |
| 420 |
QualType destType, |
| 421 |
SourceLocation loc) { |
| 422 |
if (srcType == destType) |
| 423 |
return val; |
| 424 |
|
| 425 |
// Get the src/dest element type. |
| 426 |
QualType srcElemTy = srcType->castAs<ComplexType>()->getElementType(); |
| 427 |
QualType destElemTy = destType->castAs<ComplexType>()->getElementType(); |
| 428 |
|
| 429 |
cir::CastKind castOpKind; |
| 430 |
if (srcElemTy->isFloatingType() && destElemTy->isFloatingType()) |
| 431 |
castOpKind = cir::CastKind::float_complex; |
| 432 |
else if (srcElemTy->isFloatingType() && destElemTy->isIntegerType()) |
| 433 |
castOpKind = cir::CastKind::float_complex_to_int_complex; |
| 434 |
else if (srcElemTy->isIntegerType() && destElemTy->isFloatingType()) |
| 435 |
castOpKind = cir::CastKind::int_complex_to_float_complex; |
| 436 |
else if (srcElemTy->isIntegerType() && destElemTy->isIntegerType()) |
| 437 |
castOpKind = cir::CastKind::int_complex; |
| 438 |
else |
| 439 |
llvm_unreachable("unexpected src type or dest type"); |
| 440 |
|
| 441 |
return builder.createCast(cgf.getLoc(loc), castOpKind, val, |
| 442 |
cgf.convertType(destType)); |
| 443 |
} |
| 444 |
|
| 445 |
mlir::Value ComplexExprEmitter::emitScalarToComplexCast(mlir::Value val, |
| 446 |
QualType srcType, |
| 447 |
QualType destType, |
| 448 |
SourceLocation loc) { |
| 449 |
cir::CastKind castOpKind; |
| 450 |
if (srcType->isFloatingType()) |
| 451 |
castOpKind = cir::CastKind::float_to_complex; |
| 452 |
else if (srcType->isIntegerType()) |
| 453 |
castOpKind = cir::CastKind::int_to_complex; |
| 454 |
else |
| 455 |
llvm_unreachable("unexpected src type"); |
| 456 |
|
| 457 |
return builder.createCast(cgf.getLoc(loc), castOpKind, val, |
| 458 |
cgf.convertType(destType)); |
| 459 |
} |
| 460 |
|
| 461 |
mlir::Value ComplexExprEmitter::emitCast(CastKind ck, Expr *op, |
| 462 |
QualType destTy) { |
| 463 |
switch (ck) { |
| 464 |
case CK_Dependent: |
| 465 |
llvm_unreachable("dependent type must be resolved before the CIR codegen"); |
| 466 |
|
| 467 |
case CK_NoOp: |
| 468 |
case CK_LValueToRValue: |
| 469 |
return Visit(op); |
| 470 |
|
| 471 |
case CK_AtomicToNonAtomic: |
| 472 |
case CK_NonAtomicToAtomic: |
| 473 |
case CK_UserDefinedConversion: { |
| 474 |
cgf.cgm.errorNYI( |
| 475 |
"ComplexExprEmitter::emitCast Atmoic & UserDefinedConversion"); |
| 476 |
return {}; |
| 477 |
} |
| 478 |
|
| 479 |
case CK_LValueBitCast: { |
| 480 |
LValue origLV = cgf.emitLValue(op); |
| 481 |
Address addr = |
| 482 |
origLV.getAddress().withElementType(builder, cgf.convertType(destTy)); |
| 483 |
LValue destLV = cgf.makeAddrLValue(addr, destTy); |
| 484 |
return emitLoadOfLValue(destLV, op->getExprLoc()); |
| 485 |
} |
| 486 |
|
| 487 |
case CK_LValueToRValueBitCast: { |
| 488 |
LValue sourceLVal = cgf.emitLValue(op); |
| 489 |
Address addr = sourceLVal.getAddress().withElementType( |
| 490 |
builder, cgf.convertTypeForMem(destTy)); |
| 491 |
LValue destLV = cgf.makeAddrLValue(addr, destTy); |
| 492 |
assert(!cir::MissingFeatures::opTBAA()); |
| 493 |
return emitLoadOfLValue(destLV, op->getExprLoc()); |
| 494 |
} |
| 495 |
|
| 496 |
case CK_BitCast: |
| 497 |
case CK_BaseToDerived: |
| 498 |
case CK_DerivedToBase: |
| 499 |
case CK_UncheckedDerivedToBase: |
| 500 |
case CK_Dynamic: |
| 501 |
case CK_ToUnion: |
| 502 |
case CK_ArrayToPointerDecay: |
| 503 |
case CK_FunctionToPointerDecay: |
| 504 |
case CK_NullToPointer: |
| 505 |
case CK_NullToMemberPointer: |
| 506 |
case CK_BaseToDerivedMemberPointer: |
| 507 |
case CK_DerivedToBaseMemberPointer: |
| 508 |
case CK_MemberPointerToBoolean: |
| 509 |
case CK_ReinterpretMemberPointer: |
| 510 |
case CK_ConstructorConversion: |
| 511 |
case CK_IntegralToPointer: |
| 512 |
case CK_PointerToIntegral: |
| 513 |
case CK_PointerToBoolean: |
| 514 |
case CK_ToVoid: |
| 515 |
case CK_VectorSplat: |
| 516 |
case CK_IntegralCast: |
| 517 |
case CK_BooleanToSignedIntegral: |
| 518 |
case CK_IntegralToBoolean: |
| 519 |
case CK_IntegralToFloating: |
| 520 |
case CK_FloatingToIntegral: |
| 521 |
case CK_FloatingToBoolean: |
| 522 |
case CK_FloatingCast: |
| 523 |
case CK_CPointerToObjCPointerCast: |
| 524 |
case CK_BlockPointerToObjCPointerCast: |
| 525 |
case CK_AnyPointerToBlockPointerCast: |
| 526 |
case CK_ObjCObjectLValueCast: |
| 527 |
case CK_FloatingComplexToReal: |
| 528 |
case CK_FloatingComplexToBoolean: |
| 529 |
case CK_IntegralComplexToReal: |
| 530 |
case CK_IntegralComplexToBoolean: |
| 531 |
case CK_ARCProduceObject: |
| 532 |
case CK_ARCConsumeObject: |
| 533 |
case CK_ARCReclaimReturnedObject: |
| 534 |
case CK_ARCExtendBlockObject: |
| 535 |
case CK_CopyAndAutoreleaseBlockObject: |
| 536 |
case CK_BuiltinFnToFnPtr: |
| 537 |
case CK_ZeroToOCLOpaqueType: |
| 538 |
case CK_AddressSpaceConversion: |
| 539 |
case CK_IntToOCLSampler: |
| 540 |
case CK_FloatingToFixedPoint: |
| 541 |
case CK_FixedPointToFloating: |
| 542 |
case CK_FixedPointCast: |
| 543 |
case CK_FixedPointToBoolean: |
| 544 |
case CK_FixedPointToIntegral: |
| 545 |
case CK_IntegralToFixedPoint: |
| 546 |
case CK_MatrixCast: |
| 547 |
case CK_HLSLVectorTruncation: |
| 548 |
case CK_HLSLMatrixTruncation: |
| 549 |
case CK_HLSLArrayRValue: |
| 550 |
case CK_HLSLElementwiseCast: |
| 551 |
case CK_HLSLAggregateSplatCast: |
| 552 |
llvm_unreachable("invalid cast kind for complex value"); |
| 553 |
|
| 554 |
case CK_FloatingRealToComplex: |
| 555 |
case CK_IntegralRealToComplex: { |
| 556 |
assert(!cir::MissingFeatures::cgFPOptionsRAII()); |
| 557 |
return emitScalarToComplexCast(cgf.emitScalarExpr(op), op->getType(), |
| 558 |
destTy, op->getExprLoc()); |
| 559 |
} |
| 560 |
|
| 561 |
case CK_FloatingComplexCast: |
| 562 |
case CK_FloatingComplexToIntegralComplex: |
| 563 |
case CK_IntegralComplexCast: |
| 564 |
case CK_IntegralComplexToFloatingComplex: { |
| 565 |
assert(!cir::MissingFeatures::cgFPOptionsRAII()); |
| 566 |
return emitComplexToComplexCast(Visit(op), op->getType(), destTy, |
| 567 |
op->getExprLoc()); |
| 568 |
} |
| 569 |
} |
| 570 |
|
| 571 |
llvm_unreachable("unknown cast resulting in complex value"); |
| 572 |
} |
| 573 |
|
| 574 |
mlir::Value ComplexExprEmitter::VisitUnaryPlus(const UnaryOperator *e) { |
| 575 |
QualType promotionTy = getPromotionType(e->getSubExpr()->getType()); |
| 576 |
mlir::Value result = VisitPlusMinus(e, cir::UnaryOpKind::Plus, promotionTy); |
| 577 |
if (!promotionTy.isNull()) |
| 578 |
return cgf.emitUnPromotedValue(result, e->getSubExpr()->getType()); |
| 579 |
return result; |
| 580 |
} |
| 581 |
|
| 582 |
mlir::Value ComplexExprEmitter::VisitUnaryMinus(const UnaryOperator *e) { |
| 583 |
QualType promotionTy = getPromotionType(e->getSubExpr()->getType()); |
| 584 |
mlir::Value result = VisitPlusMinus(e, cir::UnaryOpKind::Minus, promotionTy); |
| 585 |
if (!promotionTy.isNull()) |
| 586 |
return cgf.emitUnPromotedValue(result, e->getSubExpr()->getType()); |
| 587 |
return result; |
| 588 |
} |
| 589 |
|
| 590 |
mlir::Value ComplexExprEmitter::VisitPlusMinus(const UnaryOperator *e, |
| 591 |
cir::UnaryOpKind kind, |
| 592 |
QualType promotionType) { |
| 593 |
assert(kind == cir::UnaryOpKind::Plus || |
| 594 |
kind == cir::UnaryOpKind::Minus && |
| 595 |
"Invalid UnaryOp kind for ComplexType Plus or Minus"); |
| 596 |
|
| 597 |
mlir::Value op; |
| 598 |
if (!promotionType.isNull()) |
| 599 |
op = cgf.emitPromotedComplexExpr(e->getSubExpr(), promotionType); |
| 600 |
else |
| 601 |
op = Visit(e->getSubExpr()); |
| 602 |
return builder.createUnaryOp(cgf.getLoc(e->getExprLoc()), kind, op); |
| 603 |
} |
| 604 |
|
| 605 |
mlir::Value ComplexExprEmitter::VisitUnaryNot(const UnaryOperator *e) { |
| 606 |
mlir::Value op = Visit(e->getSubExpr()); |
| 607 |
return builder.createNot(op); |
| 608 |
} |
| 609 |
|
| 610 |
mlir::Value ComplexExprEmitter::emitBinAdd(const BinOpInfo &op) { |
| 611 |
assert(!cir::MissingFeatures::fastMathFlags()); |
| 612 |
assert(!cir::MissingFeatures::cgFPOptionsRAII()); |
| 613 |
|
| 614 |
if (mlir::isa<cir::ComplexType>(op.lhs.getType()) && |
| 615 |
mlir::isa<cir::ComplexType>(op.rhs.getType())) |
| 616 |
return cir::ComplexAddOp::create(builder, op.loc, op.lhs, op.rhs); |
| 617 |
|
| 618 |
if (mlir::isa<cir::ComplexType>(op.lhs.getType())) { |
| 619 |
mlir::Value real = builder.createComplexReal(op.loc, op.lhs); |
| 620 |
mlir::Value imag = builder.createComplexImag(op.loc, op.lhs); |
| 621 |
mlir::Value newReal = builder.createAdd(op.loc, real, op.rhs); |
| 622 |
return builder.createComplexCreate(op.loc, newReal, imag); |
| 623 |
} |
| 624 |
|
| 625 |
assert(mlir::isa<cir::ComplexType>(op.rhs.getType())); |
| 626 |
mlir::Value real = builder.createComplexReal(op.loc, op.rhs); |
| 627 |
mlir::Value imag = builder.createComplexImag(op.loc, op.rhs); |
| 628 |
mlir::Value newReal = builder.createAdd(op.loc, op.lhs, real); |
| 629 |
return builder.createComplexCreate(op.loc, newReal, imag); |
| 630 |
} |
| 631 |
|
| 632 |
mlir::Value ComplexExprEmitter::emitBinSub(const BinOpInfo &op) { |
| 633 |
assert(!cir::MissingFeatures::fastMathFlags()); |
| 634 |
assert(!cir::MissingFeatures::cgFPOptionsRAII()); |
| 635 |
|
| 636 |
if (mlir::isa<cir::ComplexType>(op.lhs.getType()) && |
| 637 |
mlir::isa<cir::ComplexType>(op.rhs.getType())) |
| 638 |
return cir::ComplexSubOp::create(builder, op.loc, op.lhs, op.rhs); |
| 639 |
|
| 640 |
if (mlir::isa<cir::ComplexType>(op.lhs.getType())) { |
| 641 |
mlir::Value real = builder.createComplexReal(op.loc, op.lhs); |
| 642 |
mlir::Value imag = builder.createComplexImag(op.loc, op.lhs); |
| 643 |
mlir::Value newReal = builder.createSub(op.loc, real, op.rhs); |
| 644 |
return builder.createComplexCreate(op.loc, newReal, imag); |
| 645 |
} |
| 646 |
|
| 647 |
assert(mlir::isa<cir::ComplexType>(op.rhs.getType())); |
| 648 |
mlir::Value real = builder.createComplexReal(op.loc, op.rhs); |
| 649 |
mlir::Value imag = builder.createComplexImag(op.loc, op.rhs); |
| 650 |
mlir::Value newReal = builder.createSub(op.loc, op.lhs, real); |
| 651 |
return builder.createComplexCreate(op.loc, newReal, imag); |
| 652 |
} |
| 653 |
|
| 654 |
static cir::ComplexRangeKind |
| 655 |
getComplexRangeAttr(LangOptions::ComplexRangeKind range) { |
| 656 |
switch (range) { |
| 657 |
case LangOptions::CX_Full: |
| 658 |
return cir::ComplexRangeKind::Full; |
| 659 |
case LangOptions::CX_Improved: |
| 660 |
return cir::ComplexRangeKind::Improved; |
| 661 |
case LangOptions::CX_Promoted: |
| 662 |
return cir::ComplexRangeKind::Promoted; |
| 663 |
case LangOptions::CX_Basic: |
| 664 |
return cir::ComplexRangeKind::Basic; |
| 665 |
case LangOptions::CX_None: |
| 666 |
// The default value for ComplexRangeKind is Full if no option is selected |
| 667 |
return cir::ComplexRangeKind::Full; |
| 668 |
} |
| 669 |
} |
| 670 |
|
| 671 |
mlir::Value ComplexExprEmitter::emitBinMul(const BinOpInfo &op) { |
| 672 |
assert(!cir::MissingFeatures::fastMathFlags()); |
| 673 |
assert(!cir::MissingFeatures::cgFPOptionsRAII()); |
| 674 |
|
| 675 |
if (mlir::isa<cir::ComplexType>(op.lhs.getType()) && |
| 676 |
mlir::isa<cir::ComplexType>(op.rhs.getType())) { |
| 677 |
cir::ComplexRangeKind rangeKind = |
| 678 |
getComplexRangeAttr(op.fpFeatures.getComplexRange()); |
| 679 |
return cir::ComplexMulOp::create(builder, op.loc, op.lhs, op.rhs, |
| 680 |
rangeKind); |
| 681 |
} |
| 682 |
|
| 683 |
if (mlir::isa<cir::ComplexType>(op.lhs.getType())) { |
| 684 |
mlir::Value real = builder.createComplexReal(op.loc, op.lhs); |
| 685 |
mlir::Value imag = builder.createComplexImag(op.loc, op.lhs); |
| 686 |
mlir::Value newReal = builder.createMul(op.loc, real, op.rhs); |
| 687 |
mlir::Value newImag = builder.createMul(op.loc, imag, op.rhs); |
| 688 |
return builder.createComplexCreate(op.loc, newReal, newImag); |
| 689 |
} |
| 690 |
|
| 691 |
assert(mlir::isa<cir::ComplexType>(op.rhs.getType())); |
| 692 |
mlir::Value real = builder.createComplexReal(op.loc, op.rhs); |
| 693 |
mlir::Value imag = builder.createComplexImag(op.loc, op.rhs); |
| 694 |
mlir::Value newReal = builder.createMul(op.loc, op.lhs, real); |
| 695 |
mlir::Value newImag = builder.createMul(op.loc, op.lhs, imag); |
| 696 |
return builder.createComplexCreate(op.loc, newReal, newImag); |
| 697 |
} |
| 698 |
|
| 699 |
mlir::Value ComplexExprEmitter::emitBinDiv(const BinOpInfo &op) { |
| 700 |
assert(!cir::MissingFeatures::fastMathFlags()); |
| 701 |
assert(!cir::MissingFeatures::cgFPOptionsRAII()); |
| 702 |
|
| 703 |
// Handle division between two complex values. In the case of complex integer |
| 704 |
// types mixed with scalar integers, the scalar integer type will always be |
| 705 |
// promoted to a complex integer value with a zero imaginary component when |
| 706 |
// the AST is formed. |
| 707 |
if (mlir::isa<cir::ComplexType>(op.lhs.getType()) && |
| 708 |
mlir::isa<cir::ComplexType>(op.rhs.getType())) { |
| 709 |
cir::ComplexRangeKind rangeKind = |
| 710 |
getComplexRangeAttr(op.fpFeatures.getComplexRange()); |
| 711 |
return cir::ComplexDivOp::create(builder, op.loc, op.lhs, op.rhs, |
| 712 |
rangeKind); |
| 713 |
} |
| 714 |
|
| 715 |
// The C99 standard (G.5.1) defines division of a complex value by a real |
| 716 |
// value in the following simplified form. |
| 717 |
if (mlir::isa<cir::ComplexType>(op.lhs.getType())) { |
| 718 |
assert(mlir::cast<cir::ComplexType>(op.lhs.getType()).getElementType() == |
| 719 |
op.rhs.getType()); |
| 720 |
mlir::Value real = builder.createComplexReal(op.loc, op.lhs); |
| 721 |
mlir::Value imag = builder.createComplexImag(op.loc, op.lhs); |
| 722 |
mlir::Value newReal = builder.createFDiv(op.loc, real, op.rhs); |
| 723 |
mlir::Value newImag = builder.createFDiv(op.loc, imag, op.rhs); |
| 724 |
return builder.createComplexCreate(op.loc, newReal, newImag); |
| 725 |
} |
| 726 |
|
| 727 |
assert(mlir::isa<cir::ComplexType>(op.rhs.getType())); |
| 728 |
cir::ConstantOp nullValue = builder.getNullValue(op.lhs.getType(), op.loc); |
| 729 |
mlir::Value lhs = builder.createComplexCreate(op.loc, op.lhs, nullValue); |
| 730 |
cir::ComplexRangeKind rangeKind = |
| 731 |
getComplexRangeAttr(op.fpFeatures.getComplexRange()); |
| 732 |
return cir::ComplexDivOp::create(builder, op.loc, lhs, op.rhs, rangeKind); |
| 733 |
} |
| 734 |
|
| 735 |
mlir::Value CIRGenFunction::emitUnPromotedValue(mlir::Value result, |
| 736 |
QualType unPromotionType) { |
| 737 |
assert(!mlir::cast<cir::ComplexType>(result.getType()).isIntegerComplex() && |
| 738 |
"integral complex will never be promoted"); |
| 739 |
return builder.createCast(cir::CastKind::float_complex, result, |
| 740 |
convertType(unPromotionType)); |
| 741 |
} |
| 742 |
|
| 743 |
mlir::Value CIRGenFunction::emitPromotedValue(mlir::Value result, |
| 744 |
QualType promotionType) { |
| 745 |
assert(!mlir::cast<cir::ComplexType>(result.getType()).isIntegerComplex() && |
| 746 |
"integral complex will never be promoted"); |
| 747 |
return builder.createCast(cir::CastKind::float_complex, result, |
| 748 |
convertType(promotionType)); |
| 749 |
} |
| 750 |
|
| 751 |
mlir::Value ComplexExprEmitter::emitPromoted(const Expr *e, |
| 752 |
QualType promotionTy) { |
| 753 |
e = e->IgnoreParens(); |
| 754 |
if (const auto *bo = dyn_cast<BinaryOperator>(e)) { |
| 755 |
switch (bo->getOpcode()) { |
| 756 |
#define HANDLE_BINOP(OP) \ |
| 757 |
case BO_##OP: \ |
| 758 |
return emitBin##OP(emitBinOps(bo, promotionTy)); |
| 759 |
HANDLE_BINOP(Add) |
| 760 |
HANDLE_BINOP(Sub) |
| 761 |
HANDLE_BINOP(Mul) |
| 762 |
HANDLE_BINOP(Div) |
| 763 |
#undef HANDLE_BINOP |
| 764 |
default: |
| 765 |
break; |
| 766 |
} |
| 767 |
} else if (const auto *unaryOp = dyn_cast<UnaryOperator>(e)) { |
| 768 |
switch (unaryOp->getOpcode()) { |
| 769 |
case UO_Minus: |
| 770 |
case UO_Plus: { |
| 771 |
auto kind = unaryOp->getOpcode() == UO_Plus ? cir::UnaryOpKind::Plus |
| 772 |
: cir::UnaryOpKind::Minus; |
| 773 |
return VisitPlusMinus(unaryOp, kind, promotionTy); |
| 774 |
} |
| 775 |
default: |
| 776 |
break; |
| 777 |
} |
| 778 |
} |
| 779 |
|
| 780 |
mlir::Value result = Visit(const_cast<Expr *>(e)); |
| 781 |
if (!promotionTy.isNull()) |
| 782 |
return cgf.emitPromotedValue(result, promotionTy); |
| 783 |
|
| 784 |
return result; |
| 785 |
} |
| 786 |
|
| 787 |
mlir::Value CIRGenFunction::emitPromotedComplexExpr(const Expr *e, |
| 788 |
QualType promotionType) { |
| 789 |
return ComplexExprEmitter(*this).emitPromoted(e, promotionType); |
| 790 |
} |
| 791 |
|
| 792 |
mlir::Value |
| 793 |
ComplexExprEmitter::emitPromotedComplexOperand(const Expr *e, |
| 794 |
QualType promotionTy) { |
| 795 |
if (e->getType()->isAnyComplexType()) { |
| 796 |
if (!promotionTy.isNull()) |
| 797 |
return cgf.emitPromotedComplexExpr(e, promotionTy); |
| 798 |
return Visit(const_cast<Expr *>(e)); |
| 799 |
} |
| 800 |
|
| 801 |
if (!promotionTy.isNull()) { |
| 802 |
QualType complexElementTy = |
| 803 |
promotionTy->castAs<ComplexType>()->getElementType(); |
| 804 |
return cgf.emitPromotedScalarExpr(e, complexElementTy); |
| 805 |
} |
| 806 |
return cgf.emitScalarExpr(e); |
| 807 |
} |
| 808 |
|
| 809 |
ComplexExprEmitter::BinOpInfo |
| 810 |
ComplexExprEmitter::emitBinOps(const BinaryOperator *e, QualType promotionTy) { |
| 811 |
BinOpInfo binOpInfo{cgf.getLoc(e->getExprLoc())}; |
| 812 |
binOpInfo.lhs = emitPromotedComplexOperand(e->getLHS(), promotionTy); |
| 813 |
binOpInfo.rhs = emitPromotedComplexOperand(e->getRHS(), promotionTy); |
| 814 |
binOpInfo.ty = promotionTy.isNull() ? e->getType() : promotionTy; |
| 815 |
binOpInfo.fpFeatures = e->getFPFeaturesInEffect(cgf.getLangOpts()); |
| 816 |
return binOpInfo; |
| 817 |
} |
| 818 |
|
| 819 |
LValue ComplexExprEmitter::emitCompoundAssignLValue( |
| 820 |
const CompoundAssignOperator *e, |
| 821 |
mlir::Value (ComplexExprEmitter::*func)(const BinOpInfo &), RValue &value) { |
| 822 |
QualType lhsTy = e->getLHS()->getType(); |
| 823 |
QualType rhsTy = e->getRHS()->getType(); |
| 824 |
SourceLocation exprLoc = e->getExprLoc(); |
| 825 |
mlir::Location loc = cgf.getLoc(exprLoc); |
| 826 |
|
| 827 |
if (lhsTy->getAs<AtomicType>()) { |
| 828 |
cgf.cgm.errorNYI("emitCompoundAssignLValue AtmoicType"); |
| 829 |
return {}; |
| 830 |
} |
| 831 |
|
| 832 |
BinOpInfo opInfo{loc}; |
| 833 |
opInfo.fpFeatures = e->getFPFeaturesInEffect(cgf.getLangOpts()); |
| 834 |
|
| 835 |
assert(!cir::MissingFeatures::cgFPOptionsRAII()); |
| 836 |
|
| 837 |
// Load the RHS and LHS operands. |
| 838 |
// __block variables need to have the rhs evaluated first, plus this should |
| 839 |
// improve codegen a little. |
| 840 |
QualType promotionTypeCR = getPromotionType(e->getComputationResultType()); |
| 841 |
opInfo.ty = promotionTypeCR.isNull() ? e->getComputationResultType() |
| 842 |
: promotionTypeCR; |
| 843 |
|
| 844 |
QualType complexElementTy = |
| 845 |
opInfo.ty->castAs<ComplexType>()->getElementType(); |
| 846 |
QualType promotionTypeRHS = getPromotionType(rhsTy); |
| 847 |
|
| 848 |
// The RHS should have been converted to the computation type. |
| 849 |
if (e->getRHS()->getType()->isRealFloatingType()) { |
| 850 |
if (!promotionTypeRHS.isNull()) { |
| 851 |
opInfo.rhs = cgf.emitPromotedScalarExpr(e->getRHS(), promotionTypeRHS); |
| 852 |
} else { |
| 853 |
assert(cgf.getContext().hasSameUnqualifiedType(complexElementTy, rhsTy)); |
| 854 |
opInfo.rhs = cgf.emitScalarExpr(e->getRHS()); |
| 855 |
} |
| 856 |
} else { |
| 857 |
if (!promotionTypeRHS.isNull()) { |
| 858 |
opInfo.rhs = cgf.emitPromotedComplexExpr(e->getRHS(), promotionTypeRHS); |
| 859 |
} else { |
| 860 |
assert(cgf.getContext().hasSameUnqualifiedType(opInfo.ty, rhsTy)); |
| 861 |
opInfo.rhs = Visit(e->getRHS()); |
| 862 |
} |
| 863 |
} |
| 864 |
|
| 865 |
LValue lhs = cgf.emitLValue(e->getLHS()); |
| 866 |
|
| 867 |
// Load from the l-value and convert it. |
| 868 |
QualType promotionTypeLHS = getPromotionType(e->getComputationLHSType()); |
| 869 |
if (lhsTy->isAnyComplexType()) { |
| 870 |
mlir::Value lhsValue = emitLoadOfLValue(lhs, exprLoc); |
| 871 |
QualType destTy = promotionTypeLHS.isNull() ? opInfo.ty : promotionTypeLHS; |
| 872 |
opInfo.lhs = emitComplexToComplexCast(lhsValue, lhsTy, destTy, exprLoc); |
| 873 |
} else { |
| 874 |
mlir::Value lhsVal = cgf.emitLoadOfScalar(lhs, exprLoc); |
| 875 |
// For floating point real operands we can directly pass the scalar form |
| 876 |
// to the binary operator emission and potentially get more efficient code. |
| 877 |
if (lhsTy->isRealFloatingType()) { |
| 878 |
QualType promotedComplexElementTy; |
| 879 |
if (!promotionTypeLHS.isNull()) { |
| 880 |
promotedComplexElementTy = |
| 881 |
cast<ComplexType>(promotionTypeLHS)->getElementType(); |
| 882 |
if (!cgf.getContext().hasSameUnqualifiedType(promotedComplexElementTy, |
| 883 |
promotionTypeLHS)) |
| 884 |
lhsVal = cgf.emitScalarConversion(lhsVal, lhsTy, |
| 885 |
promotedComplexElementTy, exprLoc); |
| 886 |
} else { |
| 887 |
if (!cgf.getContext().hasSameUnqualifiedType(complexElementTy, lhsTy)) |
| 888 |
lhsVal = cgf.emitScalarConversion(lhsVal, lhsTy, complexElementTy, |
| 889 |
exprLoc); |
| 890 |
} |
| 891 |
opInfo.lhs = lhsVal; |
| 892 |
} else { |
| 893 |
opInfo.lhs = emitScalarToComplexCast(lhsVal, lhsTy, opInfo.ty, exprLoc); |
| 894 |
} |
| 895 |
} |
| 896 |
|
| 897 |
// Expand the binary operator. |
| 898 |
mlir::Value result = (this->*func)(opInfo); |
| 899 |
|
| 900 |
// Truncate the result and store it into the LHS lvalue. |
| 901 |
if (lhsTy->isAnyComplexType()) { |
| 902 |
mlir::Value resultValue = |
| 903 |
emitComplexToComplexCast(result, opInfo.ty, lhsTy, exprLoc); |
| 904 |
emitStoreOfComplex(loc, resultValue, lhs, /*isInit*/ false); |
| 905 |
value = RValue::getComplex(resultValue); |
| 906 |
} else { |
| 907 |
mlir::Value resultValue = |
| 908 |
cgf.emitComplexToScalarConversion(result, opInfo.ty, lhsTy, exprLoc); |
| 909 |
cgf.emitStoreOfScalar(resultValue, lhs, /*isInit*/ false); |
| 910 |
value = RValue::get(resultValue); |
| 911 |
} |
| 912 |
|
| 913 |
return lhs; |
| 914 |
} |
| 915 |
|
| 916 |
mlir::Value ComplexExprEmitter::emitCompoundAssign( |
| 917 |
const CompoundAssignOperator *e, |
| 918 |
mlir::Value (ComplexExprEmitter::*func)(const BinOpInfo &)) { |
| 919 |
RValue val; |
| 920 |
LValue lv = emitCompoundAssignLValue(e, func, val); |
| 921 |
|
| 922 |
// The result of an assignment in C is the assigned r-value. |
| 923 |
if (!cgf.getLangOpts().CPlusPlus) |
| 924 |
return val.getComplexValue(); |
| 925 |
|
| 926 |
// If the lvalue is non-volatile, return the computed value of the assignment. |
| 927 |
if (!lv.isVolatileQualified()) |
| 928 |
return val.getComplexValue(); |
| 929 |
|
| 930 |
return emitLoadOfLValue(lv, e->getExprLoc()); |
| 931 |
} |
| 932 |
|
| 933 |
LValue ComplexExprEmitter::emitBinAssignLValue(const BinaryOperator *e, |
| 934 |
mlir::Value &value) { |
| 935 |
assert(cgf.getContext().hasSameUnqualifiedType(e->getLHS()->getType(), |
| 936 |
e->getRHS()->getType()) && |
| 937 |
"Invalid assignment"); |
| 938 |
|
| 939 |
// Emit the RHS. __block variables need the RHS evaluated first. |
| 940 |
value = Visit(e->getRHS()); |
| 941 |
|
| 942 |
// Compute the address to store into. |
| 943 |
LValue lhs = cgf.emitLValue(e->getLHS()); |
| 944 |
|
| 945 |
// Store the result value into the LHS lvalue. |
| 946 |
emitStoreOfComplex(cgf.getLoc(e->getExprLoc()), value, lhs, |
| 947 |
/*isInit*/ false); |
| 948 |
return lhs; |
| 949 |
} |
| 950 |
|
| 951 |
mlir::Value ComplexExprEmitter::VisitBinAssign(const BinaryOperator *e) { |
| 952 |
mlir::Value value; |
| 953 |
LValue lv = emitBinAssignLValue(e, value); |
| 954 |
|
| 955 |
// The result of an assignment in C is the assigned r-value. |
| 956 |
if (!cgf.getLangOpts().CPlusPlus) |
| 957 |
return value; |
| 958 |
|
| 959 |
// If the lvalue is non-volatile, return the computed value of the |
| 960 |
// assignment. |
| 961 |
if (!lv.isVolatile()) |
| 962 |
return value; |
| 963 |
|
| 964 |
return emitLoadOfLValue(lv, e->getExprLoc()); |
| 965 |
} |
| 966 |
|
| 967 |
mlir::Value ComplexExprEmitter::VisitBinComma(const BinaryOperator *e) { |
| 968 |
cgf.emitIgnoredExpr(e->getLHS()); |
| 969 |
return Visit(e->getRHS()); |
| 970 |
} |
| 971 |
|
| 972 |
mlir::Value ComplexExprEmitter::VisitAbstractConditionalOperator( |
| 973 |
const AbstractConditionalOperator *e) { |
| 974 |
mlir::Location loc = cgf.getLoc(e->getSourceRange()); |
| 975 |
|
| 976 |
// Bind the common expression if necessary. |
| 977 |
CIRGenFunction::OpaqueValueMapping binding(cgf, e); |
| 978 |
|
| 979 |
CIRGenFunction::ConditionalEvaluation eval(cgf); |
| 980 |
|
| 981 |
Expr *cond = e->getCond()->IgnoreParens(); |
| 982 |
mlir::Value condValue = cgf.evaluateExprAsBool(cond); |
| 983 |
|
| 984 |
return cir::TernaryOp::create( |
| 985 |
builder, loc, condValue, |
| 986 |
/*thenBuilder=*/ |
| 987 |
[&](mlir::OpBuilder &b, mlir::Location loc) { |
| 988 |
eval.beginEvaluation(); |
| 989 |
mlir::Value trueValue = Visit(e->getTrueExpr()); |
| 990 |
cir::YieldOp::create(b, loc, trueValue); |
| 991 |
eval.endEvaluation(); |
| 992 |
}, |
| 993 |
/*elseBuilder=*/ |
| 994 |
[&](mlir::OpBuilder &b, mlir::Location loc) { |
| 995 |
eval.beginEvaluation(); |
| 996 |
mlir::Value falseValue = Visit(e->getFalseExpr()); |
| 997 |
cir::YieldOp::create(b, loc, falseValue); |
| 998 |
eval.endEvaluation(); |
| 999 |
}) |
| 1000 |
.getResult(); |
| 1001 |
} |
| 1002 |
|
| 1003 |
mlir::Value ComplexExprEmitter::VisitChooseExpr(ChooseExpr *e) { |
| 1004 |
return Visit(e->getChosenSubExpr()); |
| 1005 |
} |
| 1006 |
|
| 1007 |
mlir::Value ComplexExprEmitter::VisitInitListExpr(InitListExpr *e) { |
| 1008 |
mlir::Location loc = cgf.getLoc(e->getExprLoc()); |
| 1009 |
if (e->getNumInits() == 2) { |
| 1010 |
mlir::Value real = cgf.emitScalarExpr(e->getInit(0)); |
| 1011 |
mlir::Value imag = cgf.emitScalarExpr(e->getInit(1)); |
| 1012 |
return builder.createComplexCreate(loc, real, imag); |
| 1013 |
} |
| 1014 |
|
| 1015 |
if (e->getNumInits() == 1) |
| 1016 |
return Visit(e->getInit(0)); |
| 1017 |
|
| 1018 |
assert(e->getNumInits() == 0 && "Unexpected number of inits"); |
| 1019 |
mlir::Type complexTy = cgf.convertType(e->getType()); |
| 1020 |
return builder.getNullValue(complexTy, loc); |
| 1021 |
} |
| 1022 |
|
| 1023 |
mlir::Value ComplexExprEmitter::VisitVAArgExpr(VAArgExpr *e) { |
| 1024 |
return cgf.emitVAArg(e); |
| 1025 |
} |
| 1026 |
|
| 1027 |
//===----------------------------------------------------------------------===// |
| 1028 |
// Entry Point into this File |
| 1029 |
//===----------------------------------------------------------------------===// |
| 1030 |
|
| 1031 |
/// EmitComplexExpr - Emit the computation of the specified expression of |
| 1032 |
/// complex type, ignoring the result. |
| 1033 |
mlir::Value CIRGenFunction::emitComplexExpr(const Expr *e) { |
| 1034 |
assert(e && getComplexType(e->getType()) && |
| 1035 |
"Invalid complex expression to emit"); |
| 1036 |
|
| 1037 |
return ComplexExprEmitter(*this).Visit(const_cast<Expr *>(e)); |
| 1038 |
} |
| 1039 |
|
| 1040 |
void CIRGenFunction::emitComplexExprIntoLValue(const Expr *e, LValue dest, |
| 1041 |
bool isInit) { |
| 1042 |
assert(e && getComplexType(e->getType()) && |
| 1043 |
"Invalid complex expression to emit"); |
| 1044 |
ComplexExprEmitter emitter(*this); |
| 1045 |
mlir::Value value = emitter.Visit(const_cast<Expr *>(e)); |
| 1046 |
emitter.emitStoreOfComplex(getLoc(e->getExprLoc()), value, dest, isInit); |
| 1047 |
} |
| 1048 |
|
| 1049 |
/// EmitStoreOfComplex - Store a complex number into the specified l-value. |
| 1050 |
void CIRGenFunction::emitStoreOfComplex(mlir::Location loc, mlir::Value v, |
| 1051 |
LValue dest, bool isInit) { |
| 1052 |
ComplexExprEmitter(*this).emitStoreOfComplex(loc, v, dest, isInit); |
| 1053 |
} |
| 1054 |
|
| 1055 |
mlir::Value CIRGenFunction::emitLoadOfComplex(LValue src, SourceLocation loc) { |
| 1056 |
return ComplexExprEmitter(*this).emitLoadOfLValue(src, loc); |
| 1057 |
} |
| 1058 |
|
| 1059 |
LValue CIRGenFunction::emitComplexAssignmentLValue(const BinaryOperator *e) { |
| 1060 |
assert(e->getOpcode() == BO_Assign && "Expected assign op"); |
| 1061 |
|
| 1062 |
mlir::Value value; // ignored |
| 1063 |
LValue lvalue = ComplexExprEmitter(*this).emitBinAssignLValue(e, value); |
| 1064 |
if (getLangOpts().OpenMP) |
| 1065 |
cgm.errorNYI("emitComplexAssignmentLValue OpenMP"); |
| 1066 |
|
| 1067 |
return lvalue; |
| 1068 |
} |
| 1069 |
|
| 1070 |
using CompoundFunc = |
| 1071 |
mlir::Value (ComplexExprEmitter::*)(const ComplexExprEmitter::BinOpInfo &); |
| 1072 |
|
| 1073 |
static CompoundFunc getComplexOp(BinaryOperatorKind op) { |
| 1074 |
switch (op) { |
| 1075 |
case BO_MulAssign: |
| 1076 |
return &ComplexExprEmitter::emitBinMul; |
| 1077 |
case BO_DivAssign: |
| 1078 |
return &ComplexExprEmitter::emitBinDiv; |
| 1079 |
case BO_SubAssign: |
| 1080 |
return &ComplexExprEmitter::emitBinSub; |
| 1081 |
case BO_AddAssign: |
| 1082 |
return &ComplexExprEmitter::emitBinAdd; |
| 1083 |
default: |
| 1084 |
llvm_unreachable("unexpected complex compound assignment"); |
| 1085 |
} |
| 1086 |
} |
| 1087 |
|
| 1088 |
LValue CIRGenFunction::emitComplexCompoundAssignmentLValue( |
| 1089 |
const CompoundAssignOperator *e) { |
| 1090 |
CompoundFunc op = getComplexOp(e->getOpcode()); |
| 1091 |
RValue val; |
| 1092 |
return ComplexExprEmitter(*this).emitCompoundAssignLValue(e, op, val); |
| 1093 |
} |
| 1094 |
|
| 1095 |
mlir::Value CIRGenFunction::emitComplexPrePostIncDec(const UnaryOperator *e, |
| 1096 |
LValue lv, |
| 1097 |
cir::UnaryOpKind op, |
| 1098 |
bool isPre) { |
| 1099 |
assert(op == cir::UnaryOpKind::Inc || |
| 1100 |
op == cir::UnaryOpKind::Dec && "Invalid UnaryOp kind for ComplexType"); |
| 1101 |
|
| 1102 |
mlir::Value inVal = emitLoadOfComplex(lv, e->getExprLoc()); |
| 1103 |
mlir::Location loc = getLoc(e->getExprLoc()); |
| 1104 |
mlir::Value incVal = builder.createUnaryOp(loc, op, inVal); |
| 1105 |
|
| 1106 |
// Store the updated result through the lvalue. |
| 1107 |
emitStoreOfComplex(loc, incVal, lv, /*isInit=*/false); |
| 1108 |
|
| 1109 |
if (getLangOpts().OpenMP) |
| 1110 |
cgm.errorNYI(loc, "emitComplexPrePostIncDec OpenMP"); |
| 1111 |
|
| 1112 |
// If this is a postinc, return the value read from memory, otherwise use the |
| 1113 |
// updated value. |
| 1114 |
return isPre ? incVal : inVal; |
| 1115 |
} |
| 1116 |
|
| 1117 |
LValue CIRGenFunction::emitScalarCompoundAssignWithComplex( |
| 1118 |
const CompoundAssignOperator *e, mlir::Value &result) { |
| 1119 |
// Key Instructions: Don't need to create an atom group here; one will already |
| 1120 |
// be active through scalar handling code. |
| 1121 |
CompoundFunc op = getComplexOp(e->getOpcode()); |
| 1122 |
RValue value; |
| 1123 |
LValue ret = ComplexExprEmitter(*this).emitCompoundAssignLValue(e, op, value); |
| 1124 |
result = value.getValue(); |
| 1125 |
return ret; |
| 1126 |
} |