blob: 1aeffb79b194b13e15789f40a10e46486b5a0fe1 [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
David Blaikie94c83372015-10-26 18:40:56 +000016#include "llvm/Transforms/Utils/LoopVersioning.h"
Adam Nemet215746b2015-07-10 18:55:13 +000017#include "llvm/Analysis/LoopAccessAnalysis.h"
18#include "llvm/Analysis/LoopInfo.h"
Silviu Baranga2910a4f2015-11-09 13:26:09 +000019#include "llvm/Analysis/ScalarEvolutionExpander.h"
Adam Nemet215746b2015-07-10 18:55:13 +000020#include "llvm/IR/Dominators.h"
21#include "llvm/Transforms/Utils/BasicBlockUtils.h"
22#include "llvm/Transforms/Utils/Cloning.h"
Adam Nemet215746b2015-07-10 18:55:13 +000023
24using namespace llvm;
25
Silviu Baranga2910a4f2015-11-09 13:26:09 +000026LoopVersioning::LoopVersioning(const LoopAccessInfo &LAI, Loop *L, LoopInfo *LI,
27 DominatorTree *DT, ScalarEvolution *SE,
28 bool UseLAIChecks)
29 : VersionedLoop(L), NonVersionedLoop(nullptr), LAI(LAI), LI(LI), DT(DT),
30 SE(SE) {
Adam Nemet215746b2015-07-10 18:55:13 +000031 assert(L->getExitBlock() && "No single exit block");
32 assert(L->getLoopPreheader() && "No preheader");
Silviu Baranga2910a4f2015-11-09 13:26:09 +000033 if (UseLAIChecks) {
34 setAliasChecks(LAI.getRuntimePointerChecking()->getChecks());
Silviu Baranga9cd9a7e2015-12-09 16:06:28 +000035 setSCEVChecks(LAI.PSE.getUnionPredicate());
Silviu Baranga2910a4f2015-11-09 13:26:09 +000036 }
Adam Nemet215746b2015-07-10 18:55:13 +000037}
38
Silviu Baranga2910a4f2015-11-09 13:26:09 +000039void LoopVersioning::setAliasChecks(
40 const SmallVector<RuntimePointerChecking::PointerCheck, 4> Checks) {
41 AliasChecks = std::move(Checks);
42}
43
44void LoopVersioning::setSCEVChecks(SCEVUnionPredicate Check) {
45 Preds = std::move(Check);
Adam Nemetdfaeb332015-08-12 16:51:19 +000046}
47
Adam Nemete4813402015-08-20 17:22:29 +000048void LoopVersioning::versionLoop(
49 const SmallVectorImpl<Instruction *> &DefsUsedOutside) {
Adam Nemet215746b2015-07-10 18:55:13 +000050 Instruction *FirstCheckInst;
51 Instruction *MemRuntimeCheck;
Silviu Baranga2910a4f2015-11-09 13:26:09 +000052 Value *SCEVRuntimeCheck;
53 Value *RuntimeCheck = nullptr;
54
Adam Nemet215746b2015-07-10 18:55:13 +000055 // Add the memcheck in the original preheader (this is empty initially).
Silviu Baranga2910a4f2015-11-09 13:26:09 +000056 BasicBlock *RuntimeCheckBB = VersionedLoop->getLoopPreheader();
Adam Nemet215746b2015-07-10 18:55:13 +000057 std::tie(FirstCheckInst, MemRuntimeCheck) =
Silviu Baranga2910a4f2015-11-09 13:26:09 +000058 LAI.addRuntimeChecks(RuntimeCheckBB->getTerminator(), AliasChecks);
Adam Nemet215746b2015-07-10 18:55:13 +000059 assert(MemRuntimeCheck && "called even though needsAnyChecking = false");
60
Silviu Baranga9cd9a7e2015-12-09 16:06:28 +000061 const SCEVUnionPredicate &Pred = LAI.PSE.getUnionPredicate();
Silviu Baranga2910a4f2015-11-09 13:26:09 +000062 SCEVExpander Exp(*SE, RuntimeCheckBB->getModule()->getDataLayout(),
63 "scev.check");
64 SCEVRuntimeCheck =
65 Exp.expandCodeForPredicate(&Pred, RuntimeCheckBB->getTerminator());
66 auto *CI = dyn_cast<ConstantInt>(SCEVRuntimeCheck);
67
68 // Discard the SCEV runtime check if it is always true.
69 if (CI && CI->isZero())
70 SCEVRuntimeCheck = nullptr;
71
72 if (MemRuntimeCheck && SCEVRuntimeCheck) {
73 RuntimeCheck = BinaryOperator::Create(Instruction::Or, MemRuntimeCheck,
74 SCEVRuntimeCheck, "ldist.safe");
75 if (auto *I = dyn_cast<Instruction>(RuntimeCheck))
76 I->insertBefore(RuntimeCheckBB->getTerminator());
77 } else
78 RuntimeCheck = MemRuntimeCheck ? MemRuntimeCheck : SCEVRuntimeCheck;
79
80 assert(RuntimeCheck && "called even though we don't need "
81 "any runtime checks");
82
Adam Nemet215746b2015-07-10 18:55:13 +000083 // Rename the block to make the IR more readable.
Silviu Baranga2910a4f2015-11-09 13:26:09 +000084 RuntimeCheckBB->setName(VersionedLoop->getHeader()->getName() +
85 ".lver.check");
Adam Nemet215746b2015-07-10 18:55:13 +000086
87 // Create empty preheader for the loop (and after cloning for the
88 // non-versioned loop).
Silviu Baranga2910a4f2015-11-09 13:26:09 +000089 BasicBlock *PH =
90 SplitBlock(RuntimeCheckBB, RuntimeCheckBB->getTerminator(), DT, LI);
Adam Nemet215746b2015-07-10 18:55:13 +000091 PH->setName(VersionedLoop->getHeader()->getName() + ".ph");
92
93 // Clone the loop including the preheader.
94 //
95 // FIXME: This does not currently preserve SimplifyLoop because the exit
96 // block is a join between the two loops.
97 SmallVector<BasicBlock *, 8> NonVersionedLoopBlocks;
98 NonVersionedLoop =
Silviu Baranga2910a4f2015-11-09 13:26:09 +000099 cloneLoopWithPreheader(PH, RuntimeCheckBB, VersionedLoop, VMap,
100 ".lver.orig", LI, DT, NonVersionedLoopBlocks);
Adam Nemet215746b2015-07-10 18:55:13 +0000101 remapInstructionsInBlocks(NonVersionedLoopBlocks, VMap);
102
103 // Insert the conditional branch based on the result of the memchecks.
Silviu Baranga2910a4f2015-11-09 13:26:09 +0000104 Instruction *OrigTerm = RuntimeCheckBB->getTerminator();
Adam Nemet215746b2015-07-10 18:55:13 +0000105 BranchInst::Create(NonVersionedLoop->getLoopPreheader(),
Silviu Baranga2910a4f2015-11-09 13:26:09 +0000106 VersionedLoop->getLoopPreheader(), RuntimeCheck, OrigTerm);
Adam Nemet215746b2015-07-10 18:55:13 +0000107 OrigTerm->eraseFromParent();
108
109 // The loops merge in the original exit block. This is now dominated by the
110 // memchecking block.
Silviu Baranga2910a4f2015-11-09 13:26:09 +0000111 DT->changeImmediateDominator(VersionedLoop->getExitBlock(), RuntimeCheckBB);
Adam Nemete4813402015-08-20 17:22:29 +0000112
113 // Adds the necessary PHI nodes for the versioned loops based on the
114 // loop-defined values used outside of the loop.
115 addPHINodes(DefsUsedOutside);
Adam Nemet215746b2015-07-10 18:55:13 +0000116}
117
118void LoopVersioning::addPHINodes(
119 const SmallVectorImpl<Instruction *> &DefsUsedOutside) {
120 BasicBlock *PHIBlock = VersionedLoop->getExitBlock();
121 assert(PHIBlock && "No single successor to loop exit block");
122
123 for (auto *Inst : DefsUsedOutside) {
124 auto *NonVersionedLoopInst = cast<Instruction>(VMap[Inst]);
125 PHINode *PN;
126
127 // First see if we have a single-operand PHI with the value defined by the
128 // original loop.
129 for (auto I = PHIBlock->begin(); (PN = dyn_cast<PHINode>(I)); ++I) {
130 assert(PN->getNumOperands() == 1 &&
131 "Exit block should only have on predecessor");
132 if (PN->getIncomingValue(0) == Inst)
133 break;
134 }
135 // If not create it.
136 if (!PN) {
137 PN = PHINode::Create(Inst->getType(), 2, Inst->getName() + ".lver",
Duncan P. N. Exon Smith5b4c8372015-10-13 02:39:05 +0000138 &PHIBlock->front());
Adam Nemet215746b2015-07-10 18:55:13 +0000139 for (auto *User : Inst->users())
140 if (!VersionedLoop->contains(cast<Instruction>(User)->getParent()))
141 User->replaceUsesOfWith(Inst, PN);
142 PN->addIncoming(Inst, VersionedLoop->getExitingBlock());
143 }
144 // Add the new incoming value from the non-versioned loop.
145 PN->addIncoming(NonVersionedLoopInst, NonVersionedLoop->getExitingBlock());
146 }
147}
Adam Nemetd52ed842016-02-03 00:06:10 +0000148
149namespace {
150/// \brief Also expose this is a pass. Currently this is only used for
151/// unit-testing. It adds all memchecks necessary to remove all may-aliasing
152/// array accesses from the loop.
153class LoopVersioningPass : public FunctionPass {
154public:
155 LoopVersioningPass() : FunctionPass(ID) {
156 initializeLoopVersioningPassPass(*PassRegistry::getPassRegistry());
157 }
158
159 bool runOnFunction(Function &F) override {
160 auto *LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
161 auto *LAA = &getAnalysis<LoopAccessAnalysis>();
162 auto *DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
163 auto *SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE();
164
165 // Build up a worklist of inner-loops to version. This is necessary as the
166 // act of versioning a loop creates new loops and can invalidate iterators
167 // across the loops.
168 SmallVector<Loop *, 8> Worklist;
169
170 for (Loop *TopLevelLoop : *LI)
171 for (Loop *L : depth_first(TopLevelLoop))
172 // We only handle inner-most loops.
173 if (L->empty())
174 Worklist.push_back(L);
175
176 // Now walk the identified inner loops.
177 bool Changed = false;
178 for (Loop *L : Worklist) {
179 const LoopAccessInfo &LAI = LAA->getInfo(L, ValueToValueMap());
180 if (LAI.getNumRuntimePointerChecks() ||
181 !LAI.PSE.getUnionPredicate().isAlwaysTrue()) {
182 LoopVersioning LVer(LAI, L, LI, DT, SE);
183 LVer.versionLoop();
184 Changed = true;
185 }
186 }
187
188 return Changed;
189 }
190
191 void getAnalysisUsage(AnalysisUsage &AU) const override {
192 AU.addRequired<LoopInfoWrapperPass>();
193 AU.addPreserved<LoopInfoWrapperPass>();
194 AU.addRequired<LoopAccessAnalysis>();
195 AU.addRequired<DominatorTreeWrapperPass>();
196 AU.addPreserved<DominatorTreeWrapperPass>();
197 AU.addRequired<ScalarEvolutionWrapperPass>();
198 }
199
200 static char ID;
201};
202}
203
204#define LVER_OPTION "loop-versioning"
205#define DEBUG_TYPE LVER_OPTION
206
207char LoopVersioningPass::ID;
208static const char LVer_name[] = "Loop Versioning";
209
210INITIALIZE_PASS_BEGIN(LoopVersioningPass, LVER_OPTION, LVer_name, false, false)
211INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
212INITIALIZE_PASS_DEPENDENCY(LoopAccessAnalysis)
213INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
214INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass)
215INITIALIZE_PASS_END(LoopVersioningPass, LVER_OPTION, LVer_name, false, false)
216
217namespace llvm {
218FunctionPass *createLoopVersioningPass() {
219 return new LoopVersioningPass();
220}
221}