//===- LoopILPEstimation.h - Estimate ILP in Loops ------------------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
//
//===----------------------------------------------------------------------===//
//====  Copyright (c) 2015 Advanced Micro Devices, Inc.  All rights reserved.
//
//               Developed by: Advanced Micro Devices, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// with the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// Redistributions of source code must retain the above copyright notice, this
// list of conditions and the following disclaimers.
//
// Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimers in the documentation
// and/or other materials provided with the distribution.
//
// Neither the names of Advanced Micro Devices, Inc., nor the names of its
// contributors may be used to endorse or promote products derived from this
// Software without specific prior written permission.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH
// THE SOFTWARE.
//===----------------------------------------------------------------------===//

#ifndef LLVM_TRANSFORMS_UTILS_LOOPILPESTIMATION_H
#define LLVM_TRANSFORMS_UTILS_LOOPILPESTIMATION_H

#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/MapVector.h"
#include "llvm/Analysis/LoopInfo.h"
#include "llvm/Analysis/MemorySSA.h"
#include "llvm/Analysis/TargetTransformInfo.h"
#include "llvm/IR/Instruction.h"

namespace llvm {

struct LoopILPEstimation {
  template <uint32_t default_val> struct UIntWithDefault {
    uint32_t Val;
    UIntWithDefault() : Val(default_val){};
    UIntWithDefault(uint32_t Val) : Val(Val){};
    operator uint32_t() { return Val; }
  };

  // We want nodes to have default weight as 1.
  using OpcodeWeightMapTy = DenseMap<unsigned, UIntWithDefault<1>>;

  // However for longest path calculation we want each node to have
  // default weight zero.
  using OpcodeDistanceMapTy = DenseMap<unsigned, uint32_t>;

  // We want nodes to have default width as a very large value.
  using OpcodeWidthMapTy =
      DenseMap<unsigned,
               UIntWithDefault<std::numeric_limits<uint32_t>::max() / 2>>;

  using InstWeightsMapTy = MapVector<Instruction *, OpcodeWeightMapTy>;
  using InstDistancesMapTy = MapVector<Instruction *, OpcodeDistanceMapTy>;
  using InstWidthsMapTy = MapVector<Instruction *, OpcodeWidthMapTy>;

  static void populateMemoryUses(
      MapVector<Instruction *, SmallVector<Instruction *, 4>> MemoryUses,
      BasicBlock *BB, MemorySSA &MSSA, AliasAnalysis &AA);

  static void populateInstStats(BasicBlock *BB, InstWeightsMapTy &WeightsMap,
                                MapVector<unsigned, unsigned> &OpcodesInBB,
                                InstWidthsMapTy &WidthsMap,
                                const DataLayout &DL, bool indVarIncrBlock);

  static bool shouldTraverseEdge(Instruction *Src, Instruction *Dst);

  static unsigned getUnrollFactor(Loop *L, LoopInfo *LI, MemorySSA &MSSA,
                                  AliasAnalysis &AA,
                                  const TargetTransformInfo &TTI, unsigned VectorWidth);

  static bool hasVectorInsts(Loop *L);

  static unsigned
  getThresholdUnrollFactor(Loop *L, LoopInfo *LI, MemorySSA &MSSA,
                                        AliasAnalysis &AA,
                                        const TargetTransformInfo &TTI, bool SetGSLPZn4MaxVecRegSize);

  static const std::set<unsigned> WidthIgnoreList;
  static const unsigned MinNoInsts = 5;
};
} // end namespace llvm

#endif // LLVM_TRANSFORMS_UTILS_LOOPILPESTIMATION_H
