#ifndef LLVM_INTER_PROCEDURAL_DOMINANCE_H
#define LLVM_INTER_PROCEDURAL_DOMINANCE_H
//=========-            InterProceduralDominanceAnalysis.h -       -==========//
// Inter procedural dominance analysis
//===----------------------------------------------------------------------===//
//                     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.
//
//===----------------------------------------------------------------------===//
// PLEASE NOTE:
// isDominate(I1, I2) : Return true if I1 dominate I2
// isDominate(B1, B2) : Return true if B1 dominate B2
// isReachable(I1, I2) : Returns false if I2 is not reachable from I1
// isReachable(B1, B2) : Returns false if B2 is not reachable from B1
//===----------------------------------------------------------------------===//
#include "llvm/ADT/APInt.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/Analysis/AliasAnalysis.h"
#include "llvm/Analysis/AliasSetTracker.h"
#include "llvm/Analysis/CFG.h"
#include "llvm/Analysis/ScalarEvolution.h"
#include "llvm/Analysis/VectorUtils.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/DebugInfo.h"
#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/Dominators.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/InstIterator.h"
#include "llvm/IR/InstrTypes.h"
#include "llvm/IR/Instruction.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/TypeFinder.h"
#include "llvm/IR/ValueSymbolTable.h"
namespace llvm {
class InterProceduralDominatorAnalysis {
public:
  bool isDominate(Instruction *I1, Instruction *I2);
  bool isDominate(BasicBlock *B1, BasicBlock *B2);

  bool isReachable(Instruction *I1, Instruction *I2);
  bool isReachable(BasicBlock *B1, BasicBlock *B2);

  InterProceduralDominatorAnalysis(llvm::AnalysisManager<llvm::Function> *FAM)
      : FAM(FAM) {
    SourceHierarchy.clear();
    Visitor.clear();
    FuncWithException.clear();
    CatchBlocks.clear();
  }

  ~InterProceduralDominatorAnalysis() { resetInfo(); }

private:
  llvm::AnalysisManager<llvm::Function> *FAM;
  std::map<Function *, std::map<Instruction *, bool> *> SourceHierarchy;
  std::map<Instruction *, bool> Visitor;
  std::map<Function *, bool> FuncWithException;
  std::map<BasicBlock *, bool> CatchBlocks;

  void resetInfo();
  bool isStartFunction(Function *F);
  bool identifyCallsToFunction(Function *, SmallVector<CallBase *, 2> &);
  std::map<Instruction *, bool> *getSourcePoints(Function *);
  bool collectSourceDominatorHierarchy(Instruction *, bool HasException);
  void addSourceHierarchy(Instruction *);
  void getCatchBlocksFromFunction(Function *,
                                  SmallVector<BasicBlock *, 2> &CatchBlocks);
  bool getIntermedeateBlocks(BasicBlock *B1, BasicBlock *B2,
                             std::map<BasicBlock *, bool> &Blocks);
  bool isPostDominateWithExceptions(BasicBlock *B1, BasicBlock *B2);

  bool isDominatedBySrcHierarchy(Instruction *);
  bool verifyDominance(Instruction *I);
  bool isReachableBySrcHierarchy(Instruction *);
  bool verifyReachablity(Instruction *);
  bool collectSourceReachablityHierarchy(Instruction *);
};
} // namespace llvm
#endif
