Adam Nemet | 215746b | 2015-07-10 18:55:13 +0000 | [diff] [blame] | 1 | //===- 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 Blaikie | 94c8337 | 2015-10-26 18:40:56 +0000 | [diff] [blame] | 16 | #include "llvm/Transforms/Utils/LoopVersioning.h" |
Adam Nemet | 215746b | 2015-07-10 18:55:13 +0000 | [diff] [blame] | 17 | #include "llvm/Analysis/LoopAccessAnalysis.h" |
| 18 | #include "llvm/Analysis/LoopInfo.h" |
Silviu Baranga | 2910a4f | 2015-11-09 13:26:09 +0000 | [diff] [blame] | 19 | #include "llvm/Analysis/ScalarEvolutionExpander.h" |
Adam Nemet | 215746b | 2015-07-10 18:55:13 +0000 | [diff] [blame] | 20 | #include "llvm/IR/Dominators.h" |
Adam Nemet | 5eccf07 | 2016-03-17 20:32:32 +0000 | [diff] [blame] | 21 | #include "llvm/IR/MDBuilder.h" |
Adam Nemet | 215746b | 2015-07-10 18:55:13 +0000 | [diff] [blame] | 22 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
| 23 | #include "llvm/Transforms/Utils/Cloning.h" |
Adam Nemet | 215746b | 2015-07-10 18:55:13 +0000 | [diff] [blame] | 24 | |
| 25 | using namespace llvm; |
| 26 | |
Adam Nemet | 5eccf07 | 2016-03-17 20:32:32 +0000 | [diff] [blame] | 27 | static cl::opt<bool> |
| 28 | AnnotateNoAlias("loop-version-annotate-no-alias", cl::init(true), |
| 29 | cl::Hidden, |
| 30 | cl::desc("Add no-alias annotation for instructions that " |
| 31 | "are disambiguated by memchecks")); |
| 32 | |
Silviu Baranga | 2910a4f | 2015-11-09 13:26:09 +0000 | [diff] [blame] | 33 | LoopVersioning::LoopVersioning(const LoopAccessInfo &LAI, Loop *L, LoopInfo *LI, |
| 34 | DominatorTree *DT, ScalarEvolution *SE, |
| 35 | bool UseLAIChecks) |
| 36 | : VersionedLoop(L), NonVersionedLoop(nullptr), LAI(LAI), LI(LI), DT(DT), |
| 37 | SE(SE) { |
Adam Nemet | 215746b | 2015-07-10 18:55:13 +0000 | [diff] [blame] | 38 | assert(L->getExitBlock() && "No single exit block"); |
| 39 | assert(L->getLoopPreheader() && "No preheader"); |
Silviu Baranga | 2910a4f | 2015-11-09 13:26:09 +0000 | [diff] [blame] | 40 | if (UseLAIChecks) { |
| 41 | setAliasChecks(LAI.getRuntimePointerChecking()->getChecks()); |
Silviu Baranga | 9cd9a7e | 2015-12-09 16:06:28 +0000 | [diff] [blame] | 42 | setSCEVChecks(LAI.PSE.getUnionPredicate()); |
Silviu Baranga | 2910a4f | 2015-11-09 13:26:09 +0000 | [diff] [blame] | 43 | } |
Adam Nemet | 215746b | 2015-07-10 18:55:13 +0000 | [diff] [blame] | 44 | } |
| 45 | |
Silviu Baranga | 2910a4f | 2015-11-09 13:26:09 +0000 | [diff] [blame] | 46 | void LoopVersioning::setAliasChecks( |
Benjamin Kramer | 728f444 | 2016-05-29 10:46:35 +0000 | [diff] [blame] | 47 | SmallVector<RuntimePointerChecking::PointerCheck, 4> Checks) { |
Silviu Baranga | 2910a4f | 2015-11-09 13:26:09 +0000 | [diff] [blame] | 48 | AliasChecks = std::move(Checks); |
| 49 | } |
| 50 | |
| 51 | void LoopVersioning::setSCEVChecks(SCEVUnionPredicate Check) { |
| 52 | Preds = std::move(Check); |
Adam Nemet | dfaeb33 | 2015-08-12 16:51:19 +0000 | [diff] [blame] | 53 | } |
| 54 | |
Adam Nemet | e481340 | 2015-08-20 17:22:29 +0000 | [diff] [blame] | 55 | void LoopVersioning::versionLoop( |
| 56 | const SmallVectorImpl<Instruction *> &DefsUsedOutside) { |
Adam Nemet | 215746b | 2015-07-10 18:55:13 +0000 | [diff] [blame] | 57 | Instruction *FirstCheckInst; |
| 58 | Instruction *MemRuntimeCheck; |
Silviu Baranga | 2910a4f | 2015-11-09 13:26:09 +0000 | [diff] [blame] | 59 | Value *SCEVRuntimeCheck; |
| 60 | Value *RuntimeCheck = nullptr; |
| 61 | |
Adam Nemet | 215746b | 2015-07-10 18:55:13 +0000 | [diff] [blame] | 62 | // Add the memcheck in the original preheader (this is empty initially). |
Silviu Baranga | 2910a4f | 2015-11-09 13:26:09 +0000 | [diff] [blame] | 63 | BasicBlock *RuntimeCheckBB = VersionedLoop->getLoopPreheader(); |
Adam Nemet | 215746b | 2015-07-10 18:55:13 +0000 | [diff] [blame] | 64 | std::tie(FirstCheckInst, MemRuntimeCheck) = |
Silviu Baranga | 2910a4f | 2015-11-09 13:26:09 +0000 | [diff] [blame] | 65 | LAI.addRuntimeChecks(RuntimeCheckBB->getTerminator(), AliasChecks); |
Adam Nemet | 215746b | 2015-07-10 18:55:13 +0000 | [diff] [blame] | 66 | |
Silviu Baranga | 9cd9a7e | 2015-12-09 16:06:28 +0000 | [diff] [blame] | 67 | const SCEVUnionPredicate &Pred = LAI.PSE.getUnionPredicate(); |
Silviu Baranga | 2910a4f | 2015-11-09 13:26:09 +0000 | [diff] [blame] | 68 | SCEVExpander Exp(*SE, RuntimeCheckBB->getModule()->getDataLayout(), |
| 69 | "scev.check"); |
| 70 | SCEVRuntimeCheck = |
| 71 | Exp.expandCodeForPredicate(&Pred, RuntimeCheckBB->getTerminator()); |
| 72 | auto *CI = dyn_cast<ConstantInt>(SCEVRuntimeCheck); |
| 73 | |
| 74 | // Discard the SCEV runtime check if it is always true. |
| 75 | if (CI && CI->isZero()) |
| 76 | SCEVRuntimeCheck = nullptr; |
| 77 | |
| 78 | if (MemRuntimeCheck && SCEVRuntimeCheck) { |
| 79 | RuntimeCheck = BinaryOperator::Create(Instruction::Or, MemRuntimeCheck, |
Vikram TV | 299abc1 | 2016-06-13 10:49:28 +0000 | [diff] [blame^] | 80 | SCEVRuntimeCheck, "lver.safe"); |
Silviu Baranga | 2910a4f | 2015-11-09 13:26:09 +0000 | [diff] [blame] | 81 | if (auto *I = dyn_cast<Instruction>(RuntimeCheck)) |
| 82 | I->insertBefore(RuntimeCheckBB->getTerminator()); |
| 83 | } else |
| 84 | RuntimeCheck = MemRuntimeCheck ? MemRuntimeCheck : SCEVRuntimeCheck; |
| 85 | |
| 86 | assert(RuntimeCheck && "called even though we don't need " |
| 87 | "any runtime checks"); |
| 88 | |
Adam Nemet | 215746b | 2015-07-10 18:55:13 +0000 | [diff] [blame] | 89 | // Rename the block to make the IR more readable. |
Silviu Baranga | 2910a4f | 2015-11-09 13:26:09 +0000 | [diff] [blame] | 90 | RuntimeCheckBB->setName(VersionedLoop->getHeader()->getName() + |
| 91 | ".lver.check"); |
Adam Nemet | 215746b | 2015-07-10 18:55:13 +0000 | [diff] [blame] | 92 | |
| 93 | // Create empty preheader for the loop (and after cloning for the |
| 94 | // non-versioned loop). |
Silviu Baranga | 2910a4f | 2015-11-09 13:26:09 +0000 | [diff] [blame] | 95 | BasicBlock *PH = |
| 96 | SplitBlock(RuntimeCheckBB, RuntimeCheckBB->getTerminator(), DT, LI); |
Adam Nemet | 215746b | 2015-07-10 18:55:13 +0000 | [diff] [blame] | 97 | PH->setName(VersionedLoop->getHeader()->getName() + ".ph"); |
| 98 | |
| 99 | // Clone the loop including the preheader. |
| 100 | // |
| 101 | // FIXME: This does not currently preserve SimplifyLoop because the exit |
| 102 | // block is a join between the two loops. |
| 103 | SmallVector<BasicBlock *, 8> NonVersionedLoopBlocks; |
| 104 | NonVersionedLoop = |
Silviu Baranga | 2910a4f | 2015-11-09 13:26:09 +0000 | [diff] [blame] | 105 | cloneLoopWithPreheader(PH, RuntimeCheckBB, VersionedLoop, VMap, |
| 106 | ".lver.orig", LI, DT, NonVersionedLoopBlocks); |
Adam Nemet | 215746b | 2015-07-10 18:55:13 +0000 | [diff] [blame] | 107 | remapInstructionsInBlocks(NonVersionedLoopBlocks, VMap); |
| 108 | |
| 109 | // Insert the conditional branch based on the result of the memchecks. |
Silviu Baranga | 2910a4f | 2015-11-09 13:26:09 +0000 | [diff] [blame] | 110 | Instruction *OrigTerm = RuntimeCheckBB->getTerminator(); |
Adam Nemet | 215746b | 2015-07-10 18:55:13 +0000 | [diff] [blame] | 111 | BranchInst::Create(NonVersionedLoop->getLoopPreheader(), |
Silviu Baranga | 2910a4f | 2015-11-09 13:26:09 +0000 | [diff] [blame] | 112 | VersionedLoop->getLoopPreheader(), RuntimeCheck, OrigTerm); |
Adam Nemet | 215746b | 2015-07-10 18:55:13 +0000 | [diff] [blame] | 113 | OrigTerm->eraseFromParent(); |
| 114 | |
| 115 | // The loops merge in the original exit block. This is now dominated by the |
| 116 | // memchecking block. |
Silviu Baranga | 2910a4f | 2015-11-09 13:26:09 +0000 | [diff] [blame] | 117 | DT->changeImmediateDominator(VersionedLoop->getExitBlock(), RuntimeCheckBB); |
Adam Nemet | e481340 | 2015-08-20 17:22:29 +0000 | [diff] [blame] | 118 | |
| 119 | // Adds the necessary PHI nodes for the versioned loops based on the |
| 120 | // loop-defined values used outside of the loop. |
| 121 | addPHINodes(DefsUsedOutside); |
Adam Nemet | 215746b | 2015-07-10 18:55:13 +0000 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | void LoopVersioning::addPHINodes( |
| 125 | const SmallVectorImpl<Instruction *> &DefsUsedOutside) { |
| 126 | BasicBlock *PHIBlock = VersionedLoop->getExitBlock(); |
| 127 | assert(PHIBlock && "No single successor to loop exit block"); |
| 128 | |
| 129 | for (auto *Inst : DefsUsedOutside) { |
| 130 | auto *NonVersionedLoopInst = cast<Instruction>(VMap[Inst]); |
| 131 | PHINode *PN; |
| 132 | |
| 133 | // First see if we have a single-operand PHI with the value defined by the |
| 134 | // original loop. |
| 135 | for (auto I = PHIBlock->begin(); (PN = dyn_cast<PHINode>(I)); ++I) { |
Adam Nemet | 8b47e0d | 2016-03-22 18:38:15 +0000 | [diff] [blame] | 136 | if (PN->getIncomingValue(0) == Inst) { |
| 137 | assert(PN->getNumOperands() == 1 && |
| 138 | "Exit block should only have on predecessor"); |
Adam Nemet | 215746b | 2015-07-10 18:55:13 +0000 | [diff] [blame] | 139 | break; |
Adam Nemet | 8b47e0d | 2016-03-22 18:38:15 +0000 | [diff] [blame] | 140 | } |
Adam Nemet | 215746b | 2015-07-10 18:55:13 +0000 | [diff] [blame] | 141 | } |
| 142 | // If not create it. |
| 143 | if (!PN) { |
| 144 | PN = PHINode::Create(Inst->getType(), 2, Inst->getName() + ".lver", |
Duncan P. N. Exon Smith | 5b4c837 | 2015-10-13 02:39:05 +0000 | [diff] [blame] | 145 | &PHIBlock->front()); |
Adam Nemet | 215746b | 2015-07-10 18:55:13 +0000 | [diff] [blame] | 146 | for (auto *User : Inst->users()) |
| 147 | if (!VersionedLoop->contains(cast<Instruction>(User)->getParent())) |
| 148 | User->replaceUsesOfWith(Inst, PN); |
| 149 | PN->addIncoming(Inst, VersionedLoop->getExitingBlock()); |
| 150 | } |
| 151 | // Add the new incoming value from the non-versioned loop. |
| 152 | PN->addIncoming(NonVersionedLoopInst, NonVersionedLoop->getExitingBlock()); |
| 153 | } |
| 154 | } |
Adam Nemet | d52ed84 | 2016-02-03 00:06:10 +0000 | [diff] [blame] | 155 | |
Adam Nemet | 5eccf07 | 2016-03-17 20:32:32 +0000 | [diff] [blame] | 156 | void LoopVersioning::prepareNoAliasMetadata() { |
| 157 | // We need to turn the no-alias relation between pointer checking groups into |
| 158 | // no-aliasing annotations between instructions. |
| 159 | // |
| 160 | // We accomplish this by mapping each pointer checking group (a set of |
| 161 | // pointers memchecked together) to an alias scope and then also mapping each |
| 162 | // group to the list of scopes it can't alias. |
| 163 | |
| 164 | const RuntimePointerChecking *RtPtrChecking = LAI.getRuntimePointerChecking(); |
| 165 | LLVMContext &Context = VersionedLoop->getHeader()->getContext(); |
| 166 | |
| 167 | // First allocate an aliasing scope for each pointer checking group. |
| 168 | // |
| 169 | // While traversing through the checking groups in the loop, also create a |
| 170 | // reverse map from pointers to the pointer checking group they were assigned |
| 171 | // to. |
| 172 | MDBuilder MDB(Context); |
| 173 | MDNode *Domain = MDB.createAnonymousAliasScopeDomain("LVerDomain"); |
| 174 | |
| 175 | for (const auto &Group : RtPtrChecking->CheckingGroups) { |
| 176 | GroupToScope[&Group] = MDB.createAnonymousAliasScope(Domain); |
| 177 | |
| 178 | for (unsigned PtrIdx : Group.Members) |
| 179 | PtrToGroup[RtPtrChecking->getPointerInfo(PtrIdx).PointerValue] = &Group; |
| 180 | } |
| 181 | |
| 182 | // Go through the checks and for each pointer group, collect the scopes for |
| 183 | // each non-aliasing pointer group. |
| 184 | DenseMap<const RuntimePointerChecking::CheckingPtrGroup *, |
| 185 | SmallVector<Metadata *, 4>> |
| 186 | GroupToNonAliasingScopes; |
| 187 | |
| 188 | for (const auto &Check : AliasChecks) |
| 189 | GroupToNonAliasingScopes[Check.first].push_back(GroupToScope[Check.second]); |
| 190 | |
| 191 | // Finally, transform the above to actually map to scope list which is what |
| 192 | // the metadata uses. |
| 193 | |
| 194 | for (auto Pair : GroupToNonAliasingScopes) |
| 195 | GroupToNonAliasingScopeList[Pair.first] = MDNode::get(Context, Pair.second); |
| 196 | } |
| 197 | |
| 198 | void LoopVersioning::annotateLoopWithNoAlias() { |
| 199 | if (!AnnotateNoAlias) |
| 200 | return; |
| 201 | |
| 202 | // First prepare the maps. |
| 203 | prepareNoAliasMetadata(); |
| 204 | |
| 205 | // Add the scope and no-alias metadata to the instructions. |
| 206 | for (Instruction *I : LAI.getDepChecker().getMemoryInstructions()) { |
| 207 | annotateInstWithNoAlias(I); |
| 208 | } |
| 209 | } |
| 210 | |
Adam Nemet | b0c4eae | 2016-03-17 20:32:37 +0000 | [diff] [blame] | 211 | void LoopVersioning::annotateInstWithNoAlias(Instruction *VersionedInst, |
| 212 | const Instruction *OrigInst) { |
Adam Nemet | 5eccf07 | 2016-03-17 20:32:32 +0000 | [diff] [blame] | 213 | if (!AnnotateNoAlias) |
| 214 | return; |
| 215 | |
| 216 | LLVMContext &Context = VersionedLoop->getHeader()->getContext(); |
Adam Nemet | b0c4eae | 2016-03-17 20:32:37 +0000 | [diff] [blame] | 217 | const Value *Ptr = isa<LoadInst>(OrigInst) |
| 218 | ? cast<LoadInst>(OrigInst)->getPointerOperand() |
| 219 | : cast<StoreInst>(OrigInst)->getPointerOperand(); |
Adam Nemet | 5eccf07 | 2016-03-17 20:32:32 +0000 | [diff] [blame] | 220 | |
| 221 | // Find the group for the pointer and then add the scope metadata. |
| 222 | auto Group = PtrToGroup.find(Ptr); |
| 223 | if (Group != PtrToGroup.end()) { |
Adam Nemet | b0c4eae | 2016-03-17 20:32:37 +0000 | [diff] [blame] | 224 | VersionedInst->setMetadata( |
Adam Nemet | 5eccf07 | 2016-03-17 20:32:32 +0000 | [diff] [blame] | 225 | LLVMContext::MD_alias_scope, |
Adam Nemet | b0c4eae | 2016-03-17 20:32:37 +0000 | [diff] [blame] | 226 | MDNode::concatenate( |
| 227 | VersionedInst->getMetadata(LLVMContext::MD_alias_scope), |
| 228 | MDNode::get(Context, GroupToScope[Group->second]))); |
Adam Nemet | 5eccf07 | 2016-03-17 20:32:32 +0000 | [diff] [blame] | 229 | |
| 230 | // Add the no-alias metadata. |
| 231 | auto NonAliasingScopeList = GroupToNonAliasingScopeList.find(Group->second); |
| 232 | if (NonAliasingScopeList != GroupToNonAliasingScopeList.end()) |
Adam Nemet | b0c4eae | 2016-03-17 20:32:37 +0000 | [diff] [blame] | 233 | VersionedInst->setMetadata( |
Adam Nemet | 5eccf07 | 2016-03-17 20:32:32 +0000 | [diff] [blame] | 234 | LLVMContext::MD_noalias, |
Adam Nemet | b0c4eae | 2016-03-17 20:32:37 +0000 | [diff] [blame] | 235 | MDNode::concatenate( |
| 236 | VersionedInst->getMetadata(LLVMContext::MD_noalias), |
| 237 | NonAliasingScopeList->second)); |
Adam Nemet | 5eccf07 | 2016-03-17 20:32:32 +0000 | [diff] [blame] | 238 | } |
| 239 | } |
| 240 | |
Adam Nemet | d52ed84 | 2016-02-03 00:06:10 +0000 | [diff] [blame] | 241 | namespace { |
| 242 | /// \brief Also expose this is a pass. Currently this is only used for |
| 243 | /// unit-testing. It adds all memchecks necessary to remove all may-aliasing |
| 244 | /// array accesses from the loop. |
| 245 | class LoopVersioningPass : public FunctionPass { |
| 246 | public: |
| 247 | LoopVersioningPass() : FunctionPass(ID) { |
| 248 | initializeLoopVersioningPassPass(*PassRegistry::getPassRegistry()); |
| 249 | } |
| 250 | |
| 251 | bool runOnFunction(Function &F) override { |
| 252 | auto *LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo(); |
| 253 | auto *LAA = &getAnalysis<LoopAccessAnalysis>(); |
| 254 | auto *DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree(); |
| 255 | auto *SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE(); |
| 256 | |
| 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->empty()) |
| 266 | Worklist.push_back(L); |
| 267 | |
| 268 | // Now walk the identified inner loops. |
| 269 | bool Changed = false; |
| 270 | for (Loop *L : Worklist) { |
Xinliang David Li | ecde1c7 | 2016-06-09 03:22:39 +0000 | [diff] [blame] | 271 | const LoopAccessInfo &LAI = LAA->getInfo(L, ValueToValueMap()); |
Adam Nemet | d52ed84 | 2016-02-03 00:06:10 +0000 | [diff] [blame] | 272 | if (LAI.getNumRuntimePointerChecks() || |
| 273 | !LAI.PSE.getUnionPredicate().isAlwaysTrue()) { |
| 274 | LoopVersioning LVer(LAI, L, LI, DT, SE); |
| 275 | LVer.versionLoop(); |
Adam Nemet | 5eccf07 | 2016-03-17 20:32:32 +0000 | [diff] [blame] | 276 | LVer.annotateLoopWithNoAlias(); |
Adam Nemet | d52ed84 | 2016-02-03 00:06:10 +0000 | [diff] [blame] | 277 | Changed = true; |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | return Changed; |
| 282 | } |
| 283 | |
| 284 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 285 | AU.addRequired<LoopInfoWrapperPass>(); |
| 286 | AU.addPreserved<LoopInfoWrapperPass>(); |
| 287 | AU.addRequired<LoopAccessAnalysis>(); |
| 288 | AU.addRequired<DominatorTreeWrapperPass>(); |
| 289 | AU.addPreserved<DominatorTreeWrapperPass>(); |
| 290 | AU.addRequired<ScalarEvolutionWrapperPass>(); |
| 291 | } |
| 292 | |
| 293 | static char ID; |
| 294 | }; |
| 295 | } |
| 296 | |
| 297 | #define LVER_OPTION "loop-versioning" |
| 298 | #define DEBUG_TYPE LVER_OPTION |
| 299 | |
| 300 | char LoopVersioningPass::ID; |
| 301 | static const char LVer_name[] = "Loop Versioning"; |
| 302 | |
| 303 | INITIALIZE_PASS_BEGIN(LoopVersioningPass, LVER_OPTION, LVer_name, false, false) |
| 304 | INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass) |
| 305 | INITIALIZE_PASS_DEPENDENCY(LoopAccessAnalysis) |
| 306 | INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass) |
| 307 | INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass) |
| 308 | INITIALIZE_PASS_END(LoopVersioningPass, LVER_OPTION, LVer_name, false, false) |
| 309 | |
| 310 | namespace llvm { |
| 311 | FunctionPass *createLoopVersioningPass() { |
| 312 | return new LoopVersioningPass(); |
| 313 | } |
| 314 | } |