CodeMoverUtils.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. //===- CodeMoverUtils.cpp - CodeMover Utilities ----------------------------==//
  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 family of functions perform movements on basic blocks, and instructions
  10. // contained within a function.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/Transforms/Utils/CodeMoverUtils.h"
  14. #include "llvm/ADT/Statistic.h"
  15. #include "llvm/Analysis/DependenceAnalysis.h"
  16. #include "llvm/Analysis/PostDominators.h"
  17. #include "llvm/Analysis/ValueTracking.h"
  18. #include "llvm/IR/Dominators.h"
  19. using namespace llvm;
  20. #define DEBUG_TYPE "codemover-utils"
  21. STATISTIC(HasDependences,
  22. "Cannot move across instructions that has memory dependences");
  23. STATISTIC(MayThrowException, "Cannot move across instructions that may throw");
  24. STATISTIC(NotControlFlowEquivalent,
  25. "Instructions are not control flow equivalent");
  26. STATISTIC(NotMovedPHINode, "Movement of PHINodes are not supported");
  27. STATISTIC(NotMovedTerminator, "Movement of Terminator are not supported");
  28. namespace {
  29. /// Represent a control condition. A control condition is a condition of a
  30. /// terminator to decide which successors to execute. The pointer field
  31. /// represents the address of the condition of the terminator. The integer field
  32. /// is a bool, it is true when the basic block is executed when V is true. For
  33. /// example, `br %cond, bb0, bb1` %cond is a control condition of bb0 with the
  34. /// integer field equals to true, while %cond is a control condition of bb1 with
  35. /// the integer field equals to false.
  36. using ControlCondition = PointerIntPair<Value *, 1, bool>;
  37. #ifndef NDEBUG
  38. raw_ostream &operator<<(raw_ostream &OS, const ControlCondition &C) {
  39. OS << "[" << *C.getPointer() << ", " << (C.getInt() ? "true" : "false")
  40. << "]";
  41. return OS;
  42. }
  43. #endif
  44. /// Represent a set of control conditions required to execute ToBB from FromBB.
  45. class ControlConditions {
  46. using ConditionVectorTy = SmallVector<ControlCondition, 6>;
  47. /// A SmallVector of control conditions.
  48. ConditionVectorTy Conditions;
  49. public:
  50. /// Return a ControlConditions which stores all conditions required to execute
  51. /// \p BB from \p Dominator. If \p MaxLookup is non-zero, it limits the
  52. /// number of conditions to collect. Return std::nullopt if not all conditions
  53. /// are collected successfully, or we hit the limit.
  54. static const std::optional<ControlConditions>
  55. collectControlConditions(const BasicBlock &BB, const BasicBlock &Dominator,
  56. const DominatorTree &DT,
  57. const PostDominatorTree &PDT,
  58. unsigned MaxLookup = 6);
  59. /// Return true if there exists no control conditions required to execute ToBB
  60. /// from FromBB.
  61. bool isUnconditional() const { return Conditions.empty(); }
  62. /// Return a constant reference of Conditions.
  63. const ConditionVectorTy &getControlConditions() const { return Conditions; }
  64. /// Add \p V as one of the ControlCondition in Condition with IsTrueCondition
  65. /// equals to \p True. Return true if inserted successfully.
  66. bool addControlCondition(ControlCondition C);
  67. /// Return true if for all control conditions in Conditions, there exists an
  68. /// equivalent control condition in \p Other.Conditions.
  69. bool isEquivalent(const ControlConditions &Other) const;
  70. /// Return true if \p C1 and \p C2 are equivalent.
  71. static bool isEquivalent(const ControlCondition &C1,
  72. const ControlCondition &C2);
  73. private:
  74. ControlConditions() = default;
  75. static bool isEquivalent(const Value &V1, const Value &V2);
  76. static bool isInverse(const Value &V1, const Value &V2);
  77. };
  78. } // namespace
  79. static bool domTreeLevelBefore(DominatorTree *DT, const Instruction *InstA,
  80. const Instruction *InstB) {
  81. // Use ordered basic block in case the 2 instructions are in the same
  82. // block.
  83. if (InstA->getParent() == InstB->getParent())
  84. return InstA->comesBefore(InstB);
  85. DomTreeNode *DA = DT->getNode(InstA->getParent());
  86. DomTreeNode *DB = DT->getNode(InstB->getParent());
  87. return DA->getLevel() < DB->getLevel();
  88. }
  89. const std::optional<ControlConditions>
  90. ControlConditions::collectControlConditions(const BasicBlock &BB,
  91. const BasicBlock &Dominator,
  92. const DominatorTree &DT,
  93. const PostDominatorTree &PDT,
  94. unsigned MaxLookup) {
  95. assert(DT.dominates(&Dominator, &BB) && "Expecting Dominator to dominate BB");
  96. ControlConditions Conditions;
  97. unsigned NumConditions = 0;
  98. // BB is executed unconditional from itself.
  99. if (&Dominator == &BB)
  100. return Conditions;
  101. const BasicBlock *CurBlock = &BB;
  102. // Walk up the dominator tree from the associated DT node for BB to the
  103. // associated DT node for Dominator.
  104. do {
  105. assert(DT.getNode(CurBlock) && "Expecting a valid DT node for CurBlock");
  106. BasicBlock *IDom = DT.getNode(CurBlock)->getIDom()->getBlock();
  107. assert(DT.dominates(&Dominator, IDom) &&
  108. "Expecting Dominator to dominate IDom");
  109. // Limitation: can only handle branch instruction currently.
  110. const BranchInst *BI = dyn_cast<BranchInst>(IDom->getTerminator());
  111. if (!BI)
  112. return std::nullopt;
  113. bool Inserted = false;
  114. if (PDT.dominates(CurBlock, IDom)) {
  115. LLVM_DEBUG(dbgs() << CurBlock->getName()
  116. << " is executed unconditionally from "
  117. << IDom->getName() << "\n");
  118. } else if (PDT.dominates(CurBlock, BI->getSuccessor(0))) {
  119. LLVM_DEBUG(dbgs() << CurBlock->getName() << " is executed when \""
  120. << *BI->getCondition() << "\" is true from "
  121. << IDom->getName() << "\n");
  122. Inserted = Conditions.addControlCondition(
  123. ControlCondition(BI->getCondition(), true));
  124. } else if (PDT.dominates(CurBlock, BI->getSuccessor(1))) {
  125. LLVM_DEBUG(dbgs() << CurBlock->getName() << " is executed when \""
  126. << *BI->getCondition() << "\" is false from "
  127. << IDom->getName() << "\n");
  128. Inserted = Conditions.addControlCondition(
  129. ControlCondition(BI->getCondition(), false));
  130. } else
  131. return std::nullopt;
  132. if (Inserted)
  133. ++NumConditions;
  134. if (MaxLookup != 0 && NumConditions > MaxLookup)
  135. return std::nullopt;
  136. CurBlock = IDom;
  137. } while (CurBlock != &Dominator);
  138. return Conditions;
  139. }
  140. bool ControlConditions::addControlCondition(ControlCondition C) {
  141. bool Inserted = false;
  142. if (none_of(Conditions, [&](ControlCondition &Exists) {
  143. return ControlConditions::isEquivalent(C, Exists);
  144. })) {
  145. Conditions.push_back(C);
  146. Inserted = true;
  147. }
  148. LLVM_DEBUG(dbgs() << (Inserted ? "Inserted " : "Not inserted ") << C << "\n");
  149. return Inserted;
  150. }
  151. bool ControlConditions::isEquivalent(const ControlConditions &Other) const {
  152. if (Conditions.empty() && Other.Conditions.empty())
  153. return true;
  154. if (Conditions.size() != Other.Conditions.size())
  155. return false;
  156. return all_of(Conditions, [&](const ControlCondition &C) {
  157. return any_of(Other.Conditions, [&](const ControlCondition &OtherC) {
  158. return ControlConditions::isEquivalent(C, OtherC);
  159. });
  160. });
  161. }
  162. bool ControlConditions::isEquivalent(const ControlCondition &C1,
  163. const ControlCondition &C2) {
  164. if (C1.getInt() == C2.getInt()) {
  165. if (isEquivalent(*C1.getPointer(), *C2.getPointer()))
  166. return true;
  167. } else if (isInverse(*C1.getPointer(), *C2.getPointer()))
  168. return true;
  169. return false;
  170. }
  171. // FIXME: Use SCEV and reuse GVN/CSE logic to check for equivalence between
  172. // Values.
  173. // Currently, isEquivalent rely on other passes to ensure equivalent conditions
  174. // have the same value, e.g. GVN.
  175. bool ControlConditions::isEquivalent(const Value &V1, const Value &V2) {
  176. return &V1 == &V2;
  177. }
  178. bool ControlConditions::isInverse(const Value &V1, const Value &V2) {
  179. if (const CmpInst *Cmp1 = dyn_cast<CmpInst>(&V1))
  180. if (const CmpInst *Cmp2 = dyn_cast<CmpInst>(&V2)) {
  181. if (Cmp1->getPredicate() == Cmp2->getInversePredicate() &&
  182. Cmp1->getOperand(0) == Cmp2->getOperand(0) &&
  183. Cmp1->getOperand(1) == Cmp2->getOperand(1))
  184. return true;
  185. if (Cmp1->getPredicate() ==
  186. CmpInst::getSwappedPredicate(Cmp2->getInversePredicate()) &&
  187. Cmp1->getOperand(0) == Cmp2->getOperand(1) &&
  188. Cmp1->getOperand(1) == Cmp2->getOperand(0))
  189. return true;
  190. }
  191. return false;
  192. }
  193. bool llvm::isControlFlowEquivalent(const Instruction &I0, const Instruction &I1,
  194. const DominatorTree &DT,
  195. const PostDominatorTree &PDT) {
  196. return isControlFlowEquivalent(*I0.getParent(), *I1.getParent(), DT, PDT);
  197. }
  198. bool llvm::isControlFlowEquivalent(const BasicBlock &BB0, const BasicBlock &BB1,
  199. const DominatorTree &DT,
  200. const PostDominatorTree &PDT) {
  201. if (&BB0 == &BB1)
  202. return true;
  203. if ((DT.dominates(&BB0, &BB1) && PDT.dominates(&BB1, &BB0)) ||
  204. (PDT.dominates(&BB0, &BB1) && DT.dominates(&BB1, &BB0)))
  205. return true;
  206. // If the set of conditions required to execute BB0 and BB1 from their common
  207. // dominator are the same, then BB0 and BB1 are control flow equivalent.
  208. const BasicBlock *CommonDominator = DT.findNearestCommonDominator(&BB0, &BB1);
  209. LLVM_DEBUG(dbgs() << "The nearest common dominator of " << BB0.getName()
  210. << " and " << BB1.getName() << " is "
  211. << CommonDominator->getName() << "\n");
  212. const std::optional<ControlConditions> BB0Conditions =
  213. ControlConditions::collectControlConditions(BB0, *CommonDominator, DT,
  214. PDT);
  215. if (BB0Conditions == std::nullopt)
  216. return false;
  217. const std::optional<ControlConditions> BB1Conditions =
  218. ControlConditions::collectControlConditions(BB1, *CommonDominator, DT,
  219. PDT);
  220. if (BB1Conditions == std::nullopt)
  221. return false;
  222. return BB0Conditions->isEquivalent(*BB1Conditions);
  223. }
  224. static bool reportInvalidCandidate(const Instruction &I,
  225. llvm::Statistic &Stat) {
  226. ++Stat;
  227. LLVM_DEBUG(dbgs() << "Unable to move instruction: " << I << ". "
  228. << Stat.getDesc());
  229. return false;
  230. }
  231. /// Collect all instructions in between \p StartInst and \p EndInst, and store
  232. /// them in \p InBetweenInsts.
  233. static void
  234. collectInstructionsInBetween(Instruction &StartInst, const Instruction &EndInst,
  235. SmallPtrSetImpl<Instruction *> &InBetweenInsts) {
  236. assert(InBetweenInsts.empty() && "Expecting InBetweenInsts to be empty");
  237. /// Get the next instructions of \p I, and push them to \p WorkList.
  238. auto getNextInsts = [](Instruction &I,
  239. SmallPtrSetImpl<Instruction *> &WorkList) {
  240. if (Instruction *NextInst = I.getNextNode())
  241. WorkList.insert(NextInst);
  242. else {
  243. assert(I.isTerminator() && "Expecting a terminator instruction");
  244. for (BasicBlock *Succ : successors(&I))
  245. WorkList.insert(&Succ->front());
  246. }
  247. };
  248. SmallPtrSet<Instruction *, 10> WorkList;
  249. getNextInsts(StartInst, WorkList);
  250. while (!WorkList.empty()) {
  251. Instruction *CurInst = *WorkList.begin();
  252. WorkList.erase(CurInst);
  253. if (CurInst == &EndInst)
  254. continue;
  255. if (!InBetweenInsts.insert(CurInst).second)
  256. continue;
  257. getNextInsts(*CurInst, WorkList);
  258. }
  259. }
  260. bool llvm::isSafeToMoveBefore(Instruction &I, Instruction &InsertPoint,
  261. DominatorTree &DT, const PostDominatorTree *PDT,
  262. DependenceInfo *DI, bool CheckForEntireBlock) {
  263. // Skip tests when we don't have PDT or DI
  264. if (!PDT || !DI)
  265. return false;
  266. // Cannot move itself before itself.
  267. if (&I == &InsertPoint)
  268. return false;
  269. // Not moved.
  270. if (I.getNextNode() == &InsertPoint)
  271. return true;
  272. if (isa<PHINode>(I) || isa<PHINode>(InsertPoint))
  273. return reportInvalidCandidate(I, NotMovedPHINode);
  274. if (I.isTerminator())
  275. return reportInvalidCandidate(I, NotMovedTerminator);
  276. // TODO remove this limitation.
  277. if (!isControlFlowEquivalent(I, InsertPoint, DT, *PDT))
  278. return reportInvalidCandidate(I, NotControlFlowEquivalent);
  279. if (isReachedBefore(&I, &InsertPoint, &DT, PDT))
  280. for (const Use &U : I.uses())
  281. if (auto *UserInst = dyn_cast<Instruction>(U.getUser()))
  282. if (UserInst != &InsertPoint && !DT.dominates(&InsertPoint, U))
  283. return false;
  284. if (isReachedBefore(&InsertPoint, &I, &DT, PDT))
  285. for (const Value *Op : I.operands())
  286. if (auto *OpInst = dyn_cast<Instruction>(Op)) {
  287. if (&InsertPoint == OpInst)
  288. return false;
  289. // If OpInst is an instruction that appears earlier in the same BB as
  290. // I, then it is okay to move since OpInst will still be available.
  291. if (CheckForEntireBlock && I.getParent() == OpInst->getParent() &&
  292. DT.dominates(OpInst, &I))
  293. continue;
  294. if (!DT.dominates(OpInst, &InsertPoint))
  295. return false;
  296. }
  297. DT.updateDFSNumbers();
  298. const bool MoveForward = domTreeLevelBefore(&DT, &I, &InsertPoint);
  299. Instruction &StartInst = (MoveForward ? I : InsertPoint);
  300. Instruction &EndInst = (MoveForward ? InsertPoint : I);
  301. SmallPtrSet<Instruction *, 10> InstsToCheck;
  302. collectInstructionsInBetween(StartInst, EndInst, InstsToCheck);
  303. if (!MoveForward)
  304. InstsToCheck.insert(&InsertPoint);
  305. // Check if there exists instructions which may throw, may synchonize, or may
  306. // never return, from I to InsertPoint.
  307. if (!isSafeToSpeculativelyExecute(&I))
  308. if (llvm::any_of(InstsToCheck, [](Instruction *I) {
  309. if (I->mayThrow())
  310. return true;
  311. const CallBase *CB = dyn_cast<CallBase>(I);
  312. if (!CB)
  313. return false;
  314. if (!CB->hasFnAttr(Attribute::WillReturn))
  315. return true;
  316. if (!CB->hasFnAttr(Attribute::NoSync))
  317. return true;
  318. return false;
  319. })) {
  320. return reportInvalidCandidate(I, MayThrowException);
  321. }
  322. // Check if I has any output/flow/anti dependences with instructions from \p
  323. // StartInst to \p EndInst.
  324. if (llvm::any_of(InstsToCheck, [&DI, &I](Instruction *CurInst) {
  325. auto DepResult = DI->depends(&I, CurInst, true);
  326. if (DepResult && (DepResult->isOutput() || DepResult->isFlow() ||
  327. DepResult->isAnti()))
  328. return true;
  329. return false;
  330. }))
  331. return reportInvalidCandidate(I, HasDependences);
  332. return true;
  333. }
  334. bool llvm::isSafeToMoveBefore(BasicBlock &BB, Instruction &InsertPoint,
  335. DominatorTree &DT, const PostDominatorTree *PDT,
  336. DependenceInfo *DI) {
  337. return llvm::all_of(BB, [&](Instruction &I) {
  338. if (BB.getTerminator() == &I)
  339. return true;
  340. return isSafeToMoveBefore(I, InsertPoint, DT, PDT, DI,
  341. /*CheckForEntireBlock=*/true);
  342. });
  343. }
  344. void llvm::moveInstructionsToTheBeginning(BasicBlock &FromBB, BasicBlock &ToBB,
  345. DominatorTree &DT,
  346. const PostDominatorTree &PDT,
  347. DependenceInfo &DI) {
  348. for (Instruction &I :
  349. llvm::make_early_inc_range(llvm::drop_begin(llvm::reverse(FromBB)))) {
  350. Instruction *MovePos = ToBB.getFirstNonPHIOrDbg();
  351. if (isSafeToMoveBefore(I, *MovePos, DT, &PDT, &DI))
  352. I.moveBefore(MovePos);
  353. }
  354. }
  355. void llvm::moveInstructionsToTheEnd(BasicBlock &FromBB, BasicBlock &ToBB,
  356. DominatorTree &DT,
  357. const PostDominatorTree &PDT,
  358. DependenceInfo &DI) {
  359. Instruction *MovePos = ToBB.getTerminator();
  360. while (FromBB.size() > 1) {
  361. Instruction &I = FromBB.front();
  362. if (isSafeToMoveBefore(I, *MovePos, DT, &PDT, &DI))
  363. I.moveBefore(MovePos);
  364. }
  365. }
  366. bool llvm::nonStrictlyPostDominate(const BasicBlock *ThisBlock,
  367. const BasicBlock *OtherBlock,
  368. const DominatorTree *DT,
  369. const PostDominatorTree *PDT) {
  370. assert(isControlFlowEquivalent(*ThisBlock, *OtherBlock, *DT, *PDT) &&
  371. "ThisBlock and OtherBlock must be CFG equivalent!");
  372. const BasicBlock *CommonDominator =
  373. DT->findNearestCommonDominator(ThisBlock, OtherBlock);
  374. if (CommonDominator == nullptr)
  375. return false;
  376. /// Recursively check the predecessors of \p ThisBlock up to
  377. /// their common dominator, and see if any of them post-dominates
  378. /// \p OtherBlock.
  379. SmallVector<const BasicBlock *, 8> WorkList;
  380. SmallPtrSet<const BasicBlock *, 8> Visited;
  381. WorkList.push_back(ThisBlock);
  382. while (!WorkList.empty()) {
  383. const BasicBlock *CurBlock = WorkList.back();
  384. WorkList.pop_back();
  385. Visited.insert(CurBlock);
  386. if (PDT->dominates(CurBlock, OtherBlock))
  387. return true;
  388. for (const auto *Pred : predecessors(CurBlock)) {
  389. if (Pred == CommonDominator || Visited.count(Pred))
  390. continue;
  391. WorkList.push_back(Pred);
  392. }
  393. }
  394. return false;
  395. }
  396. bool llvm::isReachedBefore(const Instruction *I0, const Instruction *I1,
  397. const DominatorTree *DT,
  398. const PostDominatorTree *PDT) {
  399. const BasicBlock *BB0 = I0->getParent();
  400. const BasicBlock *BB1 = I1->getParent();
  401. if (BB0 == BB1)
  402. return DT->dominates(I0, I1);
  403. return nonStrictlyPostDominate(BB1, BB0, DT, PDT);
  404. }