[X86] Mark the AAD and AAM aliases as not valid in 64-bit mode.
[oota-llvm.git] / lib / Transforms / Scalar / ConstantHoisting.cpp
index ce2e7eb6bf22eb6681e3b27b787ef8bf4f752454..af3edcf7228fc4e0860ed40244bafff3b95e6fc0 100644 (file)
@@ -33,7 +33,6 @@
 // %0 = load i64* inttoptr (i64 big_constant to i64*)
 //===----------------------------------------------------------------------===//
 
-#define DEBUG_TYPE "consthoist"
 #include "llvm/Transforms/Scalar.h"
 #include "llvm/ADT/SmallSet.h"
 #include "llvm/ADT/SmallVector.h"
 #include "llvm/IR/IntrinsicInst.h"
 #include "llvm/Pass.h"
 #include "llvm/Support/Debug.h"
+#include "llvm/Support/raw_ostream.h"
+#include <tuple>
 
 using namespace llvm;
 
+#define DEBUG_TYPE "consthoist"
+
 STATISTIC(NumConstantsHoisted, "Number of constants hoisted");
 STATISTIC(NumConstantsRebased, "Number of constants rebased");
 
@@ -87,10 +90,9 @@ struct ConstantCandidate {
 struct RebasedConstantInfo {
   ConstantUseListType Uses;
   Constant *Offset;
-  mutable BasicBlock *IDom;
 
   RebasedConstantInfo(ConstantUseListType &&Uses, Constant *Offset)
-    : Uses(Uses), Offset(Offset), IDom(nullptr) { }
+    : Uses(std::move(Uses)), Offset(Offset) { }
 };
 
 /// \brief A base constant and all its rebased constants.
@@ -109,7 +111,6 @@ class ConstantHoisting : public FunctionPass {
   BasicBlock *Entry;
 
   /// Keeps track of constant candidates found in the function.
-  ConstCandMapType ConstCandMap;
   ConstCandVecType ConstCandVec;
 
   /// Keep track of cast instructions we already cloned.
@@ -119,7 +120,8 @@ class ConstantHoisting : public FunctionPass {
   SmallVector<ConstantInfo, 8> ConstantVec;
 public:
   static char ID; // Pass identification, replacement for typeid
-  ConstantHoisting() : FunctionPass(ID), TTI(0), DT(0), Entry(0) {
+  ConstantHoisting() : FunctionPass(ID), TTI(nullptr), DT(nullptr),
+                       Entry(nullptr) {
     initializeConstantHoistingPass(*PassRegistry::getPassRegistry());
   }
 
@@ -130,14 +132,14 @@ public:
   void getAnalysisUsage(AnalysisUsage &AU) const override {
     AU.setPreservesCFG();
     AU.addRequired<DominatorTreeWrapperPass>();
-    AU.addRequired<TargetTransformInfo>();
+    AU.addRequired<TargetTransformInfoWrapperPass>();
   }
 
 private:
   /// \brief Initialize the pass.
   void setup(Function &Fn) {
     DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
-    TTI = &getAnalysis<TargetTransformInfo>();
+    TTI = &getAnalysis<TargetTransformInfoWrapperPass>().getTTI(Fn);
     Entry = &Fn.getEntryBlock();
   }
 
@@ -146,29 +148,19 @@ private:
     ConstantVec.clear();
     ClonedCastMap.clear();
     ConstCandVec.clear();
-    ConstCandMap.clear();
 
     TTI = nullptr;
     DT = nullptr;
     Entry = nullptr;
   }
 
-  /// \brief Find the common dominator of all uses and cache the result for
-  /// future lookup.
-  BasicBlock *getIDom(const RebasedConstantInfo &RCI) const {
-    if (RCI.IDom)
-      return RCI.IDom;
-    RCI.IDom = findIDomOfAllUses(RCI.Uses);
-    assert(RCI.IDom && "Invalid IDom.");
-    return RCI.IDom;
-  }
-
-  BasicBlock *findIDomOfAllUses(const ConstantUseListType &Uses) const;
   Instruction *findMatInsertPt(Instruction *Inst, unsigned Idx = ~0U) const;
   Instruction *findConstantInsertionPoint(const ConstantInfo &ConstInfo) const;
-  void collectConstantCandidates(Instruction *Inst, unsigned Idx,
+  void collectConstantCandidates(ConstCandMapType &ConstCandMap,
+                                 Instruction *Inst, unsigned Idx,
                                  ConstantInt *ConstInt);
-  void collectConstantCandidates(Instruction *Inst);
+  void collectConstantCandidates(ConstCandMapType &ConstCandMap,
+                                 Instruction *Inst);
   void collectConstantCandidates(Function &Fn);
   void findAndMakeBaseConstant(ConstCandVecType::iterator S,
                                ConstCandVecType::iterator E);
@@ -185,7 +177,7 @@ char ConstantHoisting::ID = 0;
 INITIALIZE_PASS_BEGIN(ConstantHoisting, "consthoist", "Constant Hoisting",
                       false, false)
 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
-INITIALIZE_AG_DEPENDENCY(TargetTransformInfo)
+INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass)
 INITIALIZE_PASS_END(ConstantHoisting, "consthoist", "Constant Hoisting",
                     false, false)
 
@@ -195,6 +187,9 @@ FunctionPass *llvm::createConstantHoistingPass() {
 
 /// \brief Perform the constant hoisting optimization for the given function.
 bool ConstantHoisting::runOnFunction(Function &Fn) {
+  if (skipOptnoneFunction(Fn))
+    return false;
+
   DEBUG(dbgs() << "********** Begin Constant Hoisting **********\n");
   DEBUG(dbgs() << "********** Function: " << Fn.getName() << '\n');
 
@@ -214,41 +209,24 @@ bool ConstantHoisting::runOnFunction(Function &Fn) {
   return MadeChange;
 }
 
-/// \brief Find nearest common dominator of all uses.
-/// FIXME: Replace this with NearestCommonDominator once it is in common code.
-BasicBlock *
-ConstantHoisting::findIDomOfAllUses(const ConstantUseListType &Uses) const {
-  // Collect all basic blocks.
-  SmallPtrSet<BasicBlock *, 8> BBs;
-  for (auto const &U : Uses)
-    BBs.insert(findMatInsertPt(U.Inst, U.OpndIdx)->getParent());
-
-  if (BBs.count(Entry))
-    return Entry;
-
-  while (BBs.size() >= 2) {
-    BasicBlock *BB, *BB1, *BB2;
-    BB1 = *BBs.begin();
-    BB2 = *std::next(BBs.begin());
-    BB = DT->findNearestCommonDominator(BB1, BB2);
-    if (BB == Entry)
-      return Entry;
-    BBs.erase(BB1);
-    BBs.erase(BB2);
-    BBs.insert(BB);
-  }
-  assert((BBs.size() == 1) && "Expected only one element.");
-  return *BBs.begin();
-}
 
 /// \brief Find the constant materialization insertion point.
 Instruction *ConstantHoisting::findMatInsertPt(Instruction *Inst,
                                                unsigned Idx) const {
-  // The simple and common case.
-  if (!isa<PHINode>(Inst) && !isa<LandingPadInst>(Inst))
+  // If the operand is a cast instruction, then we have to materialize the
+  // constant before the cast instruction.
+  if (Idx != ~0U) {
+    Value *Opnd = Inst->getOperand(Idx);
+    if (auto CastInst = dyn_cast<Instruction>(Opnd))
+      if (CastInst->isCast())
+        return CastInst;
+  }
+
+  // The simple and common case. This also includes constant expressions.
+  if (!isa<PHINode>(Inst) && !Inst->isEHPad())
     return Inst;
 
-  // We can't insert directly before a phi node or landing pad. Insert before
+  // We can't insert directly before a phi node or an eh pad. Insert before
   // the terminator of the incoming or dominating block.
   assert(Entry != Inst->getParent() && "PHI or landing pad in entry block!");
   if (Idx != ~0U && isa<PHINode>(Inst))
@@ -262,12 +240,11 @@ Instruction *ConstantHoisting::findMatInsertPt(Instruction *Inst,
 Instruction *ConstantHoisting::
 findConstantInsertionPoint(const ConstantInfo &ConstInfo) const {
   assert(!ConstInfo.RebasedConstants.empty() && "Invalid constant info entry.");
-  // Collect all IDoms.
+  // Collect all basic blocks.
   SmallPtrSet<BasicBlock *, 8> BBs;
   for (auto const &RCI : ConstInfo.RebasedConstants)
-    BBs.insert(getIDom(RCI));
-
-  assert(!BBs.empty() && "No dominators!?");
+    for (auto const &U : RCI.Uses)
+      BBs.insert(findMatInsertPt(U.Inst, U.OpndIdx)->getParent());
 
   if (BBs.count(Entry))
     return &Entry->front();
@@ -295,7 +272,8 @@ findConstantInsertionPoint(const ConstantInfo &ConstInfo) const {
 /// The operand at index Idx is not necessarily the constant integer itself. It
 /// could also be a cast instruction or a constant expression that uses the
 // constant integer.
-void ConstantHoisting::collectConstantCandidates(Instruction *Inst,
+void ConstantHoisting::collectConstantCandidates(ConstCandMapType &ConstCandMap,
+                                                 Instruction *Inst,
                                                  unsigned Idx,
                                                  ConstantInt *ConstInt) {
   unsigned Cost;
@@ -331,7 +309,8 @@ void ConstantHoisting::collectConstantCandidates(Instruction *Inst,
 
 /// \brief Scan the instruction for expensive integer constants and record them
 /// in the constant candidate vector.
-void ConstantHoisting::collectConstantCandidates(Instruction *Inst) {
+void ConstantHoisting::collectConstantCandidates(ConstCandMapType &ConstCandMap,
+                                                 Instruction *Inst) {
   // Skip all cast instructions. They are visited indirectly later on.
   if (Inst->isCast())
     return;
@@ -345,9 +324,9 @@ void ConstantHoisting::collectConstantCandidates(Instruction *Inst) {
   for (unsigned Idx = 0, E = Inst->getNumOperands(); Idx != E; ++Idx) {
     Value *Opnd = Inst->getOperand(Idx);
 
-    // Vist constant integers.
+    // Visit constant integers.
     if (auto ConstInt = dyn_cast<ConstantInt>(Opnd)) {
-      collectConstantCandidates(Inst, Idx, ConstInt);
+      collectConstantCandidates(ConstCandMap, Inst, Idx, ConstInt);
       continue;
     }
 
@@ -361,7 +340,7 @@ void ConstantHoisting::collectConstantCandidates(Instruction *Inst) {
       if (auto *ConstInt = dyn_cast<ConstantInt>(CastInst->getOperand(0))) {
         // Pretend the constant is directly used by the instruction and ignore
         // the cast instruction.
-        collectConstantCandidates(Inst, Idx, ConstInt);
+        collectConstantCandidates(ConstCandMap, Inst, Idx, ConstInt);
         continue;
       }
     }
@@ -375,7 +354,7 @@ void ConstantHoisting::collectConstantCandidates(Instruction *Inst) {
       if (auto ConstInt = dyn_cast<ConstantInt>(ConstExpr->getOperand(0))) {
         // Pretend the constant is directly used by the instruction and ignore
         // the constant expression.
-        collectConstantCandidates(Inst, Idx, ConstInt);
+        collectConstantCandidates(ConstCandMap, Inst, Idx, ConstInt);
         continue;
       }
     }
@@ -385,9 +364,10 @@ void ConstantHoisting::collectConstantCandidates(Instruction *Inst) {
 /// \brief Collect all integer constants in the function that cannot be folded
 /// into an instruction itself.
 void ConstantHoisting::collectConstantCandidates(Function &Fn) {
+  ConstCandMapType ConstCandMap;
   for (Function::iterator BB : Fn)
     for (BasicBlock::iterator Inst : *BB)
-      collectConstantCandidates(Inst);
+      collectConstantCandidates(ConstCandMap, Inst);
 }
 
 /// \brief Find the base constant within the given range and rebase all other
@@ -419,7 +399,7 @@ void ConstantHoisting::findAndMakeBaseConstant(ConstCandVecType::iterator S,
     ConstInfo.RebasedConstants.push_back(
       RebasedConstantInfo(std::move(ConstCand->Uses), Offset));
   }
-  ConstantVec.push_back(ConstInfo);
+  ConstantVec.push_back(std::move(ConstInfo));
 }
 
 /// \brief Finds and combines constant candidates that can be easily
@@ -523,8 +503,8 @@ void ConstantHoisting::emitBaseConstants(Instruction *Base, Constant *Offset,
       ClonedCastInst->insertAfter(CastInst);
       // Use the same debug location as the original cast instruction.
       ClonedCastInst->setDebugLoc(CastInst->getDebugLoc());
-      DEBUG(dbgs() << "Clone instruction: " << *ClonedCastInst << '\n'
-                   << "To               : " << *CastInst << '\n');
+      DEBUG(dbgs() << "Clone instruction: " << *CastInst << '\n'
+                   << "To               : " << *ClonedCastInst << '\n');
     }
 
     DEBUG(dbgs() << "Update: " << *ConstUser.Inst << '\n');
@@ -595,7 +575,7 @@ bool ConstantHoisting::emitBaseConstants() {
 void ConstantHoisting::deleteDeadCastInst() const {
   for (auto const &I : ClonedCastMap)
     if (I.first->use_empty())
-      I.first->removeFromParent();
+      I.first->eraseFromParent();
 }
 
 /// \brief Optimize expensive integer constants in the given function.