#include "lib/Transforms/Halo/RegionBranchOpLevelInvariance.h"

#include <utility>

#include "lib/Analysis/LevelAnalysis/LevelAnalysis.h"
#include "lib/Analysis/SecretnessAnalysis/SecretnessAnalysis.h"
#include "lib/Transforms/Halo/Patterns.h"
#include "mlir/include/mlir/Analysis/DataFlow/Utils.h"     // from @llvm-project
#include "mlir/include/mlir/Analysis/DataFlowFramework.h"  // from @llvm-project
#include "mlir/include/mlir/IR/MLIRContext.h"              // from @llvm-project
#include "mlir/include/mlir/IR/PatternMatch.h"             // from @llvm-project
#include "mlir/include/mlir/IR/SymbolTable.h"              // from @llvm-project
#include "mlir/include/mlir/Support/LLVM.h"                // from @llvm-project
#include "mlir/include/mlir/Transforms/GreedyPatternRewriteDriver.h"  // from @llvm-project

namespace mlir {
namespace heir {

#define GEN_PASS_DEF_REGIONBRANCHOPLEVELINVARIANCE
#include "lib/Transforms/Halo/Halo.h.inc"

struct RegionBranchOpLevelInvariance
    : impl::RegionBranchOpLevelInvarianceBase<RegionBranchOpLevelInvariance> {
  using RegionBranchOpLevelInvarianceBase::RegionBranchOpLevelInvarianceBase;

  void runOnOperation() override {
    MLIRContext* context = &getContext();
    RewritePatternSet patterns(context);

    DataFlowSolver solver;
    SymbolTableCollection symbolTable;
    dataflow::loadBaselineAnalyses(solver);
    solver.load<SecretnessAnalysis>();
    solver.load<LevelAnalysis>(levelBudget);
    solver.load<LevelAnalysisBackward>(symbolTable);

    if (failed(solver.initializeAndRun(getOperation()))) {
      getOperation()->emitOpError() << "Failed to run the analysis.\n";
      signalPassFailure();
      return;
    }

    patterns.add<RegionBranchOpLevelInvariancePattern>(context, &solver);
    if (failed(applyPatternsGreedily(getOperation(), std::move(patterns)))) {
      signalPassFailure();
      return;
    }

    // Re-run solver to validate
    DataFlowSolver validationSolver;
    dataflow::loadBaselineAnalyses(validationSolver);
    validationSolver.load<SecretnessAnalysis>();
    validationSolver.load<LevelAnalysis>(levelBudget);
    if (failed(validationSolver.initializeAndRun(getOperation()))) {
      getOperation()->emitOpError() << "Failed to run validation analysis.\n";
      signalPassFailure();
      return;
    }
    if (failed(validateLevelAnalysis(validationSolver, getOperation()))) {
      signalPassFailure();
    }
  }
};

}  // namespace heir
}  // namespace mlir
