1 //===- LoopVersioning.cpp - Utility to version a loop ---------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file defines a utility class to perform loop versioning. The versioned 11 // loop speculates that otherwise may-aliasing memory accesses don't overlap and 12 // emits checks to prove this. 13 // 14 //===----------------------------------------------------------------------===// 15 16 #include "llvm/Analysis/LoopAccessAnalysis.h" 17 #include "llvm/Analysis/LoopInfo.h" 18 #include "llvm/IR/Dominators.h" 19 #include "llvm/Transforms/Utils/BasicBlockUtils.h" 20 #include "llvm/Transforms/Utils/Cloning.h" 21 #include "llvm/Transforms/Utils/LoopVersioning.h" 22 23 using namespace llvm; 24 25 LoopVersioning::LoopVersioning( 26 SmallVector<RuntimePointerChecking::PointerCheck, 4> Checks, 27 const LoopAccessInfo &LAI, Loop *L, LoopInfo *LI, DominatorTree *DT) 28 : VersionedLoop(L), NonVersionedLoop(nullptr), Checks(std::move(Checks)), 29 LAI(LAI), LI(LI), DT(DT) { 30 assert(L->getExitBlock() && "No single exit block"); 31 assert(L->getLoopPreheader() && "No preheader"); 32 } 33 34 LoopVersioning::LoopVersioning(const LoopAccessInfo &LAInfo, Loop *L, 35 LoopInfo *LI, DominatorTree *DT) 36 : VersionedLoop(L), NonVersionedLoop(nullptr), 37 Checks(LAInfo.getRuntimePointerChecking()->getChecks()), LAI(LAInfo), 38 LI(LI), DT(DT) { 39 assert(L->getExitBlock() && "No single exit block"); 40 assert(L->getLoopPreheader() && "No preheader"); 41 } 42 43 void LoopVersioning::versionLoop(Pass *P) { 44 Instruction *FirstCheckInst; 45 Instruction *MemRuntimeCheck; 46 // Add the memcheck in the original preheader (this is empty initially). 47 BasicBlock *MemCheckBB = VersionedLoop->getLoopPreheader(); 48 std::tie(FirstCheckInst, MemRuntimeCheck) = 49 LAI.addRuntimeChecks(MemCheckBB->getTerminator(), Checks); 50 assert(MemRuntimeCheck && "called even though needsAnyChecking = false"); 51 52 // Rename the block to make the IR more readable. 53 MemCheckBB->setName(VersionedLoop->getHeader()->getName() + ".lver.memcheck"); 54 55 // Create empty preheader for the loop (and after cloning for the 56 // non-versioned loop). 57 BasicBlock *PH = SplitBlock(MemCheckBB, MemCheckBB->getTerminator(), DT, LI); 58 PH->setName(VersionedLoop->getHeader()->getName() + ".ph"); 59 60 // Clone the loop including the preheader. 61 // 62 // FIXME: This does not currently preserve SimplifyLoop because the exit 63 // block is a join between the two loops. 64 SmallVector<BasicBlock *, 8> NonVersionedLoopBlocks; 65 NonVersionedLoop = 66 cloneLoopWithPreheader(PH, MemCheckBB, VersionedLoop, VMap, ".lver.orig", 67 LI, DT, NonVersionedLoopBlocks); 68 remapInstructionsInBlocks(NonVersionedLoopBlocks, VMap); 69 70 // Insert the conditional branch based on the result of the memchecks. 71 Instruction *OrigTerm = MemCheckBB->getTerminator(); 72 BranchInst::Create(NonVersionedLoop->getLoopPreheader(), 73 VersionedLoop->getLoopPreheader(), MemRuntimeCheck, 74 OrigTerm); 75 OrigTerm->eraseFromParent(); 76 77 // The loops merge in the original exit block. This is now dominated by the 78 // memchecking block. 79 DT->changeImmediateDominator(VersionedLoop->getExitBlock(), MemCheckBB); 80 } 81 82 void LoopVersioning::addPHINodes( 83 const SmallVectorImpl<Instruction *> &DefsUsedOutside) { 84 BasicBlock *PHIBlock = VersionedLoop->getExitBlock(); 85 assert(PHIBlock && "No single successor to loop exit block"); 86 87 for (auto *Inst : DefsUsedOutside) { 88 auto *NonVersionedLoopInst = cast<Instruction>(VMap[Inst]); 89 PHINode *PN; 90 91 // First see if we have a single-operand PHI with the value defined by the 92 // original loop. 93 for (auto I = PHIBlock->begin(); (PN = dyn_cast<PHINode>(I)); ++I) { 94 assert(PN->getNumOperands() == 1 && 95 "Exit block should only have on predecessor"); 96 if (PN->getIncomingValue(0) == Inst) 97 break; 98 } 99 // If not create it. 100 if (!PN) { 101 PN = PHINode::Create(Inst->getType(), 2, Inst->getName() + ".lver", 102 PHIBlock->begin()); 103 for (auto *User : Inst->users()) 104 if (!VersionedLoop->contains(cast<Instruction>(User)->getParent())) 105 User->replaceUsesOfWith(Inst, PN); 106 PN->addIncoming(Inst, VersionedLoop->getExitingBlock()); 107 } 108 // Add the new incoming value from the non-versioned loop. 109 PN->addIncoming(NonVersionedLoopInst, NonVersionedLoop->getExitingBlock()); 110 } 111 } 112