@@ -98,7 +98,7 @@ static Type getI1SameShape(Type type) {
9898}
9999
100100// ===----------------------------------------------------------------------===//
101- // Printing, parsing and builder for LLVM::CmpOp.
101+ // Printing, parsing, folding and builder for LLVM::CmpOp.
102102// ===----------------------------------------------------------------------===//
103103
104104void ICmpOp::print (OpAsmPrinter &p) {
@@ -175,6 +175,42 @@ ParseResult FCmpOp::parse(OpAsmParser &parser, OperationState &result) {
175175 return parseCmpOp<FCmpPredicate>(parser, result);
176176}
177177
178+ // / Returns a scalar or vector boolean attribute of the given type.
179+ static Attribute getBoolAttribute (Type type, MLIRContext *ctx, bool value) {
180+ auto boolAttr = BoolAttr::get (ctx, value);
181+ ShapedType shapedType = dyn_cast<ShapedType>(type);
182+ if (!shapedType)
183+ return boolAttr;
184+ return DenseElementsAttr::get (shapedType, boolAttr);
185+ }
186+
187+ OpFoldResult ICmpOp::fold (FoldAdaptor adaptor) {
188+ if (getPredicate () != ICmpPredicate::eq &&
189+ getPredicate () != ICmpPredicate::ne)
190+ return {};
191+
192+ // cmpi(eq/ne, x, x) -> true/false
193+ if (getLhs () == getRhs ())
194+ return getBoolAttribute (getType (), getContext (),
195+ getPredicate () == ICmpPredicate::eq);
196+
197+ // cmpi(eq/ne, alloca, null) -> false/true
198+ if (getLhs ().getDefiningOp <AllocaOp>() && getRhs ().getDefiningOp <NullOp>())
199+ return getBoolAttribute (getType (), getContext (),
200+ getPredicate () == ICmpPredicate::ne);
201+
202+ // cmpi(eq/ne, null, alloca) -> cmpi(eq/ne, alloca, null)
203+ if (getLhs ().getDefiningOp <NullOp>() && getRhs ().getDefiningOp <AllocaOp>()) {
204+ Value lhs = getLhs ();
205+ Value rhs = getRhs ();
206+ getLhsMutable ().assign (rhs);
207+ getRhsMutable ().assign (lhs);
208+ return getResult ();
209+ }
210+
211+ return {};
212+ }
213+
178214// ===----------------------------------------------------------------------===//
179215// Printing, parsing and verification for LLVM::AllocaOp.
180216// ===----------------------------------------------------------------------===//
@@ -2443,7 +2479,7 @@ Region *LLVMFuncOp::getCallableRegion() {
24432479}
24442480
24452481// ===----------------------------------------------------------------------===//
2446- // Verification for LLVM:: ConstantOp.
2482+ // ConstantOp.
24472483// ===----------------------------------------------------------------------===//
24482484
24492485LogicalResult LLVM::ConstantOp::verify () {
@@ -2503,6 +2539,25 @@ LogicalResult LLVM::ConstantOp::verify() {
25032539 return success ();
25042540}
25052541
2542+ bool LLVM::ConstantOp::isBuildableWith (Attribute value, Type type) {
2543+ // The value's type must be the same as the provided type.
2544+ auto typedAttr = dyn_cast<TypedAttr>(value);
2545+ if (!typedAttr || typedAttr.getType () != type || !isCompatibleType (type))
2546+ return false ;
2547+ // The value's type must be an LLVM compatible type.
2548+ if (!isCompatibleType (type))
2549+ return false ;
2550+ // TODO: Add support for additional attributes kinds once needed.
2551+ return isa<IntegerAttr, FloatAttr, ElementsAttr>(value);
2552+ }
2553+
2554+ ConstantOp LLVM::ConstantOp::materialize (OpBuilder &builder, Attribute value,
2555+ Type type, Location loc) {
2556+ if (isBuildableWith (value, type))
2557+ return builder.create <LLVM::ConstantOp>(loc, cast<TypedAttr>(value));
2558+ return nullptr ;
2559+ }
2560+
25062561// Constant op constant-folds to its value.
25072562OpFoldResult LLVM::ConstantOp::fold (FoldAdaptor) { return getValue (); }
25082563
@@ -3097,11 +3152,7 @@ LogicalResult LLVMDialect::verifyRegionResultAttribute(Operation *op,
30973152
30983153Operation *LLVMDialect::materializeConstant (OpBuilder &builder, Attribute value,
30993154 Type type, Location loc) {
3100- // TODO: Accept more possible attributes. So far, only IntegerAttr may come
3101- // up.
3102- if (!isa<IntegerAttr>(value))
3103- return nullptr ;
3104- return builder.create <LLVM::ConstantOp>(loc, type, value);
3155+ return LLVM::ConstantOp::materialize (builder, value, type, loc);
31053156}
31063157
31073158// ===----------------------------------------------------------------------===//
0 commit comments