piecewise_fold already implements a quick exit -- is there something wrong with it that you added this?

```
    if not isinstance(expr, Basic) or not expr.has(Piecewise):
        return expr
```