clang 23.0.0git
CIRGenFunction.cpp
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// Internal per-function state used for AST-to-ClangIR code gen
10//
11//===----------------------------------------------------------------------===//
12
13#include "CIRGenFunction.h"
14
15#include "CIRGenCXXABI.h"
16#include "CIRGenCall.h"
17#include "CIRGenValue.h"
18#include "mlir/IR/Location.h"
19#include "clang/AST/Attr.h"
20#include "clang/AST/ExprCXX.h"
24#include "llvm/ADT/ScopeExit.h"
25#include "llvm/IR/FPEnv.h"
26
27#include <cassert>
28
29namespace clang::CIRGen {
30
32 bool suppressNewContext)
33 : CIRGenTypeCache(cgm), cgm{cgm}, builder(builder) {
34 ehStack.setCGF(this);
35}
36
38
39// This is copied from clang/lib/CodeGen/CodeGenFunction.cpp
41 type = type.getCanonicalType();
42 while (true) {
43 switch (type->getTypeClass()) {
44#define TYPE(name, parent)
45#define ABSTRACT_TYPE(name, parent)
46#define NON_CANONICAL_TYPE(name, parent) case Type::name:
47#define DEPENDENT_TYPE(name, parent) case Type::name:
48#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(name, parent) case Type::name:
49#include "clang/AST/TypeNodes.inc"
50 llvm_unreachable("non-canonical or dependent type in IR-generation");
51
52 case Type::Auto:
53 case Type::DeducedTemplateSpecialization:
54 llvm_unreachable("undeduced type in IR-generation");
55
56 // Various scalar types.
57 case Type::Builtin:
58 case Type::Pointer:
59 case Type::BlockPointer:
60 case Type::LValueReference:
61 case Type::RValueReference:
62 case Type::MemberPointer:
63 case Type::Vector:
64 case Type::ExtVector:
65 case Type::ConstantMatrix:
66 case Type::FunctionProto:
67 case Type::FunctionNoProto:
68 case Type::Enum:
69 case Type::ObjCObjectPointer:
70 case Type::Pipe:
71 case Type::BitInt:
72 case Type::OverflowBehavior:
73 case Type::HLSLAttributedResource:
74 case Type::HLSLInlineSpirv:
75 return cir::TEK_Scalar;
76
77 // Complexes.
78 case Type::Complex:
79 return cir::TEK_Complex;
80
81 // Arrays, records, and Objective-C objects.
82 case Type::ConstantArray:
83 case Type::IncompleteArray:
84 case Type::VariableArray:
85 case Type::Record:
86 case Type::ObjCObject:
87 case Type::ObjCInterface:
88 case Type::ArrayParameter:
89 return cir::TEK_Aggregate;
90
91 // We operate on atomic values according to their underlying type.
92 case Type::Atomic:
93 type = cast<AtomicType>(type)->getValueType();
94 continue;
95 }
96 llvm_unreachable("unknown type kind!");
97 }
98}
99
101 return cgm.getTypes().convertTypeForMem(t);
102}
103
105 return cgm.getTypes().convertType(t);
106}
107
109 // Some AST nodes might contain invalid source locations (e.g.
110 // CXXDefaultArgExpr), workaround that to still get something out.
111 if (srcLoc.isValid()) {
113 PresumedLoc pLoc = sm.getPresumedLoc(srcLoc);
114 StringRef filename = pLoc.getFilename();
115 return mlir::FileLineColLoc::get(builder.getStringAttr(filename),
116 pLoc.getLine(), pLoc.getColumn());
117 }
118 // We expect to have a currSrcLoc set, so we assert here, but it isn't
119 // critical for the correctness of compilation, so in non-assert builds
120 // we fallback on using an unknown location.
121 assert(currSrcLoc && "expected to inherit some source location");
122 if (currSrcLoc)
123 return *currSrcLoc;
124 // We're brave, but time to give up.
125 return builder.getUnknownLoc();
126}
127
128mlir::Location CIRGenFunction::getLoc(SourceRange srcLoc) {
129 // Some AST nodes might contain invalid source locations (e.g.
130 // CXXDefaultArgExpr), workaround that to still get something out.
131 if (srcLoc.isValid()) {
132 mlir::Location beg = getLoc(srcLoc.getBegin());
133 mlir::Location end = getLoc(srcLoc.getEnd());
134 SmallVector<mlir::Location, 2> locs = {beg, end};
135 mlir::Attribute metadata;
136 return mlir::FusedLoc::get(locs, metadata, &getMLIRContext());
137 }
138 // We expect to have a currSrcLoc set, so we assert here, but it isn't
139 // critical for the correctness of compilation, so in non-assert builds
140 // we fallback on using an unknown location.
141 assert(currSrcLoc && "expected to inherit some source location");
142 if (currSrcLoc)
143 return *currSrcLoc;
144 // We're brave, but time to give up.
145 return builder.getUnknownLoc();
146}
147
148mlir::Location CIRGenFunction::getLoc(mlir::Location lhs, mlir::Location rhs) {
149 SmallVector<mlir::Location, 2> locs = {lhs, rhs};
150 mlir::Attribute metadata;
151 return mlir::FusedLoc::get(locs, metadata, &getMLIRContext());
152}
153
154bool CIRGenFunction::containsLabel(const Stmt *s, bool ignoreCaseStmts) {
155 // Null statement, not a label!
156 if (!s)
157 return false;
158
159 // If this is a label, we have to emit the code, consider something like:
160 // if (0) { ... foo: bar(); } goto foo;
161 //
162 // TODO: If anyone cared, we could track __label__'s, since we know that you
163 // can't jump to one from outside their declared region.
164 if (isa<LabelStmt>(s))
165 return true;
166
167 // If this is a case/default statement, and we haven't seen a switch, we
168 // have to emit the code.
169 if (isa<SwitchCase>(s) && !ignoreCaseStmts)
170 return true;
171
172 // If this is a switch statement, we want to ignore case statements when we
173 // recursively process the sub-statements of the switch. If we haven't
174 // encountered a switch statement, we treat case statements like labels, but
175 // if we are processing a switch statement, case statements are expected.
176 if (isa<SwitchStmt>(s))
177 ignoreCaseStmts = true;
178
179 // Scan subexpressions for verboten labels.
180 return std::any_of(s->child_begin(), s->child_end(),
181 [=](const Stmt *subStmt) {
182 return containsLabel(subStmt, ignoreCaseStmts);
183 });
184}
185
186/// If the specified expression does not fold to a constant, or if it does but
187/// contains a label, return false. If it constant folds return true and set
188/// the boolean result in Result.
189bool CIRGenFunction::constantFoldsToBool(const Expr *cond, bool &resultBool,
190 bool allowLabels) {
191 llvm::APSInt resultInt;
192 if (!constantFoldsToSimpleInteger(cond, resultInt, allowLabels))
193 return false;
194
195 resultBool = resultInt.getBoolValue();
196 return true;
197}
198
199/// If the specified expression does not fold to a constant, or if it does
200/// fold but contains a label, return false. If it constant folds, return
201/// true and set the folded value.
203 llvm::APSInt &resultInt,
204 bool allowLabels) {
205 // FIXME: Rename and handle conversion of other evaluatable things
206 // to bool.
207 Expr::EvalResult result;
208 if (!cond->EvaluateAsInt(result, getContext()))
209 return false; // Not foldable, not integer or not fully evaluatable.
210
211 llvm::APSInt intValue = result.Val.getInt();
212 if (!allowLabels && containsLabel(cond))
213 return false; // Contains a label.
214
215 resultInt = intValue;
216 return true;
217}
218
219void CIRGenFunction::emitAndUpdateRetAlloca(QualType type, mlir::Location loc,
220 CharUnits alignment) {
221 if (!type->isVoidType()) {
222 Address allocaAddr = Address::invalid();
223 returnValue = createMemTemp(type, alignment, loc, "__retval", &allocaAddr);
224 fnRetAlloca = allocaAddr.getPointer();
225 }
226}
227
228void CIRGenFunction::declare(mlir::Value addrVal, const Decl *var, QualType ty,
229 mlir::Location loc, CharUnits alignment,
230 bool isParam) {
231 assert(isa<NamedDecl>(var) && "Needs a named decl");
232 assert(!symbolTable.count(var) && "not supposed to be available just yet");
233
234 Address addr(addrVal, alignment);
235 cir::AllocaOp allocaOp = addr.getUnderlyingAllocaOp();
236 assert(allocaOp && "expected cir::AllocaOp");
237
238 if (isParam)
239 allocaOp.setInitAttr(mlir::UnitAttr::get(&getMLIRContext()));
240 if (ty->isReferenceType() || ty.isConstQualified())
241 allocaOp.setConstantAttr(mlir::UnitAttr::get(&getMLIRContext()));
242
243 symbolTable.insert(var, addrVal);
244}
245
247 CIRGenBuilderTy &builder = cgf.builder;
248 LexicalScope *localScope = cgf.curLexScope;
249
250 // Process all return blocks — emit cir.return ops.
251 // TODO(cir): Handle returning from a switch statement through a cleanup
252 // block. We can't simply jump to the cleanup block, because the cleanup block
253 // is not part of the case region. Either reemit all cleanups in the return
254 // block or wait for MLIR structured control flow to support early exits.
256 for (mlir::Block *retBlock : localScope->getRetBlocks()) {
257 mlir::OpBuilder::InsertionGuard guard(builder);
258 builder.setInsertionPointToEnd(retBlock);
259 retBlocks.push_back(retBlock);
260 mlir::Location retLoc = localScope->getRetLoc(retBlock);
261 emitReturn(retLoc);
262 }
263
264 // Pop cleanup scopes from the EH stack. In CIR, this emits cleanup code
265 // into the cleanup regions of cir.cleanup.scope ops — no CFG-level cleanup
266 // blocks or branches are needed.
267 if (performCleanup) {
269 forceCleanup();
270 }
271
272 mlir::Block *curBlock = builder.getBlock();
273 if (isGlobalInit() && !curBlock)
274 return;
275 if (curBlock->mightHaveTerminator() && curBlock->getTerminator())
276 return;
277
278 // Get rid of any empty block at the end of the scope. An empty non-entry
279 // block is created when a terminator (return/break/continue) is followed
280 // by unreachable code.
281 bool isEntryBlock = builder.getInsertionBlock()->isEntryBlock();
282 if (!isEntryBlock && curBlock->empty()) {
283 curBlock->erase();
284 for (mlir::Block *retBlock : retBlocks) {
285 if (retBlock->getUses().empty())
286 retBlock->erase();
287 }
288 return;
289 }
290
291 if (localScope->depth == 0) {
292 // Reached the end of the function.
293 if (localScope->getRetBlocks().size() == 1) {
294 mlir::Block *retBlock = localScope->getRetBlocks()[0];
295 mlir::Location retLoc = localScope->getRetLoc(retBlock);
296 if (retBlock->getUses().empty()) {
297 retBlock->erase();
298 } else {
299 cir::BrOp::create(builder, retLoc, retBlock);
300 return;
301 }
302 }
303 emitImplicitReturn();
304 return;
305 }
306
307 // End of any local scope != function.
308 // Ternary ops have to deal with matching arms for yielding types
309 // and do return a value, it must do its own cir.yield insertion.
310 if (!localScope->isTernary() && !curBlock->mightHaveTerminator()) {
311 !retVal ? cir::YieldOp::create(builder, localScope->endLoc)
312 : cir::YieldOp::create(builder, localScope->endLoc, retVal);
313 }
314}
315
316cir::ReturnOp CIRGenFunction::LexicalScope::emitReturn(mlir::Location loc) {
317 CIRGenBuilderTy &builder = cgf.getBuilder();
318
319 auto fn = dyn_cast<cir::FuncOp>(cgf.curFn);
320 assert(fn && "emitReturn from non-function");
321
322 if (!fn.getFunctionType().hasVoidReturn()) {
323 // Load the value from `__retval` and return it via the `cir.return` op.
324 auto value = cir::LoadOp::create(
325 builder, loc, fn.getFunctionType().getReturnType(), *cgf.fnRetAlloca);
326 return cir::ReturnOp::create(builder, loc,
327 llvm::ArrayRef(value.getResult()));
328 }
329 return cir::ReturnOp::create(builder, loc);
330}
331
332// This is copied from CodeGenModule::MayDropFunctionReturn. This is a
333// candidate for sharing between CIRGen and CodeGen.
334static bool mayDropFunctionReturn(const ASTContext &astContext,
335 QualType returnType) {
336 // We can't just discard the return value for a record type with a complex
337 // destructor or a non-trivially copyable type.
338 if (const auto *classDecl = returnType->getAsCXXRecordDecl())
339 return classDecl->hasTrivialDestructor();
340 return returnType.isTriviallyCopyableType(astContext);
341}
342
343static bool previousOpIsNonYieldingCleanup(mlir::Block *block) {
344 if (block->empty())
345 return false;
346 mlir::Operation *op = &block->back();
347 auto cleanupScopeOp = mlir::dyn_cast<cir::CleanupScopeOp>(op);
348 if (!cleanupScopeOp)
349 return false;
350
351 // Check whether the body region of the cleanup scope exits via cir.yield.
352 // Exits via cir.return or cir.goto do not fall through to the operation
353 // following the cleanup scope, and exits via break, continue, and resume
354 // are not expected here.
355 for (mlir::Block &bodyBlock : cleanupScopeOp.getBodyRegion()) {
356 if (bodyBlock.mightHaveTerminator()) {
357 if (mlir::isa<cir::YieldOp>(bodyBlock.getTerminator()))
358 return false;
359 assert(!mlir::isa<cir::BreakOp>(bodyBlock.getTerminator()) &&
360 !mlir::isa<cir::ContinueOp>(bodyBlock.getTerminator()) &&
361 !mlir::isa<cir::ResumeOp>(bodyBlock.getTerminator()));
362 }
363 }
364 return true;
365}
366
367void CIRGenFunction::LexicalScope::emitImplicitReturn() {
368 CIRGenBuilderTy &builder = cgf.getBuilder();
369 LexicalScope *localScope = cgf.curLexScope;
370
371 const auto *fd = cast<clang::FunctionDecl>(cgf.curGD.getDecl());
372
373 // In C++, flowing off the end of a non-void function is always undefined
374 // behavior. In C, flowing off the end of a non-void function is undefined
375 // behavior only if the non-existent return value is used by the caller.
376 // That influences whether the terminating op is trap, unreachable, or
377 // return.
378 if (cgf.getLangOpts().CPlusPlus && !fd->hasImplicitReturnZero() &&
379 !cgf.sawAsmBlock && !fd->getReturnType()->isVoidType() &&
380 builder.getInsertionBlock() &&
381 !previousOpIsNonYieldingCleanup(builder.getInsertionBlock())) {
382 bool shouldEmitUnreachable =
383 cgf.cgm.getCodeGenOpts().StrictReturn ||
384 !mayDropFunctionReturn(fd->getASTContext(), fd->getReturnType());
385
386 if (shouldEmitUnreachable) {
388 if (cgf.cgm.getCodeGenOpts().OptimizationLevel == 0)
389 cir::TrapOp::create(builder, localScope->endLoc);
390 else
391 cir::UnreachableOp::create(builder, localScope->endLoc);
392 builder.clearInsertionPoint();
393 return;
394 }
395 }
396
397 (void)emitReturn(localScope->endLoc);
398}
399
401 LexicalScope *scope = this;
402 while (scope) {
403 if (scope->isTry())
404 return scope->getTry();
405 scope = scope->parentScope;
406 }
407 return nullptr;
408}
409
410/// An argument came in as a promoted argument; demote it back to its
411/// declared type.
412static mlir::Value emitArgumentDemotion(CIRGenFunction &cgf, const VarDecl *var,
413 mlir::Value value) {
414 mlir::Type ty = cgf.convertType(var->getType());
415
416 // This can happen with promotions that actually don't change the
417 // underlying type, like the enum promotions.
418 if (value.getType() == ty)
419 return value;
420
421 assert((mlir::isa<cir::IntType>(ty) || cir::isAnyFloatingPointType(ty)) &&
422 "unexpected promotion type");
423
424 if (mlir::isa<cir::IntType>(ty))
425 return cgf.getBuilder().CIRBaseBuilderTy::createIntCast(value, ty);
426
427 return cgf.getBuilder().createFloatingCast(value, ty);
428}
429
431 mlir::Block *entryBB,
432 const FunctionDecl *fd,
433 SourceLocation bodyBeginLoc) {
434 // Naked functions don't have prologues.
435 if (fd && fd->hasAttr<NakedAttr>()) {
436 cgm.errorNYI(bodyBeginLoc, "naked function decl");
437 }
438
439 // Declare all the function arguments in the symbol table.
440 for (const auto nameValue : llvm::zip(args, entryBB->getArguments())) {
441 const VarDecl *paramVar = std::get<0>(nameValue);
442 mlir::Value paramVal = std::get<1>(nameValue);
443 CharUnits alignment = getContext().getDeclAlign(paramVar);
444 mlir::Location paramLoc = getLoc(paramVar->getSourceRange());
445 paramVal.setLoc(paramLoc);
446
447 mlir::Value addrVal =
448 emitAlloca(cast<NamedDecl>(paramVar)->getName(),
449 convertType(paramVar->getType()), paramLoc, alignment,
450 /*insertIntoFnEntryBlock=*/true);
451
452 declare(addrVal, paramVar, paramVar->getType(), paramLoc, alignment,
453 /*isParam=*/true);
454
455 setAddrOfLocalVar(paramVar, Address(addrVal, alignment));
456
457 bool isPromoted = isa<ParmVarDecl>(paramVar) &&
458 cast<ParmVarDecl>(paramVar)->isKNRPromoted();
460 if (isPromoted)
461 paramVal = emitArgumentDemotion(*this, paramVar, paramVal);
462
463 // Location of the store to the param storage tracked as beginning of
464 // the function body.
465 mlir::Location fnBodyBegin = getLoc(bodyBeginLoc);
466 builder.CIRBaseBuilderTy::createStore(fnBodyBegin, paramVal, addrVal);
467 }
468 assert(builder.getInsertionBlock() && "Should be valid");
469}
470
472 cir::FuncOp fn, cir::FuncType funcType,
474 SourceLocation startLoc) {
475 assert(!curFn &&
476 "CIRGenFunction can only be used for one function at a time");
477
478 curFn = fn;
479
480 const Decl *d = gd.getDecl();
481
482 didCallStackSave = false;
483 curCodeDecl = d;
484 const auto *fd = dyn_cast_or_null<FunctionDecl>(d);
485 curFuncDecl = (d ? d->getNonClosureContext() : nullptr);
486
487 prologueCleanupDepth = ehStack.stable_begin();
488
489 mlir::Block *entryBB = &fn.getBlocks().front();
490 builder.setInsertionPointToStart(entryBB);
491
492 // Determine the function body begin location for the prolog.
493 // If fd is null or has no body, use startLoc as fallback.
494 SourceLocation bodyBeginLoc = startLoc;
495 if (fd) {
496 if (Stmt *body = fd->getBody())
497 bodyBeginLoc = body->getBeginLoc();
498 else
499 bodyBeginLoc = fd->getLocation();
500 }
501
502 emitFunctionProlog(args, entryBB, fd, bodyBeginLoc);
503
504 // When the current function is not void, create an address to store the
505 // result value.
506 if (!returnType->isVoidType()) {
507 // Determine the function body end location.
508 // If fd is null or has no body, use loc as fallback.
509 SourceLocation bodyEndLoc = loc;
510 if (fd) {
511 if (Stmt *body = fd->getBody())
512 bodyEndLoc = body->getEndLoc();
513 else
514 bodyEndLoc = fd->getLocation();
515 }
516 emitAndUpdateRetAlloca(returnType, getLoc(bodyEndLoc),
517 getContext().getTypeAlignInChars(returnType));
518
519 // If this is an implicit-return-zero function, initialize the return
520 // value. This mirrors the implicit-return-zero handling in classic
521 // codegen's EmitFunctionProlog (CGCall.cpp). It is done here, after
522 // emitAndUpdateRetAlloca, because in CIR the return slot is created
523 // after the prolog (the opposite of classic codegen, where ReturnValue
524 // is set up before EmitFunctionProlog runs).
525 // TODO(cir): Align prolog handling with classic codegen.
526 if (fd && fd->hasImplicitReturnZero()) {
527 mlir::Type cirRetTy = convertType(returnType.getUnqualifiedType());
528 mlir::Location bodyBeginMLIRLoc = getLoc(bodyBeginLoc);
529 mlir::Value zero = builder.getNullValue(cirRetTy, bodyBeginMLIRLoc);
530 builder.CIRBaseBuilderTy::createStore(bodyBeginMLIRLoc, zero,
531 returnValue.getPointer());
532 }
533 }
534
535 // Only implicit-object member functions (without an explicit `this`
536 // parameter) receive an implicit `this` argument that the CXXABI prolog has
537 // to set up. C++23 explicit-object members (P0847R7) carry their object via a
538 // regular parameter and use the standard parameter prolog instead.
539 if (isa_and_nonnull<CXXMethodDecl>(d) &&
540 cast<CXXMethodDecl>(d)->isImplicitObjectMemberFunction()) {
541 cgm.getCXXABI().emitInstanceFunctionProlog(loc, *this);
542
543 const auto *md = cast<CXXMethodDecl>(d);
544 if (md->getParent()->isLambda() && md->getOverloadedOperator() == OO_Call) {
545 // We're in a lambda.
546 auto fn = dyn_cast<cir::FuncOp>(curFn);
547 assert(fn && "lambda in non-function region");
548 fn.setLambda(true);
549
550 // Figure out the captures.
551 md->getParent()->getCaptureFields(lambdaCaptureFields,
554 // If the lambda captures the object referred to by '*this' - either by
555 // value or by reference, make sure CXXThisValue points to the correct
556 // object.
557
558 // Get the lvalue for the field (which is a copy of the enclosing object
559 // or contains the address of the enclosing object).
560 LValue thisFieldLValue =
562 if (!lambdaThisCaptureField->getType()->isPointerType()) {
563 // If the enclosing object was captured by value, just use its
564 // address. Sign this pointer.
565 cxxThisValue = thisFieldLValue.getPointer();
566 } else {
567 // Load the lvalue pointed to by the field, since '*this' was captured
568 // by reference.
570 emitLoadOfLValue(thisFieldLValue, SourceLocation()).getValue();
571 }
572 }
573 for (auto *fd : md->getParent()->fields()) {
574 if (fd->hasCapturedVLAType())
575 cgm.errorNYI(loc, "lambda captured VLA type");
576 }
577 } else {
578 // Not in a lambda; just use 'this' from the method.
579 // FIXME: Should we generate a new load for each use of 'this'? The fast
580 // register allocator would be happier...
582 }
583
586 }
587
588 // If any of the arguments have a variably modified type, make sure to
589 // emit the type size, but only if the function is not naked. Naked functions
590 // have no prolog to run this evaluation.
591 if (!fd || !fd->hasAttr<NakedAttr>()) {
592 for (const VarDecl *vd : args) {
593 // Dig out the type as written from ParmVarDecls; it's unclear whether
594 // the standard (C99 6.9.1p10) requires this, but we're following the
595 // precedent set by gcc.
596 QualType ty;
597 if (const auto *pvd = dyn_cast<ParmVarDecl>(vd))
598 ty = pvd->getOriginalType();
599 else
600 ty = vd->getType();
601 if (ty->isVariablyModifiedType())
603 }
604 }
605}
606
608 for (cir::BlockAddressOp &blockAddress : cgm.unresolvedBlockAddressToLabel) {
609 cir::LabelOp labelOp =
610 cgm.lookupBlockAddressInfo(blockAddress.getBlockAddrInfo());
611 assert(labelOp && "expected cir.labelOp to already be emitted");
612 cgm.updateResolvedBlockAddress(blockAddress, labelOp);
613 }
614 cgm.unresolvedBlockAddressToLabel.clear();
615}
616
619 return;
622 mlir::OpBuilder::InsertionGuard guard(builder);
623 builder.setInsertionPointToEnd(indirectGotoBlock);
624 for (auto &[blockAdd, labelOp] : cgm.blockAddressToLabel) {
625 succesors.push_back(labelOp->getBlock());
626 rangeOperands.push_back(labelOp->getBlock()->getArguments());
627 }
628 cir::IndirectBrOp::create(builder, builder.getUnknownLoc(),
629 indirectGotoBlock->getArgument(0), false,
630 rangeOperands, succesors);
631 cgm.blockAddressToLabel.clear();
632}
633
635 // Resolve block address-to-label mappings, then emit the indirect branch
636 // with the corresponding targets.
639
640 // If a label address was taken but no indirect goto was used, we can't remove
641 // the block argument here. Instead, we mark the 'indirectbr' op
642 // as poison so that the cleanup can be deferred to lowering, since the
643 // verifier doesn't allow the 'indirectbr' target address to be null.
644 if (indirectGotoBlock && indirectGotoBlock->hasNoPredecessors()) {
645 auto indrBr = cast<cir::IndirectBrOp>(indirectGotoBlock->front());
646 indrBr.setPoison(true);
647 }
648
649 // Pop any cleanups that might have been associated with the
650 // parameters. Do this in whatever block we're currently in; it's
651 // important to do this before we enter the return block or return
652 // edges will be *really* confused.
653 // TODO(cir): Use prologueCleanupDepth here.
654 bool hasCleanups = ehStack.stable_begin() != prologueCleanupDepth;
655 if (hasCleanups) {
657 // FIXME(cir): should we clearInsertionPoint? breaks many testcases
659 }
660
661 assert(deferredConditionalCleanupStack.empty() &&
662 "deferred conditional cleanups were not consumed by a "
663 "FullExprCleanupScope");
664}
665
666mlir::LogicalResult CIRGenFunction::emitFunctionBody(const clang::Stmt *body) {
667 // We start with function level scope for variables.
669
670 if (const CompoundStmt *block = dyn_cast<CompoundStmt>(body))
671 return emitCompoundStmtWithoutScope(*block);
672
673 return emitStmt(body, /*useCurrentScope=*/true);
674}
675
676static void eraseEmptyAndUnusedBlocks(cir::FuncOp func) {
677 // Remove any leftover blocks that are unreachable and empty, since they do
678 // not represent unreachable code useful for warnings nor anything deemed
679 // useful in general.
680 SmallVector<mlir::Block *> blocksToDelete;
681 for (mlir::Block &block : func.getBlocks()) {
682 if (block.empty() && block.getUses().empty())
683 blocksToDelete.push_back(&block);
684 }
685 for (mlir::Block *block : blocksToDelete)
686 block->erase();
687}
688
689cir::FuncOp CIRGenFunction::generateCode(clang::GlobalDecl gd, cir::FuncOp fn,
690 cir::FuncType funcType) {
691 const auto *funcDecl = cast<FunctionDecl>(gd.getDecl());
692 curGD = gd;
693
694 if (funcDecl->isInlineBuiltinDeclaration()) {
695 // When generating code for a builtin with an inline declaration, use a
696 // mangled name to hold the actual body, while keeping an external
697 // declaration in case the function pointer is referenced somewhere.
698 std::string fdInlineName = (cgm.getMangledName(funcDecl) + ".inline").str();
699 cir::FuncOp clone =
700 mlir::cast_or_null<cir::FuncOp>(cgm.getGlobalValue(fdInlineName));
701 if (!clone) {
702 mlir::OpBuilder::InsertionGuard guard(builder);
703 builder.setInsertionPoint(fn);
704 clone = cir::FuncOp::create(builder, fn.getLoc(), fdInlineName,
705 fn.getFunctionType());
706 cgm.insertGlobalSymbol(clone);
707 clone.setLinkage(cir::GlobalLinkageKind::InternalLinkage);
708 clone.setSymVisibility("private");
709 clone.setInlineKind(cir::InlineKind::AlwaysInline);
710 }
711 fn.setLinkage(cir::GlobalLinkageKind::ExternalLinkage);
712 fn.setSymVisibility("private");
713 fn = clone;
714 } else {
715 // Detect the unusual situation where an inline version is shadowed by a
716 // non-inline version. In that case we should pick the external one
717 // everywhere. That's GCC behavior too.
718 for (const FunctionDecl *pd = funcDecl->getPreviousDecl(); pd;
719 pd = pd->getPreviousDecl()) {
720 if (LLVM_UNLIKELY(pd->isInlineBuiltinDeclaration())) {
721 std::string inlineName = funcDecl->getName().str() + ".inline";
722 if (auto inlineFn = mlir::cast_or_null<cir::FuncOp>(
723 cgm.getGlobalValue(inlineName))) {
724 // Replace all uses of the .inline function with the regular function
725 // FIXME: This performs a linear walk over the module. Introduce some
726 // caching here.
727 if (inlineFn
728 .replaceAllSymbolUses(fn.getSymNameAttr(), cgm.getModule())
729 .failed())
730 llvm_unreachable("Failed to replace inline builtin symbol uses");
731 cgm.eraseGlobalSymbol(inlineFn);
732 inlineFn.erase();
733 }
734 break;
735 }
736 }
737 }
738
739 SourceLocation loc = funcDecl->getLocation();
740 Stmt *body = funcDecl->getBody();
741 SourceRange bodyRange =
742 body ? body->getSourceRange() : funcDecl->getLocation();
743
744 SourceLocRAIIObject fnLoc{*this, loc.isValid() ? getLoc(loc)
745 : builder.getUnknownLoc()};
746
747 auto validMLIRLoc = [&](clang::SourceLocation clangLoc) {
748 return clangLoc.isValid() ? getLoc(clangLoc) : builder.getUnknownLoc();
749 };
750 const mlir::Location fusedLoc = mlir::FusedLoc::get(
752 {validMLIRLoc(bodyRange.getBegin()), validMLIRLoc(bodyRange.getEnd())});
753 mlir::Block *entryBB = fn.addEntryBlock();
754
755 FunctionArgList args;
756 QualType retTy = buildFunctionArgList(gd, args);
757
758 // Create a scope in the symbol table to hold variable declarations.
760 {
761 LexicalScope lexScope(*this, fusedLoc, entryBB);
762
763 // Emit the standard function prologue.
764 startFunction(gd, retTy, fn, funcType, args, loc, bodyRange.getBegin());
765 if (funcDecl->UsesFPIntrin() || funcDecl->hasAttr<StrictFPAttr>()) {
766 cgm.errorNYI(loc, "STDC FENV_ACCESS");
767 return fn;
768 }
769
770 // Save parameters for coroutine function.
771 if (body && isa_and_nonnull<CoroutineBodyStmt>(body))
772 llvm::append_range(fnArgs, funcDecl->parameters());
773
774 if (isa<CXXDestructorDecl>(funcDecl)) {
775 emitDestructorBody(args);
776 } else if (isa<CXXConstructorDecl>(funcDecl)) {
778 } else if (getLangOpts().CUDA && !getLangOpts().CUDAIsDevice &&
779 funcDecl->hasAttr<CUDAGlobalAttr>()) {
780 cgm.getCUDARuntime().emitDeviceStub(*this, fn, args);
781 } else if (isa<CXXMethodDecl>(funcDecl) &&
782 cast<CXXMethodDecl>(funcDecl)->isLambdaStaticInvoker()) {
783 // The lambda static invoker function is special, because it forwards or
784 // clones the body of the function call operator (but is actually
785 // static).
787 } else if (funcDecl->isDefaulted() && isa<CXXMethodDecl>(funcDecl) &&
788 (cast<CXXMethodDecl>(funcDecl)->isCopyAssignmentOperator() ||
789 cast<CXXMethodDecl>(funcDecl)->isMoveAssignmentOperator())) {
790 // Implicit copy-assignment gets the same special treatment as implicit
791 // copy-constructors.
793 } else if (body) {
794 // Emit standard function body.
795 if (mlir::failed(emitFunctionBody(body))) {
796 return nullptr;
797 }
798 } else {
799 // Anything without a body should have been handled above.
800 llvm_unreachable("no definition for normal function");
801 }
802
803 if (mlir::failed(fn.verifyBody()))
804 return nullptr;
805
806 finishFunction(bodyRange.getEnd());
807 }
808
810 return fn;
811}
812
815 const auto *ctor = cast<CXXConstructorDecl>(curGD.getDecl());
816 CXXCtorType ctorType = curGD.getCtorType();
817
818 assert((cgm.getTarget().getCXXABI().hasConstructorVariants() ||
819 ctorType == Ctor_Complete) &&
820 "can only generate complete ctor for this ABI");
821
822 cgm.setCXXSpecialMemberAttr(cast<cir::FuncOp>(curFn), ctor);
823
824 if (ctorType == Ctor_Complete && isConstructorDelegationValid(ctor) &&
825 cgm.getTarget().getCXXABI().hasConstructorVariants()) {
826 emitDelegateCXXConstructorCall(ctor, Ctor_Base, args, ctor->getEndLoc());
827 return;
828 }
829
830 const FunctionDecl *definition = nullptr;
831 Stmt *body = ctor->getBody(definition);
832 assert(definition == ctor && "emitting wrong constructor body");
833
834 bool isTryBody = isa_and_nonnull<CXXTryStmt>(body);
835
836 // A type that handles the emission of the constructor body, that can be
837 // called directly for cases where we don't have a try-body, or passed to
838 // emitCXXTryStmt.
839 struct ctorTryBodyEmitter final : cxxTryBodyEmitter {
840 const CXXConstructorDecl *ctor = nullptr;
841 CXXCtorType ctorType;
842 FunctionArgList &args;
843 Stmt *emitterBody = nullptr;
844 ctorTryBodyEmitter(const CXXConstructorDecl *ctor, CXXCtorType ctorType,
845 FunctionArgList &args, bool isTryBody, Stmt *b)
846 : ctor(ctor), ctorType(ctorType), args(args),
847 emitterBody(isTryBody ? cast<CXXTryStmt>(b)->getTryBlock() : b) {}
848 ~ctorTryBodyEmitter() override = default;
849
850 mlir::LogicalResult operator()(CIRGenFunction &cgf) override {
853
854 //// TODO: in restricted cases, we can emit the vbase initializers of a
855 //// complete ctor and then delegate to the base ctor.
856
857 cgf.emitCtorPrologue(ctor, ctorType, args);
858 return cgf.emitStmt(emitterBody, /*useCurrentScope=*/true);
859 }
860 };
861
862 ctorTryBodyEmitter emitter{ctor, ctorType, args, isTryBody, body};
863 mlir::LogicalResult bodyRes =
864 isTryBody ? emitCXXTryStmt(*cast<CXXTryStmt>(body), emitter)
865 : emitter(*this);
866
867 // TODO(cir): propagate this result via mlir::logical result. Just
868 // unreachable now just to have it handled.
869 if (bodyRes.failed())
870 cgm.errorNYI(ctor->getSourceRange(),
871 "emitConstructorBody: emit body statement failed.");
872}
873
874/// Emits the body of the current destructor.
876 const CXXDestructorDecl *dtor = cast<CXXDestructorDecl>(curGD.getDecl());
877 CXXDtorType dtorType = curGD.getDtorType();
878
879 cgm.setCXXSpecialMemberAttr(cast<cir::FuncOp>(curFn), dtor);
880
881 // For an abstract class, non-base destructors are never used (and can't
882 // be emitted in general, because vbase dtors may not have been validated
883 // by Sema), but the Itanium ABI doesn't make them optional and Clang may
884 // in fact emit references to them from other compilations, so emit them
885 // as functions containing a trap instruction.
886 if (dtorType != Dtor_Base && dtor->getParent()->isAbstract()) {
887 SourceLocation loc =
888 dtor->hasBody() ? dtor->getBody()->getBeginLoc() : dtor->getLocation();
889 emitTrap(getLoc(loc), true);
890 return;
891 }
892
893 Stmt *body = dtor->getBody();
895
896 // The call to operator delete in a deleting destructor happens
897 // outside of the function-try-block, which means it's always
898 // possible to delegate the destructor body to the complete
899 // destructor. Do so.
900 if (dtorType == Dtor_Deleting || dtorType == Dtor_VectorDeleting) {
902 cgm.errorNYI(dtor->getSourceRange(), "emitConditionalArrayDtorCall");
903 RunCleanupsScope dtorEpilogue(*this);
905 if (haveInsertPoint()) {
907 emitCXXDestructorCall(dtor, Dtor_Complete, /*forVirtualBase=*/false,
908 /*delegating=*/false, loadCXXThisAddress(), thisTy);
909 }
910 return;
911 }
912
913 // If the body is a function-try-block, enter the try before
914 // anything else.
915 const bool isTryBody = isa_and_nonnull<CXXTryStmt>(body);
916 if (isTryBody)
917 cgm.errorNYI(dtor->getSourceRange(), "function-try-block destructor");
918
920
921 // Enter the epilogue cleanups.
922 RunCleanupsScope dtorEpilogue(*this);
923
924 // If this is the complete variant, just invoke the base variant;
925 // the epilogue will destruct the virtual bases. But we can't do
926 // this optimization if the body is a function-try-block, because
927 // we'd introduce *two* handler blocks. In the Microsoft ABI, we
928 // always delegate because we might not have a definition in this TU.
929 switch (dtorType) {
930 case Dtor_Unified:
931 llvm_unreachable("not expecting a unified dtor");
932 case Dtor_Comdat:
933 llvm_unreachable("not expecting a COMDAT");
934 case Dtor_Deleting:
936 llvm_unreachable("already handled deleting case");
937
938 case Dtor_Complete:
939 assert((body || getTarget().getCXXABI().isMicrosoft()) &&
940 "can't emit a dtor without a body for non-Microsoft ABIs");
941
942 // Enter the cleanup scopes for virtual bases.
944
945 if (!isTryBody) {
947 emitCXXDestructorCall(dtor, Dtor_Base, /*forVirtualBase=*/false,
948 /*delegating=*/false, loadCXXThisAddress(), thisTy);
949 break;
950 }
951
952 // Fallthrough: act like we're in the base variant.
953 [[fallthrough]];
954
955 case Dtor_Base:
956 assert(body);
957
958 // Enter the cleanup scopes for fields and non-virtual bases.
960
962
963 if (isTryBody) {
964 cgm.errorNYI(dtor->getSourceRange(), "function-try-block destructor");
965 } else if (body) {
966 (void)emitStmt(body, /*useCurrentScope=*/true);
967 } else {
968 assert(dtor->isImplicit() && "bodyless dtor not implicit");
969 // nothing to do besides what's in the epilogue
970 }
971 // -fapple-kext must inline any call to this dtor into
972 // the caller's body.
974
975 break;
976 }
977
978 // Jump out through the epilogue cleanups.
979 dtorEpilogue.forceCleanup();
980
981 // Exit the try if applicable.
982 if (isTryBody)
983 cgm.errorNYI(dtor->getSourceRange(), "function-try-block destructor");
984}
985
986/// Given a value of type T* that may not be to a complete object, construct
987/// an l-vlaue withi the natural pointee alignment of T.
989 QualType ty) {
990 // FIXME(cir): is it safe to assume Op->getResult(0) is valid? Perhaps
991 // assert on the result type first.
992 LValueBaseInfo baseInfo;
994 CharUnits align = cgm.getNaturalTypeAlignment(ty, &baseInfo);
995 return makeAddrLValue(Address(val, align), ty, baseInfo);
996}
997
999 QualType ty) {
1000 LValueBaseInfo baseInfo;
1001 CharUnits alignment = cgm.getNaturalTypeAlignment(ty, &baseInfo);
1002 Address addr(val, convertTypeForMem(ty), alignment);
1004 return makeAddrLValue(addr, ty, baseInfo);
1005}
1006
1007// Map the LangOption for exception behavior into the corresponding enum in
1008// the IR.
1009static llvm::fp::ExceptionBehavior
1011 switch (kind) {
1013 return llvm::fp::ebIgnore;
1015 return llvm::fp::ebMayTrap;
1017 return llvm::fp::ebStrict;
1019 llvm_unreachable("expected explicitly initialized exception behavior");
1020 }
1021 llvm_unreachable("unsupported FP exception behavior");
1022}
1023
1025 FunctionArgList &args) {
1026 const auto *fd = cast<FunctionDecl>(gd.getDecl());
1027 QualType retTy = fd->getReturnType();
1028
1029 // Only implicit-object member functions need the CXXABI-supplied `this`
1030 // parameter prepended to the arg list. Explicit-object members carry the
1031 // object as a regular parameter that fd->parameters() already enumerates.
1032 const auto *md = dyn_cast<CXXMethodDecl>(fd);
1033 if (md && md->isImplicitObjectMemberFunction()) {
1034 if (cgm.getCXXABI().hasThisReturn(gd))
1035 cgm.errorNYI(fd->getSourceRange(), "this return");
1036 else if (cgm.getCXXABI().hasMostDerivedReturn(gd))
1037 cgm.errorNYI(fd->getSourceRange(), "most derived return");
1038 cgm.getCXXABI().buildThisParam(*this, args);
1039 }
1040
1041 bool passedParams = true;
1042 if (const auto *cd = dyn_cast<CXXConstructorDecl>(fd))
1043 if (auto inherited = cd->getInheritedConstructor())
1044 passedParams =
1045 getTypes().inheritingCtorHasParams(inherited, gd.getCtorType());
1046
1047 if (passedParams) {
1048 for (auto *param : fd->parameters()) {
1049 args.push_back(param);
1050 if (!param->hasAttr<PassObjectSizeAttr>())
1051 continue;
1052
1053 auto *implicit = ImplicitParamDecl::Create(
1054 getContext(), param->getDeclContext(), param->getLocation(),
1055 /*Id=*/nullptr, getContext().getSizeType(), ImplicitParamKind::Other);
1056 sizeArguments[param] = implicit;
1057 args.push_back(implicit);
1058 }
1059 }
1060
1061 if (md && (isa<CXXConstructorDecl>(md) || isa<CXXDestructorDecl>(md)))
1062 cgm.getCXXABI().addImplicitStructorParams(*this, retTy, args);
1063
1064 return retTy;
1065}
1066
1068 // Initializing an aggregate temporary in C++11: T{...}.
1069 if (!e->isGLValue())
1070 return emitAggExprToLValue(e);
1071
1072 // An lvalue initializer list must be initializing a reference.
1073 assert(e->isTransparent() && "non-transparent glvalue init list");
1074 return emitLValue(e->getInit(0));
1075}
1076
1077static std::variant<LValue, RValue>
1079 bool forLValue, AggValueSlot slot) {
1081 SmallVector<OVMD> opaques;
1082 llvm::scope_exit opaque_cleanup{
1083 [&]() { llvm::for_each(opaques, [&](OVMD &o) { o.unbind(cgf); }); }};
1084
1085 // Find the result expression, if any.
1086 const Expr *resultExpr = e->getResultExpr();
1087 std::variant<LValue, RValue> result;
1088
1089 for (const Expr *semantic : e->semantics()) {
1090 // If this semantic expression is an opaque value, bind it
1091 // to the result of its source expression.
1092 if (const auto *ov = dyn_cast<OpaqueValueExpr>(semantic)) {
1093
1094 // Skip unique OVEs.
1095 if (ov->isUnique()) {
1096 assert(ov != resultExpr &&
1097 "A unique OVE cannot be used as the result expression");
1098 continue;
1099 }
1100
1101 // If this is the result expression, we may need to evaluate
1102 // directly into the slot.
1103 OVMD opaqueData;
1104 if (ov == resultExpr && ov->isPRValue() && !forLValue &&
1106 cgf.cgm.errorNYI(e->getSourceRange(),
1107 "emitPseudoObjectExpr for RValue & aggregate kind");
1108 } else {
1109 opaqueData = OVMD::bind(cgf, ov, ov->getSourceExpr());
1110
1111 // If this is the result, also evaluate the result now.
1112 if (ov == resultExpr) {
1113 // FIXME: This doesn't really affect anything, but I cannot find a
1114 // test for this, so leave an ErrorNYI here until we can find one.
1115 cgf.cgm.errorNYI(e->getSourceRange(),
1116 "emitPseudoObjectExpr as result");
1117 if (forLValue)
1118 result = cgf.emitLValue(ov);
1119 else
1120 cgf.cgm.errorNYI(e->getSourceRange(),
1121 "emitPseudoObjectExpr as an RValue");
1122 }
1123 }
1124 opaques.push_back(opaqueData);
1125 } else if (semantic == resultExpr) {
1126 // Otherwise, if the expression is the result, evaluate it
1127 // and remember the result.
1128 if (forLValue)
1129 result = cgf.emitLValue(semantic);
1130 else
1131 result = cgf.emitAnyExpr(semantic, slot);
1132 } else {
1133 // FIXME: best I can tell, this is only reachable as an r-value, so this
1134 // isn't properly tested.
1135 cgf.cgm.errorNYI(e->getSourceRange(),
1136 "emitPseudoObjectExpr as an ignored value");
1137 // Otherwise, evaluate the expression in an ignored context.
1138 cgf.emitIgnoredExpr(semantic);
1139 }
1140 }
1141
1142 return result;
1143}
1144
1146 AggValueSlot slot) {
1147 return std::get<RValue>(
1148 emitPseudoObjectExpr(*this, e, /*forLValue=*/false, slot));
1149}
1150
1152 return std::get<LValue>(emitPseudoObjectExpr(*this, e, /*forLValue=*/true,
1154}
1155
1156/// Emit code to compute a designator that specifies the location
1157/// of the expression.
1158/// FIXME: document this function better.
1161 switch (e->getStmtClass()) {
1162 default:
1164 "emitLValue: unsupported l-value class");
1165 return LValue();
1166
1167 case Expr::ObjCPropertyRefExprClass:
1168 llvm_unreachable("cannot emit a property reference directly");
1169
1170 case Expr::ObjCSelectorExprClass:
1172 "emitLValue: ObjCSelectorExpr");
1173 return LValue();
1174 case Expr::ObjCIsaExprClass:
1175 getCIRGenModule().errorNYI(e->getSourceRange(), "emitLValue: ObjCIsaExpr");
1176 return LValue();
1177 case Expr::BinaryOperatorClass:
1179 case Expr::CompoundAssignOperatorClass: {
1180 QualType ty = e->getType();
1181 if (const AtomicType *at = ty->getAs<AtomicType>())
1182 ty = at->getValueType();
1183 if (!ty->isAnyComplexType())
1185
1187 }
1188 case Expr::CallExprClass:
1189 case Expr::CXXMemberCallExprClass:
1190 case Expr::CXXOperatorCallExprClass:
1191 case Expr::UserDefinedLiteralClass:
1193 case Expr::CXXRewrittenBinaryOperatorClass:
1195 "emitLValue: CXXRewrittenBinaryOperator");
1196 return LValue();
1197 case Expr::VAArgExprClass:
1198 getCIRGenModule().errorNYI(e->getSourceRange(), "emitLValue: VAArgExpr");
1199 return LValue();
1200 case Expr::DeclRefExprClass:
1202 case Expr::ConstantExprClass:
1203 getCIRGenModule().errorNYI(e->getSourceRange(), "emitLValue: ConstantExpr");
1204 return LValue();
1205 case Expr::ParenExprClass:
1206 return emitLValue(cast<ParenExpr>(e)->getSubExpr());
1207 case Expr::GenericSelectionExprClass:
1208 return emitLValue(cast<GenericSelectionExpr>(e)->getResultExpr());
1209 case Expr::PredefinedExprClass:
1211 case Expr::StringLiteralClass:
1213 case Expr::ObjCEncodeExprClass:
1215 "emitLValue: ObjCEncodeExpr");
1216 return LValue();
1217 case Expr::PseudoObjectExprClass:
1219 case Expr::InitListExprClass:
1221 case Expr::CXXTemporaryObjectExprClass:
1222 case Expr::CXXConstructExprClass:
1224 case Expr::CXXBindTemporaryExprClass:
1226 case Expr::CXXUuidofExprClass:
1228 "emitLValue: CXXUuidofExpr");
1229 return LValue();
1230 case Expr::LambdaExprClass:
1231 getCIRGenModule().errorNYI(e->getSourceRange(), "emitLValue: LambdaExpr");
1232 return LValue();
1233 case Expr::ExprWithCleanupsClass: {
1234 const auto *cleanups = cast<ExprWithCleanups>(e);
1235 FullExprCleanupScope scope(*this, cleanups->getSubExpr());
1236 LValue lv = emitLValue(cleanups->getSubExpr());
1237 if (lv.isSimple()) {
1238 // Defend against branches out of gnu statement expressions surrounded by
1239 // cleanups.
1240 Address addr = lv.getAddress();
1241 mlir::Value v = addr.getPointer();
1242 scope.exit({&v});
1243 return LValue::makeAddr(addr.withPointer(v), lv.getType(),
1244 lv.getBaseInfo());
1245 }
1246 // FIXME: Is it possible to create an ExprWithCleanups that produces a
1247 // bitfield lvalue or some other non-simple lvalue?
1248 return lv;
1249 }
1250 case Expr::CXXDefaultArgExprClass: {
1251 auto *dae = cast<CXXDefaultArgExpr>(e);
1252 CXXDefaultArgExprScope scope(*this, dae);
1253 return emitLValue(dae->getExpr());
1254 }
1255 case Expr::CXXDefaultInitExprClass: {
1256 auto *die = cast<CXXDefaultInitExpr>(e);
1257 CXXDefaultInitExprScope scope(*this, die);
1258 return emitLValue(die->getExpr());
1259 }
1260 case Expr::CXXTypeidExprClass:
1262 case Expr::ObjCMessageExprClass:
1264 "emitLValue: ObjCMessageExpr");
1265 return LValue();
1266 case Expr::ObjCIvarRefExprClass:
1268 "emitLValue: ObjCIvarRefExpr");
1269 return LValue();
1270 case Expr::StmtExprClass:
1271 getCIRGenModule().errorNYI(e->getSourceRange(), "emitLValue: StmtExpr");
1272 return LValue();
1273 case Expr::UnaryOperatorClass:
1275 case Expr::ArraySubscriptExprClass:
1277 case Expr::MatrixSingleSubscriptExprClass:
1279 "emitLValue: MatrixSingleSubscriptExpr");
1280 return LValue();
1281 case Expr::MatrixSubscriptExprClass:
1283 "emitLValue: MatrixSubscriptExpr");
1284 return LValue();
1285 case Expr::ArraySectionExprClass:
1287 "emitLValue: ArraySectionExpr");
1288 return LValue();
1289 case Expr::ExtVectorElementExprClass:
1291 case Expr::MatrixElementExprClass:
1293 "emitLValue: MatrixElementExpr");
1294 return LValue();
1295 case Expr::CXXThisExprClass:
1296 getCIRGenModule().errorNYI(e->getSourceRange(), "emitLValue: CXXThisExpr");
1297 return LValue();
1298 case Expr::MemberExprClass:
1300 case Expr::CompoundLiteralExprClass:
1302 case Expr::ConditionalOperatorClass:
1304 case Expr::BinaryConditionalOperatorClass:
1306 case Expr::ChooseExprClass:
1307 return emitLValue(cast<ChooseExpr>(e)->getChosenSubExpr());
1308 case Expr::OpaqueValueExprClass:
1310 case Expr::SubstNonTypeTemplateParmExprClass:
1311 return emitLValue(cast<SubstNonTypeTemplateParmExpr>(e)->getReplacement());
1312 case Expr::ImplicitCastExprClass:
1313 case Expr::CStyleCastExprClass:
1314 case Expr::CXXFunctionalCastExprClass:
1315 case Expr::CXXStaticCastExprClass:
1316 case Expr::CXXDynamicCastExprClass:
1317 case Expr::CXXReinterpretCastExprClass:
1318 case Expr::CXXConstCastExprClass:
1319 return emitCastLValue(cast<CastExpr>(e));
1320 case Expr::CXXAddrspaceCastExprClass:
1321 case Expr::ObjCBridgedCastExprClass:
1322 // TODO(cir): These can just be moved into the cast handling above, but
1323 // they need test cases.
1325 "emitLValue: addrspace or ObjC bridged cast");
1326 return LValue();
1327 case Expr::MaterializeTemporaryExprClass:
1329 case Expr::CoawaitExprClass:
1330 getCIRGenModule().errorNYI(e->getSourceRange(), "emitLValue: CoawaitExpr");
1331 return LValue();
1332 case Expr::CoyieldExprClass:
1333 getCIRGenModule().errorNYI(e->getSourceRange(), "emitLValue: CoyieldExpr");
1334 return LValue();
1335 case Expr::PackIndexingExprClass:
1337 "emitLValue: PackIndexingExpr");
1338 return LValue();
1339 case Expr::HLSLOutArgExprClass:
1340 llvm_unreachable("cannot emit a HLSL out argument directly");
1341 }
1342}
1343
1344static std::string getVersionedTmpName(llvm::StringRef name, unsigned cnt) {
1345 SmallString<256> buffer;
1346 llvm::raw_svector_ostream out(buffer);
1347 out << name << cnt;
1348 return std::string(out.str());
1349}
1350
1352 return getVersionedTmpName("ref.tmp", counterRefTmp++);
1353}
1354
1356 return getVersionedTmpName("agg.tmp", counterAggTmp++);
1357}
1358
1359void CIRGenFunction::emitNullInitialization(mlir::Location loc, Address destPtr,
1360 QualType ty) {
1361 // Ignore empty classes in C++.
1362 if (getLangOpts().CPlusPlus)
1363 if (const auto *rd = ty->getAsCXXRecordDecl(); rd && rd->isEmpty())
1364 return;
1365
1366 // Cast the dest ptr to the appropriate i8 pointer type.
1367 if (builder.isInt8Ty(destPtr.getElementType())) {
1368 cgm.errorNYI(loc, "Cast the dest ptr to the appropriate i8 pointer type");
1369 }
1370
1371 // Get size and alignment info for this aggregate.
1372 const CharUnits size = getContext().getTypeSizeInChars(ty);
1373 if (size.isZero()) {
1374 // But note that getTypeInfo returns 0 for a VLA.
1375 if (isa<VariableArrayType>(getContext().getAsArrayType(ty))) {
1376 cgm.errorNYI(loc,
1377 "emitNullInitialization for zero size VariableArrayType");
1378 } else {
1379 return;
1380 }
1381 }
1382
1383 // If the type contains a pointer to data member we can't memset it to zero.
1384 // Instead, create a null constant and copy it to the destination.
1385 // TODO: there are other patterns besides zero that we can usefully memset,
1386 // like -1, which happens to be the pattern used by member-pointers.
1387 if (!cgm.getTypes().isZeroInitializable(ty)) {
1388 cgm.errorNYI(loc, "type is not zero initializable");
1389 }
1390
1391 // In LLVM Codegen: otherwise, just memset the whole thing to zero using
1392 // Builder.CreateMemSet. In CIR just emit a store of #cir.zero to the
1393 // respective address.
1394 // Builder.CreateMemSet(DestPtr, Builder.getInt8(0), SizeVal, false);
1395 const mlir::Value zeroValue = builder.getNullValue(convertType(ty), loc);
1396 builder.createStore(loc, zeroValue, destPtr);
1397}
1398
1400 const clang::Expr *e)
1401 : cgf(cgf) {
1402 ConstructorHelper(e->getFPFeaturesInEffect(cgf.getLangOpts()));
1403}
1404
1406 FPOptions fpFeatures)
1407 : cgf(cgf) {
1408 ConstructorHelper(fpFeatures);
1409}
1410
1411void CIRGenFunction::CIRGenFPOptionsRAII::ConstructorHelper(
1412 FPOptions fpFeatures) {
1413 oldFPFeatures = cgf.curFPFeatures;
1414 cgf.curFPFeatures = fpFeatures;
1415
1416 oldExcept = cgf.builder.getDefaultConstrainedExcept();
1417 oldRounding = cgf.builder.getDefaultConstrainedRounding();
1418
1419 if (oldFPFeatures == fpFeatures)
1420 return;
1421
1422 // TODO(cir): create guard to restore fast math configurations.
1424
1425 [[maybe_unused]] llvm::RoundingMode newRoundingBehavior =
1426 fpFeatures.getRoundingMode();
1427 // TODO(cir): override rounding behaviour once FM configs are guarded.
1428 [[maybe_unused]] llvm::fp::ExceptionBehavior newExceptionBehavior =
1430 fpFeatures.getExceptionMode()));
1431 // TODO(cir): override exception behaviour once FM configs are guarded.
1432
1433 // TODO(cir): override FP flags once FM configs are guarded.
1435
1436 assert((cgf.curFuncDecl == nullptr || cgf.builder.getIsFPConstrained() ||
1437 isa<CXXConstructorDecl>(cgf.curFuncDecl) ||
1438 isa<CXXDestructorDecl>(cgf.curFuncDecl) ||
1439 (newExceptionBehavior == llvm::fp::ebIgnore &&
1440 newRoundingBehavior == llvm::RoundingMode::NearestTiesToEven)) &&
1441 "FPConstrained should be enabled on entire function");
1442
1443 // TODO(cir): mark CIR function with fast math attributes.
1445}
1446
1448 cgf.curFPFeatures = oldFPFeatures;
1449 cgf.builder.setDefaultConstrainedExcept(oldExcept);
1450 cgf.builder.setDefaultConstrainedRounding(oldRounding);
1451}
1452
1453// TODO(cir): should be shared with LLVM codegen.
1455 const Expr *e = ce->getSubExpr();
1456
1457 if (ce->getCastKind() == CK_UncheckedDerivedToBase)
1458 return false;
1459
1460 if (isa<CXXThisExpr>(e->IgnoreParens())) {
1461 // We always assume that 'this' is never null.
1462 return false;
1463 }
1464
1465 if (const ImplicitCastExpr *ice = dyn_cast<ImplicitCastExpr>(ce)) {
1466 // And that glvalue casts are never null.
1467 if (ice->isGLValue())
1468 return false;
1469 }
1470
1471 return true;
1472}
1473
1474/// Computes the length of an array in elements, as well as the base
1475/// element type and a properly-typed first element pointer.
1476mlir::Value
1478 QualType &baseType, Address &addr) {
1479 const clang::ArrayType *arrayType = origArrayType;
1480
1481 // If it's a VLA, we have to load the stored size. Note that
1482 // this is the size of the VLA in bytes, not its size in elements.
1483 mlir::Value numVLAElements = nullptr;
1486
1487 // Walk into all VLAs. This doesn't require changes to addr,
1488 // which has type T* where T is the first non-VLA element type.
1489 do {
1490 QualType elementType = arrayType->getElementType();
1491 arrayType = getContext().getAsArrayType(elementType);
1492
1493 // If we only have VLA components, 'addr' requires no adjustment.
1494 if (!arrayType) {
1495 baseType = elementType;
1496 return numVLAElements;
1497 }
1499
1500 // We get out here only if we find a constant array type
1501 // inside the VLA.
1502 }
1503
1504 // Classic codegen emits an all-zero inbounds GEP to convert addr from
1505 // [M x [N x T]]* to T*. CIR doesn't need this because callers handle
1506 // the array-to-element pointer conversion themselves (via array_to_ptrdecay
1507 // casts, ptr_bitcast, or manual array type peeling).
1508
1509 uint64_t countFromCLAs = 1;
1510 QualType eltType;
1511
1512 auto cirArrayType = mlir::dyn_cast<cir::ArrayType>(addr.getElementType());
1513
1514 while (cirArrayType) {
1516 countFromCLAs *= cirArrayType.getSize();
1517 eltType = arrayType->getElementType();
1518
1519 cirArrayType =
1520 mlir::dyn_cast<cir::ArrayType>(cirArrayType.getElementType());
1521
1522 arrayType = getContext().getAsArrayType(arrayType->getElementType());
1523 assert((!cirArrayType || arrayType) &&
1524 "CIR and Clang types are out-of-sync");
1525 }
1526
1527 if (arrayType) {
1528 // From this point onwards, the Clang array type has been emitted
1529 // as some other type (probably a packed struct). Compute the array
1530 // size, and just emit the 'begin' expression as a bitcast.
1531 cgm.errorNYI(*currSrcLoc, "length for non-array underlying types");
1532 }
1533
1534 baseType = eltType;
1535
1536 mlir::Value numElements =
1537 builder.getConstInt(*currSrcLoc, sizeTy, countFromCLAs);
1538
1539 // If we had any VLA dimensions, factor them in.
1540 if (numVLAElements)
1541 numElements =
1542 builder.createMul(numVLAElements.getLoc(), numVLAElements, numElements,
1544
1545 return numElements;
1546}
1547
1549 // If we already made the indirect branch for indirect goto, return its block.
1551 return;
1552
1553 mlir::OpBuilder::InsertionGuard guard(builder);
1555 builder.createBlock(builder.getBlock()->getParent(), {}, {voidPtrTy},
1556 {builder.getUnknownLoc()});
1557}
1558
1560 mlir::Value ptrValue, QualType ty, SourceLocation loc,
1561 SourceLocation assumptionLoc, int64_t alignment, mlir::Value offsetValue) {
1563 mlir::Location assumeLoc = getLoc(assumptionLoc);
1564 mlir::Value alignValue = builder.getUInt64(alignment, assumeLoc);
1565 mlir::Value cond = builder.getBool(true, assumeLoc);
1566 llvm::SmallVector<mlir::Value> bundleArgs{ptrValue, alignValue};
1567 if (offsetValue)
1568 bundleArgs.push_back(offsetValue);
1569 cir::AssumeOp::create(builder, assumeLoc, cond, cir::AssumeBundleKind::Align,
1570 bundleArgs);
1571 return ptrValue;
1572}
1573
1575 mlir::Value ptrValue, const Expr *expr, SourceLocation assumptionLoc,
1576 int64_t alignment, mlir::Value offsetValue) {
1577 QualType ty = expr->getType();
1578 SourceLocation loc = expr->getExprLoc();
1579 return emitAlignmentAssumption(ptrValue, ty, loc, assumptionLoc, alignment,
1580 offsetValue);
1581}
1582
1584 const VariableArrayType *vla =
1585 cgm.getASTContext().getAsVariableArrayType(type);
1586 assert(vla && "type was not a variable array type!");
1587 return getVLASize(vla);
1588}
1589
1592 // The number of elements so far; always size_t.
1593 mlir::Value numElements;
1594
1595 QualType elementType;
1596 do {
1597 elementType = type->getElementType();
1598 mlir::Value vlaSize = vlaSizeMap[type->getSizeExpr()];
1599 assert(vlaSize && "no size for VLA!");
1600 assert(vlaSize.getType() == sizeTy);
1601
1602 if (!numElements) {
1603 numElements = vlaSize;
1604 } else {
1605 // It's undefined behavior if this wraps around, so mark it that way.
1606 // FIXME: Teach -fsanitize=undefined to trap this.
1607
1608 numElements =
1609 builder.createMul(numElements.getLoc(), numElements, vlaSize,
1611 }
1612 } while ((type = getContext().getAsVariableArrayType(elementType)));
1613
1614 assert(numElements && "Undefined elements number");
1615 return {numElements, elementType};
1616}
1617
1620 mlir::Value vlaSize = vlaSizeMap[vla->getSizeExpr()];
1621 assert(vlaSize && "no size for VLA!");
1622 assert(vlaSize.getType() == sizeTy);
1623 return {vlaSize, vla->getElementType()};
1624}
1625
1626// TODO(cir): Most of this function can be shared between CIRGen
1627// and traditional LLVM codegen
1629 assert(type->isVariablyModifiedType() &&
1630 "Must pass variably modified type to EmitVLASizes!");
1631
1632 // We're going to walk down into the type and look for VLA
1633 // expressions.
1634 do {
1635 assert(type->isVariablyModifiedType());
1636
1637 const Type *ty = type.getTypePtr();
1638 switch (ty->getTypeClass()) {
1639 case Type::CountAttributed:
1640 case Type::PackIndexing:
1641 case Type::ArrayParameter:
1642 case Type::HLSLAttributedResource:
1643 case Type::HLSLInlineSpirv:
1644 case Type::PredefinedSugar:
1645 cgm.errorNYI("CIRGenFunction::emitVariablyModifiedType");
1646 break;
1647
1648#define TYPE(Class, Base)
1649#define ABSTRACT_TYPE(Class, Base)
1650#define NON_CANONICAL_TYPE(Class, Base)
1651#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1652#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base)
1653#include "clang/AST/TypeNodes.inc"
1654 llvm_unreachable(
1655 "dependent type must be resolved before the CIR codegen");
1656
1657 // These types are never variably-modified.
1658 case Type::Builtin:
1659 case Type::Complex:
1660 case Type::Vector:
1661 case Type::ExtVector:
1662 case Type::ConstantMatrix:
1663 case Type::Record:
1664 case Type::Enum:
1665 case Type::Using:
1666 case Type::TemplateSpecialization:
1667 case Type::ObjCTypeParam:
1668 case Type::ObjCObject:
1669 case Type::ObjCInterface:
1670 case Type::ObjCObjectPointer:
1671 case Type::BitInt:
1672 case Type::OverflowBehavior:
1673 llvm_unreachable("type class is never variably-modified!");
1674
1675 case Type::Adjusted:
1676 type = cast<clang::AdjustedType>(ty)->getAdjustedType();
1677 break;
1678
1679 case Type::Decayed:
1680 type = cast<clang::DecayedType>(ty)->getPointeeType();
1681 break;
1682
1683 case Type::Pointer:
1684 type = cast<clang::PointerType>(ty)->getPointeeType();
1685 break;
1686
1687 case Type::BlockPointer:
1688 type = cast<clang::BlockPointerType>(ty)->getPointeeType();
1689 break;
1690
1691 case Type::LValueReference:
1692 case Type::RValueReference:
1693 type = cast<clang::ReferenceType>(ty)->getPointeeType();
1694 break;
1695
1696 case Type::MemberPointer:
1697 type = cast<clang::MemberPointerType>(ty)->getPointeeType();
1698 break;
1699
1700 case Type::ConstantArray:
1701 case Type::IncompleteArray:
1702 // Losing element qualification here is fine.
1703 type = cast<clang::ArrayType>(ty)->getElementType();
1704 break;
1705
1706 case Type::VariableArray: {
1707 // Losing element qualification here is fine.
1709
1710 // Unknown size indication requires no size computation.
1711 // Otherwise, evaluate and record it.
1712 if (const Expr *sizeExpr = vat->getSizeExpr()) {
1713 // It's possible that we might have emitted this already,
1714 // e.g. with a typedef and a pointer to it.
1715 mlir::Value &entry = vlaSizeMap[sizeExpr];
1716 if (!entry) {
1717 mlir::Value size = emitScalarExpr(sizeExpr);
1719
1720 // Always zexting here would be wrong if it weren't
1721 // undefined behavior to have a negative bound.
1722 // FIXME: What about when size's type is larger than size_t?
1723 entry = builder.createBoolIntToIntCast(size, sizeTy);
1724 }
1725 }
1726 type = vat->getElementType();
1727 break;
1728 }
1729
1730 case Type::FunctionProto:
1731 case Type::FunctionNoProto:
1732 type = cast<clang::FunctionType>(ty)->getReturnType();
1733 break;
1734
1735 case Type::Paren:
1736 case Type::TypeOf:
1737 case Type::UnaryTransform:
1738 case Type::Attributed:
1739 case Type::BTFTagAttributed:
1740 case Type::SubstTemplateTypeParm:
1741 case Type::MacroQualified:
1742 // Keep walking after single level desugaring.
1743 type = type.getSingleStepDesugaredType(getContext());
1744 break;
1745
1746 case Type::Typedef:
1747 case Type::Decltype:
1748 case Type::Auto:
1749 case Type::DeducedTemplateSpecialization:
1750 // Stop walking: nothing to do.
1751 return;
1752
1753 case Type::TypeOfExpr:
1754 // Stop walking: emit typeof expression.
1755 emitIgnoredExpr(cast<clang::TypeOfExprType>(ty)->getUnderlyingExpr());
1756 return;
1757
1758 case Type::Atomic:
1759 type = cast<clang::AtomicType>(ty)->getValueType();
1760 break;
1761
1762 case Type::Pipe:
1763 type = cast<clang::PipeType>(ty)->getElementType();
1764 break;
1765 }
1766 } while (type->isVariablyModifiedType());
1767}
1768
1770 if (getContext().getBuiltinVaListType()->isArrayType())
1771 return emitPointerWithAlignment(e);
1772 return emitLValue(e).getAddress();
1773}
1774
1775} // namespace clang::CIRGen
Defines the clang::Expr interface and subclasses for C++ expressions.
*collection of selector each with an associated kind and an ordered *collection of selectors A selector has a kind
APSInt & getInt()
Definition APValue.h:508
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition ASTContext.h:223
SourceManager & getSourceManager()
Definition ASTContext.h:863
CharUnits getDeclAlign(const Decl *D, bool ForAlignof=false) const
Return a conservative estimate of the alignment of the specified decl D.
const ArrayType * getAsArrayType(QualType T) const
Type Query functions.
CharUnits getTypeSizeInChars(QualType T) const
Return the size of the specified (complete) type T, in characters.
Represents an array type, per C99 6.7.5.2 - Array Declarators.
Definition TypeBase.h:3786
QualType getElementType() const
Definition TypeBase.h:3798
Address withPointer(mlir::Value newPtr) const
Return address with different pointer, but same element type and alignment.
Definition Address.h:83
mlir::Value getPointer() const
Definition Address.h:98
mlir::Type getElementType() const
Definition Address.h:125
static Address invalid()
Definition Address.h:76
An aggregate value slot.
static AggValueSlot ignored()
Returns an aggregate value slot indicating that the aggregate value is being ignored.
mlir::Value createFloatingCast(mlir::Value v, mlir::Type destType)
CIRGenFPOptionsRAII(CIRGenFunction &cgf, FPOptions FPFeatures)
void exit(ArrayRef< mlir::Value * > valuesToReload={})
A non-RAII class containing all the information about a bound opaque value.
Enters a new scope for capturing cleanups, all of which will be executed once the scope is exited.
void forceCleanup(ArrayRef< mlir::Value * > valuesToReload={})
Force the emission of cleanups now, instead of waiting until this object is destroyed.
static bool isConstructorDelegationValid(const clang::CXXConstructorDecl *ctor)
Checks whether the given constructor is a valid subject for the complete-to-base constructor delegati...
void emitFunctionProlog(const FunctionArgList &args, mlir::Block *entryBB, const FunctionDecl *fd, SourceLocation bodyBeginLoc)
Emit the function prologue: declare function arguments in the symbol table.
mlir::Type convertType(clang::QualType t)
LValue emitCXXBindTemporaryLValue(const CXXBindTemporaryExpr *e)
LValue emitOpaqueValueLValue(const OpaqueValueExpr *e)
static cir::TypeEvaluationKind getEvaluationKind(clang::QualType type)
Return the cir::TypeEvaluationKind of QualType type.
clang::GlobalDecl curGD
The GlobalDecl for the current function being compiled or the global variable currently being initial...
EHScopeStack::stable_iterator prologueCleanupDepth
The cleanup depth enclosing all the cleanups associated with the parameters.
cir::FuncOp generateCode(clang::GlobalDecl gd, cir::FuncOp fn, cir::FuncType funcType)
CIRGenTypes & getTypes() const
Address emitPointerWithAlignment(const clang::Expr *expr, LValueBaseInfo *baseInfo=nullptr)
Given an expression with a pointer type, emit the value and compute our best estimate of the alignmen...
void emitVariablyModifiedType(QualType ty)
RValue emitLoadOfLValue(LValue lv, SourceLocation loc)
Given an expression that represents a value lvalue, this method emits the address of the lvalue,...
const clang::LangOptions & getLangOpts() const
void emitTrap(mlir::Location loc, bool createNewBlock)
Emit a trap instruction, which is used to abort the program in an abnormal way, usually for debugging...
VlaSizePair getVLASize(const VariableArrayType *type)
Returns an MLIR::Value+QualType pair that corresponds to the size, in non-variably-sized elements,...
LValue makeNaturalAlignPointeeAddrLValue(mlir::Value v, clang::QualType t)
Given a value of type T* that may not be to a complete object, construct an l-vlaue withi the natural...
LValue emitMemberExpr(const MemberExpr *e)
const TargetInfo & getTarget() const
LValue emitConditionalOperatorLValue(const AbstractConditionalOperator *expr)
LValue emitLValue(const clang::Expr *e)
Emit code to compute a designator that specifies the location of the expression.
const clang::Decl * curFuncDecl
LValue emitLValueForLambdaField(const FieldDecl *field)
LValue makeNaturalAlignAddrLValue(mlir::Value val, QualType ty)
llvm::DenseMap< const Expr *, mlir::Value > vlaSizeMap
bool constantFoldsToSimpleInteger(const clang::Expr *cond, llvm::APSInt &resultInt, bool allowLabels=false)
If the specified expression does not fold to a constant, or if it does fold but contains a label,...
LValue emitComplexCompoundAssignmentLValue(const CompoundAssignOperator *e)
mlir::Location getLoc(clang::SourceLocation srcLoc)
Helpers to convert Clang's SourceLocation to a MLIR Location.
bool constantFoldsToBool(const clang::Expr *cond, bool &resultBool, bool allowLabels=false)
If the specified expression does not fold to a constant, or if it does but contains a label,...
void emitDelegateCXXConstructorCall(const clang::CXXConstructorDecl *ctor, clang::CXXCtorType ctorType, const FunctionArgList &args, clang::SourceLocation loc)
VlaSizePair getVLAElements1D(const VariableArrayType *vla)
Return the number of elements for a single dimension for the given array type.
mlir::Value emitArrayLength(const clang::ArrayType *arrayType, QualType &baseType, Address &addr)
Computes the length of an array in elements, as well as the base element type and a properly-typed fi...
void emitNullInitialization(mlir::Location loc, Address destPtr, QualType ty)
LValue emitArraySubscriptExpr(const clang::ArraySubscriptExpr *e)
llvm::ScopedHashTableScope< const clang::Decl *, mlir::Value > SymTableScopeTy
mlir::Block * indirectGotoBlock
IndirectBranch - The first time an indirect goto is seen we create a block reserved for the indirect ...
mlir::Operation * curFn
The current function or global initializer that is generated code for.
EHScopeStack ehStack
Tracks function scope overall cleanup handling.
void enterDtorCleanups(const CXXDestructorDecl *dtor, CXXDtorType type)
Enter the cleanups necessary to complete the given phase of destruction for a destructor.
llvm::SmallVector< const ParmVarDecl * > fnArgs
Save Parameter Decl for coroutine.
llvm::SmallVector< PendingCleanupEntry > deferredConditionalCleanupStack
std::optional< mlir::Value > fnRetAlloca
The compiler-generated variable that holds the return value.
void emitImplicitAssignmentOperatorBody(FunctionArgList &args)
mlir::Type convertTypeForMem(QualType t)
mlir::LogicalResult emitCXXTryStmt(const clang::CXXTryStmt &s, cxxTryBodyEmitter &bodyCallback)
clang::QualType buildFunctionArgList(clang::GlobalDecl gd, FunctionArgList &args)
void emitCtorPrologue(const clang::CXXConstructorDecl *ctor, clang::CXXCtorType ctorType, FunctionArgList &args)
This routine generates necessary code to initialize base classes and non-static data members belongin...
mlir::Value emitAlloca(llvm::StringRef name, mlir::Type ty, mlir::Location loc, clang::CharUnits alignment, bool insertIntoFnEntryBlock, mlir::Value arraySize=nullptr)
LValue emitAggExprToLValue(const Expr *e)
LValue emitCompoundAssignmentLValue(const clang::CompoundAssignOperator *e)
Address returnValue
The temporary alloca to hold the return value.
static bool hasAggregateEvaluationKind(clang::QualType type)
void finishFunction(SourceLocation endLoc)
mlir::LogicalResult emitFunctionBody(const clang::Stmt *body)
LValue emitUnaryOpLValue(const clang::UnaryOperator *e)
clang::FieldDecl * lambdaThisCaptureField
const clang::Decl * curCodeDecl
This is the inner-most code context, which includes blocks.
void emitConstructorBody(FunctionArgList &args)
LValue emitCallExprLValue(const clang::CallExpr *e)
bool haveInsertPoint() const
True if an insertion point is defined.
LValue emitStringLiteralLValue(const StringLiteral *e, llvm::StringRef name=".str")
llvm::SmallDenseMap< const ParmVarDecl *, const ImplicitParamDecl * > sizeArguments
If a ParmVarDecl had the pass_object_size attribute, this will contain a mapping from said ParmVarDec...
mlir::Value emitScalarExpr(const clang::Expr *e, bool ignoreResultAssign=false)
Emit the computation of the specified expression of scalar type.
LValue emitPseudoObjectLValue(const PseudoObjectExpr *E)
void popCleanupBlocks(EHScopeStack::stable_iterator oldCleanupStackDepth, ArrayRef< mlir::Value * > valuesToReload={})
Takes the old cleanup stack size and emits the cleanup blocks that have been added.
bool shouldNullCheckClassCastValue(const CastExpr *ce)
CIRGenBuilderTy & getBuilder()
bool didCallStackSave
Whether a cir.stacksave operation has been added.
LValue emitBinaryOperatorLValue(const BinaryOperator *e)
void startFunction(clang::GlobalDecl gd, clang::QualType returnType, cir::FuncOp fn, cir::FuncType funcType, FunctionArgList args, clang::SourceLocation loc, clang::SourceLocation startLoc)
Emit code for the start of a function.
unsigned counterRefTmp
Hold counters for incrementally naming temporaries.
mlir::MLIRContext & getMLIRContext()
void emitDestructorBody(FunctionArgList &args)
Emits the body of the current destructor.
LValue emitInitListLValue(const InitListExpr *e)
LValue emitCastLValue(const CastExpr *e)
Casts are never lvalues unless that cast is to a reference type.
LValue emitCXXTypeidLValue(const CXXTypeidExpr *e)
bool containsLabel(const clang::Stmt *s, bool ignoreCaseStmts=false)
Return true if the statement contains a label in it.
RValue emitPseudoObjectRValue(const PseudoObjectExpr *e, AggValueSlot slot=AggValueSlot::ignored())
LValue emitDeclRefLValue(const clang::DeclRefExpr *e)
llvm::DenseMap< const clang::ValueDecl *, clang::FieldDecl * > lambdaCaptureFields
mlir::Value emitAlignmentAssumption(mlir::Value ptrValue, QualType ty, SourceLocation loc, SourceLocation assumptionLoc, int64_t alignment, mlir::Value offsetValue=nullptr)
LValue makeAddrLValue(Address addr, QualType ty, AlignmentSource source=AlignmentSource::Type)
LValue emitPredefinedLValue(const PredefinedExpr *e)
RValue emitAnyExpr(const clang::Expr *e, AggValueSlot aggSlot=AggValueSlot::ignored(), bool ignoreResult=false)
Emit code to compute the specified expression which can have any type.
void emitCXXDestructorCall(const CXXDestructorDecl *dd, CXXDtorType type, bool forVirtualBase, bool delegating, Address thisAddr, QualType thisTy)
void emitLambdaStaticInvokeBody(const CXXMethodDecl *md)
CIRGenFunction(CIRGenModule &cgm, CIRGenBuilderTy &builder, bool suppressNewContext=false)
std::optional< mlir::Location > currSrcLoc
Use to track source locations across nested visitor traversals.
LValue emitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *e)
LValue emitExtVectorElementExpr(const ExtVectorElementExpr *e)
clang::ASTContext & getContext() const
void setAddrOfLocalVar(const clang::VarDecl *vd, Address addr)
Set the address of a local variable.
mlir::LogicalResult emitStmt(const clang::Stmt *s, bool useCurrentScope, llvm::ArrayRef< const Attr * > attrs={})
Address emitVAListRef(const Expr *e)
Build a "reference" to a va_list; this is either the address or the value of the expression,...
mlir::LogicalResult emitCompoundStmtWithoutScope(const clang::CompoundStmt &s, Address *lastValue=nullptr, AggValueSlot slot=AggValueSlot::ignored())
void emitIgnoredExpr(const clang::Expr *e)
Emit code to compute the specified expression, ignoring the result.
Address createMemTemp(QualType t, mlir::Location loc, const Twine &name="tmp", Address *alloca=nullptr, mlir::OpBuilder::InsertPoint ip={})
Create a temporary memory object of the given type, with appropriate alignmen and cast it to the defa...
LValue emitCXXConstructLValue(const CXXConstructExpr *e)
LValue emitCompoundLiteralLValue(const CompoundLiteralExpr *e)
This class organizes the cross-function state that is used while generating CIR code.
DiagnosticBuilder errorNYI(SourceLocation, llvm::StringRef)
Helpers to emit "not yet implemented" error diagnostics.
bool inheritingCtorHasParams(const InheritedConstructor &inherited, CXXCtorType type)
Determine if a C++ inheriting constructor should have parameters matching those of its inherited cons...
Type for representing both the decl and type of parameters to a function.
Definition CIRGenCall.h:193
Address getAddress() const
static LValue makeAddr(Address address, clang::QualType t, LValueBaseInfo baseInfo)
clang::QualType getType() const
mlir::Value getPointer() const
LValueBaseInfo getBaseInfo() const
bool isSimple() const
This trivial value class is used to represent the result of an expression that is evaluated.
Definition CIRGenValue.h:33
mlir::Value getValue() const
Return the value of this scalar value.
Definition CIRGenValue.h:57
Represents a C++ constructor within a class.
Definition DeclCXX.h:2633
Represents a C++ destructor within a class.
Definition DeclCXX.h:2895
const CXXRecordDecl * getParent() const
Return the parent of this method declaration, which is the class in which this method is defined.
Definition DeclCXX.h:2284
QualType getFunctionObjectParameterType() const
Definition DeclCXX.h:2308
bool isAbstract() const
Determine whether this class has a pure virtual function.
Definition DeclCXX.h:1226
bool isEmpty() const
Determine whether this is an empty class in the sense of (C++11 [meta.unary.prop]).
Definition DeclCXX.h:1191
CastExpr - Base class for type casts, including both implicit casts (ImplicitCastExpr) and explicit c...
Definition Expr.h:3679
CastKind getCastKind() const
Definition Expr.h:3723
Expr * getSubExpr()
Definition Expr.h:3729
CharUnits - This is an opaque type for sizes expressed in character units.
Definition CharUnits.h:38
bool isZero() const
isZero - Test whether the quantity equals zero.
Definition CharUnits.h:122
CompoundStmt - This represents a group of statements like { stmt stmt }.
Definition Stmt.h:1750
bool isImplicit() const
isImplicit - Indicates whether the declaration was implicitly generated by the implementation.
Definition DeclBase.h:601
Decl * getNonClosureContext()
Find the innermost non-closure ancestor of this declaration, walking up through blocks,...
SourceLocation getLocation() const
Definition DeclBase.h:447
bool hasAttr() const
Definition DeclBase.h:585
This represents one expression.
Definition Expr.h:112
bool EvaluateAsInt(EvalResult &Result, const ASTContext &Ctx, SideEffectsKind AllowSideEffects=SE_NoSideEffects, bool InConstantContext=false) const
EvaluateAsInt - Return true if this is a constant which we can fold and convert to an integer,...
bool isGLValue() const
Definition Expr.h:287
FPOptions getFPFeaturesInEffect(const LangOptions &LO) const
Returns the set of floating point options that apply to this expression.
Definition Expr.cpp:3998
Expr * IgnoreParens() LLVM_READONLY
Skip past any parentheses which might surround this expression until reaching a fixed point.
Definition Expr.cpp:3095
bool isPRValue() const
Definition Expr.h:285
QualType getType() const
Definition Expr.h:144
LangOptions::FPExceptionModeKind getExceptionMode() const
RoundingMode getRoundingMode() const
Represents a function declaration or definition.
Definition Decl.h:2018
Stmt * getBody(const FunctionDecl *&Definition) const
Retrieve the body (definition) of the function.
Definition Decl.cpp:3253
SourceRange getSourceRange() const override LLVM_READONLY
Source range that this declaration covers.
Definition Decl.cpp:4543
bool hasBody(const FunctionDecl *&Definition) const
Returns true if the function has a body.
Definition Decl.cpp:3173
FunctionDecl * getPreviousDecl()
Return the previous declaration of this declaration or NULL if this is the first declaration.
GlobalDecl - represents a global declaration.
Definition GlobalDecl.h:57
CXXCtorType getCtorType() const
Definition GlobalDecl.h:108
const Decl * getDecl() const
Definition GlobalDecl.h:106
ImplicitCastExpr - Allows us to explicitly represent implicit type conversions, which have no direct ...
Definition Expr.h:3856
static ImplicitParamDecl * Create(ASTContext &C, DeclContext *DC, SourceLocation IdLoc, const IdentifierInfo *Id, QualType T, ImplicitParamKind ParamKind)
Create implicit parameter.
Definition Decl.cpp:5596
Describes an C or C++ initializer list.
Definition Expr.h:5302
bool isTransparent() const
Is this a transparent initializer list (that is, an InitListExpr that is purely syntactic,...
Definition Expr.cpp:2471
const Expr * getInit(unsigned Init) const
Definition Expr.h:5357
FPExceptionModeKind
Possible floating point exception behavior.
@ FPE_Default
Used internally to represent initial unspecified value.
@ FPE_Strict
Strictly preserve the floating-point exception semantics.
@ FPE_MayTrap
Transformations do not cause new exceptions but may hide some.
@ FPE_Ignore
Assume that floating-point exceptions are masked.
Represents an unpacked "presumed" location which can be presented to the user.
unsigned getColumn() const
Return the presumed column number of this location.
const char * getFilename() const
Return the presumed filename of this location.
unsigned getLine() const
Return the presumed line number of this location.
PseudoObjectExpr - An expression which accesses a pseudo-object l-value.
Definition Expr.h:6804
Expr * getResultExpr()
Return the result-bearing expression, or null if there is none.
Definition Expr.h:6852
ArrayRef< Expr * > semantics()
Definition Expr.h:6876
A (possibly-)qualified type.
Definition TypeBase.h:937
bool isTriviallyCopyableType(const ASTContext &Context) const
Return true if this is a trivially copyable type (C++0x [basic.types]p9)
Definition Type.cpp:2967
QualType getUnqualifiedType() const
Retrieve the unqualified variant of the given type, removing as little sugar as possible.
Definition TypeBase.h:8541
Encodes a location in the source.
bool isValid() const
Return true if this is a valid SourceLocation object.
This class handles loading and caching of source files into memory.
PresumedLoc getPresumedLoc(SourceLocation Loc, bool UseLineDirectives=true) const
Returns the "presumed" location of a SourceLocation specifies.
A trivial tuple used to represent a source range.
SourceLocation getEnd() const
SourceLocation getBegin() const
Stmt - This represents one statement.
Definition Stmt.h:86
child_iterator child_begin()
Definition Stmt.h:1601
StmtClass getStmtClass() const
Definition Stmt.h:1503
SourceRange getSourceRange() const LLVM_READONLY
SourceLocation tokens are not useful in isolation - they are low level value objects created/interpre...
Definition Stmt.cpp:343
child_iterator child_end()
Definition Stmt.h:1602
SourceLocation getBeginLoc() const LLVM_READONLY
Definition Stmt.cpp:355
bool isVoidType() const
Definition TypeBase.h:9050
CXXRecordDecl * getAsCXXRecordDecl() const
Retrieves the CXXRecordDecl that this type refers to, either because the type is a RecordType or beca...
Definition Type.h:26
bool isAnyComplexType() const
Definition TypeBase.h:8819
bool isVariablyModifiedType() const
Whether this type is a variably-modified type (C99 6.7.5).
Definition TypeBase.h:2864
TypeClass getTypeClass() const
Definition TypeBase.h:2445
const T * getAs() const
Member-template getAs<specific type>'.
Definition TypeBase.h:9277
QualType getType() const
Definition Decl.h:723
Represents a variable declaration or definition.
Definition Decl.h:924
SourceRange getSourceRange() const override LLVM_READONLY
Source range that this declaration covers.
Definition Decl.cpp:2169
Represents a C array with a specified size that is not an integer-constant-expression.
Definition TypeBase.h:4030
Expr * getSizeExpr() const
Definition TypeBase.h:4044
@ Type
The l-value was considered opaque, so the alignment was determined from a type.
@ Decl
The l-value was an access to a declared entity or something equivalently strong, like the address of ...
static bool previousOpIsNonYieldingCleanup(mlir::Block *block)
static std::string getVersionedTmpName(llvm::StringRef name, unsigned cnt)
static bool mayDropFunctionReturn(const ASTContext &astContext, QualType returnType)
static mlir::Value emitArgumentDemotion(CIRGenFunction &cgf, const VarDecl *var, mlir::Value value)
An argument came in as a promoted argument; demote it back to its declared type.
static void eraseEmptyAndUnusedBlocks(cir::FuncOp func)
static std::variant< LValue, RValue > emitPseudoObjectExpr(CIRGenFunction &cgf, const PseudoObjectExpr *e, bool forLValue, AggValueSlot slot)
static llvm::fp::ExceptionBehavior toConstrainedExceptMd(LangOptions::FPExceptionModeKind kind)
const internal::VariadicAllOfMatcher< Type > type
Matches Types in the clang AST.
const AstTypeMatcher< ArrayType > arrayType
const internal::VariadicDynCastAllOfMatcher< Stmt, Expr > expr
Matches expressions.
CXXCtorType
C++ constructor types.
Definition ABI.h:24
@ Ctor_Base
Base object ctor.
Definition ABI.h:26
@ Ctor_Complete
Complete object ctor.
Definition ABI.h:25
bool isa(CodeGen::Address addr)
Definition Address.h:330
@ CPlusPlus
CXXDtorType
C++ destructor types.
Definition ABI.h:34
@ Dtor_VectorDeleting
Vector deleting dtor.
Definition ABI.h:40
@ Dtor_Comdat
The COMDAT used for dtors.
Definition ABI.h:38
@ Dtor_Unified
GCC-style unified dtor.
Definition ABI.h:39
@ Dtor_Base
Base object dtor.
Definition ABI.h:37
@ Dtor_Complete
Complete object dtor.
Definition ABI.h:36
@ Dtor_Deleting
Deleting dtor.
Definition ABI.h:35
U cast(CodeGen::Address addr)
Definition Address.h:327
@ Other
Other implicit parameter.
Definition Decl.h:1763
static bool fastMathFuncAttributes()
static bool vtableInitialization()
static bool constructABIArgDirectExtend()
static bool runCleanupsScope()
static bool emitTypeCheck()
static bool fastMathGuard()
static bool fastMathFlags()
static bool generateDebugInfo()
static bool incrementProfileCounter()
Represents a scope, including function bodies, compound statements, and the substatements of if/while...
llvm::ArrayRef< mlir::Block * > getRetBlocks()
LexicalScope(CIRGenFunction &cgf, mlir::Location loc, mlir::Block *eb)
mlir::Location getRetLoc(mlir::Block *b)
EvalResult is a struct with detailed info about an evaluated expression.
Definition Expr.h:648
APValue Val
Val - This is the value the expression can be folded to.
Definition Expr.h:650