IfConversion.cpp 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631
  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/MachineBranchProbabilityInfo.h"
  20. #include "llvm/CodeGen/MachineFunctionPass.h"
  21. #include "llvm/CodeGen/MachineInstrBuilder.h"
  22. #include "llvm/CodeGen/MachineModuleInfo.h"
  23. #include "llvm/CodeGen/MachineRegisterInfo.h"
  24. #include "llvm/CodeGen/TargetSchedule.h"
  25. #include "llvm/CodeGen/LivePhysRegs.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 = llvm::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. /// IfConvertTriangle - If convert a triangle sub-CFG.
  984. ///
  985. bool IfConverter::IfConvertTriangle(BBInfo &BBI, IfcvtKind Kind) {
  986. BBInfo &TrueBBI = BBAnalysis[BBI.TrueBB->getNumber()];
  987. BBInfo &FalseBBI = BBAnalysis[BBI.FalseBB->getNumber()];
  988. BBInfo *CvtBBI = &TrueBBI;
  989. BBInfo *NextBBI = &FalseBBI;
  990. DebugLoc dl; // FIXME: this is nowhere
  991. SmallVector<MachineOperand, 4> Cond(BBI.BrCond.begin(), BBI.BrCond.end());
  992. if (Kind == ICTriangleFalse || Kind == ICTriangleFRev)
  993. std::swap(CvtBBI, NextBBI);
  994. if (CvtBBI->IsDone ||
  995. (CvtBBI->CannotBeCopied && CvtBBI->BB->pred_size() > 1)) {
  996. // Something has changed. It's no longer safe to predicate this block.
  997. BBI.IsAnalyzed = false;
  998. CvtBBI->IsAnalyzed = false;
  999. return false;
  1000. }
  1001. if (CvtBBI->BB->hasAddressTaken())
  1002. // Conservatively abort if-conversion if BB's address is taken.
  1003. return false;
  1004. if (Kind == ICTriangleFalse || Kind == ICTriangleFRev)
  1005. if (TII->ReverseBranchCondition(Cond))
  1006. llvm_unreachable("Unable to reverse branch condition!");
  1007. if (Kind == ICTriangleRev || Kind == ICTriangleFRev) {
  1008. if (ReverseBranchCondition(*CvtBBI)) {
  1009. // BB has been changed, modify its predecessors (except for this
  1010. // one) so they don't get ifcvt'ed based on bad intel.
  1011. for (MachineBasicBlock::pred_iterator PI = CvtBBI->BB->pred_begin(),
  1012. E = CvtBBI->BB->pred_end(); PI != E; ++PI) {
  1013. MachineBasicBlock *PBB = *PI;
  1014. if (PBB == BBI.BB)
  1015. continue;
  1016. BBInfo &PBBI = BBAnalysis[PBB->getNumber()];
  1017. if (PBBI.IsEnqueued) {
  1018. PBBI.IsAnalyzed = false;
  1019. PBBI.IsEnqueued = false;
  1020. }
  1021. }
  1022. }
  1023. }
  1024. // Initialize liveins to the first BB. These are potentially redefined by
  1025. // predicated instructions.
  1026. Redefs.init(TRI);
  1027. Redefs.addLiveIns(CvtBBI->BB);
  1028. Redefs.addLiveIns(NextBBI->BB);
  1029. DontKill.clear();
  1030. bool HasEarlyExit = CvtBBI->FalseBB != NULL;
  1031. if (CvtBBI->BB->pred_size() > 1) {
  1032. BBI.NonPredSize -= TII->RemoveBranch(*BBI.BB);
  1033. // Copy instructions in the true block, predicate them, and add them to
  1034. // the entry block.
  1035. CopyAndPredicateBlock(BBI, *CvtBBI, Cond, true);
  1036. // RemoveExtraEdges won't work if the block has an unanalyzable branch, so
  1037. // explicitly remove CvtBBI as a successor.
  1038. BBI.BB->removeSuccessor(CvtBBI->BB);
  1039. } else {
  1040. // Predicate the 'true' block after removing its branch.
  1041. CvtBBI->NonPredSize -= TII->RemoveBranch(*CvtBBI->BB);
  1042. PredicateBlock(*CvtBBI, CvtBBI->BB->end(), Cond);
  1043. // Now merge the entry of the triangle with the true block.
  1044. BBI.NonPredSize -= TII->RemoveBranch(*BBI.BB);
  1045. MergeBlocks(BBI, *CvtBBI, false);
  1046. }
  1047. // If 'true' block has a 'false' successor, add an exit branch to it.
  1048. if (HasEarlyExit) {
  1049. SmallVector<MachineOperand, 4> RevCond(CvtBBI->BrCond.begin(),
  1050. CvtBBI->BrCond.end());
  1051. if (TII->ReverseBranchCondition(RevCond))
  1052. llvm_unreachable("Unable to reverse branch condition!");
  1053. TII->InsertBranch(*BBI.BB, CvtBBI->FalseBB, NULL, RevCond, dl);
  1054. BBI.BB->addSuccessor(CvtBBI->FalseBB);
  1055. }
  1056. // Merge in the 'false' block if the 'false' block has no other
  1057. // predecessors. Otherwise, add an unconditional branch to 'false'.
  1058. bool FalseBBDead = false;
  1059. bool IterIfcvt = true;
  1060. bool isFallThrough = canFallThroughTo(BBI.BB, NextBBI->BB);
  1061. if (!isFallThrough) {
  1062. // Only merge them if the true block does not fallthrough to the false
  1063. // block. By not merging them, we make it possible to iteratively
  1064. // ifcvt the blocks.
  1065. if (!HasEarlyExit &&
  1066. NextBBI->BB->pred_size() == 1 && !NextBBI->HasFallThrough &&
  1067. !NextBBI->BB->hasAddressTaken()) {
  1068. MergeBlocks(BBI, *NextBBI);
  1069. FalseBBDead = true;
  1070. } else {
  1071. InsertUncondBranch(BBI.BB, NextBBI->BB, TII);
  1072. BBI.HasFallThrough = false;
  1073. }
  1074. // Mixed predicated and unpredicated code. This cannot be iteratively
  1075. // predicated.
  1076. IterIfcvt = false;
  1077. }
  1078. RemoveExtraEdges(BBI);
  1079. // Update block info. BB can be iteratively if-converted.
  1080. if (!IterIfcvt)
  1081. BBI.IsDone = true;
  1082. InvalidatePreds(BBI.BB);
  1083. CvtBBI->IsDone = true;
  1084. if (FalseBBDead)
  1085. NextBBI->IsDone = true;
  1086. // FIXME: Must maintain LiveIns.
  1087. return true;
  1088. }
  1089. /// IfConvertDiamond - If convert a diamond sub-CFG.
  1090. ///
  1091. bool IfConverter::IfConvertDiamond(BBInfo &BBI, IfcvtKind Kind,
  1092. unsigned NumDups1, unsigned NumDups2) {
  1093. BBInfo &TrueBBI = BBAnalysis[BBI.TrueBB->getNumber()];
  1094. BBInfo &FalseBBI = BBAnalysis[BBI.FalseBB->getNumber()];
  1095. MachineBasicBlock *TailBB = TrueBBI.TrueBB;
  1096. // True block must fall through or end with an unanalyzable terminator.
  1097. if (!TailBB) {
  1098. if (blockAlwaysFallThrough(TrueBBI))
  1099. TailBB = FalseBBI.TrueBB;
  1100. assert((TailBB || !TrueBBI.IsBrAnalyzable) && "Unexpected!");
  1101. }
  1102. if (TrueBBI.IsDone || FalseBBI.IsDone ||
  1103. TrueBBI.BB->pred_size() > 1 ||
  1104. FalseBBI.BB->pred_size() > 1) {
  1105. // Something has changed. It's no longer safe to predicate these blocks.
  1106. BBI.IsAnalyzed = false;
  1107. TrueBBI.IsAnalyzed = false;
  1108. FalseBBI.IsAnalyzed = false;
  1109. return false;
  1110. }
  1111. if (TrueBBI.BB->hasAddressTaken() || FalseBBI.BB->hasAddressTaken())
  1112. // Conservatively abort if-conversion if either BB has its address taken.
  1113. return false;
  1114. // Put the predicated instructions from the 'true' block before the
  1115. // instructions from the 'false' block, unless the true block would clobber
  1116. // the predicate, in which case, do the opposite.
  1117. BBInfo *BBI1 = &TrueBBI;
  1118. BBInfo *BBI2 = &FalseBBI;
  1119. SmallVector<MachineOperand, 4> RevCond(BBI.BrCond.begin(), BBI.BrCond.end());
  1120. if (TII->ReverseBranchCondition(RevCond))
  1121. llvm_unreachable("Unable to reverse branch condition!");
  1122. SmallVector<MachineOperand, 4> *Cond1 = &BBI.BrCond;
  1123. SmallVector<MachineOperand, 4> *Cond2 = &RevCond;
  1124. // Figure out the more profitable ordering.
  1125. bool DoSwap = false;
  1126. if (TrueBBI.ClobbersPred && !FalseBBI.ClobbersPred)
  1127. DoSwap = true;
  1128. else if (TrueBBI.ClobbersPred == FalseBBI.ClobbersPred) {
  1129. if (TrueBBI.NonPredSize > FalseBBI.NonPredSize)
  1130. DoSwap = true;
  1131. }
  1132. if (DoSwap) {
  1133. std::swap(BBI1, BBI2);
  1134. std::swap(Cond1, Cond2);
  1135. }
  1136. // Remove the conditional branch from entry to the blocks.
  1137. BBI.NonPredSize -= TII->RemoveBranch(*BBI.BB);
  1138. // Initialize liveins to the first BB. These are potentially redefined by
  1139. // predicated instructions.
  1140. Redefs.init(TRI);
  1141. Redefs.addLiveIns(BBI1->BB);
  1142. // Remove the duplicated instructions at the beginnings of both paths.
  1143. MachineBasicBlock::iterator DI1 = BBI1->BB->begin();
  1144. MachineBasicBlock::iterator DI2 = BBI2->BB->begin();
  1145. MachineBasicBlock::iterator DIE1 = BBI1->BB->end();
  1146. MachineBasicBlock::iterator DIE2 = BBI2->BB->end();
  1147. // Skip dbg_value instructions
  1148. while (DI1 != DIE1 && DI1->isDebugValue())
  1149. ++DI1;
  1150. while (DI2 != DIE2 && DI2->isDebugValue())
  1151. ++DI2;
  1152. BBI1->NonPredSize -= NumDups1;
  1153. BBI2->NonPredSize -= NumDups1;
  1154. // Skip past the dups on each side separately since there may be
  1155. // differing dbg_value entries.
  1156. for (unsigned i = 0; i < NumDups1; ++DI1) {
  1157. if (!DI1->isDebugValue())
  1158. ++i;
  1159. }
  1160. while (NumDups1 != 0) {
  1161. ++DI2;
  1162. if (!DI2->isDebugValue())
  1163. --NumDups1;
  1164. }
  1165. // Compute a set of registers which must not be killed by instructions in BB1:
  1166. // This is everything used+live in BB2 after the duplicated instructions. We
  1167. // can compute this set by simulating liveness backwards from the end of BB2.
  1168. DontKill.init(TRI);
  1169. for (MachineBasicBlock::reverse_iterator I = BBI2->BB->rbegin(),
  1170. E = MachineBasicBlock::reverse_iterator(DI2); I != E; ++I) {
  1171. DontKill.stepBackward(*I);
  1172. }
  1173. for (MachineBasicBlock::const_iterator I = BBI1->BB->begin(), E = DI1; I != E;
  1174. ++I) {
  1175. Redefs.stepForward(*I);
  1176. }
  1177. BBI.BB->splice(BBI.BB->end(), BBI1->BB, BBI1->BB->begin(), DI1);
  1178. BBI2->BB->erase(BBI2->BB->begin(), DI2);
  1179. // Remove branch from 'true' block and remove duplicated instructions.
  1180. BBI1->NonPredSize -= TII->RemoveBranch(*BBI1->BB);
  1181. DI1 = BBI1->BB->end();
  1182. for (unsigned i = 0; i != NumDups2; ) {
  1183. // NumDups2 only counted non-dbg_value instructions, so this won't
  1184. // run off the head of the list.
  1185. assert (DI1 != BBI1->BB->begin());
  1186. --DI1;
  1187. // skip dbg_value instructions
  1188. if (!DI1->isDebugValue())
  1189. ++i;
  1190. }
  1191. BBI1->BB->erase(DI1, BBI1->BB->end());
  1192. // Kill flags in the true block for registers living into the false block
  1193. // must be removed.
  1194. RemoveKills(BBI1->BB->begin(), BBI1->BB->end(), DontKill, *TRI);
  1195. // Remove 'false' block branch and find the last instruction to predicate.
  1196. BBI2->NonPredSize -= TII->RemoveBranch(*BBI2->BB);
  1197. DI2 = BBI2->BB->end();
  1198. while (NumDups2 != 0) {
  1199. // NumDups2 only counted non-dbg_value instructions, so this won't
  1200. // run off the head of the list.
  1201. assert (DI2 != BBI2->BB->begin());
  1202. --DI2;
  1203. // skip dbg_value instructions
  1204. if (!DI2->isDebugValue())
  1205. --NumDups2;
  1206. }
  1207. // Remember which registers would later be defined by the false block.
  1208. // This allows us not to predicate instructions in the true block that would
  1209. // later be re-defined. That is, rather than
  1210. // subeq r0, r1, #1
  1211. // addne r0, r1, #1
  1212. // generate:
  1213. // sub r0, r1, #1
  1214. // addne r0, r1, #1
  1215. SmallSet<unsigned, 4> RedefsByFalse;
  1216. SmallSet<unsigned, 4> ExtUses;
  1217. if (TII->isProfitableToUnpredicate(*BBI1->BB, *BBI2->BB)) {
  1218. for (MachineBasicBlock::iterator FI = BBI2->BB->begin(); FI != DI2; ++FI) {
  1219. if (FI->isDebugValue())
  1220. continue;
  1221. SmallVector<unsigned, 4> Defs;
  1222. for (unsigned i = 0, e = FI->getNumOperands(); i != e; ++i) {
  1223. const MachineOperand &MO = FI->getOperand(i);
  1224. if (!MO.isReg())
  1225. continue;
  1226. unsigned Reg = MO.getReg();
  1227. if (!Reg)
  1228. continue;
  1229. if (MO.isDef()) {
  1230. Defs.push_back(Reg);
  1231. } else if (!RedefsByFalse.count(Reg)) {
  1232. // These are defined before ctrl flow reach the 'false' instructions.
  1233. // They cannot be modified by the 'true' instructions.
  1234. for (MCSubRegIterator SubRegs(Reg, TRI, /*IncludeSelf=*/true);
  1235. SubRegs.isValid(); ++SubRegs)
  1236. ExtUses.insert(*SubRegs);
  1237. }
  1238. }
  1239. for (unsigned i = 0, e = Defs.size(); i != e; ++i) {
  1240. unsigned Reg = Defs[i];
  1241. if (!ExtUses.count(Reg)) {
  1242. for (MCSubRegIterator SubRegs(Reg, TRI, /*IncludeSelf=*/true);
  1243. SubRegs.isValid(); ++SubRegs)
  1244. RedefsByFalse.insert(*SubRegs);
  1245. }
  1246. }
  1247. }
  1248. }
  1249. // Predicate the 'true' block.
  1250. PredicateBlock(*BBI1, BBI1->BB->end(), *Cond1, &RedefsByFalse);
  1251. // Predicate the 'false' block.
  1252. PredicateBlock(*BBI2, DI2, *Cond2);
  1253. // Merge the true block into the entry of the diamond.
  1254. MergeBlocks(BBI, *BBI1, TailBB == 0);
  1255. MergeBlocks(BBI, *BBI2, TailBB == 0);
  1256. // If the if-converted block falls through or unconditionally branches into
  1257. // the tail block, and the tail block does not have other predecessors, then
  1258. // fold the tail block in as well. Otherwise, unless it falls through to the
  1259. // tail, add a unconditional branch to it.
  1260. if (TailBB) {
  1261. BBInfo &TailBBI = BBAnalysis[TailBB->getNumber()];
  1262. bool CanMergeTail = !TailBBI.HasFallThrough &&
  1263. !TailBBI.BB->hasAddressTaken();
  1264. // There may still be a fall-through edge from BBI1 or BBI2 to TailBB;
  1265. // check if there are any other predecessors besides those.
  1266. unsigned NumPreds = TailBB->pred_size();
  1267. if (NumPreds > 1)
  1268. CanMergeTail = false;
  1269. else if (NumPreds == 1 && CanMergeTail) {
  1270. MachineBasicBlock::pred_iterator PI = TailBB->pred_begin();
  1271. if (*PI != BBI1->BB && *PI != BBI2->BB)
  1272. CanMergeTail = false;
  1273. }
  1274. if (CanMergeTail) {
  1275. MergeBlocks(BBI, TailBBI);
  1276. TailBBI.IsDone = true;
  1277. } else {
  1278. BBI.BB->addSuccessor(TailBB);
  1279. InsertUncondBranch(BBI.BB, TailBB, TII);
  1280. BBI.HasFallThrough = false;
  1281. }
  1282. }
  1283. // RemoveExtraEdges won't work if the block has an unanalyzable branch,
  1284. // which can happen here if TailBB is unanalyzable and is merged, so
  1285. // explicitly remove BBI1 and BBI2 as successors.
  1286. BBI.BB->removeSuccessor(BBI1->BB);
  1287. BBI.BB->removeSuccessor(BBI2->BB);
  1288. RemoveExtraEdges(BBI);
  1289. // Update block info.
  1290. BBI.IsDone = TrueBBI.IsDone = FalseBBI.IsDone = true;
  1291. InvalidatePreds(BBI.BB);
  1292. // FIXME: Must maintain LiveIns.
  1293. return true;
  1294. }
  1295. static bool MaySpeculate(const MachineInstr *MI,
  1296. SmallSet<unsigned, 4> &LaterRedefs,
  1297. const TargetInstrInfo *TII) {
  1298. bool SawStore = true;
  1299. if (!MI->isSafeToMove(TII, 0, SawStore))
  1300. return false;
  1301. for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
  1302. const MachineOperand &MO = MI->getOperand(i);
  1303. if (!MO.isReg())
  1304. continue;
  1305. unsigned Reg = MO.getReg();
  1306. if (!Reg)
  1307. continue;
  1308. if (MO.isDef() && !LaterRedefs.count(Reg))
  1309. return false;
  1310. }
  1311. return true;
  1312. }
  1313. /// PredicateBlock - Predicate instructions from the start of the block to the
  1314. /// specified end with the specified condition.
  1315. void IfConverter::PredicateBlock(BBInfo &BBI,
  1316. MachineBasicBlock::iterator E,
  1317. SmallVectorImpl<MachineOperand> &Cond,
  1318. SmallSet<unsigned, 4> *LaterRedefs) {
  1319. bool AnyUnpred = false;
  1320. bool MaySpec = LaterRedefs != 0;
  1321. for (MachineBasicBlock::iterator I = BBI.BB->begin(); I != E; ++I) {
  1322. if (I->isDebugValue() || TII->isPredicated(I))
  1323. continue;
  1324. // It may be possible not to predicate an instruction if it's the 'true'
  1325. // side of a diamond and the 'false' side may re-define the instruction's
  1326. // defs.
  1327. if (MaySpec && MaySpeculate(I, *LaterRedefs, TII)) {
  1328. AnyUnpred = true;
  1329. continue;
  1330. }
  1331. // If any instruction is predicated, then every instruction after it must
  1332. // be predicated.
  1333. MaySpec = false;
  1334. if (!TII->PredicateInstruction(I, Cond)) {
  1335. #ifndef NDEBUG
  1336. dbgs() << "Unable to predicate " << *I << "!\n";
  1337. #endif
  1338. llvm_unreachable(0);
  1339. }
  1340. // If the predicated instruction now redefines a register as the result of
  1341. // if-conversion, add an implicit kill.
  1342. UpdatePredRedefs(I, Redefs);
  1343. }
  1344. std::copy(Cond.begin(), Cond.end(), std::back_inserter(BBI.Predicate));
  1345. BBI.IsAnalyzed = false;
  1346. BBI.NonPredSize = 0;
  1347. ++NumIfConvBBs;
  1348. if (AnyUnpred)
  1349. ++NumUnpred;
  1350. }
  1351. /// CopyAndPredicateBlock - Copy and predicate instructions from source BB to
  1352. /// the destination block. Skip end of block branches if IgnoreBr is true.
  1353. void IfConverter::CopyAndPredicateBlock(BBInfo &ToBBI, BBInfo &FromBBI,
  1354. SmallVectorImpl<MachineOperand> &Cond,
  1355. bool IgnoreBr) {
  1356. MachineFunction &MF = *ToBBI.BB->getParent();
  1357. for (MachineBasicBlock::iterator I = FromBBI.BB->begin(),
  1358. E = FromBBI.BB->end(); I != E; ++I) {
  1359. // Do not copy the end of the block branches.
  1360. if (IgnoreBr && I->isBranch())
  1361. break;
  1362. MachineInstr *MI = MF.CloneMachineInstr(I);
  1363. ToBBI.BB->insert(ToBBI.BB->end(), MI);
  1364. ToBBI.NonPredSize++;
  1365. unsigned ExtraPredCost = TII->getPredicationCost(&*I);
  1366. unsigned NumCycles = SchedModel.computeInstrLatency(&*I, false);
  1367. if (NumCycles > 1)
  1368. ToBBI.ExtraCost += NumCycles-1;
  1369. ToBBI.ExtraCost2 += ExtraPredCost;
  1370. if (!TII->isPredicated(I) && !MI->isDebugValue()) {
  1371. if (!TII->PredicateInstruction(MI, Cond)) {
  1372. #ifndef NDEBUG
  1373. dbgs() << "Unable to predicate " << *I << "!\n";
  1374. #endif
  1375. llvm_unreachable(0);
  1376. }
  1377. }
  1378. // If the predicated instruction now redefines a register as the result of
  1379. // if-conversion, add an implicit kill.
  1380. UpdatePredRedefs(MI, Redefs);
  1381. // Some kill flags may not be correct anymore.
  1382. if (!DontKill.empty())
  1383. RemoveKills(*MI, DontKill);
  1384. }
  1385. if (!IgnoreBr) {
  1386. std::vector<MachineBasicBlock *> Succs(FromBBI.BB->succ_begin(),
  1387. FromBBI.BB->succ_end());
  1388. MachineBasicBlock *NBB = getNextBlock(FromBBI.BB);
  1389. MachineBasicBlock *FallThrough = FromBBI.HasFallThrough ? NBB : NULL;
  1390. for (unsigned i = 0, e = Succs.size(); i != e; ++i) {
  1391. MachineBasicBlock *Succ = Succs[i];
  1392. // Fallthrough edge can't be transferred.
  1393. if (Succ == FallThrough)
  1394. continue;
  1395. ToBBI.BB->addSuccessor(Succ);
  1396. }
  1397. }
  1398. std::copy(FromBBI.Predicate.begin(), FromBBI.Predicate.end(),
  1399. std::back_inserter(ToBBI.Predicate));
  1400. std::copy(Cond.begin(), Cond.end(), std::back_inserter(ToBBI.Predicate));
  1401. ToBBI.ClobbersPred |= FromBBI.ClobbersPred;
  1402. ToBBI.IsAnalyzed = false;
  1403. ++NumDupBBs;
  1404. }
  1405. /// MergeBlocks - Move all instructions from FromBB to the end of ToBB.
  1406. /// This will leave FromBB as an empty block, so remove all of its
  1407. /// successor edges except for the fall-through edge. If AddEdges is true,
  1408. /// i.e., when FromBBI's branch is being moved, add those successor edges to
  1409. /// ToBBI.
  1410. void IfConverter::MergeBlocks(BBInfo &ToBBI, BBInfo &FromBBI, bool AddEdges) {
  1411. assert(!FromBBI.BB->hasAddressTaken() &&
  1412. "Removing a BB whose address is taken!");
  1413. ToBBI.BB->splice(ToBBI.BB->end(),
  1414. FromBBI.BB, FromBBI.BB->begin(), FromBBI.BB->end());
  1415. std::vector<MachineBasicBlock *> Succs(FromBBI.BB->succ_begin(),
  1416. FromBBI.BB->succ_end());
  1417. MachineBasicBlock *NBB = getNextBlock(FromBBI.BB);
  1418. MachineBasicBlock *FallThrough = FromBBI.HasFallThrough ? NBB : NULL;
  1419. for (unsigned i = 0, e = Succs.size(); i != e; ++i) {
  1420. MachineBasicBlock *Succ = Succs[i];
  1421. // Fallthrough edge can't be transferred.
  1422. if (Succ == FallThrough)
  1423. continue;
  1424. FromBBI.BB->removeSuccessor(Succ);
  1425. if (AddEdges && !ToBBI.BB->isSuccessor(Succ))
  1426. ToBBI.BB->addSuccessor(Succ);
  1427. }
  1428. // Now FromBBI always falls through to the next block!
  1429. if (NBB && !FromBBI.BB->isSuccessor(NBB))
  1430. FromBBI.BB->addSuccessor(NBB);
  1431. std::copy(FromBBI.Predicate.begin(), FromBBI.Predicate.end(),
  1432. std::back_inserter(ToBBI.Predicate));
  1433. FromBBI.Predicate.clear();
  1434. ToBBI.NonPredSize += FromBBI.NonPredSize;
  1435. ToBBI.ExtraCost += FromBBI.ExtraCost;
  1436. ToBBI.ExtraCost2 += FromBBI.ExtraCost2;
  1437. FromBBI.NonPredSize = 0;
  1438. FromBBI.ExtraCost = 0;
  1439. FromBBI.ExtraCost2 = 0;
  1440. ToBBI.ClobbersPred |= FromBBI.ClobbersPred;
  1441. ToBBI.HasFallThrough = FromBBI.HasFallThrough;
  1442. ToBBI.IsAnalyzed = false;
  1443. FromBBI.IsAnalyzed = false;
  1444. }