================
@@ -2784,6 +2786,59 @@ void LoweringPreparePass::lowerStdOp(cir::StdOpInterface
typedOp) {
op->erase();
}
+/// If `cond` is the result of a chain of cir.not ops, sets it to the input of
+/// the innermost cir.not and erases the cir.not ops left without uses. An
+/// odd number of cir.not ops inverts the condition, so `trueRegion` and
+/// `falseRegion` are swapped. If one region is empty, the other is empty
+/// after a swap, and the caller restores any region its op requires. Returns
+/// true if the regions were swapped.
+static bool stripNotsFromCondition(mlir::OpOperand &cond,
+ mlir::Region &trueRegion,
+ mlir::Region &falseRegion) {
+ llvm::SmallVector<cir::NotOp> notOps;
+ mlir::Value value = cond.get();
+ while (auto notOp = value.getDefiningOp<cir::NotOp>()) {
+ notOps.push_back(notOp);
+ value = notOp.getInput();
+ }
+ if (notOps.empty())
+ return false;
+
+ cond.set(value);
+ bool swapped = notOps.size() % 2 == 1;
+ if (swapped) {
+ mlir::Region trueBody;
+ trueBody.takeBody(trueRegion);
+ trueRegion.takeBody(falseRegion);
+ falseRegion.takeBody(trueBody);
+ }
+ for (cir::NotOp notOp : notOps) {
+ if (!notOp->use_empty())
+ break;
+ notOp.erase();
+ }
+ return swapped;
+}
+
+void LoweringPreparePass::lowerIfOp(cir::IfOp op) {
+ bool hadElse = !op.getElseRegion().empty();
+ bool swapped = stripNotsFromCondition(op.getConditionMutable(),
+ op.getThenRegion(),
op.getElseRegion());
+ if (!swapped || hadElse)
----------------
adams381 wrote:
It isn't a bug. It swapped in both cases, before the early return, and the
three `takeBody` calls are the swap. `hadElse` only skipped a fix-up: with no
else, the swap left the then region empty, and FlattenCFG needs a then block,
so that path added a yield-only one. `lowerIfOp` now gives an empty else that
block first and then just swaps.
https://github.com/llvm/llvm-project/pull/233918
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits