IfConversion.cpp 60 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677
  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.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #define DEBUG_TYPE "ifcvt"
  14. #include "llvm/CodeGen/Passes.h"
  15. #include "BranchFolding.h"
  16. #include "llvm/ADT/STLExtras.h"
  17. #include "llvm/ADT/SmallSet.h"
  18. #include "llvm/ADT/Statistic.h"
  19. #include "llvm/CodeGen/LivePhysRegs.h"
  20. #include "llvm/CodeGen/MachineBranchProbabilityInfo.h"
  21. #include "llvm/CodeGen/MachineFunctionPass.h"
  22. #include "llvm/CodeGen/MachineInstrBuilder.h"
  23. #include "llvm/CodeGen/MachineModuleInfo.h"
  24. #include "llvm/CodeGen/MachineRegisterInfo.h"
  25. #include "llvm/CodeGen/TargetSchedule.h"
  26. #include "llvm/MC/MCInstrItineraries.h"
  27. #include "llvm/Support/CommandLine.h"
  28. #include "llvm/Support/Debug.h"
  29. #include "llvm/Support/ErrorHandling.h"
  30. #include "llvm/Support/raw_ostream.h"
  31. #include "llvm/Target/TargetInstrInfo.h"
  32. #include "llvm/Target/TargetLowering.h"
  33. #include "llvm/Target/TargetMachine.h"
  34. #include "llvm/Target/TargetRegisterInfo.h"
  35. #include "llvm/Target/TargetSubtargetInfo.h"
  36. using namespace llvm;
  37. // Hidden options for help debugging.
  38. static cl::opt<int> IfCvtFnStart("ifcvt-fn-start", cl::init(-1), cl::Hidden);
  39. static cl::opt<int> IfCvtFnStop("ifcvt-fn-stop", cl::init(-1), cl::Hidden);
  40. static cl::opt<int> IfCvtLimit("ifcvt-limit", cl::init(-1), cl::Hidden);
  41. static cl::opt<bool> DisableSimple("disable-ifcvt-simple",
  42. cl::init(false), cl::Hidden);
  43. static cl::opt<bool> DisableSimpleF("disable-ifcvt-simple-false",
  44. cl::init(false), cl::Hidden);
  45. static cl::opt<bool> DisableTriangle("disable-ifcvt-triangle",
  46. cl::init(false), cl::Hidden);
  47. static cl::opt<bool> DisableTriangleR("disable-ifcvt-triangle-rev",
  48. cl::init(false), cl::Hidden);
  49. static cl::opt<bool> DisableTriangleF("disable-ifcvt-triangle-false",
  50. cl::init(false), cl::Hidden);
  51. static cl::opt<bool> DisableTriangleFR("disable-ifcvt-triangle-false-rev",
  52. cl::init(false), cl::Hidden);
  53. static cl::opt<bool> DisableDiamond("disable-ifcvt-diamond",
  54. cl::init(false), cl::Hidden);
  55. static cl::opt<bool> IfCvtBranchFold("ifcvt-branch-fold",
  56. cl::init(true), cl::Hidden);
  57. STATISTIC(NumSimple, "Number of simple if-conversions performed");
  58. STATISTIC(NumSimpleFalse, "Number of simple (F) if-conversions performed");
  59. STATISTIC(NumTriangle, "Number of triangle if-conversions performed");
  60. STATISTIC(NumTriangleRev, "Number of triangle (R) if-conversions performed");
  61. STATISTIC(NumTriangleFalse,"Number of triangle (F) if-conversions performed");
  62. STATISTIC(NumTriangleFRev, "Number of triangle (F/R) if-conversions performed");
  63. STATISTIC(NumDiamonds, "Number of diamond if-conversions performed");
  64. STATISTIC(NumIfConvBBs, "Number of if-converted blocks");
  65. STATISTIC(NumDupBBs, "Number of duplicated blocks");
  66. STATISTIC(NumUnpred, "Number of true blocks of diamonds unpredicated");
  67. namespace {
  68. class IfConverter : public MachineFunctionPass {
  69. enum IfcvtKind {
  70. ICNotClassfied, // BB data valid, but not classified.
  71. ICSimpleFalse, // Same as ICSimple, but on the false path.
  72. ICSimple, // BB is entry of an one split, no rejoin sub-CFG.
  73. ICTriangleFRev, // Same as ICTriangleFalse, but false path rev condition.
  74. ICTriangleRev, // Same as ICTriangle, but true path rev condition.
  75. ICTriangleFalse, // Same as ICTriangle, but on the false path.
  76. ICTriangle, // BB is entry of a triangle sub-CFG.
  77. ICDiamond // BB is entry of a diamond sub-CFG.
  78. };
  79. /// BBInfo - One per MachineBasicBlock, this is used to cache the result
  80. /// if-conversion feasibility analysis. This includes results from
  81. /// TargetInstrInfo::AnalyzeBranch() (i.e. TBB, FBB, and Cond), and its
  82. /// classification, and common tail block of its successors (if it's a
  83. /// diamond shape), its size, whether it's predicable, and whether any
  84. /// instruction can clobber the 'would-be' predicate.
  85. ///
  86. /// IsDone - True if BB is not to be considered for ifcvt.
  87. /// IsBeingAnalyzed - True if BB is currently being analyzed.
  88. /// IsAnalyzed - True if BB has been analyzed (info is still valid).
  89. /// IsEnqueued - True if BB has been enqueued to be ifcvt'ed.
  90. /// IsBrAnalyzable - True if AnalyzeBranch() returns false.
  91. /// HasFallThrough - True if BB may fallthrough to the following BB.
  92. /// IsUnpredicable - True if BB is known to be unpredicable.
  93. /// ClobbersPred - True if BB could modify predicates (e.g. has
  94. /// cmp, call, etc.)
  95. /// NonPredSize - Number of non-predicated instructions.
  96. /// ExtraCost - Extra cost for multi-cycle instructions.
  97. /// ExtraCost2 - Some instructions are slower when predicated
  98. /// BB - Corresponding MachineBasicBlock.
  99. /// TrueBB / FalseBB- See AnalyzeBranch().
  100. /// BrCond - Conditions for end of block conditional branches.
  101. /// Predicate - Predicate used in the BB.
  102. struct BBInfo {
  103. bool IsDone : 1;
  104. bool IsBeingAnalyzed : 1;
  105. bool IsAnalyzed : 1;
  106. bool IsEnqueued : 1;
  107. bool IsBrAnalyzable : 1;
  108. bool HasFallThrough : 1;
  109. bool IsUnpredicable : 1;
  110. bool CannotBeCopied : 1;
  111. bool ClobbersPred : 1;
  112. unsigned NonPredSize;
  113. unsigned ExtraCost;
  114. unsigned ExtraCost2;
  115. MachineBasicBlock *BB;
  116. MachineBasicBlock *TrueBB;
  117. MachineBasicBlock *FalseBB;
  118. SmallVector<MachineOperand, 4> BrCond;
  119. SmallVector<MachineOperand, 4> Predicate;
  120. BBInfo() : IsDone(false), IsBeingAnalyzed(false),
  121. IsAnalyzed(false), IsEnqueued(false), IsBrAnalyzable(false),
  122. HasFallThrough(false), IsUnpredicable(false),
  123. CannotBeCopied(false), ClobbersPred(false), NonPredSize(0),
  124. ExtraCost(0), ExtraCost2(0), BB(0), TrueBB(0), FalseBB(0) {}
  125. };
  126. /// IfcvtToken - Record information about pending if-conversions to attempt:
  127. /// BBI - Corresponding BBInfo.
  128. /// Kind - Type of block. See IfcvtKind.
  129. /// NeedSubsumption - True if the to-be-predicated BB has already been
  130. /// predicated.
  131. /// NumDups - Number of instructions that would be duplicated due
  132. /// to this if-conversion. (For diamonds, the number of
  133. /// identical instructions at the beginnings of both
  134. /// paths).
  135. /// NumDups2 - For diamonds, the number of identical instructions
  136. /// at the ends of both paths.
  137. struct IfcvtToken {
  138. BBInfo &BBI;
  139. IfcvtKind Kind;
  140. bool NeedSubsumption;
  141. unsigned NumDups;
  142. unsigned NumDups2;
  143. IfcvtToken(BBInfo &b, IfcvtKind k, bool s, unsigned d, unsigned d2 = 0)
  144. : BBI(b), Kind(k), NeedSubsumption(s), NumDups(d), NumDups2(d2) {}
  145. };
  146. /// BBAnalysis - Results of if-conversion feasibility analysis indexed by
  147. /// basic block number.
  148. std::vector<BBInfo> BBAnalysis;
  149. TargetSchedModel SchedModel;
  150. const TargetLoweringBase *TLI;
  151. const TargetInstrInfo *TII;
  152. const TargetRegisterInfo *TRI;
  153. const MachineBranchProbabilityInfo *MBPI;
  154. MachineRegisterInfo *MRI;
  155. LivePhysRegs Redefs;
  156. LivePhysRegs DontKill;
  157. bool PreRegAlloc;
  158. bool MadeChange;
  159. int FnNum;
  160. public:
  161. static char ID;
  162. IfConverter() : MachineFunctionPass(ID), FnNum(-1) {
  163. initializeIfConverterPass(*PassRegistry::getPassRegistry());
  164. }
  165. virtual void getAnalysisUsage(AnalysisUsage &AU) const {
  166. AU.addRequired<MachineBranchProbabilityInfo>();
  167. MachineFunctionPass::getAnalysisUsage(AU);
  168. }
  169. virtual bool runOnMachineFunction(MachineFunction &MF);
  170. private:
  171. bool ReverseBranchCondition(BBInfo &BBI);
  172. bool ValidSimple(BBInfo &TrueBBI, unsigned &Dups,
  173. const BranchProbability &Prediction) const;
  174. bool ValidTriangle(BBInfo &TrueBBI, BBInfo &FalseBBI,
  175. bool FalseBranch, unsigned &Dups,
  176. const BranchProbability &Prediction) const;
  177. bool ValidDiamond(BBInfo &TrueBBI, BBInfo &FalseBBI,
  178. unsigned &Dups1, unsigned &Dups2) const;
  179. void ScanInstructions(BBInfo &BBI);
  180. BBInfo &AnalyzeBlock(MachineBasicBlock *BB,
  181. std::vector<IfcvtToken*> &Tokens);
  182. bool FeasibilityAnalysis(BBInfo &BBI, SmallVectorImpl<MachineOperand> &Cond,
  183. bool isTriangle = false, bool RevBranch = false);
  184. void AnalyzeBlocks(MachineFunction &MF, std::vector<IfcvtToken*> &Tokens);
  185. void InvalidatePreds(MachineBasicBlock *BB);
  186. void RemoveExtraEdges(BBInfo &BBI);
  187. bool IfConvertSimple(BBInfo &BBI, IfcvtKind Kind);
  188. bool IfConvertTriangle(BBInfo &BBI, IfcvtKind Kind);
  189. bool IfConvertDiamond(BBInfo &BBI, IfcvtKind Kind,
  190. unsigned NumDups1, unsigned NumDups2);
  191. void PredicateBlock(BBInfo &BBI,
  192. MachineBasicBlock::iterator E,
  193. SmallVectorImpl<MachineOperand> &Cond,
  194. SmallSet<unsigned, 4> *LaterRedefs = 0);
  195. void CopyAndPredicateBlock(BBInfo &ToBBI, BBInfo &FromBBI,
  196. SmallVectorImpl<MachineOperand> &Cond,
  197. bool IgnoreBr = false);
  198. void MergeBlocks(BBInfo &ToBBI, BBInfo &FromBBI, bool AddEdges = true);
  199. bool MeetIfcvtSizeLimit(MachineBasicBlock &BB,
  200. unsigned Cycle, unsigned Extra,
  201. const BranchProbability &Prediction) const {
  202. return Cycle > 0 && TII->isProfitableToIfCvt(BB, Cycle, Extra,
  203. Prediction);
  204. }
  205. bool MeetIfcvtSizeLimit(MachineBasicBlock &TBB,
  206. unsigned TCycle, unsigned TExtra,
  207. MachineBasicBlock &FBB,
  208. unsigned FCycle, unsigned FExtra,
  209. const BranchProbability &Prediction) const {
  210. return TCycle > 0 && FCycle > 0 &&
  211. TII->isProfitableToIfCvt(TBB, TCycle, TExtra, FBB, FCycle, FExtra,
  212. Prediction);
  213. }
  214. // blockAlwaysFallThrough - Block ends without a terminator.
  215. bool blockAlwaysFallThrough(BBInfo &BBI) const {
  216. return BBI.IsBrAnalyzable && BBI.TrueBB == NULL;
  217. }
  218. // IfcvtTokenCmp - Used to sort if-conversion candidates.
  219. static bool IfcvtTokenCmp(IfcvtToken *C1, IfcvtToken *C2) {
  220. int Incr1 = (C1->Kind == ICDiamond)
  221. ? -(int)(C1->NumDups + C1->NumDups2) : (int)C1->NumDups;
  222. int Incr2 = (C2->Kind == ICDiamond)
  223. ? -(int)(C2->NumDups + C2->NumDups2) : (int)C2->NumDups;
  224. if (Incr1 > Incr2)
  225. return true;
  226. else if (Incr1 == Incr2) {
  227. // Favors subsumption.
  228. if (C1->NeedSubsumption == false && C2->NeedSubsumption == true)
  229. return true;
  230. else if (C1->NeedSubsumption == C2->NeedSubsumption) {
  231. // Favors diamond over triangle, etc.
  232. if ((unsigned)C1->Kind < (unsigned)C2->Kind)
  233. return true;
  234. else if (C1->Kind == C2->Kind)
  235. return C1->BBI.BB->getNumber() < C2->BBI.BB->getNumber();
  236. }
  237. }
  238. return false;
  239. }
  240. };
  241. char IfConverter::ID = 0;
  242. }
  243. char &llvm::IfConverterID = IfConverter::ID;
  244. INITIALIZE_PASS_BEGIN(IfConverter, "if-converter", "If Converter", false, false)
  245. INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfo)
  246. INITIALIZE_PASS_END(IfConverter, "if-converter", "If Converter", false, false)
  247. bool IfConverter::runOnMachineFunction(MachineFunction &MF) {
  248. TLI = MF.getTarget().getTargetLowering();
  249. TII = MF.getTarget().getInstrInfo();
  250. TRI = MF.getTarget().getRegisterInfo();
  251. MBPI = &getAnalysis<MachineBranchProbabilityInfo>();
  252. MRI = &MF.getRegInfo();
  253. const TargetSubtargetInfo &ST =
  254. MF.getTarget().getSubtarget<TargetSubtargetInfo>();
  255. SchedModel.init(*ST.getSchedModel(), &ST, TII);
  256. if (!TII) return false;
  257. PreRegAlloc = MRI->isSSA();
  258. bool BFChange = false;
  259. if (!PreRegAlloc) {
  260. // Tail merge tend to expose more if-conversion opportunities.
  261. BranchFolder BF(true, false);
  262. BFChange = BF.OptimizeFunction(MF, TII,
  263. MF.getTarget().getRegisterInfo(),
  264. getAnalysisIfAvailable<MachineModuleInfo>());
  265. }
  266. DEBUG(dbgs() << "\nIfcvt: function (" << ++FnNum << ") \'"
  267. << MF.getName() << "\'");
  268. if (FnNum < IfCvtFnStart || (IfCvtFnStop != -1 && FnNum > IfCvtFnStop)) {
  269. DEBUG(dbgs() << " skipped\n");
  270. return false;
  271. }
  272. DEBUG(dbgs() << "\n");
  273. MF.RenumberBlocks();
  274. BBAnalysis.resize(MF.getNumBlockIDs());
  275. std::vector<IfcvtToken*> Tokens;
  276. MadeChange = false;
  277. unsigned NumIfCvts = NumSimple + NumSimpleFalse + NumTriangle +
  278. NumTriangleRev + NumTriangleFalse + NumTriangleFRev + NumDiamonds;
  279. while (IfCvtLimit == -1 || (int)NumIfCvts < IfCvtLimit) {
  280. // Do an initial analysis for each basic block and find all the potential
  281. // candidates to perform if-conversion.
  282. bool Change = false;
  283. AnalyzeBlocks(MF, Tokens);
  284. while (!Tokens.empty()) {
  285. IfcvtToken *Token = Tokens.back();
  286. Tokens.pop_back();
  287. BBInfo &BBI = Token->BBI;
  288. IfcvtKind Kind = Token->Kind;
  289. unsigned NumDups = Token->NumDups;
  290. unsigned NumDups2 = Token->NumDups2;
  291. delete Token;
  292. // If the block has been evicted out of the queue or it has already been
  293. // marked dead (due to it being predicated), then skip it.
  294. if (BBI.IsDone)
  295. BBI.IsEnqueued = false;
  296. if (!BBI.IsEnqueued)
  297. continue;
  298. BBI.IsEnqueued = false;
  299. bool RetVal = false;
  300. switch (Kind) {
  301. default: llvm_unreachable("Unexpected!");
  302. case ICSimple:
  303. case ICSimpleFalse: {
  304. bool isFalse = Kind == ICSimpleFalse;
  305. if ((isFalse && DisableSimpleF) || (!isFalse && DisableSimple)) break;
  306. DEBUG(dbgs() << "Ifcvt (Simple" << (Kind == ICSimpleFalse ?
  307. " false" : "")
  308. << "): BB#" << BBI.BB->getNumber() << " ("
  309. << ((Kind == ICSimpleFalse)
  310. ? BBI.FalseBB->getNumber()
  311. : BBI.TrueBB->getNumber()) << ") ");
  312. RetVal = IfConvertSimple(BBI, Kind);
  313. DEBUG(dbgs() << (RetVal ? "succeeded!" : "failed!") << "\n");
  314. if (RetVal) {
  315. if (isFalse) ++NumSimpleFalse;
  316. else ++NumSimple;
  317. }
  318. break;
  319. }
  320. case ICTriangle:
  321. case ICTriangleRev:
  322. case ICTriangleFalse:
  323. case ICTriangleFRev: {
  324. bool isFalse = Kind == ICTriangleFalse;
  325. bool isRev = (Kind == ICTriangleRev || Kind == ICTriangleFRev);
  326. if (DisableTriangle && !isFalse && !isRev) break;
  327. if (DisableTriangleR && !isFalse && isRev) break;
  328. if (DisableTriangleF && isFalse && !isRev) break;
  329. if (DisableTriangleFR && isFalse && isRev) break;
  330. DEBUG(dbgs() << "Ifcvt (Triangle");
  331. if (isFalse)
  332. DEBUG(dbgs() << " false");
  333. if (isRev)
  334. DEBUG(dbgs() << " rev");
  335. DEBUG(dbgs() << "): BB#" << BBI.BB->getNumber() << " (T:"
  336. << BBI.TrueBB->getNumber() << ",F:"
  337. << BBI.FalseBB->getNumber() << ") ");
  338. RetVal = IfConvertTriangle(BBI, Kind);
  339. DEBUG(dbgs() << (RetVal ? "succeeded!" : "failed!") << "\n");
  340. if (RetVal) {
  341. if (isFalse) {
  342. if (isRev) ++NumTriangleFRev;
  343. else ++NumTriangleFalse;
  344. } else {
  345. if (isRev) ++NumTriangleRev;
  346. else ++NumTriangle;
  347. }
  348. }
  349. break;
  350. }
  351. case ICDiamond: {
  352. if (DisableDiamond) break;
  353. DEBUG(dbgs() << "Ifcvt (Diamond): BB#" << BBI.BB->getNumber() << " (T:"
  354. << BBI.TrueBB->getNumber() << ",F:"
  355. << BBI.FalseBB->getNumber() << ") ");
  356. RetVal = IfConvertDiamond(BBI, Kind, NumDups, NumDups2);
  357. DEBUG(dbgs() << (RetVal ? "succeeded!" : "failed!") << "\n");
  358. if (RetVal) ++NumDiamonds;
  359. break;
  360. }
  361. }
  362. Change |= RetVal;
  363. NumIfCvts = NumSimple + NumSimpleFalse + NumTriangle + NumTriangleRev +
  364. NumTriangleFalse + NumTriangleFRev + NumDiamonds;
  365. if (IfCvtLimit != -1 && (int)NumIfCvts >= IfCvtLimit)
  366. break;
  367. }
  368. if (!Change)
  369. break;
  370. MadeChange |= Change;
  371. }
  372. // Delete tokens in case of early exit.
  373. while (!Tokens.empty()) {
  374. IfcvtToken *Token = Tokens.back();
  375. Tokens.pop_back();
  376. delete Token;
  377. }
  378. Tokens.clear();
  379. BBAnalysis.clear();
  380. if (MadeChange && IfCvtBranchFold) {
  381. BranchFolder BF(false, false);
  382. BF.OptimizeFunction(MF, TII,
  383. MF.getTarget().getRegisterInfo(),
  384. getAnalysisIfAvailable<MachineModuleInfo>());
  385. }
  386. MadeChange |= BFChange;
  387. return MadeChange;
  388. }
  389. /// findFalseBlock - BB has a fallthrough. Find its 'false' successor given
  390. /// its 'true' successor.
  391. static MachineBasicBlock *findFalseBlock(MachineBasicBlock *BB,
  392. MachineBasicBlock *TrueBB) {
  393. for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(),
  394. E = BB->succ_end(); SI != E; ++SI) {
  395. MachineBasicBlock *SuccBB = *SI;
  396. if (SuccBB != TrueBB)
  397. return SuccBB;
  398. }
  399. return NULL;
  400. }
  401. /// ReverseBranchCondition - Reverse the condition of the end of the block
  402. /// branch. Swap block's 'true' and 'false' successors.
  403. bool IfConverter::ReverseBranchCondition(BBInfo &BBI) {
  404. DebugLoc dl; // FIXME: this is nowhere
  405. if (!TII->ReverseBranchCondition(BBI.BrCond)) {
  406. TII->RemoveBranch(*BBI.BB);
  407. TII->InsertBranch(*BBI.BB, BBI.FalseBB, BBI.TrueBB, BBI.BrCond, dl);
  408. std::swap(BBI.TrueBB, BBI.FalseBB);
  409. return true;
  410. }
  411. return false;
  412. }
  413. /// getNextBlock - Returns the next block in the function blocks ordering. If
  414. /// it is the end, returns NULL.
  415. static inline MachineBasicBlock *getNextBlock(MachineBasicBlock *BB) {
  416. MachineFunction::iterator I = BB;
  417. MachineFunction::iterator E = BB->getParent()->end();
  418. if (++I == E)
  419. return NULL;
  420. return I;
  421. }
  422. /// ValidSimple - Returns true if the 'true' block (along with its
  423. /// predecessor) forms a valid simple shape for ifcvt. It also returns the
  424. /// number of instructions that the ifcvt would need to duplicate if performed
  425. /// in Dups.
  426. bool IfConverter::ValidSimple(BBInfo &TrueBBI, unsigned &Dups,
  427. const BranchProbability &Prediction) const {
  428. Dups = 0;
  429. if (TrueBBI.IsBeingAnalyzed || TrueBBI.IsDone)
  430. return false;
  431. if (TrueBBI.IsBrAnalyzable)
  432. return false;
  433. if (TrueBBI.BB->pred_size() > 1) {
  434. if (TrueBBI.CannotBeCopied ||
  435. !TII->isProfitableToDupForIfCvt(*TrueBBI.BB, TrueBBI.NonPredSize,
  436. Prediction))
  437. return false;
  438. Dups = TrueBBI.NonPredSize;
  439. }
  440. return true;
  441. }
  442. /// ValidTriangle - Returns true if the 'true' and 'false' blocks (along
  443. /// with their common predecessor) forms a valid triangle shape for ifcvt.
  444. /// If 'FalseBranch' is true, it checks if 'true' block's false branch
  445. /// branches to the 'false' block rather than the other way around. It also
  446. /// returns the number of instructions that the ifcvt would need to duplicate
  447. /// if performed in 'Dups'.
  448. bool IfConverter::ValidTriangle(BBInfo &TrueBBI, BBInfo &FalseBBI,
  449. bool FalseBranch, unsigned &Dups,
  450. const BranchProbability &Prediction) const {
  451. Dups = 0;
  452. if (TrueBBI.IsBeingAnalyzed || TrueBBI.IsDone)
  453. return false;
  454. if (TrueBBI.BB->pred_size() > 1) {
  455. if (TrueBBI.CannotBeCopied)
  456. return false;
  457. unsigned Size = TrueBBI.NonPredSize;
  458. if (TrueBBI.IsBrAnalyzable) {
  459. if (TrueBBI.TrueBB && TrueBBI.BrCond.empty())
  460. // Ends with an unconditional branch. It will be removed.
  461. --Size;
  462. else {
  463. MachineBasicBlock *FExit = FalseBranch
  464. ? TrueBBI.TrueBB : TrueBBI.FalseBB;
  465. if (FExit)
  466. // Require a conditional branch
  467. ++Size;
  468. }
  469. }
  470. if (!TII->isProfitableToDupForIfCvt(*TrueBBI.BB, Size, Prediction))
  471. return false;
  472. Dups = Size;
  473. }
  474. MachineBasicBlock *TExit = FalseBranch ? TrueBBI.FalseBB : TrueBBI.TrueBB;
  475. if (!TExit && blockAlwaysFallThrough(TrueBBI)) {
  476. MachineFunction::iterator I = TrueBBI.BB;
  477. if (++I == TrueBBI.BB->getParent()->end())
  478. return false;
  479. TExit = I;
  480. }
  481. return TExit && TExit == FalseBBI.BB;
  482. }
  483. /// ValidDiamond - Returns true if the 'true' and 'false' blocks (along
  484. /// with their common predecessor) forms a valid diamond shape for ifcvt.
  485. bool IfConverter::ValidDiamond(BBInfo &TrueBBI, BBInfo &FalseBBI,
  486. unsigned &Dups1, unsigned &Dups2) const {
  487. Dups1 = Dups2 = 0;
  488. if (TrueBBI.IsBeingAnalyzed || TrueBBI.IsDone ||
  489. FalseBBI.IsBeingAnalyzed || FalseBBI.IsDone)
  490. return false;
  491. MachineBasicBlock *TT = TrueBBI.TrueBB;
  492. MachineBasicBlock *FT = FalseBBI.TrueBB;
  493. if (!TT && blockAlwaysFallThrough(TrueBBI))
  494. TT = getNextBlock(TrueBBI.BB);
  495. if (!FT && blockAlwaysFallThrough(FalseBBI))
  496. FT = getNextBlock(FalseBBI.BB);
  497. if (TT != FT)
  498. return false;
  499. if (TT == NULL && (TrueBBI.IsBrAnalyzable || FalseBBI.IsBrAnalyzable))
  500. return false;
  501. if (TrueBBI.BB->pred_size() > 1 || FalseBBI.BB->pred_size() > 1)
  502. return false;
  503. // FIXME: Allow true block to have an early exit?
  504. if (TrueBBI.FalseBB || FalseBBI.FalseBB ||
  505. (TrueBBI.ClobbersPred && FalseBBI.ClobbersPred))
  506. return false;
  507. // Count duplicate instructions at the beginning of the true and false blocks.
  508. MachineBasicBlock::iterator TIB = TrueBBI.BB->begin();
  509. MachineBasicBlock::iterator FIB = FalseBBI.BB->begin();
  510. MachineBasicBlock::iterator TIE = TrueBBI.BB->end();
  511. MachineBasicBlock::iterator FIE = FalseBBI.BB->end();
  512. while (TIB != TIE && FIB != FIE) {
  513. // Skip dbg_value instructions. These do not count.
  514. if (TIB->isDebugValue()) {
  515. while (TIB != TIE && TIB->isDebugValue())
  516. ++TIB;
  517. if (TIB == TIE)
  518. break;
  519. }
  520. if (FIB->isDebugValue()) {
  521. while (FIB != FIE && FIB->isDebugValue())
  522. ++FIB;
  523. if (FIB == FIE)
  524. break;
  525. }
  526. if (!TIB->isIdenticalTo(FIB))
  527. break;
  528. ++Dups1;
  529. ++TIB;
  530. ++FIB;
  531. }
  532. // Now, in preparation for counting duplicate instructions at the ends of the
  533. // blocks, move the end iterators up past any branch instructions.
  534. while (TIE != TIB) {
  535. --TIE;
  536. if (!TIE->isBranch())
  537. break;
  538. }
  539. while (FIE != FIB) {
  540. --FIE;
  541. if (!FIE->isBranch())
  542. break;
  543. }
  544. // If Dups1 includes all of a block, then don't count duplicate
  545. // instructions at the end of the blocks.
  546. if (TIB == TIE || FIB == FIE)
  547. return true;
  548. // Count duplicate instructions at the ends of the blocks.
  549. while (TIE != TIB && FIE != FIB) {
  550. // Skip dbg_value instructions. These do not count.
  551. if (TIE->isDebugValue()) {
  552. while (TIE != TIB && TIE->isDebugValue())
  553. --TIE;
  554. if (TIE == TIB)
  555. break;
  556. }
  557. if (FIE->isDebugValue()) {
  558. while (FIE != FIB && FIE->isDebugValue())
  559. --FIE;
  560. if (FIE == FIB)
  561. break;
  562. }
  563. if (!TIE->isIdenticalTo(FIE))
  564. break;
  565. ++Dups2;
  566. --TIE;
  567. --FIE;
  568. }
  569. return true;
  570. }
  571. /// ScanInstructions - Scan all the instructions in the block to determine if
  572. /// the block is predicable. In most cases, that means all the instructions
  573. /// in the block are isPredicable(). Also checks if the block contains any
  574. /// instruction which can clobber a predicate (e.g. condition code register).
  575. /// If so, the block is not predicable unless it's the last instruction.
  576. void IfConverter::ScanInstructions(BBInfo &BBI) {
  577. if (BBI.IsDone)
  578. return;
  579. bool AlreadyPredicated = !BBI.Predicate.empty();
  580. // First analyze the end of BB branches.
  581. BBI.TrueBB = BBI.FalseBB = NULL;
  582. BBI.BrCond.clear();
  583. BBI.IsBrAnalyzable =
  584. !TII->AnalyzeBranch(*BBI.BB, BBI.TrueBB, BBI.FalseBB, BBI.BrCond);
  585. BBI.HasFallThrough = BBI.IsBrAnalyzable && BBI.FalseBB == NULL;
  586. if (BBI.BrCond.size()) {
  587. // No false branch. This BB must end with a conditional branch and a
  588. // fallthrough.
  589. if (!BBI.FalseBB)
  590. BBI.FalseBB = findFalseBlock(BBI.BB, BBI.TrueBB);
  591. if (!BBI.FalseBB) {
  592. // Malformed bcc? True and false blocks are the same?
  593. BBI.IsUnpredicable = true;
  594. return;
  595. }
  596. }
  597. // Then scan all the instructions.
  598. BBI.NonPredSize = 0;
  599. BBI.ExtraCost = 0;
  600. BBI.ExtraCost2 = 0;
  601. BBI.ClobbersPred = false;
  602. for (MachineBasicBlock::iterator I = BBI.BB->begin(), E = BBI.BB->end();
  603. I != E; ++I) {
  604. if (I->isDebugValue())
  605. continue;
  606. if (I->isNotDuplicable())
  607. BBI.CannotBeCopied = true;
  608. bool isPredicated = TII->isPredicated(I);
  609. bool isCondBr = BBI.IsBrAnalyzable && I->isConditionalBranch();
  610. // A conditional branch is not predicable, but it may be eliminated.
  611. if (isCondBr)
  612. continue;
  613. if (!isPredicated) {
  614. BBI.NonPredSize++;
  615. unsigned ExtraPredCost = TII->getPredicationCost(&*I);
  616. unsigned NumCycles = SchedModel.computeInstrLatency(&*I, false);
  617. if (NumCycles > 1)
  618. BBI.ExtraCost += NumCycles-1;
  619. BBI.ExtraCost2 += ExtraPredCost;
  620. } else if (!AlreadyPredicated) {
  621. // FIXME: This instruction is already predicated before the
  622. // if-conversion pass. It's probably something like a conditional move.
  623. // Mark this block unpredicable for now.
  624. BBI.IsUnpredicable = true;
  625. return;
  626. }
  627. if (BBI.ClobbersPred && !isPredicated) {
  628. // Predicate modification instruction should end the block (except for
  629. // already predicated instructions and end of block branches).
  630. // Predicate may have been modified, the subsequent (currently)
  631. // unpredicated instructions cannot be correctly predicated.
  632. BBI.IsUnpredicable = true;
  633. return;
  634. }
  635. // FIXME: Make use of PredDefs? e.g. ADDC, SUBC sets predicates but are
  636. // still potentially predicable.
  637. std::vector<MachineOperand> PredDefs;
  638. if (TII->DefinesPredicate(I, PredDefs))
  639. BBI.ClobbersPred = true;
  640. if (!TII->isPredicable(I)) {
  641. BBI.IsUnpredicable = true;
  642. return;
  643. }
  644. }
  645. }
  646. /// FeasibilityAnalysis - Determine if the block is a suitable candidate to be
  647. /// predicated by the specified predicate.
  648. bool IfConverter::FeasibilityAnalysis(BBInfo &BBI,
  649. SmallVectorImpl<MachineOperand> &Pred,
  650. bool isTriangle, bool RevBranch) {
  651. // If the block is dead or unpredicable, then it cannot be predicated.
  652. if (BBI.IsDone || BBI.IsUnpredicable)
  653. return false;
  654. // If it is already predicated, check if the new predicate subsumes
  655. // its predicate.
  656. if (BBI.Predicate.size() && !TII->SubsumesPredicate(Pred, BBI.Predicate))
  657. return false;
  658. if (BBI.BrCond.size()) {
  659. if (!isTriangle)
  660. return false;
  661. // Test predicate subsumption.
  662. SmallVector<MachineOperand, 4> RevPred(Pred.begin(), Pred.end());
  663. SmallVector<MachineOperand, 4> Cond(BBI.BrCond.begin(), BBI.BrCond.end());
  664. if (RevBranch) {
  665. if (TII->ReverseBranchCondition(Cond))
  666. return false;
  667. }
  668. if (TII->ReverseBranchCondition(RevPred) ||
  669. !TII->SubsumesPredicate(Cond, RevPred))
  670. return false;
  671. }
  672. return true;
  673. }
  674. /// AnalyzeBlock - Analyze the structure of the sub-CFG starting from
  675. /// the specified block. Record its successors and whether it looks like an
  676. /// if-conversion candidate.
  677. IfConverter::BBInfo &IfConverter::AnalyzeBlock(MachineBasicBlock *BB,
  678. std::vector<IfcvtToken*> &Tokens) {
  679. BBInfo &BBI = BBAnalysis[BB->getNumber()];
  680. if (BBI.IsAnalyzed || BBI.IsBeingAnalyzed)
  681. return BBI;
  682. BBI.BB = BB;
  683. BBI.IsBeingAnalyzed = true;
  684. ScanInstructions(BBI);
  685. // Unanalyzable or ends with fallthrough or unconditional branch, or if is not
  686. // considered for ifcvt anymore.
  687. if (!BBI.IsBrAnalyzable || BBI.BrCond.empty() || BBI.IsDone) {
  688. BBI.IsBeingAnalyzed = false;
  689. BBI.IsAnalyzed = true;
  690. return BBI;
  691. }
  692. // Do not ifcvt if either path is a back edge to the entry block.
  693. if (BBI.TrueBB == BB || BBI.FalseBB == BB) {
  694. BBI.IsBeingAnalyzed = false;
  695. BBI.IsAnalyzed = true;
  696. return BBI;
  697. }
  698. // Do not ifcvt if true and false fallthrough blocks are the same.
  699. if (!BBI.FalseBB) {
  700. BBI.IsBeingAnalyzed = false;
  701. BBI.IsAnalyzed = true;
  702. return BBI;
  703. }
  704. BBInfo &TrueBBI = AnalyzeBlock(BBI.TrueBB, Tokens);
  705. BBInfo &FalseBBI = AnalyzeBlock(BBI.FalseBB, Tokens);
  706. if (TrueBBI.IsDone && FalseBBI.IsDone) {
  707. BBI.IsBeingAnalyzed = false;
  708. BBI.IsAnalyzed = true;
  709. return BBI;
  710. }
  711. SmallVector<MachineOperand, 4> RevCond(BBI.BrCond.begin(), BBI.BrCond.end());
  712. bool CanRevCond = !TII->ReverseBranchCondition(RevCond);
  713. unsigned Dups = 0;
  714. unsigned Dups2 = 0;
  715. bool TNeedSub = !TrueBBI.Predicate.empty();
  716. bool FNeedSub = !FalseBBI.Predicate.empty();
  717. bool Enqueued = false;
  718. BranchProbability Prediction = MBPI->getEdgeProbability(BB, TrueBBI.BB);
  719. if (CanRevCond && ValidDiamond(TrueBBI, FalseBBI, Dups, Dups2) &&
  720. MeetIfcvtSizeLimit(*TrueBBI.BB, (TrueBBI.NonPredSize - (Dups + Dups2) +
  721. TrueBBI.ExtraCost), TrueBBI.ExtraCost2,
  722. *FalseBBI.BB, (FalseBBI.NonPredSize - (Dups + Dups2) +
  723. FalseBBI.ExtraCost),FalseBBI.ExtraCost2,
  724. Prediction) &&
  725. FeasibilityAnalysis(TrueBBI, BBI.BrCond) &&
  726. FeasibilityAnalysis(FalseBBI, RevCond)) {
  727. // Diamond:
  728. // EBB
  729. // / \_
  730. // | |
  731. // TBB FBB
  732. // \ /
  733. // TailBB
  734. // Note TailBB can be empty.
  735. Tokens.push_back(new IfcvtToken(BBI, ICDiamond, TNeedSub|FNeedSub, Dups,
  736. Dups2));
  737. Enqueued = true;
  738. }
  739. if (ValidTriangle(TrueBBI, FalseBBI, false, Dups, Prediction) &&
  740. MeetIfcvtSizeLimit(*TrueBBI.BB, TrueBBI.NonPredSize + TrueBBI.ExtraCost,
  741. TrueBBI.ExtraCost2, Prediction) &&
  742. FeasibilityAnalysis(TrueBBI, BBI.BrCond, true)) {
  743. // Triangle:
  744. // EBB
  745. // | \_
  746. // | |
  747. // | TBB
  748. // | /
  749. // FBB
  750. Tokens.push_back(new IfcvtToken(BBI, ICTriangle, TNeedSub, Dups));
  751. Enqueued = true;
  752. }
  753. if (ValidTriangle(TrueBBI, FalseBBI, true, Dups, Prediction) &&
  754. MeetIfcvtSizeLimit(*TrueBBI.BB, TrueBBI.NonPredSize + TrueBBI.ExtraCost,
  755. TrueBBI.ExtraCost2, Prediction) &&
  756. FeasibilityAnalysis(TrueBBI, BBI.BrCond, true, true)) {
  757. Tokens.push_back(new IfcvtToken(BBI, ICTriangleRev, TNeedSub, Dups));
  758. Enqueued = true;
  759. }
  760. if (ValidSimple(TrueBBI, Dups, Prediction) &&
  761. MeetIfcvtSizeLimit(*TrueBBI.BB, TrueBBI.NonPredSize + TrueBBI.ExtraCost,
  762. TrueBBI.ExtraCost2, Prediction) &&
  763. FeasibilityAnalysis(TrueBBI, BBI.BrCond)) {
  764. // Simple (split, no rejoin):
  765. // EBB
  766. // | \_
  767. // | |
  768. // | TBB---> exit
  769. // |
  770. // FBB
  771. Tokens.push_back(new IfcvtToken(BBI, ICSimple, TNeedSub, Dups));
  772. Enqueued = true;
  773. }
  774. if (CanRevCond) {
  775. // Try the other path...
  776. if (ValidTriangle(FalseBBI, TrueBBI, false, Dups,
  777. Prediction.getCompl()) &&
  778. MeetIfcvtSizeLimit(*FalseBBI.BB,
  779. FalseBBI.NonPredSize + FalseBBI.ExtraCost,
  780. FalseBBI.ExtraCost2, Prediction.getCompl()) &&
  781. FeasibilityAnalysis(FalseBBI, RevCond, true)) {
  782. Tokens.push_back(new IfcvtToken(BBI, ICTriangleFalse, FNeedSub, Dups));
  783. Enqueued = true;
  784. }
  785. if (ValidTriangle(FalseBBI, TrueBBI, true, Dups,
  786. Prediction.getCompl()) &&
  787. MeetIfcvtSizeLimit(*FalseBBI.BB,
  788. FalseBBI.NonPredSize + FalseBBI.ExtraCost,
  789. FalseBBI.ExtraCost2, Prediction.getCompl()) &&
  790. FeasibilityAnalysis(FalseBBI, RevCond, true, true)) {
  791. Tokens.push_back(new IfcvtToken(BBI, ICTriangleFRev, FNeedSub, Dups));
  792. Enqueued = true;
  793. }
  794. if (ValidSimple(FalseBBI, Dups, Prediction.getCompl()) &&
  795. MeetIfcvtSizeLimit(*FalseBBI.BB,
  796. FalseBBI.NonPredSize + FalseBBI.ExtraCost,
  797. FalseBBI.ExtraCost2, Prediction.getCompl()) &&
  798. FeasibilityAnalysis(FalseBBI, RevCond)) {
  799. Tokens.push_back(new IfcvtToken(BBI, ICSimpleFalse, FNeedSub, Dups));
  800. Enqueued = true;
  801. }
  802. }
  803. BBI.IsEnqueued = Enqueued;
  804. BBI.IsBeingAnalyzed = false;
  805. BBI.IsAnalyzed = true;
  806. return BBI;
  807. }
  808. /// AnalyzeBlocks - Analyze all blocks and find entries for all if-conversion
  809. /// candidates.
  810. void IfConverter::AnalyzeBlocks(MachineFunction &MF,
  811. std::vector<IfcvtToken*> &Tokens) {
  812. for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I) {
  813. MachineBasicBlock *BB = I;
  814. AnalyzeBlock(BB, Tokens);
  815. }
  816. // Sort to favor more complex ifcvt scheme.
  817. std::stable_sort(Tokens.begin(), Tokens.end(), IfcvtTokenCmp);
  818. }
  819. /// canFallThroughTo - Returns true either if ToBB is the next block after BB or
  820. /// that all the intervening blocks are empty (given BB can fall through to its
  821. /// next block).
  822. static bool canFallThroughTo(MachineBasicBlock *BB, MachineBasicBlock *ToBB) {
  823. MachineFunction::iterator PI = BB;
  824. MachineFunction::iterator I = std::next(PI);
  825. MachineFunction::iterator TI = ToBB;
  826. MachineFunction::iterator E = BB->getParent()->end();
  827. while (I != TI) {
  828. // Check isSuccessor to avoid case where the next block is empty, but
  829. // it's not a successor.
  830. if (I == E || !I->empty() || !PI->isSuccessor(I))
  831. return false;
  832. PI = I++;
  833. }
  834. return true;
  835. }
  836. /// InvalidatePreds - Invalidate predecessor BB info so it would be re-analyzed
  837. /// to determine if it can be if-converted. If predecessor is already enqueued,
  838. /// dequeue it!
  839. void IfConverter::InvalidatePreds(MachineBasicBlock *BB) {
  840. for (MachineBasicBlock::pred_iterator PI = BB->pred_begin(),
  841. E = BB->pred_end(); PI != E; ++PI) {
  842. BBInfo &PBBI = BBAnalysis[(*PI)->getNumber()];
  843. if (PBBI.IsDone || PBBI.BB == BB)
  844. continue;
  845. PBBI.IsAnalyzed = false;
  846. PBBI.IsEnqueued = false;
  847. }
  848. }
  849. /// InsertUncondBranch - Inserts an unconditional branch from BB to ToBB.
  850. ///
  851. static void InsertUncondBranch(MachineBasicBlock *BB, MachineBasicBlock *ToBB,
  852. const TargetInstrInfo *TII) {
  853. DebugLoc dl; // FIXME: this is nowhere
  854. SmallVector<MachineOperand, 0> NoCond;
  855. TII->InsertBranch(*BB, ToBB, NULL, NoCond, dl);
  856. }
  857. /// RemoveExtraEdges - Remove true / false edges if either / both are no longer
  858. /// successors.
  859. void IfConverter::RemoveExtraEdges(BBInfo &BBI) {
  860. MachineBasicBlock *TBB = NULL, *FBB = NULL;
  861. SmallVector<MachineOperand, 4> Cond;
  862. if (!TII->AnalyzeBranch(*BBI.BB, TBB, FBB, Cond))
  863. BBI.BB->CorrectExtraCFGEdges(TBB, FBB, !Cond.empty());
  864. }
  865. /// Behaves like LiveRegUnits::StepForward() but also adds implicit uses to all
  866. /// values defined in MI which are not live/used by MI.
  867. static void UpdatePredRedefs(MachineInstr *MI, LivePhysRegs &Redefs) {
  868. for (ConstMIBundleOperands Ops(MI); Ops.isValid(); ++Ops) {
  869. if (!Ops->isReg() || !Ops->isKill())
  870. continue;
  871. unsigned Reg = Ops->getReg();
  872. if (Reg == 0)
  873. continue;
  874. Redefs.removeReg(Reg);
  875. }
  876. for (MIBundleOperands Ops(MI); Ops.isValid(); ++Ops) {
  877. if (!Ops->isReg() || !Ops->isDef())
  878. continue;
  879. unsigned Reg = Ops->getReg();
  880. if (Reg == 0 || Redefs.contains(Reg))
  881. continue;
  882. Redefs.addReg(Reg);
  883. MachineOperand &Op = *Ops;
  884. MachineInstr *MI = Op.getParent();
  885. MachineInstrBuilder MIB(*MI->getParent()->getParent(), MI);
  886. MIB.addReg(Reg, RegState::Implicit | RegState::Undef);
  887. }
  888. }
  889. /**
  890. * Remove kill flags from operands with a registers in the @p DontKill set.
  891. */
  892. static void RemoveKills(MachineInstr &MI, const LivePhysRegs &DontKill) {
  893. for (MIBundleOperands O(&MI); O.isValid(); ++O) {
  894. if (!O->isReg() || !O->isKill())
  895. continue;
  896. if (DontKill.contains(O->getReg()))
  897. O->setIsKill(false);
  898. }
  899. }
  900. /**
  901. * Walks a range of machine instructions and removes kill flags for registers
  902. * in the @p DontKill set.
  903. */
  904. static void RemoveKills(MachineBasicBlock::iterator I,
  905. MachineBasicBlock::iterator E,
  906. const LivePhysRegs &DontKill,
  907. const MCRegisterInfo &MCRI) {
  908. for ( ; I != E; ++I)
  909. RemoveKills(*I, DontKill);
  910. }
  911. /// IfConvertSimple - If convert a simple (split, no rejoin) sub-CFG.
  912. ///
  913. bool IfConverter::IfConvertSimple(BBInfo &BBI, IfcvtKind Kind) {
  914. BBInfo &TrueBBI = BBAnalysis[BBI.TrueBB->getNumber()];
  915. BBInfo &FalseBBI = BBAnalysis[BBI.FalseBB->getNumber()];
  916. BBInfo *CvtBBI = &TrueBBI;
  917. BBInfo *NextBBI = &FalseBBI;
  918. SmallVector<MachineOperand, 4> Cond(BBI.BrCond.begin(), BBI.BrCond.end());
  919. if (Kind == ICSimpleFalse)
  920. std::swap(CvtBBI, NextBBI);
  921. if (CvtBBI->IsDone ||
  922. (CvtBBI->CannotBeCopied && CvtBBI->BB->pred_size() > 1)) {
  923. // Something has changed. It's no longer safe to predicate this block.
  924. BBI.IsAnalyzed = false;
  925. CvtBBI->IsAnalyzed = false;
  926. return false;
  927. }
  928. if (CvtBBI->BB->hasAddressTaken())
  929. // Conservatively abort if-conversion if BB's address is taken.
  930. return false;
  931. if (Kind == ICSimpleFalse)
  932. if (TII->ReverseBranchCondition(Cond))
  933. llvm_unreachable("Unable to reverse branch condition!");
  934. // Initialize liveins to the first BB. These are potentiall redefined by
  935. // predicated instructions.
  936. Redefs.init(TRI);
  937. Redefs.addLiveIns(CvtBBI->BB);
  938. Redefs.addLiveIns(NextBBI->BB);
  939. // Compute a set of registers which must not be killed by instructions in
  940. // BB1: This is everything live-in to BB2.
  941. DontKill.init(TRI);
  942. DontKill.addLiveIns(NextBBI->BB);
  943. if (CvtBBI->BB->pred_size() > 1) {
  944. BBI.NonPredSize -= TII->RemoveBranch(*BBI.BB);
  945. // Copy instructions in the true block, predicate them, and add them to
  946. // the entry block.
  947. CopyAndPredicateBlock(BBI, *CvtBBI, Cond);
  948. // RemoveExtraEdges won't work if the block has an unanalyzable branch, so
  949. // explicitly remove CvtBBI as a successor.
  950. BBI.BB->removeSuccessor(CvtBBI->BB);
  951. } else {
  952. RemoveKills(CvtBBI->BB->begin(), CvtBBI->BB->end(), DontKill, *TRI);
  953. PredicateBlock(*CvtBBI, CvtBBI->BB->end(), Cond);
  954. // Merge converted block into entry block.
  955. BBI.NonPredSize -= TII->RemoveBranch(*BBI.BB);
  956. MergeBlocks(BBI, *CvtBBI);
  957. }
  958. bool IterIfcvt = true;
  959. if (!canFallThroughTo(BBI.BB, NextBBI->BB)) {
  960. InsertUncondBranch(BBI.BB, NextBBI->BB, TII);
  961. BBI.HasFallThrough = false;
  962. // Now ifcvt'd block will look like this:
  963. // BB:
  964. // ...
  965. // t, f = cmp
  966. // if t op
  967. // b BBf
  968. //
  969. // We cannot further ifcvt this block because the unconditional branch
  970. // will have to be predicated on the new condition, that will not be
  971. // available if cmp executes.
  972. IterIfcvt = false;
  973. }
  974. RemoveExtraEdges(BBI);
  975. // Update block info. BB can be iteratively if-converted.
  976. if (!IterIfcvt)
  977. BBI.IsDone = true;
  978. InvalidatePreds(BBI.BB);
  979. CvtBBI->IsDone = true;
  980. // FIXME: Must maintain LiveIns.
  981. return true;
  982. }
  983. /// Scale down weights to fit into uint32_t. NewTrue is the new weight
  984. /// for successor TrueBB, and NewFalse is the new weight for successor
  985. /// FalseBB.
  986. static void ScaleWeights(uint64_t NewTrue, uint64_t NewFalse,
  987. MachineBasicBlock *MBB,
  988. const MachineBasicBlock *TrueBB,
  989. const MachineBasicBlock *FalseBB,
  990. const MachineBranchProbabilityInfo *MBPI) {
  991. uint64_t NewMax = (NewTrue > NewFalse) ? NewTrue : NewFalse;
  992. uint32_t Scale = (NewMax / UINT32_MAX) + 1;
  993. for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(),
  994. SE = MBB->succ_end();
  995. SI != SE; ++SI) {
  996. if (*SI == TrueBB)
  997. MBB->setSuccWeight(SI, (uint32_t)(NewTrue / Scale));
  998. else if (*SI == FalseBB)
  999. MBB->setSuccWeight(SI, (uint32_t)(NewFalse / Scale));
  1000. else
  1001. MBB->setSuccWeight(SI, MBPI->getEdgeWeight(MBB, SI) / Scale);
  1002. }
  1003. }
  1004. /// IfConvertTriangle - If convert a triangle sub-CFG.
  1005. ///
  1006. bool IfConverter::IfConvertTriangle(BBInfo &BBI, IfcvtKind Kind) {
  1007. BBInfo &TrueBBI = BBAnalysis[BBI.TrueBB->getNumber()];
  1008. BBInfo &FalseBBI = BBAnalysis[BBI.FalseBB->getNumber()];
  1009. BBInfo *CvtBBI = &TrueBBI;
  1010. BBInfo *NextBBI = &FalseBBI;
  1011. DebugLoc dl; // FIXME: this is nowhere
  1012. SmallVector<MachineOperand, 4> Cond(BBI.BrCond.begin(), BBI.BrCond.end());
  1013. if (Kind == ICTriangleFalse || Kind == ICTriangleFRev)
  1014. std::swap(CvtBBI, NextBBI);
  1015. if (CvtBBI->IsDone ||
  1016. (CvtBBI->CannotBeCopied && CvtBBI->BB->pred_size() > 1)) {
  1017. // Something has changed. It's no longer safe to predicate this block.
  1018. BBI.IsAnalyzed = false;
  1019. CvtBBI->IsAnalyzed = false;
  1020. return false;
  1021. }
  1022. if (CvtBBI->BB->hasAddressTaken())
  1023. // Conservatively abort if-conversion if BB's address is taken.
  1024. return false;
  1025. if (Kind == ICTriangleFalse || Kind == ICTriangleFRev)
  1026. if (TII->ReverseBranchCondition(Cond))
  1027. llvm_unreachable("Unable to reverse branch condition!");
  1028. if (Kind == ICTriangleRev || Kind == ICTriangleFRev) {
  1029. if (ReverseBranchCondition(*CvtBBI)) {
  1030. // BB has been changed, modify its predecessors (except for this
  1031. // one) so they don't get ifcvt'ed based on bad intel.
  1032. for (MachineBasicBlock::pred_iterator PI = CvtBBI->BB->pred_begin(),
  1033. E = CvtBBI->BB->pred_end(); PI != E; ++PI) {
  1034. MachineBasicBlock *PBB = *PI;
  1035. if (PBB == BBI.BB)
  1036. continue;
  1037. BBInfo &PBBI = BBAnalysis[PBB->getNumber()];
  1038. if (PBBI.IsEnqueued) {
  1039. PBBI.IsAnalyzed = false;
  1040. PBBI.IsEnqueued = false;
  1041. }
  1042. }
  1043. }
  1044. }
  1045. // Initialize liveins to the first BB. These are potentially redefined by
  1046. // predicated instructions.
  1047. Redefs.init(TRI);
  1048. Redefs.addLiveIns(CvtBBI->BB);
  1049. Redefs.addLiveIns(NextBBI->BB);
  1050. DontKill.clear();
  1051. bool HasEarlyExit = CvtBBI->FalseBB != NULL;
  1052. uint64_t CvtNext = 0, CvtFalse = 0, BBNext = 0, BBCvt = 0, SumWeight = 0;
  1053. uint32_t WeightScale = 0;
  1054. if (HasEarlyExit) {
  1055. // Get weights before modifying CvtBBI->BB and BBI.BB.
  1056. CvtNext = MBPI->getEdgeWeight(CvtBBI->BB, NextBBI->BB);
  1057. CvtFalse = MBPI->getEdgeWeight(CvtBBI->BB, CvtBBI->FalseBB);
  1058. BBNext = MBPI->getEdgeWeight(BBI.BB, NextBBI->BB);
  1059. BBCvt = MBPI->getEdgeWeight(BBI.BB, CvtBBI->BB);
  1060. SumWeight = MBPI->getSumForBlock(CvtBBI->BB, WeightScale);
  1061. }
  1062. if (CvtBBI->BB->pred_size() > 1) {
  1063. BBI.NonPredSize -= TII->RemoveBranch(*BBI.BB);
  1064. // Copy instructions in the true block, predicate them, and add them to
  1065. // the entry block.
  1066. CopyAndPredicateBlock(BBI, *CvtBBI, Cond, true);
  1067. // RemoveExtraEdges won't work if the block has an unanalyzable branch, so
  1068. // explicitly remove CvtBBI as a successor.
  1069. BBI.BB->removeSuccessor(CvtBBI->BB);
  1070. } else {
  1071. // Predicate the 'true' block after removing its branch.
  1072. CvtBBI->NonPredSize -= TII->RemoveBranch(*CvtBBI->BB);
  1073. PredicateBlock(*CvtBBI, CvtBBI->BB->end(), Cond);
  1074. // Now merge the entry of the triangle with the true block.
  1075. BBI.NonPredSize -= TII->RemoveBranch(*BBI.BB);
  1076. MergeBlocks(BBI, *CvtBBI, false);
  1077. }
  1078. // If 'true' block has a 'false' successor, add an exit branch to it.
  1079. if (HasEarlyExit) {
  1080. SmallVector<MachineOperand, 4> RevCond(CvtBBI->BrCond.begin(),
  1081. CvtBBI->BrCond.end());
  1082. if (TII->ReverseBranchCondition(RevCond))
  1083. llvm_unreachable("Unable to reverse branch condition!");
  1084. TII->InsertBranch(*BBI.BB, CvtBBI->FalseBB, NULL, RevCond, dl);
  1085. BBI.BB->addSuccessor(CvtBBI->FalseBB);
  1086. // Update the edge weight for both CvtBBI->FalseBB and NextBBI.
  1087. // New_Weight(BBI.BB, NextBBI->BB) =
  1088. // Weight(BBI.BB, NextBBI->BB) * getSumForBlock(CvtBBI->BB) +
  1089. // Weight(BBI.BB, CvtBBI->BB) * Weight(CvtBBI->BB, NextBBI->BB)
  1090. // New_Weight(BBI.BB, CvtBBI->FalseBB) =
  1091. // Weight(BBI.BB, CvtBBI->BB) * Weight(CvtBBI->BB, CvtBBI->FalseBB)
  1092. uint64_t NewNext = BBNext * SumWeight + (BBCvt * CvtNext) / WeightScale;
  1093. uint64_t NewFalse = (BBCvt * CvtFalse) / WeightScale;
  1094. // We need to scale down all weights of BBI.BB to fit uint32_t.
  1095. // Here BBI.BB is connected to CvtBBI->FalseBB and will fall through to
  1096. // the next block.
  1097. ScaleWeights(NewNext, NewFalse, BBI.BB, getNextBlock(BBI.BB),
  1098. CvtBBI->FalseBB, MBPI);
  1099. }
  1100. // Merge in the 'false' block if the 'false' block has no other
  1101. // predecessors. Otherwise, add an unconditional branch to 'false'.
  1102. bool FalseBBDead = false;
  1103. bool IterIfcvt = true;
  1104. bool isFallThrough = canFallThroughTo(BBI.BB, NextBBI->BB);
  1105. if (!isFallThrough) {
  1106. // Only merge them if the true block does not fallthrough to the false
  1107. // block. By not merging them, we make it possible to iteratively
  1108. // ifcvt the blocks.
  1109. if (!HasEarlyExit &&
  1110. NextBBI->BB->pred_size() == 1 && !NextBBI->HasFallThrough &&
  1111. !NextBBI->BB->hasAddressTaken()) {
  1112. MergeBlocks(BBI, *NextBBI);
  1113. FalseBBDead = true;
  1114. } else {
  1115. InsertUncondBranch(BBI.BB, NextBBI->BB, TII);
  1116. BBI.HasFallThrough = false;
  1117. }
  1118. // Mixed predicated and unpredicated code. This cannot be iteratively
  1119. // predicated.
  1120. IterIfcvt = false;
  1121. }
  1122. RemoveExtraEdges(BBI);
  1123. // Update block info. BB can be iteratively if-converted.
  1124. if (!IterIfcvt)
  1125. BBI.IsDone = true;
  1126. InvalidatePreds(BBI.BB);
  1127. CvtBBI->IsDone = true;
  1128. if (FalseBBDead)
  1129. NextBBI->IsDone = true;
  1130. // FIXME: Must maintain LiveIns.
  1131. return true;
  1132. }
  1133. /// IfConvertDiamond - If convert a diamond sub-CFG.
  1134. ///
  1135. bool IfConverter::IfConvertDiamond(BBInfo &BBI, IfcvtKind Kind,
  1136. unsigned NumDups1, unsigned NumDups2) {
  1137. BBInfo &TrueBBI = BBAnalysis[BBI.TrueBB->getNumber()];
  1138. BBInfo &FalseBBI = BBAnalysis[BBI.FalseBB->getNumber()];
  1139. MachineBasicBlock *TailBB = TrueBBI.TrueBB;
  1140. // True block must fall through or end with an unanalyzable terminator.
  1141. if (!TailBB) {
  1142. if (blockAlwaysFallThrough(TrueBBI))
  1143. TailBB = FalseBBI.TrueBB;
  1144. assert((TailBB || !TrueBBI.IsBrAnalyzable) && "Unexpected!");
  1145. }
  1146. if (TrueBBI.IsDone || FalseBBI.IsDone ||
  1147. TrueBBI.BB->pred_size() > 1 ||
  1148. FalseBBI.BB->pred_size() > 1) {
  1149. // Something has changed. It's no longer safe to predicate these blocks.
  1150. BBI.IsAnalyzed = false;
  1151. TrueBBI.IsAnalyzed = false;
  1152. FalseBBI.IsAnalyzed = false;
  1153. return false;
  1154. }
  1155. if (TrueBBI.BB->hasAddressTaken() || FalseBBI.BB->hasAddressTaken())
  1156. // Conservatively abort if-conversion if either BB has its address taken.
  1157. return false;
  1158. // Put the predicated instructions from the 'true' block before the
  1159. // instructions from the 'false' block, unless the true block would clobber
  1160. // the predicate, in which case, do the opposite.
  1161. BBInfo *BBI1 = &TrueBBI;
  1162. BBInfo *BBI2 = &FalseBBI;
  1163. SmallVector<MachineOperand, 4> RevCond(BBI.BrCond.begin(), BBI.BrCond.end());
  1164. if (TII->ReverseBranchCondition(RevCond))
  1165. llvm_unreachable("Unable to reverse branch condition!");
  1166. SmallVector<MachineOperand, 4> *Cond1 = &BBI.BrCond;
  1167. SmallVector<MachineOperand, 4> *Cond2 = &RevCond;
  1168. // Figure out the more profitable ordering.
  1169. bool DoSwap = false;
  1170. if (TrueBBI.ClobbersPred && !FalseBBI.ClobbersPred)
  1171. DoSwap = true;
  1172. else if (TrueBBI.ClobbersPred == FalseBBI.ClobbersPred) {
  1173. if (TrueBBI.NonPredSize > FalseBBI.NonPredSize)
  1174. DoSwap = true;
  1175. }
  1176. if (DoSwap) {
  1177. std::swap(BBI1, BBI2);
  1178. std::swap(Cond1, Cond2);
  1179. }
  1180. // Remove the conditional branch from entry to the blocks.
  1181. BBI.NonPredSize -= TII->RemoveBranch(*BBI.BB);
  1182. // Initialize liveins to the first BB. These are potentially redefined by
  1183. // predicated instructions.
  1184. Redefs.init(TRI);
  1185. Redefs.addLiveIns(BBI1->BB);
  1186. // Remove the duplicated instructions at the beginnings of both paths.
  1187. MachineBasicBlock::iterator DI1 = BBI1->BB->begin();
  1188. MachineBasicBlock::iterator DI2 = BBI2->BB->begin();
  1189. MachineBasicBlock::iterator DIE1 = BBI1->BB->end();
  1190. MachineBasicBlock::iterator DIE2 = BBI2->BB->end();
  1191. // Skip dbg_value instructions
  1192. while (DI1 != DIE1 && DI1->isDebugValue())
  1193. ++DI1;
  1194. while (DI2 != DIE2 && DI2->isDebugValue())
  1195. ++DI2;
  1196. BBI1->NonPredSize -= NumDups1;
  1197. BBI2->NonPredSize -= NumDups1;
  1198. // Skip past the dups on each side separately since there may be
  1199. // differing dbg_value entries.
  1200. for (unsigned i = 0; i < NumDups1; ++DI1) {
  1201. if (!DI1->isDebugValue())
  1202. ++i;
  1203. }
  1204. while (NumDups1 != 0) {
  1205. ++DI2;
  1206. if (!DI2->isDebugValue())
  1207. --NumDups1;
  1208. }
  1209. // Compute a set of registers which must not be killed by instructions in BB1:
  1210. // This is everything used+live in BB2 after the duplicated instructions. We
  1211. // can compute this set by simulating liveness backwards from the end of BB2.
  1212. DontKill.init(TRI);
  1213. for (MachineBasicBlock::reverse_iterator I = BBI2->BB->rbegin(),
  1214. E = MachineBasicBlock::reverse_iterator(DI2); I != E; ++I) {
  1215. DontKill.stepBackward(*I);
  1216. }
  1217. for (MachineBasicBlock::const_iterator I = BBI1->BB->begin(), E = DI1; I != E;
  1218. ++I) {
  1219. Redefs.stepForward(*I);
  1220. }
  1221. BBI.BB->splice(BBI.BB->end(), BBI1->BB, BBI1->BB->begin(), DI1);
  1222. BBI2->BB->erase(BBI2->BB->begin(), DI2);
  1223. // Remove branch from 'true' block and remove duplicated instructions.
  1224. BBI1->NonPredSize -= TII->RemoveBranch(*BBI1->BB);
  1225. DI1 = BBI1->BB->end();
  1226. for (unsigned i = 0; i != NumDups2; ) {
  1227. // NumDups2 only counted non-dbg_value instructions, so this won't
  1228. // run off the head of the list.
  1229. assert (DI1 != BBI1->BB->begin());
  1230. --DI1;
  1231. // skip dbg_value instructions
  1232. if (!DI1->isDebugValue())
  1233. ++i;
  1234. }
  1235. BBI1->BB->erase(DI1, BBI1->BB->end());
  1236. // Kill flags in the true block for registers living into the false block
  1237. // must be removed.
  1238. RemoveKills(BBI1->BB->begin(), BBI1->BB->end(), DontKill, *TRI);
  1239. // Remove 'false' block branch and find the last instruction to predicate.
  1240. BBI2->NonPredSize -= TII->RemoveBranch(*BBI2->BB);
  1241. DI2 = BBI2->BB->end();
  1242. while (NumDups2 != 0) {
  1243. // NumDups2 only counted non-dbg_value instructions, so this won't
  1244. // run off the head of the list.
  1245. assert (DI2 != BBI2->BB->begin());
  1246. --DI2;
  1247. // skip dbg_value instructions
  1248. if (!DI2->isDebugValue())
  1249. --NumDups2;
  1250. }
  1251. // Remember which registers would later be defined by the false block.
  1252. // This allows us not to predicate instructions in the true block that would
  1253. // later be re-defined. That is, rather than
  1254. // subeq r0, r1, #1
  1255. // addne r0, r1, #1
  1256. // generate:
  1257. // sub r0, r1, #1
  1258. // addne r0, r1, #1
  1259. SmallSet<unsigned, 4> RedefsByFalse;
  1260. SmallSet<unsigned, 4> ExtUses;
  1261. if (TII->isProfitableToUnpredicate(*BBI1->BB, *BBI2->BB)) {
  1262. for (MachineBasicBlock::iterator FI = BBI2->BB->begin(); FI != DI2; ++FI) {
  1263. if (FI->isDebugValue())
  1264. continue;
  1265. SmallVector<unsigned, 4> Defs;
  1266. for (unsigned i = 0, e = FI->getNumOperands(); i != e; ++i) {
  1267. const MachineOperand &MO = FI->getOperand(i);
  1268. if (!MO.isReg())
  1269. continue;
  1270. unsigned Reg = MO.getReg();
  1271. if (!Reg)
  1272. continue;
  1273. if (MO.isDef()) {
  1274. Defs.push_back(Reg);
  1275. } else if (!RedefsByFalse.count(Reg)) {
  1276. // These are defined before ctrl flow reach the 'false' instructions.
  1277. // They cannot be modified by the 'true' instructions.
  1278. for (MCSubRegIterator SubRegs(Reg, TRI, /*IncludeSelf=*/true);
  1279. SubRegs.isValid(); ++SubRegs)
  1280. ExtUses.insert(*SubRegs);
  1281. }
  1282. }
  1283. for (unsigned i = 0, e = Defs.size(); i != e; ++i) {
  1284. unsigned Reg = Defs[i];
  1285. if (!ExtUses.count(Reg)) {
  1286. for (MCSubRegIterator SubRegs(Reg, TRI, /*IncludeSelf=*/true);
  1287. SubRegs.isValid(); ++SubRegs)
  1288. RedefsByFalse.insert(*SubRegs);
  1289. }
  1290. }
  1291. }
  1292. }
  1293. // Predicate the 'true' block.
  1294. PredicateBlock(*BBI1, BBI1->BB->end(), *Cond1, &RedefsByFalse);
  1295. // Predicate the 'false' block.
  1296. PredicateBlock(*BBI2, DI2, *Cond2);
  1297. // Merge the true block into the entry of the diamond.
  1298. MergeBlocks(BBI, *BBI1, TailBB == 0);
  1299. MergeBlocks(BBI, *BBI2, TailBB == 0);
  1300. // If the if-converted block falls through or unconditionally branches into
  1301. // the tail block, and the tail block does not have other predecessors, then
  1302. // fold the tail block in as well. Otherwise, unless it falls through to the
  1303. // tail, add a unconditional branch to it.
  1304. if (TailBB) {
  1305. BBInfo &TailBBI = BBAnalysis[TailBB->getNumber()];
  1306. bool CanMergeTail = !TailBBI.HasFallThrough &&
  1307. !TailBBI.BB->hasAddressTaken();
  1308. // There may still be a fall-through edge from BBI1 or BBI2 to TailBB;
  1309. // check if there are any other predecessors besides those.
  1310. unsigned NumPreds = TailBB->pred_size();
  1311. if (NumPreds > 1)
  1312. CanMergeTail = false;
  1313. else if (NumPreds == 1 && CanMergeTail) {
  1314. MachineBasicBlock::pred_iterator PI = TailBB->pred_begin();
  1315. if (*PI != BBI1->BB && *PI != BBI2->BB)
  1316. CanMergeTail = false;
  1317. }
  1318. if (CanMergeTail) {
  1319. MergeBlocks(BBI, TailBBI);
  1320. TailBBI.IsDone = true;
  1321. } else {
  1322. BBI.BB->addSuccessor(TailBB);
  1323. InsertUncondBranch(BBI.BB, TailBB, TII);
  1324. BBI.HasFallThrough = false;
  1325. }
  1326. }
  1327. // RemoveExtraEdges won't work if the block has an unanalyzable branch,
  1328. // which can happen here if TailBB is unanalyzable and is merged, so
  1329. // explicitly remove BBI1 and BBI2 as successors.
  1330. BBI.BB->removeSuccessor(BBI1->BB);
  1331. BBI.BB->removeSuccessor(BBI2->BB);
  1332. RemoveExtraEdges(BBI);
  1333. // Update block info.
  1334. BBI.IsDone = TrueBBI.IsDone = FalseBBI.IsDone = true;
  1335. InvalidatePreds(BBI.BB);
  1336. // FIXME: Must maintain LiveIns.
  1337. return true;
  1338. }
  1339. static bool MaySpeculate(const MachineInstr *MI,
  1340. SmallSet<unsigned, 4> &LaterRedefs,
  1341. const TargetInstrInfo *TII) {
  1342. bool SawStore = true;
  1343. if (!MI->isSafeToMove(TII, 0, SawStore))
  1344. return false;
  1345. for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
  1346. const MachineOperand &MO = MI->getOperand(i);
  1347. if (!MO.isReg())
  1348. continue;
  1349. unsigned Reg = MO.getReg();
  1350. if (!Reg)
  1351. continue;
  1352. if (MO.isDef() && !LaterRedefs.count(Reg))
  1353. return false;
  1354. }
  1355. return true;
  1356. }
  1357. /// PredicateBlock - Predicate instructions from the start of the block to the
  1358. /// specified end with the specified condition.
  1359. void IfConverter::PredicateBlock(BBInfo &BBI,
  1360. MachineBasicBlock::iterator E,
  1361. SmallVectorImpl<MachineOperand> &Cond,
  1362. SmallSet<unsigned, 4> *LaterRedefs) {
  1363. bool AnyUnpred = false;
  1364. bool MaySpec = LaterRedefs != 0;
  1365. for (MachineBasicBlock::iterator I = BBI.BB->begin(); I != E; ++I) {
  1366. if (I->isDebugValue() || TII->isPredicated(I))
  1367. continue;
  1368. // It may be possible not to predicate an instruction if it's the 'true'
  1369. // side of a diamond and the 'false' side may re-define the instruction's
  1370. // defs.
  1371. if (MaySpec && MaySpeculate(I, *LaterRedefs, TII)) {
  1372. AnyUnpred = true;
  1373. continue;
  1374. }
  1375. // If any instruction is predicated, then every instruction after it must
  1376. // be predicated.
  1377. MaySpec = false;
  1378. if (!TII->PredicateInstruction(I, Cond)) {
  1379. #ifndef NDEBUG
  1380. dbgs() << "Unable to predicate " << *I << "!\n";
  1381. #endif
  1382. llvm_unreachable(0);
  1383. }
  1384. // If the predicated instruction now redefines a register as the result of
  1385. // if-conversion, add an implicit kill.
  1386. UpdatePredRedefs(I, Redefs);
  1387. }
  1388. std::copy(Cond.begin(), Cond.end(), std::back_inserter(BBI.Predicate));
  1389. BBI.IsAnalyzed = false;
  1390. BBI.NonPredSize = 0;
  1391. ++NumIfConvBBs;
  1392. if (AnyUnpred)
  1393. ++NumUnpred;
  1394. }
  1395. /// CopyAndPredicateBlock - Copy and predicate instructions from source BB to
  1396. /// the destination block. Skip end of block branches if IgnoreBr is true.
  1397. void IfConverter::CopyAndPredicateBlock(BBInfo &ToBBI, BBInfo &FromBBI,
  1398. SmallVectorImpl<MachineOperand> &Cond,
  1399. bool IgnoreBr) {
  1400. MachineFunction &MF = *ToBBI.BB->getParent();
  1401. for (MachineBasicBlock::iterator I = FromBBI.BB->begin(),
  1402. E = FromBBI.BB->end(); I != E; ++I) {
  1403. // Do not copy the end of the block branches.
  1404. if (IgnoreBr && I->isBranch())
  1405. break;
  1406. MachineInstr *MI = MF.CloneMachineInstr(I);
  1407. ToBBI.BB->insert(ToBBI.BB->end(), MI);
  1408. ToBBI.NonPredSize++;
  1409. unsigned ExtraPredCost = TII->getPredicationCost(&*I);
  1410. unsigned NumCycles = SchedModel.computeInstrLatency(&*I, false);
  1411. if (NumCycles > 1)
  1412. ToBBI.ExtraCost += NumCycles-1;
  1413. ToBBI.ExtraCost2 += ExtraPredCost;
  1414. if (!TII->isPredicated(I) && !MI->isDebugValue()) {
  1415. if (!TII->PredicateInstruction(MI, Cond)) {
  1416. #ifndef NDEBUG
  1417. dbgs() << "Unable to predicate " << *I << "!\n";
  1418. #endif
  1419. llvm_unreachable(0);
  1420. }
  1421. }
  1422. // If the predicated instruction now redefines a register as the result of
  1423. // if-conversion, add an implicit kill.
  1424. UpdatePredRedefs(MI, Redefs);
  1425. // Some kill flags may not be correct anymore.
  1426. if (!DontKill.empty())
  1427. RemoveKills(*MI, DontKill);
  1428. }
  1429. if (!IgnoreBr) {
  1430. std::vector<MachineBasicBlock *> Succs(FromBBI.BB->succ_begin(),
  1431. FromBBI.BB->succ_end());
  1432. MachineBasicBlock *NBB = getNextBlock(FromBBI.BB);
  1433. MachineBasicBlock *FallThrough = FromBBI.HasFallThrough ? NBB : NULL;
  1434. for (unsigned i = 0, e = Succs.size(); i != e; ++i) {
  1435. MachineBasicBlock *Succ = Succs[i];
  1436. // Fallthrough edge can't be transferred.
  1437. if (Succ == FallThrough)
  1438. continue;
  1439. ToBBI.BB->addSuccessor(Succ);
  1440. }
  1441. }
  1442. std::copy(FromBBI.Predicate.begin(), FromBBI.Predicate.end(),
  1443. std::back_inserter(ToBBI.Predicate));
  1444. std::copy(Cond.begin(), Cond.end(), std::back_inserter(ToBBI.Predicate));
  1445. ToBBI.ClobbersPred |= FromBBI.ClobbersPred;
  1446. ToBBI.IsAnalyzed = false;
  1447. ++NumDupBBs;
  1448. }
  1449. /// MergeBlocks - Move all instructions from FromBB to the end of ToBB.
  1450. /// This will leave FromBB as an empty block, so remove all of its
  1451. /// successor edges except for the fall-through edge. If AddEdges is true,
  1452. /// i.e., when FromBBI's branch is being moved, add those successor edges to
  1453. /// ToBBI.
  1454. void IfConverter::MergeBlocks(BBInfo &ToBBI, BBInfo &FromBBI, bool AddEdges) {
  1455. assert(!FromBBI.BB->hasAddressTaken() &&
  1456. "Removing a BB whose address is taken!");
  1457. ToBBI.BB->splice(ToBBI.BB->end(),
  1458. FromBBI.BB, FromBBI.BB->begin(), FromBBI.BB->end());
  1459. std::vector<MachineBasicBlock *> Succs(FromBBI.BB->succ_begin(),
  1460. FromBBI.BB->succ_end());
  1461. MachineBasicBlock *NBB = getNextBlock(FromBBI.BB);
  1462. MachineBasicBlock *FallThrough = FromBBI.HasFallThrough ? NBB : NULL;
  1463. for (unsigned i = 0, e = Succs.size(); i != e; ++i) {
  1464. MachineBasicBlock *Succ = Succs[i];
  1465. // Fallthrough edge can't be transferred.
  1466. if (Succ == FallThrough)
  1467. continue;
  1468. FromBBI.BB->removeSuccessor(Succ);
  1469. if (AddEdges && !ToBBI.BB->isSuccessor(Succ))
  1470. ToBBI.BB->addSuccessor(Succ);
  1471. }
  1472. // Now FromBBI always falls through to the next block!
  1473. if (NBB && !FromBBI.BB->isSuccessor(NBB))
  1474. FromBBI.BB->addSuccessor(NBB);
  1475. std::copy(FromBBI.Predicate.begin(), FromBBI.Predicate.end(),
  1476. std::back_inserter(ToBBI.Predicate));
  1477. FromBBI.Predicate.clear();
  1478. ToBBI.NonPredSize += FromBBI.NonPredSize;
  1479. ToBBI.ExtraCost += FromBBI.ExtraCost;
  1480. ToBBI.ExtraCost2 += FromBBI.ExtraCost2;
  1481. FromBBI.NonPredSize = 0;
  1482. FromBBI.ExtraCost = 0;
  1483. FromBBI.ExtraCost2 = 0;
  1484. ToBBI.ClobbersPred |= FromBBI.ClobbersPred;
  1485. ToBBI.HasFallThrough = FromBBI.HasFallThrough;
  1486. ToBBI.IsAnalyzed = false;
  1487. FromBBI.IsAnalyzed = false;
  1488. }