CodeMoverUtils.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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) {
  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 (!DT.dominates(&InsertPoint, &I))
  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 (!DT.dominates(&I, &InsertPoint))
  283. for (const Value *Op : I.operands())
  284. if (auto *OpInst = dyn_cast<Instruction>(Op))
  285. if (&InsertPoint == OpInst || !DT.dominates(OpInst, &InsertPoint))
  286. return false;
  287. DT.updateDFSNumbers();
  288. const bool MoveForward = domTreeLevelBefore(&DT, &I, &InsertPoint);
  289. Instruction &StartInst = (MoveForward ? I : InsertPoint);
  290. Instruction &EndInst = (MoveForward ? InsertPoint : I);
  291. SmallPtrSet<Instruction *, 10> InstsToCheck;
  292. collectInstructionsInBetween(StartInst, EndInst, InstsToCheck);
  293. if (!MoveForward)
  294. InstsToCheck.insert(&InsertPoint);
  295. // Check if there exists instructions which may throw, may synchonize, or may
  296. // never return, from I to InsertPoint.
  297. if (!isSafeToSpeculativelyExecute(&I))
  298. if (llvm::any_of(InstsToCheck, [](Instruction *I) {
  299. if (I->mayThrow())
  300. return true;
  301. const CallBase *CB = dyn_cast<CallBase>(I);
  302. if (!CB)
  303. return false;
  304. if (!CB->hasFnAttr(Attribute::WillReturn))
  305. return true;
  306. if (!CB->hasFnAttr(Attribute::NoSync))
  307. return true;
  308. return false;
  309. })) {
  310. return reportInvalidCandidate(I, MayThrowException);
  311. }
  312. // Check if I has any output/flow/anti dependences with instructions from \p
  313. // StartInst to \p EndInst.
  314. if (llvm::any_of(InstsToCheck, [&DI, &I](Instruction *CurInst) {
  315. auto DepResult = DI->depends(&I, CurInst, true);
  316. if (DepResult && (DepResult->isOutput() || DepResult->isFlow() ||
  317. DepResult->isAnti()))
  318. return true;
  319. return false;
  320. }))
  321. return reportInvalidCandidate(I, HasDependences);
  322. return true;
  323. }
  324. bool llvm::isSafeToMoveBefore(BasicBlock &BB, Instruction &InsertPoint,
  325. DominatorTree &DT, const PostDominatorTree *PDT,
  326. DependenceInfo *DI) {
  327. return llvm::all_of(BB, [&](Instruction &I) {
  328. if (BB.getTerminator() == &I)
  329. return true;
  330. return isSafeToMoveBefore(I, InsertPoint, DT, PDT, DI);
  331. });
  332. }
  333. void llvm::moveInstructionsToTheBeginning(BasicBlock &FromBB, BasicBlock &ToBB,
  334. DominatorTree &DT,
  335. const PostDominatorTree &PDT,
  336. DependenceInfo &DI) {
  337. for (auto It = ++FromBB.rbegin(); It != FromBB.rend();) {
  338. Instruction *MovePos = ToBB.getFirstNonPHIOrDbg();
  339. Instruction &I = *It;
  340. // Increment the iterator before modifying FromBB.
  341. ++It;
  342. if (isSafeToMoveBefore(I, *MovePos, DT, &PDT, &DI))
  343. I.moveBefore(MovePos);
  344. }
  345. }
  346. void llvm::moveInstructionsToTheEnd(BasicBlock &FromBB, BasicBlock &ToBB,
  347. DominatorTree &DT,
  348. const PostDominatorTree &PDT,
  349. DependenceInfo &DI) {
  350. Instruction *MovePos = ToBB.getTerminator();
  351. while (FromBB.size() > 1) {
  352. Instruction &I = FromBB.front();
  353. if (isSafeToMoveBefore(I, *MovePos, DT, &PDT, &DI))
  354. I.moveBefore(MovePos);
  355. }
  356. }