Skip to content

Missed optimization: dead code is not eliminated based on possible floating-point values at any optimization level. #82381

Open
@Archaic-Dreams

Description

@Archaic-Dreams

https://godbolt.org/z/6sWTPvne9

#include <algorithm>
#include <stdexcept>

class TestClass
{
public:
    void SetValue(float value);

private:
    float m_Value;
};

void TestClass::SetValue(float value)
{
    if (value >= 0.0f && value <= 100.0f) {
        m_Value = value;
    }
    else {
        throw std::out_of_range("Value must be [0, 100].");
    }
}

void TestFunc(TestClass& t, float value)
{
    value = std::clamp(value, 30.0f, 50.0f);
    // When TestClass::SetValue is inlined, the exception throwing code is not eliminated.
    // Given that at this point we can prove that 'value' lies in the range [30.0f, 50.0f] well within the range required by the setter function, we can rid the not taken paths of code.
    t.SetValue(value);
}

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions