1 //===- LoopVersioning.cpp - Utility to version a loop ---------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines a utility class to perform loop versioning.  The versioned
10 // loop speculates that otherwise may-aliasing memory accesses don't overlap and
11 // emits checks to prove this.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #include "llvm/Transforms/Utils/LoopVersioning.h"
16 #include "llvm/ADT/ArrayRef.h"
17 #include "llvm/Analysis/LoopAccessAnalysis.h"
18 #include "llvm/Analysis/LoopInfo.h"
19 #include "llvm/Analysis/MemorySSA.h"
20 #include "llvm/Analysis/ScalarEvolution.h"
21 #include "llvm/Analysis/TargetLibraryInfo.h"
22 #include "llvm/IR/Dominators.h"
23 #include "llvm/IR/MDBuilder.h"
24 #include "llvm/IR/PassManager.h"
25 #include "llvm/InitializePasses.h"
26 #include "llvm/Support/CommandLine.h"
27 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
28 #include "llvm/Transforms/Utils/Cloning.h"
29 #include "llvm/Transforms/Utils/ScalarEvolutionExpander.h"
30 
31 using namespace llvm;
32 
33 static cl::opt<bool>
34     AnnotateNoAlias("loop-version-annotate-no-alias", cl::init(true),
35                     cl::Hidden,
36                     cl::desc("Add no-alias annotation for instructions that "
37                              "are disambiguated by memchecks"));
38 
39 LoopVersioning::LoopVersioning(const LoopAccessInfo &LAI,
40                                ArrayRef<RuntimePointerCheck> Checks, Loop *L,
41                                LoopInfo *LI, DominatorTree *DT,
42                                ScalarEvolution *SE)
43     : VersionedLoop(L), NonVersionedLoop(nullptr),
44       AliasChecks(Checks.begin(), Checks.end()),
45       Preds(LAI.getPSE().getUnionPredicate()), LAI(LAI), LI(LI), DT(DT),
46       SE(SE) {
47   assert(L->getExitBlock() && "No single exit block");
48   assert(L->isLoopSimplifyForm() && "Loop is not in loop-simplify form");
49 }
50 
51 void LoopVersioning::versionLoop(
52     const SmallVectorImpl<Instruction *> &DefsUsedOutside) {
53   Instruction *FirstCheckInst;
54   Instruction *MemRuntimeCheck;
55   Value *SCEVRuntimeCheck;
56   Value *RuntimeCheck = nullptr;
57 
58   // Add the memcheck in the original preheader (this is empty initially).
59   BasicBlock *RuntimeCheckBB = VersionedLoop->getLoopPreheader();
60   const auto &RtPtrChecking = *LAI.getRuntimePointerChecking();
61   std::tie(FirstCheckInst, MemRuntimeCheck) =
62       addRuntimeChecks(RuntimeCheckBB->getTerminator(), VersionedLoop,
63                        AliasChecks, RtPtrChecking.getSE());
64 
65   SCEVExpander Exp(*SE, RuntimeCheckBB->getModule()->getDataLayout(),
66                    "scev.check");
67   SCEVRuntimeCheck =
68       Exp.expandCodeForPredicate(&Preds, RuntimeCheckBB->getTerminator());
69   auto *CI = dyn_cast<ConstantInt>(SCEVRuntimeCheck);
70 
71   // Discard the SCEV runtime check if it is always true.
72   if (CI && CI->isZero())
73     SCEVRuntimeCheck = nullptr;
74 
75   if (MemRuntimeCheck && SCEVRuntimeCheck) {
76     RuntimeCheck = BinaryOperator::Create(Instruction::Or, MemRuntimeCheck,
77                                           SCEVRuntimeCheck, "lver.safe");
78     if (auto *I = dyn_cast<Instruction>(RuntimeCheck))
79       I->insertBefore(RuntimeCheckBB->getTerminator());
80   } else
81     RuntimeCheck = MemRuntimeCheck ? MemRuntimeCheck : SCEVRuntimeCheck;
82 
83   assert(RuntimeCheck && "called even though we don't need "
84                          "any runtime checks");
85 
86   // Rename the block to make the IR more readable.
87   RuntimeCheckBB->setName(VersionedLoop->getHeader()->getName() +
88                           ".lver.check");
89 
90   // Create empty preheader for the loop (and after cloning for the
91   // non-versioned loop).
92   BasicBlock *PH =
93       SplitBlock(RuntimeCheckBB, RuntimeCheckBB->getTerminator(), DT, LI,
94                  nullptr, VersionedLoop->getHeader()->getName() + ".ph");
95 
96   // Clone the loop including the preheader.
97   //
98   // FIXME: This does not currently preserve SimplifyLoop because the exit
99   // block is a join between the two loops.
100   SmallVector<BasicBlock *, 8> NonVersionedLoopBlocks;
101   NonVersionedLoop =
102       cloneLoopWithPreheader(PH, RuntimeCheckBB, VersionedLoop, VMap,
103                              ".lver.orig", LI, DT, NonVersionedLoopBlocks);
104   remapInstructionsInBlocks(NonVersionedLoopBlocks, VMap);
105 
106   // Insert the conditional branch based on the result of the memchecks.
107   Instruction *OrigTerm = RuntimeCheckBB->getTerminator();
108   BranchInst::Create(NonVersionedLoop->getLoopPreheader(),
109                      VersionedLoop->getLoopPreheader(), RuntimeCheck, OrigTerm);
110   OrigTerm->eraseFromParent();
111 
112   // The loops merge in the original exit block.  This is now dominated by the
113   // memchecking block.
114   DT->changeImmediateDominator(VersionedLoop->getExitBlock(), RuntimeCheckBB);
115 
116   // Adds the necessary PHI nodes for the versioned loops based on the
117   // loop-defined values used outside of the loop.
118   addPHINodes(DefsUsedOutside);
119   formDedicatedExitBlocks(NonVersionedLoop, DT, LI, nullptr, true);
120   formDedicatedExitBlocks(VersionedLoop, DT, LI, nullptr, true);
121   assert(NonVersionedLoop->isLoopSimplifyForm() &&
122          VersionedLoop->isLoopSimplifyForm() &&
123          "The versioned loops should be in simplify form.");
124 }
125 
126 void LoopVersioning::addPHINodes(
127     const SmallVectorImpl<Instruction *> &DefsUsedOutside) {
128   BasicBlock *PHIBlock = VersionedLoop->getExitBlock();
129   assert(PHIBlock && "No single successor to loop exit block");
130   PHINode *PN;
131 
132   // First add a single-operand PHI for each DefsUsedOutside if one does not
133   // exists yet.
134   for (auto *Inst : DefsUsedOutside) {
135     // See if we have a single-operand PHI with the value defined by the
136     // original loop.
137     for (auto I = PHIBlock->begin(); (PN = dyn_cast<PHINode>(I)); ++I) {
138       if (PN->getIncomingValue(0) == Inst)
139         break;
140     }
141     // If not create it.
142     if (!PN) {
143       PN = PHINode::Create(Inst->getType(), 2, Inst->getName() + ".lver",
144                            &PHIBlock->front());
145       SmallVector<User*, 8> UsersToUpdate;
146       for (User *U : Inst->users())
147         if (!VersionedLoop->contains(cast<Instruction>(U)->getParent()))
148           UsersToUpdate.push_back(U);
149       for (User *U : UsersToUpdate)
150         U->replaceUsesOfWith(Inst, PN);
151       PN->addIncoming(Inst, VersionedLoop->getExitingBlock());
152     }
153   }
154 
155   // Then for each PHI add the operand for the edge from the cloned loop.
156   for (auto I = PHIBlock->begin(); (PN = dyn_cast<PHINode>(I)); ++I) {
157     assert(PN->getNumOperands() == 1 &&
158            "Exit block should only have on predecessor");
159 
160     // If the definition was cloned used that otherwise use the same value.
161     Value *ClonedValue = PN->getIncomingValue(0);
162     auto Mapped = VMap.find(ClonedValue);
163     if (Mapped != VMap.end())
164       ClonedValue = Mapped->second;
165 
166     PN->addIncoming(ClonedValue, NonVersionedLoop->getExitingBlock());
167   }
168 }
169 
170 void LoopVersioning::prepareNoAliasMetadata() {
171   // We need to turn the no-alias relation between pointer checking groups into
172   // no-aliasing annotations between instructions.
173   //
174   // We accomplish this by mapping each pointer checking group (a set of
175   // pointers memchecked together) to an alias scope and then also mapping each
176   // group to the list of scopes it can't alias.
177 
178   const RuntimePointerChecking *RtPtrChecking = LAI.getRuntimePointerChecking();
179   LLVMContext &Context = VersionedLoop->getHeader()->getContext();
180 
181   // First allocate an aliasing scope for each pointer checking group.
182   //
183   // While traversing through the checking groups in the loop, also create a
184   // reverse map from pointers to the pointer checking group they were assigned
185   // to.
186   MDBuilder MDB(Context);
187   MDNode *Domain = MDB.createAnonymousAliasScopeDomain("LVerDomain");
188 
189   for (const auto &Group : RtPtrChecking->CheckingGroups) {
190     GroupToScope[&Group] = MDB.createAnonymousAliasScope(Domain);
191 
192     for (unsigned PtrIdx : Group.Members)
193       PtrToGroup[RtPtrChecking->getPointerInfo(PtrIdx).PointerValue] = &Group;
194   }
195 
196   // Go through the checks and for each pointer group, collect the scopes for
197   // each non-aliasing pointer group.
198   DenseMap<const RuntimeCheckingPtrGroup *, SmallVector<Metadata *, 4>>
199       GroupToNonAliasingScopes;
200 
201   for (const auto &Check : AliasChecks)
202     GroupToNonAliasingScopes[Check.first].push_back(GroupToScope[Check.second]);
203 
204   // Finally, transform the above to actually map to scope list which is what
205   // the metadata uses.
206 
207   for (auto Pair : GroupToNonAliasingScopes)
208     GroupToNonAliasingScopeList[Pair.first] = MDNode::get(Context, Pair.second);
209 }
210 
211 void LoopVersioning::annotateLoopWithNoAlias() {
212   if (!AnnotateNoAlias)
213     return;
214 
215   // First prepare the maps.
216   prepareNoAliasMetadata();
217 
218   // Add the scope and no-alias metadata to the instructions.
219   for (Instruction *I : LAI.getDepChecker().getMemoryInstructions()) {
220     annotateInstWithNoAlias(I);
221   }
222 }
223 
224 void LoopVersioning::annotateInstWithNoAlias(Instruction *VersionedInst,
225                                              const Instruction *OrigInst) {
226   if (!AnnotateNoAlias)
227     return;
228 
229   LLVMContext &Context = VersionedLoop->getHeader()->getContext();
230   const Value *Ptr = isa<LoadInst>(OrigInst)
231                          ? cast<LoadInst>(OrigInst)->getPointerOperand()
232                          : cast<StoreInst>(OrigInst)->getPointerOperand();
233 
234   // Find the group for the pointer and then add the scope metadata.
235   auto Group = PtrToGroup.find(Ptr);
236   if (Group != PtrToGroup.end()) {
237     VersionedInst->setMetadata(
238         LLVMContext::MD_alias_scope,
239         MDNode::concatenate(
240             VersionedInst->getMetadata(LLVMContext::MD_alias_scope),
241             MDNode::get(Context, GroupToScope[Group->second])));
242 
243     // Add the no-alias metadata.
244     auto NonAliasingScopeList = GroupToNonAliasingScopeList.find(Group->second);
245     if (NonAliasingScopeList != GroupToNonAliasingScopeList.end())
246       VersionedInst->setMetadata(
247           LLVMContext::MD_noalias,
248           MDNode::concatenate(
249               VersionedInst->getMetadata(LLVMContext::MD_noalias),
250               NonAliasingScopeList->second));
251   }
252 }
253 
254 namespace {
255 bool runImpl(LoopInfo *LI, function_ref<const LoopAccessInfo &(Loop &)> GetLAA,
256              DominatorTree *DT, ScalarEvolution *SE) {
257   // Build up a worklist of inner-loops to version. This is necessary as the
258   // act of versioning a loop creates new loops and can invalidate iterators
259   // across the loops.
260   SmallVector<Loop *, 8> Worklist;
261 
262   for (Loop *TopLevelLoop : *LI)
263     for (Loop *L : depth_first(TopLevelLoop))
264       // We only handle inner-most loops.
265       if (L->isInnermost())
266         Worklist.push_back(L);
267 
268   // Now walk the identified inner loops.
269   bool Changed = false;
270   for (Loop *L : Worklist) {
271     if (!L->isLoopSimplifyForm() || !L->isRotatedForm() ||
272         !L->getExitingBlock())
273       continue;
274     const LoopAccessInfo &LAI = GetLAA(*L);
275     if (!LAI.hasConvergentOp() &&
276         (LAI.getNumRuntimePointerChecks() ||
277          !LAI.getPSE().getUnionPredicate().isAlwaysTrue())) {
278       LoopVersioning LVer(LAI, LAI.getRuntimePointerChecking()->getChecks(), L,
279                           LI, DT, SE);
280       LVer.versionLoop();
281       LVer.annotateLoopWithNoAlias();
282       Changed = true;
283     }
284   }
285 
286   return Changed;
287 }
288 
289 /// Also expose this is a pass.  Currently this is only used for
290 /// unit-testing.  It adds all memchecks necessary to remove all may-aliasing
291 /// array accesses from the loop.
292 class LoopVersioningLegacyPass : public FunctionPass {
293 public:
294   LoopVersioningLegacyPass() : FunctionPass(ID) {
295     initializeLoopVersioningLegacyPassPass(*PassRegistry::getPassRegistry());
296   }
297 
298   bool runOnFunction(Function &F) override {
299     auto *LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
300     auto GetLAA = [&](Loop &L) -> const LoopAccessInfo & {
301       return getAnalysis<LoopAccessLegacyAnalysis>().getInfo(&L);
302     };
303 
304     auto *DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
305     auto *SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE();
306 
307     return runImpl(LI, GetLAA, DT, SE);
308   }
309 
310   void getAnalysisUsage(AnalysisUsage &AU) const override {
311     AU.addRequired<LoopInfoWrapperPass>();
312     AU.addPreserved<LoopInfoWrapperPass>();
313     AU.addRequired<LoopAccessLegacyAnalysis>();
314     AU.addRequired<DominatorTreeWrapperPass>();
315     AU.addPreserved<DominatorTreeWrapperPass>();
316     AU.addRequired<ScalarEvolutionWrapperPass>();
317   }
318 
319   static char ID;
320 };
321 }
322 
323 #define LVER_OPTION "loop-versioning"
324 #define DEBUG_TYPE LVER_OPTION
325 
326 char LoopVersioningLegacyPass::ID;
327 static const char LVer_name[] = "Loop Versioning";
328 
329 INITIALIZE_PASS_BEGIN(LoopVersioningLegacyPass, LVER_OPTION, LVer_name, false,
330                       false)
331 INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
332 INITIALIZE_PASS_DEPENDENCY(LoopAccessLegacyAnalysis)
333 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
334 INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass)
335 INITIALIZE_PASS_END(LoopVersioningLegacyPass, LVER_OPTION, LVer_name, false,
336                     false)
337 
338 namespace llvm {
339 FunctionPass *createLoopVersioningLegacyPass() {
340   return new LoopVersioningLegacyPass();
341 }
342 
343 PreservedAnalyses LoopVersioningPass::run(Function &F,
344                                           FunctionAnalysisManager &AM) {
345   auto &SE = AM.getResult<ScalarEvolutionAnalysis>(F);
346   auto &LI = AM.getResult<LoopAnalysis>(F);
347   auto &TTI = AM.getResult<TargetIRAnalysis>(F);
348   auto &DT = AM.getResult<DominatorTreeAnalysis>(F);
349   auto &TLI = AM.getResult<TargetLibraryAnalysis>(F);
350   auto &AA = AM.getResult<AAManager>(F);
351   auto &AC = AM.getResult<AssumptionAnalysis>(F);
352   MemorySSA *MSSA = EnableMSSALoopDependency
353                         ? &AM.getResult<MemorySSAAnalysis>(F).getMSSA()
354                         : nullptr;
355 
356   auto &LAM = AM.getResult<LoopAnalysisManagerFunctionProxy>(F).getManager();
357   auto GetLAA = [&](Loop &L) -> const LoopAccessInfo & {
358     LoopStandardAnalysisResults AR = {AA,  AC,  DT,      LI,  SE,
359                                       TLI, TTI, nullptr, MSSA};
360     return LAM.getResult<LoopAccessAnalysis>(L, AR);
361   };
362 
363   if (runImpl(&LI, GetLAA, &DT, &SE))
364     return PreservedAnalyses::none();
365   return PreservedAnalyses::all();
366 }
367 } // namespace llvm
368