Esempio n. 1
0
/// Assign pass manager to manage this pass.
void LoopPass::assignPassManager(PMStack &PMS,
                                 PassManagerType PreferredType) {
    // Find LPPassManager
    while (!PMS.empty() &&
            PMS.top()->getPassManagerType() > PMT_LoopPassManager)
        PMS.pop();

    LPPassManager *LPPM;
    if (PMS.top()->getPassManagerType() == PMT_LoopPassManager)
        LPPM = (LPPassManager*)PMS.top();
    else {
        // Create new Loop Pass Manager if it does not exist.
        assert (!PMS.empty() && "Unable to create Loop Pass Manager");
        PMDataManager *PMD = PMS.top();

        // [1] Create new Loop Pass Manager
        LPPM = new LPPassManager();
        LPPM->populateInheritedAnalysis(PMS);

        // [2] Set up new manager's top level manager
        PMTopLevelManager *TPM = PMD->getTopLevelManager();
        TPM->addIndirectPassManager(LPPM);

        // [3] Assign manager to manage this new manager. This may create
        // and push new managers into PMS
        Pass *P = LPPM->getAsPass();
        TPM->schedulePass(P);

        // [4] Push new manager into PMS
        PMS.push(LPPM);
    }

    LPPM->add(this);
}
Esempio n. 2
0
// Check if this pass is suitable for the current LPPassManager, if
// available. This pass P is not suitable for a LPPassManager if P
// is not preserving higher level analysis info used by other
// LPPassManager passes. In such case, pop LPPassManager from the
// stack. This will force assignPassManager() to create new
// LPPassManger as expected.
void LoopPass::preparePassManager(PMStack &PMS) {

  // Find LPPassManager 
  while (!PMS.empty() &&
         PMS.top()->getPassManagerType() > PMT_LoopPassManager)
    PMS.pop();

  LPPassManager *LPPM = dynamic_cast<LPPassManager *>(PMS.top());

  // If this pass is destroying high level information that is used
  // by other passes that are managed by LPM then do not insert
  // this pass in current LPM. Use new LPPassManager.
  if (LPPM && !LPPM->preserveHigherLevelAnalysis(this)) 
    PMS.pop();
}
Esempio n. 3
0
bool LoopExtractor::runOnLoop(Loop *L, LPPassManager &LPM) {
  // Only visit top-level loops.
  if (L->getParentLoop())
    return false;

  // If LoopSimplify form is not available, stay out of trouble.
  if (!L->isLoopSimplifyForm())
    return false;

  DominatorTree &DT = getAnalysis<DominatorTree>();
  bool Changed = false;

  // If there is more than one top-level loop in this function, extract all of
  // the loops. Otherwise there is exactly one top-level loop; in this case if
  // this function is more than a minimal wrapper around the loop, extract
  // the loop.
  bool ShouldExtractLoop = false;

  // Extract the loop if the entry block doesn't branch to the loop header.
  TerminatorInst *EntryTI =
    L->getHeader()->getParent()->getEntryBlock().getTerminator();
  if (!isa<BranchInst>(EntryTI) ||
      !cast<BranchInst>(EntryTI)->isUnconditional() ||
      EntryTI->getSuccessor(0) != L->getHeader())
    ShouldExtractLoop = true;
  else {
    // Check to see if any exits from the loop are more than just return
    // blocks.
    SmallVector<BasicBlock*, 8> ExitBlocks;
    L->getExitBlocks(ExitBlocks);
    for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i)
      if (!isa<ReturnInst>(ExitBlocks[i]->getTerminator())) {
        ShouldExtractLoop = true;
        break;
      }
  }
  if (ShouldExtractLoop) {
    if (NumLoops == 0) return Changed;
    --NumLoops;
    if (ExtractLoop(DT, L) != 0) {
      Changed = true;
      // After extraction, the loop is replaced by a function call, so
      // we shouldn't try to run any more loop passes on it.
      LPM.deleteLoopFromQueue(L);
    }
    ++NumExtracted;
  }

  return Changed;
}
Esempio n. 4
0
bool LoopDeletionLegacyPass::runOnLoop(Loop *L, LPPassManager &LPM) {
  if (skipLoop(L))
    return false;
  DominatorTree &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
  ScalarEvolution &SE = getAnalysis<ScalarEvolutionWrapperPass>().getSE();
  LoopInfo &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();

  DEBUG(dbgs() << "Analyzing Loop for deletion: ");
  DEBUG(L->dump());

  LoopDeletionResult Result = deleteLoopIfDead(L, DT, SE, LI);

  if (Result == LoopDeletionResult::Deleted)
    LPM.markLoopAsDeleted(*L);

  return Result != LoopDeletionResult::Unmodified;
}
Esempio n. 5
0
bool LoopRegionOutliner::runOnLoop(Loop *L, LPPassManager &LPM) {
  // Only visit top-level loops.
  if (L->getParentLoop())
    return false;

  // If LoopSimplify form is not available, stay out of trouble.
  if (!L->isLoopSimplifyForm())
    return false;
#if LLVM_VERSION_MINOR == 5
    DominatorTree &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
#else
    DominatorTree &DT = getAnalysis<DominatorTree>();
#endif
  
  bool Changed = false;

  // Extract the loop if it was not previously extracted:
  // If this loop is inside a function prefixed with __cere__
  // it means we are looking at an already outlined loop
  bool ShouldExtractLoop = true;
  Function *function = L->getHeader()->getParent();
  std::string name = function->getName();
  std::size_t found = name.find("__cere__");
  if (found != std::string::npos) {
    ShouldExtractLoop = false;
  }

  if (ShouldExtractLoop) {
    if (NumLoops == 0)
      return Changed;
    --NumLoops;
    RegionExtractor Extractor(DT, *L, RegionName, ProfileApp, false);
    if (Extractor.extractCodeRegion() != 0) {
      Changed = true;
      // After extraction, the loop is replaced by a function call, so
      // we shouldn't try to run any more loop passes on it.
      LPM.deleteLoopFromQueue(L);
    }
    ++NumExtracted;
  }

  return Changed;
}
      // If one loop has very large self trip count
      // we don't want to unroll it.
      // self trip count means trip count divide by the parent's trip count. for example
      // for (int i = 0; i < 16; i++) {
      //   for (int j = 0; j < 4; j++) {
      //     for (int k = 0; k < 2; k++) {
      //       ...
      //     }
      //     ...
      //   }
      // The inner loops j and k could be unrolled, but the loop i will not be unrolled.
      // The return value true means the L could be unrolled, otherwise, it could not
      // be unrolled.
      bool handleParentLoops(Loop *L, LPPassManager &LPM) {
        Loop *currL = L;
        ScalarEvolution *SE = &getAnalysis<ScalarEvolution>();
        BasicBlock *ExitBlock = currL->getLoopLatch();
        if (!ExitBlock || !L->isLoopExiting(ExitBlock))
          ExitBlock = currL->getExitingBlock();

        unsigned currTripCount = 0;
        bool shouldUnroll = true;
        if (ExitBlock)
          currTripCount = SE->getSmallConstantTripCount(L, ExitBlock);

        while(currL) {
          Loop *parentL = currL->getParentLoop();
          unsigned parentTripCount = 0;
          if (parentL) {
            BasicBlock *parentExitBlock = parentL->getLoopLatch();
            if (!parentExitBlock || !parentL->isLoopExiting(parentExitBlock))
              parentExitBlock = parentL->getExitingBlock();

            if (parentExitBlock)
              parentTripCount = SE->getSmallConstantTripCount(parentL, parentExitBlock);
          }
          if ((parentTripCount != 0 && currTripCount / parentTripCount > 16) ||
              (currTripCount > 32)) {
            if (currL == L)
              shouldUnroll = false;
            setUnrollID(currL, false);
            if (currL != L)
              LPM.deleteLoopFromQueue(currL);
          }
          currL = parentL;
          currTripCount = parentTripCount;
        }
        return shouldUnroll;
      }
