/**
 * Copyright (C) 2025 yuygfgg
 * 
 * This file is part of Vapoursynth-llvmexpr.
 * 
 * Vapoursynth-llvmexpr is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Vapoursynth-llvmexpr is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Vapoursynth-llvmexpr.  If not, see <https://www.gnu.org/licenses/>.
 */

#ifndef LLVMEXPR_ANALYSIS_STATIC_ARRAY_OPT_PASS_HPP
#define LLVMEXPR_ANALYSIS_STATIC_ARRAY_OPT_PASS_HPP

#include "../framework/Pass.hpp"

namespace analysis {

/**
    Converts dynamic array allocations to static allocations when possible.
    This optimization transforms ARRAY_ALLOC_DYN to ARRAY_ALLOC_STATIC when:
    1. Array size is known at compile time (via constant propagation)
    2. Array size is <= threshold (1024 elements)
    3. Array is allocated only once in the entire expression
    
    The transformation involves:
    - Changing token type from ARRAY_ALLOC_DYN to ARRAY_ALLOC_STATIC
    - Setting the static_size in the payload
    - Inserting a DROP instruction to consume the size value from stack
    
    Depends on: ConstPropPass, BlockAnalysisPass
 */
class StaticArrayOptPass : public TransformationPass {
  public:
    [[nodiscard]] const char* getName() const override {
        return "Static Array Optimization Pass";
    }

    PreservedAnalyses run(std::vector<Token>& tokens,
                          AnalysisManager& am) override;
};

} // namespace analysis

#endif // LLVMEXPR_ANALYSIS_STATIC_ARRAY_OPT_PASS_HPP
