1414#include " CIRGenConstantEmitter.h"
1515#include " CIRGenFunction.h"
1616#include " CIRGenModule.h"
17+ #include " CIRGenTBAA.h"
1718#include " CIRGenValue.h"
1819#include " mlir/IR/BuiltinAttributes.h"
1920#include " mlir/IR/Value.h"
@@ -69,7 +70,8 @@ Address CIRGenFunction::emitAddrOfFieldStorage(Address base,
6970// / Given an expression of pointer type, try to
7071// / derive a more accurate bound on the alignment of the pointer.
7172Address CIRGenFunction::emitPointerWithAlignment (const Expr *expr,
72- LValueBaseInfo *baseInfo) {
73+ LValueBaseInfo *baseInfo,
74+ TBAAAccessInfo *tbaaInfo) {
7375 // We allow this with ObjC object pointers because of fragile ABIs.
7476 assert (expr->getType ()->isPointerType () ||
7577 expr->getType ()->isObjCObjectPointerType ());
@@ -98,12 +100,18 @@ Address CIRGenFunction::emitPointerWithAlignment(const Expr *expr,
98100 *baseInfo = innerBaseInfo;
99101
100102 if (isa<ExplicitCastExpr>(ce)) {
101- LValueBaseInfo targetTypeBaseInfo;
102-
103103 const QualType pointeeType = expr->getType ()->getPointeeType ();
104+
105+ LValueBaseInfo targetTypeBaseInfo;
104106 const CharUnits align =
105107 cgm.getNaturalTypeAlignment (pointeeType, &targetTypeBaseInfo);
106108
109+ if (tbaaInfo) {
110+ TBAAAccessInfo targetTypeTbaaInfo = cgm.getTBAAAccessInfo (pointeeType);
111+ *tbaaInfo =
112+ cgm.mergeTBAAInfoForCast (*tbaaInfo, targetTypeTbaaInfo);
113+ }
114+
107115 // If the source l-value is opaque, honor the alignment of the
108116 // casted-to type.
109117 if (innerBaseInfo.getAlignmentSource () != AlignmentSource::Decl) {
@@ -315,7 +323,8 @@ static LValue emitGlobalVarDeclLValue(CIRGenFunction &cgf, const Expr *e,
315323
316324void CIRGenFunction::emitStoreOfScalar (mlir::Value value, Address addr,
317325 bool isVolatile, QualType ty,
318- LValueBaseInfo baseInfo, bool isInit,
326+ LValueBaseInfo baseInfo, TBAAAccessInfo tbaaInfo,
327+ bool isInit,
319328 bool isNontemporal) {
320329 assert (!cir::MissingFeatures::opLoadStoreThreadLocal ());
321330
@@ -338,8 +347,7 @@ void CIRGenFunction::emitStoreOfScalar(mlir::Value value, Address addr,
338347
339348 value = emitToMemory (value, ty);
340349
341- assert (!cir::MissingFeatures::opLoadStoreTbaa ());
342- LValue atomicLValue = LValue::makeAddr (addr, ty, baseInfo);
350+ LValue atomicLValue = LValue::makeAddr (addr, ty, baseInfo, tbaaInfo);
343351 if (ty->isAtomicType () ||
344352 (!isInit && isLValueSuitableForInlineAtomic (atomicLValue))) {
345353 emitAtomicStore (RValue::get (value), atomicLValue, isInit);
@@ -357,14 +365,13 @@ void CIRGenFunction::emitStoreOfScalar(mlir::Value value, Address addr,
357365 }
358366
359367 assert (currSrcLoc && " must pass in source location" );
360- builder.createStore (*currSrcLoc, value, addr, isVolatile);
368+ cir::StoreOp store = builder.createStore (*currSrcLoc, value, addr, isVolatile);
369+ cgm.decorateOperationWithTBAA (store, tbaaInfo);
361370
362371 if (isNontemporal) {
363372 cgm.errorNYI (addr.getPointer ().getLoc (), " emitStoreOfScalar nontemporal" );
364373 return ;
365374 }
366-
367- assert (!cir::MissingFeatures::opTBAA ());
368375}
369376
370377// TODO: Replace this with a proper TargetInfo function call.
@@ -426,6 +433,8 @@ Address CIRGenFunction::getAddrOfBitFieldStorage(LValue base,
426433LValue CIRGenFunction::emitLValueForBitField (LValue base,
427434 const FieldDecl *field) {
428435 LValueBaseInfo baseInfo = base.getBaseInfo ();
436+ TBAAAccessInfo tbaaInfo{};
437+
429438 const CIRGenRecordLayout &layout =
430439 cgm.getTypes ().getCIRGenRecordLayout (field->getParent ());
431440 const CIRGenBitFieldInfo &info = layout.getBitFieldInfo (field);
@@ -444,7 +453,7 @@ LValue CIRGenFunction::emitLValueForBitField(LValue base,
444453 // TODO(cir): Support TBAA for bit fields.
445454 assert (!cir::MissingFeatures::opTBAA ());
446455 LValueBaseInfo fieldBaseInfo (baseInfo.getAlignmentSource ());
447- return LValue::makeBitfield (addr, info, fieldType, fieldBaseInfo);
456+ return LValue::makeBitfield (addr, info, fieldType, fieldBaseInfo, tbaaInfo );
448457}
449458
450459LValue CIRGenFunction::emitLValueForField (LValue base, const FieldDecl *field) {
@@ -457,7 +466,10 @@ LValue CIRGenFunction::emitLValueForField(LValue base, const FieldDecl *field) {
457466 const RecordDecl *rec = field->getParent ();
458467 AlignmentSource baseAlignSource = baseInfo.getAlignmentSource ();
459468 LValueBaseInfo fieldBaseInfo (getFieldAlignmentSource (baseAlignSource));
460- assert (!cir::MissingFeatures::opTBAA ());
469+ TBAAAccessInfo fieldTbaaInfo{};
470+
471+ // TODO(cir): Initialize tbaa info
472+ assert (!MissingFeatures::opTBAA ());
461473
462474 Address addr = base.getAddress ();
463475 if (auto *classDecl = dyn_cast<CXXRecordDecl>(rec)) {
@@ -489,11 +501,11 @@ LValue CIRGenFunction::emitLValueForField(LValue base, const FieldDecl *field) {
489501 // If this is a reference field, load the reference right now.
490502 if (fieldType->isReferenceType ()) {
491503 assert (!cir::MissingFeatures::opTBAA ());
492- LValue refLVal = makeAddrLValue (addr, fieldType, fieldBaseInfo);
504+ LValue refLVal = makeAddrLValue (addr, fieldType, fieldBaseInfo, fieldTbaaInfo );
493505 if (recordCVR & Qualifiers::Volatile)
494506 refLVal.getQuals ().addVolatile ();
495507 addr = emitLoadOfReference (refLVal, getLoc (field->getSourceRange ()),
496- &fieldBaseInfo);
508+ &fieldBaseInfo, &fieldTbaaInfo );
497509
498510 // Qualifiers on the struct don't apply to the referencee.
499511 recordCVR = 0 ;
@@ -561,13 +573,13 @@ void CIRGenFunction::emitStoreOfScalar(mlir::Value value, LValue lvalue,
561573 }
562574
563575 emitStoreOfScalar (value, lvalue.getAddress (), lvalue.isVolatile (),
564- lvalue.getType (), lvalue.getBaseInfo (), isInit,
576+ lvalue.getType (), lvalue.getBaseInfo (), lvalue. getTBAAInfo (), isInit,
565577 /* isNontemporal=*/ false );
566578}
567579
568580mlir::Value CIRGenFunction::emitLoadOfScalar (Address addr, bool isVolatile,
569581 QualType ty, SourceLocation loc,
570- LValueBaseInfo baseInfo) {
582+ LValueBaseInfo baseInfo, TBAAAccessInfo tbaaInfo ) {
571583 assert (!cir::MissingFeatures::opLoadStoreThreadLocal ());
572584 mlir::Type eltTy = addr.getElementType ();
573585
@@ -586,8 +598,7 @@ mlir::Value CIRGenFunction::emitLoadOfScalar(Address addr, bool isVolatile,
586598 " emitLoadOfScalar Vec3 & PreserveVec3Type disabled" );
587599 }
588600
589- assert (!cir::MissingFeatures::opLoadStoreTbaa ());
590- LValue atomicLValue = LValue::makeAddr (addr, ty, baseInfo);
601+ LValue atomicLValue = LValue::makeAddr (addr, ty, baseInfo, tbaaInfo);
591602 if (ty->isAtomicType () || isLValueSuitableForInlineAtomic (atomicLValue))
592603 cgm.errorNYI (" emitLoadOfScalar: load atomic" );
593604
@@ -596,19 +607,21 @@ mlir::Value CIRGenFunction::emitLoadOfScalar(Address addr, bool isVolatile,
596607
597608 assert (!cir::MissingFeatures::opLoadEmitScalarRangeCheck ());
598609
599- mlir::Value loadOp = builder.createLoad (getLoc (loc), addr, isVolatile);
610+ cir::LoadOp loadOp = builder.createLoad (getLoc (loc), addr, isVolatile);
611+ cgm.decorateOperationWithTBAA (loadOp, tbaaInfo);
612+
600613 if (!ty->isBooleanType () && ty->hasBooleanRepresentation ())
601614 cgm.errorNYI (" emitLoadOfScalar: boolean type with boolean representation" );
602615
616+
603617 return loadOp;
604618}
605619
606620mlir::Value CIRGenFunction::emitLoadOfScalar (LValue lvalue,
607621 SourceLocation loc) {
608622 assert (!cir::MissingFeatures::opLoadStoreNontemporal ());
609- assert (!cir::MissingFeatures::opLoadStoreTbaa ());
610623 return emitLoadOfScalar (lvalue.getAddress (), lvalue.isVolatile (),
611- lvalue.getType (), loc, lvalue.getBaseInfo ());
624+ lvalue.getType (), loc, lvalue.getBaseInfo (), lvalue. getTBAAInfo () );
612625}
613626
614627// / Given an expression that represents a value lvalue, this
@@ -2285,17 +2298,19 @@ RValue CIRGenFunction::emitReferenceBindingToExpr(const Expr *e) {
22852298}
22862299
22872300Address CIRGenFunction::emitLoadOfReference (LValue refLVal, mlir::Location loc,
2288- LValueBaseInfo *pointeeBaseInfo) {
2301+ LValueBaseInfo *pointeeBaseInfo, TBAAAccessInfo *pointeeTbaaInfo ) {
22892302 if (refLVal.isVolatile ())
22902303 cgm.errorNYI (loc, " load of volatile reference" );
22912304
2305+ QualType pointeeType = refLVal.getType ()->getPointeeType ();
22922306 cir::LoadOp load =
22932307 cir::LoadOp::create (builder, loc, refLVal.getAddress ().getElementType (),
22942308 refLVal.getAddress ().getPointer ());
22952309
2296- assert (!cir::MissingFeatures::opTBAA ());
2310+ cgm.decorateOperationWithTBAA (load, refLVal.getTBAAInfo ());
2311+ if (pointeeTbaaInfo)
2312+ *pointeeTbaaInfo = cgm.getTBAAAccessInfo (pointeeType);
22972313
2298- QualType pointeeType = refLVal.getType ()->getPointeeType ();
22992314 CharUnits align = cgm.getNaturalTypeAlignment (pointeeType, pointeeBaseInfo);
23002315 return Address (load, convertTypeForMem (pointeeType), align);
23012316}
@@ -2306,10 +2321,10 @@ LValue CIRGenFunction::emitLoadOfReferenceLValue(Address refAddr,
23062321 AlignmentSource source) {
23072322 LValue refLVal = makeAddrLValue (refAddr, refTy, LValueBaseInfo (source));
23082323 LValueBaseInfo pointeeBaseInfo;
2309- assert (! cir::MissingFeatures::opTBAA ()) ;
2310- Address pointeeAddr = emitLoadOfReference (refLVal, loc, &pointeeBaseInfo);
2324+ TBAAAccessInfo tbaaAccessInfo ;
2325+ Address pointeeAddr = emitLoadOfReference (refLVal, loc, &pointeeBaseInfo, &tbaaAccessInfo );
23112326 return makeAddrLValue (pointeeAddr, refLVal.getType ()->getPointeeType (),
2312- pointeeBaseInfo);
2327+ pointeeBaseInfo, tbaaAccessInfo );
23132328}
23142329
23152330void CIRGenFunction::emitTrap (mlir::Location loc, bool createNewBlock) {
0 commit comments