Esempio n. 7
0
// Inserts an unwinding annotation (assume or assert, depending on the function
// given in the constructor) and removes the loop.
bool RmLoopPass::runOnLoop(Loop *L, LPPassManager &LPM){

  BasicBlock *latch  = L -> getLoopLatch();
  BasicBlock *header = L -> getHeader();
  SmallVector<BasicBlock *, 1> exitBBs;
  L -> getExitBlocks(exitBBs);
  BasicBlock *exitBB = NULL;
  SmallVector<BasicBlock *, 1>::iterator it = exitBBs.begin();
  for(; it != exitBBs.end() && !exitBB; ++it){
    if(std::find(createdBB.begin(), createdBB.end(), *it) == createdBB.end())
      exitBB = *it;
  }

  assert(exitBB && "exitBB is null");

  // std::cout << "\n\n LOOP REMOVAL:\n";
  // std::cout << "Latch: " << latch -> getName().str() << "\n";
  // std::cout << "Header: " << header -> getName().str() << "\n";
  // std::cout << "ExitBB: " << exitBB -> getName().str() << "\n";
  //assert(exitBBs.size() == 1 && "RmLoopPass - more than one exit BB");
  // At this point we have an header, a latch and an exit BasicBlock 
  // and they all are different
  assert(latch && header
        && "Not able to obtain some loop basic block; try to run doInitialization before");
  
  // Get loop last branch instruction
  BranchInst *br = cast<BranchInst>(latch -> getTerminator());
  // assert(br -> isConditional() && "loop terminator with unconditional branch");
  // Get loop's last iteration condition
  Value *cond = NULL;      // Loop last iteration branch condition
  if(br -> isConditional())
    cond = br -> getCondition();
  else{
    std::cout << "\n************************************************************\n";
    std::cout <<   "*BE CAREFUL!!!! There is a latch with unconditional branch!*\n";
    std::cout <<   "************************************************************\n";
  }

  // In order to remove the back edge, we need to remove the loop from the LPPAssManager
  LPM.deleteLoopFromQueue(L);

  // Create a new BasicBlock with the unwinding annotation.
  // Unreachable instruction is used as a terminator instruction in this BasicBlock
  BasicBlock *newBB = BasicBlock::Create(header -> getContext()
                                        , "unwinding_annotation"
                                        , header -> getParent());
  createdBB.push_back(newBB);
  Type *t = Type::getInt32Ty(header -> getContext());
  Constant *c = llvm::ConstantInt::get(t,uint32_t(0));
  ArrayRef<Value *> *param = new ArrayRef<Value *>(c);
  CallInst::Create(function, *param, "", newBB);
  if(unreachable){
    new UnreachableInst(header -> getContext(), newBB); 
  }else{
    BranchInst::Create(exitBB,newBB);
    for(BasicBlock::iterator it = exitBB->begin(); it != exitBB->end();++it){
      PHINode *phi = dyn_cast<PHINode>(it);
      if(!phi)
        break;
      //Value *latchValue = phi->getIncomingValueForBlock(latch);
      phi->addIncoming(UndefValue::get(phi -> getType()),newBB);
    }
  }

  BranchInst *newBr = NULL;
  if(cond){
    if(br -> getSuccessor(0) == header){
      newBr = BranchInst::Create(newBB,br -> getSuccessor(1),cond);
    }else{
      newBr = BranchInst::Create(br -> getSuccessor(1),newBB,cond);
    }
  }else{
    newBr = BranchInst::Create(newBB);
  }
  ReplaceInstWithInst(br,newBr);

  // The latch BasicBlock must be removed from the PHI nodes in
  // the header BasicBlock
  for(BasicBlock::iterator it = header->begin(); it != header->end();++it){
    PHINode *phi = dyn_cast<PHINode>(it);
    if(!phi)
      break;
    int latchIndex = phi->getBasicBlockIndex(latch);
    phi->removeIncomingValue(latchIndex);
  }

 //std::cout << "\n---- NewBB ------\n";

 //newBB -> print(outs());

 //std::cout << "\n---- ExitBB\n";

 //exitBB -> print(outs());


  return true;
}
Esempio n. 8
0
/// runOnLoop - Remove dead loops, by which we mean loops that do not impact the
/// observable behavior of the program other than finite running time.  Note
/// we do ensure that this never remove a loop that might be infinite, as doing
/// so could change the halting/non-halting nature of a program.
/// NOTE: This entire process relies pretty heavily on LoopSimplify and LCSSA
/// in order to make various safety checks work.
bool LoopDeletion::runOnLoop(Loop *L, LPPassManager &LPM) {
  // We can only remove the loop if there is a preheader that we can
  // branch from after removing it.
  BasicBlock *preheader = L->getLoopPreheader();
  if (!preheader)
    return false;

  // If LoopSimplify form is not available, stay out of trouble.
  if (!L->hasDedicatedExits())
    return false;

  // We can't remove loops that contain subloops.  If the subloops were dead,
  // they would already have been removed in earlier executions of this pass.
  if (L->begin() != L->end())
    return false;

  SmallVector<BasicBlock*, 4> exitingBlocks;
  L->getExitingBlocks(exitingBlocks);

  SmallVector<BasicBlock*, 4> exitBlocks;
  L->getUniqueExitBlocks(exitBlocks);

  // We require that the loop only have a single exit block.  Otherwise, we'd
  // be in the situation of needing to be able to solve statically which exit
  // block will be branched to, or trying to preserve the branching logic in
  // a loop invariant manner.
  if (exitBlocks.size() != 1)
    return false;

  // Finally, we have to check that the loop really is dead.
  bool Changed = false;
  if (!isLoopDead(L, exitingBlocks, exitBlocks, Changed, preheader))
    return Changed;

  // Don't remove loops for which we can't solve the trip count.
  // They could be infinite, in which case we'd be changing program behavior.
  ScalarEvolution &SE = getAnalysis<ScalarEvolution>();
  const SCEV *S = SE.getMaxBackedgeTakenCount(L);
  if (isa<SCEVCouldNotCompute>(S))
    return Changed;

  // Now that we know the removal is safe, remove the loop by changing the
  // branch from the preheader to go to the single exit block.
  BasicBlock *exitBlock = exitBlocks[0];

  // Because we're deleting a large chunk of code at once, the sequence in which
  // we remove things is very important to avoid invalidation issues.  Don't
  // mess with this unless you have good reason and know what you're doing.

  // Tell ScalarEvolution that the loop is deleted. Do this before
  // deleting the loop so that ScalarEvolution can look at the loop
  // to determine what it needs to clean up.
  SE.forgetLoop(L);

  // Connect the preheader directly to the exit block.
  TerminatorInst *TI = preheader->getTerminator();
  TI->replaceUsesOfWith(L->getHeader(), exitBlock);

  // Rewrite phis in the exit block to get their inputs from
  // the preheader instead of the exiting block.
  BasicBlock *exitingBlock = exitingBlocks[0];
  BasicBlock::iterator BI = exitBlock->begin();
  while (PHINode *P = dyn_cast<PHINode>(BI)) {
    int j = P->getBasicBlockIndex(exitingBlock);
    assert(j >= 0 && "Can't find exiting block in exit block's phi node!");
    P->setIncomingBlock(j, preheader);
    for (unsigned i = 1; i < exitingBlocks.size(); ++i)
      P->removeIncomingValue(exitingBlocks[i]);
    ++BI;
  }

  // Update the dominator tree and remove the instructions and blocks that will
  // be deleted from the reference counting scheme.
  DominatorTree &DT = getAnalysis<DominatorTree>();
  SmallVector<DomTreeNode*, 8> ChildNodes;
  for (Loop::block_iterator LI = L->block_begin(), LE = L->block_end();
       LI != LE; ++LI) {
    // Move all of the block's children to be children of the preheader, which
    // allows us to remove the domtree entry for the block.
    ChildNodes.insert(ChildNodes.begin(), DT[*LI]->begin(), DT[*LI]->end());
    for (SmallVectorImpl<DomTreeNode *>::iterator DI = ChildNodes.begin(),
         DE = ChildNodes.end(); DI != DE; ++DI) {
      DT.changeImmediateDominator(*DI, DT[preheader]);
    }

    ChildNodes.clear();
    DT.eraseNode(*LI);

    // Remove the block from the reference counting scheme, so that we can
    // delete it freely later.
    (*LI)->dropAllReferences();
  }

  // Erase the instructions and the blocks without having to worry
  // about ordering because we already dropped the references.
  // NOTE: This iteration is safe because erasing the block does not remove its
  // entry from the loop's block list.  We do that in the next section.
  for (Loop::block_iterator LI = L->block_begin(), LE = L->block_end();
       LI != LE; ++LI)
    (*LI)->eraseFromParent();

  // Finally, the blocks from loopinfo.  This has to happen late because
  // otherwise our loop iterators won't work.
  LoopInfo &loopInfo = getAnalysis<LoopInfo>();
  SmallPtrSet<BasicBlock*, 8> blocks;
  blocks.insert(L->block_begin(), L->block_end());
  for (SmallPtrSet<BasicBlock*,8>::iterator I = blocks.begin(),
       E = blocks.end(); I != E; ++I)
    loopInfo.removeBlock(*I);

  // The last step is to inform the loop pass manager that we've
  // eliminated this loop.
  LPM.deleteLoopFromQueue(L);
  Changed = true;

  ++NumDeleted;

  return Changed;
}
Esempio n. 9
0
void DSWP::cleanup(Loop *L, LPPassManager &LPM) {
  // Move some instructions that may not have been inserted in the right
  // place, delete the old loop, and clean up our aux data structures for this
  // loop.

  /*
   * move the produce instructions, which have been inserted after the branch,
   * in front of it
   */
  for (int i = 0; i < MAX_THREAD; i++) {
      for (Function::iterator bi = allFunc[i]->begin(), be = allFunc[i]->end(); bi != be; ++bi) {
          BasicBlock *bb = bi;
          TerminatorInst *term = NULL;
          for (BasicBlock::iterator ii = bb->begin(), ie = bb->end(); ii != ie; ++ii) {
              Instruction *inst = ii;
              if (isa<TerminatorInst>(inst)) {
                  term = dyn_cast<TerminatorInst>(inst);
                  break;
              }
          }

          if (term == NULL) {
              error("term cannot be null");
          }

          while (true) {
              Instruction *last = &bb->getInstList().back();
              if (isa<TerminatorInst>(last))
                  break;
              last->moveBefore(term);
          }
      }
  }

  /*
   * move the phi nodes to the top of the block
   */
  for (int i = 0; i < MAX_THREAD; i++) {
      for (Function::iterator bi = allFunc[i]->begin(), be = allFunc[i]->end(); bi != be; ++bi) {
          BasicBlock *bb = bi;
          Instruction *first_nonphi = bb->getFirstNonPHI();
          BasicBlock::iterator ii = bb->begin(), ie = bb->end();

          // advance the iterator up to one past first_nonphi
          while (&(*ii) != first_nonphi) {
              ++ii;
          }
          ++ii;

          // move any phi nodes after the first nonphi to before it
          for (BasicBlock::iterator i_next; ii != ie; ii = i_next) {
              i_next = ii;
              ++i_next;
              Instruction *inst = ii;
              if (isa<PHINode>(inst)) {
                  inst->moveBefore(first_nonphi);
              }
          }
      }
  }

  cout << "begin to delete loop" << endl;
  for (Loop::block_iterator bi = L->block_begin(), be = L->block_end(); bi != be; ++bi) {
      BasicBlock *BB = *bi;
      for (BasicBlock::iterator ii = BB->begin(), i_next, ie = BB->end(); ii != ie; ii = i_next) {
          i_next = ii;
          ++i_next;
          Instruction &inst = *ii;
          inst.replaceAllUsesWith(UndefValue::get(inst.getType()));
          inst.eraseFromParent();
      }
  }

  // Delete the basic blocks only afterwards
  // so that backwards branch instructions don't break
  for (Loop::block_iterator bi = L->block_begin(), be = L->block_end(); bi != be; ++bi) {
      BasicBlock *BB = *bi;
      BB->eraseFromParent();
  }

  LPM.deleteLoopFromQueue(L);

}
Esempio n. 10
0
bool LoopExtractor::runOnLoop(Loop *L, LPPassManager &LPM) {
  if (skipOptnoneFunction(L))
    return false;

  // Only visit top-level loops.
  if (L->getParentLoop())
    return false;

  // If LoopSimplify form is not available, stay out of trouble.
  if (!L->isLoopSimplifyForm())
    return false;

  DominatorTree &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
  bool Changed = false;

  // If there is more than one top-level loop in this function, extract all of
  // the loops. Otherwise there is exactly one top-level loop; in this case if
  // this function is more than a minimal wrapper around the loop, extract
  // the loop.
  bool ShouldExtractLoop = false;

  // Extract the loop if the entry block doesn't branch to the loop header.
  TerminatorInst *EntryTI =
    L->getHeader()->getParent()->getEntryBlock().getTerminator();
  if (!isa<BranchInst>(EntryTI) ||
      !cast<BranchInst>(EntryTI)->isUnconditional() ||
      EntryTI->getSuccessor(0) != L->getHeader()) {
    ShouldExtractLoop = true;
  } else {
    // Check to see if any exits from the loop are more than just return
    // blocks.
    SmallVector<BasicBlock*, 8> ExitBlocks;
    L->getExitBlocks(ExitBlocks);
    for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i)
      if (!isa<ReturnInst>(ExitBlocks[i]->getTerminator())) {
        ShouldExtractLoop = true;
        break;
      }
  }

  if (ShouldExtractLoop) {
    // We must omit landing pads. Landing pads must accompany the invoke
    // instruction. But this would result in a loop in the extracted
    // function. An infinite cycle occurs when it tries to extract that loop as
    // well.
    SmallVector<BasicBlock*, 8> ExitBlocks;
    L->getExitBlocks(ExitBlocks);
    for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i)
      if (ExitBlocks[i]->isLandingPad()) {
        ShouldExtractLoop = false;
        break;
      }
  }

  if (ShouldExtractLoop) {
    if (NumLoops == 0) return Changed;
    --NumLoops;
    CodeExtractor Extractor(DT, *L);
    if (Extractor.extractCodeRegion() != nullptr) {
      Changed = true;
      // After extraction, the loop is replaced by a function call, so
      // we shouldn't try to run any more loop passes on it.
      LPM.deleteLoopFromQueue(L);
    }
    ++NumExtracted;
  }

  return Changed;
}