[clang] [CIR] Implement simple folding for unary operations (PR #174882)

Andy Kaylor via cfe-commits cfe-commits at lists.llvm.org
Thu Jan 8 09:35:40 PST 2026


================
@@ -2479,6 +2479,39 @@ OpFoldResult cir::UnaryOp::fold(FoldAdaptor adaptor) {
       if (isBoolNot(previous))
         return previous.getInput();
 
+  // Fold constant unary operations.
+  if (auto srcConst = getInput().getDefiningOp<cir::ConstantOp>()) {
----------------
andykaylor wrote:

I'll try a rewrite using the adaptor. That should be quick to implement, and it seems to be a more standard way of doing things. I've implemented the next step (folding during CG) in my sandbox, and I can verify that doing it that way does create the constants in the order suggested above and leaves a dead constant behind. The dead constant seems to be cleaned up if I wait until `CIRCanonicalize`, but for some reason `createOrFold` leaves it behind.

https://github.com/llvm/llvm-project/pull/174882


More information about the cfe-commits mailing list