[llvm-branch-commits] [clang] [CIR] Make cir-canonicalize a full canonicalizer (PR #240769)
Henrich Lauko via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Tue Nov 17 06:46:24 PST 2026
https://github.com/xlauko updated https://github.com/llvm/llvm-project/pull/240769
>From b433a84482271f20f6775ac386134ced34427511 Mon Sep 17 00:00:00 2001
From: Henrich Lauko <hlauko at nvidia.com>
Date: Tue, 17 Nov 2026 14:25:08 +0000
Subject: [PATCH] [CIR] Make cir-canonicalize a full canonicalizer
cir-canonicalize applied the CIR canonicalization patterns and folders
only to a hand-written list of operations. It now builds the pattern set
once in initialize() and runs CIR's greedy driver over every operation,
top-down like MLIR's canonicalizer, so every pattern and folder applies
everywhere. Only operations that rewriting left dead are erased, such as
the inputs of a folded operation: CIR stays close to the source, so
unused locals and ignored expressions are kept as in classic codegen.
Empty scopes and switches were removed only because they were dead, so
they get explicit canonicalization patterns.
For this, cir::applyOpPatternsGreedily takes an eraseOnlyNewlyDead flag.
With it, the driver records which of the given operations are unused
when rewriting starts and never erases those for being dead; a pattern
or a fold still can. The default still erases every trivially dead
operation.
The label tests used unused constants to show which code the driver
skips. Those constants now stay either way, so the tests use empty
scopes, which the new pattern erases once the driver visits them.
The new test-convergence option fails the pass when a second run of the
patterns changes the IR, as in the upstream canonicalizer.
Assisted-by: Claude Code (claude-opus-5-5).
---
clang/include/clang/CIR/Dialect/IR/CIROps.td | 2 +
clang/include/clang/CIR/Dialect/Passes.td | 12 ++-
.../Dialect/Transforms/CIRTransformUtils.h | 6 +-
clang/lib/CIR/Dialect/IR/CIRDialect.cpp | 54 +++++-----
.../Dialect/Transforms/CIRCanonicalize.cpp | 99 ++++++++-----------
.../Transforms/CIRGreedyRewriteDriver.cpp | 25 +++--
clang/test/CIR/CodeGen/atomic-libcall.c | 4 +
clang/test/CIR/CodeGen/atomic.c | 3 +
clang/test/CIR/CodeGen/bitint-record-layout.c | 3 +
clang/test/CIR/CodeGen/fold-during-cg.c | 16 +--
.../CIR/CodeGen/implicit-value-init-expr.cpp | 1 +
clang/test/CIR/CodeGen/new.cpp | 4 +
.../no-unique-address-empty-consteval.cpp | 3 +
.../test/CIR/CodeGen/paren-list-agg-init.cpp | 2 +-
.../CIR/CodeGen/pointer-to-data-member.cpp | 6 +-
clang/test/CIR/CodeGen/union.c | 5 +
clang/test/CIR/CodeGen/vla.c | 10 +-
clang/test/CIR/CodeGen/void-value-expr.cpp | 2 +
clang/test/CIR/CodeGenCXX/new-array-init.cpp | 6 ++
clang/test/CIR/CodeGenOpenACC/combined-copy.c | 1 +
.../CIR/CodeGenSYCL/global-address-space.cpp | 1 +
.../CIR/Transforms/canonicalize-dead-code.cir | 62 ++++++++++++
.../CIR/Transforms/canonicalize-label.cir | 59 ++++++-----
clang/test/CIR/Transforms/canonicalize.cir | 25 ++---
24 files changed, 276 insertions(+), 135 deletions(-)
create mode 100644 clang/test/CIR/Transforms/canonicalize-dead-code.cir
diff --git a/clang/include/clang/CIR/Dialect/IR/CIROps.td b/clang/include/clang/CIR/Dialect/IR/CIROps.td
index 5985bce8278b0b..3ae0369f1ca5f5 100644
--- a/clang/include/clang/CIR/Dialect/IR/CIROps.td
+++ b/clang/include/clang/CIR/Dialect/IR/CIROps.td
@@ -1447,6 +1447,7 @@ def CIR_ScopeOp : CIR_EnterAnyRegionBranchOpBase<"scope", [
let regions = (region AnyRegion:$scopeRegion);
let hasFolder = 1;
+ let hasCanonicalizeMethod = 1;
let hasVerifier = 1;
let skipDefaultBuilders = 1;
let assemblyFormat = [{
@@ -1827,6 +1828,7 @@ def CIR_SwitchOp : CIR_EnterAnyRegionBranchOpBase<"switch", [
let regions = (region AnyRegion:$body);
+ let hasCanonicalizeMethod = 1;
let skipDefaultBuilders = 1;
let builders = [
OpBuilder<(ins "mlir::Value":$condition,
diff --git a/clang/include/clang/CIR/Dialect/Passes.td b/clang/include/clang/CIR/Dialect/Passes.td
index 9a4bf9e576e4b1..53a6dba8ffadcb 100644
--- a/clang/include/clang/CIR/Dialect/Passes.td
+++ b/clang/include/clang/CIR/Dialect/Passes.td
@@ -11,10 +11,12 @@
include "mlir/Pass/PassBase.td"
-def CIRCanonicalize : Pass<"cir-canonicalize"> {
+def CIRCanonicalize : Pass<"cir-canonicalize", "mlir::ModuleOp"> {
let summary = "Performs CIR canonicalization";
let description = [{
- Perform canonicalizations on CIR and removes some redundant operations.
+ Applies the canonicalization patterns and folders of every CIR operation
+ to all operations until a fixpoint is reached. Operations that are unused
+ in the input are kept; operations that rewriting leaves dead are erased.
This pass performs basic cleanup and canonicalization transformations that
are not intended to affect CIR-to-source fidelity and high-level code
@@ -25,6 +27,12 @@ def CIRCanonicalize : Pass<"cir-canonicalize"> {
`cir-simplify` pass.
}];
+ let options = [
+ Option<"testConvergence", "test-convergence", "bool", /*default=*/"false",
+ "Test only: Fail pass if a second run of the patterns changes the "
+ "IR">
+ ];
+
let constructor = "mlir::createCIRCanonicalizePass()";
let dependentDialects = ["cir::CIRDialect"];
}
diff --git a/clang/include/clang/CIR/Dialect/Transforms/CIRTransformUtils.h b/clang/include/clang/CIR/Dialect/Transforms/CIRTransformUtils.h
index 4cae7fc4407b16..b19e581f85b517 100644
--- a/clang/include/clang/CIR/Dialect/Transforms/CIRTransformUtils.h
+++ b/clang/include/clang/CIR/Dialect/Transforms/CIRTransformUtils.h
@@ -72,10 +72,14 @@ void collectUnreachable(mlir::Operation *parent,
/// into branches, no CFG edge leads to such code, so MLIR's driver skips it as
/// unreachable. Unlike MLIR's driver, it folds an operation in place at most 64
/// times, which ends folds that never stop succeeding on a cycle of SSA values.
+/// With `eraseOnlyNewlyDead`, dead code elimination spares the operations of
+/// `ops` that are unused when rewriting starts, so it erases only what
+/// rewriting leaves dead. A pattern or a fold can still erase any operation.
/// Return whether the IR changed.
bool applyOpPatternsGreedily(llvm::ArrayRef<mlir::Operation *> ops,
const mlir::FrozenRewritePatternSet &patterns,
- mlir::Region &scope);
+ mlir::Region &scope,
+ bool eraseOnlyNewlyDead = false);
} // namespace cir
diff --git a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
index 160c54743e7ff8..a60438cb464747 100644
--- a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
+++ b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
@@ -2083,24 +2083,32 @@ LogicalResult cir::ScopeOp::verify() {
return success();
}
+/// Returns true if `region` is a single block that holds only a `cir.yield`.
+static bool isYieldOnlyRegion(Region ®ion) {
+ if (!region.hasOneBlock())
+ return false;
+ Block &block = region.front();
+ return llvm::hasSingleElement(block) && isa<cir::YieldOp>(block.front());
+}
+
LogicalResult cir::ScopeOp::fold(FoldAdaptor /*adaptor*/,
SmallVectorImpl<OpFoldResult> &results) {
- // Only fold "trivial" scopes: a single block containing only a `cir.yield`.
- if (!getRegion().hasOneBlock())
- return failure();
- Block &block = getRegion().front();
- if (block.getOperations().size() != 1)
+ // Only fold a trivial scope that produces a value.
+ if (getNumResults() != 1 || !isYieldOnlyRegion(getRegion()))
return failure();
-
- auto yield = dyn_cast<cir::YieldOp>(block.front());
- if (!yield)
+ auto yield = cast<cir::YieldOp>(getRegion().front().front());
+ if (yield.getNumOperands() != 1)
return failure();
+ results.push_back(yield.getOperand(0));
+ return success();
+}
- // Only fold when the scope produces a value.
- if (getNumResults() != 1 || yield.getNumOperands() != 1)
+/// Erases a scope that yields no value and contains only its terminator.
+LogicalResult cir::ScopeOp::canonicalize(ScopeOp op,
+ PatternRewriter &rewriter) {
+ if (op.getNumResults() != 0 || !isYieldOnlyRegion(op.getRegion()))
return failure();
-
- results.push_back(yield.getOperand(0));
+ rewriter.eraseOp(op);
return success();
}
@@ -2179,28 +2187,19 @@ void cir::CleanupScopeOp::getSuccessorRegions(
LogicalResult cir::CleanupScopeOp::canonicalize(CleanupScopeOp op,
PatternRewriter &rewriter) {
- auto isRegionTrivial = [](Region ®ion) {
- assert(!region.empty() && "CleanupScopeOp regions must not be empty");
- if (!region.hasOneBlock())
- return false;
- Block &block = llvm::getSingleElement(region);
- return llvm::hasSingleElement(block) &&
- isa<cir::YieldOp>(llvm::getSingleElement(block));
- };
-
Region &body = op.getBodyRegion();
Region &cleanup = op.getCleanupRegion();
// An EH-only cleanup scope with an empty body can never trigger its cleanup
// region — there are no operations in the body that could throw. Erase it.
- if (op.getCleanupKind() == CleanupKind::EH && isRegionTrivial(body)) {
+ if (op.getCleanupKind() == CleanupKind::EH && isYieldOnlyRegion(body)) {
rewriter.eraseOp(op);
return success();
}
// A cleanup scope with a trivial cleanup region has no cleanup to perform.
// Inline the body into the parent block and erase the scope.
- if (!isRegionTrivial(cleanup) || !body.hasOneBlock())
+ if (!isYieldOnlyRegion(cleanup) || !body.hasOneBlock())
return failure();
Block &bodyBlock = body.front();
@@ -2386,6 +2385,15 @@ void cir::CaseOp::build(OpBuilder &builder, OperationState &result,
// SwitchOp
//===----------------------------------------------------------------------===//
+/// Erases a switch whose body contains only its terminator.
+LogicalResult cir::SwitchOp::canonicalize(SwitchOp op,
+ PatternRewriter &rewriter) {
+ if (!isYieldOnlyRegion(op.getBody()))
+ return failure();
+ rewriter.eraseOp(op);
+ return success();
+}
+
void cir::SwitchOp::build(OpBuilder &builder, OperationState &result,
Value cond, BuilderOpStateCallbackRef switchBuilder) {
assert(switchBuilder && "the builder callback for regions must be present");
diff --git a/clang/lib/CIR/Dialect/Transforms/CIRCanonicalize.cpp b/clang/lib/CIR/Dialect/Transforms/CIRCanonicalize.cpp
index 1374f608a0d737..c0d496fc537c32 100644
--- a/clang/lib/CIR/Dialect/Transforms/CIRCanonicalize.cpp
+++ b/clang/lib/CIR/Dialect/Transforms/CIRCanonicalize.cpp
@@ -6,25 +6,21 @@
//
//===----------------------------------------------------------------------===//
//
-// This file implements pass that canonicalizes CIR operations, eliminating
-// redundant branches, empty scopes, and other unnecessary operations.
+// This file implements the CIR canonicalization pass. It applies the
+// canonicalization patterns and folders of the CIR dialect to every operation
+// until a fixpoint is reached.
//
//===----------------------------------------------------------------------===//
#include "PassDetail.h"
-#include "mlir/Dialect/Func/IR/FuncOps.h"
-#include "mlir/IR/Block.h"
-#include "mlir/IR/Operation.h"
+#include "mlir/IR/Iterators.h"
#include "mlir/IR/PatternMatch.h"
-#include "mlir/IR/Region.h"
-#include "mlir/Support/LogicalResult.h"
+#include "mlir/Rewrite/FrozenRewritePatternSet.h"
#include "clang/CIR/Dialect/IR/CIRDialect.h"
#include "clang/CIR/Dialect/Passes.h"
#include "clang/CIR/Dialect/Transforms/CIRTransformUtils.h"
-#include "clang/CIR/MissingFeatures.h"
using namespace mlir;
-using namespace cir;
namespace mlir {
#define GEN_PASS_DEF_CIRCANONICALIZE
@@ -33,61 +29,52 @@ namespace mlir {
namespace {
-//===----------------------------------------------------------------------===//
-// CIRCanonicalizePass
-//===----------------------------------------------------------------------===//
-
struct CIRCanonicalizePass
: public impl::CIRCanonicalizeBase<CIRCanonicalizePass> {
using CIRCanonicalizeBase::CIRCanonicalizeBase;
- // The same operation rewriting done here could have been performed
- // by CanonicalizerPass (adding hasCanonicalizer for target Ops and
- // implementing the same from above in CIRDialects.cpp). However, it's
- // currently too aggressive for static analysis purposes, since it might
- // remove things where a diagnostic can be generated.
- //
- // FIXME: perhaps we can add one more mode to GreedyRewriteConfig to
- // disable this behavior.
- void runOnOperation() override;
-};
+ LogicalResult initialize(MLIRContext *context) override {
+ RewritePatternSet owningPatterns(context);
+ for (RegisteredOperationName op : context->getRegisteredOperationsByDialect(
+ cir::CIRDialect::getDialectNamespace()))
+ op.getCanonicalizationPatterns(owningPatterns, context);
+ patterns =
+ std::make_shared<FrozenRewritePatternSet>(std::move(owningPatterns));
+ return success();
+ }
-void CIRCanonicalizePass::runOnOperation() {
- RewritePatternSet patterns(&getContext());
+ void runOnOperation() override {
+ Region &body = getOperation().getBodyRegion();
+ bool changed = applyPatterns(body);
+ // A single run must reach a fixpoint, so a second run changes nothing.
+ if (testConvergence && applyPatterns(body))
+ return signalPassFailure();
+ if (!changed)
+ markAllAnalysesPreserved();
+ }
- // Collect canonicalization patterns from CIR ops.
- mlir::Dialect *cir = getContext().getLoadedDialect<cir::CIRDialect>();
- for (mlir::RegisteredOperationName op :
- getContext().getRegisteredOperations())
- if (&op.getDialect() == cir)
- op.getCanonicalizationPatterns(patterns, &getContext());
+ /// Applies the patterns to every operation in `body` and returns whether
+ /// the IR changed.
+ bool applyPatterns(Region &body) {
+ // The driver pops from the back of the worklist, so seeding it in reverse
+ // pre-order visits the IR top-down.
+ SmallVector<Operation *> ops;
+ body.walk<WalkOrder::PostOrder, ReverseIterator>(
+ [&](Operation *op) { ops.push_back(op); });
- // Collect operations to apply patterns.
- llvm::SmallVector<Operation *, 16> ops;
- getOperation()->walk([&](Operation *op) {
- assert(!cir::MissingFeatures::tryOp());
- assert(!cir::MissingFeatures::callOp());
+ // CIR's driver also rewrites the code that control enters only through
+ // cir.goto or cir.switch, which MLIR's drivers skip as unreachable until
+ // cir-flatten-cfg and cir-goto-solver turn those jumps into branches.
+ //
+ // Only operations that rewriting left dead are erased: CIR stays close to
+ // the source, so an unused local or an ignored expression is still
+ // emitted, as in classic codegen.
+ return cir::applyOpPatternsGreedily(ops, *patterns, body,
+ /*eraseOnlyNewlyDead=*/true);
+ }
- // Many operations are here to perform a manual `fold` in
- // applyOpPatternsGreedily.
- if (isa<BrOp, BrCondOp, BuiltinIntCastOp, CastOp, ScopeOp, SwitchOp,
- SelectOp, IncOp, DecOp, MinusOp, FNegOp, NotOp, AddOp, MulOp, AndOp,
- OrOp, XorOp, MaxOp, MinOp, ComplexCreateOp, ComplexImagOp,
- ComplexRealOp, VecCmpOp, VecFCmpOp, VecCreateOp, VecExtractOp,
- VecShuffleOp, VecShuffleDynamicOp, VecTernaryOp, BitClrsbOp,
- BitClzOp, BitCtzOp, BitFfsOp, BitParityOp, BitPopcountOp,
- BitReverseOp, ByteSwapOp, RotateOp, ConstantOp, CleanupScopeOp>(op))
- ops.push_back(op);
- });
-
- // Apply patterns. CIR's driver also rewrites the code that control enters
- // only through cir.goto or cir.switch, which MLIR's driver skips as
- // unreachable until cir-flatten-cfg and cir-goto-solver turn those jumps into
- // branches.
- cir::applyOpPatternsGreedily(ops,
- FrozenRewritePatternSet(std::move(patterns)),
- getOperation()->getRegion(0));
-}
+ std::shared_ptr<const FrozenRewritePatternSet> patterns;
+};
} // namespace
diff --git a/clang/lib/CIR/Dialect/Transforms/CIRGreedyRewriteDriver.cpp b/clang/lib/CIR/Dialect/Transforms/CIRGreedyRewriteDriver.cpp
index 746bc8df020b4b..0c5c63d3fd6f84 100644
--- a/clang/lib/CIR/Dialect/Transforms/CIRGreedyRewriteDriver.cpp
+++ b/clang/lib/CIR/Dialect/Transforms/CIRGreedyRewriteDriver.cpp
@@ -79,9 +79,10 @@ class GreedyRewriteDriver final : public RewriterBase::Listener {
rewriter.setListener(this);
}
- /// Rewrite `ops` and every operation the rewrites touch. Return whether the
- /// IR changed.
- bool run(ArrayRef<Operation *> ops);
+ /// Rewrite `ops` and every operation the rewrites touch. With
+ /// `eraseOnlyNewlyDead`, dead code elimination spares the operations of
+ /// `ops` that are unused now. Return whether the IR changed.
+ bool run(ArrayRef<Operation *> ops, bool eraseOnlyNewlyDead);
private:
bool isReachable(Operation *op);
@@ -105,13 +106,20 @@ class GreedyRewriteDriver final : public RewriterBase::Listener {
cir::GotoAnalysis reachability;
/// How many times each operation has folded in place.
DenseMap<Operation *, unsigned> inPlaceFolds;
+ /// Operations to keep even when they are trivially dead. With
+ /// `eraseOnlyNewlyDead`, the given operations that start out unused.
+ DenseSet<Operation *> keptIfDead;
};
} // namespace
-bool GreedyRewriteDriver::run(ArrayRef<Operation *> ops) {
- for (Operation *op : ops)
+bool GreedyRewriteDriver::run(ArrayRef<Operation *> ops,
+ bool eraseOnlyNewlyDead) {
+ for (Operation *op : ops) {
worklist.push(op);
+ if (eraseOnlyNewlyDead && op->use_empty())
+ keptIfDead.insert(op);
+ }
bool changed = false;
while (!worklist.empty()) {
@@ -119,7 +127,7 @@ bool GreedyRewriteDriver::run(ArrayRef<Operation *> ops) {
if (!isReachable(op))
continue;
- if (deadness.isOpTriviallyDead(op)) {
+ if (!keptIfDead.contains(op) && deadness.isOpTriviallyDead(op)) {
rewriter.eraseOp(op);
changed = true;
continue;
@@ -289,13 +297,14 @@ void GreedyRewriteDriver::notifyOperationErased(Operation *op) {
deadness.invalidate(op);
reachability.notifyOperationErased(op);
inPlaceFolds.erase(op);
+ keptIfDead.erase(op);
addOperandsToWorklist(op);
worklist.remove(op);
}
bool cir::applyOpPatternsGreedily(ArrayRef<Operation *> ops,
const FrozenRewritePatternSet &patterns,
- Region &scope) {
+ Region &scope, bool eraseOnlyNewlyDead) {
assert(llvm::all_of(ops,
[&](Operation *op) {
return scope.findAncestorOpInRegion(*op) != nullptr;
@@ -304,5 +313,5 @@ bool cir::applyOpPatternsGreedily(ArrayRef<Operation *> ops,
if (ops.empty())
return false;
return GreedyRewriteDriver(ops.front()->getContext(), patterns, scope)
- .run(ops);
+ .run(ops, eraseOnlyNewlyDead);
}
diff --git a/clang/test/CIR/CodeGen/atomic-libcall.c b/clang/test/CIR/CodeGen/atomic-libcall.c
index b20eefce22b76b..cd2fdbe2fdb2e5 100644
--- a/clang/test/CIR/CodeGen/atomic-libcall.c
+++ b/clang/test/CIR/CodeGen/atomic-libcall.c
@@ -38,6 +38,7 @@ void scoped_load(struct Big *ptr) {
__scoped_atomic_load(ptr, &b, __ATOMIC_RELAXED, __MEMORY_SCOPE_SYSTEM);
// CIR: %[[DEST_SLOT:.+]] = cir.alloca "b" align(4) : !cir.ptr<!rec_Big>
// CIR: %[[PTR:.+]] = cir.load align(8) %{{.+}} : !cir.ptr<!cir.ptr<!rec_Big>>, !cir.ptr<!rec_Big>
+ // CIR-NEXT: %{{.+}} = cir.const #cir.int<0> : !s32i
// CIR-NEXT: %[[PTR_INTPTR:.+]] = cir.cast bitcast %[[PTR]] : !cir.ptr<!rec_Big> -> !cir.ptr<!cir.int<u, 192>>
// CIR-NEXT: %[[DEST_INTPTR:.+]] = cir.cast bitcast %[[DEST_SLOT]] : !cir.ptr<!rec_Big> -> !cir.ptr<!cir.int<u, 192>>
// CIR-NEXT: %[[SIZE:.+]] = cir.const #cir.int<24> : !u64i
@@ -102,6 +103,7 @@ void scoped_store(struct Big *dest, struct Big *val) {
__scoped_atomic_store(dest, val, __ATOMIC_RELAXED, __MEMORY_SCOPE_SYSTEM);
// CIR: %[[DEST_PTR:.+]] = cir.load align(8) %{{.+}} : !cir.ptr<!cir.ptr<!rec_Big>>, !cir.ptr<!rec_Big>
+ // CIR-NEXT: %{{.+}} = cir.const #cir.int<0> : !s32i
// CIR-NEXT: %[[VALUE_PTR:.+]] = cir.load align(8) %{{.+}} : !cir.ptr<!cir.ptr<!rec_Big>>, !cir.ptr<!rec_Big>
// CIR-NEXT: %[[DEST_INTPTR:.+]] = cir.cast bitcast %[[DEST_PTR]] : !cir.ptr<!rec_Big> -> !cir.ptr<!cir.int<u, 192>>
// CIR-NEXT: %[[VALUE_INTPTR:.+]] = cir.cast bitcast %[[VALUE_PTR]] : !cir.ptr<!rec_Big> -> !cir.ptr<!cir.int<u, 192>>
@@ -177,6 +179,7 @@ void scoped_exchange(struct Big *ptr, struct Big *val, struct Big *old) {
// CIR-NEXT: %[[VAL:.*]] = cir.alloca "val" align(8) init : !cir.ptr<!cir.ptr<!rec_Big>>
// CIR-NEXT: %[[OLD:.*]] = cir.alloca "old" align(8) init : !cir.ptr<!cir.ptr<!rec_Big>>
// CIR: %[[LOAD_PTR:.*]] = cir.load align(8) %[[PTR]] : !cir.ptr<!cir.ptr<!rec_Big>>, !cir.ptr<!rec_Big>
+ // CIR-NEXT: %{{.+}} = cir.const #cir.int<0> : !s32i
// CIR-NEXT: %[[LOAD_VAL:.*]] = cir.load align(8) %[[VAL]] : !cir.ptr<!cir.ptr<!rec_Big>>, !cir.ptr<!rec_Big>
// CIR-NEXT: %[[LOAD_OLD:.*]] = cir.load align(8) %[[OLD]] : !cir.ptr<!cir.ptr<!rec_Big>>, !cir.ptr<!rec_Big>
// CIR-NEXT: %[[CAST_PTR:.*]] = cir.cast bitcast %[[LOAD_PTR]] : !cir.ptr<!rec_Big> -> !cir.ptr<!cir.int<u, 192>>
@@ -284,6 +287,7 @@ void scoped_cmpxchg(struct Big *ptr, struct Big *expected, struct Big *desired)
__scoped_atomic_compare_exchange(ptr, expected, desired, /*weak=*/0, __ATOMIC_SEQ_CST, __ATOMIC_ACQUIRE, __MEMORY_SCOPE_SYSTEM);
// CIR: %[[PTR:.+]] = cir.load align(8) %{{.+}} : !cir.ptr<!cir.ptr<!rec_Big>>, !cir.ptr<!rec_Big>
+ // CIR-NEXT: %{{.+}} = cir.const #cir.int<0> : !s32i
// CIR-NEXT: %[[EXPECTED:.+]] = cir.load align(8) %{{.+}} : !cir.ptr<!cir.ptr<!rec_Big>>, !cir.ptr<!rec_Big>
// CIR-NEXT: %[[DESIRED:.+]] = cir.load align(8) %{{.+}} : !cir.ptr<!cir.ptr<!rec_Big>>, !cir.ptr<!rec_Big>
// CIR-NEXT: %[[PTR_INTPTR:.+]] = cir.cast bitcast %[[PTR]] : !cir.ptr<!rec_Big> -> !cir.ptr<!cir.int<u, 192>>
diff --git a/clang/test/CIR/CodeGen/atomic.c b/clang/test/CIR/CodeGen/atomic.c
index 97e7435d68f7af..f151dd43e036ea 100644
--- a/clang/test/CIR/CodeGen/atomic.c
+++ b/clang/test/CIR/CodeGen/atomic.c
@@ -102,12 +102,15 @@ void init_with_padding(_Atomic struct S1 *p) {
// CIR-NEXT: cir.libc.memset %[[MEMSET_SIZE]] bytes at %[[PTR_VOID]] align(8) to %[[MEMSET_VALUE]] : !cir.ptr<!void>, !u8i, !u64i
// CIR-NEXT: %[[VALUE_PTR:.+]] = cir.get_member %[[PTR]][0] {name = "value"} : !cir.ptr<!rec_anon_struct> -> !cir.ptr<!rec_S1>
// CIR-NEXT: %[[X_PTR:.+]] = cir.get_member %[[VALUE_PTR]][0] {name = "x"} : !cir.ptr<!rec_S1> -> !cir.ptr<!s16i>
+ // CIR-NEXT: %{{.+}} = cir.const #cir.int<1> : !s32i
// CIR-NEXT: %[[X_INIT:.+]] = cir.const #cir.int<1> : !s16i
// CIR-NEXT: cir.store align(8) %[[X_INIT]], %[[X_PTR]] : !s16i, !cir.ptr<!s16i>
// CIR-NEXT: %[[Y_PTR:.+]] = cir.get_member %[[VALUE_PTR]][1] {name = "y"} : !cir.ptr<!rec_S1> -> !cir.ptr<!s16i>
+ // CIR-NEXT: %{{.+}} = cir.const #cir.int<2> : !s32i
// CIR-NEXT: %[[Y_INIT:.+]] = cir.const #cir.int<2> : !s16i
// CIR-NEXT: cir.store align(2) %[[Y_INIT]], %[[Y_PTR]] : !s16i, !cir.ptr<!s16i>
// CIR-NEXT: %[[Z_PTR:.+]] = cir.get_member %[[VALUE_PTR]][2] {name = "z"} : !cir.ptr<!rec_S1> -> !cir.ptr<!s16i>
+ // CIR-NEXT: %{{.+}} = cir.const #cir.int<3> : !s32i
// CIR-NEXT: %[[Z_INIT:.+]] = cir.const #cir.int<3> : !s16i
// CIR-NEXT: cir.store align(4) %[[Z_INIT]], %[[Z_PTR]] : !s16i, !cir.ptr<!s16i>
diff --git a/clang/test/CIR/CodeGen/bitint-record-layout.c b/clang/test/CIR/CodeGen/bitint-record-layout.c
index d4c95bee2a49ba..aa0dc008bfe561 100644
--- a/clang/test/CIR/CodeGen/bitint-record-layout.c
+++ b/clang/test/CIR/CodeGen/bitint-record-layout.c
@@ -201,6 +201,7 @@ _BitInt(128) get_bi(void) { return l128[1].bi; }
// CIR-LABEL: cir.func no_inline dso_local @get_bi() -> !s128i_bitint
// CIR-NEXT: %[[RET_ALLOC:.*]] = cir.alloca "__retval" align(8) : !cir.ptr<!s128i_bitint>
// CIR-NEXT: %[[GET_GLOB:.*]] = cir.get_global @l128 : !cir.ptr<!cir.array<!rec_Last128 x 2>>
+// CIR-NEXT: %{{.+}} = cir.const #cir.int<1> : !s32i
// CIR-NEXT: %[[ONE:.*]] = cir.const #cir.int<1> : !s64i
// CIR-NEXT: %[[ARR_GEP:.*]] = cir.get_element inbounds %[[GET_GLOB]][%[[ONE]] : !s64i] : !cir.ptr<!cir.array<!rec_Last128 x 2>> -> !cir.ptr<!rec_Last128>
// CIR-NEXT: %[[GET_BI:.*]] = cir.get_member %[[ARR_GEP]][2] {name = "bi"} : !cir.ptr<!rec_Last128> -> !cir.ptr<!s128i_bitint>
@@ -215,9 +216,11 @@ _BitInt(128) get_bi2(void) { return arrMem[1].bi[1]; }
// CIR-LABEL: cir.func no_inline dso_local @get_bi2() -> !s128i_bitint attributes {"cir.target-features" = "+cx8,+mmx,+sse,+sse2,+x87", nothrow, optnone} {
// CIR-NEXT: %[[RET_ALLOC:.*]] = cir.alloca "__retval" align(8) : !cir.ptr<!s128i_bitint>
// CIR-NEXT: %[[GET_GLOB:.*]] = cir.get_global @arrMem : !cir.ptr<!cir.array<!rec_ArrMem x 2>>
+// CIR-NEXT: %{{.+}} = cir.const #cir.int<1> : !s32i
// CIR-NEXT: %[[ONE_2:.*]] = cir.const #cir.int<1> : !s64i
// CIR-NEXT: %[[ARR_GEP:.*]] = cir.get_element inbounds %[[GET_GLOB]][%[[ONE_2]] : !s64i] : !cir.ptr<!cir.array<!rec_ArrMem x 2>> -> !cir.ptr<!rec_ArrMem>
// CIR-NEXT: %[[GET_BI_ARR:.*]] = cir.get_member %[[ARR_GEP]][2] {name = "bi"} : !cir.ptr<!rec_ArrMem> -> !cir.ptr<!cir.array<!s128i_bitint x 2>>
+// CIR-NEXT: %{{.+}} = cir.const #cir.int<1> : !s32i
// CIR-NEXT: %[[ONE:.*]] = cir.const #cir.int<1> : !s64i
// CIR-NEXT: %[[GET_BI_ELT:.*]] = cir.get_element inbounds %[[GET_BI_ARR]][%[[ONE]] : !s64i] : !cir.ptr<!cir.array<!s128i_bitint x 2>> -> !cir.ptr<!s128i_bitint>
// CIR-NEXT: %[[LOAD_BI:.*]] = cir.load align(8) %[[GET_BI_ELT]] : !cir.ptr<!s128i_bitint>, !s128i_bitint
diff --git a/clang/test/CIR/CodeGen/fold-during-cg.c b/clang/test/CIR/CodeGen/fold-during-cg.c
index 137d5bb3d4f20d..1e2eb0bbcd2419 100644
--- a/clang/test/CIR/CodeGen/fold-during-cg.c
+++ b/clang/test/CIR/CodeGen/fold-during-cg.c
@@ -1,9 +1,9 @@
// RUN: %clang_cc1 -std=c23 -triple x86_64-unknown-linux-gnu -mmlir -mlir-print-ir-before=cir-canonicalize -Wno-unused-value -Wno-constant-conversion -Wno-literal-conversion -fclangir -emit-cir %s -o %t.cir 2> %t-before.cir
// Note: The "before" check intentionally uses the same CIR check prefixes to
// verify that the IR is folded during code generation and not during
-// canonicalization. In a few cases, it is necessary to match an unused
-// duplicate constant that will be erased during canonicalization.
-// RUN: FileCheck --input-file=%t-before.cir %s -check-prefixes=CIR,CIR-DUP,ALL
+// canonicalization. In a few cases, an unused duplicate constant is
+// matched explicitly; canonicalization does not erase it.
+// RUN: FileCheck --input-file=%t-before.cir %s -check-prefixes=CIR,ALL
// RUN: FileCheck --input-file=%t.cir %s -check-prefixes=CIR,ALL
// RUN: %clang_cc1 -std=c23 -triple x86_64-unknown-linux-gnu -Wno-unused-value -Wno-constant-conversion -Wno-literal-conversion -fclangir -emit-llvm %s -o %t-cir.ll
// RUN: FileCheck --input-file=%t-cir.ll %s -check-prefixes=LLVM_OGCG,ALL
@@ -133,7 +133,7 @@ void fold_int_minus() {
// LLVM_OGCG: store i32 -2, ptr %{{.*}}
n = -(-3);
- // CIR-DUP: %[[UNUSED_THREE:.*]] = cir.const #cir.int<3> : !s32i
+ // CIR: %[[UNUSED_THREE:.*]] = cir.const #cir.int<3> : !s32i
// CIR: %[[THREE:.*]] = cir.const #cir.int<3> : !s32i
// CIR-NOT: cir.minus
// CIR: cir.store{{.*}} %[[THREE]], %{{.*}}
@@ -158,7 +158,7 @@ void fold_int_minus() {
// LLVM_OGCG: store i16 -2, ptr %{{.*}}
s = -(-3);
- // CIR-DUP: %[[UNUSED_THREE:.*]] = cir.const #cir.int<3> : !s32i
+ // CIR: %[[UNUSED_THREE:.*]] = cir.const #cir.int<3> : !s32i
// CIR: %[[THREE:.*]] = cir.const #cir.int<3> : !s16i
// CIR-NOT: cir.minus
// CIR: cir.store{{.*}} %[[THREE]], %{{.*}}
@@ -183,7 +183,7 @@ void fold_int_minus() {
// LLVM_OGCG: store i32 -2, ptr %{{.*}}
u = -(-3);
- // CIR-DUP: %[[UNUSED_THREE:.*]] = cir.const #cir.int<3> : !s32i
+ // CIR: %[[UNUSED_THREE:.*]] = cir.const #cir.int<3> : !s32i
// CIR: %[[THREE:.*]] = cir.const #cir.int<3> : !u32i
// CIR-NOT: cir.minus
// CIR: cir.store{{.*}} %[[THREE]], %{{.*}}
@@ -238,7 +238,7 @@ void fold_float_minus() {
// LLVM_OGCG: store float -2.000000e+00, ptr %{{.*}}
f = -(-3.0f);
- // CIR-DUP: %[[UNUSED_THREE:.*]] = cir.const #cir.fp<3.000000e+00> : !cir.float
+ // CIR: %[[UNUSED_THREE:.*]] = cir.const #cir.fp<3.000000e+00> : !cir.float
// CIR: %[[THREE:.*]] = cir.const #cir.fp<3.000000e+00> : !cir.float
// CIR-NOT: cir.minus
// CIR: cir.store{{.*}} %[[THREE]], %{{.*}}
@@ -251,7 +251,7 @@ void fold_float_minus() {
// LLVM_OGCG: store double -2.000000e+00, ptr %{{.*}}
d = -(-3.0);
- // CIR-DUP: %[[UNUSED_THREE:.*]] = cir.const #cir.fp<3.000000e+00> : !cir.double
+ // CIR: %[[UNUSED_THREE:.*]] = cir.const #cir.fp<3.000000e+00> : !cir.double
// CIR: %[[THREE:.*]] = cir.const #cir.fp<3.000000e+00> : !cir.double
// CIR-NOT: cir.minus
// CIR: cir.store{{.*}} %[[THREE]], %{{.*}}
diff --git a/clang/test/CIR/CodeGen/implicit-value-init-expr.cpp b/clang/test/CIR/CodeGen/implicit-value-init-expr.cpp
index 399e72bcf202d4..e780b9780eb813 100644
--- a/clang/test/CIR/CodeGen/implicit-value-init-expr.cpp
+++ b/clang/test/CIR/CodeGen/implicit-value-init-expr.cpp
@@ -14,6 +14,7 @@ void test(void *p) {
// CIR: cir.func{{.*}} @_Z4testPv
// CIR-NEXT: %[[P:.*]] = cir.alloca "p" align(8) init : !cir.ptr<!cir.ptr<!void>>
// CIR-NEXT: cir.store %arg0, %[[P]] : !cir.ptr<!void>, !cir.ptr<!cir.ptr<!void>>
+// CIR-NEXT: %{{.+}} = cir.const #cir.int<4> : !u64i
// CIR-NEXT: %[[P1:.*]] = cir.load align(8) %[[P]] : !cir.ptr<!cir.ptr<!void>>, !cir.ptr<!void>
// CIR-NEXT: %[[P2:.*]] = cir.cast bitcast %[[P1]] : !cir.ptr<!void> -> !cir.ptr<!s32i>
// CIR-NEXT: %[[P3:.*]] = cir.const #cir.int<0> : !s32i
diff --git a/clang/test/CIR/CodeGen/new.cpp b/clang/test/CIR/CodeGen/new.cpp
index 3a26f7264c0001..e89a030ab360ea 100644
--- a/clang/test/CIR/CodeGen/new.cpp
+++ b/clang/test/CIR/CodeGen/new.cpp
@@ -464,6 +464,7 @@ void t_new_huge_float_size() {
}
// CHECK: cir.func {{.*}} @_Z21t_new_huge_float_sizev()
+// CHECK: %{{.+}} = cir.const #cir.int<18446744073709551615> : !u64i
// CHECK: %[[ALLOCATION_SIZE:.*]] = cir.const #cir.int<18446744073709551615> : !u64i
// CHECK: %{{.*}} = cir.call @_Znam(%[[ALLOCATION_SIZE]]) {allocsize = array<i32: 0>, builtin, memory_effects = #cir.memory_effects<other = none, arg_mem = none, inaccessible_mem = readwrite, errno_mem = write, target_mem0 = none, target_mem1 = none>} : (!u64i {llvm.noundef}) -> (!cir.ptr<!void> {llvm.noalias, llvm.nonnull, llvm.noundef})
@@ -1140,6 +1141,7 @@ void test_array_new_with_ctor_partial_init_list() {
// CIR-BEFORE-LPP: cir.func {{.*}} @_Z42test_array_new_with_ctor_partial_init_listv
// CIR-BEFORE-LPP: %[[P_ADDR:.*]] = cir.alloca "p" {{.*}} init : !cir.ptr<!cir.ptr<!rec_G>>
+// CIR-BEFORE-LPP: %{{.+}} = cir.const #cir.int<8> : !u64i
// CIR-BEFORE-LPP: %[[EIGHT:.*]] = cir.const #cir.int<8> : !u64i
// CIR-BEFORE-LPP: %[[RAW_PTR:.*]] = cir.call @_Znam(%[[EIGHT]]) {allocsize = array<i32: 0>, builtin, memory_effects = #cir.memory_effects<other = none, arg_mem = none, inaccessible_mem = readwrite, errno_mem = write, target_mem0 = none, target_mem1 = none>} : (!u64i {llvm.noundef})
// CIR-BEFORE-LPP: %[[BEGIN:.*]] = cir.cast bitcast %[[RAW_PTR]] : !cir.ptr<!void> -> !cir.ptr<!rec_G>
@@ -1161,6 +1163,7 @@ void test_array_new_with_ctor_partial_init_list() {
// CHECK: cir.func {{.*}} @_Z42test_array_new_with_ctor_partial_init_listv
// CHECK: %[[P_ADDR:.*]] = cir.alloca "p" {{.*}} init : !cir.ptr<!cir.ptr<!rec_G>>
+// CHECK: %{{.+}} = cir.const #cir.int<8> : !u64i
// CHECK: %[[EIGHT:.*]] = cir.const #cir.int<8> : !u64i
// CHECK: %[[RAW_PTR:.*]] = cir.call @_Znam(%[[EIGHT]]) {allocsize = array<i32: 0>, builtin, memory_effects = #cir.memory_effects<other = none, arg_mem = none, inaccessible_mem = readwrite, errno_mem = write, target_mem0 = none, target_mem1 = none>} : (!u64i {llvm.noundef})
// CHECK: %[[BEGIN:.*]] = cir.cast bitcast %[[RAW_PTR]] : !cir.ptr<!void> -> !cir.ptr<!rec_G>
@@ -1240,6 +1243,7 @@ namespace gh227980 {
}
// CHECK-LABEL: cir.func{{.*}}@_ZN8gh22798015huge_float_sizeEv()
+// CHECK: %{{.+}} = cir.const #cir.int<18446744073709551615> : !u64i
// CHECK: %[[NEG:.*]] = cir.const #cir.int<18446744073709551615> : !u64i
// CHECK: cir.call @_Znam(%[[NEG]]) {allocsize = array<i32: 0>, builtin, memory_effects = #cir.memory_effects<other = none, arg_mem = none, inaccessible_mem = readwrite, errno_mem = write, target_mem0 = none, target_mem1 = none>} : (!u64i {llvm.noundef})
diff --git a/clang/test/CIR/CodeGen/no-unique-address-empty-consteval.cpp b/clang/test/CIR/CodeGen/no-unique-address-empty-consteval.cpp
index 403a191a40f751..026f69f8d19b57 100644
--- a/clang/test/CIR/CodeGen/no-unique-address-empty-consteval.cpp
+++ b/clang/test/CIR/CodeGen/no-unique-address-empty-consteval.cpp
@@ -65,6 +65,7 @@ struct U {
// CIR-LABEL: cir.func {{.*}} @_Z19test_aggregate_initv(
// CIR: %[[V3:.*]] = cir.get_member %{{.+}}[0] {name = "v3"}
+// CIR-NEXT: %{{.+}} = cir.const #cir.int<39612> : !s32i
// CIR-NEXT: %[[C:.*]] = cir.const #cir.int<39612>
// CIR-NEXT: cir.store align(8) %[[C]], %[[V3]] : !u64i, !cir.ptr<!u64i>
// CIR-NOT: #cir.zero : !rec_Empty
@@ -94,6 +95,7 @@ unsigned long test_default_member_init() {
// CIR: %[[V:.*]] = cir.get_member %{{.+}}[0] {name = "v"}
// CIR: %[[X:.*]] = cir.load align(8) %{{.+}} : !cir.ptr<!u64i>, !u64i
// CIR-NEXT: cir.store align(8) %[[X]], %[[V]] : !u64i, !cir.ptr<!u64i>
+// CIR-NEXT: %{{.+}} = cir.cast bitcast %{{.+}} : !cir.ptr<!rec_S> -> !cir.ptr<!rec_Empty>
// CIR-NEXT: cir.return
// LLVM-LABEL: define {{.*}} void @_ZN1SC2Em(
@@ -119,6 +121,7 @@ unsigned long test_default_member_init() {
// CIR: %[[V4:.*]] = cir.get_member %{{.+}}[0] {name = "v4"}
// CIR: %[[X4:.*]] = cir.load align(8) %{{.+}} : !cir.ptr<!u64i>, !u64i
// CIR-NEXT: cir.store align(8) %[[X4]], %[[V4]] : !u64i, !cir.ptr<!u64i>
+// CIR-NEXT: %{{.+}} = cir.cast bitcast %{{.+}} : !cir.ptr<!rec_V> -> !cir.ptr<!rec_Empty>
// CIR-NEXT: cir.return
// LLVM-LABEL: define {{.*}} void @_ZN1VC2Em(
diff --git a/clang/test/CIR/CodeGen/paren-list-agg-init.cpp b/clang/test/CIR/CodeGen/paren-list-agg-init.cpp
index e240aecaabf516..beda22e79786a4 100644
--- a/clang/test/CIR/CodeGen/paren-list-agg-init.cpp
+++ b/clang/test/CIR/CodeGen/paren-list-agg-init.cpp
@@ -363,7 +363,7 @@ D foo8() {
// CIR: %[[FP_2:.*]] = cir.const #cir.fp<2
// CIR: cir.store align(8) %[[FP_2]], %[[GET_J]] : !cir.double, !cir.ptr<!cir.double>
// CIR: %[[GET_C:.*]] = cir.get_member %[[D_ALLOCA]][3] {name = "c"} : !cir.ptr<![[STRUCT_D]]> -> !cir.ptr<![[STRUCT_A]]>
-// CIR: %[[GET_C_PTR_I8:.*]] = cir.cast bitcast %12 : !cir.ptr<![[STRUCT_A]]> -> !cir.ptr<!u8i>
+// CIR: %[[GET_C_PTR_I8:.*]] = cir.cast bitcast %[[GET_C]] : !cir.ptr<![[STRUCT_A]]> -> !cir.ptr<!u8i>
// CIR: %[[CONST_0:.*]] = cir.const #cir.int<0> : !u8i
// CIR: %[[CONST_16:.*]] = cir.const #cir.int<16> : !u64i
// CIR: %[[GET_C_VOID_PTR:.*]] = cir.cast bitcast %[[GET_C_PTR_I8]] : !cir.ptr<!u8i> -> !cir.ptr<!void>
diff --git a/clang/test/CIR/CodeGen/pointer-to-data-member.cpp b/clang/test/CIR/CodeGen/pointer-to-data-member.cpp
index c0dd470e4a66e5..27b60cc8ec2a03 100644
--- a/clang/test/CIR/CodeGen/pointer-to-data-member.cpp
+++ b/clang/test/CIR/CodeGen/pointer-to-data-member.cpp
@@ -458,7 +458,8 @@ void useHasField() {
// CIR-BEFORE-LABEL: cir.func {{.*}}_Z14changeFieldPtrIXadL_ZN8hasField5fieldEEEEvRS0_
// CIR-BEFORE: %[[ARG:.*]] = cir.alloca "hf" {{.*}} : !cir.ptr<!cir.ptr<!rec_hasField>>
-// CIR-BEFORE: %[[ONE:.*]] = cir.const #cir.int<1>
+// CIR-BEFORE: %{{.+}} = cir.const #cir.int<1> : !s32i
+// CIR-BEFORE: %[[ONE:.*]] = cir.const #cir.int<1> : !u32i
// CIR-BEFORE: %[[LOAD_ARG:.*]] = cir.load %[[ARG]] : !cir.ptr<!cir.ptr<!rec_hasField>>, !cir.ptr<!rec_hasField>
// CIR-BEFORE: %[[GET_DM:.*]] = cir.const #cir.data_member<[0]> : !cir.data_member<!u32i in !rec_hasField>
// CIR-BEFORE: %[[GET_MEM:.*]] = cir.get_runtime_member %[[LOAD_ARG]][%[[GET_DM]] : !cir.data_member<!u32i in !rec_hasField>] : !cir.ptr<!rec_hasField> -> !cir.ptr<!u32i>
@@ -466,7 +467,8 @@ void useHasField() {
// CIR-AFTER-LABEL: cir.func {{.*}}_Z14changeFieldPtrIXadL_ZN8hasField5fieldEEEEvRS0_
// CIR-AFTER: %[[ARG:.*]] = cir.alloca "hf" {{.*}} : !cir.ptr<!cir.ptr<!rec_hasField>>
-// CIR-AFTER: %[[ONE:.*]] = cir.const #cir.int<1>
+// CIR-AFTER: %{{.+}} = cir.const #cir.int<1> : !s32i
+// CIR-AFTER: %[[ONE:.*]] = cir.const #cir.int<1> : !u32i
// CIR-AFTER: %[[LOAD_ARG:.*]] = cir.load %[[ARG]] : !cir.ptr<!cir.ptr<!rec_hasField>>, !cir.ptr<!rec_hasField>
// CIR-AFTER: %[[ZERO:.*]] = cir.const #cir.int<0> : !s64i
// CIR-AFTER: %[[ARG_AS_PTR:.*]] = cir.cast bitcast %[[LOAD_ARG]] : !cir.ptr<!rec_hasField> -> !cir.ptr<!s8i>
diff --git a/clang/test/CIR/CodeGen/union.c b/clang/test/CIR/CodeGen/union.c
index 65937399a1e832..ce4130be956291 100644
--- a/clang/test/CIR/CodeGen/union.c
+++ b/clang/test/CIR/CodeGen/union.c
@@ -118,6 +118,7 @@ void shouldGenerateUnionAccess(union U2 u) {
// CIR-NEXT: %[[CAST:.*]] = cir.cast bitcast %[[COERCE]] : !cir.ptr<!u64i> -> !cir.ptr<!rec_U2>
// CIR-NEXT: %[[U:.*]] = cir.alloca "u" align(8) init : !cir.ptr<!rec_U2>
// CIR-NEXT: cir.copy %[[CAST]] align(8) to %[[U]] align(8) : !cir.ptr<!rec_U2>
+// CIR-NEXT: %{{.+}} = cir.const #cir.int<0> : !s32i
// CIR-NEXT: %[[ZERO:.*]] = cir.const #cir.int<0> : !s8i
// CIR-NEXT: %[[B_PTR:.*]] = cir.get_member %[[U]][0] {name = "b"} : !cir.ptr<!rec_U2> -> !cir.ptr<!s8i>
// CIR-NEXT: cir.store{{.*}} %[[ZERO]], %[[B_PTR]] : !s8i, !cir.ptr<!s8i>
@@ -180,8 +181,10 @@ void f3(union U3 u) {
// CIR-NEXT: %[[CAST:.*]] = cir.cast bitcast %[[COERCE]] : !cir.ptr<!cir.int<u, 40>> -> !cir.ptr<!rec_U3>
// CIR-NEXT: %[[U:.*]] = cir.alloca "u" align(1) init : !cir.ptr<!rec_U3>
// CIR-NEXT: cir.copy %[[CAST]] align(8) to %[[U]] align(1) : !cir.ptr<!rec_U3>
+// CIR-NEXT: %{{.+}} = cir.const #cir.int<0> : !s32i
// CIR-NEXT: %[[ZERO:.*]] = cir.const #cir.int<0> : !s8i
// CIR-NEXT: %[[C_PTR:.*]] = cir.get_member %[[U]][0] {name = "c"} : !cir.ptr<!rec_U3> -> !cir.ptr<!cir.array<!s8i x 5>>
+// CIR-NEXT: %{{.+}} = cir.const #cir.int<2> : !s32i
// CIR-NEXT: %[[IDX:.*]] = cir.const #cir.int<2> : !s64i
// CIR-NEXT: %[[ELEM_PTR:.*]] = cir.get_element inbounds %[[C_PTR]][%[[IDX]] : !s64i] : !cir.ptr<!cir.array<!s8i x 5>> -> !cir.ptr<!s8i>
// CIR-NEXT: cir.store{{.*}} %[[ZERO]], %[[ELEM_PTR]] : !s8i, !cir.ptr<!s8i>
@@ -214,8 +217,10 @@ void f5(union U4 u) {
// CIR-NEXT: %[[CAST:.*]] = cir.cast bitcast %[[COERCE]] : !cir.ptr<!u64i> -> !cir.ptr<!rec_U4>
// CIR-NEXT: %[[U:.*]] = cir.alloca "u" align(4) init : !cir.ptr<!rec_U4>
// CIR-NEXT: cir.copy %[[CAST]] align(8) to %[[U]] align(4) : !cir.ptr<!rec_U4>
+// CIR-NEXT: %{{.+}} = cir.const #cir.int<65> : !s32i
// CIR-NEXT: %[[CHAR_VAL:.*]] = cir.const #cir.int<65> : !s8i
// CIR-NEXT: %[[C_PTR:.*]] = cir.get_member %[[U]][0] {name = "c"} : !cir.ptr<!rec_U4> -> !cir.ptr<!cir.array<!s8i x 5>>
+// CIR-NEXT: %{{.+}} = cir.const #cir.int<4> : !s32i
// CIR-NEXT: %[[IDX:.*]] = cir.const #cir.int<4> : !s64i
// CIR-NEXT: %[[ELEM_PTR:.*]] = cir.get_element inbounds %[[C_PTR]][%[[IDX]] : !s64i] : !cir.ptr<!cir.array<!s8i x 5>> -> !cir.ptr<!s8i>
// CIR-NEXT: cir.store{{.*}} %[[CHAR_VAL]], %[[ELEM_PTR]] : !s8i, !cir.ptr<!s8i>
diff --git a/clang/test/CIR/CodeGen/vla.c b/clang/test/CIR/CodeGen/vla.c
index 81b4cc8827138d..03fec449cc4333 100644
--- a/clang/test/CIR/CodeGen/vla.c
+++ b/clang/test/CIR/CodeGen/vla.c
@@ -445,6 +445,8 @@ double vla_param_2d(int n, double m[n][n], int i, int j) {
// CIR: cir.store{{.*}} %[[M_ARG]], %[[M_ADDR]]
// CIR: cir.store{{.*}} %[[I_ARG]], %[[I_ADDR]]
// CIR: cir.store{{.*}} %[[J_ARG]], %[[J_ADDR]]
+// CIR: %[[N_OUTER:.*]] = cir.load{{.*}} %[[N_ADDR]]
+// CIR: %{{.+}} = cir.cast integral %[[N_OUTER]] : !s32i -> !u64i
// CIR: %[[N:.*]] = cir.load{{.*}} %[[N_ADDR]]
// CIR: %[[VLA_SIZE:.*]] = cir.cast integral %[[N]] : !s32i -> !u64i
// CIR: %[[M:.*]] = cir.load{{.*}} %[[M_ADDR]]
@@ -467,6 +469,8 @@ double vla_param_2d(int n, double m[n][n], int i, int j) {
// LLVM: store ptr %[[M_ARG]], ptr %[[M_ADDR]]
// LLVM: store i32 %[[I_ARG]], ptr %[[I_ADDR]]
// LLVM: store i32 %[[J_ARG]], ptr %[[J_ADDR]]
+// LLVM: %[[N_OUTER:.*]] = load i32, ptr %[[N_ADDR]]
+// LLVM: %{{.+}} = sext i32 %[[N_OUTER]] to i64
// LLVM: %[[N:.*]] = load i32, ptr %[[N_ADDR]]
// LLVM: %[[VLA_SIZE:.*]] = sext i32 %[[N]] to i64
// LLVM: %[[M:.*]] = load ptr, ptr %[[M_ADDR]]
@@ -512,12 +516,14 @@ void complex_vla_cast(int n) {
// CIR: %[[LEN_ADDR:.*]] = cir.alloca "n" {{.*}} init : !cir.ptr<!s32i>
// CIR: %[[SAVED_STACK:.*]] = cir.alloca "saved_stack" {{.*}} : !cir.ptr<!cir.ptr<!u8i>>
// CIR: cir.store %{{.*}}, %[[LEN_ADDR]] : !s32i, !cir.ptr<!s32i>
+// CIR: %[[LEN_UNUSED:.*]] = cir.load {{.*}} %[[LEN_ADDR]] : !cir.ptr<!s32i>, !s32i
+// CIR: %{{.+}} = cir.cast integral %[[LEN_UNUSED]] : !s32i -> !u64i
// CIR: %[[LEN:.*]] = cir.load {{.*}} %[[LEN_ADDR]] : !cir.ptr<!s32i>, !s32i
// CIR: %[[LEN_SIZE_T:.*]] = cir.cast integral %[[LEN]] : !s32i -> !u64i
// CIR: %[[STACK_PTR:.*]] = cir.stacksave : !cir.ptr<!u8i>
// CIR: cir.store {{.*}} %[[STACK_PTR]], %[[SAVED_STACK]] : !cir.ptr<!u8i>, !cir.ptr<!cir.ptr<!u8i>>
// CIR: cir.cleanup.scope {
-// CIR: %[[ArR:.*]] = cir.alloca "arr" {{.*}} size(%3) : !cir.ptr<!cir.complex<!cir.float>>
+// CIR: %[[ArR:.*]] = cir.alloca "arr" {{.*}} size(%[[LEN_SIZE_T]]) : !cir.ptr<!cir.complex<!cir.float>>
// CIR: cir.yield
// CIR: } cleanup normal {
// CIR: %[[STACK_RESTORE_PTR:.*]] = cir.load {{.*}} %[[SAVED_STACK]] : !cir.ptr<!cir.ptr<!u8i>>, !cir.ptr<!u8i>
@@ -529,6 +535,8 @@ void complex_vla_cast(int n) {
// LLVM: %[[LEN_ADDR:.*]] = alloca i32, align 4
// LLVM: %[[SAVED_STACK:.*]] = alloca ptr, align 8
// LLVM: store i32 %{{.*}}, ptr %[[LEN_ADDR]], align 4
+// LLVM: %[[LEN_UNUSED:.*]] = load i32, ptr %[[LEN_ADDR]], align 4
+// LLVM: %{{.+}} = sext i32 %[[LEN_UNUSED]] to i64
// LLVM: %[[LEN:.*]] = load i32, ptr %[[LEN_ADDR]], align 4
// LLVM: %[[LEN_SIZE_T:.*]] = sext i32 %[[LEN]] to i64
// LLVM: %[[STACK_PTR:.*]] = call ptr @llvm.stacksave.p0()
diff --git a/clang/test/CIR/CodeGen/void-value-expr.cpp b/clang/test/CIR/CodeGen/void-value-expr.cpp
index fadbb05464880c..a012def049ecac 100644
--- a/clang/test/CIR/CodeGen/void-value-expr.cpp
+++ b/clang/test/CIR/CodeGen/void-value-expr.cpp
@@ -58,6 +58,8 @@ void ternaryCheap(bool b) {
// CIR: %[[ARG:.*]] = cir.alloca {{.*}} : !cir.ptr<!cir.bool>
// CIR: cir.load{{.*}}%[[ARG]] : !cir.ptr<!cir.bool>, !cir.bool
// CIR-NEXT: cir.load{{.*}}%[[ARG]] : !cir.ptr<!cir.bool>, !cir.bool
+// CIR-NEXT: cir.const #cir.int<0> : !s32i
+// CIR-NEXT: cir.const #cir.int<1> : !s32i
// CIR-NEXT: cir.load{{.*}}%[[ARG]] : !cir.ptr<!cir.bool>, !cir.bool
// CIR-NEXT: cir.return
// LLVM-LABEL: define{{.*}} void @_Z12ternaryCheapb
diff --git a/clang/test/CIR/CodeGenCXX/new-array-init.cpp b/clang/test/CIR/CodeGenCXX/new-array-init.cpp
index 183c9aac2a3dbf..3aff6ec9ff246a 100644
--- a/clang/test/CIR/CodeGenCXX/new-array-init.cpp
+++ b/clang/test/CIR/CodeGenCXX/new-array-init.cpp
@@ -388,6 +388,7 @@ void string_nonconst_paren_extra_paren(int n) {
// CIR-LABEL: cir.func {{.*}}@_Z12string_exactv()
// LLVM-LABEL: define{{.*}} void @_Z12string_exactv
void string_exact() {
+ // CIR: %{{.+}} = cir.const #cir.int<4> : !u64i
// CIR: %[[SIZE:.*]] = cir.const #cir.int<4> : !u64i
// CIR: %[[ALLOC:.*]] = cir.call @_Znam(%[[SIZE]]) {allocsize = array<i32: 0>, builtin, memory_effects = #cir.memory_effects<other = none, arg_mem = none, inaccessible_mem = readwrite, errno_mem = write, target_mem0 = none, target_mem1 = none>} : (!u64i {llvm.noundef}) -> (!cir.ptr<!void> {llvm.noalias, llvm.nonnull, llvm.noundef})
// CIR: %[[ALLOC_CAST:.*]] = cir.cast bitcast %[[ALLOC]] : !cir.ptr<!void> -> !cir.ptr<!s8i>
@@ -404,6 +405,7 @@ void string_exact() {
// CIR-LABEL: cir.func {{.*}}@_Z18string_exact_parenv()
// LLVM-LABEL: define{{.*}} void @_Z18string_exact_parenv
void string_exact_paren() {
+ // CIR: %{{.+}} = cir.const #cir.int<4> : !u64i
// CIR: %[[SIZE:.*]] = cir.const #cir.int<4> : !u64i
// CIR: %[[ALLOC:.*]] = cir.call @_Znam(%[[SIZE]]) {allocsize = array<i32: 0>, builtin, memory_effects = #cir.memory_effects<other = none, arg_mem = none, inaccessible_mem = readwrite, errno_mem = write, target_mem0 = none, target_mem1 = none>} : (!u64i {llvm.noundef}) -> (!cir.ptr<!void> {llvm.noalias, llvm.nonnull, llvm.noundef})
// CIR: %[[ALLOC_CAST:.*]] = cir.cast bitcast %[[ALLOC]] : !cir.ptr<!void> -> !cir.ptr<!s8i>
@@ -420,6 +422,7 @@ void string_exact_paren() {
// CIR-LABEL: cir.func {{.*}}@_Z28string_exact_paren_extensionv()
// LLVM-LABEL: define{{.*}} void @_Z28string_exact_paren_extensionv
void string_exact_paren_extension() {
+ // CIR: %{{.+}} = cir.const #cir.int<4> : !u64i
// CIR: %[[SIZE:.*]] = cir.const #cir.int<4> : !u64i
// CIR: %[[ALLOC:.*]] = cir.call @_Znam(%[[SIZE]]) {allocsize = array<i32: 0>, builtin, memory_effects = #cir.memory_effects<other = none, arg_mem = none, inaccessible_mem = readwrite, errno_mem = write, target_mem0 = none, target_mem1 = none>} : (!u64i {llvm.noundef}) -> (!cir.ptr<!void> {llvm.noalias, llvm.nonnull, llvm.noundef})
// CIR: %[[ALLOC_CAST:.*]] = cir.cast bitcast %[[ALLOC]] : !cir.ptr<!void> -> !cir.ptr<!s8i>
@@ -436,6 +439,7 @@ void string_exact_paren_extension() {
// CIR-LABEL: cir.func {{.*}}@_Z17string_sufficientv()
// LLVM-LABEL: define{{.*}} void @_Z17string_sufficientv
void string_sufficient() {
+ // CIR: %{{.+}} = cir.const #cir.int<15> : !u64i
// CIR: %[[SIZE:.*]] = cir.const #cir.int<15> : !u64i
// CIR: %[[ALLOC:.*]] = cir.call @_Znam(%[[SIZE]]) {allocsize = array<i32: 0>, builtin, memory_effects = #cir.memory_effects<other = none, arg_mem = none, inaccessible_mem = readwrite, errno_mem = write, target_mem0 = none, target_mem1 = none>} : (!u64i {llvm.noundef}) -> (!cir.ptr<!void> {llvm.noalias, llvm.nonnull, llvm.noundef})
// CIR: %[[ALLOC_CAST:.*]] = cir.cast bitcast %[[ALLOC]] : !cir.ptr<!void> -> !cir.ptr<!s8i>
@@ -452,6 +456,7 @@ void string_sufficient() {
// CIR-LABEL: cir.func {{.*}}@_Z23string_sufficient_parenv()
// LLVM-LABEL: define{{.*}} void @_Z23string_sufficient_parenv
void string_sufficient_paren() {
+ // CIR: %{{.+}} = cir.const #cir.int<15> : !u64i
// CIR: %[[SIZE:.*]] = cir.const #cir.int<15> : !u64i
// CIR: %[[ALLOC:.*]] = cir.call @_Znam(%[[SIZE]]) {allocsize = array<i32: 0>, builtin, memory_effects = #cir.memory_effects<other = none, arg_mem = none, inaccessible_mem = readwrite, errno_mem = write, target_mem0 = none, target_mem1 = none>} : (!u64i {llvm.noundef}) -> (!cir.ptr<!void> {llvm.noalias, llvm.nonnull, llvm.noundef})
// CIR: %[[ALLOC_CAST:.*]] = cir.cast bitcast %[[ALLOC]] : !cir.ptr<!void> -> !cir.ptr<!s8i>
@@ -637,6 +642,7 @@ void unknown_bound() {
// CIR-LABEL: cir.func {{.*}}@_Z20unknown_bound_stringv()
// LLVM-LABEL: define{{.*}} void @_Z20unknown_bound_stringv
void unknown_bound_string() {
+ // CIR: %{{.+}} = cir.const #cir.int<6> : !u64i
// CIR: %[[SIZE:.*]] = cir.const #cir.int<6> : !u64i
// CIR: %[[ALLOC:.*]] = cir.call @_Znam(%[[SIZE]]) {allocsize = array<i32: 0>, builtin, memory_effects = #cir.memory_effects<other = none, arg_mem = none, inaccessible_mem = readwrite, errno_mem = write, target_mem0 = none, target_mem1 = none>} : (!u64i {llvm.noundef}) -> (!cir.ptr<!void> {llvm.noalias, llvm.nonnull, llvm.noundef})
// CIR: %[[ALLOC_CHAR:.*]] = cir.cast bitcast %[[ALLOC]] : !cir.ptr<!void> -> !cir.ptr<!s8i>
diff --git a/clang/test/CIR/CodeGenOpenACC/combined-copy.c b/clang/test/CIR/CodeGenOpenACC/combined-copy.c
index b910743f16e203..40ac1bdb6add89 100644
--- a/clang/test/CIR/CodeGenOpenACC/combined-copy.c
+++ b/clang/test/CIR/CodeGenOpenACC/combined-copy.c
@@ -1087,6 +1087,7 @@ void copy_member_of_array_element_member() {
#pragma acc parallel loop copy(outer.inner[2].b)
for(int i = 0; i < 5; ++i);
// CHECK-NEXT: %[[GETINNER:.*]] = cir.get_member %[[OUTER]][0] {name = "inner"} : !cir.ptr<!rec_OuterTy> -> !cir.ptr<!cir.array<!rec_InnerTy x 4>>
+ // CHECK-NEXT: %{{.+}} = cir.const #cir.int<2> : !s32i
// CHECK-NEXT: %[[TWO:.*]] = cir.const #cir.int<2> : !s64i
// CHECK-NEXT: %[[GET_ELT:.*]] = cir.get_element inbounds %[[GETINNER]][%[[TWO]] : !s64i] : !cir.ptr<!cir.array<!rec_InnerTy x 4>> -> !cir.ptr<!rec_InnerTy>
// CHECK-NEXT: %[[GETB:.*]] = cir.get_member %[[GET_ELT]][1] {name = "b"} : !cir.ptr<!rec_InnerTy> -> !cir.ptr<!s32i>
diff --git a/clang/test/CIR/CodeGenSYCL/global-address-space.cpp b/clang/test/CIR/CodeGenSYCL/global-address-space.cpp
index f2220474615f98..e7f4419609ace0 100644
--- a/clang/test/CIR/CodeGenSYCL/global-address-space.cpp
+++ b/clang/test/CIR/CodeGenSYCL/global-address-space.cpp
@@ -55,6 +55,7 @@ int i;
// CIR: cir.return
// LLVM-LABEL: define {{.*}} ptr addrspace(2) @_Z15addr_explicit_cv()
+// LLVM-CIR: %{{.+}} = addrspacecast ptr %{{.+}} to ptr addrspace(4)
// LLVM-NOT: addrspacecast
// LLVM: ret ptr addrspace(2)
diff --git a/clang/test/CIR/Transforms/canonicalize-dead-code.cir b/clang/test/CIR/Transforms/canonicalize-dead-code.cir
new file mode 100644
index 00000000000000..4e2ce38dd19d4e
--- /dev/null
+++ b/clang/test/CIR/Transforms/canonicalize-dead-code.cir
@@ -0,0 +1,62 @@
+// RUN: cir-opt %s -cir-canonicalize=test-convergence -o - | FileCheck %s
+
+!s32i = !cir.int<s, 32>
+
+module {
+ // Operations that are dead before rewriting are kept, while folding still
+ // replaces the uses of a foldable operation. The constant keeps a use in the
+ // dead addition, so it stays.
+ // CHECK-LABEL: cir.func @dead_ops
+ cir.func @dead_ops(%arg0: !cir.ptr<!s32i>) -> !s32i {
+ // CHECK-NEXT: cir.alloca "unused"
+ %0 = cir.alloca "unused" align(4) : !cir.ptr<!s32i>
+ // CHECK-NEXT: %[[LOAD:.*]] = cir.load %arg0
+ %1 = cir.load %arg0 : !cir.ptr<!s32i>, !s32i
+ // CHECK-NEXT: %[[ONE:.*]] = cir.const #cir.int<1> : !s32i
+ %2 = cir.const #cir.int<1> : !s32i
+ // CHECK-NEXT: cir.add %[[LOAD]], %[[ONE]] : !s32i
+ %3 = cir.add %1, %2 : !s32i
+ // CHECK-NEXT: %[[NEG:.*]] = cir.const #cir.int<-1> : !s32i
+ %4 = cir.minus %2 : !s32i
+ // CHECK-NEXT: cir.return %[[NEG]] : !s32i
+ cir.return %4 : !s32i
+ }
+
+ // An operation that a fold leaves unused is erased.
+ // CHECK-LABEL: cir.func @fold_leftover
+ cir.func @fold_leftover() -> !s32i {
+ %0 = cir.const #cir.int<1> : !s32i
+ // CHECK-NEXT: %[[NEG:.*]] = cir.const #cir.int<-1> : !s32i
+ %1 = cir.minus %0 : !s32i
+ // CHECK-NEXT: cir.return %[[NEG]] : !s32i
+ cir.return %1 : !s32i
+ }
+
+ // An unused operation still folds. Rewriting creates the constant that
+ // replaces it, so that constant is erased once it is dead, and so is the
+ // operand that only the folded operation used.
+ // CHECK-LABEL: cir.func @unused_fold
+ cir.func @unused_fold() {
+ %0 = cir.const #cir.int<1> : !s32i
+ %1 = cir.minus %0 : !s32i
+ // CHECK-NEXT: cir.return
+ cir.return
+ }
+
+ // A block that starts with a label is reached through cir.goto only, which
+ // is not a CFG edge, so it must not be erased as unreachable.
+ // CHECK-LABEL: cir.func @goto_target
+ cir.func @goto_target(%arg0: !cir.bool) {
+ cir.brcond %arg0 ^bb1, ^bb2
+ ^bb1:
+ // CHECK: cir.goto "L"
+ cir.goto "L"
+ ^bb2:
+ cir.return
+ ^bb3:
+ // CHECK: cir.label "L"
+ cir.label "L"
+ // CHECK-NEXT: cir.return
+ cir.return
+ }
+}
diff --git a/clang/test/CIR/Transforms/canonicalize-label.cir b/clang/test/CIR/Transforms/canonicalize-label.cir
index a727b8a1088a61..71be54325e5387 100644
--- a/clang/test/CIR/Transforms/canonicalize-label.cir
+++ b/clang/test/CIR/Transforms/canonicalize-label.cir
@@ -50,12 +50,13 @@ module {
// A goto to a label inside a scope enters the outer block at that scope.
// The canonicalizer simplifies the scope and the operations after it, but
- // not the constant before it, which no jump reaches. It keeps the label at
- // the start of its block.
+ // not the empty scope before it, which no jump reaches. It keeps the label
+ // at the start of its block.
cir.func @label_in_scope(%arg0: !s32i) -> !s32i {
cir.goto "in"
^bb1:
- %0 = cir.const #cir.int<7> : !s32i
+ cir.scope {
+ }
cir.scope {
cir.br ^bb2
^bb2:
@@ -71,7 +72,8 @@ module {
// CHECK: cir.func{{.*}} @label_in_scope(%[[ARG0:.*]]: !s32i) -> !s32i {
// CHECK-NEXT: cir.goto "in"
// CHECK-NEXT: ^bb1:
- // CHECK-NEXT: cir.const #cir.int<7> : !s32i
+ // CHECK-NEXT: cir.scope {
+ // CHECK-NEXT: }
// CHECK-NEXT: cir.scope {
// CHECK-NEXT: cir.br ^[[LABEL:bb[0-9]+]]
// CHECK-NEXT: ^[[LABEL]]:
@@ -82,15 +84,17 @@ module {
// CHECK-NEXT: }
// Every block on the way to a nested label is entered at the operation that
- // holds the label. The constants before those operations stay.
+ // holds the label. The empty scopes before those operations stay.
cir.func @nested_holders() {
cir.goto "in"
^bb1:
- %0 = cir.const #cir.int<1> : !s32i
+ cir.scope {
+ }
cir.scope {
cir.goto "in"
^bb2:
- %1 = cir.const #cir.int<2> : !s32i
+ cir.scope {
+ }
cir.scope {
cir.br ^bb3
^bb3:
@@ -110,11 +114,13 @@ module {
// CHECK: cir.func{{.*}} @nested_holders() {
// CHECK-NEXT: cir.goto "in"
// CHECK-NEXT: ^bb1:
- // CHECK-NEXT: cir.const #cir.int<1> : !s32i
+ // CHECK-NEXT: cir.scope {
+ // CHECK-NEXT: }
// CHECK-NEXT: cir.scope {
// CHECK-NEXT: cir.goto "in"
// CHECK-NEXT: ^bb{{[0-9]+}}:
- // CHECK-NEXT: cir.const #cir.int<2> : !s32i
+ // CHECK-NEXT: cir.scope {
+ // CHECK-NEXT: }
// CHECK-NEXT: cir.scope {
// CHECK-NEXT: cir.br ^[[LABEL:bb[0-9]+]]
// CHECK-NEXT: ^[[LABEL]]:
@@ -131,7 +137,8 @@ module {
cir.func @two_holders() {
cir.goto "a"
^bb1:
- %0 = cir.const #cir.int<1> : !s32i
+ cir.scope {
+ }
cir.scope {
cir.br ^bb2
^bb2:
@@ -151,7 +158,8 @@ module {
// CHECK: cir.func{{.*}} @two_holders() {
// CHECK-NEXT: cir.goto "a"
// CHECK-NEXT: ^bb1:
- // CHECK-NEXT: cir.const #cir.int<1> : !s32i
+ // CHECK-NEXT: cir.scope {
+ // CHECK-NEXT: }
// CHECK-NEXT: cir.scope {
// CHECK-NEXT: cir.br ^bb1
// CHECK-NEXT: ^bb1:
@@ -171,7 +179,8 @@ module {
cir.func @holder_successors(%arg0: !cir.bool) {
cir.goto "in"
^bb1:
- %0 = cir.const #cir.int<1> : !s32i
+ cir.scope {
+ }
cir.scope {
cir.br ^bb2
^bb2:
@@ -189,7 +198,8 @@ module {
// CHECK: cir.func{{.*}} @holder_successors(%[[ARG0:.*]]: !cir.bool) {
// CHECK-NEXT: cir.goto "in"
// CHECK-NEXT: ^bb1:
- // CHECK-NEXT: cir.const #cir.int<1> : !s32i
+ // CHECK-NEXT: cir.scope {
+ // CHECK-NEXT: }
// CHECK-NEXT: cir.scope {
// CHECK-NEXT: cir.br ^bb1
// CHECK-NEXT: ^bb1:
@@ -323,15 +333,16 @@ module {
// CHECK-NEXT: }
// The block that holds a cir.case after a cir.break has no CFG predecessor
- // either, but the cir.switch jumps to the case. The constant before the case
- // stays.
+ // either, but the cir.switch jumps to the case. The empty scope before the
+ // case stays.
cir.func @case_after_break(%arg0: !s32i) {
cir.switch(%arg0 : !s32i) {
cir.case(equal, [#cir.int<1> : !s32i]) {
cir.scope {
cir.break
^bb1:
- %0 = cir.const #cir.int<1> : !s32i
+ cir.scope {
+ }
cir.case(equal, [#cir.int<2> : !s32i]) {
cir.scope {
}
@@ -353,7 +364,8 @@ module {
// CHECK-NEXT: cir.scope {
// CHECK-NEXT: cir.break
// CHECK-NEXT: ^bb1:
- // CHECK-NEXT: cir.const #cir.int<1> : !s32i
+ // CHECK-NEXT: cir.scope {
+ // CHECK-NEXT: }
// CHECK-NEXT: cir.case(equal, [#cir.int<2> : !s32i]) {
// CHECK-NEXT: cir.yield
// CHECK-NEXT: }
@@ -373,7 +385,8 @@ module {
cir.case(equal, [#cir.int<1> : !s32i]) {
cir.break
^bb1:
- %0 = cir.const #cir.int<1> : !s32i
+ cir.scope {
+ }
cir.scope {
cir.case(equal, [#cir.int<2> : !s32i]) {
cir.scope {
@@ -393,7 +406,8 @@ module {
// CHECK-NEXT: cir.case(equal, [#cir.int<1> : !s32i]) {
// CHECK-NEXT: cir.break
// CHECK-NEXT: ^bb1:
- // CHECK-NEXT: cir.const #cir.int<1> : !s32i
+ // CHECK-NEXT: cir.scope {
+ // CHECK-NEXT: }
// CHECK-NEXT: cir.scope {
// CHECK-NEXT: cir.case(equal, [#cir.int<2> : !s32i]) {
// CHECK-NEXT: cir.yield
@@ -496,13 +510,14 @@ module {
// CHECK-NEXT: }
// The verifier checks no dominance in a block that only a jump enters, so
- // it can hold SSA cycles. Folding them must stop. The unused constant goes
- // away, which shows that the block is visited.
+ // it can hold SSA cycles. Folding them must stop. The empty scope goes away,
+ // which shows that the block is visited.
cir.func @cycles_after_label() {
cir.goto "out"
^bb1:
cir.label "out"
- %c = cir.const #cir.int<0> : !s32i
+ cir.scope {
+ }
%0 = cir.cast int_to_bool %1 : !s32i -> !cir.bool
%1 = cir.cast bool_to_int %0 : !cir.bool -> !s32i
%2 = cir.not %3 : !cir.bool
diff --git a/clang/test/CIR/Transforms/canonicalize.cir b/clang/test/CIR/Transforms/canonicalize.cir
index 5743e9aaac6eb8..9d7855cf46748b 100644
--- a/clang/test/CIR/Transforms/canonicalize.cir
+++ b/clang/test/CIR/Transforms/canonicalize.cir
@@ -371,10 +371,9 @@ module {
// CHECK-NEXT: cir.return %[[P]] : !s32i
// CHECK-NEXT: }
- // The driver visits a user before the operations it uses. The outer cir.not
- // folds to the constant, and the inner one then dies unused. Visited the
- // other way round, each would fold to a new constant, and the store would
- // keep the original one too.
+ // The driver visits the operations top-down. The inner cir.not folds to a
+ // new constant first and the outer one then folds to another, while the
+ // store keeps the original constant.
cir.func @not_not_order(%arg0: !cir.ptr<!cir.bool>) -> !cir.bool {
%0 = cir.const #true
%1 = cir.not %0 : !cir.bool
@@ -385,7 +384,8 @@ module {
// CHECK: cir.func{{.*}} @not_not_order(%[[ARG0:.*]]: !cir.ptr<!cir.bool>) -> !cir.bool
// CHECK-NEXT: %[[TRUE:.*]] = cir.const #true
// CHECK-NEXT: cir.store %[[TRUE]], %[[ARG0]] : !cir.bool, !cir.ptr<!cir.bool>
- // CHECK-NEXT: cir.return %[[TRUE]] : !cir.bool
+ // CHECK-NEXT: %[[FOLDED:.*]] = cir.const #true
+ // CHECK-NEXT: cir.return %[[FOLDED]] : !cir.bool
// CHECK-NEXT: }
// Folding the cast in the scope revisits the scope, which then folds away.
@@ -400,26 +400,29 @@ module {
// CHECK-NEXT: cir.return %[[ARG0]] : !s32i
// CHECK-NEXT: }
- // Merging a block into its predecessor revisits the operations it moves, so
- // the unused load goes away.
+ // The load is unused before rewriting, so it stays when merging its block
+ // into the predecessor moves it.
cir.func @merged_dead_load(%arg0: !cir.ptr<!s32i>) {
cir.br ^bb1
^bb1:
%0 = cir.load %arg0 : !cir.ptr<!s32i>, !s32i
cir.return
}
- // CHECK: cir.func{{.*}} @merged_dead_load(%{{.*}}: !cir.ptr<!s32i>)
+ // CHECK: cir.func{{.*}} @merged_dead_load(%[[ARG0:.*]]: !cir.ptr<!s32i>)
+ // CHECK-NEXT: cir.load %[[ARG0]] : !cir.ptr<!s32i>, !s32i
// CHECK-NEXT: cir.return
// CHECK-NEXT: }
- // Erasing the unused cast revisits the load that defines its operand, which
- // is then dead too.
+ // The cast is unused before rewriting, so it stays, and so does the load
+ // that defines its operand.
cir.func @dead_operand_definer(%arg0: !cir.ptr<!s32i>) {
%0 = cir.load %arg0 : !cir.ptr<!s32i>, !s32i
%1 = cir.cast int_to_bool %0 : !s32i -> !cir.bool
cir.return
}
- // CHECK: cir.func{{.*}} @dead_operand_definer(%{{.*}}: !cir.ptr<!s32i>)
+ // CHECK: cir.func{{.*}} @dead_operand_definer(%[[ARG0:.*]]: !cir.ptr<!s32i>)
+ // CHECK-NEXT: %[[LOAD:.*]] = cir.load %[[ARG0]] : !cir.ptr<!s32i>, !s32i
+ // CHECK-NEXT: cir.cast int_to_bool %[[LOAD]] : !s32i -> !cir.bool
// CHECK-NEXT: cir.return
// CHECK-NEXT: }
More information about the llvm-branch-commits
mailing list