| 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 | // This contains code to emit Builtin calls as CIR or a function call to be |
| 10 | // later resolved. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "CIRGenCall.h" |
| 15 | #include "CIRGenFunction.h" |
| 16 | #include "CIRGenModule.h" |
| 17 | #include "CIRGenValue.h" |
| 18 | #include "mlir/IR/BuiltinAttributes.h" |
| 19 | #include "mlir/IR/Value.h" |
| 20 | #include "mlir/Support/LLVM.h" |
| 21 | #include "clang/AST/Expr.h" |
| 22 | #include "clang/AST/GlobalDecl.h" |
| 23 | #include "llvm/Support/ErrorHandling.h" |
| 24 | |
| 25 | using namespace clang; |
| 26 | using namespace clang::CIRGen; |
| 27 | |
| 28 | RValue CIRGenFunction::emitBuiltinExpr(const GlobalDecl &gd, unsigned builtinID, |
| 29 | const CallExpr *e, |
| 30 | ReturnValueSlot returnValue) { |
| 31 | // See if we can constant fold this builtin. If so, don't emit it at all. |
| 32 | // TODO: Extend this handling to all builtin calls that we can constant-fold. |
| 33 | Expr::EvalResult result; |
| 34 | if (e->isPRValue() && e->EvaluateAsRValue(result, cgm.getASTContext()) && |
| 35 | !result.hasSideEffects()) { |
| 36 | if (result.Val.isInt()) { |
| 37 | return RValue::get(builder.getConstInt(getLoc(e->getSourceRange()), |
| 38 | result.Val.getInt())); |
| 39 | } |
| 40 | if (result.Val.isFloat()) { |
| 41 | // Note: we are using result type of CallExpr to determine the type of |
| 42 | // the constant. Classic codegen uses the result value to determine the |
| 43 | // type. We feel it should be Ok to use expression type because it is |
| 44 | // hard to imagine a builtin function evaluates to a value that |
| 45 | // over/underflows its own defined type. |
| 46 | mlir::Type type = convertType(e->getType()); |
| 47 | return RValue::get(builder.getConstFP(getLoc(e->getExprLoc()), type, |
| 48 | result.Val.getFloat())); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | mlir::Location loc = getLoc(e->getExprLoc()); |
| 53 | cgm.errorNYI(loc, "non constant foldable builtin calls" ); |
| 54 | return getUndefRValue(ty: e->getType()); |
| 55 | } |
| 56 | |