blob: 2fc0771f1525d9d52d7d08358212f8c85be3f03c [file] [log] [blame]
Adam Nemet215746b2015-07-10 18:55:13 +00001//===- 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
23using namespace llvm;
24
Adam Nemet0a674402015-07-28 05:01:53 +000025LoopVersioning::LoopVersioning(
26 SmallVector<RuntimePointerChecking::PointerCheck, 4> Checks,
Adam Nemet0bc06872015-08-10 23:05:31 +000027 const LoopAccessInfo &LAI, Loop *L, LoopInfo *LI, DominatorTree *DT)
Adam Nemet6b6082d2015-08-03 23:32:57 +000028 : VersionedLoop(L), NonVersionedLoop(nullptr), Checks(std::move(Checks)),
29 LAI(LAI), LI(LI), DT(DT) {
Adam Nemet215746b2015-07-10 18:55:13 +000030 assert(L->getExitBlock() && "No single exit block");
31 assert(L->getLoopPreheader() && "No preheader");
32}
33
Adam Nemet215746b2015-07-10 18:55:13 +000034void LoopVersioning::versionLoop(Pass *P) {
35 Instruction *FirstCheckInst;
36 Instruction *MemRuntimeCheck;
37 // Add the memcheck in the original preheader (this is empty initially).
38 BasicBlock *MemCheckBB = VersionedLoop->getLoopPreheader();
39 std::tie(FirstCheckInst, MemRuntimeCheck) =
Adam Nemet0a674402015-07-28 05:01:53 +000040 LAI.addRuntimeCheck(MemCheckBB->getTerminator(), Checks);
Adam Nemet215746b2015-07-10 18:55:13 +000041 assert(MemRuntimeCheck && "called even though needsAnyChecking = false");
42
43 // Rename the block to make the IR more readable.
44 MemCheckBB->setName(VersionedLoop->getHeader()->getName() + ".lver.memcheck");
45
46 // Create empty preheader for the loop (and after cloning for the
47 // non-versioned loop).
48 BasicBlock *PH = SplitBlock(MemCheckBB, MemCheckBB->getTerminator(), DT, LI);
49 PH->setName(VersionedLoop->getHeader()->getName() + ".ph");
50
51 // Clone the loop including the preheader.
52 //
53 // FIXME: This does not currently preserve SimplifyLoop because the exit
54 // block is a join between the two loops.
55 SmallVector<BasicBlock *, 8> NonVersionedLoopBlocks;
56 NonVersionedLoop =
57 cloneLoopWithPreheader(PH, MemCheckBB, VersionedLoop, VMap, ".lver.orig",
58 LI, DT, NonVersionedLoopBlocks);
59 remapInstructionsInBlocks(NonVersionedLoopBlocks, VMap);
60
61 // Insert the conditional branch based on the result of the memchecks.
62 Instruction *OrigTerm = MemCheckBB->getTerminator();
63 BranchInst::Create(NonVersionedLoop->getLoopPreheader(),
64 VersionedLoop->getLoopPreheader(), MemRuntimeCheck,
65 OrigTerm);
66 OrigTerm->eraseFromParent();
67
68 // The loops merge in the original exit block. This is now dominated by the
69 // memchecking block.
70 DT->changeImmediateDominator(VersionedLoop->getExitBlock(), MemCheckBB);
71}
72
73void LoopVersioning::addPHINodes(
74 const SmallVectorImpl<Instruction *> &DefsUsedOutside) {
75 BasicBlock *PHIBlock = VersionedLoop->getExitBlock();
76 assert(PHIBlock && "No single successor to loop exit block");
77
78 for (auto *Inst : DefsUsedOutside) {
79 auto *NonVersionedLoopInst = cast<Instruction>(VMap[Inst]);
80 PHINode *PN;
81
82 // First see if we have a single-operand PHI with the value defined by the
83 // original loop.
84 for (auto I = PHIBlock->begin(); (PN = dyn_cast<PHINode>(I)); ++I) {
85 assert(PN->getNumOperands() == 1 &&
86 "Exit block should only have on predecessor");
87 if (PN->getIncomingValue(0) == Inst)
88 break;
89 }
90 // If not create it.
91 if (!PN) {
92 PN = PHINode::Create(Inst->getType(), 2, Inst->getName() + ".lver",
93 PHIBlock->begin());
94 for (auto *User : Inst->users())
95 if (!VersionedLoop->contains(cast<Instruction>(User)->getParent()))
96 User->replaceUsesOfWith(Inst, PN);
97 PN->addIncoming(Inst, VersionedLoop->getExitingBlock());
98 }
99 // Add the new incoming value from the non-versioned loop.
100 PN->addIncoming(NonVersionedLoopInst, NonVersionedLoop->getExitingBlock());
101 }
102}