当前位置: 首页>>代码示例>>C++>>正文


C++ ScalarEvolution::getBackedgeTakenCount方法代码示例

本文整理汇总了C++中ScalarEvolution::getBackedgeTakenCount方法的典型用法代码示例。如果您正苦于以下问题:C++ ScalarEvolution::getBackedgeTakenCount方法的具体用法?C++ ScalarEvolution::getBackedgeTakenCount怎么用?C++ ScalarEvolution::getBackedgeTakenCount使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在ScalarEvolution的用法示例。


在下文中一共展示了ScalarEvolution::getBackedgeTakenCount方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: UnrollRuntimeLoopProlog

/// Insert code in the prolog code when unrolling a loop with a
/// run-time trip-count.
///
/// This method assumes that the loop unroll factor is total number
/// of loop bodes in the loop after unrolling. (Some folks refer
/// to the unroll factor as the number of *extra* copies added).
/// We assume also that the loop unroll factor is a power-of-two. So, after
/// unrolling the loop, the number of loop bodies executed is 2,
/// 4, 8, etc.  Note - LLVM converts the if-then-sequence to a switch
/// instruction in SimplifyCFG.cpp.  Then, the backend decides how code for
/// the switch instruction is generated.
///
///    extraiters = tripcount % loopfactor
///    if (extraiters == 0) jump Loop:
///    if (extraiters == loopfactor) jump L1
///    if (extraiters == loopfactor-1) jump L2
///    ...
///    L1:  LoopBody;
///    L2:  LoopBody;
///    ...
///    if tripcount < loopfactor jump End
///    Loop:
///    ...
///    End:
///
bool llvm::UnrollRuntimeLoopProlog(Loop *L, unsigned Count, LoopInfo *LI,
                                   LPPassManager *LPM) {
  // for now, only unroll loops that contain a single exit
  if (!L->getExitingBlock())
    return false;

  // Make sure the loop is in canonical form, and there is a single
  // exit block only.
  if (!L->isLoopSimplifyForm() || !L->getUniqueExitBlock())
    return false;

  // Use Scalar Evolution to compute the trip count.  This allows more
  // loops to be unrolled than relying on induction var simplification
  if (!LPM)
    return false;
  ScalarEvolution *SE = LPM->getAnalysisIfAvailable<ScalarEvolution>();
  if (!SE)
    return false;

  // Only unroll loops with a computable trip count and the trip count needs
  // to be an int value (allowing a pointer type is a TODO item)
  const SCEV *BECount = SE->getBackedgeTakenCount(L);
  if (isa<SCEVCouldNotCompute>(BECount) || !BECount->getType()->isIntegerTy())
    return false;

  // Add 1 since the backedge count doesn't include the first loop iteration
  const SCEV *TripCountSC =
    SE->getAddExpr(BECount, SE->getConstant(BECount->getType(), 1));
  if (isa<SCEVCouldNotCompute>(TripCountSC))
    return false;

  // We only handle cases when the unroll factor is a power of 2.
  // Count is the loop unroll factor, the number of extra copies added + 1.
  if ((Count & (Count-1)) != 0)
    return false;

  // If this loop is nested, then the loop unroller changes the code in
  // parent loop, so the Scalar Evolution pass needs to be run again
  if (Loop *ParentLoop = L->getParentLoop())
    SE->forgetLoop(ParentLoop);

  BasicBlock *PH = L->getLoopPreheader();
  BasicBlock *Header = L->getHeader();
  BasicBlock *Latch = L->getLoopLatch();
  // It helps to splits the original preheader twice, one for the end of the
  // prolog code and one for a new loop preheader
  BasicBlock *PEnd = SplitEdge(PH, Header, LPM->getAsPass());
  BasicBlock *NewPH = SplitBlock(PEnd, PEnd->getTerminator(), LPM->getAsPass());
  BranchInst *PreHeaderBR = cast<BranchInst>(PH->getTerminator());

  // Compute the number of extra iterations required, which is:
  //  extra iterations = run-time trip count % (loop unroll factor + 1)
  SCEVExpander Expander(*SE, "loop-unroll");
  Value *TripCount = Expander.expandCodeFor(TripCountSC, TripCountSC->getType(),
                                            PreHeaderBR);
  Type *CountTy = TripCount->getType();
  BinaryOperator *ModVal =
    BinaryOperator::CreateURem(TripCount,
                               ConstantInt::get(CountTy, Count),
                               "xtraiter");
  ModVal->insertBefore(PreHeaderBR);

  // Check if for no extra iterations, then jump to unrolled loop
  Value *BranchVal = new ICmpInst(PreHeaderBR,
                                  ICmpInst::ICMP_NE, ModVal,
                                  ConstantInt::get(CountTy, 0), "lcmp");
  // Branch to either the extra iterations or the unrolled loop
  // We will fix up the true branch label when adding loop body copies
  BranchInst::Create(PEnd, PEnd, BranchVal, PreHeaderBR);
  assert(PreHeaderBR->isUnconditional() &&
         PreHeaderBR->getSuccessor(0) == PEnd &&
         "CFG edges in Preheader are not correct");
  PreHeaderBR->eraseFromParent();

  ValueToValueMapTy LVMap;
//.........这里部分代码省略.........
开发者ID:compnerd,项目名称:llvm,代码行数:101,代码来源:LoopUnrollRuntime.cpp

示例2: UnrollRuntimeLoopProlog

/// Insert code in the prolog code when unrolling a loop with a
/// run-time trip-count.
///
/// This method assumes that the loop unroll factor is total number
/// of loop bodes in the loop after unrolling. (Some folks refer
/// to the unroll factor as the number of *extra* copies added).
/// We assume also that the loop unroll factor is a power-of-two. So, after
/// unrolling the loop, the number of loop bodies executed is 2,
/// 4, 8, etc.  Note - LLVM converts the if-then-sequence to a switch
/// instruction in SimplifyCFG.cpp.  Then, the backend decides how code for
/// the switch instruction is generated.
///
///        extraiters = tripcount % loopfactor
///        if (extraiters == 0) jump Loop:
///        else jump Prol
/// Prol:  LoopBody;
///        extraiters -= 1                 // Omitted if unroll factor is 2.
///        if (extraiters != 0) jump Prol: // Omitted if unroll factor is 2.
///        if (tripcount < loopfactor) jump End
/// Loop:
/// ...
/// End:
///
bool llvm::UnrollRuntimeLoopProlog(Loop *L, unsigned Count,
                                   bool AllowExpensiveTripCount, LoopInfo *LI,
                                   LPPassManager *LPM) {
    // for now, only unroll loops that contain a single exit
    if (!L->getExitingBlock())
        return false;

    // Make sure the loop is in canonical form, and there is a single
    // exit block only.
    if (!L->isLoopSimplifyForm() || !L->getUniqueExitBlock())
        return false;

    // Use Scalar Evolution to compute the trip count.  This allows more
    // loops to be unrolled than relying on induction var simplification
    if (!LPM)
        return false;
    ScalarEvolution *SE = LPM->getAnalysisIfAvailable<ScalarEvolution>();
    if (!SE)
        return false;

    // Only unroll loops with a computable trip count and the trip count needs
    // to be an int value (allowing a pointer type is a TODO item)
    const SCEV *BECountSC = SE->getBackedgeTakenCount(L);
    if (isa<SCEVCouldNotCompute>(BECountSC) ||
            !BECountSC->getType()->isIntegerTy())
        return false;

    unsigned BEWidth = cast<IntegerType>(BECountSC->getType())->getBitWidth();

    // Add 1 since the backedge count doesn't include the first loop iteration
    const SCEV *TripCountSC =
        SE->getAddExpr(BECountSC, SE->getConstant(BECountSC->getType(), 1));
    if (isa<SCEVCouldNotCompute>(TripCountSC))
        return false;

    BasicBlock *Header = L->getHeader();
    const DataLayout &DL = Header->getModule()->getDataLayout();
    SCEVExpander Expander(*SE, DL, "loop-unroll");
    if (!AllowExpensiveTripCount && Expander.isHighCostExpansion(TripCountSC, L))
        return false;

    // We only handle cases when the unroll factor is a power of 2.
    // Count is the loop unroll factor, the number of extra copies added + 1.
    if (!isPowerOf2_32(Count))
        return false;

    // This constraint lets us deal with an overflowing trip count easily; see the
    // comment on ModVal below.
    if (Log2_32(Count) > BEWidth)
        return false;

    // If this loop is nested, then the loop unroller changes the code in
    // parent loop, so the Scalar Evolution pass needs to be run again
    if (Loop *ParentLoop = L->getParentLoop())
        SE->forgetLoop(ParentLoop);

    // Grab analyses that we preserve.
    auto *DTWP = LPM->getAnalysisIfAvailable<DominatorTreeWrapperPass>();
    auto *DT = DTWP ? &DTWP->getDomTree() : nullptr;

    BasicBlock *PH = L->getLoopPreheader();
    BasicBlock *Latch = L->getLoopLatch();
    // It helps to splits the original preheader twice, one for the end of the
    // prolog code and one for a new loop preheader
    BasicBlock *PEnd = SplitEdge(PH, Header, DT, LI);
    BasicBlock *NewPH = SplitBlock(PEnd, PEnd->getTerminator(), DT, LI);
    BranchInst *PreHeaderBR = cast<BranchInst>(PH->getTerminator());

    // Compute the number of extra iterations required, which is:
    //  extra iterations = run-time trip count % (loop unroll factor + 1)
    Value *TripCount = Expander.expandCodeFor(TripCountSC, TripCountSC->getType(),
                       PreHeaderBR);
    Value *BECount = Expander.expandCodeFor(BECountSC, BECountSC->getType(),
                                            PreHeaderBR);

    IRBuilder<> B(PreHeaderBR);
    Value *ModVal = B.CreateAnd(TripCount, Count - 1, "xtraiter");
//.........这里部分代码省略.........
开发者ID:cyrilmagsuci,项目名称:freebsd,代码行数:101,代码来源:LoopUnrollRuntime.cpp


注:本文中的ScalarEvolution::getBackedgeTakenCount方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。