IfConversion.cpp 86 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289
  1. //===-- IfConversion.cpp - Machine code if conversion pass. ---------------===//
  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. //
  10. // This file implements the machine instruction level if-conversion pass, which
  11. // tries to convert conditional branches into predicated instructions.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #include "llvm/CodeGen/Passes.h"
  15. #include "BranchFolding.h"
  16. #include "llvm/ADT/STLExtras.h"
  17. #include "llvm/ADT/ScopeExit.h"
  18. #include "llvm/ADT/SmallSet.h"
  19. #include "llvm/ADT/Statistic.h"
  20. #include "llvm/CodeGen/LivePhysRegs.h"
  21. #include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
  22. #include "llvm/CodeGen/MachineBranchProbabilityInfo.h"
  23. #include "llvm/CodeGen/MachineFunctionPass.h"
  24. #include "llvm/CodeGen/MachineInstrBuilder.h"
  25. #include "llvm/CodeGen/MachineModuleInfo.h"
  26. #include "llvm/CodeGen/MachineRegisterInfo.h"
  27. #include "llvm/CodeGen/TargetSchedule.h"
  28. #include "llvm/Support/CommandLine.h"
  29. #include "llvm/Support/Debug.h"
  30. #include "llvm/Support/ErrorHandling.h"
  31. #include "llvm/Support/raw_ostream.h"
  32. #include "llvm/Target/TargetInstrInfo.h"
  33. #include "llvm/Target/TargetLowering.h"
  34. #include "llvm/Target/TargetRegisterInfo.h"
  35. #include "llvm/Target/TargetSubtargetInfo.h"
  36. #include <algorithm>
  37. #include <utility>
  38. using namespace llvm;
  39. #define DEBUG_TYPE "ifcvt"
  40. // Hidden options for help debugging.
  41. static cl::opt<int> IfCvtFnStart("ifcvt-fn-start", cl::init(-1), cl::Hidden);
  42. static cl::opt<int> IfCvtFnStop("ifcvt-fn-stop", cl::init(-1), cl::Hidden);
  43. static cl::opt<int> IfCvtLimit("ifcvt-limit", cl::init(-1), cl::Hidden);
  44. static cl::opt<bool> DisableSimple("disable-ifcvt-simple",
  45. cl::init(false), cl::Hidden);
  46. static cl::opt<bool> DisableSimpleF("disable-ifcvt-simple-false",
  47. cl::init(false), cl::Hidden);
  48. static cl::opt<bool> DisableTriangle("disable-ifcvt-triangle",
  49. cl::init(false), cl::Hidden);
  50. static cl::opt<bool> DisableTriangleR("disable-ifcvt-triangle-rev",
  51. cl::init(false), cl::Hidden);
  52. static cl::opt<bool> DisableTriangleF("disable-ifcvt-triangle-false",
  53. cl::init(false), cl::Hidden);
  54. static cl::opt<bool> DisableTriangleFR("disable-ifcvt-triangle-false-rev",
  55. cl::init(false), cl::Hidden);
  56. static cl::opt<bool> DisableDiamond("disable-ifcvt-diamond",
  57. cl::init(false), cl::Hidden);
  58. static cl::opt<bool> DisableForkedDiamond("disable-ifcvt-forked-diamond",
  59. cl::init(false), cl::Hidden);
  60. static cl::opt<bool> IfCvtBranchFold("ifcvt-branch-fold",
  61. cl::init(true), cl::Hidden);
  62. STATISTIC(NumSimple, "Number of simple if-conversions performed");
  63. STATISTIC(NumSimpleFalse, "Number of simple (F) if-conversions performed");
  64. STATISTIC(NumTriangle, "Number of triangle if-conversions performed");
  65. STATISTIC(NumTriangleRev, "Number of triangle (R) if-conversions performed");
  66. STATISTIC(NumTriangleFalse,"Number of triangle (F) if-conversions performed");
  67. STATISTIC(NumTriangleFRev, "Number of triangle (F/R) if-conversions performed");
  68. STATISTIC(NumDiamonds, "Number of diamond if-conversions performed");
  69. STATISTIC(NumForkedDiamonds, "Number of forked-diamond if-conversions performed");
  70. STATISTIC(NumIfConvBBs, "Number of if-converted blocks");
  71. STATISTIC(NumDupBBs, "Number of duplicated blocks");
  72. STATISTIC(NumUnpred, "Number of true blocks of diamonds unpredicated");
  73. namespace {
  74. class IfConverter : public MachineFunctionPass {
  75. enum IfcvtKind {
  76. ICNotClassfied, // BB data valid, but not classified.
  77. ICSimpleFalse, // Same as ICSimple, but on the false path.
  78. ICSimple, // BB is entry of an one split, no rejoin sub-CFG.
  79. ICTriangleFRev, // Same as ICTriangleFalse, but false path rev condition.
  80. ICTriangleRev, // Same as ICTriangle, but true path rev condition.
  81. ICTriangleFalse, // Same as ICTriangle, but on the false path.
  82. ICTriangle, // BB is entry of a triangle sub-CFG.
  83. ICDiamond, // BB is entry of a diamond sub-CFG.
  84. ICForkedDiamond // BB is entry of an almost diamond sub-CFG, with a
  85. // common tail that can be shared.
  86. };
  87. /// One per MachineBasicBlock, this is used to cache the result
  88. /// if-conversion feasibility analysis. This includes results from
  89. /// TargetInstrInfo::analyzeBranch() (i.e. TBB, FBB, and Cond), and its
  90. /// classification, and common tail block of its successors (if it's a
  91. /// diamond shape), its size, whether it's predicable, and whether any
  92. /// instruction can clobber the 'would-be' predicate.
  93. ///
  94. /// IsDone - True if BB is not to be considered for ifcvt.
  95. /// IsBeingAnalyzed - True if BB is currently being analyzed.
  96. /// IsAnalyzed - True if BB has been analyzed (info is still valid).
  97. /// IsEnqueued - True if BB has been enqueued to be ifcvt'ed.
  98. /// IsBrAnalyzable - True if analyzeBranch() returns false.
  99. /// HasFallThrough - True if BB may fallthrough to the following BB.
  100. /// IsUnpredicable - True if BB is known to be unpredicable.
  101. /// ClobbersPred - True if BB could modify predicates (e.g. has
  102. /// cmp, call, etc.)
  103. /// NonPredSize - Number of non-predicated instructions.
  104. /// ExtraCost - Extra cost for multi-cycle instructions.
  105. /// ExtraCost2 - Some instructions are slower when predicated
  106. /// BB - Corresponding MachineBasicBlock.
  107. /// TrueBB / FalseBB- See analyzeBranch().
  108. /// BrCond - Conditions for end of block conditional branches.
  109. /// Predicate - Predicate used in the BB.
  110. struct BBInfo {
  111. bool IsDone : 1;
  112. bool IsBeingAnalyzed : 1;
  113. bool IsAnalyzed : 1;
  114. bool IsEnqueued : 1;
  115. bool IsBrAnalyzable : 1;
  116. bool IsBrReversible : 1;
  117. bool HasFallThrough : 1;
  118. bool IsUnpredicable : 1;
  119. bool CannotBeCopied : 1;
  120. bool ClobbersPred : 1;
  121. unsigned NonPredSize;
  122. unsigned ExtraCost;
  123. unsigned ExtraCost2;
  124. MachineBasicBlock *BB;
  125. MachineBasicBlock *TrueBB;
  126. MachineBasicBlock *FalseBB;
  127. SmallVector<MachineOperand, 4> BrCond;
  128. SmallVector<MachineOperand, 4> Predicate;
  129. BBInfo() : IsDone(false), IsBeingAnalyzed(false),
  130. IsAnalyzed(false), IsEnqueued(false), IsBrAnalyzable(false),
  131. IsBrReversible(false), HasFallThrough(false),
  132. IsUnpredicable(false), CannotBeCopied(false),
  133. ClobbersPred(false), NonPredSize(0), ExtraCost(0),
  134. ExtraCost2(0), BB(nullptr), TrueBB(nullptr),
  135. FalseBB(nullptr) {}
  136. };
  137. /// Record information about pending if-conversions to attempt:
  138. /// BBI - Corresponding BBInfo.
  139. /// Kind - Type of block. See IfcvtKind.
  140. /// NeedSubsumption - True if the to-be-predicated BB has already been
  141. /// predicated.
  142. /// NumDups - Number of instructions that would be duplicated due
  143. /// to this if-conversion. (For diamonds, the number of
  144. /// identical instructions at the beginnings of both
  145. /// paths).
  146. /// NumDups2 - For diamonds, the number of identical instructions
  147. /// at the ends of both paths.
  148. struct IfcvtToken {
  149. BBInfo &BBI;
  150. IfcvtKind Kind;
  151. unsigned NumDups;
  152. unsigned NumDups2;
  153. bool NeedSubsumption : 1;
  154. bool TClobbersPred : 1;
  155. bool FClobbersPred : 1;
  156. IfcvtToken(BBInfo &b, IfcvtKind k, bool s, unsigned d, unsigned d2 = 0,
  157. bool tc = false, bool fc = false)
  158. : BBI(b), Kind(k), NumDups(d), NumDups2(d2), NeedSubsumption(s),
  159. TClobbersPred(tc), FClobbersPred(fc) {}
  160. };
  161. /// Results of if-conversion feasibility analysis indexed by basic block
  162. /// number.
  163. std::vector<BBInfo> BBAnalysis;
  164. TargetSchedModel SchedModel;
  165. const TargetLoweringBase *TLI;
  166. const TargetInstrInfo *TII;
  167. const TargetRegisterInfo *TRI;
  168. const MachineBranchProbabilityInfo *MBPI;
  169. MachineRegisterInfo *MRI;
  170. LivePhysRegs Redefs;
  171. LivePhysRegs DontKill;
  172. bool PreRegAlloc;
  173. bool MadeChange;
  174. int FnNum;
  175. std::function<bool(const Function &)> PredicateFtor;
  176. public:
  177. static char ID;
  178. IfConverter(std::function<bool(const Function &)> Ftor = nullptr)
  179. : MachineFunctionPass(ID), FnNum(-1), PredicateFtor(std::move(Ftor)) {
  180. initializeIfConverterPass(*PassRegistry::getPassRegistry());
  181. }
  182. void getAnalysisUsage(AnalysisUsage &AU) const override {
  183. AU.addRequired<MachineBlockFrequencyInfo>();
  184. AU.addRequired<MachineBranchProbabilityInfo>();
  185. MachineFunctionPass::getAnalysisUsage(AU);
  186. }
  187. bool runOnMachineFunction(MachineFunction &MF) override;
  188. MachineFunctionProperties getRequiredProperties() const override {
  189. return MachineFunctionProperties().set(
  190. MachineFunctionProperties::Property::NoVRegs);
  191. }
  192. private:
  193. bool ReverseBranchCondition(BBInfo &BBI) const;
  194. bool ValidSimple(BBInfo &TrueBBI, unsigned &Dups,
  195. BranchProbability Prediction) const;
  196. bool ValidTriangle(BBInfo &TrueBBI, BBInfo &FalseBBI,
  197. bool FalseBranch, unsigned &Dups,
  198. BranchProbability Prediction) const;
  199. bool CountDuplicatedInstructions(
  200. MachineBasicBlock::iterator &TIB, MachineBasicBlock::iterator &FIB,
  201. MachineBasicBlock::iterator &TIE, MachineBasicBlock::iterator &FIE,
  202. unsigned &Dups1, unsigned &Dups2,
  203. MachineBasicBlock &TBB, MachineBasicBlock &FBB,
  204. bool SkipUnconditionalBranches) const;
  205. bool ValidDiamond(BBInfo &TrueBBI, BBInfo &FalseBBI,
  206. unsigned &Dups1, unsigned &Dups2,
  207. BBInfo &TrueBBICalc, BBInfo &FalseBBICalc) const;
  208. bool ValidForkedDiamond(BBInfo &TrueBBI, BBInfo &FalseBBI,
  209. unsigned &Dups1, unsigned &Dups2,
  210. BBInfo &TrueBBICalc, BBInfo &FalseBBICalc) const;
  211. void AnalyzeBranches(BBInfo &BBI);
  212. void ScanInstructions(BBInfo &BBI,
  213. MachineBasicBlock::iterator &Begin,
  214. MachineBasicBlock::iterator &End,
  215. bool BranchUnpredicable = false) const;
  216. bool RescanInstructions(
  217. MachineBasicBlock::iterator &TIB, MachineBasicBlock::iterator &FIB,
  218. MachineBasicBlock::iterator &TIE, MachineBasicBlock::iterator &FIE,
  219. BBInfo &TrueBBI, BBInfo &FalseBBI) const;
  220. void AnalyzeBlock(MachineBasicBlock &MBB,
  221. std::vector<std::unique_ptr<IfcvtToken>> &Tokens);
  222. bool FeasibilityAnalysis(BBInfo &BBI, SmallVectorImpl<MachineOperand> &Cond,
  223. bool isTriangle = false, bool RevBranch = false,
  224. bool hasCommonTail = false);
  225. void AnalyzeBlocks(MachineFunction &MF,
  226. std::vector<std::unique_ptr<IfcvtToken>> &Tokens);
  227. void InvalidatePreds(MachineBasicBlock &MBB);
  228. void RemoveExtraEdges(BBInfo &BBI);
  229. bool IfConvertSimple(BBInfo &BBI, IfcvtKind Kind);
  230. bool IfConvertTriangle(BBInfo &BBI, IfcvtKind Kind);
  231. bool IfConvertDiamondCommon(BBInfo &BBI, BBInfo &TrueBBI, BBInfo &FalseBBI,
  232. unsigned NumDups1, unsigned NumDups2,
  233. bool TClobbersPred, bool FClobbersPred,
  234. bool RemoveBranch, bool MergeAddEdges);
  235. bool IfConvertDiamond(BBInfo &BBI, IfcvtKind Kind,
  236. unsigned NumDups1, unsigned NumDups2,
  237. bool TClobbers, bool FClobbers);
  238. bool IfConvertForkedDiamond(BBInfo &BBI, IfcvtKind Kind,
  239. unsigned NumDups1, unsigned NumDups2,
  240. bool TClobbers, bool FClobbers);
  241. void PredicateBlock(BBInfo &BBI,
  242. MachineBasicBlock::iterator E,
  243. SmallVectorImpl<MachineOperand> &Cond,
  244. SmallSet<unsigned, 4> *LaterRedefs = nullptr);
  245. void CopyAndPredicateBlock(BBInfo &ToBBI, BBInfo &FromBBI,
  246. SmallVectorImpl<MachineOperand> &Cond,
  247. bool IgnoreBr = false);
  248. void MergeBlocks(BBInfo &ToBBI, BBInfo &FromBBI, bool AddEdges = true);
  249. bool MeetIfcvtSizeLimit(MachineBasicBlock &BB,
  250. unsigned Cycle, unsigned Extra,
  251. BranchProbability Prediction) const {
  252. return Cycle > 0 && TII->isProfitableToIfCvt(BB, Cycle, Extra,
  253. Prediction);
  254. }
  255. bool MeetIfcvtSizeLimit(MachineBasicBlock &TBB,
  256. unsigned TCycle, unsigned TExtra,
  257. MachineBasicBlock &FBB,
  258. unsigned FCycle, unsigned FExtra,
  259. BranchProbability Prediction) const {
  260. return TCycle > 0 && FCycle > 0 &&
  261. TII->isProfitableToIfCvt(TBB, TCycle, TExtra, FBB, FCycle, FExtra,
  262. Prediction);
  263. }
  264. /// Returns true if Block ends without a terminator.
  265. bool blockAlwaysFallThrough(BBInfo &BBI) const {
  266. return BBI.IsBrAnalyzable && BBI.TrueBB == nullptr;
  267. }
  268. /// Used to sort if-conversion candidates.
  269. static bool IfcvtTokenCmp(const std::unique_ptr<IfcvtToken> &C1,
  270. const std::unique_ptr<IfcvtToken> &C2) {
  271. int Incr1 = (C1->Kind == ICDiamond)
  272. ? -(int)(C1->NumDups + C1->NumDups2) : (int)C1->NumDups;
  273. int Incr2 = (C2->Kind == ICDiamond)
  274. ? -(int)(C2->NumDups + C2->NumDups2) : (int)C2->NumDups;
  275. if (Incr1 > Incr2)
  276. return true;
  277. else if (Incr1 == Incr2) {
  278. // Favors subsumption.
  279. if (!C1->NeedSubsumption && C2->NeedSubsumption)
  280. return true;
  281. else if (C1->NeedSubsumption == C2->NeedSubsumption) {
  282. // Favors diamond over triangle, etc.
  283. if ((unsigned)C1->Kind < (unsigned)C2->Kind)
  284. return true;
  285. else if (C1->Kind == C2->Kind)
  286. return C1->BBI.BB->getNumber() < C2->BBI.BB->getNumber();
  287. }
  288. }
  289. return false;
  290. }
  291. };
  292. char IfConverter::ID = 0;
  293. }
  294. char &llvm::IfConverterID = IfConverter::ID;
  295. INITIALIZE_PASS_BEGIN(IfConverter, "if-converter", "If Converter", false, false)
  296. INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfo)
  297. INITIALIZE_PASS_END(IfConverter, "if-converter", "If Converter", false, false)
  298. bool IfConverter::runOnMachineFunction(MachineFunction &MF) {
  299. if (skipFunction(*MF.getFunction()) ||
  300. (PredicateFtor && !PredicateFtor(*MF.getFunction())))
  301. return false;
  302. const TargetSubtargetInfo &ST = MF.getSubtarget();
  303. TLI = ST.getTargetLowering();
  304. TII = ST.getInstrInfo();
  305. TRI = ST.getRegisterInfo();
  306. BranchFolder::MBFIWrapper MBFI(getAnalysis<MachineBlockFrequencyInfo>());
  307. MBPI = &getAnalysis<MachineBranchProbabilityInfo>();
  308. MRI = &MF.getRegInfo();
  309. SchedModel.init(ST.getSchedModel(), &ST, TII);
  310. if (!TII) return false;
  311. PreRegAlloc = MRI->isSSA();
  312. bool BFChange = false;
  313. if (!PreRegAlloc) {
  314. // Tail merge tend to expose more if-conversion opportunities.
  315. BranchFolder BF(true, false, MBFI, *MBPI);
  316. BFChange = BF.OptimizeFunction(MF, TII, ST.getRegisterInfo(),
  317. getAnalysisIfAvailable<MachineModuleInfo>());
  318. }
  319. DEBUG(dbgs() << "\nIfcvt: function (" << ++FnNum << ") \'"
  320. << MF.getName() << "\'");
  321. if (FnNum < IfCvtFnStart || (IfCvtFnStop != -1 && FnNum > IfCvtFnStop)) {
  322. DEBUG(dbgs() << " skipped\n");
  323. return false;
  324. }
  325. DEBUG(dbgs() << "\n");
  326. MF.RenumberBlocks();
  327. BBAnalysis.resize(MF.getNumBlockIDs());
  328. std::vector<std::unique_ptr<IfcvtToken>> Tokens;
  329. MadeChange = false;
  330. unsigned NumIfCvts = NumSimple + NumSimpleFalse + NumTriangle +
  331. NumTriangleRev + NumTriangleFalse + NumTriangleFRev + NumDiamonds;
  332. while (IfCvtLimit == -1 || (int)NumIfCvts < IfCvtLimit) {
  333. // Do an initial analysis for each basic block and find all the potential
  334. // candidates to perform if-conversion.
  335. bool Change = false;
  336. AnalyzeBlocks(MF, Tokens);
  337. while (!Tokens.empty()) {
  338. std::unique_ptr<IfcvtToken> Token = std::move(Tokens.back());
  339. Tokens.pop_back();
  340. BBInfo &BBI = Token->BBI;
  341. IfcvtKind Kind = Token->Kind;
  342. unsigned NumDups = Token->NumDups;
  343. unsigned NumDups2 = Token->NumDups2;
  344. // If the block has been evicted out of the queue or it has already been
  345. // marked dead (due to it being predicated), then skip it.
  346. if (BBI.IsDone)
  347. BBI.IsEnqueued = false;
  348. if (!BBI.IsEnqueued)
  349. continue;
  350. BBI.IsEnqueued = false;
  351. bool RetVal = false;
  352. switch (Kind) {
  353. default: llvm_unreachable("Unexpected!");
  354. case ICSimple:
  355. case ICSimpleFalse: {
  356. bool isFalse = Kind == ICSimpleFalse;
  357. if ((isFalse && DisableSimpleF) || (!isFalse && DisableSimple)) break;
  358. DEBUG(dbgs() << "Ifcvt (Simple" << (Kind == ICSimpleFalse ?
  359. " false" : "")
  360. << "): BB#" << BBI.BB->getNumber() << " ("
  361. << ((Kind == ICSimpleFalse)
  362. ? BBI.FalseBB->getNumber()
  363. : BBI.TrueBB->getNumber()) << ") ");
  364. RetVal = IfConvertSimple(BBI, Kind);
  365. DEBUG(dbgs() << (RetVal ? "succeeded!" : "failed!") << "\n");
  366. if (RetVal) {
  367. if (isFalse) ++NumSimpleFalse;
  368. else ++NumSimple;
  369. }
  370. break;
  371. }
  372. case ICTriangle:
  373. case ICTriangleRev:
  374. case ICTriangleFalse:
  375. case ICTriangleFRev: {
  376. bool isFalse = Kind == ICTriangleFalse;
  377. bool isRev = (Kind == ICTriangleRev || Kind == ICTriangleFRev);
  378. if (DisableTriangle && !isFalse && !isRev) break;
  379. if (DisableTriangleR && !isFalse && isRev) break;
  380. if (DisableTriangleF && isFalse && !isRev) break;
  381. if (DisableTriangleFR && isFalse && isRev) break;
  382. DEBUG(dbgs() << "Ifcvt (Triangle");
  383. if (isFalse)
  384. DEBUG(dbgs() << " false");
  385. if (isRev)
  386. DEBUG(dbgs() << " rev");
  387. DEBUG(dbgs() << "): BB#" << BBI.BB->getNumber() << " (T:"
  388. << BBI.TrueBB->getNumber() << ",F:"
  389. << BBI.FalseBB->getNumber() << ") ");
  390. RetVal = IfConvertTriangle(BBI, Kind);
  391. DEBUG(dbgs() << (RetVal ? "succeeded!" : "failed!") << "\n");
  392. if (RetVal) {
  393. if (isFalse) {
  394. if (isRev) ++NumTriangleFRev;
  395. else ++NumTriangleFalse;
  396. } else {
  397. if (isRev) ++NumTriangleRev;
  398. else ++NumTriangle;
  399. }
  400. }
  401. break;
  402. }
  403. case ICDiamond: {
  404. if (DisableDiamond) break;
  405. DEBUG(dbgs() << "Ifcvt (Diamond): BB#" << BBI.BB->getNumber() << " (T:"
  406. << BBI.TrueBB->getNumber() << ",F:"
  407. << BBI.FalseBB->getNumber() << ") ");
  408. RetVal = IfConvertDiamond(BBI, Kind, NumDups, NumDups2,
  409. Token->TClobbersPred,
  410. Token->FClobbersPred);
  411. DEBUG(dbgs() << (RetVal ? "succeeded!" : "failed!") << "\n");
  412. if (RetVal) ++NumDiamonds;
  413. break;
  414. }
  415. case ICForkedDiamond: {
  416. if (DisableForkedDiamond) break;
  417. DEBUG(dbgs() << "Ifcvt (Forked Diamond): BB#"
  418. << BBI.BB->getNumber() << " (T:"
  419. << BBI.TrueBB->getNumber() << ",F:"
  420. << BBI.FalseBB->getNumber() << ") ");
  421. RetVal = IfConvertForkedDiamond(BBI, Kind, NumDups, NumDups2,
  422. Token->TClobbersPred,
  423. Token->FClobbersPred);
  424. DEBUG(dbgs() << (RetVal ? "succeeded!" : "failed!") << "\n");
  425. if (RetVal) ++NumForkedDiamonds;
  426. break;
  427. }
  428. }
  429. Change |= RetVal;
  430. NumIfCvts = NumSimple + NumSimpleFalse + NumTriangle + NumTriangleRev +
  431. NumTriangleFalse + NumTriangleFRev + NumDiamonds;
  432. if (IfCvtLimit != -1 && (int)NumIfCvts >= IfCvtLimit)
  433. break;
  434. }
  435. if (!Change)
  436. break;
  437. MadeChange |= Change;
  438. }
  439. Tokens.clear();
  440. BBAnalysis.clear();
  441. if (MadeChange && IfCvtBranchFold) {
  442. BranchFolder BF(false, false, MBFI, *MBPI);
  443. BF.OptimizeFunction(MF, TII, MF.getSubtarget().getRegisterInfo(),
  444. getAnalysisIfAvailable<MachineModuleInfo>());
  445. }
  446. MadeChange |= BFChange;
  447. return MadeChange;
  448. }
  449. /// BB has a fallthrough. Find its 'false' successor given its 'true' successor.
  450. static MachineBasicBlock *findFalseBlock(MachineBasicBlock *BB,
  451. MachineBasicBlock *TrueBB) {
  452. for (MachineBasicBlock *SuccBB : BB->successors()) {
  453. if (SuccBB != TrueBB)
  454. return SuccBB;
  455. }
  456. return nullptr;
  457. }
  458. /// Reverse the condition of the end of the block branch. Swap block's 'true'
  459. /// and 'false' successors.
  460. bool IfConverter::ReverseBranchCondition(BBInfo &BBI) const {
  461. DebugLoc dl; // FIXME: this is nowhere
  462. if (!TII->ReverseBranchCondition(BBI.BrCond)) {
  463. TII->RemoveBranch(*BBI.BB);
  464. TII->insertBranch(*BBI.BB, BBI.FalseBB, BBI.TrueBB, BBI.BrCond, dl);
  465. std::swap(BBI.TrueBB, BBI.FalseBB);
  466. return true;
  467. }
  468. return false;
  469. }
  470. /// Returns the next block in the function blocks ordering. If it is the end,
  471. /// returns NULL.
  472. static inline MachineBasicBlock *getNextBlock(MachineBasicBlock &MBB) {
  473. MachineFunction::iterator I = MBB.getIterator();
  474. MachineFunction::iterator E = MBB.getParent()->end();
  475. if (++I == E)
  476. return nullptr;
  477. return &*I;
  478. }
  479. /// Returns true if the 'true' block (along with its predecessor) forms a valid
  480. /// simple shape for ifcvt. It also returns the number of instructions that the
  481. /// ifcvt would need to duplicate if performed in Dups.
  482. bool IfConverter::ValidSimple(BBInfo &TrueBBI, unsigned &Dups,
  483. BranchProbability Prediction) const {
  484. Dups = 0;
  485. if (TrueBBI.IsBeingAnalyzed || TrueBBI.IsDone)
  486. return false;
  487. if (TrueBBI.IsBrAnalyzable)
  488. return false;
  489. if (TrueBBI.BB->pred_size() > 1) {
  490. if (TrueBBI.CannotBeCopied ||
  491. !TII->isProfitableToDupForIfCvt(*TrueBBI.BB, TrueBBI.NonPredSize,
  492. Prediction))
  493. return false;
  494. Dups = TrueBBI.NonPredSize;
  495. }
  496. return true;
  497. }
  498. /// Returns true if the 'true' and 'false' blocks (along with their common
  499. /// predecessor) forms a valid triangle shape for ifcvt. If 'FalseBranch' is
  500. /// true, it checks if 'true' block's false branch branches to the 'false' block
  501. /// rather than the other way around. It also returns the number of instructions
  502. /// that the ifcvt would need to duplicate if performed in 'Dups'.
  503. bool IfConverter::ValidTriangle(BBInfo &TrueBBI, BBInfo &FalseBBI,
  504. bool FalseBranch, unsigned &Dups,
  505. BranchProbability Prediction) const {
  506. Dups = 0;
  507. if (TrueBBI.IsBeingAnalyzed || TrueBBI.IsDone)
  508. return false;
  509. if (TrueBBI.BB->pred_size() > 1) {
  510. if (TrueBBI.CannotBeCopied)
  511. return false;
  512. unsigned Size = TrueBBI.NonPredSize;
  513. if (TrueBBI.IsBrAnalyzable) {
  514. if (TrueBBI.TrueBB && TrueBBI.BrCond.empty())
  515. // Ends with an unconditional branch. It will be removed.
  516. --Size;
  517. else {
  518. MachineBasicBlock *FExit = FalseBranch
  519. ? TrueBBI.TrueBB : TrueBBI.FalseBB;
  520. if (FExit)
  521. // Require a conditional branch
  522. ++Size;
  523. }
  524. }
  525. if (!TII->isProfitableToDupForIfCvt(*TrueBBI.BB, Size, Prediction))
  526. return false;
  527. Dups = Size;
  528. }
  529. MachineBasicBlock *TExit = FalseBranch ? TrueBBI.FalseBB : TrueBBI.TrueBB;
  530. if (!TExit && blockAlwaysFallThrough(TrueBBI)) {
  531. MachineFunction::iterator I = TrueBBI.BB->getIterator();
  532. if (++I == TrueBBI.BB->getParent()->end())
  533. return false;
  534. TExit = &*I;
  535. }
  536. return TExit && TExit == FalseBBI.BB;
  537. }
  538. /// Increment \p It until it points to a non-debug instruction or to \p End.
  539. /// @param It Iterator to increment
  540. /// @param End Iterator that points to end. Will be compared to It
  541. /// @returns true if It == End, false otherwise.
  542. static inline bool skipDebugInstructionsForward(
  543. MachineBasicBlock::iterator &It,
  544. MachineBasicBlock::iterator &End) {
  545. while (It != End && It->isDebugValue())
  546. It++;
  547. return It == End;
  548. }
  549. /// Shrink the provided inclusive range by one instruction.
  550. /// If the range was one instruction (\p It == \p Begin), It is not modified,
  551. /// but \p Empty is set to true.
  552. static inline void shrinkInclusiveRange(
  553. MachineBasicBlock::iterator &Begin,
  554. MachineBasicBlock::iterator &It,
  555. bool &Empty) {
  556. if (It == Begin)
  557. Empty = true;
  558. else
  559. It--;
  560. }
  561. /// Decrement \p It until it points to a non-debug instruction or the range is
  562. /// empty.
  563. /// @param It Iterator to decrement.
  564. /// @param Begin Iterator that points to beginning. Will be compared to It
  565. /// @param Empty Set to true if the resulting range is Empty
  566. /// @returns the value of Empty as a convenience.
  567. static inline bool skipDebugInstructionsBackward(
  568. MachineBasicBlock::iterator &Begin,
  569. MachineBasicBlock::iterator &It,
  570. bool &Empty) {
  571. while (!Empty && It->isDebugValue())
  572. shrinkInclusiveRange(Begin, It, Empty);
  573. return Empty;
  574. }
  575. /// Count duplicated instructions and move the iterators to show where they
  576. /// are.
  577. /// @param TIB True Iterator Begin
  578. /// @param FIB False Iterator Begin
  579. /// These two iterators initially point to the first instruction of the two
  580. /// blocks, and finally point to the first non-shared instruction.
  581. /// @param TIE True Iterator End
  582. /// @param FIE False Iterator End
  583. /// These two iterators initially point to End() for the two blocks() and
  584. /// finally point to the first shared instruction in the tail.
  585. /// Upon return [TIB, TIE), and [FIB, FIE) mark the un-duplicated portions of
  586. /// two blocks.
  587. /// @param Dups1 count of duplicated instructions at the beginning of the 2
  588. /// blocks.
  589. /// @param Dups2 count of duplicated instructions at the end of the 2 blocks.
  590. /// @param SkipUnconditionalBranches if true, Don't make sure that
  591. /// unconditional branches at the end of the blocks are the same. True is
  592. /// passed when the blocks are analyzable to allow for fallthrough to be
  593. /// handled.
  594. /// @return false if the shared portion prevents if conversion.
  595. bool IfConverter::CountDuplicatedInstructions(
  596. MachineBasicBlock::iterator &TIB,
  597. MachineBasicBlock::iterator &FIB,
  598. MachineBasicBlock::iterator &TIE,
  599. MachineBasicBlock::iterator &FIE,
  600. unsigned &Dups1, unsigned &Dups2,
  601. MachineBasicBlock &TBB, MachineBasicBlock &FBB,
  602. bool SkipUnconditionalBranches) const {
  603. while (TIB != TIE && FIB != FIE) {
  604. // Skip dbg_value instructions. These do not count.
  605. if(skipDebugInstructionsForward(TIB, TIE))
  606. break;
  607. if(skipDebugInstructionsForward(FIB, FIE))
  608. break;
  609. if (!TIB->isIdenticalTo(*FIB))
  610. break;
  611. // A pred-clobbering instruction in the shared portion prevents
  612. // if-conversion.
  613. std::vector<MachineOperand> PredDefs;
  614. if (TII->DefinesPredicate(*TIB, PredDefs))
  615. return false;
  616. // If we get all the way to the branch instructions, don't count them.
  617. if (!TIB->isBranch())
  618. ++Dups1;
  619. ++TIB;
  620. ++FIB;
  621. }
  622. // Check for already containing all of the block.
  623. if (TIB == TIE || FIB == FIE)
  624. return true;
  625. // Now, in preparation for counting duplicate instructions at the ends of the
  626. // blocks, move the end iterators up past any branch instructions.
  627. --TIE;
  628. --FIE;
  629. // After this point TIB and TIE define an inclusive range, which means that
  630. // TIB == TIE is true when there is one more instruction to consider, not at
  631. // the end. Because we may not be able to go before TIB, we need a flag to
  632. // indicate a completely empty range.
  633. bool TEmpty = false, FEmpty = false;
  634. // Upon exit TIE and FIE will both point at the last non-shared instruction.
  635. // They need to be moved forward to point past the last non-shared
  636. // instruction if the range they delimit is non-empty.
  637. auto IncrementEndIteratorsOnExit = make_scope_exit([&]() {
  638. if (!TEmpty)
  639. ++TIE;
  640. if (!FEmpty)
  641. ++FIE;
  642. });
  643. if (!TBB.succ_empty() || !FBB.succ_empty()) {
  644. if (SkipUnconditionalBranches) {
  645. while (!TEmpty && TIE->isUnconditionalBranch())
  646. shrinkInclusiveRange(TIB, TIE, TEmpty);
  647. while (!FEmpty && FIE->isUnconditionalBranch())
  648. shrinkInclusiveRange(FIB, FIE, FEmpty);
  649. }
  650. }
  651. // If Dups1 includes all of a block, then don't count duplicate
  652. // instructions at the end of the blocks.
  653. if (TEmpty || FEmpty)
  654. return true;
  655. // Count duplicate instructions at the ends of the blocks.
  656. while (!TEmpty && !FEmpty) {
  657. // Skip dbg_value instructions. These do not count.
  658. if (skipDebugInstructionsBackward(TIB, TIE, TEmpty))
  659. break;
  660. if (skipDebugInstructionsBackward(FIB, FIE, FEmpty))
  661. break;
  662. if (!TIE->isIdenticalTo(*FIE))
  663. break;
  664. // We have to verify that any branch instructions are the same, and then we
  665. // don't count them toward the # of duplicate instructions.
  666. if (!TIE->isBranch())
  667. ++Dups2;
  668. shrinkInclusiveRange(TIB, TIE, TEmpty);
  669. shrinkInclusiveRange(FIB, FIE, FEmpty);
  670. }
  671. return true;
  672. }
  673. /// RescanInstructions - Run ScanInstructions on a pair of blocks.
  674. /// @param TIB - True Iterator Begin, points to first non-shared instruction
  675. /// @param FIB - False Iterator Begin, points to first non-shared instruction
  676. /// @param TIE - True Iterator End, points past last non-shared instruction
  677. /// @param FIE - False Iterator End, points past last non-shared instruction
  678. /// @param TrueBBI - BBInfo to update for the true block.
  679. /// @param FalseBBI - BBInfo to update for the false block.
  680. /// @returns - false if either block cannot be predicated or if both blocks end
  681. /// with a predicate-clobbering instruction.
  682. bool IfConverter::RescanInstructions(
  683. MachineBasicBlock::iterator &TIB, MachineBasicBlock::iterator &FIB,
  684. MachineBasicBlock::iterator &TIE, MachineBasicBlock::iterator &FIE,
  685. BBInfo &TrueBBI, BBInfo &FalseBBI) const {
  686. bool BranchUnpredicable = true;
  687. TrueBBI.IsUnpredicable = FalseBBI.IsUnpredicable = false;
  688. ScanInstructions(TrueBBI, TIB, TIE, BranchUnpredicable);
  689. if (TrueBBI.IsUnpredicable)
  690. return false;
  691. ScanInstructions(FalseBBI, FIB, FIE, BranchUnpredicable);
  692. if (FalseBBI.IsUnpredicable)
  693. return false;
  694. if (TrueBBI.ClobbersPred && FalseBBI.ClobbersPred)
  695. return false;
  696. return true;
  697. }
  698. #ifndef NDEBUG
  699. static void verifySameBranchInstructions(
  700. MachineBasicBlock *MBB1,
  701. MachineBasicBlock *MBB2) {
  702. MachineBasicBlock::iterator B1 = MBB1->begin();
  703. MachineBasicBlock::iterator B2 = MBB2->begin();
  704. MachineBasicBlock::iterator E1 = std::prev(MBB1->end());
  705. MachineBasicBlock::iterator E2 = std::prev(MBB2->end());
  706. bool Empty1 = false, Empty2 = false;
  707. while (!Empty1 && !Empty2) {
  708. skipDebugInstructionsBackward(B1, E1, Empty1);
  709. skipDebugInstructionsBackward(B2, E2, Empty2);
  710. if (Empty1 && Empty2)
  711. break;
  712. if (Empty1) {
  713. assert(!E2->isBranch() && "Branch mis-match, one block is empty.");
  714. break;
  715. }
  716. if (Empty2) {
  717. assert(!E1->isBranch() && "Branch mis-match, one block is empty.");
  718. break;
  719. }
  720. if (E1->isBranch() || E2->isBranch())
  721. assert(E1->isIdenticalTo(*E2) &&
  722. "Branch mis-match, branch instructions don't match.");
  723. else
  724. break;
  725. shrinkInclusiveRange(B1, E1, Empty1);
  726. shrinkInclusiveRange(B2, E2, Empty2);
  727. }
  728. }
  729. #endif
  730. /// ValidForkedDiamond - Returns true if the 'true' and 'false' blocks (along
  731. /// with their common predecessor) form a diamond if a common tail block is
  732. /// extracted.
  733. /// While not strictly a diamond, this pattern would form a diamond if
  734. /// tail-merging had merged the shared tails.
  735. /// EBB
  736. /// _/ \_
  737. /// | |
  738. /// TBB FBB
  739. /// / \ / \
  740. /// FalseBB TrueBB FalseBB
  741. /// Currently only handles analyzable branches.
  742. /// Specifically excludes actual diamonds to avoid overlap.
  743. bool IfConverter::ValidForkedDiamond(
  744. BBInfo &TrueBBI, BBInfo &FalseBBI,
  745. unsigned &Dups1, unsigned &Dups2,
  746. BBInfo &TrueBBICalc, BBInfo &FalseBBICalc) const {
  747. Dups1 = Dups2 = 0;
  748. if (TrueBBI.IsBeingAnalyzed || TrueBBI.IsDone ||
  749. FalseBBI.IsBeingAnalyzed || FalseBBI.IsDone)
  750. return false;
  751. if (!TrueBBI.IsBrAnalyzable || !FalseBBI.IsBrAnalyzable)
  752. return false;
  753. // Don't IfConvert blocks that can't be folded into their predecessor.
  754. if (TrueBBI.BB->pred_size() > 1 || FalseBBI.BB->pred_size() > 1)
  755. return false;
  756. // This function is specifically looking for conditional tails, as
  757. // unconditional tails are already handled by the standard diamond case.
  758. if (TrueBBI.BrCond.size() == 0 ||
  759. FalseBBI.BrCond.size() == 0)
  760. return false;
  761. MachineBasicBlock *TT = TrueBBI.TrueBB;
  762. MachineBasicBlock *TF = TrueBBI.FalseBB;
  763. MachineBasicBlock *FT = FalseBBI.TrueBB;
  764. MachineBasicBlock *FF = FalseBBI.FalseBB;
  765. if (!TT)
  766. TT = getNextBlock(*TrueBBI.BB);
  767. if (!TF)
  768. TF = getNextBlock(*TrueBBI.BB);
  769. if (!FT)
  770. FT = getNextBlock(*FalseBBI.BB);
  771. if (!FF)
  772. FF = getNextBlock(*FalseBBI.BB);
  773. if (!TT || !TF)
  774. return false;
  775. // Check successors. If they don't match, bail.
  776. if (!((TT == FT && TF == FF) || (TF == FT && TT == FF)))
  777. return false;
  778. bool FalseReversed = false;
  779. if (TF == FT && TT == FF) {
  780. // If the branches are opposing, but we can't reverse, don't do it.
  781. if (!FalseBBI.IsBrReversible)
  782. return false;
  783. FalseReversed = true;
  784. ReverseBranchCondition(FalseBBI);
  785. }
  786. auto UnReverseOnExit = make_scope_exit([&]() {
  787. if (FalseReversed)
  788. ReverseBranchCondition(FalseBBI);
  789. });
  790. // Count duplicate instructions at the beginning of the true and false blocks.
  791. MachineBasicBlock::iterator TIB = TrueBBI.BB->begin();
  792. MachineBasicBlock::iterator FIB = FalseBBI.BB->begin();
  793. MachineBasicBlock::iterator TIE = TrueBBI.BB->end();
  794. MachineBasicBlock::iterator FIE = FalseBBI.BB->end();
  795. if(!CountDuplicatedInstructions(TIB, FIB, TIE, FIE, Dups1, Dups2,
  796. *TrueBBI.BB, *FalseBBI.BB,
  797. /* SkipUnconditionalBranches */ true))
  798. return false;
  799. TrueBBICalc.BB = TrueBBI.BB;
  800. FalseBBICalc.BB = FalseBBI.BB;
  801. if (!RescanInstructions(TIB, FIB, TIE, FIE, TrueBBICalc, FalseBBICalc))
  802. return false;
  803. // The size is used to decide whether to if-convert, and the shared portions
  804. // are subtracted off. Because of the subtraction, we just use the size that
  805. // was calculated by the original ScanInstructions, as it is correct.
  806. TrueBBICalc.NonPredSize = TrueBBI.NonPredSize;
  807. FalseBBICalc.NonPredSize = FalseBBI.NonPredSize;
  808. return true;
  809. }
  810. /// ValidDiamond - Returns true if the 'true' and 'false' blocks (along
  811. /// with their common predecessor) forms a valid diamond shape for ifcvt.
  812. bool IfConverter::ValidDiamond(
  813. BBInfo &TrueBBI, BBInfo &FalseBBI,
  814. unsigned &Dups1, unsigned &Dups2,
  815. BBInfo &TrueBBICalc, BBInfo &FalseBBICalc) const {
  816. Dups1 = Dups2 = 0;
  817. if (TrueBBI.IsBeingAnalyzed || TrueBBI.IsDone ||
  818. FalseBBI.IsBeingAnalyzed || FalseBBI.IsDone)
  819. return false;
  820. MachineBasicBlock *TT = TrueBBI.TrueBB;
  821. MachineBasicBlock *FT = FalseBBI.TrueBB;
  822. if (!TT && blockAlwaysFallThrough(TrueBBI))
  823. TT = getNextBlock(*TrueBBI.BB);
  824. if (!FT && blockAlwaysFallThrough(FalseBBI))
  825. FT = getNextBlock(*FalseBBI.BB);
  826. if (TT != FT)
  827. return false;
  828. if (!TT && (TrueBBI.IsBrAnalyzable || FalseBBI.IsBrAnalyzable))
  829. return false;
  830. if (TrueBBI.BB->pred_size() > 1 || FalseBBI.BB->pred_size() > 1)
  831. return false;
  832. // FIXME: Allow true block to have an early exit?
  833. if (TrueBBI.FalseBB || FalseBBI.FalseBB)
  834. return false;
  835. // Count duplicate instructions at the beginning and end of the true and
  836. // false blocks.
  837. // Skip unconditional branches only if we are considering an analyzable
  838. // diamond. Otherwise the branches must be the same.
  839. bool SkipUnconditionalBranches =
  840. TrueBBI.IsBrAnalyzable && FalseBBI.IsBrAnalyzable;
  841. MachineBasicBlock::iterator TIB = TrueBBI.BB->begin();
  842. MachineBasicBlock::iterator FIB = FalseBBI.BB->begin();
  843. MachineBasicBlock::iterator TIE = TrueBBI.BB->end();
  844. MachineBasicBlock::iterator FIE = FalseBBI.BB->end();
  845. if(!CountDuplicatedInstructions(TIB, FIB, TIE, FIE, Dups1, Dups2,
  846. *TrueBBI.BB, *FalseBBI.BB,
  847. SkipUnconditionalBranches))
  848. return false;
  849. TrueBBICalc.BB = TrueBBI.BB;
  850. FalseBBICalc.BB = FalseBBI.BB;
  851. if (!RescanInstructions(TIB, FIB, TIE, FIE, TrueBBICalc, FalseBBICalc))
  852. return false;
  853. // The size is used to decide whether to if-convert, and the shared portions
  854. // are subtracted off. Because of the subtraction, we just use the size that
  855. // was calculated by the original ScanInstructions, as it is correct.
  856. TrueBBICalc.NonPredSize = TrueBBI.NonPredSize;
  857. FalseBBICalc.NonPredSize = FalseBBI.NonPredSize;
  858. return true;
  859. }
  860. /// AnalyzeBranches - Look at the branches at the end of a block to determine if
  861. /// the block is predicable.
  862. void IfConverter::AnalyzeBranches(BBInfo &BBI) {
  863. if (BBI.IsDone)
  864. return;
  865. BBI.TrueBB = BBI.FalseBB = nullptr;
  866. BBI.BrCond.clear();
  867. BBI.IsBrAnalyzable =
  868. !TII->analyzeBranch(*BBI.BB, BBI.TrueBB, BBI.FalseBB, BBI.BrCond);
  869. SmallVector<MachineOperand, 4> RevCond(BBI.BrCond.begin(), BBI.BrCond.end());
  870. BBI.IsBrReversible = (RevCond.size() == 0) ||
  871. !TII->ReverseBranchCondition(RevCond);
  872. BBI.HasFallThrough = BBI.IsBrAnalyzable && BBI.FalseBB == nullptr;
  873. if (BBI.BrCond.size()) {
  874. // No false branch. This BB must end with a conditional branch and a
  875. // fallthrough.
  876. if (!BBI.FalseBB)
  877. BBI.FalseBB = findFalseBlock(BBI.BB, BBI.TrueBB);
  878. if (!BBI.FalseBB) {
  879. // Malformed bcc? True and false blocks are the same?
  880. BBI.IsUnpredicable = true;
  881. }
  882. }
  883. }
  884. /// ScanInstructions - Scan all the instructions in the block to determine if
  885. /// the block is predicable. In most cases, that means all the instructions
  886. /// in the block are isPredicable(). Also checks if the block contains any
  887. /// instruction which can clobber a predicate (e.g. condition code register).
  888. /// If so, the block is not predicable unless it's the last instruction.
  889. void IfConverter::ScanInstructions(BBInfo &BBI,
  890. MachineBasicBlock::iterator &Begin,
  891. MachineBasicBlock::iterator &End,
  892. bool BranchUnpredicable) const {
  893. if (BBI.IsDone || BBI.IsUnpredicable)
  894. return;
  895. bool AlreadyPredicated = !BBI.Predicate.empty();
  896. BBI.NonPredSize = 0;
  897. BBI.ExtraCost = 0;
  898. BBI.ExtraCost2 = 0;
  899. BBI.ClobbersPred = false;
  900. for (MachineInstr &MI : make_range(Begin, End)) {
  901. if (MI.isDebugValue())
  902. continue;
  903. // It's unsafe to duplicate convergent instructions in this context, so set
  904. // BBI.CannotBeCopied to true if MI is convergent. To see why, consider the
  905. // following CFG, which is subject to our "simple" transformation.
  906. //
  907. // BB0 // if (c1) goto BB1; else goto BB2;
  908. // / \
  909. // BB1 |
  910. // | BB2 // if (c2) goto TBB; else goto FBB;
  911. // | / |
  912. // | / |
  913. // TBB |
  914. // | |
  915. // | FBB
  916. // |
  917. // exit
  918. //
  919. // Suppose we want to move TBB's contents up into BB1 and BB2 (in BB1 they'd
  920. // be unconditional, and in BB2, they'd be predicated upon c2), and suppose
  921. // TBB contains a convergent instruction. This is safe iff doing so does
  922. // not add a control-flow dependency to the convergent instruction -- i.e.,
  923. // it's safe iff the set of control flows that leads us to the convergent
  924. // instruction does not get smaller after the transformation.
  925. //
  926. // Originally we executed TBB if c1 || c2. After the transformation, there
  927. // are two copies of TBB's instructions. We get to the first if c1, and we
  928. // get to the second if !c1 && c2.
  929. //
  930. // There are clearly fewer ways to satisfy the condition "c1" than
  931. // "c1 || c2". Since we've shrunk the set of control flows which lead to
  932. // our convergent instruction, the transformation is unsafe.
  933. if (MI.isNotDuplicable() || MI.isConvergent())
  934. BBI.CannotBeCopied = true;
  935. bool isPredicated = TII->isPredicated(MI);
  936. bool isCondBr = BBI.IsBrAnalyzable && MI.isConditionalBranch();
  937. if (BranchUnpredicable && MI.isBranch()) {
  938. BBI.IsUnpredicable = true;
  939. return;
  940. }
  941. // A conditional branch is not predicable, but it may be eliminated.
  942. if (isCondBr)
  943. continue;
  944. if (!isPredicated) {
  945. BBI.NonPredSize++;
  946. unsigned ExtraPredCost = TII->getPredicationCost(MI);
  947. unsigned NumCycles = SchedModel.computeInstrLatency(&MI, false);
  948. if (NumCycles > 1)
  949. BBI.ExtraCost += NumCycles-1;
  950. BBI.ExtraCost2 += ExtraPredCost;
  951. } else if (!AlreadyPredicated) {
  952. // FIXME: This instruction is already predicated before the
  953. // if-conversion pass. It's probably something like a conditional move.
  954. // Mark this block unpredicable for now.
  955. BBI.IsUnpredicable = true;
  956. return;
  957. }
  958. if (BBI.ClobbersPred && !isPredicated) {
  959. // Predicate modification instruction should end the block (except for
  960. // already predicated instructions and end of block branches).
  961. // Predicate may have been modified, the subsequent (currently)
  962. // unpredicated instructions cannot be correctly predicated.
  963. BBI.IsUnpredicable = true;
  964. return;
  965. }
  966. // FIXME: Make use of PredDefs? e.g. ADDC, SUBC sets predicates but are
  967. // still potentially predicable.
  968. std::vector<MachineOperand> PredDefs;
  969. if (TII->DefinesPredicate(MI, PredDefs))
  970. BBI.ClobbersPred = true;
  971. if (!TII->isPredicable(MI)) {
  972. BBI.IsUnpredicable = true;
  973. return;
  974. }
  975. }
  976. }
  977. /// Determine if the block is a suitable candidate to be predicated by the
  978. /// specified predicate.
  979. /// @param BBI BBInfo for the block to check
  980. /// @param Pred Predicate array for the branch that leads to BBI
  981. /// @param isTriangle true if the Analysis is for a triangle
  982. /// @param RevBranch true if Reverse(Pred) leads to BBI (e.g. BBI is the false
  983. /// case
  984. /// @param hasCommonTail true if BBI shares a tail with a sibling block that
  985. /// contains any instruction that would make the block unpredicable.
  986. bool IfConverter::FeasibilityAnalysis(BBInfo &BBI,
  987. SmallVectorImpl<MachineOperand> &Pred,
  988. bool isTriangle, bool RevBranch,
  989. bool hasCommonTail) {
  990. // If the block is dead or unpredicable, then it cannot be predicated.
  991. // Two blocks may share a common unpredicable tail, but this doesn't prevent
  992. // them from being if-converted. The non-shared portion is assumed to have
  993. // been checked
  994. if (BBI.IsDone || (BBI.IsUnpredicable && !hasCommonTail))
  995. return false;
  996. // If it is already predicated but we couldn't analyze its terminator, the
  997. // latter might fallthrough, but we can't determine where to.
  998. // Conservatively avoid if-converting again.
  999. if (BBI.Predicate.size() && !BBI.IsBrAnalyzable)
  1000. return false;
  1001. // If it is already predicated, check if the new predicate subsumes
  1002. // its predicate.
  1003. if (BBI.Predicate.size() && !TII->SubsumesPredicate(Pred, BBI.Predicate))
  1004. return false;
  1005. if (!hasCommonTail && BBI.BrCond.size()) {
  1006. if (!isTriangle)
  1007. return false;
  1008. // Test predicate subsumption.
  1009. SmallVector<MachineOperand, 4> RevPred(Pred.begin(), Pred.end());
  1010. SmallVector<MachineOperand, 4> Cond(BBI.BrCond.begin(), BBI.BrCond.end());
  1011. if (RevBranch) {
  1012. if (TII->ReverseBranchCondition(Cond))
  1013. return false;
  1014. }
  1015. if (TII->ReverseBranchCondition(RevPred) ||
  1016. !TII->SubsumesPredicate(Cond, RevPred))
  1017. return false;
  1018. }
  1019. return true;
  1020. }
  1021. /// Analyze the structure of the sub-CFG starting from the specified block.
  1022. /// Record its successors and whether it looks like an if-conversion candidate.
  1023. void IfConverter::AnalyzeBlock(
  1024. MachineBasicBlock &MBB, std::vector<std::unique_ptr<IfcvtToken>> &Tokens) {
  1025. struct BBState {
  1026. BBState(MachineBasicBlock &MBB) : MBB(&MBB), SuccsAnalyzed(false) {}
  1027. MachineBasicBlock *MBB;
  1028. /// This flag is true if MBB's successors have been analyzed.
  1029. bool SuccsAnalyzed;
  1030. };
  1031. // Push MBB to the stack.
  1032. SmallVector<BBState, 16> BBStack(1, MBB);
  1033. while (!BBStack.empty()) {
  1034. BBState &State = BBStack.back();
  1035. MachineBasicBlock *BB = State.MBB;
  1036. BBInfo &BBI = BBAnalysis[BB->getNumber()];
  1037. if (!State.SuccsAnalyzed) {
  1038. if (BBI.IsAnalyzed || BBI.IsBeingAnalyzed) {
  1039. BBStack.pop_back();
  1040. continue;
  1041. }
  1042. BBI.BB = BB;
  1043. BBI.IsBeingAnalyzed = true;
  1044. AnalyzeBranches(BBI);
  1045. MachineBasicBlock::iterator Begin = BBI.BB->begin();
  1046. MachineBasicBlock::iterator End = BBI.BB->end();
  1047. ScanInstructions(BBI, Begin, End);
  1048. // Unanalyzable or ends with fallthrough or unconditional branch, or if is
  1049. // not considered for ifcvt anymore.
  1050. if (!BBI.IsBrAnalyzable || BBI.BrCond.empty() || BBI.IsDone) {
  1051. BBI.IsBeingAnalyzed = false;
  1052. BBI.IsAnalyzed = true;
  1053. BBStack.pop_back();
  1054. continue;
  1055. }
  1056. // Do not ifcvt if either path is a back edge to the entry block.
  1057. if (BBI.TrueBB == BB || BBI.FalseBB == BB) {
  1058. BBI.IsBeingAnalyzed = false;
  1059. BBI.IsAnalyzed = true;
  1060. BBStack.pop_back();
  1061. continue;
  1062. }
  1063. // Do not ifcvt if true and false fallthrough blocks are the same.
  1064. if (!BBI.FalseBB) {
  1065. BBI.IsBeingAnalyzed = false;
  1066. BBI.IsAnalyzed = true;
  1067. BBStack.pop_back();
  1068. continue;
  1069. }
  1070. // Push the False and True blocks to the stack.
  1071. State.SuccsAnalyzed = true;
  1072. BBStack.push_back(*BBI.FalseBB);
  1073. BBStack.push_back(*BBI.TrueBB);
  1074. continue;
  1075. }
  1076. BBInfo &TrueBBI = BBAnalysis[BBI.TrueBB->getNumber()];
  1077. BBInfo &FalseBBI = BBAnalysis[BBI.FalseBB->getNumber()];
  1078. if (TrueBBI.IsDone && FalseBBI.IsDone) {
  1079. BBI.IsBeingAnalyzed = false;
  1080. BBI.IsAnalyzed = true;
  1081. BBStack.pop_back();
  1082. continue;
  1083. }
  1084. SmallVector<MachineOperand, 4>
  1085. RevCond(BBI.BrCond.begin(), BBI.BrCond.end());
  1086. bool CanRevCond = !TII->ReverseBranchCondition(RevCond);
  1087. unsigned Dups = 0;
  1088. unsigned Dups2 = 0;
  1089. bool TNeedSub = !TrueBBI.Predicate.empty();
  1090. bool FNeedSub = !FalseBBI.Predicate.empty();
  1091. bool Enqueued = false;
  1092. BranchProbability Prediction = MBPI->getEdgeProbability(BB, TrueBBI.BB);
  1093. if (CanRevCond) {
  1094. BBInfo TrueBBICalc, FalseBBICalc;
  1095. auto feasibleDiamond = [&]() {
  1096. bool MeetsSize = MeetIfcvtSizeLimit(
  1097. *TrueBBI.BB, (TrueBBICalc.NonPredSize - (Dups + Dups2) +
  1098. TrueBBICalc.ExtraCost), TrueBBICalc.ExtraCost2,
  1099. *FalseBBI.BB, (FalseBBICalc.NonPredSize - (Dups + Dups2) +
  1100. FalseBBICalc.ExtraCost), FalseBBICalc.ExtraCost2,
  1101. Prediction);
  1102. bool TrueFeasible = FeasibilityAnalysis(TrueBBI, BBI.BrCond,
  1103. /* IsTriangle */ false, /* RevCond */ false,
  1104. /* hasCommonTail */ true);
  1105. bool FalseFeasible = FeasibilityAnalysis(FalseBBI, RevCond,
  1106. /* IsTriangle */ false, /* RevCond */ false,
  1107. /* hasCommonTail */ true);
  1108. return MeetsSize && TrueFeasible && FalseFeasible;
  1109. };
  1110. if (ValidDiamond(TrueBBI, FalseBBI, Dups, Dups2,
  1111. TrueBBICalc, FalseBBICalc)) {
  1112. if (feasibleDiamond()) {
  1113. // Diamond:
  1114. // EBB
  1115. // / \_
  1116. // | |
  1117. // TBB FBB
  1118. // \ /
  1119. // TailBB
  1120. // Note TailBB can be empty.
  1121. Tokens.push_back(llvm::make_unique<IfcvtToken>(
  1122. BBI, ICDiamond, TNeedSub | FNeedSub, Dups, Dups2,
  1123. (bool) TrueBBICalc.ClobbersPred, (bool) FalseBBICalc.ClobbersPred));
  1124. Enqueued = true;
  1125. }
  1126. } else if (ValidForkedDiamond(TrueBBI, FalseBBI, Dups, Dups2,
  1127. TrueBBICalc, FalseBBICalc)) {
  1128. if (feasibleDiamond()) {
  1129. // ForkedDiamond:
  1130. // if TBB and FBB have a common tail that includes their conditional
  1131. // branch instructions, then we can If Convert this pattern.
  1132. // EBB
  1133. // _/ \_
  1134. // | |
  1135. // TBB FBB
  1136. // / \ / \
  1137. // FalseBB TrueBB FalseBB
  1138. //
  1139. Tokens.push_back(llvm::make_unique<IfcvtToken>(
  1140. BBI, ICForkedDiamond, TNeedSub | FNeedSub, Dups, Dups2,
  1141. (bool) TrueBBICalc.ClobbersPred, (bool) FalseBBICalc.ClobbersPred));
  1142. Enqueued = true;
  1143. }
  1144. }
  1145. }
  1146. if (ValidTriangle(TrueBBI, FalseBBI, false, Dups, Prediction) &&
  1147. MeetIfcvtSizeLimit(*TrueBBI.BB, TrueBBI.NonPredSize + TrueBBI.ExtraCost,
  1148. TrueBBI.ExtraCost2, Prediction) &&
  1149. FeasibilityAnalysis(TrueBBI, BBI.BrCond, true)) {
  1150. // Triangle:
  1151. // EBB
  1152. // | \_
  1153. // | |
  1154. // | TBB
  1155. // | /
  1156. // FBB
  1157. Tokens.push_back(
  1158. llvm::make_unique<IfcvtToken>(BBI, ICTriangle, TNeedSub, Dups));
  1159. Enqueued = true;
  1160. }
  1161. if (ValidTriangle(TrueBBI, FalseBBI, true, Dups, Prediction) &&
  1162. MeetIfcvtSizeLimit(*TrueBBI.BB, TrueBBI.NonPredSize + TrueBBI.ExtraCost,
  1163. TrueBBI.ExtraCost2, Prediction) &&
  1164. FeasibilityAnalysis(TrueBBI, BBI.BrCond, true, true)) {
  1165. Tokens.push_back(
  1166. llvm::make_unique<IfcvtToken>(BBI, ICTriangleRev, TNeedSub, Dups));
  1167. Enqueued = true;
  1168. }
  1169. if (ValidSimple(TrueBBI, Dups, Prediction) &&
  1170. MeetIfcvtSizeLimit(*TrueBBI.BB, TrueBBI.NonPredSize + TrueBBI.ExtraCost,
  1171. TrueBBI.ExtraCost2, Prediction) &&
  1172. FeasibilityAnalysis(TrueBBI, BBI.BrCond)) {
  1173. // Simple (split, no rejoin):
  1174. // EBB
  1175. // | \_
  1176. // | |
  1177. // | TBB---> exit
  1178. // |
  1179. // FBB
  1180. Tokens.push_back(
  1181. llvm::make_unique<IfcvtToken>(BBI, ICSimple, TNeedSub, Dups));
  1182. Enqueued = true;
  1183. }
  1184. if (CanRevCond) {
  1185. // Try the other path...
  1186. if (ValidTriangle(FalseBBI, TrueBBI, false, Dups,
  1187. Prediction.getCompl()) &&
  1188. MeetIfcvtSizeLimit(*FalseBBI.BB,
  1189. FalseBBI.NonPredSize + FalseBBI.ExtraCost,
  1190. FalseBBI.ExtraCost2, Prediction.getCompl()) &&
  1191. FeasibilityAnalysis(FalseBBI, RevCond, true)) {
  1192. Tokens.push_back(llvm::make_unique<IfcvtToken>(BBI, ICTriangleFalse,
  1193. FNeedSub, Dups));
  1194. Enqueued = true;
  1195. }
  1196. if (ValidTriangle(FalseBBI, TrueBBI, true, Dups,
  1197. Prediction.getCompl()) &&
  1198. MeetIfcvtSizeLimit(*FalseBBI.BB,
  1199. FalseBBI.NonPredSize + FalseBBI.ExtraCost,
  1200. FalseBBI.ExtraCost2, Prediction.getCompl()) &&
  1201. FeasibilityAnalysis(FalseBBI, RevCond, true, true)) {
  1202. Tokens.push_back(
  1203. llvm::make_unique<IfcvtToken>(BBI, ICTriangleFRev, FNeedSub, Dups));
  1204. Enqueued = true;
  1205. }
  1206. if (ValidSimple(FalseBBI, Dups, Prediction.getCompl()) &&
  1207. MeetIfcvtSizeLimit(*FalseBBI.BB,
  1208. FalseBBI.NonPredSize + FalseBBI.ExtraCost,
  1209. FalseBBI.ExtraCost2, Prediction.getCompl()) &&
  1210. FeasibilityAnalysis(FalseBBI, RevCond)) {
  1211. Tokens.push_back(
  1212. llvm::make_unique<IfcvtToken>(BBI, ICSimpleFalse, FNeedSub, Dups));
  1213. Enqueued = true;
  1214. }
  1215. }
  1216. BBI.IsEnqueued = Enqueued;
  1217. BBI.IsBeingAnalyzed = false;
  1218. BBI.IsAnalyzed = true;
  1219. BBStack.pop_back();
  1220. }
  1221. }
  1222. /// Analyze all blocks and find entries for all if-conversion candidates.
  1223. void IfConverter::AnalyzeBlocks(
  1224. MachineFunction &MF, std::vector<std::unique_ptr<IfcvtToken>> &Tokens) {
  1225. for (MachineBasicBlock &MBB : MF)
  1226. AnalyzeBlock(MBB, Tokens);
  1227. // Sort to favor more complex ifcvt scheme.
  1228. std::stable_sort(Tokens.begin(), Tokens.end(), IfcvtTokenCmp);
  1229. }
  1230. /// Returns true either if ToMBB is the next block after MBB or that all the
  1231. /// intervening blocks are empty (given MBB can fall through to its next block).
  1232. static bool canFallThroughTo(MachineBasicBlock &MBB, MachineBasicBlock &ToMBB) {
  1233. MachineFunction::iterator PI = MBB.getIterator();
  1234. MachineFunction::iterator I = std::next(PI);
  1235. MachineFunction::iterator TI = ToMBB.getIterator();
  1236. MachineFunction::iterator E = MBB.getParent()->end();
  1237. while (I != TI) {
  1238. // Check isSuccessor to avoid case where the next block is empty, but
  1239. // it's not a successor.
  1240. if (I == E || !I->empty() || !PI->isSuccessor(&*I))
  1241. return false;
  1242. PI = I++;
  1243. }
  1244. return true;
  1245. }
  1246. /// Invalidate predecessor BB info so it would be re-analyzed to determine if it
  1247. /// can be if-converted. If predecessor is already enqueued, dequeue it!
  1248. void IfConverter::InvalidatePreds(MachineBasicBlock &MBB) {
  1249. for (const MachineBasicBlock *Predecessor : MBB.predecessors()) {
  1250. BBInfo &PBBI = BBAnalysis[Predecessor->getNumber()];
  1251. if (PBBI.IsDone || PBBI.BB == &MBB)
  1252. continue;
  1253. PBBI.IsAnalyzed = false;
  1254. PBBI.IsEnqueued = false;
  1255. }
  1256. }
  1257. /// Inserts an unconditional branch from \p MBB to \p ToMBB.
  1258. static void InsertUncondBranch(MachineBasicBlock &MBB, MachineBasicBlock &ToMBB,
  1259. const TargetInstrInfo *TII) {
  1260. DebugLoc dl; // FIXME: this is nowhere
  1261. SmallVector<MachineOperand, 0> NoCond;
  1262. TII->insertBranch(MBB, &ToMBB, nullptr, NoCond, dl);
  1263. }
  1264. /// Remove true / false edges if either / both are no longer successors.
  1265. void IfConverter::RemoveExtraEdges(BBInfo &BBI) {
  1266. MachineBasicBlock *TBB = nullptr, *FBB = nullptr;
  1267. SmallVector<MachineOperand, 4> Cond;
  1268. if (!TII->analyzeBranch(*BBI.BB, TBB, FBB, Cond))
  1269. BBI.BB->CorrectExtraCFGEdges(TBB, FBB, !Cond.empty());
  1270. }
  1271. /// Behaves like LiveRegUnits::StepForward() but also adds implicit uses to all
  1272. /// values defined in MI which are also live/used by MI.
  1273. static void UpdatePredRedefs(MachineInstr &MI, LivePhysRegs &Redefs) {
  1274. const TargetRegisterInfo *TRI = MI.getParent()->getParent()
  1275. ->getSubtarget().getRegisterInfo();
  1276. // Before stepping forward past MI, remember which regs were live
  1277. // before MI. This is needed to set the Undef flag only when reg is
  1278. // dead.
  1279. SparseSet<unsigned> LiveBeforeMI;
  1280. LiveBeforeMI.setUniverse(TRI->getNumRegs());
  1281. for (unsigned Reg : Redefs)
  1282. LiveBeforeMI.insert(Reg);
  1283. SmallVector<std::pair<unsigned, const MachineOperand*>, 4> Clobbers;
  1284. Redefs.stepForward(MI, Clobbers);
  1285. // Now add the implicit uses for each of the clobbered values.
  1286. for (auto Clobber : Clobbers) {
  1287. // FIXME: Const cast here is nasty, but better than making StepForward
  1288. // take a mutable instruction instead of const.
  1289. unsigned Reg = Clobber.first;
  1290. MachineOperand &Op = const_cast<MachineOperand&>(*Clobber.second);
  1291. MachineInstr *OpMI = Op.getParent();
  1292. MachineInstrBuilder MIB(*OpMI->getParent()->getParent(), OpMI);
  1293. if (Op.isRegMask()) {
  1294. // First handle regmasks. They clobber any entries in the mask which
  1295. // means that we need a def for those registers.
  1296. if (LiveBeforeMI.count(Reg))
  1297. MIB.addReg(Reg, RegState::Implicit);
  1298. // We also need to add an implicit def of this register for the later
  1299. // use to read from.
  1300. // For the register allocator to have allocated a register clobbered
  1301. // by the call which is used later, it must be the case that
  1302. // the call doesn't return.
  1303. MIB.addReg(Reg, RegState::Implicit | RegState::Define);
  1304. continue;
  1305. }
  1306. assert(Op.isReg() && "Register operand required");
  1307. if (Op.isDead()) {
  1308. // If we found a dead def, but it needs to be live, then remove the dead
  1309. // flag.
  1310. if (Redefs.contains(Op.getReg()))
  1311. Op.setIsDead(false);
  1312. }
  1313. if (LiveBeforeMI.count(Reg))
  1314. MIB.addReg(Reg, RegState::Implicit);
  1315. }
  1316. }
  1317. /// Remove kill flags from operands with a registers in the \p DontKill set.
  1318. static void RemoveKills(MachineInstr &MI, const LivePhysRegs &DontKill) {
  1319. for (MIBundleOperands O(MI); O.isValid(); ++O) {
  1320. if (!O->isReg() || !O->isKill())
  1321. continue;
  1322. if (DontKill.contains(O->getReg()))
  1323. O->setIsKill(false);
  1324. }
  1325. }
  1326. /// Walks a range of machine instructions and removes kill flags for registers
  1327. /// in the \p DontKill set.
  1328. static void RemoveKills(MachineBasicBlock::iterator I,
  1329. MachineBasicBlock::iterator E,
  1330. const LivePhysRegs &DontKill,
  1331. const MCRegisterInfo &MCRI) {
  1332. for (MachineInstr &MI : make_range(I, E))
  1333. RemoveKills(MI, DontKill);
  1334. }
  1335. /// If convert a simple (split, no rejoin) sub-CFG.
  1336. bool IfConverter::IfConvertSimple(BBInfo &BBI, IfcvtKind Kind) {
  1337. BBInfo &TrueBBI = BBAnalysis[BBI.TrueBB->getNumber()];
  1338. BBInfo &FalseBBI = BBAnalysis[BBI.FalseBB->getNumber()];
  1339. BBInfo *CvtBBI = &TrueBBI;
  1340. BBInfo *NextBBI = &FalseBBI;
  1341. SmallVector<MachineOperand, 4> Cond(BBI.BrCond.begin(), BBI.BrCond.end());
  1342. if (Kind == ICSimpleFalse)
  1343. std::swap(CvtBBI, NextBBI);
  1344. MachineBasicBlock &CvtMBB = *CvtBBI->BB;
  1345. MachineBasicBlock &NextMBB = *NextBBI->BB;
  1346. if (CvtBBI->IsDone ||
  1347. (CvtBBI->CannotBeCopied && CvtMBB.pred_size() > 1)) {
  1348. // Something has changed. It's no longer safe to predicate this block.
  1349. BBI.IsAnalyzed = false;
  1350. CvtBBI->IsAnalyzed = false;
  1351. return false;
  1352. }
  1353. if (CvtMBB.hasAddressTaken())
  1354. // Conservatively abort if-conversion if BB's address is taken.
  1355. return false;
  1356. if (Kind == ICSimpleFalse)
  1357. if (TII->ReverseBranchCondition(Cond))
  1358. llvm_unreachable("Unable to reverse branch condition!");
  1359. // Initialize liveins to the first BB. These are potentiall redefined by
  1360. // predicated instructions.
  1361. Redefs.init(TRI);
  1362. Redefs.addLiveIns(CvtMBB);
  1363. Redefs.addLiveIns(NextMBB);
  1364. // Compute a set of registers which must not be killed by instructions in
  1365. // BB1: This is everything live-in to BB2.
  1366. DontKill.init(TRI);
  1367. DontKill.addLiveIns(NextMBB);
  1368. if (CvtMBB.pred_size() > 1) {
  1369. BBI.NonPredSize -= TII->RemoveBranch(*BBI.BB);
  1370. // Copy instructions in the true block, predicate them, and add them to
  1371. // the entry block.
  1372. CopyAndPredicateBlock(BBI, *CvtBBI, Cond);
  1373. // RemoveExtraEdges won't work if the block has an unanalyzable branch, so
  1374. // explicitly remove CvtBBI as a successor.
  1375. BBI.BB->removeSuccessor(&CvtMBB, true);
  1376. } else {
  1377. RemoveKills(CvtMBB.begin(), CvtMBB.end(), DontKill, *TRI);
  1378. PredicateBlock(*CvtBBI, CvtMBB.end(), Cond);
  1379. // Merge converted block into entry block.
  1380. BBI.NonPredSize -= TII->RemoveBranch(*BBI.BB);
  1381. MergeBlocks(BBI, *CvtBBI);
  1382. }
  1383. bool IterIfcvt = true;
  1384. if (!canFallThroughTo(*BBI.BB, NextMBB)) {
  1385. InsertUncondBranch(*BBI.BB, NextMBB, TII);
  1386. BBI.HasFallThrough = false;
  1387. // Now ifcvt'd block will look like this:
  1388. // BB:
  1389. // ...
  1390. // t, f = cmp
  1391. // if t op
  1392. // b BBf
  1393. //
  1394. // We cannot further ifcvt this block because the unconditional branch
  1395. // will have to be predicated on the new condition, that will not be
  1396. // available if cmp executes.
  1397. IterIfcvt = false;
  1398. }
  1399. RemoveExtraEdges(BBI);
  1400. // Update block info. BB can be iteratively if-converted.
  1401. if (!IterIfcvt)
  1402. BBI.IsDone = true;
  1403. InvalidatePreds(*BBI.BB);
  1404. CvtBBI->IsDone = true;
  1405. // FIXME: Must maintain LiveIns.
  1406. return true;
  1407. }
  1408. /// If convert a triangle sub-CFG.
  1409. bool IfConverter::IfConvertTriangle(BBInfo &BBI, IfcvtKind Kind) {
  1410. BBInfo &TrueBBI = BBAnalysis[BBI.TrueBB->getNumber()];
  1411. BBInfo &FalseBBI = BBAnalysis[BBI.FalseBB->getNumber()];
  1412. BBInfo *CvtBBI = &TrueBBI;
  1413. BBInfo *NextBBI = &FalseBBI;
  1414. DebugLoc dl; // FIXME: this is nowhere
  1415. SmallVector<MachineOperand, 4> Cond(BBI.BrCond.begin(), BBI.BrCond.end());
  1416. if (Kind == ICTriangleFalse || Kind == ICTriangleFRev)
  1417. std::swap(CvtBBI, NextBBI);
  1418. MachineBasicBlock &CvtMBB = *CvtBBI->BB;
  1419. MachineBasicBlock &NextMBB = *NextBBI->BB;
  1420. if (CvtBBI->IsDone ||
  1421. (CvtBBI->CannotBeCopied && CvtMBB.pred_size() > 1)) {
  1422. // Something has changed. It's no longer safe to predicate this block.
  1423. BBI.IsAnalyzed = false;
  1424. CvtBBI->IsAnalyzed = false;
  1425. return false;
  1426. }
  1427. if (CvtMBB.hasAddressTaken())
  1428. // Conservatively abort if-conversion if BB's address is taken.
  1429. return false;
  1430. if (Kind == ICTriangleFalse || Kind == ICTriangleFRev)
  1431. if (TII->ReverseBranchCondition(Cond))
  1432. llvm_unreachable("Unable to reverse branch condition!");
  1433. if (Kind == ICTriangleRev || Kind == ICTriangleFRev) {
  1434. if (ReverseBranchCondition(*CvtBBI)) {
  1435. // BB has been changed, modify its predecessors (except for this
  1436. // one) so they don't get ifcvt'ed based on bad intel.
  1437. for (MachineBasicBlock *PBB : CvtMBB.predecessors()) {
  1438. if (PBB == BBI.BB)
  1439. continue;
  1440. BBInfo &PBBI = BBAnalysis[PBB->getNumber()];
  1441. if (PBBI.IsEnqueued) {
  1442. PBBI.IsAnalyzed = false;
  1443. PBBI.IsEnqueued = false;
  1444. }
  1445. }
  1446. }
  1447. }
  1448. // Initialize liveins to the first BB. These are potentially redefined by
  1449. // predicated instructions.
  1450. Redefs.init(TRI);
  1451. Redefs.addLiveIns(CvtMBB);
  1452. Redefs.addLiveIns(NextMBB);
  1453. DontKill.clear();
  1454. bool HasEarlyExit = CvtBBI->FalseBB != nullptr;
  1455. BranchProbability CvtNext, CvtFalse, BBNext, BBCvt;
  1456. if (HasEarlyExit) {
  1457. // Get probabilities before modifying CvtMBB and BBI.BB.
  1458. CvtNext = MBPI->getEdgeProbability(&CvtMBB, &NextMBB);
  1459. CvtFalse = MBPI->getEdgeProbability(&CvtMBB, CvtBBI->FalseBB);
  1460. BBNext = MBPI->getEdgeProbability(BBI.BB, &NextMBB);
  1461. BBCvt = MBPI->getEdgeProbability(BBI.BB, &CvtMBB);
  1462. }
  1463. if (CvtMBB.pred_size() > 1) {
  1464. BBI.NonPredSize -= TII->RemoveBranch(*BBI.BB);
  1465. // Copy instructions in the true block, predicate them, and add them to
  1466. // the entry block.
  1467. CopyAndPredicateBlock(BBI, *CvtBBI, Cond, true);
  1468. // RemoveExtraEdges won't work if the block has an unanalyzable branch, so
  1469. // explicitly remove CvtBBI as a successor.
  1470. BBI.BB->removeSuccessor(&CvtMBB, true);
  1471. } else {
  1472. // Predicate the 'true' block after removing its branch.
  1473. CvtBBI->NonPredSize -= TII->RemoveBranch(CvtMBB);
  1474. PredicateBlock(*CvtBBI, CvtMBB.end(), Cond);
  1475. // Now merge the entry of the triangle with the true block.
  1476. BBI.NonPredSize -= TII->RemoveBranch(*BBI.BB);
  1477. MergeBlocks(BBI, *CvtBBI, false);
  1478. }
  1479. // If 'true' block has a 'false' successor, add an exit branch to it.
  1480. if (HasEarlyExit) {
  1481. SmallVector<MachineOperand, 4> RevCond(CvtBBI->BrCond.begin(),
  1482. CvtBBI->BrCond.end());
  1483. if (TII->ReverseBranchCondition(RevCond))
  1484. llvm_unreachable("Unable to reverse branch condition!");
  1485. // Update the edge probability for both CvtBBI->FalseBB and NextBBI.
  1486. // NewNext = New_Prob(BBI.BB, NextMBB) =
  1487. // Prob(BBI.BB, NextMBB) +
  1488. // Prob(BBI.BB, CvtMBB) * Prob(CvtMBB, NextMBB)
  1489. // NewFalse = New_Prob(BBI.BB, CvtBBI->FalseBB) =
  1490. // Prob(BBI.BB, CvtMBB) * Prob(CvtMBB, CvtBBI->FalseBB)
  1491. auto NewTrueBB = getNextBlock(*BBI.BB);
  1492. auto NewNext = BBNext + BBCvt * CvtNext;
  1493. auto NewTrueBBIter = find(BBI.BB->successors(), NewTrueBB);
  1494. if (NewTrueBBIter != BBI.BB->succ_end())
  1495. BBI.BB->setSuccProbability(NewTrueBBIter, NewNext);
  1496. auto NewFalse = BBCvt * CvtFalse;
  1497. TII->insertBranch(*BBI.BB, CvtBBI->FalseBB, nullptr, RevCond, dl);
  1498. BBI.BB->addSuccessor(CvtBBI->FalseBB, NewFalse);
  1499. }
  1500. // Merge in the 'false' block if the 'false' block has no other
  1501. // predecessors. Otherwise, add an unconditional branch to 'false'.
  1502. bool FalseBBDead = false;
  1503. bool IterIfcvt = true;
  1504. bool isFallThrough = canFallThroughTo(*BBI.BB, NextMBB);
  1505. if (!isFallThrough) {
  1506. // Only merge them if the true block does not fallthrough to the false
  1507. // block. By not merging them, we make it possible to iteratively
  1508. // ifcvt the blocks.
  1509. if (!HasEarlyExit &&
  1510. NextMBB.pred_size() == 1 && !NextBBI->HasFallThrough &&
  1511. !NextMBB.hasAddressTaken()) {
  1512. MergeBlocks(BBI, *NextBBI);
  1513. FalseBBDead = true;
  1514. } else {
  1515. InsertUncondBranch(*BBI.BB, NextMBB, TII);
  1516. BBI.HasFallThrough = false;
  1517. }
  1518. // Mixed predicated and unpredicated code. This cannot be iteratively
  1519. // predicated.
  1520. IterIfcvt = false;
  1521. }
  1522. RemoveExtraEdges(BBI);
  1523. // Update block info. BB can be iteratively if-converted.
  1524. if (!IterIfcvt)
  1525. BBI.IsDone = true;
  1526. InvalidatePreds(*BBI.BB);
  1527. CvtBBI->IsDone = true;
  1528. if (FalseBBDead)
  1529. NextBBI->IsDone = true;
  1530. // FIXME: Must maintain LiveIns.
  1531. return true;
  1532. }
  1533. /// Common code shared between diamond conversions.
  1534. /// \p BBI, \p TrueBBI, and \p FalseBBI form the diamond shape.
  1535. /// \p NumDups1 - number of shared instructions at the beginning of \p TrueBBI
  1536. /// and FalseBBI
  1537. /// \p NumDups2 - number of shared instructions at the end of \p TrueBBI
  1538. /// and \p FalseBBI
  1539. /// \p RemoveBranch - Remove the common branch of the two blocks before
  1540. /// predicating. Only false for unanalyzable fallthrough
  1541. /// cases. The caller will replace the branch if necessary.
  1542. /// \p MergeAddEdges - Add successor edges when merging blocks. Only false for
  1543. /// unanalyzable fallthrough
  1544. bool IfConverter::IfConvertDiamondCommon(
  1545. BBInfo &BBI, BBInfo &TrueBBI, BBInfo &FalseBBI,
  1546. unsigned NumDups1, unsigned NumDups2,
  1547. bool TClobbersPred, bool FClobbersPred,
  1548. bool RemoveBranch, bool MergeAddEdges) {
  1549. if (TrueBBI.IsDone || FalseBBI.IsDone ||
  1550. TrueBBI.BB->pred_size() > 1 || FalseBBI.BB->pred_size() > 1) {
  1551. // Something has changed. It's no longer safe to predicate these blocks.
  1552. BBI.IsAnalyzed = false;
  1553. TrueBBI.IsAnalyzed = false;
  1554. FalseBBI.IsAnalyzed = false;
  1555. return false;
  1556. }
  1557. if (TrueBBI.BB->hasAddressTaken() || FalseBBI.BB->hasAddressTaken())
  1558. // Conservatively abort if-conversion if either BB has its address taken.
  1559. return false;
  1560. // Put the predicated instructions from the 'true' block before the
  1561. // instructions from the 'false' block, unless the true block would clobber
  1562. // the predicate, in which case, do the opposite.
  1563. BBInfo *BBI1 = &TrueBBI;
  1564. BBInfo *BBI2 = &FalseBBI;
  1565. SmallVector<MachineOperand, 4> RevCond(BBI.BrCond.begin(), BBI.BrCond.end());
  1566. if (TII->ReverseBranchCondition(RevCond))
  1567. llvm_unreachable("Unable to reverse branch condition!");
  1568. SmallVector<MachineOperand, 4> *Cond1 = &BBI.BrCond;
  1569. SmallVector<MachineOperand, 4> *Cond2 = &RevCond;
  1570. // Figure out the more profitable ordering.
  1571. bool DoSwap = false;
  1572. if (TClobbersPred && !FClobbersPred)
  1573. DoSwap = true;
  1574. else if (!TClobbersPred && !FClobbersPred) {
  1575. if (TrueBBI.NonPredSize > FalseBBI.NonPredSize)
  1576. DoSwap = true;
  1577. } else if (TClobbersPred && FClobbersPred)
  1578. llvm_unreachable("Predicate info cannot be clobbered by both sides.");
  1579. if (DoSwap) {
  1580. std::swap(BBI1, BBI2);
  1581. std::swap(Cond1, Cond2);
  1582. }
  1583. // Remove the conditional branch from entry to the blocks.
  1584. BBI.NonPredSize -= TII->RemoveBranch(*BBI.BB);
  1585. MachineBasicBlock &MBB1 = *BBI1->BB;
  1586. MachineBasicBlock &MBB2 = *BBI2->BB;
  1587. // Initialize the Redefs:
  1588. // - BB2 live-in regs need implicit uses before being redefined by BB1
  1589. // instructions.
  1590. // - BB1 live-out regs need implicit uses before being redefined by BB2
  1591. // instructions. We start with BB1 live-ins so we have the live-out regs
  1592. // after tracking the BB1 instructions.
  1593. Redefs.init(TRI);
  1594. Redefs.addLiveIns(MBB1);
  1595. Redefs.addLiveIns(MBB2);
  1596. // Remove the duplicated instructions at the beginnings of both paths.
  1597. // Skip dbg_value instructions
  1598. MachineBasicBlock::iterator DI1 = MBB1.getFirstNonDebugInstr();
  1599. MachineBasicBlock::iterator DI2 = MBB2.getFirstNonDebugInstr();
  1600. BBI1->NonPredSize -= NumDups1;
  1601. BBI2->NonPredSize -= NumDups1;
  1602. // Skip past the dups on each side separately since there may be
  1603. // differing dbg_value entries.
  1604. for (unsigned i = 0; i < NumDups1; ++DI1) {
  1605. if (!DI1->isDebugValue())
  1606. ++i;
  1607. }
  1608. while (NumDups1 != 0) {
  1609. ++DI2;
  1610. if (!DI2->isDebugValue())
  1611. --NumDups1;
  1612. }
  1613. // Compute a set of registers which must not be killed by instructions in BB1:
  1614. // This is everything used+live in BB2 after the duplicated instructions. We
  1615. // can compute this set by simulating liveness backwards from the end of BB2.
  1616. DontKill.init(TRI);
  1617. for (const MachineInstr &MI : make_range(MBB2.rbegin(), ++DI2.getReverse()))
  1618. DontKill.stepBackward(MI);
  1619. for (const MachineInstr &MI : make_range(MBB1.begin(), DI1)) {
  1620. SmallVector<std::pair<unsigned, const MachineOperand*>, 4> IgnoredClobbers;
  1621. Redefs.stepForward(MI, IgnoredClobbers);
  1622. }
  1623. BBI.BB->splice(BBI.BB->end(), &MBB1, MBB1.begin(), DI1);
  1624. MBB2.erase(MBB2.begin(), DI2);
  1625. // The branches have been checked to match, so it is safe to remove the branch
  1626. // in BB1 and rely on the copy in BB2
  1627. #ifndef NDEBUG
  1628. // Unanalyzable branches must match exactly. Check that now.
  1629. if (!BBI1->IsBrAnalyzable)
  1630. verifySameBranchInstructions(&MBB1, &MBB2);
  1631. #endif
  1632. BBI1->NonPredSize -= TII->RemoveBranch(*BBI1->BB);
  1633. // Remove duplicated instructions.
  1634. DI1 = MBB1.end();
  1635. for (unsigned i = 0; i != NumDups2; ) {
  1636. // NumDups2 only counted non-dbg_value instructions, so this won't
  1637. // run off the head of the list.
  1638. assert(DI1 != MBB1.begin());
  1639. --DI1;
  1640. // skip dbg_value instructions
  1641. if (!DI1->isDebugValue())
  1642. ++i;
  1643. }
  1644. MBB1.erase(DI1, MBB1.end());
  1645. // Kill flags in the true block for registers living into the false block
  1646. // must be removed.
  1647. RemoveKills(MBB1.begin(), MBB1.end(), DontKill, *TRI);
  1648. DI2 = BBI2->BB->end();
  1649. // The branches have been checked to match. Skip over the branch in the false
  1650. // block so that we don't try to predicate it.
  1651. if (RemoveBranch)
  1652. BBI2->NonPredSize -= TII->RemoveBranch(*BBI2->BB);
  1653. else {
  1654. do {
  1655. assert(DI2 != MBB2.begin());
  1656. DI2--;
  1657. } while (DI2->isBranch() || DI2->isDebugValue());
  1658. DI2++;
  1659. }
  1660. while (NumDups2 != 0) {
  1661. // NumDups2 only counted non-dbg_value instructions, so this won't
  1662. // run off the head of the list.
  1663. assert(DI2 != MBB2.begin());
  1664. --DI2;
  1665. // skip dbg_value instructions
  1666. if (!DI2->isDebugValue())
  1667. --NumDups2;
  1668. }
  1669. // Remember which registers would later be defined by the false block.
  1670. // This allows us not to predicate instructions in the true block that would
  1671. // later be re-defined. That is, rather than
  1672. // subeq r0, r1, #1
  1673. // addne r0, r1, #1
  1674. // generate:
  1675. // sub r0, r1, #1
  1676. // addne r0, r1, #1
  1677. SmallSet<unsigned, 4> RedefsByFalse;
  1678. SmallSet<unsigned, 4> ExtUses;
  1679. if (TII->isProfitableToUnpredicate(MBB1, MBB2)) {
  1680. for (const MachineInstr &FI : make_range(MBB2.begin(), DI2)) {
  1681. if (FI.isDebugValue())
  1682. continue;
  1683. SmallVector<unsigned, 4> Defs;
  1684. for (const MachineOperand &MO : FI.operands()) {
  1685. if (!MO.isReg())
  1686. continue;
  1687. unsigned Reg = MO.getReg();
  1688. if (!Reg)
  1689. continue;
  1690. if (MO.isDef()) {
  1691. Defs.push_back(Reg);
  1692. } else if (!RedefsByFalse.count(Reg)) {
  1693. // These are defined before ctrl flow reach the 'false' instructions.
  1694. // They cannot be modified by the 'true' instructions.
  1695. for (MCSubRegIterator SubRegs(Reg, TRI, /*IncludeSelf=*/true);
  1696. SubRegs.isValid(); ++SubRegs)
  1697. ExtUses.insert(*SubRegs);
  1698. }
  1699. }
  1700. for (unsigned Reg : Defs) {
  1701. if (!ExtUses.count(Reg)) {
  1702. for (MCSubRegIterator SubRegs(Reg, TRI, /*IncludeSelf=*/true);
  1703. SubRegs.isValid(); ++SubRegs)
  1704. RedefsByFalse.insert(*SubRegs);
  1705. }
  1706. }
  1707. }
  1708. }
  1709. // Predicate the 'true' block.
  1710. PredicateBlock(*BBI1, MBB1.end(), *Cond1, &RedefsByFalse);
  1711. // After predicating BBI1, if there is a predicated terminator in BBI1 and
  1712. // a non-predicated in BBI2, then we don't want to predicate the one from
  1713. // BBI2. The reason is that if we merged these blocks, we would end up with
  1714. // two predicated terminators in the same block.
  1715. if (!MBB2.empty() && (DI2 == MBB2.end())) {
  1716. MachineBasicBlock::iterator BBI1T = MBB1.getFirstTerminator();
  1717. MachineBasicBlock::iterator BBI2T = MBB2.getFirstTerminator();
  1718. if (BBI1T != MBB1.end() && TII->isPredicated(*BBI1T) &&
  1719. BBI2T != MBB2.end() && !TII->isPredicated(*BBI2T))
  1720. --DI2;
  1721. }
  1722. // Predicate the 'false' block.
  1723. PredicateBlock(*BBI2, DI2, *Cond2);
  1724. // Merge the true block into the entry of the diamond.
  1725. MergeBlocks(BBI, *BBI1, MergeAddEdges);
  1726. MergeBlocks(BBI, *BBI2, MergeAddEdges);
  1727. return true;
  1728. }
  1729. /// If convert an almost-diamond sub-CFG where the true
  1730. /// and false blocks share a common tail.
  1731. bool IfConverter::IfConvertForkedDiamond(
  1732. BBInfo &BBI, IfcvtKind Kind,
  1733. unsigned NumDups1, unsigned NumDups2,
  1734. bool TClobbersPred, bool FClobbersPred) {
  1735. BBInfo &TrueBBI = BBAnalysis[BBI.TrueBB->getNumber()];
  1736. BBInfo &FalseBBI = BBAnalysis[BBI.FalseBB->getNumber()];
  1737. // Save the debug location for later.
  1738. DebugLoc dl;
  1739. MachineBasicBlock::iterator TIE = TrueBBI.BB->getFirstTerminator();
  1740. if (TIE != TrueBBI.BB->end())
  1741. dl = TIE->getDebugLoc();
  1742. // Removing branches from both blocks is safe, because we have already
  1743. // determined that both blocks have the same branch instructions. The branch
  1744. // will be added back at the end, unpredicated.
  1745. if (!IfConvertDiamondCommon(
  1746. BBI, TrueBBI, FalseBBI,
  1747. NumDups1, NumDups2,
  1748. TClobbersPred, FClobbersPred,
  1749. /* RemoveBranch */ true, /* MergeAddEdges */ true))
  1750. return false;
  1751. // Add back the branch.
  1752. // Debug location saved above when removing the branch from BBI2
  1753. TII->insertBranch(*BBI.BB, TrueBBI.TrueBB, TrueBBI.FalseBB,
  1754. TrueBBI.BrCond, dl);
  1755. RemoveExtraEdges(BBI);
  1756. // Update block info.
  1757. BBI.IsDone = TrueBBI.IsDone = FalseBBI.IsDone = true;
  1758. InvalidatePreds(*BBI.BB);
  1759. // FIXME: Must maintain LiveIns.
  1760. return true;
  1761. }
  1762. /// If convert a diamond sub-CFG.
  1763. bool IfConverter::IfConvertDiamond(BBInfo &BBI, IfcvtKind Kind,
  1764. unsigned NumDups1, unsigned NumDups2,
  1765. bool TClobbersPred, bool FClobbersPred) {
  1766. BBInfo &TrueBBI = BBAnalysis[BBI.TrueBB->getNumber()];
  1767. BBInfo &FalseBBI = BBAnalysis[BBI.FalseBB->getNumber()];
  1768. MachineBasicBlock *TailBB = TrueBBI.TrueBB;
  1769. // True block must fall through or end with an unanalyzable terminator.
  1770. if (!TailBB) {
  1771. if (blockAlwaysFallThrough(TrueBBI))
  1772. TailBB = FalseBBI.TrueBB;
  1773. assert((TailBB || !TrueBBI.IsBrAnalyzable) && "Unexpected!");
  1774. }
  1775. if (!IfConvertDiamondCommon(
  1776. BBI, TrueBBI, FalseBBI,
  1777. NumDups1, NumDups2,
  1778. TClobbersPred, FClobbersPred,
  1779. /* RemoveBranch */ TrueBBI.IsBrAnalyzable,
  1780. /* MergeAddEdges */ TailBB == nullptr))
  1781. return false;
  1782. // If the if-converted block falls through or unconditionally branches into
  1783. // the tail block, and the tail block does not have other predecessors, then
  1784. // fold the tail block in as well. Otherwise, unless it falls through to the
  1785. // tail, add a unconditional branch to it.
  1786. if (TailBB) {
  1787. BBInfo &TailBBI = BBAnalysis[TailBB->getNumber()];
  1788. bool CanMergeTail = !TailBBI.HasFallThrough &&
  1789. !TailBBI.BB->hasAddressTaken();
  1790. // The if-converted block can still have a predicated terminator
  1791. // (e.g. a predicated return). If that is the case, we cannot merge
  1792. // it with the tail block.
  1793. MachineBasicBlock::const_iterator TI = BBI.BB->getFirstTerminator();
  1794. if (TI != BBI.BB->end() && TII->isPredicated(*TI))
  1795. CanMergeTail = false;
  1796. // There may still be a fall-through edge from BBI1 or BBI2 to TailBB;
  1797. // check if there are any other predecessors besides those.
  1798. unsigned NumPreds = TailBB->pred_size();
  1799. if (NumPreds > 1)
  1800. CanMergeTail = false;
  1801. else if (NumPreds == 1 && CanMergeTail) {
  1802. MachineBasicBlock::pred_iterator PI = TailBB->pred_begin();
  1803. if (*PI != TrueBBI.BB && *PI != FalseBBI.BB)
  1804. CanMergeTail = false;
  1805. }
  1806. if (CanMergeTail) {
  1807. MergeBlocks(BBI, TailBBI);
  1808. TailBBI.IsDone = true;
  1809. } else {
  1810. BBI.BB->addSuccessor(TailBB, BranchProbability::getOne());
  1811. InsertUncondBranch(*BBI.BB, *TailBB, TII);
  1812. BBI.HasFallThrough = false;
  1813. }
  1814. }
  1815. // RemoveExtraEdges won't work if the block has an unanalyzable branch,
  1816. // which can happen here if TailBB is unanalyzable and is merged, so
  1817. // explicitly remove BBI1 and BBI2 as successors.
  1818. BBI.BB->removeSuccessor(TrueBBI.BB);
  1819. BBI.BB->removeSuccessor(FalseBBI.BB, /* NormalizeSuccessProbs */ true);
  1820. RemoveExtraEdges(BBI);
  1821. // Update block info.
  1822. BBI.IsDone = TrueBBI.IsDone = FalseBBI.IsDone = true;
  1823. InvalidatePreds(*BBI.BB);
  1824. // FIXME: Must maintain LiveIns.
  1825. return true;
  1826. }
  1827. static bool MaySpeculate(const MachineInstr &MI,
  1828. SmallSet<unsigned, 4> &LaterRedefs) {
  1829. bool SawStore = true;
  1830. if (!MI.isSafeToMove(nullptr, SawStore))
  1831. return false;
  1832. for (const MachineOperand &MO : MI.operands()) {
  1833. if (!MO.isReg())
  1834. continue;
  1835. unsigned Reg = MO.getReg();
  1836. if (!Reg)
  1837. continue;
  1838. if (MO.isDef() && !LaterRedefs.count(Reg))
  1839. return false;
  1840. }
  1841. return true;
  1842. }
  1843. /// Predicate instructions from the start of the block to the specified end with
  1844. /// the specified condition.
  1845. void IfConverter::PredicateBlock(BBInfo &BBI,
  1846. MachineBasicBlock::iterator E,
  1847. SmallVectorImpl<MachineOperand> &Cond,
  1848. SmallSet<unsigned, 4> *LaterRedefs) {
  1849. bool AnyUnpred = false;
  1850. bool MaySpec = LaterRedefs != nullptr;
  1851. for (MachineInstr &I : make_range(BBI.BB->begin(), E)) {
  1852. if (I.isDebugValue() || TII->isPredicated(I))
  1853. continue;
  1854. // It may be possible not to predicate an instruction if it's the 'true'
  1855. // side of a diamond and the 'false' side may re-define the instruction's
  1856. // defs.
  1857. if (MaySpec && MaySpeculate(I, *LaterRedefs)) {
  1858. AnyUnpred = true;
  1859. continue;
  1860. }
  1861. // If any instruction is predicated, then every instruction after it must
  1862. // be predicated.
  1863. MaySpec = false;
  1864. if (!TII->PredicateInstruction(I, Cond)) {
  1865. #ifndef NDEBUG
  1866. dbgs() << "Unable to predicate " << I << "!\n";
  1867. #endif
  1868. llvm_unreachable(nullptr);
  1869. }
  1870. // If the predicated instruction now redefines a register as the result of
  1871. // if-conversion, add an implicit kill.
  1872. UpdatePredRedefs(I, Redefs);
  1873. }
  1874. BBI.Predicate.append(Cond.begin(), Cond.end());
  1875. BBI.IsAnalyzed = false;
  1876. BBI.NonPredSize = 0;
  1877. ++NumIfConvBBs;
  1878. if (AnyUnpred)
  1879. ++NumUnpred;
  1880. }
  1881. /// Copy and predicate instructions from source BB to the destination block.
  1882. /// Skip end of block branches if IgnoreBr is true.
  1883. void IfConverter::CopyAndPredicateBlock(BBInfo &ToBBI, BBInfo &FromBBI,
  1884. SmallVectorImpl<MachineOperand> &Cond,
  1885. bool IgnoreBr) {
  1886. MachineFunction &MF = *ToBBI.BB->getParent();
  1887. MachineBasicBlock &FromMBB = *FromBBI.BB;
  1888. for (MachineInstr &I : FromMBB) {
  1889. // Do not copy the end of the block branches.
  1890. if (IgnoreBr && I.isBranch())
  1891. break;
  1892. MachineInstr *MI = MF.CloneMachineInstr(&I);
  1893. ToBBI.BB->insert(ToBBI.BB->end(), MI);
  1894. ToBBI.NonPredSize++;
  1895. unsigned ExtraPredCost = TII->getPredicationCost(I);
  1896. unsigned NumCycles = SchedModel.computeInstrLatency(&I, false);
  1897. if (NumCycles > 1)
  1898. ToBBI.ExtraCost += NumCycles-1;
  1899. ToBBI.ExtraCost2 += ExtraPredCost;
  1900. if (!TII->isPredicated(I) && !MI->isDebugValue()) {
  1901. if (!TII->PredicateInstruction(*MI, Cond)) {
  1902. #ifndef NDEBUG
  1903. dbgs() << "Unable to predicate " << I << "!\n";
  1904. #endif
  1905. llvm_unreachable(nullptr);
  1906. }
  1907. }
  1908. // If the predicated instruction now redefines a register as the result of
  1909. // if-conversion, add an implicit kill.
  1910. UpdatePredRedefs(*MI, Redefs);
  1911. // Some kill flags may not be correct anymore.
  1912. if (!DontKill.empty())
  1913. RemoveKills(*MI, DontKill);
  1914. }
  1915. if (!IgnoreBr) {
  1916. std::vector<MachineBasicBlock *> Succs(FromMBB.succ_begin(),
  1917. FromMBB.succ_end());
  1918. MachineBasicBlock *NBB = getNextBlock(FromMBB);
  1919. MachineBasicBlock *FallThrough = FromBBI.HasFallThrough ? NBB : nullptr;
  1920. for (MachineBasicBlock *Succ : Succs) {
  1921. // Fallthrough edge can't be transferred.
  1922. if (Succ == FallThrough)
  1923. continue;
  1924. ToBBI.BB->addSuccessor(Succ);
  1925. }
  1926. }
  1927. ToBBI.Predicate.append(FromBBI.Predicate.begin(), FromBBI.Predicate.end());
  1928. ToBBI.Predicate.append(Cond.begin(), Cond.end());
  1929. ToBBI.ClobbersPred |= FromBBI.ClobbersPred;
  1930. ToBBI.IsAnalyzed = false;
  1931. ++NumDupBBs;
  1932. }
  1933. /// Move all instructions from FromBB to the end of ToBB. This will leave
  1934. /// FromBB as an empty block, so remove all of its successor edges except for
  1935. /// the fall-through edge. If AddEdges is true, i.e., when FromBBI's branch is
  1936. /// being moved, add those successor edges to ToBBI.
  1937. void IfConverter::MergeBlocks(BBInfo &ToBBI, BBInfo &FromBBI, bool AddEdges) {
  1938. MachineBasicBlock &FromMBB = *FromBBI.BB;
  1939. assert(!FromMBB.hasAddressTaken() &&
  1940. "Removing a BB whose address is taken!");
  1941. // In case FromMBB contains terminators (e.g. return instruction),
  1942. // first move the non-terminator instructions, then the terminators.
  1943. MachineBasicBlock::iterator FromTI = FromMBB.getFirstTerminator();
  1944. MachineBasicBlock::iterator ToTI = ToBBI.BB->getFirstTerminator();
  1945. ToBBI.BB->splice(ToTI, &FromMBB, FromMBB.begin(), FromTI);
  1946. // If FromBB has non-predicated terminator we should copy it at the end.
  1947. if (FromTI != FromMBB.end() && !TII->isPredicated(*FromTI))
  1948. ToTI = ToBBI.BB->end();
  1949. ToBBI.BB->splice(ToTI, &FromMBB, FromTI, FromMBB.end());
  1950. // Force normalizing the successors' probabilities of ToBBI.BB to convert all
  1951. // unknown probabilities into known ones.
  1952. // FIXME: This usage is too tricky and in the future we would like to
  1953. // eliminate all unknown probabilities in MBB.
  1954. ToBBI.BB->normalizeSuccProbs();
  1955. SmallVector<MachineBasicBlock *, 4> FromSuccs(FromMBB.succ_begin(),
  1956. FromMBB.succ_end());
  1957. MachineBasicBlock *NBB = getNextBlock(FromMBB);
  1958. MachineBasicBlock *FallThrough = FromBBI.HasFallThrough ? NBB : nullptr;
  1959. // The edge probability from ToBBI.BB to FromMBB, which is only needed when
  1960. // AddEdges is true and FromMBB is a successor of ToBBI.BB.
  1961. auto To2FromProb = BranchProbability::getZero();
  1962. if (AddEdges && ToBBI.BB->isSuccessor(&FromMBB)) {
  1963. To2FromProb = MBPI->getEdgeProbability(ToBBI.BB, &FromMBB);
  1964. // Set the edge probability from ToBBI.BB to FromMBB to zero to avoid the
  1965. // edge probability being merged to other edges when this edge is removed
  1966. // later.
  1967. ToBBI.BB->setSuccProbability(find(ToBBI.BB->successors(), &FromMBB),
  1968. BranchProbability::getZero());
  1969. }
  1970. for (MachineBasicBlock *Succ : FromSuccs) {
  1971. // Fallthrough edge can't be transferred.
  1972. if (Succ == FallThrough)
  1973. continue;
  1974. auto NewProb = BranchProbability::getZero();
  1975. if (AddEdges) {
  1976. // Calculate the edge probability for the edge from ToBBI.BB to Succ,
  1977. // which is a portion of the edge probability from FromMBB to Succ. The
  1978. // portion ratio is the edge probability from ToBBI.BB to FromMBB (if
  1979. // FromBBI is a successor of ToBBI.BB. See comment below for excepion).
  1980. NewProb = MBPI->getEdgeProbability(&FromMBB, Succ);
  1981. // To2FromProb is 0 when FromMBB is not a successor of ToBBI.BB. This
  1982. // only happens when if-converting a diamond CFG and FromMBB is the
  1983. // tail BB. In this case FromMBB post-dominates ToBBI.BB and hence we
  1984. // could just use the probabilities on FromMBB's out-edges when adding
  1985. // new successors.
  1986. if (!To2FromProb.isZero())
  1987. NewProb *= To2FromProb;
  1988. }
  1989. FromMBB.removeSuccessor(Succ);
  1990. if (AddEdges) {
  1991. // If the edge from ToBBI.BB to Succ already exists, update the
  1992. // probability of this edge by adding NewProb to it. An example is shown
  1993. // below, in which A is ToBBI.BB and B is FromMBB. In this case we
  1994. // don't have to set C as A's successor as it already is. We only need to
  1995. // update the edge probability on A->C. Note that B will not be
  1996. // immediately removed from A's successors. It is possible that B->D is
  1997. // not removed either if D is a fallthrough of B. Later the edge A->D
  1998. // (generated here) and B->D will be combined into one edge. To maintain
  1999. // correct edge probability of this combined edge, we need to set the edge
  2000. // probability of A->B to zero, which is already done above. The edge
  2001. // probability on A->D is calculated by scaling the original probability
  2002. // on A->B by the probability of B->D.
  2003. //
  2004. // Before ifcvt: After ifcvt (assume B->D is kept):
  2005. //
  2006. // A A
  2007. // /| /|\
  2008. // / B / B|
  2009. // | /| | ||
  2010. // |/ | | |/
  2011. // C D C D
  2012. //
  2013. if (ToBBI.BB->isSuccessor(Succ))
  2014. ToBBI.BB->setSuccProbability(
  2015. find(ToBBI.BB->successors(), Succ),
  2016. MBPI->getEdgeProbability(ToBBI.BB, Succ) + NewProb);
  2017. else
  2018. ToBBI.BB->addSuccessor(Succ, NewProb);
  2019. }
  2020. }
  2021. // Now FromBBI always falls through to the next block!
  2022. if (NBB && !FromMBB.isSuccessor(NBB))
  2023. FromMBB.addSuccessor(NBB);
  2024. // Normalize the probabilities of ToBBI.BB's successors with all adjustment
  2025. // we've done above.
  2026. ToBBI.BB->normalizeSuccProbs();
  2027. ToBBI.Predicate.append(FromBBI.Predicate.begin(), FromBBI.Predicate.end());
  2028. FromBBI.Predicate.clear();
  2029. ToBBI.NonPredSize += FromBBI.NonPredSize;
  2030. ToBBI.ExtraCost += FromBBI.ExtraCost;
  2031. ToBBI.ExtraCost2 += FromBBI.ExtraCost2;
  2032. FromBBI.NonPredSize = 0;
  2033. FromBBI.ExtraCost = 0;
  2034. FromBBI.ExtraCost2 = 0;
  2035. ToBBI.ClobbersPred |= FromBBI.ClobbersPred;
  2036. ToBBI.HasFallThrough = FromBBI.HasFallThrough;
  2037. ToBBI.IsAnalyzed = false;
  2038. FromBBI.IsAnalyzed = false;
  2039. }
  2040. FunctionPass *
  2041. llvm::createIfConverter(std::function<bool(const Function &)> Ftor) {
  2042. return new IfConverter(std::move(Ftor));
  2043. }