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 mlir::Value dynAllocSize) {
228 return cir::AllocaOp::create(*
this, loc, addrType, type, name, alignment,
232 mlir::Value
createAlloca(mlir::Location loc, cir::PointerType addrType,
233 mlir::Type type, llvm::StringRef name,
235 mlir::Value dynAllocSize) {
237 return createAlloca(loc, addrType, type, name, alignmentAttr, dynAllocSize);
240 mlir::Value
createAlloca(mlir::Location loc, cir::PointerType addrType,
241 mlir::Type type, llvm::StringRef name,
242 mlir::IntegerAttr alignment) {
243 return cir::AllocaOp::create(*
this, loc, addrType, type, name, alignment);
249 mlir::ArrayAttr indices = {}) {
250 cir::PointerType type =
getPointerTo(globalOp.getSymType());
256 cir::GlobalOp globalOp,
257 mlir::ArrayAttr indices = {}) {
258 auto symbol = mlir::FlatSymbolRefAttr::get(globalOp.getSymNameAttr());
259 return cir::GlobalViewAttr::get(type, symbol, indices);
264 return cir::GetGlobalOp::create(
265 *
this, loc,
getPointerTo(global.getSymType()), global.getSymName());
274 return cir::CopyOp::create(*
this, dst.getLoc(), dst, src);
277 cir::StoreOp
createStore(mlir::Location loc, mlir::Value val, mlir::Value dst,
278 bool isVolatile =
false,
279 mlir::IntegerAttr align = {},
280 cir::MemOrderAttr order = {}) {
281 return cir::StoreOp::create(*
this, loc, val, dst, isVolatile, align, order);
286 mlir::StringRef name,
287 mlir::Type type,
bool isConstant,
288 cir::GlobalLinkageKind linkage) {
289 mlir::OpBuilder::InsertionGuard guard(*
this);
290 setInsertionPointToStart(mlirModule.getBody());
291 return cir::GlobalOp::create(*
this, loc, name, type, isConstant, linkage);
295 mlir::Value base, llvm::StringRef name,
297 return cir::GetMemberOp::create(*
this, loc, resultTy, base, name, index);
304 return cir::LoadOp::create(*
this, loc, addr,
false,
305 false, alignmentAttr,
310 mlir::Value stride) {
311 return cir::PtrStrideOp::create(*
this, loc, base.getType(), base, stride);
318 cir::CallOp
createCallOp(mlir::Location loc, mlir::SymbolRefAttr callee,
319 mlir::Type returnType, mlir::ValueRange operands,
321 auto op = cir::CallOp::create(*
this, loc, callee, returnType, operands);
327 mlir::ValueRange operands,
329 return createCallOp(loc, mlir::SymbolRefAttr::get(callee),
330 callee.getFunctionType().getReturnType(), operands,
336 cir::FuncType funcType, mlir::ValueRange operands,
339 resOperands.append(operands.begin(), operands.end());
340 return createCallOp(loc, mlir::SymbolRefAttr(), funcType.getReturnType(),
345 mlir::Location loc, mlir::SymbolRefAttr callee = mlir::SymbolRefAttr(),
346 mlir::Type returnType = cir::VoidType(),
347 mlir::ValueRange operands = mlir::ValueRange(),
348 [[maybe_unused]] cir::SideEffect sideEffect = cir::SideEffect::All) {
355 mlir::Location loc, cir::FuncOp callee, mlir::ValueRange operands,
356 [[maybe_unused]] cir::SideEffect sideEffect = cir::SideEffect::All) {
360 callee.getFunctionType().getReturnType(), operands);
367 mlir::Value
createCast(mlir::Location loc, cir::CastKind kind,
368 mlir::Value src, mlir::Type newTy) {
369 if (newTy == src.getType())
371 return cir::CastOp::create(*
this, loc, newTy, kind, src);
376 if (newTy == src.getType())
378 return createCast(src.getLoc(), kind, src, newTy);
382 return createCast(cir::CastKind::integral, src, newTy);
386 return createCast(cir::CastKind::int_to_ptr, src, newTy);
390 return createCast(cir::CastKind::ptr_to_int, src, newTy);
398 return createCast(cir::CastKind::bool_to_int, src, newTy);
402 return createCast(cir::CastKind::bitcast, src, newTy);
407 return createCast(loc, cir::CastKind::bitcast, src, newTy);
411 assert(mlir::isa<cir::PointerType>(src.getType()) &&
"expected ptr src");
420 cir::BinOpKind kind, mlir::Value rhs) {
421 return cir::BinOp::create(*
this, loc, lhs.getType(), kind, lhs, rhs);
426 llvm::APInt val = llvm::APInt::getLowBitsSet(size, bits);
427 auto type = cir::IntType::get(getContext(), size,
false);
431 mlir::Value
createAnd(mlir::Location loc, mlir::Value lhs, mlir::Value rhs) {
432 return createBinop(loc, lhs, cir::BinOpKind::And, rhs);
435 mlir::Value
createOr(mlir::Location loc, mlir::Value lhs, mlir::Value rhs) {
436 return createBinop(loc, lhs, cir::BinOpKind::Or, rhs);
440 mlir::Value trueValue, mlir::Value falseValue) {
441 assert(trueValue.getType() == falseValue.getType() &&
442 "trueValue and falseValue should have the same type");
443 return cir::SelectOp::create(*
this, loc, trueValue.getType(), condition,
444 trueValue, falseValue);
457 mlir::Value
createMul(mlir::Location loc, mlir::Value lhs, mlir::Value rhs,
459 auto op = cir::BinOp::create(*
this, loc, lhs.getType(), cir::BinOpKind::Mul,
461 op.setNoUnsignedWrap(
476 mlir::Value
createSub(mlir::Location loc, mlir::Value lhs, mlir::Value rhs,
478 auto op = cir::BinOp::create(*
this, loc, lhs.getType(), cir::BinOpKind::Sub,
480 op.setNoUnsignedWrap(
498 mlir::Value
createAdd(mlir::Location loc, mlir::Value lhs, mlir::Value rhs,
500 auto op = cir::BinOp::create(*
this, loc, lhs.getType(), cir::BinOpKind::Add,
502 op.setNoUnsignedWrap(
521 mlir::Value lhs, mlir::Value rhs) {
522 return cir::CmpOp::create(*
this, loc,
getBoolTy(), kind, lhs, rhs);
525 mlir::Value
createIsNaN(mlir::Location loc, mlir::Value operand) {
526 return createCompare(loc, cir::CmpOpKind::ne, operand, operand);
529 mlir::Value
createShift(mlir::Location loc, mlir::Value lhs, mlir::Value rhs,
531 return cir::ShiftOp::create(*
this, loc, lhs.getType(), lhs, rhs,
536 const llvm::APInt &rhs,
bool isShiftLeft) {
541 mlir::Value
createShift(mlir::Location loc, mlir::Value lhs,
unsigned bits,
543 auto width = mlir::dyn_cast<cir::IntType>(lhs.getType()).getWidth();
544 auto shift = llvm::APInt(width, bits);
574 std::find_if(block->rbegin(), block->rend(), [](mlir::Operation &op) {
575 return mlir::isa<cir::AllocaOp, cir::LabelOp>(&op);
578 if (last != block->rend())
579 return OpBuilder::InsertPoint(block, ++mlir::Block::iterator(&*last));
580 return OpBuilder::InsertPoint(block, block->begin());
601 return alignment ? getI64IntegerAttr(alignment) : mlir::IntegerAttr();
610 return cir::ConditionOp::create(*
this, condition.getLoc(), condition);
614 cir::YieldOp
createYield(mlir::Location loc, mlir::ValueRange value = {}) {
615 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::GlobalViewAttr getGlobalViewAttr(cir::GlobalOp globalOp, mlir::ArrayAttr indices={})
Get constant address of a global variable as an MLIR attribute.
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 createAlloca(mlir::Location loc, cir::PointerType addrType, mlir::Type type, llvm::StringRef name, clang::CharUnits alignment, mlir::Value dynAllocSize)
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)
cir::CopyOp createCopy(mlir::Value dst, mlir::Value src)
Create a copy with inferred length.
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)
mlir::Value createAlloca(mlir::Location loc, cir::PointerType addrType, mlir::Type type, llvm::StringRef name, mlir::IntegerAttr alignment, mlir::Value dynAllocSize)
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()