// Copyright 2024 The Dawn & Tint Authors
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this
//    list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice,
//    this list of conditions and the following disclaimer in the documentation
//    and/or other materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its
//    contributors may be used to endorse or promote products derived from
//    this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#include "src/tint/lang/msl/writer/raise/unary_polyfill.h"

#include <utility>

#include "src/tint/lang/core/ir/builder.h"
#include "src/tint/lang/core/ir/validator.h"

using namespace tint::core::fluent_types;  // NOLINT

namespace tint::msl::writer::raise {
namespace {

/// PIMPL state for the transform.
struct State {
    /// The IR module.
    core::ir::Module& ir;

    /// The IR builder.
    core::ir::Builder b{ir};

    /// The type manager.
    core::type::Manager& ty{ir.Types()};

    /// Process the module.
    void Process() {
        // Find the unary operators that need replacing.
        Vector<core::ir::Unary*, 4> signed_int_negate_worklist;
        for (auto* inst : ir.Instructions()) {
            if (auto* unary = inst->As<core::ir::Unary>()) {
                auto op = unary->Op();
                auto* type = unary->Val()->Type();
                if (op == core::UnaryOp::kNegation && type->IsSignedIntegerScalarOrVector()) {
                    signed_int_negate_worklist.Push(unary);
                }
            }
        }

        // Replace the instructions that we found.
        for (auto* signed_int_negate : signed_int_negate_worklist) {
            SignedIntegerNegation(signed_int_negate);
        }
    }

    /// Replace a signed integer negation to avoid undefined behavior.
    /// @param unary the unary instruction
    void SignedIntegerNegation(core::ir::Unary* unary) {
        // Replace `-x` with `as_type<int>((~as_type<uint>(x)) + 1)`.
        auto* signed_type = unary->Result(0)->Type();
        auto* unsigned_type = ty.MatchWidth(ty.u32(), signed_type);
        b.InsertBefore(unary, [&] {
            auto* unsigned_value = b.Bitcast(unsigned_type, unary->Val());
            auto* complement = b.Complement(unsigned_type, unsigned_value);
            auto* plus_one = b.Add(unsigned_type, complement, b.MatchWidth(u32(1), unsigned_type));
            auto* result = b.Bitcast(signed_type, plus_one);
            unary->Result(0)->ReplaceAllUsesWith(result->Result(0));
        });
        unary->Destroy();
    }
};

}  // namespace

Result<SuccessType> UnaryPolyfill(core::ir::Module& ir) {
    auto result = ValidateAndDumpIfNeeded(ir, "UnaryPolyfill transform",
                                          core::ir::Capabilities{
                                              core::ir::Capability::kAllowPointersInStructures,
                                          });
    if (result != Success) {
        return result.Failure();
    }

    State{ir}.Process();

    return Success;
}

}  // namespace tint::msl::writer::raise
