StructurizeCFG.cpp 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235
  1. //===- StructurizeCFG.cpp -------------------------------------------------===//
  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. #include "llvm/Transforms/Scalar/StructurizeCFG.h"
  9. #include "llvm/ADT/DenseMap.h"
  10. #include "llvm/ADT/MapVector.h"
  11. #include "llvm/ADT/SCCIterator.h"
  12. #include "llvm/ADT/STLExtras.h"
  13. #include "llvm/ADT/SmallPtrSet.h"
  14. #include "llvm/ADT/SmallSet.h"
  15. #include "llvm/ADT/SmallVector.h"
  16. #include "llvm/Analysis/InstructionSimplify.h"
  17. #include "llvm/Analysis/LegacyDivergenceAnalysis.h"
  18. #include "llvm/Analysis/RegionInfo.h"
  19. #include "llvm/Analysis/RegionIterator.h"
  20. #include "llvm/Analysis/RegionPass.h"
  21. #include "llvm/IR/BasicBlock.h"
  22. #include "llvm/IR/CFG.h"
  23. #include "llvm/IR/Constants.h"
  24. #include "llvm/IR/Dominators.h"
  25. #include "llvm/IR/Function.h"
  26. #include "llvm/IR/InstrTypes.h"
  27. #include "llvm/IR/Instruction.h"
  28. #include "llvm/IR/Instructions.h"
  29. #include "llvm/IR/Metadata.h"
  30. #include "llvm/IR/PassManager.h"
  31. #include "llvm/IR/PatternMatch.h"
  32. #include "llvm/IR/Type.h"
  33. #include "llvm/IR/Use.h"
  34. #include "llvm/IR/Value.h"
  35. #include "llvm/IR/ValueHandle.h"
  36. #include "llvm/InitializePasses.h"
  37. #include "llvm/Pass.h"
  38. #include "llvm/Support/Casting.h"
  39. #include "llvm/Support/CommandLine.h"
  40. #include "llvm/Support/Debug.h"
  41. #include "llvm/Support/raw_ostream.h"
  42. #include "llvm/Transforms/Scalar.h"
  43. #include "llvm/Transforms/Utils.h"
  44. #include "llvm/Transforms/Utils/Local.h"
  45. #include "llvm/Transforms/Utils/SSAUpdater.h"
  46. #include <algorithm>
  47. #include <cassert>
  48. #include <utility>
  49. using namespace llvm;
  50. using namespace llvm::PatternMatch;
  51. #define DEBUG_TYPE "structurizecfg"
  52. // The name for newly created blocks.
  53. const char FlowBlockName[] = "Flow";
  54. namespace {
  55. static cl::opt<bool> ForceSkipUniformRegions(
  56. "structurizecfg-skip-uniform-regions",
  57. cl::Hidden,
  58. cl::desc("Force whether the StructurizeCFG pass skips uniform regions"),
  59. cl::init(false));
  60. static cl::opt<bool>
  61. RelaxedUniformRegions("structurizecfg-relaxed-uniform-regions", cl::Hidden,
  62. cl::desc("Allow relaxed uniform region checks"),
  63. cl::init(true));
  64. // Definition of the complex types used in this pass.
  65. using BBValuePair = std::pair<BasicBlock *, Value *>;
  66. using RNVector = SmallVector<RegionNode *, 8>;
  67. using BBVector = SmallVector<BasicBlock *, 8>;
  68. using BranchVector = SmallVector<BranchInst *, 8>;
  69. using BBValueVector = SmallVector<BBValuePair, 2>;
  70. using BBSet = SmallPtrSet<BasicBlock *, 8>;
  71. using PhiMap = MapVector<PHINode *, BBValueVector>;
  72. using BB2BBVecMap = MapVector<BasicBlock *, BBVector>;
  73. using BBPhiMap = DenseMap<BasicBlock *, PhiMap>;
  74. using BBPredicates = DenseMap<BasicBlock *, Value *>;
  75. using PredMap = DenseMap<BasicBlock *, BBPredicates>;
  76. using BB2BBMap = DenseMap<BasicBlock *, BasicBlock *>;
  77. using BranchDebugLocMap = DenseMap<BasicBlock *, DebugLoc>;
  78. // A traits type that is intended to be used in graph algorithms. The graph
  79. // traits starts at an entry node, and traverses the RegionNodes that are in
  80. // the Nodes set.
  81. struct SubGraphTraits {
  82. using NodeRef = std::pair<RegionNode *, SmallDenseSet<RegionNode *> *>;
  83. using BaseSuccIterator = GraphTraits<RegionNode *>::ChildIteratorType;
  84. // This wraps a set of Nodes into the iterator, so we know which edges to
  85. // filter out.
  86. class WrappedSuccIterator
  87. : public iterator_adaptor_base<
  88. WrappedSuccIterator, BaseSuccIterator,
  89. typename std::iterator_traits<BaseSuccIterator>::iterator_category,
  90. NodeRef, std::ptrdiff_t, NodeRef *, NodeRef> {
  91. SmallDenseSet<RegionNode *> *Nodes;
  92. public:
  93. WrappedSuccIterator(BaseSuccIterator It, SmallDenseSet<RegionNode *> *Nodes)
  94. : iterator_adaptor_base(It), Nodes(Nodes) {}
  95. NodeRef operator*() const { return {*I, Nodes}; }
  96. };
  97. static bool filterAll(const NodeRef &N) { return true; }
  98. static bool filterSet(const NodeRef &N) { return N.second->count(N.first); }
  99. using ChildIteratorType =
  100. filter_iterator<WrappedSuccIterator, bool (*)(const NodeRef &)>;
  101. static NodeRef getEntryNode(Region *R) {
  102. return {GraphTraits<Region *>::getEntryNode(R), nullptr};
  103. }
  104. static NodeRef getEntryNode(NodeRef N) { return N; }
  105. static iterator_range<ChildIteratorType> children(const NodeRef &N) {
  106. auto *filter = N.second ? &filterSet : &filterAll;
  107. return make_filter_range(
  108. make_range<WrappedSuccIterator>(
  109. {GraphTraits<RegionNode *>::child_begin(N.first), N.second},
  110. {GraphTraits<RegionNode *>::child_end(N.first), N.second}),
  111. filter);
  112. }
  113. static ChildIteratorType child_begin(const NodeRef &N) {
  114. return children(N).begin();
  115. }
  116. static ChildIteratorType child_end(const NodeRef &N) {
  117. return children(N).end();
  118. }
  119. };
  120. /// Finds the nearest common dominator of a set of BasicBlocks.
  121. ///
  122. /// For every BB you add to the set, you can specify whether we "remember" the
  123. /// block. When you get the common dominator, you can also ask whether it's one
  124. /// of the blocks we remembered.
  125. class NearestCommonDominator {
  126. DominatorTree *DT;
  127. BasicBlock *Result = nullptr;
  128. bool ResultIsRemembered = false;
  129. /// Add BB to the resulting dominator.
  130. void addBlock(BasicBlock *BB, bool Remember) {
  131. if (!Result) {
  132. Result = BB;
  133. ResultIsRemembered = Remember;
  134. return;
  135. }
  136. BasicBlock *NewResult = DT->findNearestCommonDominator(Result, BB);
  137. if (NewResult != Result)
  138. ResultIsRemembered = false;
  139. if (NewResult == BB)
  140. ResultIsRemembered |= Remember;
  141. Result = NewResult;
  142. }
  143. public:
  144. explicit NearestCommonDominator(DominatorTree *DomTree) : DT(DomTree) {}
  145. void addBlock(BasicBlock *BB) {
  146. addBlock(BB, /* Remember = */ false);
  147. }
  148. void addAndRememberBlock(BasicBlock *BB) {
  149. addBlock(BB, /* Remember = */ true);
  150. }
  151. /// Get the nearest common dominator of all the BBs added via addBlock() and
  152. /// addAndRememberBlock().
  153. BasicBlock *result() { return Result; }
  154. /// Is the BB returned by getResult() one of the blocks we added to the set
  155. /// with addAndRememberBlock()?
  156. bool resultIsRememberedBlock() { return ResultIsRemembered; }
  157. };
  158. /// Transforms the control flow graph on one single entry/exit region
  159. /// at a time.
  160. ///
  161. /// After the transform all "If"/"Then"/"Else" style control flow looks like
  162. /// this:
  163. ///
  164. /// \verbatim
  165. /// 1
  166. /// ||
  167. /// | |
  168. /// 2 |
  169. /// | /
  170. /// |/
  171. /// 3
  172. /// || Where:
  173. /// | | 1 = "If" block, calculates the condition
  174. /// 4 | 2 = "Then" subregion, runs if the condition is true
  175. /// | / 3 = "Flow" blocks, newly inserted flow blocks, rejoins the flow
  176. /// |/ 4 = "Else" optional subregion, runs if the condition is false
  177. /// 5 5 = "End" block, also rejoins the control flow
  178. /// \endverbatim
  179. ///
  180. /// Control flow is expressed as a branch where the true exit goes into the
  181. /// "Then"/"Else" region, while the false exit skips the region
  182. /// The condition for the optional "Else" region is expressed as a PHI node.
  183. /// The incoming values of the PHI node are true for the "If" edge and false
  184. /// for the "Then" edge.
  185. ///
  186. /// Additionally to that even complicated loops look like this:
  187. ///
  188. /// \verbatim
  189. /// 1
  190. /// ||
  191. /// | |
  192. /// 2 ^ Where:
  193. /// | / 1 = "Entry" block
  194. /// |/ 2 = "Loop" optional subregion, with all exits at "Flow" block
  195. /// 3 3 = "Flow" block, with back edge to entry block
  196. /// |
  197. /// \endverbatim
  198. ///
  199. /// The back edge of the "Flow" block is always on the false side of the branch
  200. /// while the true side continues the general flow. So the loop condition
  201. /// consist of a network of PHI nodes where the true incoming values expresses
  202. /// breaks and the false values expresses continue states.
  203. class StructurizeCFG {
  204. Type *Boolean;
  205. ConstantInt *BoolTrue;
  206. ConstantInt *BoolFalse;
  207. UndefValue *BoolUndef;
  208. Function *Func;
  209. Region *ParentRegion;
  210. LegacyDivergenceAnalysis *DA = nullptr;
  211. DominatorTree *DT;
  212. SmallVector<RegionNode *, 8> Order;
  213. BBSet Visited;
  214. BBSet FlowSet;
  215. SmallVector<WeakVH, 8> AffectedPhis;
  216. BBPhiMap DeletedPhis;
  217. BB2BBVecMap AddedPhis;
  218. PredMap Predicates;
  219. BranchVector Conditions;
  220. BB2BBMap Loops;
  221. PredMap LoopPreds;
  222. BranchVector LoopConds;
  223. BranchDebugLocMap TermDL;
  224. RegionNode *PrevNode;
  225. void orderNodes();
  226. void analyzeLoops(RegionNode *N);
  227. Value *buildCondition(BranchInst *Term, unsigned Idx, bool Invert);
  228. void gatherPredicates(RegionNode *N);
  229. void collectInfos();
  230. void insertConditions(bool Loops);
  231. void simplifyConditions();
  232. void delPhiValues(BasicBlock *From, BasicBlock *To);
  233. void addPhiValues(BasicBlock *From, BasicBlock *To);
  234. void findUndefBlocks(BasicBlock *PHIBlock,
  235. const SmallSet<BasicBlock *, 8> &Incomings,
  236. SmallVector<BasicBlock *> &UndefBlks) const;
  237. void setPhiValues();
  238. void simplifyAffectedPhis();
  239. void killTerminator(BasicBlock *BB);
  240. void changeExit(RegionNode *Node, BasicBlock *NewExit,
  241. bool IncludeDominator);
  242. BasicBlock *getNextFlow(BasicBlock *Dominator);
  243. BasicBlock *needPrefix(bool NeedEmpty);
  244. BasicBlock *needPostfix(BasicBlock *Flow, bool ExitUseAllowed);
  245. void setPrevNode(BasicBlock *BB);
  246. bool dominatesPredicates(BasicBlock *BB, RegionNode *Node);
  247. bool isPredictableTrue(RegionNode *Node);
  248. void wireFlow(bool ExitUseAllowed, BasicBlock *LoopEnd);
  249. void handleLoops(bool ExitUseAllowed, BasicBlock *LoopEnd);
  250. void createFlow();
  251. void rebuildSSA();
  252. public:
  253. void init(Region *R);
  254. bool run(Region *R, DominatorTree *DT);
  255. bool makeUniformRegion(Region *R, LegacyDivergenceAnalysis *DA);
  256. };
  257. class StructurizeCFGLegacyPass : public RegionPass {
  258. bool SkipUniformRegions;
  259. public:
  260. static char ID;
  261. explicit StructurizeCFGLegacyPass(bool SkipUniformRegions_ = false)
  262. : RegionPass(ID), SkipUniformRegions(SkipUniformRegions_) {
  263. if (ForceSkipUniformRegions.getNumOccurrences())
  264. SkipUniformRegions = ForceSkipUniformRegions.getValue();
  265. initializeStructurizeCFGLegacyPassPass(*PassRegistry::getPassRegistry());
  266. }
  267. bool runOnRegion(Region *R, RGPassManager &RGM) override {
  268. StructurizeCFG SCFG;
  269. SCFG.init(R);
  270. if (SkipUniformRegions) {
  271. LegacyDivergenceAnalysis *DA = &getAnalysis<LegacyDivergenceAnalysis>();
  272. if (SCFG.makeUniformRegion(R, DA))
  273. return false;
  274. }
  275. DominatorTree *DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
  276. return SCFG.run(R, DT);
  277. }
  278. StringRef getPassName() const override { return "Structurize control flow"; }
  279. void getAnalysisUsage(AnalysisUsage &AU) const override {
  280. if (SkipUniformRegions)
  281. AU.addRequired<LegacyDivergenceAnalysis>();
  282. AU.addRequiredID(LowerSwitchID);
  283. AU.addRequired<DominatorTreeWrapperPass>();
  284. AU.addPreserved<DominatorTreeWrapperPass>();
  285. RegionPass::getAnalysisUsage(AU);
  286. }
  287. };
  288. } // end anonymous namespace
  289. char StructurizeCFGLegacyPass::ID = 0;
  290. INITIALIZE_PASS_BEGIN(StructurizeCFGLegacyPass, "structurizecfg",
  291. "Structurize the CFG", false, false)
  292. INITIALIZE_PASS_DEPENDENCY(LegacyDivergenceAnalysis)
  293. INITIALIZE_PASS_DEPENDENCY(LowerSwitchLegacyPass)
  294. INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
  295. INITIALIZE_PASS_DEPENDENCY(RegionInfoPass)
  296. INITIALIZE_PASS_END(StructurizeCFGLegacyPass, "structurizecfg",
  297. "Structurize the CFG", false, false)
  298. /// Build up the general order of nodes, by performing a topological sort of the
  299. /// parent region's nodes, while ensuring that there is no outer cycle node
  300. /// between any two inner cycle nodes.
  301. void StructurizeCFG::orderNodes() {
  302. Order.resize(std::distance(GraphTraits<Region *>::nodes_begin(ParentRegion),
  303. GraphTraits<Region *>::nodes_end(ParentRegion)));
  304. if (Order.empty())
  305. return;
  306. SmallDenseSet<RegionNode *> Nodes;
  307. auto EntryNode = SubGraphTraits::getEntryNode(ParentRegion);
  308. // A list of range indices of SCCs in Order, to be processed.
  309. SmallVector<std::pair<unsigned, unsigned>, 8> WorkList;
  310. unsigned I = 0, E = Order.size();
  311. while (true) {
  312. // Run through all the SCCs in the subgraph starting with Entry.
  313. for (auto SCCI =
  314. scc_iterator<SubGraphTraits::NodeRef, SubGraphTraits>::begin(
  315. EntryNode);
  316. !SCCI.isAtEnd(); ++SCCI) {
  317. auto &SCC = *SCCI;
  318. // An SCC up to the size of 2, can be reduced to an entry (the last node),
  319. // and a possible additional node. Therefore, it is already in order, and
  320. // there is no need to add it to the work-list.
  321. unsigned Size = SCC.size();
  322. if (Size > 2)
  323. WorkList.emplace_back(I, I + Size);
  324. // Add the SCC nodes to the Order array.
  325. for (const auto &N : SCC) {
  326. assert(I < E && "SCC size mismatch!");
  327. Order[I++] = N.first;
  328. }
  329. }
  330. assert(I == E && "SCC size mismatch!");
  331. // If there are no more SCCs to order, then we are done.
  332. if (WorkList.empty())
  333. break;
  334. std::tie(I, E) = WorkList.pop_back_val();
  335. // Collect the set of nodes in the SCC's subgraph. These are only the
  336. // possible child nodes; we do not add the entry (last node) otherwise we
  337. // will have the same exact SCC all over again.
  338. Nodes.clear();
  339. Nodes.insert(Order.begin() + I, Order.begin() + E - 1);
  340. // Update the entry node.
  341. EntryNode.first = Order[E - 1];
  342. EntryNode.second = &Nodes;
  343. }
  344. }
  345. /// Determine the end of the loops
  346. void StructurizeCFG::analyzeLoops(RegionNode *N) {
  347. if (N->isSubRegion()) {
  348. // Test for exit as back edge
  349. BasicBlock *Exit = N->getNodeAs<Region>()->getExit();
  350. if (Visited.count(Exit))
  351. Loops[Exit] = N->getEntry();
  352. } else {
  353. // Test for successors as back edge
  354. BasicBlock *BB = N->getNodeAs<BasicBlock>();
  355. BranchInst *Term = cast<BranchInst>(BB->getTerminator());
  356. for (BasicBlock *Succ : Term->successors())
  357. if (Visited.count(Succ))
  358. Loops[Succ] = BB;
  359. }
  360. }
  361. /// Build the condition for one edge
  362. Value *StructurizeCFG::buildCondition(BranchInst *Term, unsigned Idx,
  363. bool Invert) {
  364. Value *Cond = Invert ? BoolFalse : BoolTrue;
  365. if (Term->isConditional()) {
  366. Cond = Term->getCondition();
  367. if (Idx != (unsigned)Invert)
  368. Cond = invertCondition(Cond);
  369. }
  370. return Cond;
  371. }
  372. /// Analyze the predecessors of each block and build up predicates
  373. void StructurizeCFG::gatherPredicates(RegionNode *N) {
  374. RegionInfo *RI = ParentRegion->getRegionInfo();
  375. BasicBlock *BB = N->getEntry();
  376. BBPredicates &Pred = Predicates[BB];
  377. BBPredicates &LPred = LoopPreds[BB];
  378. for (BasicBlock *P : predecessors(BB)) {
  379. // Ignore it if it's a branch from outside into our region entry
  380. if (!ParentRegion->contains(P))
  381. continue;
  382. Region *R = RI->getRegionFor(P);
  383. if (R == ParentRegion) {
  384. // It's a top level block in our region
  385. BranchInst *Term = cast<BranchInst>(P->getTerminator());
  386. for (unsigned i = 0, e = Term->getNumSuccessors(); i != e; ++i) {
  387. BasicBlock *Succ = Term->getSuccessor(i);
  388. if (Succ != BB)
  389. continue;
  390. if (Visited.count(P)) {
  391. // Normal forward edge
  392. if (Term->isConditional()) {
  393. // Try to treat it like an ELSE block
  394. BasicBlock *Other = Term->getSuccessor(!i);
  395. if (Visited.count(Other) && !Loops.count(Other) &&
  396. !Pred.count(Other) && !Pred.count(P)) {
  397. Pred[Other] = BoolFalse;
  398. Pred[P] = BoolTrue;
  399. continue;
  400. }
  401. }
  402. Pred[P] = buildCondition(Term, i, false);
  403. } else {
  404. // Back edge
  405. LPred[P] = buildCondition(Term, i, true);
  406. }
  407. }
  408. } else {
  409. // It's an exit from a sub region
  410. while (R->getParent() != ParentRegion)
  411. R = R->getParent();
  412. // Edge from inside a subregion to its entry, ignore it
  413. if (*R == *N)
  414. continue;
  415. BasicBlock *Entry = R->getEntry();
  416. if (Visited.count(Entry))
  417. Pred[Entry] = BoolTrue;
  418. else
  419. LPred[Entry] = BoolFalse;
  420. }
  421. }
  422. }
  423. /// Collect various loop and predicate infos
  424. void StructurizeCFG::collectInfos() {
  425. // Reset predicate
  426. Predicates.clear();
  427. // and loop infos
  428. Loops.clear();
  429. LoopPreds.clear();
  430. // Reset the visited nodes
  431. Visited.clear();
  432. for (RegionNode *RN : reverse(Order)) {
  433. LLVM_DEBUG(dbgs() << "Visiting: "
  434. << (RN->isSubRegion() ? "SubRegion with entry: " : "")
  435. << RN->getEntry()->getName() << "\n");
  436. // Analyze all the conditions leading to a node
  437. gatherPredicates(RN);
  438. // Remember that we've seen this node
  439. Visited.insert(RN->getEntry());
  440. // Find the last back edges
  441. analyzeLoops(RN);
  442. }
  443. // Reset the collected term debug locations
  444. TermDL.clear();
  445. for (BasicBlock &BB : *Func) {
  446. if (const DebugLoc &DL = BB.getTerminator()->getDebugLoc())
  447. TermDL[&BB] = DL;
  448. }
  449. }
  450. /// Insert the missing branch conditions
  451. void StructurizeCFG::insertConditions(bool Loops) {
  452. BranchVector &Conds = Loops ? LoopConds : Conditions;
  453. Value *Default = Loops ? BoolTrue : BoolFalse;
  454. SSAUpdater PhiInserter;
  455. for (BranchInst *Term : Conds) {
  456. assert(Term->isConditional());
  457. BasicBlock *Parent = Term->getParent();
  458. BasicBlock *SuccTrue = Term->getSuccessor(0);
  459. BasicBlock *SuccFalse = Term->getSuccessor(1);
  460. PhiInserter.Initialize(Boolean, "");
  461. PhiInserter.AddAvailableValue(&Func->getEntryBlock(), Default);
  462. PhiInserter.AddAvailableValue(Loops ? SuccFalse : Parent, Default);
  463. BBPredicates &Preds = Loops ? LoopPreds[SuccFalse] : Predicates[SuccTrue];
  464. NearestCommonDominator Dominator(DT);
  465. Dominator.addBlock(Parent);
  466. Value *ParentValue = nullptr;
  467. for (std::pair<BasicBlock *, Value *> BBAndPred : Preds) {
  468. BasicBlock *BB = BBAndPred.first;
  469. Value *Pred = BBAndPred.second;
  470. if (BB == Parent) {
  471. ParentValue = Pred;
  472. break;
  473. }
  474. PhiInserter.AddAvailableValue(BB, Pred);
  475. Dominator.addAndRememberBlock(BB);
  476. }
  477. if (ParentValue) {
  478. Term->setCondition(ParentValue);
  479. } else {
  480. if (!Dominator.resultIsRememberedBlock())
  481. PhiInserter.AddAvailableValue(Dominator.result(), Default);
  482. Term->setCondition(PhiInserter.GetValueInMiddleOfBlock(Parent));
  483. }
  484. }
  485. }
  486. /// Simplify any inverted conditions that were built by buildConditions.
  487. void StructurizeCFG::simplifyConditions() {
  488. SmallVector<Instruction *> InstToErase;
  489. for (auto &I : concat<PredMap::value_type>(Predicates, LoopPreds)) {
  490. auto &Preds = I.second;
  491. for (auto &J : Preds) {
  492. auto &Cond = J.second;
  493. Instruction *Inverted;
  494. if (match(Cond, m_Not(m_OneUse(m_Instruction(Inverted)))) &&
  495. !Cond->use_empty()) {
  496. if (auto *InvertedCmp = dyn_cast<CmpInst>(Inverted)) {
  497. InvertedCmp->setPredicate(InvertedCmp->getInversePredicate());
  498. Cond->replaceAllUsesWith(InvertedCmp);
  499. InstToErase.push_back(cast<Instruction>(Cond));
  500. }
  501. }
  502. }
  503. }
  504. for (auto *I : InstToErase)
  505. I->eraseFromParent();
  506. }
  507. /// Remove all PHI values coming from "From" into "To" and remember
  508. /// them in DeletedPhis
  509. void StructurizeCFG::delPhiValues(BasicBlock *From, BasicBlock *To) {
  510. PhiMap &Map = DeletedPhis[To];
  511. for (PHINode &Phi : To->phis()) {
  512. bool Recorded = false;
  513. while (Phi.getBasicBlockIndex(From) != -1) {
  514. Value *Deleted = Phi.removeIncomingValue(From, false);
  515. Map[&Phi].push_back(std::make_pair(From, Deleted));
  516. if (!Recorded) {
  517. AffectedPhis.push_back(&Phi);
  518. Recorded = true;
  519. }
  520. }
  521. }
  522. }
  523. /// Add a dummy PHI value as soon as we knew the new predecessor
  524. void StructurizeCFG::addPhiValues(BasicBlock *From, BasicBlock *To) {
  525. for (PHINode &Phi : To->phis()) {
  526. Value *Undef = UndefValue::get(Phi.getType());
  527. Phi.addIncoming(Undef, From);
  528. }
  529. AddedPhis[To].push_back(From);
  530. }
  531. /// When we are reconstructing a PHI inside \p PHIBlock with incoming values
  532. /// from predecessors \p Incomings, we have a chance to mark the available value
  533. /// from some blocks as undefined. The function will find out all such blocks
  534. /// and return in \p UndefBlks.
  535. void StructurizeCFG::findUndefBlocks(
  536. BasicBlock *PHIBlock, const SmallSet<BasicBlock *, 8> &Incomings,
  537. SmallVector<BasicBlock *> &UndefBlks) const {
  538. // We may get a post-structured CFG like below:
  539. //
  540. // | P1
  541. // |/
  542. // F1
  543. // |\
  544. // | N
  545. // |/
  546. // F2
  547. // |\
  548. // | P2
  549. // |/
  550. // F3
  551. // |\
  552. // B
  553. //
  554. // B is the block that has a PHI being reconstructed. P1/P2 are predecessors
  555. // of B before structurization. F1/F2/F3 are flow blocks inserted during
  556. // structurization process. Block N is not a predecessor of B before
  557. // structurization, but are placed between the predecessors(P1/P2) of B after
  558. // structurization. This usually means that threads went to N never take the
  559. // path N->F2->F3->B. For example, the threads take the branch F1->N may
  560. // always take the branch F2->P2. So, when we are reconstructing a PHI
  561. // originally in B, we can safely say the incoming value from N is undefined.
  562. SmallSet<BasicBlock *, 8> VisitedBlock;
  563. SmallVector<BasicBlock *, 8> Stack;
  564. if (PHIBlock == ParentRegion->getExit()) {
  565. for (auto P : predecessors(PHIBlock)) {
  566. if (ParentRegion->contains(P))
  567. Stack.push_back(P);
  568. }
  569. } else {
  570. append_range(Stack, predecessors(PHIBlock));
  571. }
  572. // Do a backward traversal over the CFG, and stop further searching if
  573. // the block is not a Flow. If a block is neither flow block nor the
  574. // incoming predecessor, then the incoming value from the block is
  575. // undefined value for the PHI being reconstructed.
  576. while (!Stack.empty()) {
  577. BasicBlock *Current = Stack.pop_back_val();
  578. if (VisitedBlock.contains(Current))
  579. continue;
  580. VisitedBlock.insert(Current);
  581. if (FlowSet.contains(Current)) {
  582. for (auto P : predecessors(Current))
  583. Stack.push_back(P);
  584. } else if (!Incomings.contains(Current)) {
  585. UndefBlks.push_back(Current);
  586. }
  587. }
  588. }
  589. /// Add the real PHI value as soon as everything is set up
  590. void StructurizeCFG::setPhiValues() {
  591. SmallVector<PHINode *, 8> InsertedPhis;
  592. SSAUpdater Updater(&InsertedPhis);
  593. for (const auto &AddedPhi : AddedPhis) {
  594. BasicBlock *To = AddedPhi.first;
  595. const BBVector &From = AddedPhi.second;
  596. if (!DeletedPhis.count(To))
  597. continue;
  598. SmallVector<BasicBlock *> UndefBlks;
  599. bool CachedUndefs = false;
  600. PhiMap &Map = DeletedPhis[To];
  601. for (const auto &PI : Map) {
  602. PHINode *Phi = PI.first;
  603. Value *Undef = UndefValue::get(Phi->getType());
  604. Updater.Initialize(Phi->getType(), "");
  605. Updater.AddAvailableValue(&Func->getEntryBlock(), Undef);
  606. Updater.AddAvailableValue(To, Undef);
  607. SmallSet<BasicBlock *, 8> Incomings;
  608. SmallVector<BasicBlock *> ConstantPreds;
  609. for (const auto &VI : PI.second) {
  610. Incomings.insert(VI.first);
  611. Updater.AddAvailableValue(VI.first, VI.second);
  612. if (isa<Constant>(VI.second))
  613. ConstantPreds.push_back(VI.first);
  614. }
  615. if (!CachedUndefs) {
  616. findUndefBlocks(To, Incomings, UndefBlks);
  617. CachedUndefs = true;
  618. }
  619. for (auto UB : UndefBlks) {
  620. // If this undef block is dominated by any predecessor(before
  621. // structurization) of reconstructed PHI with constant incoming value,
  622. // don't mark the available value as undefined. Setting undef to such
  623. // block will stop us from getting optimal phi insertion.
  624. if (any_of(ConstantPreds,
  625. [&](BasicBlock *CP) { return DT->dominates(CP, UB); }))
  626. continue;
  627. Updater.AddAvailableValue(UB, Undef);
  628. }
  629. for (BasicBlock *FI : From)
  630. Phi->setIncomingValueForBlock(FI, Updater.GetValueAtEndOfBlock(FI));
  631. AffectedPhis.push_back(Phi);
  632. }
  633. DeletedPhis.erase(To);
  634. }
  635. assert(DeletedPhis.empty());
  636. AffectedPhis.append(InsertedPhis.begin(), InsertedPhis.end());
  637. }
  638. void StructurizeCFG::simplifyAffectedPhis() {
  639. bool Changed;
  640. do {
  641. Changed = false;
  642. SimplifyQuery Q(Func->getParent()->getDataLayout());
  643. Q.DT = DT;
  644. // Setting CanUseUndef to true might extend value liveness, set it to false
  645. // to achieve better register pressure.
  646. Q.CanUseUndef = false;
  647. for (WeakVH VH : AffectedPhis) {
  648. if (auto Phi = dyn_cast_or_null<PHINode>(VH)) {
  649. if (auto NewValue = simplifyInstruction(Phi, Q)) {
  650. Phi->replaceAllUsesWith(NewValue);
  651. Phi->eraseFromParent();
  652. Changed = true;
  653. }
  654. }
  655. }
  656. } while (Changed);
  657. }
  658. /// Remove phi values from all successors and then remove the terminator.
  659. void StructurizeCFG::killTerminator(BasicBlock *BB) {
  660. Instruction *Term = BB->getTerminator();
  661. if (!Term)
  662. return;
  663. for (BasicBlock *Succ : successors(BB))
  664. delPhiValues(BB, Succ);
  665. if (DA)
  666. DA->removeValue(Term);
  667. Term->eraseFromParent();
  668. }
  669. /// Let node exit(s) point to NewExit
  670. void StructurizeCFG::changeExit(RegionNode *Node, BasicBlock *NewExit,
  671. bool IncludeDominator) {
  672. if (Node->isSubRegion()) {
  673. Region *SubRegion = Node->getNodeAs<Region>();
  674. BasicBlock *OldExit = SubRegion->getExit();
  675. BasicBlock *Dominator = nullptr;
  676. // Find all the edges from the sub region to the exit.
  677. // We use make_early_inc_range here because we modify BB's terminator.
  678. for (BasicBlock *BB : llvm::make_early_inc_range(predecessors(OldExit))) {
  679. if (!SubRegion->contains(BB))
  680. continue;
  681. // Modify the edges to point to the new exit
  682. delPhiValues(BB, OldExit);
  683. BB->getTerminator()->replaceUsesOfWith(OldExit, NewExit);
  684. addPhiValues(BB, NewExit);
  685. // Find the new dominator (if requested)
  686. if (IncludeDominator) {
  687. if (!Dominator)
  688. Dominator = BB;
  689. else
  690. Dominator = DT->findNearestCommonDominator(Dominator, BB);
  691. }
  692. }
  693. // Change the dominator (if requested)
  694. if (Dominator)
  695. DT->changeImmediateDominator(NewExit, Dominator);
  696. // Update the region info
  697. SubRegion->replaceExit(NewExit);
  698. } else {
  699. BasicBlock *BB = Node->getNodeAs<BasicBlock>();
  700. killTerminator(BB);
  701. BranchInst *Br = BranchInst::Create(NewExit, BB);
  702. Br->setDebugLoc(TermDL[BB]);
  703. addPhiValues(BB, NewExit);
  704. if (IncludeDominator)
  705. DT->changeImmediateDominator(NewExit, BB);
  706. }
  707. }
  708. /// Create a new flow node and update dominator tree and region info
  709. BasicBlock *StructurizeCFG::getNextFlow(BasicBlock *Dominator) {
  710. LLVMContext &Context = Func->getContext();
  711. BasicBlock *Insert = Order.empty() ? ParentRegion->getExit() :
  712. Order.back()->getEntry();
  713. BasicBlock *Flow = BasicBlock::Create(Context, FlowBlockName,
  714. Func, Insert);
  715. FlowSet.insert(Flow);
  716. // use a temporary variable to avoid a use-after-free if the map's storage is
  717. // reallocated
  718. DebugLoc DL = TermDL[Dominator];
  719. TermDL[Flow] = std::move(DL);
  720. DT->addNewBlock(Flow, Dominator);
  721. ParentRegion->getRegionInfo()->setRegionFor(Flow, ParentRegion);
  722. return Flow;
  723. }
  724. /// Create a new or reuse the previous node as flow node
  725. BasicBlock *StructurizeCFG::needPrefix(bool NeedEmpty) {
  726. BasicBlock *Entry = PrevNode->getEntry();
  727. if (!PrevNode->isSubRegion()) {
  728. killTerminator(Entry);
  729. if (!NeedEmpty || Entry->getFirstInsertionPt() == Entry->end())
  730. return Entry;
  731. }
  732. // create a new flow node
  733. BasicBlock *Flow = getNextFlow(Entry);
  734. // and wire it up
  735. changeExit(PrevNode, Flow, true);
  736. PrevNode = ParentRegion->getBBNode(Flow);
  737. return Flow;
  738. }
  739. /// Returns the region exit if possible, otherwise just a new flow node
  740. BasicBlock *StructurizeCFG::needPostfix(BasicBlock *Flow,
  741. bool ExitUseAllowed) {
  742. if (!Order.empty() || !ExitUseAllowed)
  743. return getNextFlow(Flow);
  744. BasicBlock *Exit = ParentRegion->getExit();
  745. DT->changeImmediateDominator(Exit, Flow);
  746. addPhiValues(Flow, Exit);
  747. return Exit;
  748. }
  749. /// Set the previous node
  750. void StructurizeCFG::setPrevNode(BasicBlock *BB) {
  751. PrevNode = ParentRegion->contains(BB) ? ParentRegion->getBBNode(BB)
  752. : nullptr;
  753. }
  754. /// Does BB dominate all the predicates of Node?
  755. bool StructurizeCFG::dominatesPredicates(BasicBlock *BB, RegionNode *Node) {
  756. BBPredicates &Preds = Predicates[Node->getEntry()];
  757. return llvm::all_of(Preds, [&](std::pair<BasicBlock *, Value *> Pred) {
  758. return DT->dominates(BB, Pred.first);
  759. });
  760. }
  761. /// Can we predict that this node will always be called?
  762. bool StructurizeCFG::isPredictableTrue(RegionNode *Node) {
  763. BBPredicates &Preds = Predicates[Node->getEntry()];
  764. bool Dominated = false;
  765. // Regionentry is always true
  766. if (!PrevNode)
  767. return true;
  768. for (std::pair<BasicBlock*, Value*> Pred : Preds) {
  769. BasicBlock *BB = Pred.first;
  770. Value *V = Pred.second;
  771. if (V != BoolTrue)
  772. return false;
  773. if (!Dominated && DT->dominates(BB, PrevNode->getEntry()))
  774. Dominated = true;
  775. }
  776. // TODO: The dominator check is too strict
  777. return Dominated;
  778. }
  779. /// Take one node from the order vector and wire it up
  780. void StructurizeCFG::wireFlow(bool ExitUseAllowed,
  781. BasicBlock *LoopEnd) {
  782. RegionNode *Node = Order.pop_back_val();
  783. Visited.insert(Node->getEntry());
  784. if (isPredictableTrue(Node)) {
  785. // Just a linear flow
  786. if (PrevNode) {
  787. changeExit(PrevNode, Node->getEntry(), true);
  788. }
  789. PrevNode = Node;
  790. } else {
  791. // Insert extra prefix node (or reuse last one)
  792. BasicBlock *Flow = needPrefix(false);
  793. // Insert extra postfix node (or use exit instead)
  794. BasicBlock *Entry = Node->getEntry();
  795. BasicBlock *Next = needPostfix(Flow, ExitUseAllowed);
  796. // let it point to entry and next block
  797. BranchInst *Br = BranchInst::Create(Entry, Next, BoolUndef, Flow);
  798. Br->setDebugLoc(TermDL[Flow]);
  799. Conditions.push_back(Br);
  800. addPhiValues(Flow, Entry);
  801. DT->changeImmediateDominator(Entry, Flow);
  802. PrevNode = Node;
  803. while (!Order.empty() && !Visited.count(LoopEnd) &&
  804. dominatesPredicates(Entry, Order.back())) {
  805. handleLoops(false, LoopEnd);
  806. }
  807. changeExit(PrevNode, Next, false);
  808. setPrevNode(Next);
  809. }
  810. }
  811. void StructurizeCFG::handleLoops(bool ExitUseAllowed,
  812. BasicBlock *LoopEnd) {
  813. RegionNode *Node = Order.back();
  814. BasicBlock *LoopStart = Node->getEntry();
  815. if (!Loops.count(LoopStart)) {
  816. wireFlow(ExitUseAllowed, LoopEnd);
  817. return;
  818. }
  819. if (!isPredictableTrue(Node))
  820. LoopStart = needPrefix(true);
  821. LoopEnd = Loops[Node->getEntry()];
  822. wireFlow(false, LoopEnd);
  823. while (!Visited.count(LoopEnd)) {
  824. handleLoops(false, LoopEnd);
  825. }
  826. assert(LoopStart != &LoopStart->getParent()->getEntryBlock());
  827. // Create an extra loop end node
  828. LoopEnd = needPrefix(false);
  829. BasicBlock *Next = needPostfix(LoopEnd, ExitUseAllowed);
  830. BranchInst *Br = BranchInst::Create(Next, LoopStart, BoolUndef, LoopEnd);
  831. Br->setDebugLoc(TermDL[LoopEnd]);
  832. LoopConds.push_back(Br);
  833. addPhiValues(LoopEnd, LoopStart);
  834. setPrevNode(Next);
  835. }
  836. /// After this function control flow looks like it should be, but
  837. /// branches and PHI nodes only have undefined conditions.
  838. void StructurizeCFG::createFlow() {
  839. BasicBlock *Exit = ParentRegion->getExit();
  840. bool EntryDominatesExit = DT->dominates(ParentRegion->getEntry(), Exit);
  841. AffectedPhis.clear();
  842. DeletedPhis.clear();
  843. AddedPhis.clear();
  844. Conditions.clear();
  845. LoopConds.clear();
  846. PrevNode = nullptr;
  847. Visited.clear();
  848. while (!Order.empty()) {
  849. handleLoops(EntryDominatesExit, nullptr);
  850. }
  851. if (PrevNode)
  852. changeExit(PrevNode, Exit, EntryDominatesExit);
  853. else
  854. assert(EntryDominatesExit);
  855. }
  856. /// Handle a rare case where the disintegrated nodes instructions
  857. /// no longer dominate all their uses. Not sure if this is really necessary
  858. void StructurizeCFG::rebuildSSA() {
  859. SSAUpdater Updater;
  860. for (BasicBlock *BB : ParentRegion->blocks())
  861. for (Instruction &I : *BB) {
  862. bool Initialized = false;
  863. // We may modify the use list as we iterate over it, so we use
  864. // make_early_inc_range.
  865. for (Use &U : llvm::make_early_inc_range(I.uses())) {
  866. Instruction *User = cast<Instruction>(U.getUser());
  867. if (User->getParent() == BB) {
  868. continue;
  869. } else if (PHINode *UserPN = dyn_cast<PHINode>(User)) {
  870. if (UserPN->getIncomingBlock(U) == BB)
  871. continue;
  872. }
  873. if (DT->dominates(&I, User))
  874. continue;
  875. if (!Initialized) {
  876. Value *Undef = UndefValue::get(I.getType());
  877. Updater.Initialize(I.getType(), "");
  878. Updater.AddAvailableValue(&Func->getEntryBlock(), Undef);
  879. Updater.AddAvailableValue(BB, &I);
  880. Initialized = true;
  881. }
  882. Updater.RewriteUseAfterInsertions(U);
  883. }
  884. }
  885. }
  886. static bool hasOnlyUniformBranches(Region *R, unsigned UniformMDKindID,
  887. const LegacyDivergenceAnalysis &DA) {
  888. // Bool for if all sub-regions are uniform.
  889. bool SubRegionsAreUniform = true;
  890. // Count of how many direct children are conditional.
  891. unsigned ConditionalDirectChildren = 0;
  892. for (auto *E : R->elements()) {
  893. if (!E->isSubRegion()) {
  894. auto Br = dyn_cast<BranchInst>(E->getEntry()->getTerminator());
  895. if (!Br || !Br->isConditional())
  896. continue;
  897. if (!DA.isUniform(Br))
  898. return false;
  899. // One of our direct children is conditional.
  900. ConditionalDirectChildren++;
  901. LLVM_DEBUG(dbgs() << "BB: " << Br->getParent()->getName()
  902. << " has uniform terminator\n");
  903. } else {
  904. // Explicitly refuse to treat regions as uniform if they have non-uniform
  905. // subregions. We cannot rely on DivergenceAnalysis for branches in
  906. // subregions because those branches may have been removed and re-created,
  907. // so we look for our metadata instead.
  908. //
  909. // Warning: It would be nice to treat regions as uniform based only on
  910. // their direct child basic blocks' terminators, regardless of whether
  911. // subregions are uniform or not. However, this requires a very careful
  912. // look at SIAnnotateControlFlow to make sure nothing breaks there.
  913. for (auto *BB : E->getNodeAs<Region>()->blocks()) {
  914. auto Br = dyn_cast<BranchInst>(BB->getTerminator());
  915. if (!Br || !Br->isConditional())
  916. continue;
  917. if (!Br->getMetadata(UniformMDKindID)) {
  918. // Early exit if we cannot have relaxed uniform regions.
  919. if (!RelaxedUniformRegions)
  920. return false;
  921. SubRegionsAreUniform = false;
  922. break;
  923. }
  924. }
  925. }
  926. }
  927. // Our region is uniform if:
  928. // 1. All conditional branches that are direct children are uniform (checked
  929. // above).
  930. // 2. And either:
  931. // a. All sub-regions are uniform.
  932. // b. There is one or less conditional branches among the direct children.
  933. return SubRegionsAreUniform || (ConditionalDirectChildren <= 1);
  934. }
  935. void StructurizeCFG::init(Region *R) {
  936. LLVMContext &Context = R->getEntry()->getContext();
  937. Boolean = Type::getInt1Ty(Context);
  938. BoolTrue = ConstantInt::getTrue(Context);
  939. BoolFalse = ConstantInt::getFalse(Context);
  940. BoolUndef = UndefValue::get(Boolean);
  941. this->DA = nullptr;
  942. }
  943. bool StructurizeCFG::makeUniformRegion(Region *R,
  944. LegacyDivergenceAnalysis *DA) {
  945. if (R->isTopLevelRegion())
  946. return false;
  947. this->DA = DA;
  948. // TODO: We could probably be smarter here with how we handle sub-regions.
  949. // We currently rely on the fact that metadata is set by earlier invocations
  950. // of the pass on sub-regions, and that this metadata doesn't get lost --
  951. // but we shouldn't rely on metadata for correctness!
  952. unsigned UniformMDKindID =
  953. R->getEntry()->getContext().getMDKindID("structurizecfg.uniform");
  954. if (hasOnlyUniformBranches(R, UniformMDKindID, *DA)) {
  955. LLVM_DEBUG(dbgs() << "Skipping region with uniform control flow: " << *R
  956. << '\n');
  957. // Mark all direct child block terminators as having been treated as
  958. // uniform. To account for a possible future in which non-uniform
  959. // sub-regions are treated more cleverly, indirect children are not
  960. // marked as uniform.
  961. MDNode *MD = MDNode::get(R->getEntry()->getParent()->getContext(), {});
  962. for (RegionNode *E : R->elements()) {
  963. if (E->isSubRegion())
  964. continue;
  965. if (Instruction *Term = E->getEntry()->getTerminator())
  966. Term->setMetadata(UniformMDKindID, MD);
  967. }
  968. return true;
  969. }
  970. return false;
  971. }
  972. /// Run the transformation for each region found
  973. bool StructurizeCFG::run(Region *R, DominatorTree *DT) {
  974. if (R->isTopLevelRegion())
  975. return false;
  976. this->DT = DT;
  977. Func = R->getEntry()->getParent();
  978. ParentRegion = R;
  979. orderNodes();
  980. collectInfos();
  981. createFlow();
  982. insertConditions(false);
  983. insertConditions(true);
  984. setPhiValues();
  985. simplifyConditions();
  986. simplifyAffectedPhis();
  987. rebuildSSA();
  988. // Cleanup
  989. Order.clear();
  990. Visited.clear();
  991. DeletedPhis.clear();
  992. AddedPhis.clear();
  993. Predicates.clear();
  994. Conditions.clear();
  995. Loops.clear();
  996. LoopPreds.clear();
  997. LoopConds.clear();
  998. FlowSet.clear();
  999. TermDL.clear();
  1000. return true;
  1001. }
  1002. Pass *llvm::createStructurizeCFGPass(bool SkipUniformRegions) {
  1003. return new StructurizeCFGLegacyPass(SkipUniformRegions);
  1004. }
  1005. static void addRegionIntoQueue(Region &R, std::vector<Region *> &Regions) {
  1006. Regions.push_back(&R);
  1007. for (const auto &E : R)
  1008. addRegionIntoQueue(*E, Regions);
  1009. }
  1010. PreservedAnalyses StructurizeCFGPass::run(Function &F,
  1011. FunctionAnalysisManager &AM) {
  1012. bool Changed = false;
  1013. DominatorTree *DT = &AM.getResult<DominatorTreeAnalysis>(F);
  1014. auto &RI = AM.getResult<RegionInfoAnalysis>(F);
  1015. std::vector<Region *> Regions;
  1016. addRegionIntoQueue(*RI.getTopLevelRegion(), Regions);
  1017. while (!Regions.empty()) {
  1018. Region *R = Regions.back();
  1019. StructurizeCFG SCFG;
  1020. SCFG.init(R);
  1021. Changed |= SCFG.run(R, DT);
  1022. Regions.pop_back();
  1023. }
  1024. if (!Changed)
  1025. return PreservedAnalyses::all();
  1026. PreservedAnalyses PA;
  1027. PA.preserve<DominatorTreeAnalysis>();
  1028. return PA;
  1029. }