[LIR] Re-instate r244880, reverted in r244884, factoring the handling of
[oota-llvm.git] / lib / Transforms / Scalar / LoopIdiomRecognize.cpp
index 8ccbf9dcb1a7ef2670a8142a97c5220f8b1c456d..ee5203105201f791815302dafe72402785f30867 100644 (file)
@@ -69,7 +69,9 @@ namespace {
 
 class LoopIdiomRecognize : public LoopPass {
   Loop *CurLoop;
 
 class LoopIdiomRecognize : public LoopPass {
   Loop *CurLoop;
+  AliasAnalysis *AA;
   DominatorTree *DT;
   DominatorTree *DT;
+  LoopInfo *LI;
   ScalarEvolution *SE;
   TargetLibraryInfo *TLI;
   const TargetTransformInfo *TTI;
   ScalarEvolution *SE;
   TargetLibraryInfo *TLI;
   const TargetTransformInfo *TTI;
@@ -78,10 +80,6 @@ public:
   static char ID;
   explicit LoopIdiomRecognize() : LoopPass(ID) {
     initializeLoopIdiomRecognizePass(*PassRegistry::getPassRegistry());
   static char ID;
   explicit LoopIdiomRecognize() : LoopPass(ID) {
     initializeLoopIdiomRecognizePass(*PassRegistry::getPassRegistry());
-    DT = nullptr;
-    SE = nullptr;
-    TLI = nullptr;
-    TTI = nullptr;
   }
 
   bool runOnLoop(Loop *L, LPPassManager &LPM) override;
   }
 
   bool runOnLoop(Loop *L, LPPassManager &LPM) override;
@@ -106,30 +104,6 @@ public:
     AU.addRequired<TargetTransformInfoWrapperPass>();
   }
 
     AU.addRequired<TargetTransformInfoWrapperPass>();
   }
 
-  DominatorTree *getDominatorTree() {
-    return DT ? DT
-              : (DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree());
-  }
-
-  ScalarEvolution *getScalarEvolution() {
-    return SE ? SE : (SE = &getAnalysis<ScalarEvolution>());
-  }
-
-  TargetLibraryInfo *getTargetLibraryInfo() {
-    if (!TLI)
-      TLI = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
-
-    return TLI;
-  }
-
-  const TargetTransformInfo *getTargetTransformInfo() {
-    return TTI ? TTI
-               : (TTI = &getAnalysis<TargetTransformInfoWrapperPass>().getTTI(
-                      *CurLoop->getHeader()->getParent()));
-  }
-
-  Loop *getLoop() const { return CurLoop; }
-
 private:
   /// \name Countable Loop Idiom Handling
   /// @{
 private:
   /// \name Countable Loop Idiom Handling
   /// @{
@@ -205,7 +179,6 @@ bool LoopIdiomRecognize::runOnLoop(Loop *L, LPPassManager &LPM) {
     return false;
 
   CurLoop = L;
     return false;
 
   CurLoop = L;
-
   // If the loop could not be converted to canonical form, it must have an
   // indirectbr in it, just give up.
   if (!L->getLoopPreheader())
   // If the loop could not be converted to canonical form, it must have an
   // indirectbr in it, just give up.
   if (!L->getLoopPreheader())
@@ -216,9 +189,17 @@ bool LoopIdiomRecognize::runOnLoop(Loop *L, LPPassManager &LPM) {
   if (Name == "memset" || Name == "memcpy")
     return false;
 
   if (Name == "memset" || Name == "memcpy")
     return false;
 
+  AA = &getAnalysis<AliasAnalysis>();
+  DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
+  LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
   SE = &getAnalysis<ScalarEvolution>();
   SE = &getAnalysis<ScalarEvolution>();
+  TLI = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
+  TTI = &getAnalysis<TargetTransformInfoWrapperPass>().getTTI(
+      *CurLoop->getHeader()->getParent());
+
   if (SE->hasLoopInvariantBackedgeTakenCount(L))
     return runOnCountableLoop();
   if (SE->hasLoopInvariantBackedgeTakenCount(L))
     return runOnCountableLoop();
+
   return runOnNoncountableLoop();
 }
 
   return runOnNoncountableLoop();
 }
 
@@ -234,15 +215,6 @@ bool LoopIdiomRecognize::runOnCountableLoop() {
     if (BECst->getValue()->getValue() == 0)
       return false;
 
     if (BECst->getValue()->getValue() == 0)
       return false;
 
-  // set DT
-  (void)getDominatorTree();
-
-  LoopInfo &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
-  TLI = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
-
-  // set TLI
-  (void)getTargetLibraryInfo();
-
   SmallVector<BasicBlock *, 8> ExitBlocks;
   CurLoop->getUniqueExitBlocks(ExitBlocks);
 
   SmallVector<BasicBlock *, 8> ExitBlocks;
   CurLoop->getUniqueExitBlocks(ExitBlocks);
 
@@ -254,7 +226,7 @@ bool LoopIdiomRecognize::runOnCountableLoop() {
   // Scan all the blocks in the loop that are not in subloops.
   for (auto *BB : CurLoop->getBlocks()) {
     // Ignore blocks in subloops.
   // Scan all the blocks in the loop that are not in subloops.
   for (auto *BB : CurLoop->getBlocks()) {
     // Ignore blocks in subloops.
-    if (LI.getLoopFor(BB) != CurLoop)
+    if (LI->getLoopFor(BB) != CurLoop)
       continue;
 
     MadeChange |= runOnLoopBlock(BB, BECount, ExitBlocks);
       continue;
 
     MadeChange |= runOnLoopBlock(BB, BECount, ExitBlocks);
@@ -535,7 +507,7 @@ bool LoopIdiomRecognize::processLoopStridedStore(
                                           Preheader->getTerminator());
 
   if (mayLoopAccessLocation(BasePtr, MRI_ModRef, CurLoop, BECount, StoreSize,
                                           Preheader->getTerminator());
 
   if (mayLoopAccessLocation(BasePtr, MRI_ModRef, CurLoop, BECount, StoreSize,
-                            getAnalysis<AliasAnalysis>(), TheStore)) {
+                            *AA, TheStore)) {
     Expander.clear();
     // If we generated new code for the base pointer, clean up.
     RecursivelyDeleteTriviallyDeadInstructions(BasePtr, TLI);
     Expander.clear();
     // If we generated new code for the base pointer, clean up.
     RecursivelyDeleteTriviallyDeadInstructions(BasePtr, TLI);
@@ -624,7 +596,7 @@ bool LoopIdiomRecognize::processLoopStoreOfLoopLoad(
       Preheader->getTerminator());
 
   if (mayLoopAccessLocation(StoreBasePtr, MRI_ModRef, CurLoop, BECount,
       Preheader->getTerminator());
 
   if (mayLoopAccessLocation(StoreBasePtr, MRI_ModRef, CurLoop, BECount,
-                            StoreSize, getAnalysis<AliasAnalysis>(), SI)) {
+                            StoreSize, *AA, SI)) {
     Expander.clear();
     // If we generated new code for the base pointer, clean up.
     RecursivelyDeleteTriviallyDeadInstructions(StoreBasePtr, TLI);
     Expander.clear();
     // If we generated new code for the base pointer, clean up.
     RecursivelyDeleteTriviallyDeadInstructions(StoreBasePtr, TLI);
@@ -638,7 +610,7 @@ bool LoopIdiomRecognize::processLoopStoreOfLoopLoad(
       Preheader->getTerminator());
 
   if (mayLoopAccessLocation(LoadBasePtr, MRI_Mod, CurLoop, BECount, StoreSize,
       Preheader->getTerminator());
 
   if (mayLoopAccessLocation(LoadBasePtr, MRI_Mod, CurLoop, BECount, StoreSize,
-                            getAnalysis<AliasAnalysis>(), SI)) {
+                            *AA, SI)) {
     Expander.clear();
     // If we generated new code for the base pointer, clean up.
     RecursivelyDeleteTriviallyDeadInstructions(LoadBasePtr, TLI);
     Expander.clear();
     // If we generated new code for the base pointer, clean up.
     RecursivelyDeleteTriviallyDeadInstructions(LoadBasePtr, TLI);
@@ -852,10 +824,6 @@ static bool detectPopcountIdiom(Loop *CurLoop, BasicBlock *PreCondBB,
 /// If detected, transforms the relevant code to issue the popcount intrinsic
 /// function call, and returns true; otherwise, returns false.
 bool LoopIdiomRecognize::recognizePopcount() {
 /// If detected, transforms the relevant code to issue the popcount intrinsic
 /// function call, and returns true; otherwise, returns false.
 bool LoopIdiomRecognize::recognizePopcount() {
-  (void)getScalarEvolution();
-  (void)getTargetLibraryInfo();
-  (void)getTargetTransformInfo();
-
   if (TTI->getPopcntSupport(32) != TargetTransformInfo::PSK_FastHardware)
     return false;
 
   if (TTI->getPopcntSupport(32) != TargetTransformInfo::PSK_FastHardware)
     return false;