//===- MoveLoadSliceGSLP.h ------------------------------------------*- C++ -*-===//
//
//                     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_SCALAR_MOVELOADSLICEGSLP_H
#define LLVM_TRANSFORMS_SCALAR_MOVELOADSLICEGSLP_H

#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/Analysis/AliasAnalysis.h"
#include "llvm/Analysis/LegacyAliasAnalysis.h"
#include "llvm/Analysis/LoopAccessAnalysis.h"
#include "llvm/Analysis/ScalarEvolution.h"
#include "llvm/IR/BasicBlock.h"
#include "llvm/IR/DataLayout.h"
#include "llvm/IR/Dominators.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/Instruction.h"
#include "llvm/InitializePasses.h"
#include "llvm/Pass.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Transforms/Scalar.h"
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
#include "llvm/IR/Verifier.h"

namespace llvm {

struct MoveLoadSliceGSLPPass : public PassInfoMixin<MoveLoadSliceGSLPPass> {
  ScalarEvolution *SE = nullptr;
  DominatorTree *DT = nullptr;
  LoopInfo *LI = nullptr;
  AliasAnalysis *AA = nullptr;
  const DataLayout *DL = nullptr;

public:
  PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);

  // Glue for old PM.
  bool runImpl(Function &F, ScalarEvolution *SE_, DominatorTree *DT_,
               LoopInfo *LI_, AliasAnalysis *AA_, const DataLayout *DL_);

  // A load instruction is safe to be moved only when there is no side-effect causing
  // aliasing instruction before it in the BB.
  bool safeToMoveAboveWithinBB(Instruction *);
  // Skip load if cannot be moved into pred
  bool skipLoadForPred(Instruction *, Instruction *, BasicBlock *);
  // Returns a list of all the load instructions in the BB
  SmallVector<Instruction *, 4> *getAllLoads(BasicBlock &);
  // Returns a list of all the single load instructions in the BB
  SmallVector<Instruction *, 4> *getSingleLoads(BasicBlock &);
  // Check if any load in the list accesses neighboring memory location of the
  // location accessed by loadInst
  Instruction *containsLoadsToNeighboringLoc(SmallVector<Instruction *, 4> *,
                                     Instruction *);
  // Get the program slice/chop of the load instruction
  SmallVector<Instruction *, 4> *getFullyDepLoadSlice(Instruction *,
                                                      BasicBlock *);
};
} // end namespace llvm

#endif // LLVM_TRANSFORMS_SCALAR_MOVELOADSLICEGSLP_H
