StructurizeCFG.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957
  1. //===-- StructurizeCFG.cpp ------------------------------------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #include "llvm/Transforms/Scalar.h"
  10. #include "llvm/ADT/MapVector.h"
  11. #include "llvm/ADT/PostOrderIterator.h"
  12. #include "llvm/ADT/SCCIterator.h"
  13. #include "llvm/Analysis/LoopInfo.h"
  14. #include "llvm/Analysis/RegionInfo.h"
  15. #include "llvm/Analysis/RegionIterator.h"
  16. #include "llvm/Analysis/RegionPass.h"
  17. #include "llvm/IR/Module.h"
  18. #include "llvm/IR/PatternMatch.h"
  19. #include "llvm/Support/Debug.h"
  20. #include "llvm/Support/raw_ostream.h"
  21. #include "llvm/Transforms/Utils/SSAUpdater.h"
  22. using namespace llvm;
  23. using namespace llvm::PatternMatch;
  24. #define DEBUG_TYPE "structurizecfg"
  25. namespace {
  26. // Definition of the complex types used in this pass.
  27. typedef std::pair<BasicBlock *, Value *> BBValuePair;
  28. typedef SmallVector<RegionNode*, 8> RNVector;
  29. typedef SmallVector<BasicBlock*, 8> BBVector;
  30. typedef SmallVector<BranchInst*, 8> BranchVector;
  31. typedef SmallVector<BBValuePair, 2> BBValueVector;
  32. typedef SmallPtrSet<BasicBlock *, 8> BBSet;
  33. typedef MapVector<PHINode *, BBValueVector> PhiMap;
  34. typedef MapVector<BasicBlock *, BBVector> BB2BBVecMap;
  35. typedef DenseMap<DomTreeNode *, unsigned> DTN2UnsignedMap;
  36. typedef DenseMap<BasicBlock *, PhiMap> BBPhiMap;
  37. typedef DenseMap<BasicBlock *, Value *> BBPredicates;
  38. typedef DenseMap<BasicBlock *, BBPredicates> PredMap;
  39. typedef DenseMap<BasicBlock *, BasicBlock*> BB2BBMap;
  40. // The name for newly created blocks.
  41. static const char *const FlowBlockName = "Flow";
  42. /// @brief Find the nearest common dominator for multiple BasicBlocks
  43. ///
  44. /// Helper class for StructurizeCFG
  45. /// TODO: Maybe move into common code
  46. class NearestCommonDominator {
  47. DominatorTree *DT;
  48. DTN2UnsignedMap IndexMap;
  49. BasicBlock *Result;
  50. unsigned ResultIndex;
  51. bool ExplicitMentioned;
  52. public:
  53. /// \brief Start a new query
  54. NearestCommonDominator(DominatorTree *DomTree) {
  55. DT = DomTree;
  56. Result = nullptr;
  57. }
  58. /// \brief Add BB to the resulting dominator
  59. void addBlock(BasicBlock *BB, bool Remember = true) {
  60. DomTreeNode *Node = DT->getNode(BB);
  61. if (!Result) {
  62. unsigned Numbering = 0;
  63. for (;Node;Node = Node->getIDom())
  64. IndexMap[Node] = ++Numbering;
  65. Result = BB;
  66. ResultIndex = 1;
  67. ExplicitMentioned = Remember;
  68. return;
  69. }
  70. for (;Node;Node = Node->getIDom())
  71. if (IndexMap.count(Node))
  72. break;
  73. else
  74. IndexMap[Node] = 0;
  75. assert(Node && "Dominator tree invalid!");
  76. unsigned Numbering = IndexMap[Node];
  77. if (Numbering > ResultIndex) {
  78. Result = Node->getBlock();
  79. ResultIndex = Numbering;
  80. ExplicitMentioned = Remember && (Result == BB);
  81. } else if (Numbering == ResultIndex) {
  82. ExplicitMentioned |= Remember;
  83. }
  84. }
  85. /// \brief Is "Result" one of the BBs added with "Remember" = True?
  86. bool wasResultExplicitMentioned() {
  87. return ExplicitMentioned;
  88. }
  89. /// \brief Get the query result
  90. BasicBlock *getResult() {
  91. return Result;
  92. }
  93. };
  94. /// @brief Transforms the control flow graph on one single entry/exit region
  95. /// at a time.
  96. ///
  97. /// After the transform all "If"/"Then"/"Else" style control flow looks like
  98. /// this:
  99. ///
  100. /// \verbatim
  101. /// 1
  102. /// ||
  103. /// | |
  104. /// 2 |
  105. /// | /
  106. /// |/
  107. /// 3
  108. /// || Where:
  109. /// | | 1 = "If" block, calculates the condition
  110. /// 4 | 2 = "Then" subregion, runs if the condition is true
  111. /// | / 3 = "Flow" blocks, newly inserted flow blocks, rejoins the flow
  112. /// |/ 4 = "Else" optional subregion, runs if the condition is false
  113. /// 5 5 = "End" block, also rejoins the control flow
  114. /// \endverbatim
  115. ///
  116. /// Control flow is expressed as a branch where the true exit goes into the
  117. /// "Then"/"Else" region, while the false exit skips the region
  118. /// The condition for the optional "Else" region is expressed as a PHI node.
  119. /// The incomming values of the PHI node are true for the "If" edge and false
  120. /// for the "Then" edge.
  121. ///
  122. /// Additionally to that even complicated loops look like this:
  123. ///
  124. /// \verbatim
  125. /// 1
  126. /// ||
  127. /// | |
  128. /// 2 ^ Where:
  129. /// | / 1 = "Entry" block
  130. /// |/ 2 = "Loop" optional subregion, with all exits at "Flow" block
  131. /// 3 3 = "Flow" block, with back edge to entry block
  132. /// |
  133. /// \endverbatim
  134. ///
  135. /// The back edge of the "Flow" block is always on the false side of the branch
  136. /// while the true side continues the general flow. So the loop condition
  137. /// consist of a network of PHI nodes where the true incoming values expresses
  138. /// breaks and the false values expresses continue states.
  139. class StructurizeCFG : public RegionPass {
  140. Type *Boolean;
  141. ConstantInt *BoolTrue;
  142. ConstantInt *BoolFalse;
  143. UndefValue *BoolUndef;
  144. Function *Func;
  145. Region *ParentRegion;
  146. DominatorTree *DT;
  147. LoopInfo *LI;
  148. RNVector Order;
  149. BBSet Visited;
  150. BBPhiMap DeletedPhis;
  151. BB2BBVecMap AddedPhis;
  152. PredMap Predicates;
  153. BranchVector Conditions;
  154. BB2BBMap Loops;
  155. PredMap LoopPreds;
  156. BranchVector LoopConds;
  157. RegionNode *PrevNode;
  158. void orderNodes();
  159. void analyzeLoops(RegionNode *N);
  160. Value *invert(Value *Condition);
  161. Value *buildCondition(BranchInst *Term, unsigned Idx, bool Invert);
  162. void gatherPredicates(RegionNode *N);
  163. void collectInfos();
  164. void insertConditions(bool Loops);
  165. void delPhiValues(BasicBlock *From, BasicBlock *To);
  166. void addPhiValues(BasicBlock *From, BasicBlock *To);
  167. void setPhiValues();
  168. void killTerminator(BasicBlock *BB);
  169. void changeExit(RegionNode *Node, BasicBlock *NewExit,
  170. bool IncludeDominator);
  171. BasicBlock *getNextFlow(BasicBlock *Dominator);
  172. BasicBlock *needPrefix(bool NeedEmpty);
  173. BasicBlock *needPostfix(BasicBlock *Flow, bool ExitUseAllowed);
  174. void setPrevNode(BasicBlock *BB);
  175. bool dominatesPredicates(BasicBlock *BB, RegionNode *Node);
  176. bool isPredictableTrue(RegionNode *Node);
  177. void wireFlow(bool ExitUseAllowed, BasicBlock *LoopEnd);
  178. void handleLoops(bool ExitUseAllowed, BasicBlock *LoopEnd);
  179. void createFlow();
  180. void rebuildSSA();
  181. public:
  182. static char ID;
  183. StructurizeCFG() :
  184. RegionPass(ID) {
  185. initializeStructurizeCFGPass(*PassRegistry::getPassRegistry());
  186. }
  187. using Pass::doInitialization;
  188. bool doInitialization(Region *R, RGPassManager &RGM) override;
  189. bool runOnRegion(Region *R, RGPassManager &RGM) override;
  190. const char *getPassName() const override {
  191. return "Structurize control flow";
  192. }
  193. void getAnalysisUsage(AnalysisUsage &AU) const override {
  194. AU.addRequiredID(LowerSwitchID);
  195. AU.addRequired<DominatorTreeWrapperPass>();
  196. AU.addRequired<LoopInfoWrapperPass>();
  197. AU.addPreserved<DominatorTreeWrapperPass>();
  198. RegionPass::getAnalysisUsage(AU);
  199. }
  200. };
  201. } // end anonymous namespace
  202. char StructurizeCFG::ID = 0;
  203. INITIALIZE_PASS_BEGIN(StructurizeCFG, "structurizecfg", "Structurize the CFG",
  204. false, false)
  205. INITIALIZE_PASS_DEPENDENCY(LowerSwitch)
  206. INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
  207. INITIALIZE_PASS_DEPENDENCY(RegionInfoPass)
  208. INITIALIZE_PASS_END(StructurizeCFG, "structurizecfg", "Structurize the CFG",
  209. false, false)
  210. /// \brief Initialize the types and constants used in the pass
  211. bool StructurizeCFG::doInitialization(Region *R, RGPassManager &RGM) {
  212. LLVMContext &Context = R->getEntry()->getContext();
  213. Boolean = Type::getInt1Ty(Context);
  214. BoolTrue = ConstantInt::getTrue(Context);
  215. BoolFalse = ConstantInt::getFalse(Context);
  216. BoolUndef = UndefValue::get(Boolean);
  217. return false;
  218. }
  219. /// \brief Build up the general order of nodes
  220. void StructurizeCFG::orderNodes() {
  221. RNVector TempOrder;
  222. ReversePostOrderTraversal<Region*> RPOT(ParentRegion);
  223. TempOrder.append(RPOT.begin(), RPOT.end());
  224. std::map<Loop*, unsigned> LoopBlocks;
  225. // The reverse post-order traversal of the list gives us an ordering close
  226. // to what we want. The only problem with it is that sometimes backedges
  227. // for outer loops will be visited before backedges for inner loops.
  228. for (RegionNode *RN : TempOrder) {
  229. BasicBlock *BB = RN->getEntry();
  230. Loop *Loop = LI->getLoopFor(BB);
  231. if (!LoopBlocks.count(Loop)) {
  232. LoopBlocks[Loop] = 1;
  233. continue;
  234. }
  235. LoopBlocks[Loop]++;
  236. }
  237. unsigned CurrentLoopDepth = 0;
  238. Loop *CurrentLoop = nullptr;
  239. BBSet TempVisited;
  240. for (RNVector::iterator I = TempOrder.begin(), E = TempOrder.end(); I != E; ++I) {
  241. BasicBlock *BB = (*I)->getEntry();
  242. unsigned LoopDepth = LI->getLoopDepth(BB);
  243. if (std::find(Order.begin(), Order.end(), *I) != Order.end())
  244. continue;
  245. if (LoopDepth < CurrentLoopDepth) {
  246. // Make sure we have visited all blocks in this loop before moving back to
  247. // the outer loop.
  248. RNVector::iterator LoopI = I;
  249. while(LoopBlocks[CurrentLoop]) {
  250. LoopI++;
  251. BasicBlock *LoopBB = (*LoopI)->getEntry();
  252. if (LI->getLoopFor(LoopBB) == CurrentLoop) {
  253. LoopBlocks[CurrentLoop]--;
  254. Order.push_back(*LoopI);
  255. }
  256. }
  257. }
  258. CurrentLoop = LI->getLoopFor(BB);
  259. if (CurrentLoop) {
  260. LoopBlocks[CurrentLoop]--;
  261. }
  262. CurrentLoopDepth = LoopDepth;
  263. Order.push_back(*I);
  264. }
  265. // This pass originally used a post-order traversal and then operated on
  266. // the list in reverse. Now that we are using a reverse post-order traversal
  267. // rather than re-working the whole pass to operate on the list in order,
  268. // we just reverse the list and continue to operate on it in reverse.
  269. std::reverse(Order.begin(), Order.end());
  270. }
  271. /// \brief Determine the end of the loops
  272. void StructurizeCFG::analyzeLoops(RegionNode *N) {
  273. if (N->isSubRegion()) {
  274. // Test for exit as back edge
  275. BasicBlock *Exit = N->getNodeAs<Region>()->getExit();
  276. if (Visited.count(Exit))
  277. Loops[Exit] = N->getEntry();
  278. } else {
  279. // Test for sucessors as back edge
  280. BasicBlock *BB = N->getNodeAs<BasicBlock>();
  281. BranchInst *Term = cast<BranchInst>(BB->getTerminator());
  282. for (unsigned i = 0, e = Term->getNumSuccessors(); i != e; ++i) {
  283. BasicBlock *Succ = Term->getSuccessor(i);
  284. if (Visited.count(Succ)) {
  285. Loops[Succ] = BB;
  286. }
  287. }
  288. }
  289. }
  290. /// \brief Invert the given condition
  291. Value *StructurizeCFG::invert(Value *Condition) {
  292. // First: Check if it's a constant
  293. if (Condition == BoolTrue)
  294. return BoolFalse;
  295. if (Condition == BoolFalse)
  296. return BoolTrue;
  297. if (Condition == BoolUndef)
  298. return BoolUndef;
  299. // Second: If the condition is already inverted, return the original value
  300. if (match(Condition, m_Not(m_Value(Condition))))
  301. return Condition;
  302. if (Instruction *Inst = dyn_cast<Instruction>(Condition)) {
  303. // Third: Check all the users for an invert
  304. BasicBlock *Parent = Inst->getParent();
  305. for (User *U : Condition->users())
  306. if (Instruction *I = dyn_cast<Instruction>(U))
  307. if (I->getParent() == Parent && match(I, m_Not(m_Specific(Condition))))
  308. return I;
  309. // Last option: Create a new instruction
  310. return BinaryOperator::CreateNot(Condition, "", Parent->getTerminator());
  311. }
  312. if (Argument *Arg = dyn_cast<Argument>(Condition)) {
  313. BasicBlock &EntryBlock = Arg->getParent()->getEntryBlock();
  314. return BinaryOperator::CreateNot(Condition,
  315. Arg->getName() + ".inv",
  316. EntryBlock.getTerminator());
  317. }
  318. llvm_unreachable("Unhandled condition to invert");
  319. }
  320. /// \brief Build the condition for one edge
  321. Value *StructurizeCFG::buildCondition(BranchInst *Term, unsigned Idx,
  322. bool Invert) {
  323. Value *Cond = Invert ? BoolFalse : BoolTrue;
  324. if (Term->isConditional()) {
  325. Cond = Term->getCondition();
  326. if (Idx != (unsigned)Invert)
  327. Cond = invert(Cond);
  328. }
  329. return Cond;
  330. }
  331. /// \brief Analyze the predecessors of each block and build up predicates
  332. void StructurizeCFG::gatherPredicates(RegionNode *N) {
  333. RegionInfo *RI = ParentRegion->getRegionInfo();
  334. BasicBlock *BB = N->getEntry();
  335. BBPredicates &Pred = Predicates[BB];
  336. BBPredicates &LPred = LoopPreds[BB];
  337. for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB);
  338. PI != PE; ++PI) {
  339. // Ignore it if it's a branch from outside into our region entry
  340. if (!ParentRegion->contains(*PI))
  341. continue;
  342. Region *R = RI->getRegionFor(*PI);
  343. if (R == ParentRegion) {
  344. // It's a top level block in our region
  345. BranchInst *Term = cast<BranchInst>((*PI)->getTerminator());
  346. for (unsigned i = 0, e = Term->getNumSuccessors(); i != e; ++i) {
  347. BasicBlock *Succ = Term->getSuccessor(i);
  348. if (Succ != BB)
  349. continue;
  350. if (Visited.count(*PI)) {
  351. // Normal forward edge
  352. if (Term->isConditional()) {
  353. // Try to treat it like an ELSE block
  354. BasicBlock *Other = Term->getSuccessor(!i);
  355. if (Visited.count(Other) && !Loops.count(Other) &&
  356. !Pred.count(Other) && !Pred.count(*PI)) {
  357. Pred[Other] = BoolFalse;
  358. Pred[*PI] = BoolTrue;
  359. continue;
  360. }
  361. }
  362. Pred[*PI] = buildCondition(Term, i, false);
  363. } else {
  364. // Back edge
  365. LPred[*PI] = buildCondition(Term, i, true);
  366. }
  367. }
  368. } else {
  369. // It's an exit from a sub region
  370. while (R->getParent() != ParentRegion)
  371. R = R->getParent();
  372. // Edge from inside a subregion to its entry, ignore it
  373. if (*R == *N)
  374. continue;
  375. BasicBlock *Entry = R->getEntry();
  376. if (Visited.count(Entry))
  377. Pred[Entry] = BoolTrue;
  378. else
  379. LPred[Entry] = BoolFalse;
  380. }
  381. }
  382. }
  383. /// \brief Collect various loop and predicate infos
  384. void StructurizeCFG::collectInfos() {
  385. // Reset predicate
  386. Predicates.clear();
  387. // and loop infos
  388. Loops.clear();
  389. LoopPreds.clear();
  390. // Reset the visited nodes
  391. Visited.clear();
  392. for (RNVector::reverse_iterator OI = Order.rbegin(), OE = Order.rend();
  393. OI != OE; ++OI) {
  394. DEBUG(dbgs() << "Visiting: " <<
  395. ((*OI)->isSubRegion() ? "SubRegion with entry: " : "") <<
  396. (*OI)->getEntry()->getName() << " Loop Depth: " << LI->getLoopDepth((*OI)->getEntry()) << "\n");
  397. // Analyze all the conditions leading to a node
  398. gatherPredicates(*OI);
  399. // Remember that we've seen this node
  400. Visited.insert((*OI)->getEntry());
  401. // Find the last back edges
  402. analyzeLoops(*OI);
  403. }
  404. }
  405. /// \brief Insert the missing branch conditions
  406. void StructurizeCFG::insertConditions(bool Loops) {
  407. BranchVector &Conds = Loops ? LoopConds : Conditions;
  408. Value *Default = Loops ? BoolTrue : BoolFalse;
  409. SSAUpdater PhiInserter;
  410. for (BranchInst *Term : Conds) {
  411. assert(Term->isConditional());
  412. BasicBlock *Parent = Term->getParent();
  413. BasicBlock *SuccTrue = Term->getSuccessor(0);
  414. BasicBlock *SuccFalse = Term->getSuccessor(1);
  415. PhiInserter.Initialize(Boolean, "");
  416. PhiInserter.AddAvailableValue(&Func->getEntryBlock(), Default);
  417. PhiInserter.AddAvailableValue(Loops ? SuccFalse : Parent, Default);
  418. BBPredicates &Preds = Loops ? LoopPreds[SuccFalse] : Predicates[SuccTrue];
  419. NearestCommonDominator Dominator(DT);
  420. Dominator.addBlock(Parent, false);
  421. Value *ParentValue = nullptr;
  422. for (BBPredicates::iterator PI = Preds.begin(), PE = Preds.end();
  423. PI != PE; ++PI) {
  424. if (PI->first == Parent) {
  425. ParentValue = PI->second;
  426. break;
  427. }
  428. PhiInserter.AddAvailableValue(PI->first, PI->second);
  429. Dominator.addBlock(PI->first);
  430. }
  431. if (ParentValue) {
  432. Term->setCondition(ParentValue);
  433. } else {
  434. if (!Dominator.wasResultExplicitMentioned())
  435. PhiInserter.AddAvailableValue(Dominator.getResult(), Default);
  436. Term->setCondition(PhiInserter.GetValueInMiddleOfBlock(Parent));
  437. }
  438. }
  439. }
  440. /// \brief Remove all PHI values coming from "From" into "To" and remember
  441. /// them in DeletedPhis
  442. void StructurizeCFG::delPhiValues(BasicBlock *From, BasicBlock *To) {
  443. PhiMap &Map = DeletedPhis[To];
  444. for (BasicBlock::iterator I = To->begin(), E = To->end();
  445. I != E && isa<PHINode>(*I);) {
  446. PHINode &Phi = cast<PHINode>(*I++);
  447. while (Phi.getBasicBlockIndex(From) != -1) {
  448. Value *Deleted = Phi.removeIncomingValue(From, false);
  449. Map[&Phi].push_back(std::make_pair(From, Deleted));
  450. }
  451. }
  452. }
  453. /// \brief Add a dummy PHI value as soon as we knew the new predecessor
  454. void StructurizeCFG::addPhiValues(BasicBlock *From, BasicBlock *To) {
  455. for (BasicBlock::iterator I = To->begin(), E = To->end();
  456. I != E && isa<PHINode>(*I);) {
  457. PHINode &Phi = cast<PHINode>(*I++);
  458. Value *Undef = UndefValue::get(Phi.getType());
  459. Phi.addIncoming(Undef, From);
  460. }
  461. AddedPhis[To].push_back(From);
  462. }
  463. /// \brief Add the real PHI value as soon as everything is set up
  464. void StructurizeCFG::setPhiValues() {
  465. SSAUpdater Updater;
  466. for (BB2BBVecMap::iterator AI = AddedPhis.begin(), AE = AddedPhis.end();
  467. AI != AE; ++AI) {
  468. BasicBlock *To = AI->first;
  469. BBVector &From = AI->second;
  470. if (!DeletedPhis.count(To))
  471. continue;
  472. PhiMap &Map = DeletedPhis[To];
  473. for (PhiMap::iterator PI = Map.begin(), PE = Map.end();
  474. PI != PE; ++PI) {
  475. PHINode *Phi = PI->first;
  476. Value *Undef = UndefValue::get(Phi->getType());
  477. Updater.Initialize(Phi->getType(), "");
  478. Updater.AddAvailableValue(&Func->getEntryBlock(), Undef);
  479. Updater.AddAvailableValue(To, Undef);
  480. NearestCommonDominator Dominator(DT);
  481. Dominator.addBlock(To, false);
  482. for (BBValueVector::iterator VI = PI->second.begin(),
  483. VE = PI->second.end(); VI != VE; ++VI) {
  484. Updater.AddAvailableValue(VI->first, VI->second);
  485. Dominator.addBlock(VI->first);
  486. }
  487. if (!Dominator.wasResultExplicitMentioned())
  488. Updater.AddAvailableValue(Dominator.getResult(), Undef);
  489. for (BBVector::iterator FI = From.begin(), FE = From.end();
  490. FI != FE; ++FI) {
  491. int Idx = Phi->getBasicBlockIndex(*FI);
  492. assert(Idx != -1);
  493. Phi->setIncomingValue(Idx, Updater.GetValueAtEndOfBlock(*FI));
  494. }
  495. }
  496. DeletedPhis.erase(To);
  497. }
  498. assert(DeletedPhis.empty());
  499. }
  500. /// \brief Remove phi values from all successors and then remove the terminator.
  501. void StructurizeCFG::killTerminator(BasicBlock *BB) {
  502. TerminatorInst *Term = BB->getTerminator();
  503. if (!Term)
  504. return;
  505. for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB);
  506. SI != SE; ++SI) {
  507. delPhiValues(BB, *SI);
  508. }
  509. Term->eraseFromParent();
  510. }
  511. /// \brief Let node exit(s) point to NewExit
  512. void StructurizeCFG::changeExit(RegionNode *Node, BasicBlock *NewExit,
  513. bool IncludeDominator) {
  514. if (Node->isSubRegion()) {
  515. Region *SubRegion = Node->getNodeAs<Region>();
  516. BasicBlock *OldExit = SubRegion->getExit();
  517. BasicBlock *Dominator = nullptr;
  518. // Find all the edges from the sub region to the exit
  519. for (pred_iterator I = pred_begin(OldExit), E = pred_end(OldExit);
  520. I != E;) {
  521. BasicBlock *BB = *I++;
  522. if (!SubRegion->contains(BB))
  523. continue;
  524. // Modify the edges to point to the new exit
  525. delPhiValues(BB, OldExit);
  526. BB->getTerminator()->replaceUsesOfWith(OldExit, NewExit);
  527. addPhiValues(BB, NewExit);
  528. // Find the new dominator (if requested)
  529. if (IncludeDominator) {
  530. if (!Dominator)
  531. Dominator = BB;
  532. else
  533. Dominator = DT->findNearestCommonDominator(Dominator, BB);
  534. }
  535. }
  536. // Change the dominator (if requested)
  537. if (Dominator)
  538. DT->changeImmediateDominator(NewExit, Dominator);
  539. // Update the region info
  540. SubRegion->replaceExit(NewExit);
  541. } else {
  542. BasicBlock *BB = Node->getNodeAs<BasicBlock>();
  543. killTerminator(BB);
  544. BranchInst::Create(NewExit, BB);
  545. addPhiValues(BB, NewExit);
  546. if (IncludeDominator)
  547. DT->changeImmediateDominator(NewExit, BB);
  548. }
  549. }
  550. /// \brief Create a new flow node and update dominator tree and region info
  551. BasicBlock *StructurizeCFG::getNextFlow(BasicBlock *Dominator) {
  552. LLVMContext &Context = Func->getContext();
  553. BasicBlock *Insert = Order.empty() ? ParentRegion->getExit() :
  554. Order.back()->getEntry();
  555. BasicBlock *Flow = BasicBlock::Create(Context, FlowBlockName,
  556. Func, Insert);
  557. DT->addNewBlock(Flow, Dominator);
  558. ParentRegion->getRegionInfo()->setRegionFor(Flow, ParentRegion);
  559. return Flow;
  560. }
  561. /// \brief Create a new or reuse the previous node as flow node
  562. BasicBlock *StructurizeCFG::needPrefix(bool NeedEmpty) {
  563. BasicBlock *Entry = PrevNode->getEntry();
  564. if (!PrevNode->isSubRegion()) {
  565. killTerminator(Entry);
  566. if (!NeedEmpty || Entry->getFirstInsertionPt() == Entry->end())
  567. return Entry;
  568. }
  569. // create a new flow node
  570. BasicBlock *Flow = getNextFlow(Entry);
  571. // and wire it up
  572. changeExit(PrevNode, Flow, true);
  573. PrevNode = ParentRegion->getBBNode(Flow);
  574. return Flow;
  575. }
  576. /// \brief Returns the region exit if possible, otherwise just a new flow node
  577. BasicBlock *StructurizeCFG::needPostfix(BasicBlock *Flow,
  578. bool ExitUseAllowed) {
  579. if (Order.empty() && ExitUseAllowed) {
  580. BasicBlock *Exit = ParentRegion->getExit();
  581. DT->changeImmediateDominator(Exit, Flow);
  582. addPhiValues(Flow, Exit);
  583. return Exit;
  584. }
  585. return getNextFlow(Flow);
  586. }
  587. /// \brief Set the previous node
  588. void StructurizeCFG::setPrevNode(BasicBlock *BB) {
  589. PrevNode = ParentRegion->contains(BB) ? ParentRegion->getBBNode(BB)
  590. : nullptr;
  591. }
  592. /// \brief Does BB dominate all the predicates of Node ?
  593. bool StructurizeCFG::dominatesPredicates(BasicBlock *BB, RegionNode *Node) {
  594. BBPredicates &Preds = Predicates[Node->getEntry()];
  595. for (BBPredicates::iterator PI = Preds.begin(), PE = Preds.end();
  596. PI != PE; ++PI) {
  597. if (!DT->dominates(BB, PI->first))
  598. return false;
  599. }
  600. return true;
  601. }
  602. /// \brief Can we predict that this node will always be called?
  603. bool StructurizeCFG::isPredictableTrue(RegionNode *Node) {
  604. BBPredicates &Preds = Predicates[Node->getEntry()];
  605. bool Dominated = false;
  606. // Regionentry is always true
  607. if (!PrevNode)
  608. return true;
  609. for (BBPredicates::iterator I = Preds.begin(), E = Preds.end();
  610. I != E; ++I) {
  611. if (I->second != BoolTrue)
  612. return false;
  613. if (!Dominated && DT->dominates(I->first, PrevNode->getEntry()))
  614. Dominated = true;
  615. }
  616. // TODO: The dominator check is too strict
  617. return Dominated;
  618. }
  619. /// Take one node from the order vector and wire it up
  620. void StructurizeCFG::wireFlow(bool ExitUseAllowed,
  621. BasicBlock *LoopEnd) {
  622. RegionNode *Node = Order.pop_back_val();
  623. Visited.insert(Node->getEntry());
  624. if (isPredictableTrue(Node)) {
  625. // Just a linear flow
  626. if (PrevNode) {
  627. changeExit(PrevNode, Node->getEntry(), true);
  628. }
  629. PrevNode = Node;
  630. } else {
  631. // Insert extra prefix node (or reuse last one)
  632. BasicBlock *Flow = needPrefix(false);
  633. // Insert extra postfix node (or use exit instead)
  634. BasicBlock *Entry = Node->getEntry();
  635. BasicBlock *Next = needPostfix(Flow, ExitUseAllowed);
  636. // let it point to entry and next block
  637. Conditions.push_back(BranchInst::Create(Entry, Next, BoolUndef, Flow));
  638. addPhiValues(Flow, Entry);
  639. DT->changeImmediateDominator(Entry, Flow);
  640. PrevNode = Node;
  641. while (!Order.empty() && !Visited.count(LoopEnd) &&
  642. dominatesPredicates(Entry, Order.back())) {
  643. handleLoops(false, LoopEnd);
  644. }
  645. changeExit(PrevNode, Next, false);
  646. setPrevNode(Next);
  647. }
  648. }
  649. void StructurizeCFG::handleLoops(bool ExitUseAllowed,
  650. BasicBlock *LoopEnd) {
  651. RegionNode *Node = Order.back();
  652. BasicBlock *LoopStart = Node->getEntry();
  653. if (!Loops.count(LoopStart)) {
  654. wireFlow(ExitUseAllowed, LoopEnd);
  655. return;
  656. }
  657. if (!isPredictableTrue(Node))
  658. LoopStart = needPrefix(true);
  659. LoopEnd = Loops[Node->getEntry()];
  660. wireFlow(false, LoopEnd);
  661. while (!Visited.count(LoopEnd)) {
  662. handleLoops(false, LoopEnd);
  663. }
  664. // If the start of the loop is the entry block, we can't branch to it so
  665. // insert a new dummy entry block.
  666. Function *LoopFunc = LoopStart->getParent();
  667. if (LoopStart == &LoopFunc->getEntryBlock()) {
  668. LoopStart->setName("entry.orig");
  669. BasicBlock *NewEntry =
  670. BasicBlock::Create(LoopStart->getContext(),
  671. "entry",
  672. LoopFunc,
  673. LoopStart);
  674. BranchInst::Create(LoopStart, NewEntry);
  675. }
  676. // Create an extra loop end node
  677. LoopEnd = needPrefix(false);
  678. BasicBlock *Next = needPostfix(LoopEnd, ExitUseAllowed);
  679. LoopConds.push_back(BranchInst::Create(Next, LoopStart,
  680. BoolUndef, LoopEnd));
  681. addPhiValues(LoopEnd, LoopStart);
  682. setPrevNode(Next);
  683. }
  684. /// After this function control flow looks like it should be, but
  685. /// branches and PHI nodes only have undefined conditions.
  686. void StructurizeCFG::createFlow() {
  687. BasicBlock *Exit = ParentRegion->getExit();
  688. bool EntryDominatesExit = DT->dominates(ParentRegion->getEntry(), Exit);
  689. DeletedPhis.clear();
  690. AddedPhis.clear();
  691. Conditions.clear();
  692. LoopConds.clear();
  693. PrevNode = nullptr;
  694. Visited.clear();
  695. while (!Order.empty()) {
  696. handleLoops(EntryDominatesExit, nullptr);
  697. }
  698. if (PrevNode)
  699. changeExit(PrevNode, Exit, EntryDominatesExit);
  700. else
  701. assert(EntryDominatesExit);
  702. }
  703. /// Handle a rare case where the disintegrated nodes instructions
  704. /// no longer dominate all their uses. Not sure if this is really nessasary
  705. void StructurizeCFG::rebuildSSA() {
  706. SSAUpdater Updater;
  707. for (auto *BB : ParentRegion->blocks())
  708. for (BasicBlock::iterator II = BB->begin(), IE = BB->end();
  709. II != IE; ++II) {
  710. bool Initialized = false;
  711. for (auto I = II->use_begin(), E = II->use_end(); I != E;) {
  712. Use &U = *I++;
  713. Instruction *User = cast<Instruction>(U.getUser());
  714. if (User->getParent() == BB) {
  715. continue;
  716. } else if (PHINode *UserPN = dyn_cast<PHINode>(User)) {
  717. if (UserPN->getIncomingBlock(U) == BB)
  718. continue;
  719. }
  720. if (DT->dominates(II, User))
  721. continue;
  722. if (!Initialized) {
  723. Value *Undef = UndefValue::get(II->getType());
  724. Updater.Initialize(II->getType(), "");
  725. Updater.AddAvailableValue(&Func->getEntryBlock(), Undef);
  726. Updater.AddAvailableValue(BB, II);
  727. Initialized = true;
  728. }
  729. Updater.RewriteUseAfterInsertions(U);
  730. }
  731. }
  732. }
  733. /// \brief Run the transformation for each region found
  734. bool StructurizeCFG::runOnRegion(Region *R, RGPassManager &RGM) {
  735. if (R->isTopLevelRegion())
  736. return false;
  737. Func = R->getEntry()->getParent();
  738. ParentRegion = R;
  739. DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
  740. LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
  741. orderNodes();
  742. collectInfos();
  743. createFlow();
  744. insertConditions(false);
  745. insertConditions(true);
  746. setPhiValues();
  747. rebuildSSA();
  748. // Cleanup
  749. Order.clear();
  750. Visited.clear();
  751. DeletedPhis.clear();
  752. AddedPhis.clear();
  753. Predicates.clear();
  754. Conditions.clear();
  755. Loops.clear();
  756. LoopPreds.clear();
  757. LoopConds.clear();
  758. return true;
  759. }
  760. /// \brief Create the pass
  761. Pass *llvm::createStructurizeCFGPass() {
  762. return new StructurizeCFG();
  763. }