LLVM  3.7.0
LoopVersioning.cpp
Go to the documentation of this file.
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 
17 #include "llvm/Analysis/LoopInfo.h"
18 #include "llvm/IR/Dominators.h"
22 
23 using namespace llvm;
24 
26  DominatorTree *DT,
27  const SmallVector<int, 8> *PtrToPartition)
28  : VersionedLoop(L), NonVersionedLoop(nullptr),
29  PtrToPartition(PtrToPartition), LAI(LAI), LI(LI), DT(DT) {
30  assert(L->getExitBlock() && "No single exit block");
31  assert(L->getLoopPreheader() && "No preheader");
32 }
33 
35  return LAI.getRuntimePointerChecking()->needsAnyChecking(PtrToPartition);
36 }
37 
39  Instruction *FirstCheckInst;
40  Instruction *MemRuntimeCheck;
41  // Add the memcheck in the original preheader (this is empty initially).
42  BasicBlock *MemCheckBB = VersionedLoop->getLoopPreheader();
43  std::tie(FirstCheckInst, MemRuntimeCheck) =
44  LAI.addRuntimeCheck(MemCheckBB->getTerminator(), PtrToPartition);
45  assert(MemRuntimeCheck && "called even though needsAnyChecking = false");
46 
47  // Rename the block to make the IR more readable.
48  MemCheckBB->setName(VersionedLoop->getHeader()->getName() + ".lver.memcheck");
49 
50  // Create empty preheader for the loop (and after cloning for the
51  // non-versioned loop).
52  BasicBlock *PH = SplitBlock(MemCheckBB, MemCheckBB->getTerminator(), DT, LI);
53  PH->setName(VersionedLoop->getHeader()->getName() + ".ph");
54 
55  // Clone the loop including the preheader.
56  //
57  // FIXME: This does not currently preserve SimplifyLoop because the exit
58  // block is a join between the two loops.
59  SmallVector<BasicBlock *, 8> NonVersionedLoopBlocks;
60  NonVersionedLoop =
61  cloneLoopWithPreheader(PH, MemCheckBB, VersionedLoop, VMap, ".lver.orig",
62  LI, DT, NonVersionedLoopBlocks);
63  remapInstructionsInBlocks(NonVersionedLoopBlocks, VMap);
64 
65  // Insert the conditional branch based on the result of the memchecks.
66  Instruction *OrigTerm = MemCheckBB->getTerminator();
67  BranchInst::Create(NonVersionedLoop->getLoopPreheader(),
68  VersionedLoop->getLoopPreheader(), MemRuntimeCheck,
69  OrigTerm);
70  OrigTerm->eraseFromParent();
71 
72  // The loops merge in the original exit block. This is now dominated by the
73  // memchecking block.
74  DT->changeImmediateDominator(VersionedLoop->getExitBlock(), MemCheckBB);
75 }
76 
78  const SmallVectorImpl<Instruction *> &DefsUsedOutside) {
79  BasicBlock *PHIBlock = VersionedLoop->getExitBlock();
80  assert(PHIBlock && "No single successor to loop exit block");
81 
82  for (auto *Inst : DefsUsedOutside) {
83  auto *NonVersionedLoopInst = cast<Instruction>(VMap[Inst]);
84  PHINode *PN;
85 
86  // First see if we have a single-operand PHI with the value defined by the
87  // original loop.
88  for (auto I = PHIBlock->begin(); (PN = dyn_cast<PHINode>(I)); ++I) {
89  assert(PN->getNumOperands() == 1 &&
90  "Exit block should only have on predecessor");
91  if (PN->getIncomingValue(0) == Inst)
92  break;
93  }
94  // If not create it.
95  if (!PN) {
96  PN = PHINode::Create(Inst->getType(), 2, Inst->getName() + ".lver",
97  PHIBlock->begin());
98  for (auto *User : Inst->users())
99  if (!VersionedLoop->contains(cast<Instruction>(User)->getParent()))
100  User->replaceUsesOfWith(Inst, PN);
101  PN->addIncoming(Inst, VersionedLoop->getExitingBlock());
102  }
103  // Add the new incoming value from the non-versioned loop.
104  PN->addIncoming(NonVersionedLoopInst, NonVersionedLoop->getExitingBlock());
105  }
106 }
Pass interface - Implemented by all 'passes'.
Definition: Pass.h:82
iplist< Instruction >::iterator eraseFromParent()
eraseFromParent - This method unlinks 'this' from the containing basic block and deletes it...
Definition: Instruction.cpp:70
LoopVersioning(const LoopAccessInfo &LAI, Loop *L, LoopInfo *LI, DominatorTree *DT, const SmallVector< int, 8 > *PtrToPartition=nullptr)
BasicBlock * SplitBlock(BasicBlock *Old, Instruction *SplitPt, DominatorTree *DT=nullptr, LoopInfo *LI=nullptr)
SplitBlock - Split the specified block at the specified instruction - every thing before SplitPt stay...
BlockT * getExitBlock() const
getExitBlock - If getExitBlocks would return exactly one block, return that block.
Definition: LoopInfoImpl.h:78
BlockT * getHeader() const
Definition: LoopInfo.h:96
void remapInstructionsInBlocks(const SmallVectorImpl< BasicBlock * > &Blocks, ValueToValueMapTy &VMap)
Remaps instructions in Blocks using the mapping in VMap.
iterator begin()
Instruction iterator methods.
Definition: BasicBlock.h:231
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: APInt.h:33
void setName(const Twine &Name)
Change the name of the value.
Definition: Value.cpp:250
const RuntimePointerChecking * getRuntimePointerChecking() const
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree...
Definition: Dominators.h:67
void versionLoop(Pass *P)
Performs the CFG manipulation part of versioning the loop including the DominatorTree and LoopInfo up...
void replaceUsesOfWith(Value *From, Value *To)
Replace uses of one Value with another.
Definition: User.cpp:24
#define P(N)
BlockT * getLoopPreheader() const
getLoopPreheader - If there is a preheader for this loop, return it.
Definition: LoopInfoImpl.h:108
LLVM Basic Block Representation.
Definition: BasicBlock.h:65
bool contains(const LoopT *L) const
contains - Return true if the specified loop is contained within in this loop.
Definition: LoopInfo.h:105
BlockT * getExitingBlock() const
getExitingBlock - If getExitingBlocks would return exactly one block, return that block...
Definition: LoopInfoImpl.h:51
Drive the analysis of memory accesses in the loop.
static BranchInst * Create(BasicBlock *IfTrue, Instruction *InsertBefore=nullptr)
static PHINode * Create(Type *Ty, unsigned NumReservedValues, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
Constructors - NumReservedValues is a hint for the number of incoming edges that this phi node will h...
bool needsAnyChecking(const SmallVectorImpl< int > *PtrPartition) const
Return true if any pointer requires run-time checking according to needsChecking. ...
iterator_range< user_iterator > users()
Definition: Value.h:300
LLVM_ATTRIBUTE_UNUSED_RESULT std::enable_if< !is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:285
Loop * cloneLoopWithPreheader(BasicBlock *Before, BasicBlock *LoopDomBB, Loop *OrigLoop, ValueToValueMapTy &VMap, const Twine &NameSuffix, LoopInfo *LI, DominatorTree *DT, SmallVectorImpl< BasicBlock * > &Blocks)
Clones a loop OrigLoop.
void addPHINodes(const SmallVectorImpl< Instruction * > &DefsUsedOutside)
Adds the necessary PHI nodes for the versioned loops based on the loop-defined values used outside of...
#define I(x, y, z)
Definition: MD5.cpp:54
TerminatorInst * getTerminator()
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
Definition: BasicBlock.cpp:124
void changeImmediateDominator(DomTreeNodeBase< NodeT > *N, DomTreeNodeBase< NodeT > *NewIDom)
changeImmediateDominator - This method is used to update the dominator tree information when a node's...
bool needsRuntimeChecks() const
Returns true if we need memchecks to disambiguate may-aliasing accesses.
std::pair< Instruction *, Instruction * > addRuntimeCheck(Instruction *Loc, const SmallVectorImpl< int > *PtrPartition=nullptr) const
Add code that checks at runtime if the accessed arrays overlap.