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