/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 * 
 * File:   transformersaturation.h
 * Author: pgess <v.melnychenko@xreate.org>
 *
 * Created on March 25, 2017, 9:59 PM
 */

#ifndef TRANSFORMERSATURATION_H
#define TRANSFORMERSATURATION_H

#include "transformations.h"

namespace xreate {    namespace compilation {
    
/** \brief Loop saturation support */
class TransformerSaturation: public Transformer{
public:    
    TransformerSaturation(llvm::BasicBlock* allocationBlock, TransformationsManager* manager);
    ~TransformerSaturation();
    
    llvm::Value* transform(const Expression& expression, llvm::Value* raw, const Context& ctx) override;

    void processBreak(const Context& ctx);
    
    void allocateFlag(const Context& ctx);
    bool insertSaturationChecks(llvm::BasicBlock* blockContinue, llvm::BasicBlock* blockExit, const Context& ctx);

private:
    TransformationsManager* man;
    TransformerSaturation* oldInstance = nullptr;
    llvm::BasicBlock* blockAllocation;
    llvm::Value* flagSaturation = nullptr;
};

template <> 
struct TransformerInfo<TransformerSaturation> {
    static const unsigned int id = 0;
};

} }

#endif /* TRANSFORMERSATURATION_H */

