blob: 9303dee99dbcee05860cb22a5b1de38ad57c0e84 [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
Silviu Baranga9cd9a7e2015-12-09 16:06:28 +000060 const SCEVUnionPredicate &Pred = LAI.PSE.getUnionPredicate();
Silviu Baranga2910a4f2015-11-09 13:26:09 +000061 SCEVExpander Exp(*SE, RuntimeCheckBB->getModule()->getDataLayout(),
62 "scev.check");
63 SCEVRuntimeCheck =
64 Exp.expandCodeForPredicate(&Pred, RuntimeCheckBB->getTerminator());
65 auto *CI = dyn_cast<ConstantInt>(SCEVRuntimeCheck);
66
67 // Discard the SCEV runtime check if it is always true.
68 if (CI && CI->isZero())
69 SCEVRuntimeCheck = nullptr;
70
71 if (MemRuntimeCheck && SCEVRuntimeCheck) {
72 RuntimeCheck = BinaryOperator::Create(Instruction::Or, MemRuntimeCheck,
73 SCEVRuntimeCheck, "ldist.safe");
74 if (auto *I = dyn_cast<Instruction>(RuntimeCheck))
75 I->insertBefore(RuntimeCheckBB->getTerminator());
76 } else
77 RuntimeCheck = MemRuntimeCheck ? MemRuntimeCheck : SCEVRuntimeCheck;
78
79 assert(RuntimeCheck && "called even though we don't need "
80 "any runtime checks");
81
Adam Nemet215746b2015-07-10 18:55:13 +000082 // Rename the block to make the IR more readable.
Silviu Baranga2910a4f2015-11-09 13:26:09 +000083 RuntimeCheckBB->setName(VersionedLoop->getHeader()->getName() +
84 ".lver.check");
Adam Nemet215746b2015-07-10 18:55:13 +000085
86 // Create empty preheader for the loop (and after cloning for the
87 // non-versioned loop).
Silviu Baranga2910a4f2015-11-09 13:26:09 +000088 BasicBlock *PH =
89 SplitBlock(RuntimeCheckBB, RuntimeCheckBB->getTerminator(), DT, LI);
Adam Nemet215746b2015-07-10 18:55:13 +000090 PH->setName(VersionedLoop->getHeader()->getName() + ".ph");
91
92 // Clone the loop including the preheader.
93 //
94 // FIXME: This does not currently preserve SimplifyLoop because the exit
95 // block is a join between the two loops.
96 SmallVector<BasicBlock *, 8> NonVersionedLoopBlocks;
97 NonVersionedLoop =
Silviu Baranga2910a4f2015-11-09 13:26:09 +000098 cloneLoopWithPreheader(PH, RuntimeCheckBB, VersionedLoop, VMap,
99 ".lver.orig", LI, DT, NonVersionedLoopBlocks);
Adam Nemet215746b2015-07-10 18:55:13 +0000100 remapInstructionsInBlocks(NonVersionedLoopBlocks, VMap);
101
102 // Insert the conditional branch based on the result of the memchecks.
Silviu Baranga2910a4f2015-11-09 13:26:09 +0000103 Instruction *OrigTerm = RuntimeCheckBB->getTerminator();
Adam Nemet215746b2015-07-10 18:55:13 +0000104 BranchInst::Create(NonVersionedLoop->getLoopPreheader(),
Silviu Baranga2910a4f2015-11-09 13:26:09 +0000105 VersionedLoop->getLoopPreheader(), RuntimeCheck, OrigTerm);
Adam Nemet215746b2015-07-10 18:55:13 +0000106 OrigTerm->eraseFromParent();
107
108 // The loops merge in the original exit block. This is now dominated by the
109 // memchecking block.
Silviu Baranga2910a4f2015-11-09 13:26:09 +0000110 DT->changeImmediateDominator(VersionedLoop->getExitBlock(), RuntimeCheckBB);
Adam Nemete4813402015-08-20 17:22:29 +0000111
112 // Adds the necessary PHI nodes for the versioned loops based on the
113 // loop-defined values used outside of the loop.
114 addPHINodes(DefsUsedOutside);
Adam Nemet215746b2015-07-10 18:55:13 +0000115}
116
117void LoopVersioning::addPHINodes(
118 const SmallVectorImpl<Instruction *> &DefsUsedOutside) {
119 BasicBlock *PHIBlock = VersionedLoop->getExitBlock();
120 assert(PHIBlock && "No single successor to loop exit block");
121
122 for (auto *Inst : DefsUsedOutside) {
123 auto *NonVersionedLoopInst = cast<Instruction>(VMap[Inst]);
124 PHINode *PN;
125
126 // First see if we have a single-operand PHI with the value defined by the
127 // original loop.
128 for (auto I = PHIBlock->begin(); (PN = dyn_cast<PHINode>(I)); ++I) {
129 assert(PN->getNumOperands() == 1 &&
130 "Exit block should only have on predecessor");
131 if (PN->getIncomingValue(0) == Inst)
132 break;
133 }
134 // If not create it.
135 if (!PN) {
136 PN = PHINode::Create(Inst->getType(), 2, Inst->getName() + ".lver",
Duncan P. N. Exon Smith5b4c8372015-10-13 02:39:05 +0000137 &PHIBlock->front());
Adam Nemet215746b2015-07-10 18:55:13 +0000138 for (auto *User : Inst->users())
139 if (!VersionedLoop->contains(cast<Instruction>(User)->getParent()))
140 User->replaceUsesOfWith(Inst, PN);
141 PN->addIncoming(Inst, VersionedLoop->getExitingBlock());
142 }
143 // Add the new incoming value from the non-versioned loop.
144 PN->addIncoming(NonVersionedLoopInst, NonVersionedLoop->getExitingBlock());
145 }
146}
Adam Nemetd52ed842016-02-03 00:06:10 +0000147
148namespace {
149/// \brief Also expose this is a pass. Currently this is only used for
150/// unit-testing. It adds all memchecks necessary to remove all may-aliasing
151/// array accesses from the loop.
152class LoopVersioningPass : public FunctionPass {
153public:
154 LoopVersioningPass() : FunctionPass(ID) {
155 initializeLoopVersioningPassPass(*PassRegistry::getPassRegistry());
156 }
157
158 bool runOnFunction(Function &F) override {
159 auto *LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
160 auto *LAA = &getAnalysis<LoopAccessAnalysis>();
161 auto *DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
162 auto *SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE();
163
164 // Build up a worklist of inner-loops to version. This is necessary as the
165 // act of versioning a loop creates new loops and can invalidate iterators
166 // across the loops.
167 SmallVector<Loop *, 8> Worklist;
168
169 for (Loop *TopLevelLoop : *LI)
170 for (Loop *L : depth_first(TopLevelLoop))
171 // We only handle inner-most loops.
172 if (L->empty())
173 Worklist.push_back(L);
174
175 // Now walk the identified inner loops.
176 bool Changed = false;
177 for (Loop *L : Worklist) {
178 const LoopAccessInfo &LAI = LAA->getInfo(L, ValueToValueMap());
179 if (LAI.getNumRuntimePointerChecks() ||
180 !LAI.PSE.getUnionPredicate().isAlwaysTrue()) {
181 LoopVersioning LVer(LAI, L, LI, DT, SE);
182 LVer.versionLoop();
183 Changed = true;
184 }
185 }
186
187 return Changed;
188 }
189
190 void getAnalysisUsage(AnalysisUsage &AU) const override {
191 AU.addRequired<LoopInfoWrapperPass>();
192 AU.addPreserved<LoopInfoWrapperPass>();
193 AU.addRequired<LoopAccessAnalysis>();
194 AU.addRequired<DominatorTreeWrapperPass>();
195 AU.addPreserved<DominatorTreeWrapperPass>();
196 AU.addRequired<ScalarEvolutionWrapperPass>();
197 }
198
199 static char ID;
200};
201}
202
203#define LVER_OPTION "loop-versioning"
204#define DEBUG_TYPE LVER_OPTION
205
206char LoopVersioningPass::ID;
207static const char LVer_name[] = "Loop Versioning";
208
209INITIALIZE_PASS_BEGIN(LoopVersioningPass, LVER_OPTION, LVer_name, false, false)
210INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
211INITIALIZE_PASS_DEPENDENCY(LoopAccessAnalysis)
212INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
213INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass)
214INITIALIZE_PASS_END(LoopVersioningPass, LVER_OPTION, LVer_name, false, false)
215
216namespace llvm {
217FunctionPass *createLoopVersioningPass() {
218 return new LoopVersioningPass();
219}
220}