================
@@ -135,6 +135,55 @@ mlir::Location CIRGenFunction::getLoc(mlir::Location lhs, 
mlir::Location rhs) {
   return mlir::FusedLoc::get(locs, metadata, &getMLIRContext());
 }
 
+bool CIRGenFunction::ContainsLabel(const Stmt *s, bool ignoreCaseStmts) {
+  // Null statement, not a label!
+  if (!s)
+    return false;
+
+  // If this is a label, we have to emit the code, consider something like:
+  // if (0) {  ...  foo:  bar(); }  goto foo;
+  //
+  // TODO: If anyone cared, we could track __label__'s, since we know that you
+  // can't jump to one from outside their declared region.
+  if (isa<LabelStmt>(s))
+    return true;
+
+  // If this is a case/default statement, and we haven't seen a switch, we
+  // have to emit the code.
+  if (isa<SwitchCase>(s) && !ignoreCaseStmts)
+    return true;
+
+  // If this is a switch statement, we want to ignore cases below it.
+  if (isa<SwitchStmt>(s))
----------------
andykaylor wrote:

I dug into this a bit more and I think the code here is correct. This function 
is calling itself recursively to walk through all the sub-statements in the 
original statement. If we hit a `case` statement without having seen a `switch` 
statement, we treat the `case` statement as a label (on line 153 above). 
However, if we have seen a `switch` statement and we're recursing into the 
sub-statements of that `switch` statement, then `case` statements are expected 
and we don't want to treat them as labels.

This is basically happening in the context of determining whether or not we can 
constant-fold the statement. I put together an experiment to show how the 
classic codegen handles labels in a switch statement.

https://godbolt.org/z/dY14fWezT

If there is a label in the middle of a `switch` statement, an `if` statement 
that uses that switch (`foo()`) won't be constant folded, but the same `switch` 
without labels (`bar()`) is folded.

Note that the clang IR incubator doesn't fold either case, but it does if the 
`if` is part of a `constexpr`.

https://godbolt.org/z/TqWf69joe

https://github.com/llvm/llvm-project/pull/134333
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to