CodeMoverUtils.cpp 17 KB

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