<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/104230>104230</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            `if consteval` within function template returns wrong result for `else` part
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          malchera
      </td>
    </tr>
</table>

<pre>
    Consider the following, contrived example:
```c++
#include <cassert>
#include <iostream>

consteval auto increment(int n) { return n + 1; }
auto decrement(int n) { return n - 1; }

template <typename T>
constexpr auto ct_increment(T x) {
    if consteval {
        return increment(x);
    } else {
        return decrement(x);
 }
}

int main() {
    // a is const(expr) -> should be incremented
    constexpr auto a = ct_increment(0);
    std::cout << "a = " << a << std::endl;
    static_assert(a == 1);

    // b is non-const -> should be decremented
    auto b = ct_increment(0);
 std::cout << "b = " << b << std::endl;
    assert(b == -1);
}
```
The initialization of variable `b` is not a manifestly constant-evaluated context and therefore `ct_increment` should run into the `else` branch, which should decrement `b`. However, `b` is incremented, which is unexpected.
The expected behavior can be observed when compiling with non-clang (in [this](https://godbolt.org/z/zKa9vodjj) godbolt example I've compiled with gcc, EDG and icx (the latter is LLVM-based and exhibits the same behavior).

I noticed that this bug seems to be related to the function template. If I convert it to a non-template function, correct results are compiled (see also the `#if` in line 6 of the godbolt link).
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyElc9z6jgMx_8ac9HABIcf4ZBDKWW3s29vnb2-cRyFuOvYjK0AfX_9jhICKd037dC0RJasz1eyrGI0B4eYi-VWLHcT1VLtQ94oq2sMalL48iN_9i6aEgNQjVB5a_3ZuIOQz6C9o2BOWAJeVHO0KNInkexE8iRWSf_RQm7507-VqXHatiWCSJ-1ihEDifTl_6zGRwqomru5e2rvIuFJWVAteTBOB2zQkZCZcQROyA2I9RYCUhscOBByC3ORbkGsd32I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