1 //===- LoopVersioning.cpp - Utility to version a loop ---------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
14 //===----------------------------------------------------------------------===//
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"
25 LoopVersioning::LoopVersioning(
26 SmallVector<RuntimePointerChecking::PointerCheck, 4> Checks,
27 const LoopAccessInfo &LAI, Loop *L, LoopInfo *LI, DominatorTree *DT,
28 const SmallVector<int, 8> *PtrToPartition)
29 : VersionedLoop(L), NonVersionedLoop(nullptr),
30 PtrToPartition(PtrToPartition), Checks(std::move(Checks)), LAI(LAI),
32 assert(L->getExitBlock() && "No single exit block");
33 assert(L->getLoopPreheader() && "No preheader");
36 bool LoopVersioning::needsRuntimeChecks() const {
37 return LAI.getRuntimePointerChecking()->needsAnyChecking(PtrToPartition);
40 void LoopVersioning::versionLoop(Pass *P) {
41 Instruction *FirstCheckInst;
42 Instruction *MemRuntimeCheck;
43 // Add the memcheck in the original preheader (this is empty initially).
44 BasicBlock *MemCheckBB = VersionedLoop->getLoopPreheader();
45 std::tie(FirstCheckInst, MemRuntimeCheck) =
46 LAI.addRuntimeCheck(MemCheckBB->getTerminator(), Checks);
47 assert(MemRuntimeCheck && "called even though needsAnyChecking = false");
49 // Rename the block to make the IR more readable.
50 MemCheckBB->setName(VersionedLoop->getHeader()->getName() + ".lver.memcheck");
52 // Create empty preheader for the loop (and after cloning for the
53 // non-versioned loop).
54 BasicBlock *PH = SplitBlock(MemCheckBB, MemCheckBB->getTerminator(), DT, LI);
55 PH->setName(VersionedLoop->getHeader()->getName() + ".ph");
57 // Clone the loop including the preheader.
59 // FIXME: This does not currently preserve SimplifyLoop because the exit
60 // block is a join between the two loops.
61 SmallVector<BasicBlock *, 8> NonVersionedLoopBlocks;
63 cloneLoopWithPreheader(PH, MemCheckBB, VersionedLoop, VMap, ".lver.orig",
64 LI, DT, NonVersionedLoopBlocks);
65 remapInstructionsInBlocks(NonVersionedLoopBlocks, VMap);
67 // Insert the conditional branch based on the result of the memchecks.
68 Instruction *OrigTerm = MemCheckBB->getTerminator();
69 BranchInst::Create(NonVersionedLoop->getLoopPreheader(),
70 VersionedLoop->getLoopPreheader(), MemRuntimeCheck,
72 OrigTerm->eraseFromParent();
74 // The loops merge in the original exit block. This is now dominated by the
76 DT->changeImmediateDominator(VersionedLoop->getExitBlock(), MemCheckBB);
79 void LoopVersioning::addPHINodes(
80 const SmallVectorImpl<Instruction *> &DefsUsedOutside) {
81 BasicBlock *PHIBlock = VersionedLoop->getExitBlock();
82 assert(PHIBlock && "No single successor to loop exit block");
84 for (auto *Inst : DefsUsedOutside) {
85 auto *NonVersionedLoopInst = cast<Instruction>(VMap[Inst]);
88 // First see if we have a single-operand PHI with the value defined by the
90 for (auto I = PHIBlock->begin(); (PN = dyn_cast<PHINode>(I)); ++I) {
91 assert(PN->getNumOperands() == 1 &&
92 "Exit block should only have on predecessor");
93 if (PN->getIncomingValue(0) == Inst)
98 PN = PHINode::Create(Inst->getType(), 2, Inst->getName() + ".lver",
100 for (auto *User : Inst->users())
101 if (!VersionedLoop->contains(cast<Instruction>(User)->getParent()))
102 User->replaceUsesOfWith(Inst, PN);
103 PN->addIncoming(Inst, VersionedLoop->getExitingBlock());
105 // Add the new incoming value from the non-versioned loop.
106 PN->addIncoming(NonVersionedLoopInst, NonVersionedLoop->getExitingBlock());