9#ifndef LLVM_CLANG_CIR_DIALECT_BUILDER_CIRBASEBUILDER_H
10#define LLVM_CLANG_CIR_DIALECT_BUILDER_CIRBASEBUILDER_H
17#include "llvm/ADT/STLForwardCompat.h"
18#include "llvm/Support/ErrorHandling.h"
20#include "mlir/IR/Builders.h"
21#include "mlir/IR/BuiltinAttributes.h"
22#include "mlir/IR/Location.h"
23#include "mlir/IR/Types.h"
36 llvm::to_underlying(
b));
41 llvm::to_underlying(
b));
60 :
mlir::OpBuilder(&mlirContext) {}
64 const llvm::APInt &val) {
65 return cir::ConstantOp::create(*
this, loc, cir::IntAttr::get(typ, val));
68 cir::ConstantOp
getConstant(mlir::Location loc, mlir::TypedAttr attr) {
69 return cir::ConstantOp::create(*
this, loc, attr);
74 return getConstant(loc, cir::IntAttr::get(ty, value));
77 mlir::Value
getSignedInt(mlir::Location loc, int64_t val,
unsigned numBits) {
78 auto type = cir::IntType::get(getContext(), numBits,
true);
80 llvm::APInt(numBits, val,
true));
85 auto type = cir::IntType::get(getContext(), numBits,
false);
95 assert(mlir::isa<cir::PointerType>(t) &&
"expected cir.ptr");
100 if (mlir::isa<cir::IntType>(ty))
101 return cir::IntAttr::get(ty, 0);
102 if (cir::isAnyFloatingPointType(ty))
103 return cir::FPAttr::getZero(ty);
104 if (
auto complexType = mlir::dyn_cast<cir::ComplexType>(ty))
105 return cir::ZeroAttr::get(complexType);
106 if (
auto arrTy = mlir::dyn_cast<cir::ArrayType>(ty))
107 return cir::ZeroAttr::get(arrTy);
108 if (
auto vecTy = mlir::dyn_cast<cir::VectorType>(ty))
109 return cir::ZeroAttr::get(vecTy);
110 if (
auto ptrTy = mlir::dyn_cast<cir::PointerType>(ty))
112 if (
auto recordTy = mlir::dyn_cast<cir::RecordType>(ty))
113 return cir::ZeroAttr::get(recordTy);
114 if (mlir::isa<cir::BoolType>(ty)) {
117 llvm_unreachable(
"Zero initializer for given type is NYI");
120 cir::ConstantOp
getBool(
bool state, mlir::Location loc) {
121 return cir::ConstantOp::create(*
this, loc,
getCIRBoolAttr(state));
126 cir::BoolType
getBoolTy() {
return cir::BoolType::get(getContext()); }
129 return cir::PointerType::get(ty);
137 return cir::BoolAttr::get(getContext(), state);
145 auto resultComplexTy = cir::ComplexType::get(real.getType());
146 return cir::ComplexCreateOp::create(*
this, loc, resultComplexTy, real,
151 auto operandTy = mlir::cast<cir::ComplexType>(operand.getType());
152 return cir::ComplexRealOp::create(*
this, loc, operandTy.getElementType(),
157 auto operandTy = mlir::cast<cir::ComplexType>(operand.getType());
158 return cir::ComplexImagOp::create(*
this, loc, operandTy.getElementType(),
163 bool isVolatile =
false, uint64_t alignment = 0) {
165 return cir::LoadOp::create(*
this, loc, ptr,
false, isVolatile,
166 alignmentAttr, cir::MemOrderAttr{});
170 uint64_t alignment) {
171 return createLoad(loc, ptr,
false, alignment);
175 return cir::UnaryOp::create(*
this, value.getLoc(), value.getType(),
176 cir::UnaryOpKind::Not, value);
182 llvm::function_ref<
void(mlir::OpBuilder &, mlir::Location)> condBuilder,
183 llvm::function_ref<
void(mlir::OpBuilder &, mlir::Location)> bodyBuilder) {
184 return cir::DoWhileOp::create(*
this, loc, condBuilder, bodyBuilder);
190 llvm::function_ref<
void(mlir::OpBuilder &, mlir::Location)> condBuilder,
191 llvm::function_ref<
void(mlir::OpBuilder &, mlir::Location)> bodyBuilder) {
192 return cir::WhileOp::create(*
this, loc, condBuilder, bodyBuilder);
198 llvm::function_ref<
void(mlir::OpBuilder &, mlir::Location)> condBuilder,
199 llvm::function_ref<
void(mlir::OpBuilder &, mlir::Location)> bodyBuilder,
200 llvm::function_ref<
void(mlir::OpBuilder &, mlir::Location)> stepBuilder) {
201 return cir::ForOp::create(*
this, loc, condBuilder, bodyBuilder,
207 return cir::BreakOp::create(*
this, loc);
212 return cir::ContinueOp::create(*
this, loc);
216 mlir::Value operand) {
217 return cir::UnaryOp::create(*
this, loc, kind, operand);
221 return cir::ConstPtrAttr::get(type, getI64IntegerAttr(value));
224 mlir::Value
createAlloca(mlir::Location loc, cir::PointerType addrType,
225 mlir::Type type, llvm::StringRef name,
226 mlir::IntegerAttr alignment) {
227 return cir::AllocaOp::create(*
this, loc, addrType, type, name, alignment);
232 cir::GlobalOp globalOp,
233 mlir::ArrayAttr indices = {}) {
234 auto symbol = mlir::FlatSymbolRefAttr::get(globalOp.getSymNameAttr());
235 return cir::GlobalViewAttr::get(type, symbol, indices);
240 return cir::GetGlobalOp::create(
241 *
this, loc,
getPointerTo(global.getSymType()), global.getSymName());
248 cir::StoreOp
createStore(mlir::Location loc, mlir::Value val, mlir::Value dst,
249 bool isVolatile =
false,
250 mlir::IntegerAttr align = {},
251 cir::MemOrderAttr order = {}) {
252 return cir::StoreOp::create(*
this, loc, val, dst, isVolatile, align, order);
257 mlir::StringRef name,
258 mlir::Type type,
bool isConstant,
259 cir::GlobalLinkageKind linkage) {
260 mlir::OpBuilder::InsertionGuard guard(*
this);
261 setInsertionPointToStart(mlirModule.getBody());
262 return cir::GlobalOp::create(*
this, loc, name, type, isConstant, linkage);
266 mlir::Value base, llvm::StringRef name,
268 return cir::GetMemberOp::create(*
this, loc, resultTy, base, name, index);
275 return cir::LoadOp::create(*
this, loc, addr,
false,
276 false, alignmentAttr,
281 mlir::Value stride) {
282 return cir::PtrStrideOp::create(*
this, loc, base.getType(), base, stride);
289 cir::CallOp
createCallOp(mlir::Location loc, mlir::SymbolRefAttr callee,
290 mlir::Type returnType, mlir::ValueRange operands,
292 auto op = cir::CallOp::create(*
this, loc, callee, returnType, operands);
298 mlir::ValueRange operands,
300 return createCallOp(loc, mlir::SymbolRefAttr::get(callee),
301 callee.getFunctionType().getReturnType(), operands,
307 cir::FuncType funcType, mlir::ValueRange operands,
310 resOperands.append(operands.begin(), operands.end());
311 return createCallOp(loc, mlir::SymbolRefAttr(), funcType.getReturnType(),
316 mlir::Location loc, mlir::SymbolRefAttr callee = mlir::SymbolRefAttr(),
317 mlir::Type returnType = cir::VoidType(),
318 mlir::ValueRange operands = mlir::ValueRange(),
319 [[maybe_unused]] cir::SideEffect sideEffect = cir::SideEffect::All) {
326 mlir::Location loc, cir::FuncOp callee, mlir::ValueRange operands,
327 [[maybe_unused]] cir::SideEffect sideEffect = cir::SideEffect::All) {
331 callee.getFunctionType().getReturnType(), operands);
338 mlir::Value
createCast(mlir::Location loc, cir::CastKind kind,
339 mlir::Value src, mlir::Type newTy) {
340 if (newTy == src.getType())
342 return cir::CastOp::create(*
this, loc, newTy, kind, src);
347 if (newTy == src.getType())
349 return createCast(src.getLoc(), kind, src, newTy);
353 return createCast(cir::CastKind::integral, src, newTy);
357 return createCast(cir::CastKind::int_to_ptr, src, newTy);
361 return createCast(cir::CastKind::ptr_to_int, src, newTy);
369 return createCast(cir::CastKind::bool_to_int, src, newTy);
373 return createCast(cir::CastKind::bitcast, src, newTy);
378 return createCast(loc, cir::CastKind::bitcast, src, newTy);
382 assert(mlir::isa<cir::PointerType>(src.getType()) &&
"expected ptr src");
391 cir::BinOpKind kind, mlir::Value rhs) {
392 return cir::BinOp::create(*
this, loc, lhs.getType(), kind, lhs, rhs);
397 llvm::APInt val = llvm::APInt::getLowBitsSet(size, bits);
398 auto type = cir::IntType::get(getContext(), size,
false);
402 mlir::Value
createAnd(mlir::Location loc, mlir::Value lhs, mlir::Value rhs) {
403 return createBinop(loc, lhs, cir::BinOpKind::And, rhs);
406 mlir::Value
createOr(mlir::Location loc, mlir::Value lhs, mlir::Value rhs) {
407 return createBinop(loc, lhs, cir::BinOpKind::Or, rhs);
411 mlir::Value trueValue, mlir::Value falseValue) {
412 assert(trueValue.getType() == falseValue.getType() &&
413 "trueValue and falseValue should have the same type");
414 return cir::SelectOp::create(*
this, loc, trueValue.getType(), condition,
415 trueValue, falseValue);
428 mlir::Value
createMul(mlir::Location loc, mlir::Value lhs, mlir::Value rhs,
430 auto op = cir::BinOp::create(*
this, loc, lhs.getType(), cir::BinOpKind::Mul,
432 op.setNoUnsignedWrap(
447 mlir::Value
createSub(mlir::Location loc, mlir::Value lhs, mlir::Value rhs,
449 auto op = cir::BinOp::create(*
this, loc, lhs.getType(), cir::BinOpKind::Sub,
451 op.setNoUnsignedWrap(
469 mlir::Value
createAdd(mlir::Location loc, mlir::Value lhs, mlir::Value rhs,
471 auto op = cir::BinOp::create(*
this, loc, lhs.getType(), cir::BinOpKind::Add,
473 op.setNoUnsignedWrap(
492 mlir::Value lhs, mlir::Value rhs) {
493 return cir::CmpOp::create(*
this, loc,
getBoolTy(), kind, lhs, rhs);
496 mlir::Value
createIsNaN(mlir::Location loc, mlir::Value operand) {
497 return createCompare(loc, cir::CmpOpKind::ne, operand, operand);
500 mlir::Value
createShift(mlir::Location loc, mlir::Value lhs, mlir::Value rhs,
502 return cir::ShiftOp::create(*
this, loc, lhs.getType(), lhs, rhs,
507 const llvm::APInt &rhs,
bool isShiftLeft) {
512 mlir::Value
createShift(mlir::Location loc, mlir::Value lhs,
unsigned bits,
514 auto width = mlir::dyn_cast<cir::IntType>(lhs.getType()).getWidth();
515 auto shift = llvm::APInt(width, bits);
545 std::find_if(block->rbegin(), block->rend(), [](mlir::Operation &op) {
546 return mlir::isa<cir::AllocaOp, cir::LabelOp>(&op);
549 if (last != block->rend())
550 return OpBuilder::InsertPoint(block, ++mlir::Block::iterator(&*last));
551 return OpBuilder::InsertPoint(block, block->begin());
572 return alignment ? getI64IntegerAttr(alignment) : mlir::IntegerAttr();
581 return cir::ConditionOp::create(*
this, condition.getLoc(), condition);
585 cir::YieldOp
createYield(mlir::Location loc, mlir::ValueRange value = {}) {
586 return cir::YieldOp::create(*
this, loc, value);
mlir::Value createNSWSub(mlir::Location loc, mlir::Value lhs, mlir::Value rhs)
cir::ConstantOp getBool(bool state, mlir::Location loc)
mlir::Value createShift(mlir::Location loc, mlir::Value lhs, unsigned bits, bool isShiftLeft)
cir::WhileOp createWhile(mlir::Location loc, llvm::function_ref< void(mlir::OpBuilder &, mlir::Location)> condBuilder, llvm::function_ref< void(mlir::OpBuilder &, mlir::Location)> bodyBuilder)
Create a while operation.
cir::BreakOp createBreak(mlir::Location loc)
Create a break operation.
mlir::TypedAttr getConstNullPtrAttr(mlir::Type t)
mlir::Value createGetGlobal(mlir::Location loc, cir::GlobalOp global)
mlir::IntegerAttr getAlignmentAttr(int64_t alignment)
mlir::Value createShift(mlir::Location loc, mlir::Value lhs, const llvm::APInt &rhs, bool isShiftLeft)
mlir::Value getConstAPInt(mlir::Location loc, mlir::Type typ, const llvm::APInt &val)
cir::GlobalViewAttr getGlobalViewAttr(cir::PointerType type, cir::GlobalOp globalOp, mlir::ArrayAttr indices={})
Get constant address of a global variable as an MLIR attribute.
mlir::Value createCast(cir::CastKind kind, mlir::Value src, mlir::Type newTy)
mlir::Value createLogicalOr(mlir::Location loc, mlir::Value lhs, mlir::Value rhs)
mlir::Value createShift(mlir::Location loc, mlir::Value lhs, mlir::Value rhs, bool isShiftLeft)
cir::ConditionOp createCondition(mlir::Value condition)
Create a loop condition.
mlir::Value createLowBitsSet(mlir::Location loc, unsigned size, unsigned bits)
mlir::Value createNSWAdd(mlir::Location loc, mlir::Value lhs, mlir::Value rhs)
cir::ConstantOp getNullValue(mlir::Type ty, mlir::Location loc)
cir::BoolAttr getCIRBoolAttr(bool state)
mlir::Value createBoolToInt(mlir::Value src, mlir::Type newTy)
cir::ConstantOp getConstant(mlir::Location loc, mlir::TypedAttr attr)
mlir::Value createNUWAdd(mlir::Location loc, mlir::Value lhs, mlir::Value rhs)
mlir::Value createOr(mlir::Location loc, mlir::Value lhs, mlir::Value rhs)
mlir::Value createShiftLeft(mlir::Location loc, mlir::Value lhs, mlir::Value rhs)
mlir::Value createCast(mlir::Location loc, cir::CastKind kind, mlir::Value src, mlir::Type newTy)
mlir::IntegerAttr getSizeFromCharUnits(clang::CharUnits size)
cir::PtrStrideOp createPtrStride(mlir::Location loc, mlir::Value base, mlir::Value stride)
mlir::Value createIntToPtr(mlir::Value src, mlir::Type newTy)
cir::CallOp createCallOp(mlir::Location loc, cir::FuncOp callee, mlir::ValueRange operands, llvm::ArrayRef< mlir::NamedAttribute > attrs={})
cir::ForOp createFor(mlir::Location loc, llvm::function_ref< void(mlir::OpBuilder &, mlir::Location)> condBuilder, llvm::function_ref< void(mlir::OpBuilder &, mlir::Location)> bodyBuilder, llvm::function_ref< void(mlir::OpBuilder &, mlir::Location)> stepBuilder)
Create a for operation.
static OpBuilder::InsertPoint getBestAllocaInsertPoint(mlir::Block *block)
mlir::Value createPtrToInt(mlir::Value src, mlir::Type newTy)
mlir::Value createNUWSub(mlir::Location loc, mlir::Value lhs, mlir::Value rhs)
cir::ConstantOp getFalse(mlir::Location loc)
mlir::Value createAdd(mlir::Location loc, mlir::Value lhs, mlir::Value rhs, OverflowBehavior ob=OverflowBehavior::None)
cir::GetMemberOp createGetMember(mlir::Location loc, mlir::Type resultTy, mlir::Value base, llvm::StringRef name, unsigned index)
cir::PointerType getPointerTo(mlir::Type ty)
mlir::Value createNot(mlir::Value value)
mlir::Value createComplexImag(mlir::Location loc, mlir::Value operand)
cir::ConstantOp getTrue(mlir::Location loc)
mlir::Value createNSWMul(mlir::Location loc, mlir::Value lhs, mlir::Value rhs)
mlir::Value createGetGlobal(cir::GlobalOp global)
cir::DoWhileOp createDoWhile(mlir::Location loc, llvm::function_ref< void(mlir::OpBuilder &, mlir::Location)> condBuilder, llvm::function_ref< void(mlir::OpBuilder &, mlir::Location)> bodyBuilder)
Create a do-while operation.
mlir::Value createNUWAMul(mlir::Location loc, mlir::Value lhs, mlir::Value rhs)
cir::CallOp createCallOp(mlir::Location loc, mlir::SymbolRefAttr callee, mlir::Type returnType, mlir::ValueRange operands, llvm::ArrayRef< mlir::NamedAttribute > attrs={})
mlir::Value createPtrBitcast(mlir::Value src, mlir::Type newPointeeTy)
cir::CallOp createTryCallOp(mlir::Location loc, mlir::SymbolRefAttr callee=mlir::SymbolRefAttr(), mlir::Type returnType=cir::VoidType(), mlir::ValueRange operands=mlir::ValueRange(), cir::SideEffect sideEffect=cir::SideEffect::All)
mlir::Value createShiftLeft(mlir::Location loc, mlir::Value lhs, unsigned bits)
mlir::Value createSub(mlir::Location loc, mlir::Value lhs, mlir::Value rhs, OverflowBehavior ob=OverflowBehavior::Saturated)
mlir::Value getSignedInt(mlir::Location loc, int64_t val, unsigned numBits)
mlir::Value createAnd(mlir::Location loc, mlir::Value lhs, mlir::Value rhs)
mlir::Value createIntCast(mlir::Value src, mlir::Type newTy)
cir::CallOp createTryCallOp(mlir::Location loc, cir::FuncOp callee, mlir::ValueRange operands, cir::SideEffect sideEffect=cir::SideEffect::All)
mlir::Value createBitcast(mlir::Value src, mlir::Type newTy)
CIRBaseBuilderTy(mlir::MLIRContext &mlirContext)
mlir::Value createBitcast(mlir::Location loc, mlir::Value src, mlir::Type newTy)
cir::GlobalOp createGlobal(mlir::ModuleOp mlirModule, mlir::Location loc, mlir::StringRef name, mlir::Type type, bool isConstant, cir::GlobalLinkageKind linkage)
cir::StoreOp createStore(mlir::Location loc, mlir::Value val, mlir::Value dst, bool isVolatile=false, mlir::IntegerAttr align={}, cir::MemOrderAttr order={})
cir::CmpOp createCompare(mlir::Location loc, cir::CmpOpKind kind, mlir::Value lhs, mlir::Value rhs)
mlir::IntegerAttr getAlignmentAttr(clang::CharUnits alignment)
mlir::Value createBinop(mlir::Location loc, mlir::Value lhs, cir::BinOpKind kind, mlir::Value rhs)
mlir::Value createAlloca(mlir::Location loc, cir::PointerType addrType, mlir::Type type, llvm::StringRef name, mlir::IntegerAttr alignment)
mlir::Value createSelect(mlir::Location loc, mlir::Value condition, mlir::Value trueValue, mlir::Value falseValue)
cir::ContinueOp createContinue(mlir::Location loc)
Create a continue operation.
mlir::Value createMul(mlir::Location loc, mlir::Value lhs, mlir::Value rhs, OverflowBehavior ob=OverflowBehavior::None)
mlir::TypedAttr getZeroInitAttr(mlir::Type ty)
cir::LoadOp createLoad(mlir::Location loc, mlir::Value ptr, bool isVolatile=false, uint64_t alignment=0)
cir::CallOp createIndirectCallOp(mlir::Location loc, mlir::Value indirectTarget, cir::FuncType funcType, mlir::ValueRange operands, llvm::ArrayRef< mlir::NamedAttribute > attrs={})
CIRBaseBuilderTy(mlir::OpBuilder &builder)
cir::ConstantOp getConstantInt(mlir::Location loc, mlir::Type ty, int64_t value)
mlir::Value createComplexCreate(mlir::Location loc, mlir::Value real, mlir::Value imag)
mlir::Value createPtrToBoolCast(mlir::Value v)
cir::BoolAttr getTrueAttr()
mlir::Value createShiftRight(mlir::Location loc, mlir::Value lhs, unsigned bits)
mlir::Value createIsNaN(mlir::Location loc, mlir::Value operand)
mlir::Value createAlignedLoad(mlir::Location loc, mlir::Value ptr, uint64_t alignment)
mlir::TypedAttr getConstPtrAttr(mlir::Type type, int64_t value)
mlir::Value createDummyValue(mlir::Location loc, mlir::Type type, clang::CharUnits alignment)
cir::BoolAttr getFalseAttr()
cir::PointerType getVoidPtrTy()
mlir::Value createShiftRight(mlir::Location loc, mlir::Value lhs, mlir::Value rhs)
cir::YieldOp createYield(mlir::Location loc, mlir::ValueRange value={})
Create a yield operation.
mlir::Value createLogicalAnd(mlir::Location loc, mlir::Value lhs, mlir::Value rhs)
mlir::Value createUnaryOp(mlir::Location loc, cir::UnaryOpKind kind, mlir::Value operand)
mlir::IntegerAttr getAlignmentAttr(llvm::Align alignment)
cir::BoolType getBoolTy()
mlir::Value getUnsignedInt(mlir::Location loc, uint64_t val, unsigned numBits)
mlir::Value createComplexReal(mlir::Location loc, mlir::Value operand)
CharUnits - This is an opaque type for sizes expressed in character units.
QuantityType getQuantity() const
getQuantity - Get the raw integer representation of this quantity.
constexpr OverflowBehavior operator|(OverflowBehavior a, OverflowBehavior b)
constexpr OverflowBehavior operator&(OverflowBehavior a, OverflowBehavior b)
constexpr OverflowBehavior & operator|=(OverflowBehavior &a, OverflowBehavior b)
constexpr OverflowBehavior & operator&=(OverflowBehavior &a, OverflowBehavior b)
static bool addressSpace()
static bool opCallSideEffect()
static bool opCallCallConv()