LoopVersioning.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. #include "llvm/Analysis/LoopAccessAnalysis.h"
  16. #include "llvm/Analysis/LoopInfo.h"
  17. #include "llvm/IR/Dominators.h"
  18. #include "llvm/Transforms/Utils/BasicBlockUtils.h"
  19. #include "llvm/Transforms/Utils/Cloning.h"
  20. #include "llvm/Transforms/Utils/LoopVersioning.h"
  21. using namespace llvm;
  22. LoopVersioning::LoopVersioning(const LoopAccessInfo &LAI, Loop *L, LoopInfo *LI,
  23. DominatorTree *DT,
  24. const SmallVector<int, 8> *PtrToPartition)
  25. : VersionedLoop(L), NonVersionedLoop(nullptr),
  26. PtrToPartition(PtrToPartition), LAI(LAI), LI(LI), DT(DT) {
  27. assert(L->getExitBlock() && "No single exit block");
  28. assert(L->getLoopPreheader() && "No preheader");
  29. }
  30. bool LoopVersioning::needsRuntimeChecks() const {
  31. return LAI.getRuntimePointerChecking()->needsAnyChecking(PtrToPartition);
  32. }
  33. void LoopVersioning::versionLoop(Pass *P) {
  34. Instruction *FirstCheckInst;
  35. Instruction *MemRuntimeCheck;
  36. // Add the memcheck in the original preheader (this is empty initially).
  37. BasicBlock *MemCheckBB = VersionedLoop->getLoopPreheader();
  38. std::tie(FirstCheckInst, MemRuntimeCheck) =
  39. LAI.addRuntimeCheck(MemCheckBB->getTerminator(), PtrToPartition);
  40. assert(MemRuntimeCheck && "called even though needsAnyChecking = false");
  41. // Rename the block to make the IR more readable.
  42. MemCheckBB->setName(VersionedLoop->getHeader()->getName() + ".lver.memcheck");
  43. // Create empty preheader for the loop (and after cloning for the
  44. // non-versioned loop).
  45. BasicBlock *PH = SplitBlock(MemCheckBB, MemCheckBB->getTerminator(), DT, LI);
  46. PH->setName(VersionedLoop->getHeader()->getName() + ".ph");
  47. // Clone the loop including the preheader.
  48. //
  49. // FIXME: This does not currently preserve SimplifyLoop because the exit
  50. // block is a join between the two loops.
  51. SmallVector<BasicBlock *, 8> NonVersionedLoopBlocks;
  52. NonVersionedLoop =
  53. cloneLoopWithPreheader(PH, MemCheckBB, VersionedLoop, VMap, ".lver.orig",
  54. LI, DT, NonVersionedLoopBlocks);
  55. remapInstructionsInBlocks(NonVersionedLoopBlocks, VMap);
  56. // Insert the conditional branch based on the result of the memchecks.
  57. Instruction *OrigTerm = MemCheckBB->getTerminator();
  58. BranchInst::Create(NonVersionedLoop->getLoopPreheader(),
  59. VersionedLoop->getLoopPreheader(), MemRuntimeCheck,
  60. OrigTerm);
  61. OrigTerm->eraseFromParent();
  62. // The loops merge in the original exit block. This is now dominated by the
  63. // memchecking block.
  64. DT->changeImmediateDominator(VersionedLoop->getExitBlock(), MemCheckBB);
  65. }
  66. void LoopVersioning::addPHINodes(
  67. const SmallVectorImpl<Instruction *> &DefsUsedOutside) {
  68. BasicBlock *PHIBlock = VersionedLoop->getExitBlock();
  69. assert(PHIBlock && "No single successor to loop exit block");
  70. for (auto *Inst : DefsUsedOutside) {
  71. auto *NonVersionedLoopInst = cast<Instruction>(VMap[Inst]);
  72. PHINode *PN;
  73. // First see if we have a single-operand PHI with the value defined by the
  74. // original loop.
  75. for (auto I = PHIBlock->begin(); (PN = dyn_cast<PHINode>(I)); ++I) {
  76. assert(PN->getNumOperands() == 1 &&
  77. "Exit block should only have on predecessor");
  78. if (PN->getIncomingValue(0) == Inst)
  79. break;
  80. }
  81. // If not create it.
  82. if (!PN) {
  83. PN = PHINode::Create(Inst->getType(), 2, Inst->getName() + ".lver",
  84. PHIBlock->begin());
  85. for (auto *User : Inst->users())
  86. if (!VersionedLoop->contains(cast<Instruction>(User)->getParent()))
  87. User->replaceUsesOfWith(Inst, PN);
  88. PN->addIncoming(Inst, VersionedLoop->getExitingBlock());
  89. }
  90. // Add the new incoming value from the non-versioned loop.
  91. PN->addIncoming(NonVersionedLoopInst, NonVersionedLoop->getExitingBlock());
  92. }
  93. }