This repository was archived by the owner on Feb 21, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 195
Expand file tree
/
Copy pathItaniumCXXABI.cpp
More file actions
677 lines (578 loc) 路 28.1 KB
/
Copy pathItaniumCXXABI.cpp
File metadata and controls
677 lines (578 loc) 路 28.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
//===------- ItaniumCXXABI.cpp - Emit CIR code Itanium-specific code -----===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This provides CIR lowering logic targeting the Itanium C++ ABI. The class in
// this file generates records that follow the Itanium C++ ABI, which is
// documented at:
// https://itanium-cxx-abi.github.io/cxx-abi/abi.html
// https://itanium-cxx-abi.github.io/cxx-abi/abi-eh.html
//
// It also supports the closely-related ARM ABI, documented at:
// https://developer.arm.com/documentation/ihi0041/g/
//
// This file partially mimics clang/lib/CodeGen/ItaniumCXXABI.cpp. The queries
// are adapted to operate on the CIR dialect, however.
//
//===----------------------------------------------------------------------===//
#include "../LoweringPrepareCXXABI.h"
#include "CIRCXXABI.h"
#include "LowerModule.h"
#include "mlir/Dialect/LLVMIR/LLVMDialect.h"
#include "llvm/Support/ErrorHandling.h"
namespace cir {
namespace {
class ItaniumCXXABI : public CIRCXXABI {
protected:
enum class VTableComponentLayout {
/// Components in the vtable are pointers to other records/functions.
Pointer,
/// Components in the vtable are relative offsets between the vtable and the
/// other records/functions.
Relative,
};
bool UseARMMethodPtrABI;
bool UseARMGuardVarABI;
bool Use32BitVTableOffsetABI;
VTableComponentLayout VTComponentLayout;
public:
ItaniumCXXABI(
LowerModule &LM, bool UseARMMethodPtrABI = false,
bool UseARMGuardVarABI = false,
VTableComponentLayout VTComponentLayout = VTableComponentLayout::Pointer)
: CIRCXXABI(LM), UseARMMethodPtrABI(UseARMMethodPtrABI),
UseARMGuardVarABI(UseARMGuardVarABI), Use32BitVTableOffsetABI(false),
VTComponentLayout(VTComponentLayout) {}
bool classifyReturnType(LowerFunctionInfo &FI) const override;
// FIXME(cir): This expects a CXXRecordDecl! Not any record type.
RecordArgABI getRecordArgABI(const RecordType RD) const override {
cir_cconv_assert(!cir::MissingFeatures::recordDeclIsCXXDecl());
// If C++ prohibits us from making a copy, pass by address.
cir_cconv_assert(!cir::MissingFeatures::recordDeclCanPassInRegisters());
return RAA_Default;
}
mlir::Type
lowerDataMemberType(cir::DataMemberType type,
const mlir::TypeConverter &typeConverter) const override;
mlir::Type
lowerMethodType(cir::MethodType type,
const mlir::TypeConverter &typeConverter) const override;
mlir::TypedAttr lowerDataMemberConstant(
cir::DataMemberAttr attr, const mlir::DataLayout &layout,
const mlir::TypeConverter &typeConverter) const override;
mlir::TypedAttr
lowerMethodConstant(cir::MethodAttr attr, const mlir::DataLayout &layout,
const mlir::TypeConverter &typeConverter) const override;
mlir::Operation *
lowerGetRuntimeMember(cir::GetRuntimeMemberOp op, mlir::Type loweredResultTy,
mlir::Value loweredAddr, mlir::Value loweredMember,
mlir::OpBuilder &builder) const override;
void lowerGetMethod(cir::GetMethodOp op, mlir::Value (&loweredResults)[2],
mlir::Value loweredMethod, mlir::Value loweredObjectPtr,
mlir::ConversionPatternRewriter &rewriter) const override;
mlir::Value lowerBaseDataMember(cir::BaseDataMemberOp op,
mlir::Value loweredSrc,
mlir::OpBuilder &builder) const override;
mlir::Value lowerDerivedDataMember(cir::DerivedDataMemberOp op,
mlir::Value loweredSrc,
mlir::OpBuilder &builder) const override;
mlir::Value lowerBaseMethod(cir::BaseMethodOp op, mlir::Value loweredSrc,
mlir::OpBuilder &builder) const override;
mlir::Value lowerDerivedMethod(cir::DerivedMethodOp op,
mlir::Value loweredSrc,
mlir::OpBuilder &builder) const override;
mlir::Value lowerDataMemberCmp(cir::CmpOp op, mlir::Value loweredLhs,
mlir::Value loweredRhs,
mlir::OpBuilder &builder) const override;
mlir::Value lowerMethodCmp(cir::CmpOp op, mlir::Value loweredLhs,
mlir::Value loweredRhs,
mlir::OpBuilder &builder) const override;
mlir::Value lowerDataMemberBitcast(cir::CastOp op, mlir::Type loweredDstTy,
mlir::Value loweredSrc,
mlir::OpBuilder &builder) const override;
mlir::Value
lowerDataMemberToBoolCast(cir::CastOp op, mlir::Value loweredSrc,
mlir::OpBuilder &builder) const override;
mlir::Value lowerMethodBitcast(cir::CastOp op, mlir::Type loweredDstTy,
mlir::Value loweredSrc,
mlir::OpBuilder &builder) const override;
mlir::Value lowerMethodToBoolCast(cir::CastOp op, mlir::Value loweredSrc,
mlir::OpBuilder &builder) const override;
};
} // namespace
bool ItaniumCXXABI::classifyReturnType(LowerFunctionInfo &FI) const {
const RecordType RD = mlir::dyn_cast<RecordType>(FI.getReturnType());
if (!RD)
return false;
// If C++ prohibits us from making a copy, return by address.
if (cir::MissingFeatures::recordDeclCanPassInRegisters())
cir_cconv_unreachable("NYI");
return false;
}
static cir::IntType getPtrDiffCIRTy(LowerModule &lowerMod) {
const clang::TargetInfo &target = lowerMod.getTarget();
clang::TargetInfo::IntType ptrdiffTy =
target.getPtrDiffType(clang::LangAS::Default);
return cir::IntType::get(lowerMod.getMLIRContext(),
target.getTypeWidth(ptrdiffTy),
target.isTypeSigned(ptrdiffTy));
}
mlir::Type ItaniumCXXABI::lowerDataMemberType(
cir::DataMemberType type, const mlir::TypeConverter &typeConverter) const {
// Itanium C++ ABI 2.3.1:
// A data member pointer is represented as the data member's offset in bytes
// from the address point of an object of the base type, as a ptrdiff_t.
return getPtrDiffCIRTy(LM);
}
mlir::Type
ItaniumCXXABI::lowerMethodType(cir::MethodType type,
const mlir::TypeConverter &typeConverter) const {
// Itanium C++ ABI 2.3.2:
// In all representations, the basic ABI properties of member function
// pointer types are those of the following class, where fnptr_t is the
// appropriate function-pointer type for a member function of this type:
//
// struct {
// fnptr_t ptr;
// ptrdiff_t adj;
// };
cir::IntType ptrdiffCIRTy = getPtrDiffCIRTy(LM);
// Note that clang CodeGen emits struct{ptrdiff_t, ptrdiff_t} for member
// function pointers. Let's follow this approach.
return cir::RecordType::get(type.getContext(), {ptrdiffCIRTy, ptrdiffCIRTy},
/*packed=*/false, /*padded=*/false,
cir::RecordType::Struct);
}
mlir::TypedAttr ItaniumCXXABI::lowerDataMemberConstant(
cir::DataMemberAttr attr, const mlir::DataLayout &layout,
const mlir::TypeConverter &typeConverter) const {
uint64_t memberOffset;
if (attr.isNullPtr()) {
// Itanium C++ ABI 2.3:
// A NULL pointer is represented as -1.
memberOffset = -1ull;
} else {
// Itanium C++ ABI 2.3:
// A pointer to data member is an offset from the base address of
// the class object containing it, represented as a ptrdiff_t
auto memberIndex = attr.getMemberIndex().value();
memberOffset =
attr.getType().getClsTy().getElementOffset(layout, memberIndex);
}
mlir::Type abiTy = lowerDataMemberType(attr.getType(), typeConverter);
return cir::IntAttr::get(abiTy, memberOffset);
}
mlir::TypedAttr ItaniumCXXABI::lowerMethodConstant(
cir::MethodAttr attr, const mlir::DataLayout &layout,
const mlir::TypeConverter &typeConverter) const {
cir::IntType ptrdiffCIRTy = getPtrDiffCIRTy(LM);
auto loweredMethodTy = mlir::cast<cir::RecordType>(
lowerMethodType(attr.getType(), typeConverter));
auto zero = cir::IntAttr::get(ptrdiffCIRTy, 0);
auto adj = cir::IntAttr::get(ptrdiffCIRTy, attr.getAdjustment());
// Itanium C++ ABI 2.3.2:
// In all representations, the basic ABI properties of member function
// pointer types are those of the following class, where fnptr_t is the
// appropriate function-pointer type for a member function of this type:
//
// struct {
// fnptr_t ptr;
// ptrdiff_t adj;
// };
if (attr.isNull()) {
// Itanium C++ ABI 2.3.2:
//
// In the standard representation, a null member function pointer is
// represented with ptr set to a null pointer. The value of adj is
// unspecified for null member function pointers.
//
// clang CodeGen emits struct{null, null} for null member function pointers.
// Let's do the same here.
return cir::ConstRecordAttr::get(
loweredMethodTy, mlir::ArrayAttr::get(attr.getContext(), {zero, zero}));
}
if (attr.isVirtual()) {
if (UseARMMethodPtrABI) {
// ARM C++ ABI 3.2.1:
// This ABI specifies that adj contains twice the this
// adjustment, plus 1 if the member function is virtual. The
// least significant bit of adj then makes exactly the same
// discrimination as the least significant bit of ptr does for
// Itanium.
llvm_unreachable("ARM method ptr abi NYI");
}
// Itanium C++ ABI 2.3.2:
//
// In the standard representation, a member function pointer for a
// virtual function is represented with ptr set to 1 plus the function's
// v-table entry offset (in bytes), converted to a function pointer as if
// by reinterpret_cast<fnptr_t>(uintfnptr_t(1 + offset)), where
// uintfnptr_t is an unsigned integer of the same size as fnptr_t.
auto ptr =
cir::IntAttr::get(ptrdiffCIRTy, 1 + attr.getVtableOffset().value());
return cir::ConstRecordAttr::get(
loweredMethodTy, mlir::ArrayAttr::get(attr.getContext(), {ptr, adj}));
}
// Itanium C++ ABI 2.3.2:
//
// A member function pointer for a non-virtual member function is
// represented with ptr set to a pointer to the function, using the base
// ABI's representation of function pointers.
auto ptr = cir::GlobalViewAttr::get(ptrdiffCIRTy, attr.getSymbol().value());
return cir::ConstRecordAttr::get(
loweredMethodTy, mlir::ArrayAttr::get(attr.getContext(), {ptr, adj}));
}
mlir::Operation *ItaniumCXXABI::lowerGetRuntimeMember(
cir::GetRuntimeMemberOp op, mlir::Type loweredResultTy,
mlir::Value loweredAddr, mlir::Value loweredMember,
mlir::OpBuilder &builder) const {
auto byteTy = IntType::get(op.getContext(), 8, true);
auto bytePtrTy = PointerType::get(
byteTy, mlir::cast<PointerType>(op.getAddr().getType()).getAddrSpace());
auto objectBytesPtr = CastOp::create(builder, op.getLoc(), bytePtrTy,
CastKind::bitcast, op.getAddr());
auto memberBytesPtr = PtrStrideOp::create(builder, op.getLoc(), bytePtrTy,
objectBytesPtr, loweredMember);
return CastOp::create(builder, op.getLoc(), op.getType(), CastKind::bitcast,
memberBytesPtr);
}
void ItaniumCXXABI::lowerGetMethod(
cir::GetMethodOp op, mlir::Value (&loweredResults)[2],
mlir::Value loweredMethod, mlir::Value loweredObjectPtr,
mlir::ConversionPatternRewriter &rewriter) const {
// In the Itanium and ARM ABIs, method pointers have the form:
// struct { ptrdiff_t ptr; ptrdiff_t adj; } memptr;
//
// In the Itanium ABI:
// - method pointers are virtual if (memptr.ptr & 1) is nonzero
// - the this-adjustment is (memptr.adj)
// - the virtual offset is (memptr.ptr - 1)
//
// In the ARM ABI:
// - method pointers are virtual if (memptr.adj & 1) is nonzero
// - the this-adjustment is (memptr.adj >> 1)
// - the virtual offset is (memptr.ptr)
// ARM uses 'adj' for the virtual flag because Thumb functions
// may be only single-byte aligned.
//
// If the member is virtual, the adjusted 'this' pointer points
// to a vtable pointer from which the virtual offset is applied.
//
// If the member is non-virtual, memptr.ptr is the address of
// the function to call.
mlir::Value &callee = loweredResults[0];
mlir::Value &adjustedThis = loweredResults[1];
mlir::Type calleePtrTy = op.getCallee().getType();
cir::IntType ptrdiffCIRTy = getPtrDiffCIRTy(LM);
mlir::Value ptrdiffOne = cir::ConstantOp::create(
rewriter, op.getLoc(), cir::IntAttr::get(ptrdiffCIRTy, 1));
mlir::Value adj = cir::ExtractMemberOp::create(
rewriter, op.getLoc(), ptrdiffCIRTy, loweredMethod, 1);
if (UseARMMethodPtrABI)
llvm_unreachable("ARM method ptr abi NYI");
// Apply the adjustment to the 'this' pointer.
mlir::Type thisVoidPtrTy =
cir::PointerType::get(cir::VoidType::get(rewriter.getContext()),
op.getObject().getType().getAddrSpace());
mlir::Value thisVoidPtr =
cir::CastOp::create(rewriter, op.getLoc(), thisVoidPtrTy,
cir::CastKind::bitcast, loweredObjectPtr);
adjustedThis = cir::PtrStrideOp::create(rewriter, op.getLoc(), thisVoidPtrTy,
thisVoidPtr, adj);
// Load the "ptr" field of the member function pointer and determine if it
// points to a virtual function.
mlir::Value methodPtrField = cir::ExtractMemberOp::create(
rewriter, op.getLoc(), ptrdiffCIRTy, loweredMethod, 0);
mlir::Value virtualBit = cir::BinOp::create(
rewriter, op.getLoc(), cir::BinOpKind::And, methodPtrField, ptrdiffOne);
mlir::Value isVirtual;
if (UseARMMethodPtrABI)
llvm_unreachable("ARM method ptr abi NYI");
else
isVirtual = cir::CmpOp::create(rewriter, op.getLoc(), cir::CmpOpKind::eq,
virtualBit, ptrdiffOne);
assert(!MissingFeatures::emitCFICheck());
assert(!MissingFeatures::emitVFEInfo());
assert(!MissingFeatures::emitWPDInfo());
// See their original definitions in
// ItaniumCXXABI::EmitLoadOfMemberFunctionPointer in file
// clang/lib/CodeGen/ItaniumCXXABI.cpp.
bool shouldEmitCFICheck = false;
bool shouldEmitVFEInfo =
LM.getContext().getCodeGenOpts().VirtualFunctionElimination;
bool shouldEmitWPDInfo = LM.getContext().getCodeGenOpts().WholeProgramVTables;
mlir::Block *currBlock = rewriter.getInsertionBlock();
mlir::Block *continueBlock =
rewriter.splitBlock(currBlock, rewriter.getInsertionPoint());
continueBlock->addArgument(calleePtrTy, op.getLoc());
mlir::Block *virtualBlock = rewriter.createBlock(continueBlock);
mlir::Block *nonVirtualBlock = rewriter.createBlock(continueBlock);
rewriter.setInsertionPointToEnd(currBlock);
cir::BrCondOp::create(rewriter, op.getLoc(), isVirtual, virtualBlock,
nonVirtualBlock);
auto buildVirtualBranch = [&] {
mlir::OpBuilder::InsertionGuard guard(rewriter);
rewriter.setInsertionPointToStart(virtualBlock);
// Load vtable pointer.
// Note that vtable pointer always point to the global address space.
auto vtablePtrTy = cir::PointerType::get(
cir::IntType::get(rewriter.getContext(), 8, true));
auto vtablePtrPtrTy = cir::PointerType::get(
vtablePtrTy, op.getObject().getType().getAddrSpace());
auto vtablePtrPtr =
cir::CastOp::create(rewriter, op.getLoc(), vtablePtrPtrTy,
cir::CastKind::bitcast, loweredObjectPtr);
mlir::Value vtablePtr = cir::LoadOp::create(
rewriter, op.getLoc(), vtablePtrPtr, /*isDeref=*/false,
/*isVolatile=*/false,
/*isNontemporal=*/false,
/*alignment=*/mlir::IntegerAttr(),
/*sync_scope=*/cir::SyncScopeKindAttr{},
/*mem_order=*/cir::MemOrderAttr(),
/*tbaa=*/mlir::ArrayAttr());
// Get the vtable offset.
mlir::Value vtableOffset = methodPtrField;
if (!UseARMMethodPtrABI)
vtableOffset = cir::BinOp::create(
rewriter, op.getLoc(), cir::BinOpKind::Sub, vtableOffset, ptrdiffOne);
if (Use32BitVTableOffsetABI)
llvm_unreachable("NYI");
if (shouldEmitCFICheck || shouldEmitVFEInfo || shouldEmitWPDInfo)
llvm_unreachable("NYI");
// Apply the offset to the vtable pointer and get the pointer to the target
// virtual function. Then load that pointer to get the callee.
mlir::Value funcPtr;
if (shouldEmitVFEInfo)
llvm_unreachable("NYI");
else {
if (shouldEmitCFICheck || shouldEmitWPDInfo)
llvm_unreachable("NYI");
if (VTComponentLayout == VTableComponentLayout::Relative)
llvm_unreachable("NYI");
else {
mlir::Value vfpAddr = cir::PtrStrideOp::create(
rewriter, op.getLoc(), vtablePtrTy, vtablePtr, vtableOffset);
auto vfpPtrTy = cir::PointerType::get(calleePtrTy);
mlir::Value vfpPtr = cir::CastOp::create(
rewriter, op.getLoc(), vfpPtrTy, cir::CastKind::bitcast, vfpAddr);
funcPtr = cir::LoadOp::create(rewriter, op.getLoc(), vfpPtr,
/*isDeref=*/false, /*isVolatile=*/false,
/*isNontemporal=*/false,
/*alignment=*/mlir::IntegerAttr(),
/*sync_scope=*/cir::SyncScopeKindAttr{},
/*mem_order=*/cir::MemOrderAttr(),
/*tbaa=*/mlir::ArrayAttr());
}
}
if (shouldEmitCFICheck)
llvm_unreachable("NYI");
cir::BrOp::create(rewriter, op.getLoc(), continueBlock, funcPtr);
};
auto buildNonVirtualBranch = [&] {
mlir::OpBuilder::InsertionGuard guard(rewriter);
rewriter.setInsertionPointToStart(nonVirtualBlock);
mlir::Value funcPtr =
cir::CastOp::create(rewriter, op.getLoc(), calleePtrTy,
cir::CastKind::int_to_ptr, methodPtrField);
if (shouldEmitCFICheck)
llvm_unreachable("NYI");
cir::BrOp::create(rewriter, op.getLoc(), continueBlock, funcPtr);
};
buildVirtualBranch();
buildNonVirtualBranch();
rewriter.setInsertionPointToStart(continueBlock);
callee = continueBlock->getArgument(0);
}
static mlir::Value lowerDataMemberCast(mlir::Operation *op,
mlir::Value loweredSrc,
std::int64_t offset,
bool isDerivedToBase,
mlir::OpBuilder &builder) {
if (offset == 0)
return loweredSrc;
auto nullValue = cir::ConstantOp::create(
builder, op->getLoc(), mlir::IntegerAttr::get(loweredSrc.getType(), -1));
auto isNull = cir::CmpOp::create(builder, op->getLoc(), cir::CmpOpKind::eq,
loweredSrc, nullValue);
auto offsetValue = cir::ConstantOp::create(
builder, op->getLoc(),
mlir::IntegerAttr::get(loweredSrc.getType(), offset));
auto binOpKind = isDerivedToBase ? cir::BinOpKind::Sub : cir::BinOpKind::Add;
auto adjustedPtr =
cir::BinOp::create(builder, op->getLoc(), loweredSrc.getType(), binOpKind,
loweredSrc, offsetValue);
return cir::SelectOp::create(builder, op->getLoc(), loweredSrc.getType(),
isNull, nullValue, adjustedPtr);
}
static mlir::Value lowerMethodCast(mlir::Operation *op, mlir::Value loweredSrc,
std::int64_t offset, bool isDerivedToBase,
LowerModule &lowerMod,
mlir::OpBuilder &builder) {
if (offset == 0)
return loweredSrc;
cir::IntType ptrdiffCIRTy = getPtrDiffCIRTy(lowerMod);
auto adjField = cir::ExtractMemberOp::create(builder, op->getLoc(),
ptrdiffCIRTy, loweredSrc, 1);
auto offsetValue = cir::ConstantOp::create(
builder, op->getLoc(), cir::IntAttr::get(ptrdiffCIRTy, offset));
auto binOpKind = isDerivedToBase ? cir::BinOpKind::Sub : cir::BinOpKind::Add;
auto adjustedAdjField = cir::BinOp::create(
builder, op->getLoc(), ptrdiffCIRTy, binOpKind, adjField, offsetValue);
return cir::InsertMemberOp::create(builder, op->getLoc(), loweredSrc, 1,
adjustedAdjField);
}
mlir::Value ItaniumCXXABI::lowerBaseDataMember(cir::BaseDataMemberOp op,
mlir::Value loweredSrc,
mlir::OpBuilder &builder) const {
return lowerDataMemberCast(op, loweredSrc, op.getOffset().getSExtValue(),
/*isDerivedToBase=*/true, builder);
}
mlir::Value
ItaniumCXXABI::lowerDerivedDataMember(cir::DerivedDataMemberOp op,
mlir::Value loweredSrc,
mlir::OpBuilder &builder) const {
return lowerDataMemberCast(op, loweredSrc, op.getOffset().getSExtValue(),
/*isDerivedToBase=*/false, builder);
}
mlir::Value ItaniumCXXABI::lowerBaseMethod(cir::BaseMethodOp op,
mlir::Value loweredSrc,
mlir::OpBuilder &builder) const {
return lowerMethodCast(op, loweredSrc, op.getOffset().getSExtValue(),
/*isDerivedToBase=*/true, LM, builder);
}
mlir::Value ItaniumCXXABI::lowerDerivedMethod(cir::DerivedMethodOp op,
mlir::Value loweredSrc,
mlir::OpBuilder &builder) const {
return lowerMethodCast(op, loweredSrc, op.getOffset().getSExtValue(),
/*isDerivedToBase=*/false, LM, builder);
}
mlir::Value ItaniumCXXABI::lowerDataMemberCmp(cir::CmpOp op,
mlir::Value loweredLhs,
mlir::Value loweredRhs,
mlir::OpBuilder &builder) const {
return cir::CmpOp::create(builder, op.getLoc(), op.getKind(), loweredLhs,
loweredRhs);
}
mlir::Value ItaniumCXXABI::lowerMethodCmp(cir::CmpOp op, mlir::Value loweredLhs,
mlir::Value loweredRhs,
mlir::OpBuilder &builder) const {
assert(op.getKind() == cir::CmpOpKind::eq ||
op.getKind() == cir::CmpOpKind::ne);
cir::IntType ptrdiffCIRTy = getPtrDiffCIRTy(LM);
mlir::Value ptrdiffZero = cir::ConstantOp::create(
builder, op.getLoc(), cir::IntAttr::get(ptrdiffCIRTy, 0));
mlir::Value lhsPtrField = cir::ExtractMemberOp::create(
builder, op.getLoc(), ptrdiffCIRTy, loweredLhs, 0);
mlir::Value rhsPtrField = cir::ExtractMemberOp::create(
builder, op.getLoc(), ptrdiffCIRTy, loweredRhs, 0);
mlir::Value ptrCmp = cir::CmpOp::create(builder, op.getLoc(), op.getKind(),
lhsPtrField, rhsPtrField);
mlir::Value ptrCmpToNull = cir::CmpOp::create(
builder, op.getLoc(), op.getKind(), lhsPtrField, ptrdiffZero);
mlir::Value lhsAdjField = cir::ExtractMemberOp::create(
builder, op.getLoc(), ptrdiffCIRTy, loweredLhs, 1);
mlir::Value rhsAdjField = cir::ExtractMemberOp::create(
builder, op.getLoc(), ptrdiffCIRTy, loweredRhs, 1);
mlir::Value adjCmp = cir::CmpOp::create(builder, op.getLoc(), op.getKind(),
lhsAdjField, rhsAdjField);
// We use cir.select to represent "||" and "&&" operations below:
// - cir.select if %a then %b else false => %a && %b
// - cir.select if %a then true else %b => %a || %b
// TODO: Do we need to invent dedicated "cir.logical_or" and "cir.logical_and"
// operations for this?
CIRBaseBuilderTy cirBuilder(builder);
mlir::Value trueValue = cirBuilder.getTrue(op.getLoc());
mlir::Value falseValue = cirBuilder.getFalse(op.getLoc());
auto create_and = [&](mlir::Value lhs, mlir::Value rhs) {
return cir::SelectOp::create(builder, op.getLoc(), lhs, rhs, falseValue);
};
auto create_or = [&](mlir::Value lhs, mlir::Value rhs) {
return cir::SelectOp::create(builder, op.getLoc(), lhs, trueValue, rhs);
};
mlir::Value result;
if (op.getKind() == cir::CmpOpKind::eq) {
// (lhs.ptr == null || lhs.adj == rhs.adj) && lhs.ptr == rhs.ptr
result = create_and(create_or(ptrCmpToNull, adjCmp), ptrCmp);
} else {
// (lhs.ptr != null && lhs.adj != rhs.adj) || lhs.ptr != rhs.ptr
result = create_or(create_and(ptrCmpToNull, adjCmp), ptrCmp);
}
return result;
}
mlir::Value
ItaniumCXXABI::lowerDataMemberBitcast(cir::CastOp op, mlir::Type loweredDstTy,
mlir::Value loweredSrc,
mlir::OpBuilder &builder) const {
return cir::CastOp::create(builder, op.getLoc(), loweredDstTy,
cir::CastKind::bitcast, loweredSrc);
}
mlir::Value
ItaniumCXXABI::lowerDataMemberToBoolCast(cir::CastOp op, mlir::Value loweredSrc,
mlir::OpBuilder &builder) const {
// Itanium C++ ABI 2.3:
// A NULL pointer is represented as -1.
auto nullAttr = cir::IntAttr::get(getPtrDiffCIRTy(LM), -1);
auto nullValue = cir::ConstantOp::create(builder, op.getLoc(), nullAttr);
return cir::CmpOp::create(builder, op.getLoc(), cir::CmpOpKind::ne,
loweredSrc, nullValue);
}
mlir::Value ItaniumCXXABI::lowerMethodBitcast(cir::CastOp op,
mlir::Type loweredDstTy,
mlir::Value loweredSrc,
mlir::OpBuilder &builder) const {
return loweredSrc;
}
mlir::Value
ItaniumCXXABI::lowerMethodToBoolCast(cir::CastOp op, mlir::Value loweredSrc,
mlir::OpBuilder &builder) const {
// Itanium C++ ABI 2.3.2:
//
// In the standard representation, a null member function pointer is
// represented with ptr set to a null pointer. The value of adj is
// unspecified for null member function pointers.
cir::IntType ptrdiffCIRTy = getPtrDiffCIRTy(LM);
mlir::Value ptrdiffZero = cir::ConstantOp::create(
builder, op.getLoc(), cir::IntAttr::get(ptrdiffCIRTy, 0));
mlir::Value ptrField = cir::ExtractMemberOp::create(
builder, op.getLoc(), ptrdiffCIRTy, loweredSrc, 0);
return cir::CmpOp::create(builder, op.getLoc(), cir::CmpOpKind::ne, ptrField,
ptrdiffZero);
}
CIRCXXABI *CreateItaniumCXXABI(LowerModule &LM) {
switch (LM.getCXXABIKind()) {
// Note that AArch64 uses the generic ItaniumCXXABI class since it doesn't
// include the other 32-bit ARM oddities: constructor/destructor return values
// and array cookies.
case clang::TargetCXXABI::GenericAArch64:
case clang::TargetCXXABI::AppleARM64:
// TODO: this isn't quite right, clang uses AppleARM64CXXABI which inherits
// from ARMCXXABI. We'll have to follow suit.
cir_cconv_assert(!cir::MissingFeatures::appleArm64CXXABI());
return new ItaniumCXXABI(LM, /*UseARMMethodPtrABI=*/true,
/*UseARMGuardVarABI=*/true);
case clang::TargetCXXABI::GenericItanium:
return new ItaniumCXXABI(LM);
case clang::TargetCXXABI::Microsoft:
cir_cconv_unreachable("Microsoft ABI is not Itanium-based");
default:
cir_cconv_unreachable("NYI");
}
cir_cconv_unreachable("bad ABI kind");
}
} // namespace cir
// FIXME(cir): Merge this into the CIRCXXABI class above.
class LoweringPrepareItaniumCXXABI : public cir::LoweringPrepareCXXABI {
public:
mlir::Value lowerDynamicCast(cir::CIRBaseBuilderTy &builder,
clang::ASTContext &astCtx,
cir::DynamicCastOp op) override;
mlir::Value lowerVAArg(cir::CIRBaseBuilderTy &builder, cir::VAArgOp op,
const cir::CIRDataLayout &datalayout) override;
mlir::Value lowerDeleteArray(cir::CIRBaseBuilderTy &builder,
cir::DeleteArrayOp op,
const cir::CIRDataLayout &datalayout) override;
};