| 1 | //===- LLVMArithOps.cpp - LLVM dialect arithmetic ops ---------------------===// |
| 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 file defines the ops of op partition `Arith` of the LLVM dialect. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #include "mlir/Dialect/LLVMIR/LLVMArithOps.h" |
| 14 | |
| 15 | #include "IR/LLVMOps.h" |
| 16 | #include "mlir/Dialect/LLVMIR/LLVMConstantOps.h" |
| 17 | #include "mlir/Dialect/LLVMIR/LLVMMemoryOps.h" |
| 18 | #include "mlir/IR/Builders.h" |
| 19 | #include "mlir/IR/DialectImplementation.h" |
| 20 | #include "mlir/IR/Matchers.h" |
| 21 | #include "llvm/ADT/STLExtras.h" |
| 22 | |
| 23 | #include <optional> |
| 24 | |
| 25 | using namespace mlir; |
| 26 | using namespace mlir::LLVM; |
| 27 | |
| 28 | /// Returns a boolean type that has the same shape as `type`. It supports both |
| 29 | /// fixed size vectors as well as scalable vectors. |
| 30 | Type mlir::LLVM::getI1SameShape(Type type) { |
| 31 | Type i1Type = IntegerType::get(context: type.getContext(), width: 1); |
| 32 | if (LLVM::isCompatibleVectorType(type)) |
| 33 | return LLVM::getVectorType(elementType: i1Type, numElements: LLVM::getVectorNumElements(type)); |
| 34 | return i1Type; |
| 35 | } |
| 36 | |
| 37 | //===----------------------------------------------------------------------===// |
| 38 | // Printing, parsing, folding and builder for LLVM::CmpOp. |
| 39 | //===----------------------------------------------------------------------===// |
| 40 | |
| 41 | template <typename PredicateAttr, typename Predicate> |
| 42 | static ParseResult parseCmpPredicateImpl( |
| 43 | OpAsmParser &parser, PredicateAttr &predicate, |
| 44 | function_ref<std::optional<Predicate>(StringRef)> symbolize) { |
| 45 | std::string spelling; |
| 46 | SMLoc loc = parser.getCurrentLocation(); |
| 47 | if (parser.parseString(string: &spelling)) |
| 48 | return failure(); |
| 49 | std::optional<Predicate> value = symbolize(spelling); |
| 50 | if (!value) |
| 51 | return parser.emitError(loc) |
| 52 | << "'" << spelling |
| 53 | << "' is an incorrect value of the 'predicate' attribute" ; |
| 54 | predicate = PredicateAttr::get(parser.getContext(), *value); |
| 55 | return success(); |
| 56 | } |
| 57 | |
| 58 | ParseResult mlir::LLVM::parseCmpPredicate(OpAsmParser &parser, |
| 59 | ICmpPredicateAttr &predicate) { |
| 60 | return parseCmpPredicateImpl<ICmpPredicateAttr, ICmpPredicate>( |
| 61 | parser, predicate, |
| 62 | symbolize: [](StringRef spelling) { return symbolizeICmpPredicate(spelling); }); |
| 63 | } |
| 64 | |
| 65 | ParseResult mlir::LLVM::parseCmpPredicate(OpAsmParser &parser, |
| 66 | FCmpPredicateAttr &predicate) { |
| 67 | return parseCmpPredicateImpl<FCmpPredicateAttr, FCmpPredicate>( |
| 68 | parser, predicate, |
| 69 | symbolize: [](StringRef spelling) { return symbolizeFCmpPredicate(spelling); }); |
| 70 | } |
| 71 | |
| 72 | void mlir::LLVM::printCmpPredicate(OpAsmPrinter &printer, Operation *, |
| 73 | ICmpPredicateAttr predicate) { |
| 74 | printer << '"' << stringifyICmpPredicate(predicate.getValue()) << '"'; |
| 75 | } |
| 76 | |
| 77 | void mlir::LLVM::printCmpPredicate(OpAsmPrinter &printer, Operation *, |
| 78 | FCmpPredicateAttr predicate) { |
| 79 | printer << '"' << stringifyFCmpPredicate(predicate.getValue()) << '"'; |
| 80 | } |
| 81 | |
| 82 | /// Returns a scalar or vector boolean attribute of the given type. |
| 83 | static Attribute getBoolAttribute(Type type, MLIRContext *ctx, bool value) { |
| 84 | auto boolAttr = BoolAttr::get(context: ctx, value); |
| 85 | ShapedType shapedType = dyn_cast<ShapedType>(Val&: type); |
| 86 | if (!shapedType) |
| 87 | return boolAttr; |
| 88 | return DenseElementsAttr::get(type: shapedType, values: boolAttr); |
| 89 | } |
| 90 | |
| 91 | OpFoldResult ICmpOp::fold(FoldAdaptor adaptor) { |
| 92 | if (getPredicate() != ICmpPredicate::eq && |
| 93 | getPredicate() != ICmpPredicate::ne) |
| 94 | return {}; |
| 95 | |
| 96 | // cmpi(eq/ne, x, x) -> true/false |
| 97 | if (getLhs() == getRhs()) |
| 98 | return getBoolAttribute(type: getType(), ctx: getContext(), |
| 99 | value: getPredicate() == ICmpPredicate::eq); |
| 100 | |
| 101 | // cmpi(eq/ne, alloca, null) -> false/true |
| 102 | if (getLhs().getDefiningOp<AllocaOp>() && getRhs().getDefiningOp<ZeroOp>()) |
| 103 | return getBoolAttribute(type: getType(), ctx: getContext(), |
| 104 | value: getPredicate() == ICmpPredicate::ne); |
| 105 | |
| 106 | // cmpi(eq/ne, null, alloca) -> cmpi(eq/ne, alloca, null) |
| 107 | if (getLhs().getDefiningOp<ZeroOp>() && getRhs().getDefiningOp<AllocaOp>()) { |
| 108 | Value lhs = getLhs(); |
| 109 | Value rhs = getRhs(); |
| 110 | getLhsMutable().assign(value: rhs); |
| 111 | getRhsMutable().assign(value: lhs); |
| 112 | return getResult(); |
| 113 | } |
| 114 | |
| 115 | return {}; |
| 116 | } |
| 117 | |
| 118 | //===----------------------------------------------------------------------===// |
| 119 | // ShlOp |
| 120 | //===----------------------------------------------------------------------===// |
| 121 | |
| 122 | OpFoldResult LLVM::ShlOp::fold(FoldAdaptor adaptor) { |
| 123 | auto rhs = dyn_cast_or_null<IntegerAttr>(Val: adaptor.getRhs()); |
| 124 | if (!rhs) |
| 125 | return {}; |
| 126 | |
| 127 | if (rhs.getValue().uge(RHS: getLhs().getType().getIntOrFloatBitWidth())) |
| 128 | return {}; // TODO: Fold into poison. |
| 129 | |
| 130 | auto lhs = dyn_cast_or_null<IntegerAttr>(Val: adaptor.getLhs()); |
| 131 | if (!lhs) |
| 132 | return {}; |
| 133 | |
| 134 | return IntegerAttr::get(type: getType(), value: lhs.getValue().shl(ShiftAmt: rhs.getValue())); |
| 135 | } |
| 136 | |
| 137 | //===----------------------------------------------------------------------===// |
| 138 | // OrOp |
| 139 | //===----------------------------------------------------------------------===// |
| 140 | |
| 141 | OpFoldResult LLVM::OrOp::fold(FoldAdaptor adaptor) { |
| 142 | auto lhs = dyn_cast_or_null<IntegerAttr>(Val: adaptor.getLhs()); |
| 143 | if (!lhs) |
| 144 | return {}; |
| 145 | |
| 146 | auto rhs = dyn_cast_or_null<IntegerAttr>(Val: adaptor.getRhs()); |
| 147 | if (!rhs) |
| 148 | return {}; |
| 149 | |
| 150 | return IntegerAttr::get(type: getType(), value: lhs.getValue() | rhs.getValue()); |
| 151 | } |
| 152 | |
| 153 | //===----------------------------------------------------------------------===// |
| 154 | // UDivOp |
| 155 | //===----------------------------------------------------------------------===// |
| 156 | Speculation::Speculatability UDivOp::getSpeculatability() { |
| 157 | // X / 0 => UB |
| 158 | Value divisor = getRhs(); |
| 159 | if (matchPattern(value: divisor, pattern: m_IntRangeWithoutZeroU())) |
| 160 | return Speculation::Speculatable; |
| 161 | |
| 162 | return Speculation::NotSpeculatable; |
| 163 | } |
| 164 | |
| 165 | //===----------------------------------------------------------------------===// |
| 166 | // SDivOp |
| 167 | //===----------------------------------------------------------------------===// |
| 168 | Speculation::Speculatability SDivOp::getSpeculatability() { |
| 169 | // This function conservatively assumes that all signed division by -1 are |
| 170 | // not speculatable. |
| 171 | // X / 0 => UB |
| 172 | // INT_MIN / -1 => UB |
| 173 | Value divisor = getRhs(); |
| 174 | if (matchPattern(value: divisor, pattern: m_IntRangeWithoutZeroS()) && |
| 175 | matchPattern(value: divisor, pattern: m_IntRangeWithoutNegOneS())) |
| 176 | return Speculation::Speculatable; |
| 177 | |
| 178 | return Speculation::NotSpeculatable; |
| 179 | } |
| 180 | |
| 181 | //===----------------------------------------------------------------------===// |
| 182 | // URemOp |
| 183 | //===----------------------------------------------------------------------===// |
| 184 | |
| 185 | Speculation::Speculatability URemOp::getSpeculatability() { |
| 186 | // urem(x, 0) => UB |
| 187 | Value divisor = getRhs(); |
| 188 | if (matchPattern(value: divisor, pattern: m_IntRangeWithoutZeroU())) |
| 189 | return Speculation::Speculatable; |
| 190 | |
| 191 | return Speculation::NotSpeculatable; |
| 192 | } |
| 193 | |
| 194 | //===----------------------------------------------------------------------===// |
| 195 | // SRemOp |
| 196 | //===----------------------------------------------------------------------===// |
| 197 | |
| 198 | Speculation::Speculatability SRemOp::getSpeculatability() { |
| 199 | // srem(x, 0) => UB |
| 200 | // srem(INT_MIN, -1) => UB |
| 201 | Value divisor = getRhs(); |
| 202 | if (matchPattern(value: divisor, pattern: m_IntRangeWithoutZeroS()) && |
| 203 | matchPattern(value: divisor, pattern: m_IntRangeWithoutNegOneS())) |
| 204 | return Speculation::Speculatable; |
| 205 | |
| 206 | return Speculation::NotSpeculatable; |
| 207 | } |
| 208 | |
| 209 | #define GET_OP_CLASSES |
| 210 | #include "mlir/Dialect/LLVMIR/LLVMArithOps.cpp.inc" |
| 211 | |