2011-04-25 50 views
2

在LLVM中間表示中,如何爲循環插入一條指令,該指令將在該循環執行之前執行一次?將指令插入到預讀器不起作用,因爲對於某些循環,預讀器爲NULL。在執行循環之前插入一條語句

回答

4

如果循環沒有預覽器,您可以創建新的預讀器。

下面是一個例子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); 
} 
+0

太謝謝你了! – dalibocai 2011-04-28 01:22:54

相關問題