optimization - 在执行循环之前插入一条语句

标签 optimization compiler-construction llvm

在LLVM中间表示中,如何为循环插入一条指令,该指令将在执行该循环之前恰好执行一次?将指令插入到预标题不起作用,因为对于某些循环,预标题为NULL。

最佳答案

如果循环中没有预标题,则可以创建新的预标题。

这是一个示例http://www.cs.ucla.edu/classes/spring08/cs259/llvm-2.2/lib/Transforms/Utils/LoopSimplify.cpphttp://www.opensource.apple.com/source/clang/clang-23/clang/lib/Transforms/Utils/LoopSimplify.cpp(找到函数InsertPreheaderForLoop并对其进行调用)

/// InsertPreheaderForLoop - Once we discover that a loop doesn't have a
/// preheader, this method is called to insert one.  This method has two phases:
/// preheader insertion and analysis updating.
///
void LoopSimplify::InsertPreheaderForLoop(Loop *L) {
  BasicBlock *Header = L->getHeader();

  // Compute the set of predecessors of the loop that are not in the loop.
  std::vector<BasicBlock*> OutsideBlocks;
  for (pred_iterator PI = pred_begin(Header), PE = pred_end(Header);
       PI != PE; ++PI)
    if (!L->contains(*PI))           // Coming in from outside the loop?
      OutsideBlocks.push_back(*PI);  // Keep track of it...

  // Split out the loop pre-header.
  BasicBlock *NewBB =
    SplitBlockPredecessors(Header, ".preheader", OutsideBlocks);


  //===--------------------------------------------------------------------===//
  //  Update analysis results now that we have performed the transformation
  //

  // We know that we have loop information to update... update it now.
  if (Loop *Parent = L->getParentLoop())
    Parent->addBasicBlockToLoop(NewBB, LI->getBase());

  DT->splitBlock(NewBB);
  if (DominanceFrontier *DF = getAnalysisToUpdate<DominanceFrontier>())
    DF->splitBlock(NewBB);

  // Make sure that NewBB is put someplace intelligent, which doesn't mess up
  // code layout too horribly.
  PlaceSplitBlockCarefully(NewBB, OutsideBlocks, L);
}

关于optimization - 在执行循环之前插入一条语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5774428/

相关文章:

iphone - 有没有办法在 Xcode 4.1 中使用 LLVM 3?

llvm - 如何在 LLVM 中将 Constant* 转换为 Value*?

optimization - Haskell 中的二次规划

optimization - Scala "primitive"优化

parsing - 为什么 %prec 在这个 Bison 语法中没有效果?

c# - 内联属性初始化和尾随逗号

llvm - 使用 llvm::dyn_cast 进行转换导致错误

multithreading - 多线程对 IO-bound 操作有意义吗?

performance - 性能总是重要的吗?

javascript - 检测当前所在的元素是否是元素的最小儿子/孙子/孙孙子