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 // If the builder's current block lives in a region nested below this
279 // lexical scope's region, popCleanup has left the insertion point past
280 // a cir.cleanup.scope inside that nested region (for example, inside a
281 // cir.for body when the body cleanup was registered there). Emitting a
282 // cir.yield at the current position would terminate the nested region
283 // instead of this scope's region. Reposition to the back block of this
284 // scope's region so the terminator below lands in the correct place.
285 if (mlir::Region *scopeRegion = entryBlock->getParent();
286 scopeRegion && !scopeRegion->empty() &&
287 curBlock->getParent() != scopeRegion) {
288 builder.setInsertionPointToEnd(&scopeRegion->back());
289 curBlock = builder.getBlock();
290 if (curBlock->mightHaveTerminator() && curBlock->getTerminator())
291 return;
292 }
293
294 // Get rid of any empty block at the end of the scope. An empty non-entry
295 // block is created when a terminator (return/break/continue) is followed
296 // by unreachable code.
297 bool isEntryBlock = builder.getInsertionBlock()->isEntryBlock();
298 if (!isEntryBlock && curBlock->empty()) {
299 curBlock->erase();
300 for (mlir::Block *retBlock : retBlocks) {
301 if (retBlock->getUses().empty())
302 retBlock->erase();
303 }
304 return;
305 }
306
307 if (localScope->depth == 0) {
308 // Reached the end of the function.
309 if (localScope->getRetBlocks().size() == 1) {
310 mlir::Block *retBlock = localScope->getRetBlocks()[0];
311 mlir::Location retLoc = localScope->getRetLoc(retBlock);
312 if (retBlock->getUses().empty()) {
313 retBlock->erase();
314 } else {
315 cir::BrOp::create(builder, retLoc, retBlock);
316 return;
317 }
318 }
319 emitImplicitReturn();
320 return;
321 }
322
323 // End of any local scope != function.
324 // Ternary ops have to deal with matching arms for yielding types
325 // and do return a value, it must do its own cir.yield insertion.
326 if (!localScope->isTernary() && !curBlock->mightHaveTerminator()) {
327 !retVal ? cir::YieldOp::create(builder, localScope->endLoc)
328 : cir::YieldOp::create(builder, localScope->endLoc, retVal);
329 }
330}
331
332cir::ReturnOp CIRGenFunction::LexicalScope::emitReturn(mlir::Location loc) {
333 CIRGenBuilderTy &builder = cgf.getBuilder();
334
335 auto fn = dyn_cast<cir::FuncOp>(cgf.curFn);
336 assert(fn && "emitReturn from non-function");
337
338 if (!fn.getFunctionType().hasVoidReturn()) {
339 // Load the value from `__retval` and return it via the `cir.return` op.
340 auto value = cir::LoadOp::create(
341 builder, loc, fn.getFunctionType().getReturnType(), *cgf.fnRetAlloca);
342 return cir::ReturnOp::create(builder, loc,
343 llvm::ArrayRef(value.getResult()));
344 }
345 return cir::ReturnOp::create(builder, loc);
346}
347
348// This is copied from CodeGenModule::MayDropFunctionReturn. This is a
349// candidate for sharing between CIRGen and CodeGen.
350static bool mayDropFunctionReturn(const ASTContext &astContext,
351 QualType returnType) {
352 // We can't just discard the return value for a record type with a complex
353 // destructor or a non-trivially copyable type.
354 if (const auto *classDecl = returnType->getAsCXXRecordDecl())
355 return classDecl->hasTrivialDestructor();
356 return returnType.isTriviallyCopyableType(astContext);
357}
358
359static bool previousOpIsNonYieldingCleanup(mlir::Block *block) {
360 if (block->empty())
361 return false;
362 mlir::Operation *op = &block->back();
363 auto cleanupScopeOp = mlir::dyn_cast<cir::CleanupScopeOp>(op);
364 if (!cleanupScopeOp)
365 return false;
366
367 // Check whether the body region of the cleanup scope exits via cir.yield.
368 // Exits via cir.return or cir.goto do not fall through to the operation
369 // following the cleanup scope, and exits via break, continue, and resume
370 // are not expected here.
371 for (mlir::Block &bodyBlock : cleanupScopeOp.getBodyRegion()) {
372 if (bodyBlock.mightHaveTerminator()) {
373 if (mlir::isa<cir::YieldOp>(bodyBlock.getTerminator()))
374 return false;
375 assert(!mlir::isa<cir::BreakOp>(bodyBlock.getTerminator()) &&
376 !mlir::isa<cir::ContinueOp>(bodyBlock.getTerminator()) &&
377 !mlir::isa<cir::ResumeOp>(bodyBlock.getTerminator()));
378 }
379 }
380 return true;
381}
382
383void CIRGenFunction::LexicalScope::emitImplicitReturn() {
384 CIRGenBuilderTy &builder = cgf.getBuilder();
385 LexicalScope *localScope = cgf.curLexScope;
386
387 const auto *fd = cast<clang::FunctionDecl>(cgf.curGD.getDecl());
388
389 // In C++, flowing off the end of a non-void function is always undefined
390 // behavior. In C, flowing off the end of a non-void function is undefined
391 // behavior only if the non-existent return value is used by the caller.
392 // That influences whether the terminating op is trap, unreachable, or
393 // return.
394 if (cgf.getLangOpts().CPlusPlus && !fd->hasImplicitReturnZero() &&
395 !cgf.sawAsmBlock && !fd->getReturnType()->isVoidType() &&
396 builder.getInsertionBlock() &&
397 !previousOpIsNonYieldingCleanup(builder.getInsertionBlock())) {
398 bool shouldEmitUnreachable =
399 cgf.cgm.getCodeGenOpts().StrictReturn ||
400 !mayDropFunctionReturn(fd->getASTContext(), fd->getReturnType());
401
402 if (shouldEmitUnreachable) {
404 if (cgf.cgm.getCodeGenOpts().OptimizationLevel == 0)
405 cir::TrapOp::create(builder, localScope->endLoc);
406 else
407 cir::UnreachableOp::create(builder, localScope->endLoc);
408 builder.clearInsertionPoint();
409 return;
410 }
411 }
412
413 (void)emitReturn(localScope->endLoc);
414}
415
417 LexicalScope *scope = this;
418 while (scope) {
419 if (scope->isTry())
420 return scope->getTry();
421 scope = scope->parentScope;
422 }
423 return nullptr;
424}
425
426/// An argument came in as a promoted argument; demote it back to its
427/// declared type.
428static mlir::Value emitArgumentDemotion(CIRGenFunction &cgf, const VarDecl *var,
429 mlir::Value value) {
430 mlir::Type ty = cgf.convertType(var->getType());
431
432 // This can happen with promotions that actually don't change the
433 // underlying type, like the enum promotions.
434 if (value.getType() == ty)
435 return value;
436
437 assert((mlir::isa<cir::IntType>(ty) || cir::isAnyFloatingPointType(ty)) &&
438 "unexpected promotion type");
439
440 if (mlir::isa<cir::IntType>(ty))
441 return cgf.getBuilder().CIRBaseBuilderTy::createIntCast(value, ty);
442
443 return cgf.getBuilder().createFloatingCast(value, ty);
444}
445
447 mlir::Block *entryBB,
448 const FunctionDecl *fd,
449 SourceLocation bodyBeginLoc) {
450 // Naked functions don't have prologues.
451 if (fd && fd->hasAttr<NakedAttr>()) {
452 cgm.errorNYI(bodyBeginLoc, "naked function decl");
453 }
454
455 // Declare all the function arguments in the symbol table.
456 for (const auto nameValue : llvm::zip(args, entryBB->getArguments())) {
457 const VarDecl *paramVar = std::get<0>(nameValue);
458 mlir::Value paramVal = std::get<1>(nameValue);
459 CharUnits alignment = getContext().getDeclAlign(paramVar);
460 mlir::Location paramLoc = getLoc(paramVar->getSourceRange());
461 paramVal.setLoc(paramLoc);
462
463 mlir::Value addrVal =
464 emitAlloca(cast<NamedDecl>(paramVar)->getName(),
465 convertType(paramVar->getType()), paramLoc, alignment,
466 /*insertIntoFnEntryBlock=*/true);
467
468 declare(addrVal, paramVar, paramVar->getType(), paramLoc, alignment,
469 /*isParam=*/true);
470
471 setAddrOfLocalVar(paramVar, Address(addrVal, alignment));
472
473 bool isPromoted = isa<ParmVarDecl>(paramVar) &&
474 cast<ParmVarDecl>(paramVar)->isKNRPromoted();
476 if (isPromoted)
477 paramVal = emitArgumentDemotion(*this, paramVar, paramVal);
478
479 // Location of the store to the param storage tracked as beginning of
480 // the function body.
481 mlir::Location fnBodyBegin = getLoc(bodyBeginLoc);
482 builder.CIRBaseBuilderTy::createStore(fnBodyBegin, paramVal, addrVal);
483 }
484 assert(builder.getInsertionBlock() && "Should be valid");
485}
486
488 cir::FuncOp fn, cir::FuncType funcType,
490 SourceLocation startLoc) {
491 assert(!curFn &&
492 "CIRGenFunction can only be used for one function at a time");
493
494 curFn = fn;
495
496 const Decl *d = gd.getDecl();
497
498 didCallStackSave = false;
499 curCodeDecl = d;
500 const auto *fd = dyn_cast_or_null<FunctionDecl>(d);
501 curFuncDecl = (d ? d->getNonClosureContext() : nullptr);
502
503 prologueCleanupDepth = ehStack.stable_begin();
504
505 mlir::Block *entryBB = &fn.getBlocks().front();
506 builder.setInsertionPointToStart(entryBB);
507
508 // Determine the function body begin location for the prolog.
509 // If fd is null or has no body, use startLoc as fallback.
510 SourceLocation bodyBeginLoc = startLoc;
511 if (fd) {
512 if (Stmt *body = fd->getBody())
513 bodyBeginLoc = body->getBeginLoc();
514 else
515 bodyBeginLoc = fd->getLocation();
516 }
517
518 emitFunctionProlog(args, entryBB, fd, bodyBeginLoc);
519
520 // When the current function is not void, create an address to store the
521 // result value.
522 if (!returnType->isVoidType()) {
523 // Determine the function body end location.
524 // If fd is null or has no body, use loc as fallback.
525 SourceLocation bodyEndLoc = loc;
526 if (fd) {
527 if (Stmt *body = fd->getBody())
528 bodyEndLoc = body->getEndLoc();
529 else
530 bodyEndLoc = fd->getLocation();
531 }
532 emitAndUpdateRetAlloca(returnType, getLoc(bodyEndLoc),
533 getContext().getTypeAlignInChars(returnType));
534
535 // If this is an implicit-return-zero function, initialize the return
536 // value. This mirrors the implicit-return-zero handling in classic
537 // codegen's EmitFunctionProlog (CGCall.cpp). It is done here, after
538 // emitAndUpdateRetAlloca, because in CIR the return slot is created
539 // after the prolog (the opposite of classic codegen, where ReturnValue
540 // is set up before EmitFunctionProlog runs).
541 // TODO(cir): Align prolog handling with classic codegen.
542 if (fd && fd->hasImplicitReturnZero()) {
543 mlir::Type cirRetTy = convertType(returnType.getUnqualifiedType());
544 mlir::Location bodyBeginMLIRLoc = getLoc(bodyBeginLoc);
545 mlir::Value zero = builder.getNullValue(cirRetTy, bodyBeginMLIRLoc);
546 builder.CIRBaseBuilderTy::createStore(bodyBeginMLIRLoc, zero,
547 returnValue.getPointer());
548 }
549 }
550
551 // Only implicit-object member functions (without an explicit `this`
552 // parameter) receive an implicit `this` argument that the CXXABI prolog has
553 // to set up. C++23 explicit-object members (P0847R7) carry their object via a
554 // regular parameter and use the standard parameter prolog instead.
555 if (isa_and_nonnull<CXXMethodDecl>(d) &&
556 cast<CXXMethodDecl>(d)->isImplicitObjectMemberFunction()) {
557 cgm.getCXXABI().emitInstanceFunctionProlog(loc, *this);
558
559 const auto *md = cast<CXXMethodDecl>(d);
560 if (md->getParent()->isLambda() && md->getOverloadedOperator() == OO_Call) {
561 // We're in a lambda.
562 auto fn = dyn_cast<cir::FuncOp>(curFn);
563 assert(fn && "lambda in non-function region");
564 fn.setLambda(true);
565
566 // Figure out the captures.
567 md->getParent()->getCaptureFields(lambdaCaptureFields,
570 // If the lambda captures the object referred to by '*this' - either by
571 // value or by reference, make sure CXXThisValue points to the correct
572 // object.
573
574 // Get the lvalue for the field (which is a copy of the enclosing object
575 // or contains the address of the enclosing object).
576 LValue thisFieldLValue =
578 if (!lambdaThisCaptureField->getType()->isPointerType()) {
579 // If the enclosing object was captured by value, just use its
580 // address. Sign this pointer.
581 cxxThisValue = thisFieldLValue.getPointer();
582 } else {
583 // Load the lvalue pointed to by the field, since '*this' was captured
584 // by reference.
586 emitLoadOfLValue(thisFieldLValue, SourceLocation()).getValue();
587 }
588 }
589 for (auto *fd : md->getParent()->fields()) {
590 if (fd->hasCapturedVLAType())
591 cgm.errorNYI(loc, "lambda captured VLA type");
592 }
593 } else {
594 // Not in a lambda; just use 'this' from the method.
595 // FIXME: Should we generate a new load for each use of 'this'? The fast
596 // register allocator would be happier...
598 }
599
602 }
603
604 // If any of the arguments have a variably modified type, make sure to
605 // emit the type size, but only if the function is not naked. Naked functions
606 // have no prolog to run this evaluation.
607 if (!fd || !fd->hasAttr<NakedAttr>()) {
608 for (const VarDecl *vd : args) {
609 // Dig out the type as written from ParmVarDecls; it's unclear whether
610 // the standard (C99 6.9.1p10) requires this, but we're following the
611 // precedent set by gcc.
612 QualType ty;
613 if (const auto *pvd = dyn_cast<ParmVarDecl>(vd))
614 ty = pvd->getOriginalType();
615 else
616 ty = vd->getType();
617 if (ty->isVariablyModifiedType())
619 }
620 }
621}
622
624 for (cir::BlockAddressOp &blockAddress : cgm.unresolvedBlockAddressToLabel) {
625 cir::LabelOp labelOp =
626 cgm.lookupBlockAddressInfo(blockAddress.getBlockAddrInfo());
627 assert(labelOp && "expected cir.labelOp to already be emitted");
628 cgm.updateResolvedBlockAddress(blockAddress, labelOp);
629 }
630 cgm.unresolvedBlockAddressToLabel.clear();
631}
632
635 return;
638 mlir::OpBuilder::InsertionGuard guard(builder);
639 builder.setInsertionPointToEnd(indirectGotoBlock);
640 for (auto &[blockAdd, labelOp] : cgm.blockAddressToLabel) {
641 succesors.push_back(labelOp->getBlock());
642 rangeOperands.push_back(labelOp->getBlock()->getArguments());
643 }
644 cir::IndirectBrOp::create(builder, builder.getUnknownLoc(),
645 indirectGotoBlock->getArgument(0), false,
646 rangeOperands, succesors);
647 cgm.blockAddressToLabel.clear();
648}
649
651 // Resolve block address-to-label mappings, then emit the indirect branch
652 // with the corresponding targets.
655
656 // If a label address was taken but no indirect goto was used, we can't remove
657 // the block argument here. Instead, we mark the 'indirectbr' op
658 // as poison so that the cleanup can be deferred to lowering, since the
659 // verifier doesn't allow the 'indirectbr' target address to be null.
660 if (indirectGotoBlock && indirectGotoBlock->hasNoPredecessors()) {
661 auto indrBr = cast<cir::IndirectBrOp>(indirectGotoBlock->front());
662 indrBr.setPoison(true);
663 }
664
665 // Pop any cleanups that might have been associated with the
666 // parameters. Do this in whatever block we're currently in; it's
667 // important to do this before we enter the return block or return
668 // edges will be *really* confused.
669 // TODO(cir): Use prologueCleanupDepth here.
670 bool hasCleanups = ehStack.stable_begin() != prologueCleanupDepth;
671 if (hasCleanups) {
673 // FIXME(cir): should we clearInsertionPoint? breaks many testcases
675 }
676
677 assert(deferredConditionalCleanupStack.empty() &&
678 "deferred conditional cleanups were not consumed by a "
679 "FullExprCleanupScope");
680}
681
682mlir::LogicalResult CIRGenFunction::emitFunctionBody(const clang::Stmt *body) {
683 // We start with function level scope for variables.
685
686 if (const CompoundStmt *block = dyn_cast<CompoundStmt>(body))
687 return emitCompoundStmtWithoutScope(*block);
688
689 return emitStmt(body, /*useCurrentScope=*/true);
690}
691
692static void eraseEmptyAndUnusedBlocks(cir::FuncOp func) {
693 // Remove any leftover blocks that are unreachable and empty, since they do
694 // not represent unreachable code useful for warnings nor anything deemed
695 // useful in general.
696 SmallVector<mlir::Block *> blocksToDelete;
697 for (mlir::Block &block : func.getBlocks()) {
698 if (block.empty() && block.getUses().empty())
699 blocksToDelete.push_back(&block);
700 }
701 for (mlir::Block *block : blocksToDelete)
702 block->erase();
703}
704
705cir::FuncOp CIRGenFunction::generateCode(clang::GlobalDecl gd, cir::FuncOp fn,
706 cir::FuncType funcType) {
707 const auto *funcDecl = cast<FunctionDecl>(gd.getDecl());
708 curGD = gd;
709
710 if (funcDecl->isInlineBuiltinDeclaration()) {
711 // When generating code for a builtin with an inline declaration, use a
712 // mangled name to hold the actual body, while keeping an external
713 // declaration in case the function pointer is referenced somewhere.
714 std::string fdInlineName = (cgm.getMangledName(funcDecl) + ".inline").str();
715 cir::FuncOp clone =
716 mlir::cast_or_null<cir::FuncOp>(cgm.getGlobalValue(fdInlineName));
717 if (!clone) {
718 mlir::OpBuilder::InsertionGuard guard(builder);
719 builder.setInsertionPoint(fn);
720 clone = cir::FuncOp::create(builder, fn.getLoc(), fdInlineName,
721 fn.getFunctionType());
722 cgm.insertGlobalSymbol(clone);
723 clone.setLinkage(cir::GlobalLinkageKind::InternalLinkage);
724 clone.setSymVisibility("private");
725 clone.setInlineKind(cir::InlineKind::AlwaysInline);
726 }
727 fn.setLinkage(cir::GlobalLinkageKind::ExternalLinkage);
728 fn.setSymVisibility("private");
729 fn = clone;
730 } else {
731 // Detect the unusual situation where an inline version is shadowed by a
732 // non-inline version. In that case we should pick the external one
733 // everywhere. That's GCC behavior too.
734 for (const FunctionDecl *pd = funcDecl->getPreviousDecl(); pd;
735 pd = pd->getPreviousDecl()) {
736 if (LLVM_UNLIKELY(pd->isInlineBuiltinDeclaration())) {
737 std::string inlineName = funcDecl->getName().str() + ".inline";
738 if (auto inlineFn = mlir::cast_or_null<cir::FuncOp>(
739 cgm.getGlobalValue(inlineName))) {
740 // Replace all uses of the .inline function with the regular function
741 // FIXME: This performs a linear walk over the module. Introduce some
742 // caching here.
743 if (inlineFn
744 .replaceAllSymbolUses(fn.getSymNameAttr(), cgm.getModule())
745 .failed())
746 llvm_unreachable("Failed to replace inline builtin symbol uses");
747 cgm.eraseGlobalSymbol(inlineFn);
748 inlineFn.erase();
749 }
750 break;
751 }
752 }
753 }
754
755 SourceLocation loc = funcDecl->getLocation();
756 Stmt *body = funcDecl->getBody();
757 SourceRange bodyRange =
758 body ? body->getSourceRange() : funcDecl->getLocation();
759
760 SourceLocRAIIObject fnLoc{*this, loc.isValid() ? getLoc(loc)
761 : builder.getUnknownLoc()};
762
763 auto validMLIRLoc = [&](clang::SourceLocation clangLoc) {
764 return clangLoc.isValid() ? getLoc(clangLoc) : builder.getUnknownLoc();
765 };
766 const mlir::Location fusedLoc = mlir::FusedLoc::get(
768 {validMLIRLoc(bodyRange.getBegin()), validMLIRLoc(bodyRange.getEnd())});
769 mlir::Block *entryBB = fn.addEntryBlock();
770
771 FunctionArgList args;
772 QualType retTy = buildFunctionArgList(gd, args);
773
774 // Create a scope in the symbol table to hold variable declarations.
776 {
777 LexicalScope lexScope(*this, fusedLoc, entryBB);
778
779 // Emit the standard function prologue.
780 startFunction(gd, retTy, fn, funcType, args, loc, bodyRange.getBegin());
781
782 // Save parameters for coroutine function.
783 if (body && isa_and_nonnull<CoroutineBodyStmt>(body))
784 llvm::append_range(fnArgs, funcDecl->parameters());
785
786 if (isa<CXXDestructorDecl>(funcDecl)) {
787 emitDestructorBody(args);
788 } else if (isa<CXXConstructorDecl>(funcDecl)) {
790 } else if (getLangOpts().CUDA && !getLangOpts().CUDAIsDevice &&
791 funcDecl->hasAttr<CUDAGlobalAttr>()) {
792 cgm.getCUDARuntime().emitDeviceStub(*this, fn, args);
793 } else if (isa<CXXMethodDecl>(funcDecl) &&
794 cast<CXXMethodDecl>(funcDecl)->isLambdaStaticInvoker()) {
795 // The lambda static invoker function is special, because it forwards or
796 // clones the body of the function call operator (but is actually
797 // static).
799 } else if (funcDecl->isDefaulted() && isa<CXXMethodDecl>(funcDecl) &&
800 (cast<CXXMethodDecl>(funcDecl)->isCopyAssignmentOperator() ||
801 cast<CXXMethodDecl>(funcDecl)->isMoveAssignmentOperator())) {
802 // Implicit copy-assignment gets the same special treatment as implicit
803 // copy-constructors.
805 } else if (body) {
806 // Emit standard function body.
807 if (mlir::failed(emitFunctionBody(body))) {
808 return nullptr;
809 }
810 } else {
811 // Anything without a body should have been handled above.
812 llvm_unreachable("no definition for normal function");
813 }
814
815 if (mlir::failed(fn.verifyBody()))
816 return nullptr;
817
818 finishFunction(bodyRange.getEnd());
819 }
820
822 return fn;
823}
824
827 const auto *ctor = cast<CXXConstructorDecl>(curGD.getDecl());
828 CXXCtorType ctorType = curGD.getCtorType();
829
830 assert((cgm.getTarget().getCXXABI().hasConstructorVariants() ||
831 ctorType == Ctor_Complete) &&
832 "can only generate complete ctor for this ABI");
833
834 cgm.setCXXSpecialMemberAttr(cast<cir::FuncOp>(curFn), ctor);
835
836 if (ctorType == Ctor_Complete && isConstructorDelegationValid(ctor) &&
837 cgm.getTarget().getCXXABI().hasConstructorVariants()) {
838 emitDelegateCXXConstructorCall(ctor, Ctor_Base, args, ctor->getEndLoc());
839 return;
840 }
841
842 const FunctionDecl *definition = nullptr;
843 Stmt *body = ctor->getBody(definition);
844 assert(definition == ctor && "emitting wrong constructor body");
845
846 bool isTryBody = isa_and_nonnull<CXXTryStmt>(body);
847
848 // A type that handles the emission of the constructor body, that can be
849 // called directly for cases where we don't have a try-body, or passed to
850 // emitCXXTryStmt.
851 struct ctorTryBodyEmitter final : cxxTryBodyEmitter {
852 const CXXConstructorDecl *ctor = nullptr;
853 CXXCtorType ctorType;
854 FunctionArgList &args;
855 Stmt *emitterBody = nullptr;
856 ctorTryBodyEmitter(const CXXConstructorDecl *ctor, CXXCtorType ctorType,
857 FunctionArgList &args, bool isTryBody, Stmt *b)
858 : ctor(ctor), ctorType(ctorType), args(args),
859 emitterBody(isTryBody ? cast<CXXTryStmt>(b)->getTryBlock() : b) {}
860 ~ctorTryBodyEmitter() override = default;
861
862 mlir::LogicalResult operator()(CIRGenFunction &cgf) override {
865
866 //// TODO: in restricted cases, we can emit the vbase initializers of a
867 //// complete ctor and then delegate to the base ctor.
868
869 cgf.emitCtorPrologue(ctor, ctorType, args);
870 return cgf.emitStmt(emitterBody, /*useCurrentScope=*/true);
871 }
872 };
873
874 ctorTryBodyEmitter emitter{ctor, ctorType, args, isTryBody, body};
875 mlir::LogicalResult bodyRes =
876 isTryBody ? emitCXXTryStmt(*cast<CXXTryStmt>(body), emitter)
877 : emitter(*this);
878
879 // TODO(cir): propagate this result via mlir::logical result. Just
880 // unreachable now just to have it handled.
881 if (bodyRes.failed())
882 cgm.errorNYI(ctor->getSourceRange(),
883 "emitConstructorBody: emit body statement failed.");
884}
885
886/// Emits the body of the current destructor.
888 const CXXDestructorDecl *dtor = cast<CXXDestructorDecl>(curGD.getDecl());
889 CXXDtorType dtorType = curGD.getDtorType();
890
891 cgm.setCXXSpecialMemberAttr(cast<cir::FuncOp>(curFn), dtor);
892
893 // For an abstract class, non-base destructors are never used (and can't
894 // be emitted in general, because vbase dtors may not have been validated
895 // by Sema), but the Itanium ABI doesn't make them optional and Clang may
896 // in fact emit references to them from other compilations, so emit them
897 // as functions containing a trap instruction.
898 if (dtorType != Dtor_Base && dtor->getParent()->isAbstract()) {
899 SourceLocation loc =
900 dtor->hasBody() ? dtor->getBody()->getBeginLoc() : dtor->getLocation();
901 emitTrap(getLoc(loc), true);
902 return;
903 }
904
905 Stmt *body = dtor->getBody();
907
908 // The call to operator delete in a deleting destructor happens
909 // outside of the function-try-block, which means it's always
910 // possible to delegate the destructor body to the complete
911 // destructor. Do so.
912 if (dtorType == Dtor_Deleting || dtorType == Dtor_VectorDeleting) {
914 cgm.errorNYI(dtor->getSourceRange(), "emitConditionalArrayDtorCall");
915 RunCleanupsScope dtorEpilogue(*this);
917 if (haveInsertPoint()) {
919 emitCXXDestructorCall(dtor, Dtor_Complete, /*forVirtualBase=*/false,
920 /*delegating=*/false, loadCXXThisAddress(), thisTy);
921 }
922 return;
923 }
924
925 // If the body is a function-try-block, enter the try before
926 // anything else.
927 const bool isTryBody = isa_and_nonnull<CXXTryStmt>(body);
928 if (isTryBody)
929 cgm.errorNYI(dtor->getSourceRange(), "function-try-block destructor");
930
932
933 // Enter the epilogue cleanups.
934 RunCleanupsScope dtorEpilogue(*this);
935
936 // If this is the complete variant, just invoke the base variant;
937 // the epilogue will destruct the virtual bases. But we can't do
938 // this optimization if the body is a function-try-block, because
939 // we'd introduce *two* handler blocks. In the Microsoft ABI, we
940 // always delegate because we might not have a definition in this TU.
941 switch (dtorType) {
942 case Dtor_Unified:
943 llvm_unreachable("not expecting a unified dtor");
944 case Dtor_Comdat:
945 llvm_unreachable("not expecting a COMDAT");
946 case Dtor_Deleting:
948 llvm_unreachable("already handled deleting case");
949
950 case Dtor_Complete:
951 assert((body || getTarget().getCXXABI().isMicrosoft()) &&
952 "can't emit a dtor without a body for non-Microsoft ABIs");
953
954 // Enter the cleanup scopes for virtual bases.
956
957 if (!isTryBody) {
959 emitCXXDestructorCall(dtor, Dtor_Base, /*forVirtualBase=*/false,
960 /*delegating=*/false, loadCXXThisAddress(), thisTy);
961 break;
962 }
963
964 // Fallthrough: act like we're in the base variant.
965 [[fallthrough]];
966
967 case Dtor_Base:
968 assert(body);
969
970 // Enter the cleanup scopes for fields and non-virtual bases.
972
974
975 if (isTryBody) {
976 cgm.errorNYI(dtor->getSourceRange(), "function-try-block destructor");
977 } else if (body) {
978 (void)emitStmt(body, /*useCurrentScope=*/true);
979 } else {
980 assert(dtor->isImplicit() && "bodyless dtor not implicit");
981 // nothing to do besides what's in the epilogue
982 }
983 // -fapple-kext must inline any call to this dtor into
984 // the caller's body.
986
987 break;
988 }
989
990 // Jump out through the epilogue cleanups.
991 dtorEpilogue.forceCleanup();
992
993 // Exit the try if applicable.
994 if (isTryBody)
995 cgm.errorNYI(dtor->getSourceRange(), "function-try-block destructor");
996}
997
998/// Given a value of type T* that may not be to a complete object, construct
999/// an l-vlaue withi the natural pointee alignment of T.
1001 QualType ty) {
1002 // FIXME(cir): is it safe to assume Op->getResult(0) is valid? Perhaps
1003 // assert on the result type first.
1004 LValueBaseInfo baseInfo;
1006 CharUnits align = cgm.getNaturalTypeAlignment(ty, &baseInfo);
1007 return makeAddrLValue(Address(val, align), ty, baseInfo);
1008}
1009
1011 QualType ty) {
1012 LValueBaseInfo baseInfo;
1013 CharUnits alignment = cgm.getNaturalTypeAlignment(ty, &baseInfo);
1014 Address addr(val, convertTypeForMem(ty), alignment);
1016 return makeAddrLValue(addr, ty, baseInfo);
1017}
1018
1019// Map the LangOption for exception behavior into the corresponding enum in
1020// the IR.
1021static llvm::fp::ExceptionBehavior
1023 switch (kind) {
1025 return llvm::fp::ebIgnore;
1027 return llvm::fp::ebMayTrap;
1029 return llvm::fp::ebStrict;
1031 llvm_unreachable("expected explicitly initialized exception behavior");
1032 }
1033 llvm_unreachable("unsupported FP exception behavior");
1034}
1035
1037 FunctionArgList &args) {
1038 const auto *fd = cast<FunctionDecl>(gd.getDecl());
1039 QualType retTy = fd->getReturnType();
1040
1041 // Only implicit-object member functions need the CXXABI-supplied `this`
1042 // parameter prepended to the arg list. Explicit-object members carry the
1043 // object as a regular parameter that fd->parameters() already enumerates.
1044 const auto *md = dyn_cast<CXXMethodDecl>(fd);
1045 if (md && md->isImplicitObjectMemberFunction()) {
1046 if (cgm.getCXXABI().hasThisReturn(gd))
1047 cgm.errorNYI(fd->getSourceRange(), "this return");
1048 else if (cgm.getCXXABI().hasMostDerivedReturn(gd))
1049 cgm.errorNYI(fd->getSourceRange(), "most derived return");
1050 cgm.getCXXABI().buildThisParam(*this, args);
1051 }
1052
1053 bool passedParams = true;
1054 if (const auto *cd = dyn_cast<CXXConstructorDecl>(fd))
1055 if (auto inherited = cd->getInheritedConstructor())
1056 passedParams =
1057 getTypes().inheritingCtorHasParams(inherited, gd.getCtorType());
1058
1059 if (passedParams) {
1060 for (auto *param : fd->parameters()) {
1061 args.push_back(param);
1062 if (!param->hasAttr<PassObjectSizeAttr>())
1063 continue;
1064
1065 auto *implicit = ImplicitParamDecl::Create(
1066 getContext(), param->getDeclContext(), param->getLocation(),
1067 /*Id=*/nullptr, getContext().getSizeType(), ImplicitParamKind::Other);
1068 sizeArguments[param] = implicit;
1069 args.push_back(implicit);
1070 }
1071 }
1072
1073 if (md && (isa<CXXConstructorDecl>(md) || isa<CXXDestructorDecl>(md)))
1074 cgm.getCXXABI().addImplicitStructorParams(*this, retTy, args);
1075
1076 return retTy;
1077}
1078
1080 // Initializing an aggregate temporary in C++11: T{...}.
1081 if (!e->isGLValue())
1082 return emitAggExprToLValue(e);
1083
1084 // An lvalue initializer list must be initializing a reference.
1085 assert(e->isTransparent() && "non-transparent glvalue init list");
1086 return emitLValue(e->getInit(0));
1087}
1088
1089static std::variant<LValue, RValue>
1091 bool forLValue, AggValueSlot slot) {
1093 SmallVector<OVMD> opaques;
1094 llvm::scope_exit opaque_cleanup{
1095 [&]() { llvm::for_each(opaques, [&](OVMD &o) { o.unbind(cgf); }); }};
1096
1097 // Find the result expression, if any.
1098 const Expr *resultExpr = e->getResultExpr();
1099 std::variant<LValue, RValue> result;
1100
1101 for (const Expr *semantic : e->semantics()) {
1102 // If this semantic expression is an opaque value, bind it
1103 // to the result of its source expression.
1104 if (const auto *ov = dyn_cast<OpaqueValueExpr>(semantic)) {
1105
1106 // Skip unique OVEs.
1107 if (ov->isUnique()) {
1108 assert(ov != resultExpr &&
1109 "A unique OVE cannot be used as the result expression");
1110 continue;
1111 }
1112
1113 // If this is the result expression, we may need to evaluate
1114 // directly into the slot.
1115 OVMD opaqueData;
1116 if (ov == resultExpr && ov->isPRValue() && !forLValue &&
1118 cgf.cgm.errorNYI(e->getSourceRange(),
1119 "emitPseudoObjectExpr for RValue & aggregate kind");
1120 } else {
1121 opaqueData = OVMD::bind(cgf, ov, ov->getSourceExpr());
1122
1123 // If this is the result, also evaluate the result now.
1124 if (ov == resultExpr) {
1125 // FIXME: This doesn't really affect anything, but I cannot find a
1126 // test for this, so leave an ErrorNYI here until we can find one.
1127 cgf.cgm.errorNYI(e->getSourceRange(),
1128 "emitPseudoObjectExpr as result");
1129 if (forLValue)
1130 result = cgf.emitLValue(ov);
1131 else
1132 cgf.cgm.errorNYI(e->getSourceRange(),
1133 "emitPseudoObjectExpr as an RValue");
1134 }
1135 }
1136 opaques.push_back(opaqueData);
1137 } else if (semantic == resultExpr) {
1138 // Otherwise, if the expression is the result, evaluate it
1139 // and remember the result.
1140 if (forLValue)
1141 result = cgf.emitLValue(semantic);
1142 else
1143 result = cgf.emitAnyExpr(semantic, slot);
1144 } else {
1145 // FIXME: best I can tell, this is only reachable as an r-value, so this
1146 // isn't properly tested.
1147 cgf.cgm.errorNYI(e->getSourceRange(),
1148 "emitPseudoObjectExpr as an ignored value");
1149 // Otherwise, evaluate the expression in an ignored context.
1150 cgf.emitIgnoredExpr(semantic);
1151 }
1152 }
1153
1154 return result;
1155}
1156
1158 AggValueSlot slot) {
1159 return std::get<RValue>(
1160 emitPseudoObjectExpr(*this, e, /*forLValue=*/false, slot));
1161}
1162
1164 return std::get<LValue>(emitPseudoObjectExpr(*this, e, /*forLValue=*/true,
1166}
1167
1168/// Emit code to compute a designator that specifies the location
1169/// of the expression.
1170/// FIXME: document this function better.
1172 // FIXME: ApplyDebugLocation DL(*this, e);
1173 switch (e->getStmtClass()) {
1174 default:
1176 std::string("l-value not implemented for '") +
1177 e->getStmtClassName() + "'");
1178 return LValue();
1179 case Expr::ConditionalOperatorClass:
1181 case Expr::BinaryConditionalOperatorClass:
1183 case Expr::ArraySubscriptExprClass:
1185 case Expr::ExtVectorElementExprClass:
1187 case Expr::UnaryOperatorClass:
1189 case Expr::StringLiteralClass:
1191 case Expr::MemberExprClass:
1193 case Expr::CompoundLiteralExprClass:
1195 case Expr::PredefinedExprClass:
1197 case Expr::BinaryOperatorClass:
1199 case Expr::CompoundAssignOperatorClass: {
1200 QualType ty = e->getType();
1201 if (ty->getAs<AtomicType>()) {
1202 cgm.errorNYI(e->getSourceRange(),
1203 "CompoundAssignOperator with AtomicType");
1204 return LValue();
1205 }
1206 if (!ty->isAnyComplexType())
1208
1210 }
1211 case Expr::CallExprClass:
1212 case Expr::CXXMemberCallExprClass:
1213 case Expr::CXXOperatorCallExprClass:
1214 case Expr::UserDefinedLiteralClass:
1216 case Expr::ExprWithCleanupsClass: {
1217 const auto *cleanups = cast<ExprWithCleanups>(e);
1218 FullExprCleanupScope scope(*this, cleanups->getSubExpr());
1219 LValue lv = emitLValue(cleanups->getSubExpr());
1220 if (lv.isSimple()) {
1221 // Defend against branches out of gnu statement expressions surrounded by
1222 // cleanups.
1223 Address addr = lv.getAddress();
1224 mlir::Value v = addr.getPointer();
1225 scope.exit({&v});
1226 return LValue::makeAddr(addr.withPointer(v), lv.getType(),
1227 lv.getBaseInfo());
1228 }
1229 // FIXME: Is it possible to create an ExprWithCleanups that produces a
1230 // bitfield lvalue or some other non-simple lvalue?
1231 return lv;
1232 }
1233 case Expr::CXXDefaultArgExprClass: {
1234 auto *dae = cast<CXXDefaultArgExpr>(e);
1235 CXXDefaultArgExprScope scope(*this, dae);
1236 return emitLValue(dae->getExpr());
1237 }
1238 case Expr::CXXTypeidExprClass:
1240 case Expr::ParenExprClass:
1241 return emitLValue(cast<ParenExpr>(e)->getSubExpr());
1242 case Expr::GenericSelectionExprClass:
1243 return emitLValue(cast<GenericSelectionExpr>(e)->getResultExpr());
1244 case Expr::DeclRefExprClass:
1246 case Expr::ImplicitCastExprClass:
1247 case Expr::CStyleCastExprClass:
1248 case Expr::CXXStaticCastExprClass:
1249 case Expr::CXXDynamicCastExprClass:
1250 case Expr::CXXReinterpretCastExprClass:
1251 case Expr::CXXConstCastExprClass:
1252 case Expr::CXXFunctionalCastExprClass:
1253 // TODO(cir): The above list is missing
1254 // CXXAddrSpaceCastExprClass, and ObjCBridgedCastExprClass.
1255 return emitCastLValue(cast<CastExpr>(e));
1256 case Expr::MaterializeTemporaryExprClass:
1258 case Expr::OpaqueValueExprClass:
1260 case Expr::ChooseExprClass:
1261 return emitLValue(cast<ChooseExpr>(e)->getChosenSubExpr());
1262 case Expr::SubstNonTypeTemplateParmExprClass:
1263 return emitLValue(cast<SubstNonTypeTemplateParmExpr>(e)->getReplacement());
1264 case Expr::InitListExprClass:
1266 case Expr::PseudoObjectExprClass:
1268 case Expr::CXXDefaultInitExprClass: {
1269 auto *die = cast<CXXDefaultInitExpr>(e);
1270 CXXDefaultInitExprScope scope(*this, die);
1271 return emitLValue(die->getExpr());
1272 }
1273 }
1274}
1275
1276static std::string getVersionedTmpName(llvm::StringRef name, unsigned cnt) {
1277 SmallString<256> buffer;
1278 llvm::raw_svector_ostream out(buffer);
1279 out << name << cnt;
1280 return std::string(out.str());
1281}
1282
1284 return getVersionedTmpName("ref.tmp", counterRefTmp++);
1285}
1286
1288 return getVersionedTmpName("agg.tmp", counterAggTmp++);
1289}
1290
1291void CIRGenFunction::emitNullInitialization(mlir::Location loc, Address destPtr,
1292 QualType ty) {
1293 // Ignore empty classes in C++.
1294 if (getLangOpts().CPlusPlus)
1295 if (const auto *rd = ty->getAsCXXRecordDecl(); rd && rd->isEmpty())
1296 return;
1297
1298 // Cast the dest ptr to the appropriate i8 pointer type.
1299 if (builder.isInt8Ty(destPtr.getElementType())) {
1300 cgm.errorNYI(loc, "Cast the dest ptr to the appropriate i8 pointer type");
1301 }
1302
1303 // Get size and alignment info for this aggregate.
1304 const CharUnits size = getContext().getTypeSizeInChars(ty);
1305 if (size.isZero()) {
1306 // But note that getTypeInfo returns 0 for a VLA.
1307 if (isa<VariableArrayType>(getContext().getAsArrayType(ty))) {
1308 cgm.errorNYI(loc,
1309 "emitNullInitialization for zero size VariableArrayType");
1310 } else {
1311 return;
1312 }
1313 }
1314
1315 // If the type contains a pointer to data member we can't memset it to zero.
1316 // Instead, create a null constant and copy it to the destination.
1317 // TODO: there are other patterns besides zero that we can usefully memset,
1318 // like -1, which happens to be the pattern used by member-pointers.
1319 if (!cgm.getTypes().isZeroInitializable(ty)) {
1320 cgm.errorNYI(loc, "type is not zero initializable");
1321 }
1322
1323 // In LLVM Codegen: otherwise, just memset the whole thing to zero using
1324 // Builder.CreateMemSet. In CIR just emit a store of #cir.zero to the
1325 // respective address.
1326 // Builder.CreateMemSet(DestPtr, Builder.getInt8(0), SizeVal, false);
1327 const mlir::Value zeroValue = builder.getNullValue(convertType(ty), loc);
1328 builder.createStore(loc, zeroValue, destPtr);
1329}
1330
1332 const clang::Expr *e)
1333 : cgf(cgf) {
1334 ConstructorHelper(e->getFPFeaturesInEffect(cgf.getLangOpts()));
1335}
1336
1338 FPOptions fpFeatures)
1339 : cgf(cgf) {
1340 ConstructorHelper(fpFeatures);
1341}
1342
1343void CIRGenFunction::CIRGenFPOptionsRAII::ConstructorHelper(
1344 FPOptions fpFeatures) {
1345 oldFPFeatures = cgf.curFPFeatures;
1346 cgf.curFPFeatures = fpFeatures;
1347
1348 oldExcept = cgf.builder.getDefaultConstrainedExcept();
1349 oldRounding = cgf.builder.getDefaultConstrainedRounding();
1350
1351 if (oldFPFeatures == fpFeatures)
1352 return;
1353
1354 // TODO(cir): create guard to restore fast math configurations.
1356
1357 [[maybe_unused]] llvm::RoundingMode newRoundingBehavior =
1358 fpFeatures.getRoundingMode();
1359 // TODO(cir): override rounding behaviour once FM configs are guarded.
1360 [[maybe_unused]] llvm::fp::ExceptionBehavior newExceptionBehavior =
1362 fpFeatures.getExceptionMode()));
1363 // TODO(cir): override exception behaviour once FM configs are guarded.
1364
1365 // TODO(cir): override FP flags once FM configs are guarded.
1367
1368 assert((cgf.curFuncDecl == nullptr || cgf.builder.getIsFPConstrained() ||
1369 isa<CXXConstructorDecl>(cgf.curFuncDecl) ||
1370 isa<CXXDestructorDecl>(cgf.curFuncDecl) ||
1371 (newExceptionBehavior == llvm::fp::ebIgnore &&
1372 newRoundingBehavior == llvm::RoundingMode::NearestTiesToEven)) &&
1373 "FPConstrained should be enabled on entire function");
1374
1375 // TODO(cir): mark CIR function with fast math attributes.
1377}
1378
1380 cgf.curFPFeatures = oldFPFeatures;
1381 cgf.builder.setDefaultConstrainedExcept(oldExcept);
1382 cgf.builder.setDefaultConstrainedRounding(oldRounding);
1383}
1384
1385// TODO(cir): should be shared with LLVM codegen.
1387 const Expr *e = ce->getSubExpr();
1388
1389 if (ce->getCastKind() == CK_UncheckedDerivedToBase)
1390 return false;
1391
1392 if (isa<CXXThisExpr>(e->IgnoreParens())) {
1393 // We always assume that 'this' is never null.
1394 return false;
1395 }
1396
1397 if (const ImplicitCastExpr *ice = dyn_cast<ImplicitCastExpr>(ce)) {
1398 // And that glvalue casts are never null.
1399 if (ice->isGLValue())
1400 return false;
1401 }
1402
1403 return true;
1404}
1405
1406/// Computes the length of an array in elements, as well as the base
1407/// element type and a properly-typed first element pointer.
1408mlir::Value
1410 QualType &baseType, Address &addr) {
1411 const clang::ArrayType *arrayType = origArrayType;
1412
1413 // If it's a VLA, we have to load the stored size. Note that
1414 // this is the size of the VLA in bytes, not its size in elements.
1415 mlir::Value numVLAElements = nullptr;
1418
1419 // Walk into all VLAs. This doesn't require changes to addr,
1420 // which has type T* where T is the first non-VLA element type.
1421 do {
1422 QualType elementType = arrayType->getElementType();
1423 arrayType = getContext().getAsArrayType(elementType);
1424
1425 // If we only have VLA components, 'addr' requires no adjustment.
1426 if (!arrayType) {
1427 baseType = elementType;
1428 return numVLAElements;
1429 }
1431
1432 // We get out here only if we find a constant array type
1433 // inside the VLA.
1434 }
1435
1436 // Classic codegen emits an all-zero inbounds GEP to convert addr from
1437 // [M x [N x T]]* to T*. CIR doesn't need this because callers handle
1438 // the array-to-element pointer conversion themselves (via array_to_ptrdecay
1439 // casts, ptr_bitcast, or manual array type peeling).
1440
1441 uint64_t countFromCLAs = 1;
1442 QualType eltType;
1443
1444 auto cirArrayType = mlir::dyn_cast<cir::ArrayType>(addr.getElementType());
1445
1446 while (cirArrayType) {
1448 countFromCLAs *= cirArrayType.getSize();
1449 eltType = arrayType->getElementType();
1450
1451 cirArrayType =
1452 mlir::dyn_cast<cir::ArrayType>(cirArrayType.getElementType());
1453
1454 arrayType = getContext().getAsArrayType(arrayType->getElementType());
1455 assert((!cirArrayType || arrayType) &&
1456 "CIR and Clang types are out-of-sync");
1457 }
1458
1459 if (arrayType) {
1460 // From this point onwards, the Clang array type has been emitted
1461 // as some other type (probably a packed struct). Compute the array
1462 // size, and just emit the 'begin' expression as a bitcast.
1463 cgm.errorNYI(*currSrcLoc, "length for non-array underlying types");
1464 }
1465
1466 baseType = eltType;
1467
1468 mlir::Value numElements =
1469 builder.getConstInt(*currSrcLoc, sizeTy, countFromCLAs);
1470
1471 // If we had any VLA dimensions, factor them in.
1472 if (numVLAElements)
1473 numElements =
1474 builder.createMul(numVLAElements.getLoc(), numVLAElements, numElements,
1476
1477 return numElements;
1478}
1479
1481 // If we already made the indirect branch for indirect goto, return its block.
1483 return;
1484
1485 mlir::OpBuilder::InsertionGuard guard(builder);
1487 builder.createBlock(builder.getBlock()->getParent(), {}, {voidPtrTy},
1488 {builder.getUnknownLoc()});
1489}
1490
1492 mlir::Value ptrValue, QualType ty, SourceLocation loc,
1493 SourceLocation assumptionLoc, int64_t alignment, mlir::Value offsetValue) {
1495 mlir::Location assumeLoc = getLoc(assumptionLoc);
1496 mlir::Value alignValue = builder.getUInt64(alignment, assumeLoc);
1497 mlir::Value cond = builder.getBool(true, assumeLoc);
1498 llvm::SmallVector<mlir::Value> bundleArgs{ptrValue, alignValue};
1499 if (offsetValue)
1500 bundleArgs.push_back(offsetValue);
1501 cir::AssumeOp::create(builder, assumeLoc, cond, cir::AssumeBundleKind::Align,
1502 bundleArgs);
1503 return ptrValue;
1504}
1505
1507 mlir::Value ptrValue, const Expr *expr, SourceLocation assumptionLoc,
1508 int64_t alignment, mlir::Value offsetValue) {
1509 QualType ty = expr->getType();
1510 SourceLocation loc = expr->getExprLoc();
1511 return emitAlignmentAssumption(ptrValue, ty, loc, assumptionLoc, alignment,
1512 offsetValue);
1513}
1514
1516 const VariableArrayType *vla =
1517 cgm.getASTContext().getAsVariableArrayType(type);
1518 assert(vla && "type was not a variable array type!");
1519 return getVLASize(vla);
1520}
1521
1524 // The number of elements so far; always size_t.
1525 mlir::Value numElements;
1526
1527 QualType elementType;
1528 do {
1529 elementType = type->getElementType();
1530 mlir::Value vlaSize = vlaSizeMap[type->getSizeExpr()];
1531 assert(vlaSize && "no size for VLA!");
1532 assert(vlaSize.getType() == sizeTy);
1533
1534 if (!numElements) {
1535 numElements = vlaSize;
1536 } else {
1537 // It's undefined behavior if this wraps around, so mark it that way.
1538 // FIXME: Teach -fsanitize=undefined to trap this.
1539
1540 numElements =
1541 builder.createMul(numElements.getLoc(), numElements, vlaSize,
1543 }
1544 } while ((type = getContext().getAsVariableArrayType(elementType)));
1545
1546 assert(numElements && "Undefined elements number");
1547 return {numElements, elementType};
1548}
1549
1552 mlir::Value vlaSize = vlaSizeMap[vla->getSizeExpr()];
1553 assert(vlaSize && "no size for VLA!");
1554 assert(vlaSize.getType() == sizeTy);
1555 return {vlaSize, vla->getElementType()};
1556}
1557
1558// TODO(cir): Most of this function can be shared between CIRGen
1559// and traditional LLVM codegen
1561 assert(type->isVariablyModifiedType() &&
1562 "Must pass variably modified type to EmitVLASizes!");
1563
1564 // We're going to walk down into the type and look for VLA
1565 // expressions.
1566 do {
1567 assert(type->isVariablyModifiedType());
1568
1569 const Type *ty = type.getTypePtr();
1570 switch (ty->getTypeClass()) {
1571 case Type::CountAttributed:
1572 case Type::PackIndexing:
1573 case Type::ArrayParameter:
1574 case Type::HLSLAttributedResource:
1575 case Type::HLSLInlineSpirv:
1576 case Type::PredefinedSugar:
1577 cgm.errorNYI("CIRGenFunction::emitVariablyModifiedType");
1578 break;
1579
1580#define TYPE(Class, Base)
1581#define ABSTRACT_TYPE(Class, Base)
1582#define NON_CANONICAL_TYPE(Class, Base)
1583#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1584#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(Class, Base)
1585#include "clang/AST/TypeNodes.inc"
1586 llvm_unreachable(
1587 "dependent type must be resolved before the CIR codegen");
1588
1589 // These types are never variably-modified.
1590 case Type::Builtin:
1591 case Type::Complex:
1592 case Type::Vector:
1593 case Type::ExtVector:
1594 case Type::ConstantMatrix:
1595 case Type::Record:
1596 case Type::Enum:
1597 case Type::Using:
1598 case Type::TemplateSpecialization:
1599 case Type::ObjCTypeParam:
1600 case Type::ObjCObject:
1601 case Type::ObjCInterface:
1602 case Type::ObjCObjectPointer:
1603 case Type::BitInt:
1604 case Type::OverflowBehavior:
1605 llvm_unreachable("type class is never variably-modified!");
1606
1607 case Type::Adjusted:
1608 type = cast<clang::AdjustedType>(ty)->getAdjustedType();
1609 break;
1610
1611 case Type::Decayed:
1612 type = cast<clang::DecayedType>(ty)->getPointeeType();
1613 break;
1614
1615 case Type::Pointer:
1616 type = cast<clang::PointerType>(ty)->getPointeeType();
1617 break;
1618
1619 case Type::BlockPointer:
1620 type = cast<clang::BlockPointerType>(ty)->getPointeeType();
1621 break;
1622
1623 case Type::LValueReference:
1624 case Type::RValueReference:
1625 type = cast<clang::ReferenceType>(ty)->getPointeeType();
1626 break;
1627
1628 case Type::MemberPointer:
1629 type = cast<clang::MemberPointerType>(ty)->getPointeeType();
1630 break;
1631
1632 case Type::ConstantArray:
1633 case Type::IncompleteArray:
1634 // Losing element qualification here is fine.
1635 type = cast<clang::ArrayType>(ty)->getElementType();
1636 break;
1637
1638 case Type::VariableArray: {
1639 // Losing element qualification here is fine.
1641
1642 // Unknown size indication requires no size computation.
1643 // Otherwise, evaluate and record it.
1644 if (const Expr *sizeExpr = vat->getSizeExpr()) {
1645 // It's possible that we might have emitted this already,
1646 // e.g. with a typedef and a pointer to it.
1647 mlir::Value &entry = vlaSizeMap[sizeExpr];
1648 if (!entry) {
1649 mlir::Value size = emitScalarExpr(sizeExpr);
1651
1652 // Always zexting here would be wrong if it weren't
1653 // undefined behavior to have a negative bound.
1654 // FIXME: What about when size's type is larger than size_t?
1655 entry = builder.createIntCast(size, sizeTy);
1656 }
1657 }
1658 type = vat->getElementType();
1659 break;
1660 }
1661
1662 case Type::FunctionProto:
1663 case Type::FunctionNoProto:
1664 type = cast<clang::FunctionType>(ty)->getReturnType();
1665 break;
1666
1667 case Type::Paren:
1668 case Type::TypeOf:
1669 case Type::UnaryTransform:
1670 case Type::Attributed:
1671 case Type::BTFTagAttributed:
1672 case Type::SubstTemplateTypeParm:
1673 case Type::MacroQualified:
1674 // Keep walking after single level desugaring.
1675 type = type.getSingleStepDesugaredType(getContext());
1676 break;
1677
1678 case Type::Typedef:
1679 case Type::Decltype:
1680 case Type::Auto:
1681 case Type::DeducedTemplateSpecialization:
1682 // Stop walking: nothing to do.
1683 return;
1684
1685 case Type::TypeOfExpr:
1686 // Stop walking: emit typeof expression.
1687 emitIgnoredExpr(cast<clang::TypeOfExprType>(ty)->getUnderlyingExpr());
1688 return;
1689
1690 case Type::Atomic:
1691 type = cast<clang::AtomicType>(ty)->getValueType();
1692 break;
1693
1694 case Type::Pipe:
1695 type = cast<clang::PipeType>(ty)->getElementType();
1696 break;
1697 }
1698 } while (type->isVariablyModifiedType());
1699}
1700
1702 if (getContext().getBuiltinVaListType()->isArrayType())
1703 return emitPointerWithAlignment(e);
1704 return emitLValue(e).getAddress();
1705}
1706
1707} // 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
__device__ __2f16 b
__device__ __2f16 float __ockl_bool s
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:228
SourceManager & getSourceManager()
Definition ASTContext.h:867
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:3784
QualType getElementType() const
Definition TypeBase.h:3796
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 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 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:2620
Represents a C++ destructor within a class.
Definition DeclCXX.h:2882
const CXXRecordDecl * getParent() const
Return the parent of this method declaration, which is the class in which this method is defined.
Definition DeclCXX.h:2271
QualType getFunctionObjectParameterType() const
Definition DeclCXX.h:2295
bool isAbstract() const
Determine whether this class has a pure virtual function.
Definition DeclCXX.h:1221
bool isEmpty() const
Determine whether this is an empty class in the sense of (C++11 [meta.unary.prop]).
Definition DeclCXX.h:1186
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:3996
Expr * IgnoreParens() LLVM_READONLY
Skip past any parentheses which might surround this expression until reaching a fixed point.
Definition Expr.cpp:3093
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:4523
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:5576
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:2469
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:2962
QualType getUnqualifiedType() const
Retrieve the unqualified variant of the given type, removing as little sugar as possible.
Definition TypeBase.h:8539
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
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
const char * getStmtClassName() const
Definition Stmt.cpp:86
SourceLocation getBeginLoc() const LLVM_READONLY
Definition Stmt.cpp:355
bool isVoidType() const
Definition TypeBase.h:9048
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:8817
bool isVariablyModifiedType() const
Whether this type is a variably-modified type (C99 6.7.5).
Definition TypeBase.h:2862
TypeClass getTypeClass() const
Definition TypeBase.h:2445
const T * getAs() const
Member-template getAs<specific type>'.
Definition TypeBase.h:9275
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:4028
Expr * getSizeExpr() const
Definition TypeBase.h:4042
@ 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