SimplifyCFG.cpp 121 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157
  1. //===- SimplifyCFG.cpp - Code to perform CFG simplification ---------------===//
  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. // Peephole optimize the CFG.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #define DEBUG_TYPE "simplifycfg"
  14. #include "llvm/Transforms/Utils/Local.h"
  15. #include "llvm/Constants.h"
  16. #include "llvm/DerivedTypes.h"
  17. #include "llvm/GlobalVariable.h"
  18. #include "llvm/IRBuilder.h"
  19. #include "llvm/Instructions.h"
  20. #include "llvm/IntrinsicInst.h"
  21. #include "llvm/LLVMContext.h"
  22. #include "llvm/Metadata.h"
  23. #include "llvm/Operator.h"
  24. #include "llvm/Type.h"
  25. #include "llvm/ADT/DenseMap.h"
  26. #include "llvm/ADT/STLExtras.h"
  27. #include "llvm/ADT/SetVector.h"
  28. #include "llvm/ADT/SmallPtrSet.h"
  29. #include "llvm/ADT/SmallVector.h"
  30. #include "llvm/ADT/Statistic.h"
  31. #include "llvm/Analysis/InstructionSimplify.h"
  32. #include "llvm/Analysis/ValueTracking.h"
  33. #include "llvm/Support/CFG.h"
  34. #include "llvm/Support/CommandLine.h"
  35. #include "llvm/Support/ConstantRange.h"
  36. #include "llvm/Support/Debug.h"
  37. #include "llvm/Support/MDBuilder.h"
  38. #include "llvm/Support/NoFolder.h"
  39. #include "llvm/Support/raw_ostream.h"
  40. #include "llvm/Target/TargetData.h"
  41. #include "llvm/Transforms/Utils/BasicBlockUtils.h"
  42. #include <algorithm>
  43. #include <set>
  44. #include <map>
  45. using namespace llvm;
  46. static cl::opt<unsigned>
  47. PHINodeFoldingThreshold("phi-node-folding-threshold", cl::Hidden, cl::init(1),
  48. cl::desc("Control the amount of phi node folding to perform (default = 1)"));
  49. static cl::opt<bool>
  50. DupRet("simplifycfg-dup-ret", cl::Hidden, cl::init(false),
  51. cl::desc("Duplicate return instructions into unconditional branches"));
  52. STATISTIC(NumSpeculations, "Number of speculative executed instructions");
  53. namespace {
  54. /// ValueEqualityComparisonCase - Represents a case of a switch.
  55. struct ValueEqualityComparisonCase {
  56. ConstantInt *Value;
  57. BasicBlock *Dest;
  58. ValueEqualityComparisonCase(ConstantInt *Value, BasicBlock *Dest)
  59. : Value(Value), Dest(Dest) {}
  60. bool operator<(ValueEqualityComparisonCase RHS) const {
  61. // Comparing pointers is ok as we only rely on the order for uniquing.
  62. return Value < RHS.Value;
  63. }
  64. };
  65. class SimplifyCFGOpt {
  66. const TargetData *const TD;
  67. Value *isValueEqualityComparison(TerminatorInst *TI);
  68. BasicBlock *GetValueEqualityComparisonCases(TerminatorInst *TI,
  69. std::vector<ValueEqualityComparisonCase> &Cases);
  70. bool SimplifyEqualityComparisonWithOnlyPredecessor(TerminatorInst *TI,
  71. BasicBlock *Pred,
  72. IRBuilder<> &Builder);
  73. bool FoldValueComparisonIntoPredecessors(TerminatorInst *TI,
  74. IRBuilder<> &Builder);
  75. bool SimplifyReturn(ReturnInst *RI, IRBuilder<> &Builder);
  76. bool SimplifyResume(ResumeInst *RI, IRBuilder<> &Builder);
  77. bool SimplifyUnreachable(UnreachableInst *UI);
  78. bool SimplifySwitch(SwitchInst *SI, IRBuilder<> &Builder);
  79. bool SimplifyIndirectBr(IndirectBrInst *IBI);
  80. bool SimplifyUncondBranch(BranchInst *BI, IRBuilder <> &Builder);
  81. bool SimplifyCondBranch(BranchInst *BI, IRBuilder <>&Builder);
  82. public:
  83. explicit SimplifyCFGOpt(const TargetData *td) : TD(td) {}
  84. bool run(BasicBlock *BB);
  85. };
  86. }
  87. /// SafeToMergeTerminators - Return true if it is safe to merge these two
  88. /// terminator instructions together.
  89. ///
  90. static bool SafeToMergeTerminators(TerminatorInst *SI1, TerminatorInst *SI2) {
  91. if (SI1 == SI2) return false; // Can't merge with self!
  92. // It is not safe to merge these two switch instructions if they have a common
  93. // successor, and if that successor has a PHI node, and if *that* PHI node has
  94. // conflicting incoming values from the two switch blocks.
  95. BasicBlock *SI1BB = SI1->getParent();
  96. BasicBlock *SI2BB = SI2->getParent();
  97. SmallPtrSet<BasicBlock*, 16> SI1Succs(succ_begin(SI1BB), succ_end(SI1BB));
  98. for (succ_iterator I = succ_begin(SI2BB), E = succ_end(SI2BB); I != E; ++I)
  99. if (SI1Succs.count(*I))
  100. for (BasicBlock::iterator BBI = (*I)->begin();
  101. isa<PHINode>(BBI); ++BBI) {
  102. PHINode *PN = cast<PHINode>(BBI);
  103. if (PN->getIncomingValueForBlock(SI1BB) !=
  104. PN->getIncomingValueForBlock(SI2BB))
  105. return false;
  106. }
  107. return true;
  108. }
  109. /// isProfitableToFoldUnconditional - Return true if it is safe and profitable
  110. /// to merge these two terminator instructions together, where SI1 is an
  111. /// unconditional branch. PhiNodes will store all PHI nodes in common
  112. /// successors.
  113. ///
  114. static bool isProfitableToFoldUnconditional(BranchInst *SI1,
  115. BranchInst *SI2,
  116. Instruction *Cond,
  117. SmallVectorImpl<PHINode*> &PhiNodes) {
  118. if (SI1 == SI2) return false; // Can't merge with self!
  119. assert(SI1->isUnconditional() && SI2->isConditional());
  120. // We fold the unconditional branch if we can easily update all PHI nodes in
  121. // common successors:
  122. // 1> We have a constant incoming value for the conditional branch;
  123. // 2> We have "Cond" as the incoming value for the unconditional branch;
  124. // 3> SI2->getCondition() and Cond have same operands.
  125. CmpInst *Ci2 = dyn_cast<CmpInst>(SI2->getCondition());
  126. if (!Ci2) return false;
  127. if (!(Cond->getOperand(0) == Ci2->getOperand(0) &&
  128. Cond->getOperand(1) == Ci2->getOperand(1)) &&
  129. !(Cond->getOperand(0) == Ci2->getOperand(1) &&
  130. Cond->getOperand(1) == Ci2->getOperand(0)))
  131. return false;
  132. BasicBlock *SI1BB = SI1->getParent();
  133. BasicBlock *SI2BB = SI2->getParent();
  134. SmallPtrSet<BasicBlock*, 16> SI1Succs(succ_begin(SI1BB), succ_end(SI1BB));
  135. for (succ_iterator I = succ_begin(SI2BB), E = succ_end(SI2BB); I != E; ++I)
  136. if (SI1Succs.count(*I))
  137. for (BasicBlock::iterator BBI = (*I)->begin();
  138. isa<PHINode>(BBI); ++BBI) {
  139. PHINode *PN = cast<PHINode>(BBI);
  140. if (PN->getIncomingValueForBlock(SI1BB) != Cond ||
  141. !isa<ConstantInt>(PN->getIncomingValueForBlock(SI2BB)))
  142. return false;
  143. PhiNodes.push_back(PN);
  144. }
  145. return true;
  146. }
  147. /// AddPredecessorToBlock - Update PHI nodes in Succ to indicate that there will
  148. /// now be entries in it from the 'NewPred' block. The values that will be
  149. /// flowing into the PHI nodes will be the same as those coming in from
  150. /// ExistPred, an existing predecessor of Succ.
  151. static void AddPredecessorToBlock(BasicBlock *Succ, BasicBlock *NewPred,
  152. BasicBlock *ExistPred) {
  153. if (!isa<PHINode>(Succ->begin())) return; // Quick exit if nothing to do
  154. PHINode *PN;
  155. for (BasicBlock::iterator I = Succ->begin();
  156. (PN = dyn_cast<PHINode>(I)); ++I)
  157. PN->addIncoming(PN->getIncomingValueForBlock(ExistPred), NewPred);
  158. }
  159. /// GetIfCondition - Given a basic block (BB) with two predecessors (and at
  160. /// least one PHI node in it), check to see if the merge at this block is due
  161. /// to an "if condition". If so, return the boolean condition that determines
  162. /// which entry into BB will be taken. Also, return by references the block
  163. /// that will be entered from if the condition is true, and the block that will
  164. /// be entered if the condition is false.
  165. ///
  166. /// This does no checking to see if the true/false blocks have large or unsavory
  167. /// instructions in them.
  168. static Value *GetIfCondition(BasicBlock *BB, BasicBlock *&IfTrue,
  169. BasicBlock *&IfFalse) {
  170. PHINode *SomePHI = cast<PHINode>(BB->begin());
  171. assert(SomePHI->getNumIncomingValues() == 2 &&
  172. "Function can only handle blocks with 2 predecessors!");
  173. BasicBlock *Pred1 = SomePHI->getIncomingBlock(0);
  174. BasicBlock *Pred2 = SomePHI->getIncomingBlock(1);
  175. // We can only handle branches. Other control flow will be lowered to
  176. // branches if possible anyway.
  177. BranchInst *Pred1Br = dyn_cast<BranchInst>(Pred1->getTerminator());
  178. BranchInst *Pred2Br = dyn_cast<BranchInst>(Pred2->getTerminator());
  179. if (Pred1Br == 0 || Pred2Br == 0)
  180. return 0;
  181. // Eliminate code duplication by ensuring that Pred1Br is conditional if
  182. // either are.
  183. if (Pred2Br->isConditional()) {
  184. // If both branches are conditional, we don't have an "if statement". In
  185. // reality, we could transform this case, but since the condition will be
  186. // required anyway, we stand no chance of eliminating it, so the xform is
  187. // probably not profitable.
  188. if (Pred1Br->isConditional())
  189. return 0;
  190. std::swap(Pred1, Pred2);
  191. std::swap(Pred1Br, Pred2Br);
  192. }
  193. if (Pred1Br->isConditional()) {
  194. // The only thing we have to watch out for here is to make sure that Pred2
  195. // doesn't have incoming edges from other blocks. If it does, the condition
  196. // doesn't dominate BB.
  197. if (Pred2->getSinglePredecessor() == 0)
  198. return 0;
  199. // If we found a conditional branch predecessor, make sure that it branches
  200. // to BB and Pred2Br. If it doesn't, this isn't an "if statement".
  201. if (Pred1Br->getSuccessor(0) == BB &&
  202. Pred1Br->getSuccessor(1) == Pred2) {
  203. IfTrue = Pred1;
  204. IfFalse = Pred2;
  205. } else if (Pred1Br->getSuccessor(0) == Pred2 &&
  206. Pred1Br->getSuccessor(1) == BB) {
  207. IfTrue = Pred2;
  208. IfFalse = Pred1;
  209. } else {
  210. // We know that one arm of the conditional goes to BB, so the other must
  211. // go somewhere unrelated, and this must not be an "if statement".
  212. return 0;
  213. }
  214. return Pred1Br->getCondition();
  215. }
  216. // Ok, if we got here, both predecessors end with an unconditional branch to
  217. // BB. Don't panic! If both blocks only have a single (identical)
  218. // predecessor, and THAT is a conditional branch, then we're all ok!
  219. BasicBlock *CommonPred = Pred1->getSinglePredecessor();
  220. if (CommonPred == 0 || CommonPred != Pred2->getSinglePredecessor())
  221. return 0;
  222. // Otherwise, if this is a conditional branch, then we can use it!
  223. BranchInst *BI = dyn_cast<BranchInst>(CommonPred->getTerminator());
  224. if (BI == 0) return 0;
  225. assert(BI->isConditional() && "Two successors but not conditional?");
  226. if (BI->getSuccessor(0) == Pred1) {
  227. IfTrue = Pred1;
  228. IfFalse = Pred2;
  229. } else {
  230. IfTrue = Pred2;
  231. IfFalse = Pred1;
  232. }
  233. return BI->getCondition();
  234. }
  235. /// ComputeSpeculuationCost - Compute an abstract "cost" of speculating the
  236. /// given instruction, which is assumed to be safe to speculate. 1 means
  237. /// cheap, 2 means less cheap, and UINT_MAX means prohibitively expensive.
  238. static unsigned ComputeSpeculationCost(const User *I) {
  239. assert(isSafeToSpeculativelyExecute(I) &&
  240. "Instruction is not safe to speculatively execute!");
  241. switch (Operator::getOpcode(I)) {
  242. default:
  243. // In doubt, be conservative.
  244. return UINT_MAX;
  245. case Instruction::GetElementPtr:
  246. // GEPs are cheap if all indices are constant.
  247. if (!cast<GEPOperator>(I)->hasAllConstantIndices())
  248. return UINT_MAX;
  249. return 1;
  250. case Instruction::Load:
  251. case Instruction::Add:
  252. case Instruction::Sub:
  253. case Instruction::And:
  254. case Instruction::Or:
  255. case Instruction::Xor:
  256. case Instruction::Shl:
  257. case Instruction::LShr:
  258. case Instruction::AShr:
  259. case Instruction::ICmp:
  260. case Instruction::Trunc:
  261. case Instruction::ZExt:
  262. case Instruction::SExt:
  263. return 1; // These are all cheap.
  264. case Instruction::Call:
  265. case Instruction::Select:
  266. return 2;
  267. }
  268. }
  269. /// DominatesMergePoint - If we have a merge point of an "if condition" as
  270. /// accepted above, return true if the specified value dominates the block. We
  271. /// don't handle the true generality of domination here, just a special case
  272. /// which works well enough for us.
  273. ///
  274. /// If AggressiveInsts is non-null, and if V does not dominate BB, we check to
  275. /// see if V (which must be an instruction) and its recursive operands
  276. /// that do not dominate BB have a combined cost lower than CostRemaining and
  277. /// are non-trapping. If both are true, the instruction is inserted into the
  278. /// set and true is returned.
  279. ///
  280. /// The cost for most non-trapping instructions is defined as 1 except for
  281. /// Select whose cost is 2.
  282. ///
  283. /// After this function returns, CostRemaining is decreased by the cost of
  284. /// V plus its non-dominating operands. If that cost is greater than
  285. /// CostRemaining, false is returned and CostRemaining is undefined.
  286. static bool DominatesMergePoint(Value *V, BasicBlock *BB,
  287. SmallPtrSet<Instruction*, 4> *AggressiveInsts,
  288. unsigned &CostRemaining) {
  289. Instruction *I = dyn_cast<Instruction>(V);
  290. if (!I) {
  291. // Non-instructions all dominate instructions, but not all constantexprs
  292. // can be executed unconditionally.
  293. if (ConstantExpr *C = dyn_cast<ConstantExpr>(V))
  294. if (C->canTrap())
  295. return false;
  296. return true;
  297. }
  298. BasicBlock *PBB = I->getParent();
  299. // We don't want to allow weird loops that might have the "if condition" in
  300. // the bottom of this block.
  301. if (PBB == BB) return false;
  302. // If this instruction is defined in a block that contains an unconditional
  303. // branch to BB, then it must be in the 'conditional' part of the "if
  304. // statement". If not, it definitely dominates the region.
  305. BranchInst *BI = dyn_cast<BranchInst>(PBB->getTerminator());
  306. if (BI == 0 || BI->isConditional() || BI->getSuccessor(0) != BB)
  307. return true;
  308. // If we aren't allowing aggressive promotion anymore, then don't consider
  309. // instructions in the 'if region'.
  310. if (AggressiveInsts == 0) return false;
  311. // If we have seen this instruction before, don't count it again.
  312. if (AggressiveInsts->count(I)) return true;
  313. // Okay, it looks like the instruction IS in the "condition". Check to
  314. // see if it's a cheap instruction to unconditionally compute, and if it
  315. // only uses stuff defined outside of the condition. If so, hoist it out.
  316. if (!isSafeToSpeculativelyExecute(I))
  317. return false;
  318. unsigned Cost = ComputeSpeculationCost(I);
  319. if (Cost > CostRemaining)
  320. return false;
  321. CostRemaining -= Cost;
  322. // Okay, we can only really hoist these out if their operands do
  323. // not take us over the cost threshold.
  324. for (User::op_iterator i = I->op_begin(), e = I->op_end(); i != e; ++i)
  325. if (!DominatesMergePoint(*i, BB, AggressiveInsts, CostRemaining))
  326. return false;
  327. // Okay, it's safe to do this! Remember this instruction.
  328. AggressiveInsts->insert(I);
  329. return true;
  330. }
  331. /// GetConstantInt - Extract ConstantInt from value, looking through IntToPtr
  332. /// and PointerNullValue. Return NULL if value is not a constant int.
  333. static ConstantInt *GetConstantInt(Value *V, const TargetData *TD) {
  334. // Normal constant int.
  335. ConstantInt *CI = dyn_cast<ConstantInt>(V);
  336. if (CI || !TD || !isa<Constant>(V) || !V->getType()->isPointerTy())
  337. return CI;
  338. // This is some kind of pointer constant. Turn it into a pointer-sized
  339. // ConstantInt if possible.
  340. IntegerType *PtrTy = TD->getIntPtrType(V->getContext());
  341. // Null pointer means 0, see SelectionDAGBuilder::getValue(const Value*).
  342. if (isa<ConstantPointerNull>(V))
  343. return ConstantInt::get(PtrTy, 0);
  344. // IntToPtr const int.
  345. if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
  346. if (CE->getOpcode() == Instruction::IntToPtr)
  347. if (ConstantInt *CI = dyn_cast<ConstantInt>(CE->getOperand(0))) {
  348. // The constant is very likely to have the right type already.
  349. if (CI->getType() == PtrTy)
  350. return CI;
  351. else
  352. return cast<ConstantInt>
  353. (ConstantExpr::getIntegerCast(CI, PtrTy, /*isSigned=*/false));
  354. }
  355. return 0;
  356. }
  357. /// GatherConstantCompares - Given a potentially 'or'd or 'and'd together
  358. /// collection of icmp eq/ne instructions that compare a value against a
  359. /// constant, return the value being compared, and stick the constant into the
  360. /// Values vector.
  361. static Value *
  362. GatherConstantCompares(Value *V, std::vector<ConstantInt*> &Vals, Value *&Extra,
  363. const TargetData *TD, bool isEQ, unsigned &UsedICmps) {
  364. Instruction *I = dyn_cast<Instruction>(V);
  365. if (I == 0) return 0;
  366. // If this is an icmp against a constant, handle this as one of the cases.
  367. if (ICmpInst *ICI = dyn_cast<ICmpInst>(I)) {
  368. if (ConstantInt *C = GetConstantInt(I->getOperand(1), TD)) {
  369. if (ICI->getPredicate() == (isEQ ? ICmpInst::ICMP_EQ:ICmpInst::ICMP_NE)) {
  370. UsedICmps++;
  371. Vals.push_back(C);
  372. return I->getOperand(0);
  373. }
  374. // If we have "x ult 3" comparison, for example, then we can add 0,1,2 to
  375. // the set.
  376. ConstantRange Span =
  377. ConstantRange::makeICmpRegion(ICI->getPredicate(), C->getValue());
  378. // If this is an and/!= check then we want to optimize "x ugt 2" into
  379. // x != 0 && x != 1.
  380. if (!isEQ)
  381. Span = Span.inverse();
  382. // If there are a ton of values, we don't want to make a ginormous switch.
  383. if (Span.getSetSize().ugt(8) || Span.isEmptySet())
  384. return 0;
  385. for (APInt Tmp = Span.getLower(); Tmp != Span.getUpper(); ++Tmp)
  386. Vals.push_back(ConstantInt::get(V->getContext(), Tmp));
  387. UsedICmps++;
  388. return I->getOperand(0);
  389. }
  390. return 0;
  391. }
  392. // Otherwise, we can only handle an | or &, depending on isEQ.
  393. if (I->getOpcode() != (isEQ ? Instruction::Or : Instruction::And))
  394. return 0;
  395. unsigned NumValsBeforeLHS = Vals.size();
  396. unsigned UsedICmpsBeforeLHS = UsedICmps;
  397. if (Value *LHS = GatherConstantCompares(I->getOperand(0), Vals, Extra, TD,
  398. isEQ, UsedICmps)) {
  399. unsigned NumVals = Vals.size();
  400. unsigned UsedICmpsBeforeRHS = UsedICmps;
  401. if (Value *RHS = GatherConstantCompares(I->getOperand(1), Vals, Extra, TD,
  402. isEQ, UsedICmps)) {
  403. if (LHS == RHS)
  404. return LHS;
  405. Vals.resize(NumVals);
  406. UsedICmps = UsedICmpsBeforeRHS;
  407. }
  408. // The RHS of the or/and can't be folded in and we haven't used "Extra" yet,
  409. // set it and return success.
  410. if (Extra == 0 || Extra == I->getOperand(1)) {
  411. Extra = I->getOperand(1);
  412. return LHS;
  413. }
  414. Vals.resize(NumValsBeforeLHS);
  415. UsedICmps = UsedICmpsBeforeLHS;
  416. return 0;
  417. }
  418. // If the LHS can't be folded in, but Extra is available and RHS can, try to
  419. // use LHS as Extra.
  420. if (Extra == 0 || Extra == I->getOperand(0)) {
  421. Value *OldExtra = Extra;
  422. Extra = I->getOperand(0);
  423. if (Value *RHS = GatherConstantCompares(I->getOperand(1), Vals, Extra, TD,
  424. isEQ, UsedICmps))
  425. return RHS;
  426. assert(Vals.size() == NumValsBeforeLHS);
  427. Extra = OldExtra;
  428. }
  429. return 0;
  430. }
  431. static void EraseTerminatorInstAndDCECond(TerminatorInst *TI) {
  432. Instruction *Cond = 0;
  433. if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) {
  434. Cond = dyn_cast<Instruction>(SI->getCondition());
  435. } else if (BranchInst *BI = dyn_cast<BranchInst>(TI)) {
  436. if (BI->isConditional())
  437. Cond = dyn_cast<Instruction>(BI->getCondition());
  438. } else if (IndirectBrInst *IBI = dyn_cast<IndirectBrInst>(TI)) {
  439. Cond = dyn_cast<Instruction>(IBI->getAddress());
  440. }
  441. TI->eraseFromParent();
  442. if (Cond) RecursivelyDeleteTriviallyDeadInstructions(Cond);
  443. }
  444. /// isValueEqualityComparison - Return true if the specified terminator checks
  445. /// to see if a value is equal to constant integer value.
  446. Value *SimplifyCFGOpt::isValueEqualityComparison(TerminatorInst *TI) {
  447. Value *CV = 0;
  448. if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) {
  449. // Do not permit merging of large switch instructions into their
  450. // predecessors unless there is only one predecessor.
  451. if (SI->getNumSuccessors()*std::distance(pred_begin(SI->getParent()),
  452. pred_end(SI->getParent())) <= 128)
  453. CV = SI->getCondition();
  454. } else if (BranchInst *BI = dyn_cast<BranchInst>(TI))
  455. if (BI->isConditional() && BI->getCondition()->hasOneUse())
  456. if (ICmpInst *ICI = dyn_cast<ICmpInst>(BI->getCondition()))
  457. if ((ICI->getPredicate() == ICmpInst::ICMP_EQ ||
  458. ICI->getPredicate() == ICmpInst::ICMP_NE) &&
  459. GetConstantInt(ICI->getOperand(1), TD))
  460. CV = ICI->getOperand(0);
  461. // Unwrap any lossless ptrtoint cast.
  462. if (TD && CV && CV->getType() == TD->getIntPtrType(CV->getContext()))
  463. if (PtrToIntInst *PTII = dyn_cast<PtrToIntInst>(CV))
  464. CV = PTII->getOperand(0);
  465. return CV;
  466. }
  467. /// GetValueEqualityComparisonCases - Given a value comparison instruction,
  468. /// decode all of the 'cases' that it represents and return the 'default' block.
  469. BasicBlock *SimplifyCFGOpt::
  470. GetValueEqualityComparisonCases(TerminatorInst *TI,
  471. std::vector<ValueEqualityComparisonCase>
  472. &Cases) {
  473. if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) {
  474. Cases.reserve(SI->getNumCases());
  475. for (SwitchInst::CaseIt i = SI->case_begin(), e = SI->case_end(); i != e; ++i)
  476. Cases.push_back(ValueEqualityComparisonCase(i.getCaseValue(),
  477. i.getCaseSuccessor()));
  478. return SI->getDefaultDest();
  479. }
  480. BranchInst *BI = cast<BranchInst>(TI);
  481. ICmpInst *ICI = cast<ICmpInst>(BI->getCondition());
  482. BasicBlock *Succ = BI->getSuccessor(ICI->getPredicate() == ICmpInst::ICMP_NE);
  483. Cases.push_back(ValueEqualityComparisonCase(GetConstantInt(ICI->getOperand(1),
  484. TD),
  485. Succ));
  486. return BI->getSuccessor(ICI->getPredicate() == ICmpInst::ICMP_EQ);
  487. }
  488. /// EliminateBlockCases - Given a vector of bb/value pairs, remove any entries
  489. /// in the list that match the specified block.
  490. static void EliminateBlockCases(BasicBlock *BB,
  491. std::vector<ValueEqualityComparisonCase> &Cases) {
  492. for (unsigned i = 0, e = Cases.size(); i != e; ++i)
  493. if (Cases[i].Dest == BB) {
  494. Cases.erase(Cases.begin()+i);
  495. --i; --e;
  496. }
  497. }
  498. /// ValuesOverlap - Return true if there are any keys in C1 that exist in C2 as
  499. /// well.
  500. static bool
  501. ValuesOverlap(std::vector<ValueEqualityComparisonCase> &C1,
  502. std::vector<ValueEqualityComparisonCase > &C2) {
  503. std::vector<ValueEqualityComparisonCase> *V1 = &C1, *V2 = &C2;
  504. // Make V1 be smaller than V2.
  505. if (V1->size() > V2->size())
  506. std::swap(V1, V2);
  507. if (V1->size() == 0) return false;
  508. if (V1->size() == 1) {
  509. // Just scan V2.
  510. ConstantInt *TheVal = (*V1)[0].Value;
  511. for (unsigned i = 0, e = V2->size(); i != e; ++i)
  512. if (TheVal == (*V2)[i].Value)
  513. return true;
  514. }
  515. // Otherwise, just sort both lists and compare element by element.
  516. array_pod_sort(V1->begin(), V1->end());
  517. array_pod_sort(V2->begin(), V2->end());
  518. unsigned i1 = 0, i2 = 0, e1 = V1->size(), e2 = V2->size();
  519. while (i1 != e1 && i2 != e2) {
  520. if ((*V1)[i1].Value == (*V2)[i2].Value)
  521. return true;
  522. if ((*V1)[i1].Value < (*V2)[i2].Value)
  523. ++i1;
  524. else
  525. ++i2;
  526. }
  527. return false;
  528. }
  529. /// SimplifyEqualityComparisonWithOnlyPredecessor - If TI is known to be a
  530. /// terminator instruction and its block is known to only have a single
  531. /// predecessor block, check to see if that predecessor is also a value
  532. /// comparison with the same value, and if that comparison determines the
  533. /// outcome of this comparison. If so, simplify TI. This does a very limited
  534. /// form of jump threading.
  535. bool SimplifyCFGOpt::
  536. SimplifyEqualityComparisonWithOnlyPredecessor(TerminatorInst *TI,
  537. BasicBlock *Pred,
  538. IRBuilder<> &Builder) {
  539. Value *PredVal = isValueEqualityComparison(Pred->getTerminator());
  540. if (!PredVal) return false; // Not a value comparison in predecessor.
  541. Value *ThisVal = isValueEqualityComparison(TI);
  542. assert(ThisVal && "This isn't a value comparison!!");
  543. if (ThisVal != PredVal) return false; // Different predicates.
  544. // Find out information about when control will move from Pred to TI's block.
  545. std::vector<ValueEqualityComparisonCase> PredCases;
  546. BasicBlock *PredDef = GetValueEqualityComparisonCases(Pred->getTerminator(),
  547. PredCases);
  548. EliminateBlockCases(PredDef, PredCases); // Remove default from cases.
  549. // Find information about how control leaves this block.
  550. std::vector<ValueEqualityComparisonCase> ThisCases;
  551. BasicBlock *ThisDef = GetValueEqualityComparisonCases(TI, ThisCases);
  552. EliminateBlockCases(ThisDef, ThisCases); // Remove default from cases.
  553. // If TI's block is the default block from Pred's comparison, potentially
  554. // simplify TI based on this knowledge.
  555. if (PredDef == TI->getParent()) {
  556. // If we are here, we know that the value is none of those cases listed in
  557. // PredCases. If there are any cases in ThisCases that are in PredCases, we
  558. // can simplify TI.
  559. if (!ValuesOverlap(PredCases, ThisCases))
  560. return false;
  561. if (isa<BranchInst>(TI)) {
  562. // Okay, one of the successors of this condbr is dead. Convert it to a
  563. // uncond br.
  564. assert(ThisCases.size() == 1 && "Branch can only have one case!");
  565. // Insert the new branch.
  566. Instruction *NI = Builder.CreateBr(ThisDef);
  567. (void) NI;
  568. // Remove PHI node entries for the dead edge.
  569. ThisCases[0].Dest->removePredecessor(TI->getParent());
  570. DEBUG(dbgs() << "Threading pred instr: " << *Pred->getTerminator()
  571. << "Through successor TI: " << *TI << "Leaving: " << *NI << "\n");
  572. EraseTerminatorInstAndDCECond(TI);
  573. return true;
  574. }
  575. SwitchInst *SI = cast<SwitchInst>(TI);
  576. // Okay, TI has cases that are statically dead, prune them away.
  577. SmallPtrSet<Constant*, 16> DeadCases;
  578. for (unsigned i = 0, e = PredCases.size(); i != e; ++i)
  579. DeadCases.insert(PredCases[i].Value);
  580. DEBUG(dbgs() << "Threading pred instr: " << *Pred->getTerminator()
  581. << "Through successor TI: " << *TI);
  582. for (SwitchInst::CaseIt i = SI->case_end(), e = SI->case_begin(); i != e;) {
  583. --i;
  584. if (DeadCases.count(i.getCaseValue())) {
  585. i.getCaseSuccessor()->removePredecessor(TI->getParent());
  586. SI->removeCase(i);
  587. }
  588. }
  589. DEBUG(dbgs() << "Leaving: " << *TI << "\n");
  590. return true;
  591. }
  592. // Otherwise, TI's block must correspond to some matched value. Find out
  593. // which value (or set of values) this is.
  594. ConstantInt *TIV = 0;
  595. BasicBlock *TIBB = TI->getParent();
  596. for (unsigned i = 0, e = PredCases.size(); i != e; ++i)
  597. if (PredCases[i].Dest == TIBB) {
  598. if (TIV != 0)
  599. return false; // Cannot handle multiple values coming to this block.
  600. TIV = PredCases[i].Value;
  601. }
  602. assert(TIV && "No edge from pred to succ?");
  603. // Okay, we found the one constant that our value can be if we get into TI's
  604. // BB. Find out which successor will unconditionally be branched to.
  605. BasicBlock *TheRealDest = 0;
  606. for (unsigned i = 0, e = ThisCases.size(); i != e; ++i)
  607. if (ThisCases[i].Value == TIV) {
  608. TheRealDest = ThisCases[i].Dest;
  609. break;
  610. }
  611. // If not handled by any explicit cases, it is handled by the default case.
  612. if (TheRealDest == 0) TheRealDest = ThisDef;
  613. // Remove PHI node entries for dead edges.
  614. BasicBlock *CheckEdge = TheRealDest;
  615. for (succ_iterator SI = succ_begin(TIBB), e = succ_end(TIBB); SI != e; ++SI)
  616. if (*SI != CheckEdge)
  617. (*SI)->removePredecessor(TIBB);
  618. else
  619. CheckEdge = 0;
  620. // Insert the new branch.
  621. Instruction *NI = Builder.CreateBr(TheRealDest);
  622. (void) NI;
  623. DEBUG(dbgs() << "Threading pred instr: " << *Pred->getTerminator()
  624. << "Through successor TI: " << *TI << "Leaving: " << *NI << "\n");
  625. EraseTerminatorInstAndDCECond(TI);
  626. return true;
  627. }
  628. namespace {
  629. /// ConstantIntOrdering - This class implements a stable ordering of constant
  630. /// integers that does not depend on their address. This is important for
  631. /// applications that sort ConstantInt's to ensure uniqueness.
  632. struct ConstantIntOrdering {
  633. bool operator()(const ConstantInt *LHS, const ConstantInt *RHS) const {
  634. return LHS->getValue().ult(RHS->getValue());
  635. }
  636. };
  637. }
  638. static int ConstantIntSortPredicate(const void *P1, const void *P2) {
  639. const ConstantInt *LHS = *(const ConstantInt**)P1;
  640. const ConstantInt *RHS = *(const ConstantInt**)P2;
  641. if (LHS->getValue().ult(RHS->getValue()))
  642. return 1;
  643. if (LHS->getValue() == RHS->getValue())
  644. return 0;
  645. return -1;
  646. }
  647. /// FoldValueComparisonIntoPredecessors - The specified terminator is a value
  648. /// equality comparison instruction (either a switch or a branch on "X == c").
  649. /// See if any of the predecessors of the terminator block are value comparisons
  650. /// on the same value. If so, and if safe to do so, fold them together.
  651. bool SimplifyCFGOpt::FoldValueComparisonIntoPredecessors(TerminatorInst *TI,
  652. IRBuilder<> &Builder) {
  653. BasicBlock *BB = TI->getParent();
  654. Value *CV = isValueEqualityComparison(TI); // CondVal
  655. assert(CV && "Not a comparison?");
  656. bool Changed = false;
  657. SmallVector<BasicBlock*, 16> Preds(pred_begin(BB), pred_end(BB));
  658. while (!Preds.empty()) {
  659. BasicBlock *Pred = Preds.pop_back_val();
  660. // See if the predecessor is a comparison with the same value.
  661. TerminatorInst *PTI = Pred->getTerminator();
  662. Value *PCV = isValueEqualityComparison(PTI); // PredCondVal
  663. if (PCV == CV && SafeToMergeTerminators(TI, PTI)) {
  664. // Figure out which 'cases' to copy from SI to PSI.
  665. std::vector<ValueEqualityComparisonCase> BBCases;
  666. BasicBlock *BBDefault = GetValueEqualityComparisonCases(TI, BBCases);
  667. std::vector<ValueEqualityComparisonCase> PredCases;
  668. BasicBlock *PredDefault = GetValueEqualityComparisonCases(PTI, PredCases);
  669. // Based on whether the default edge from PTI goes to BB or not, fill in
  670. // PredCases and PredDefault with the new switch cases we would like to
  671. // build.
  672. SmallVector<BasicBlock*, 8> NewSuccessors;
  673. if (PredDefault == BB) {
  674. // If this is the default destination from PTI, only the edges in TI
  675. // that don't occur in PTI, or that branch to BB will be activated.
  676. std::set<ConstantInt*, ConstantIntOrdering> PTIHandled;
  677. for (unsigned i = 0, e = PredCases.size(); i != e; ++i)
  678. if (PredCases[i].Dest != BB)
  679. PTIHandled.insert(PredCases[i].Value);
  680. else {
  681. // The default destination is BB, we don't need explicit targets.
  682. std::swap(PredCases[i], PredCases.back());
  683. PredCases.pop_back();
  684. --i; --e;
  685. }
  686. // Reconstruct the new switch statement we will be building.
  687. if (PredDefault != BBDefault) {
  688. PredDefault->removePredecessor(Pred);
  689. PredDefault = BBDefault;
  690. NewSuccessors.push_back(BBDefault);
  691. }
  692. for (unsigned i = 0, e = BBCases.size(); i != e; ++i)
  693. if (!PTIHandled.count(BBCases[i].Value) &&
  694. BBCases[i].Dest != BBDefault) {
  695. PredCases.push_back(BBCases[i]);
  696. NewSuccessors.push_back(BBCases[i].Dest);
  697. }
  698. } else {
  699. // If this is not the default destination from PSI, only the edges
  700. // in SI that occur in PSI with a destination of BB will be
  701. // activated.
  702. std::set<ConstantInt*, ConstantIntOrdering> PTIHandled;
  703. for (unsigned i = 0, e = PredCases.size(); i != e; ++i)
  704. if (PredCases[i].Dest == BB) {
  705. PTIHandled.insert(PredCases[i].Value);
  706. std::swap(PredCases[i], PredCases.back());
  707. PredCases.pop_back();
  708. --i; --e;
  709. }
  710. // Okay, now we know which constants were sent to BB from the
  711. // predecessor. Figure out where they will all go now.
  712. for (unsigned i = 0, e = BBCases.size(); i != e; ++i)
  713. if (PTIHandled.count(BBCases[i].Value)) {
  714. // If this is one we are capable of getting...
  715. PredCases.push_back(BBCases[i]);
  716. NewSuccessors.push_back(BBCases[i].Dest);
  717. PTIHandled.erase(BBCases[i].Value);// This constant is taken care of
  718. }
  719. // If there are any constants vectored to BB that TI doesn't handle,
  720. // they must go to the default destination of TI.
  721. for (std::set<ConstantInt*, ConstantIntOrdering>::iterator I =
  722. PTIHandled.begin(),
  723. E = PTIHandled.end(); I != E; ++I) {
  724. PredCases.push_back(ValueEqualityComparisonCase(*I, BBDefault));
  725. NewSuccessors.push_back(BBDefault);
  726. }
  727. }
  728. // Okay, at this point, we know which new successor Pred will get. Make
  729. // sure we update the number of entries in the PHI nodes for these
  730. // successors.
  731. for (unsigned i = 0, e = NewSuccessors.size(); i != e; ++i)
  732. AddPredecessorToBlock(NewSuccessors[i], Pred, BB);
  733. Builder.SetInsertPoint(PTI);
  734. // Convert pointer to int before we switch.
  735. if (CV->getType()->isPointerTy()) {
  736. assert(TD && "Cannot switch on pointer without TargetData");
  737. CV = Builder.CreatePtrToInt(CV, TD->getIntPtrType(CV->getContext()),
  738. "magicptr");
  739. }
  740. // Now that the successors are updated, create the new Switch instruction.
  741. SwitchInst *NewSI = Builder.CreateSwitch(CV, PredDefault,
  742. PredCases.size());
  743. NewSI->setDebugLoc(PTI->getDebugLoc());
  744. for (unsigned i = 0, e = PredCases.size(); i != e; ++i)
  745. NewSI->addCase(PredCases[i].Value, PredCases[i].Dest);
  746. EraseTerminatorInstAndDCECond(PTI);
  747. // Okay, last check. If BB is still a successor of PSI, then we must
  748. // have an infinite loop case. If so, add an infinitely looping block
  749. // to handle the case to preserve the behavior of the code.
  750. BasicBlock *InfLoopBlock = 0;
  751. for (unsigned i = 0, e = NewSI->getNumSuccessors(); i != e; ++i)
  752. if (NewSI->getSuccessor(i) == BB) {
  753. if (InfLoopBlock == 0) {
  754. // Insert it at the end of the function, because it's either code,
  755. // or it won't matter if it's hot. :)
  756. InfLoopBlock = BasicBlock::Create(BB->getContext(),
  757. "infloop", BB->getParent());
  758. BranchInst::Create(InfLoopBlock, InfLoopBlock);
  759. }
  760. NewSI->setSuccessor(i, InfLoopBlock);
  761. }
  762. Changed = true;
  763. }
  764. }
  765. return Changed;
  766. }
  767. // isSafeToHoistInvoke - If we would need to insert a select that uses the
  768. // value of this invoke (comments in HoistThenElseCodeToIf explain why we
  769. // would need to do this), we can't hoist the invoke, as there is nowhere
  770. // to put the select in this case.
  771. static bool isSafeToHoistInvoke(BasicBlock *BB1, BasicBlock *BB2,
  772. Instruction *I1, Instruction *I2) {
  773. for (succ_iterator SI = succ_begin(BB1), E = succ_end(BB1); SI != E; ++SI) {
  774. PHINode *PN;
  775. for (BasicBlock::iterator BBI = SI->begin();
  776. (PN = dyn_cast<PHINode>(BBI)); ++BBI) {
  777. Value *BB1V = PN->getIncomingValueForBlock(BB1);
  778. Value *BB2V = PN->getIncomingValueForBlock(BB2);
  779. if (BB1V != BB2V && (BB1V==I1 || BB2V==I2)) {
  780. return false;
  781. }
  782. }
  783. }
  784. return true;
  785. }
  786. /// HoistThenElseCodeToIf - Given a conditional branch that goes to BB1 and
  787. /// BB2, hoist any common code in the two blocks up into the branch block. The
  788. /// caller of this function guarantees that BI's block dominates BB1 and BB2.
  789. static bool HoistThenElseCodeToIf(BranchInst *BI) {
  790. // This does very trivial matching, with limited scanning, to find identical
  791. // instructions in the two blocks. In particular, we don't want to get into
  792. // O(M*N) situations here where M and N are the sizes of BB1 and BB2. As
  793. // such, we currently just scan for obviously identical instructions in an
  794. // identical order.
  795. BasicBlock *BB1 = BI->getSuccessor(0); // The true destination.
  796. BasicBlock *BB2 = BI->getSuccessor(1); // The false destination
  797. BasicBlock::iterator BB1_Itr = BB1->begin();
  798. BasicBlock::iterator BB2_Itr = BB2->begin();
  799. Instruction *I1 = BB1_Itr++, *I2 = BB2_Itr++;
  800. // Skip debug info if it is not identical.
  801. DbgInfoIntrinsic *DBI1 = dyn_cast<DbgInfoIntrinsic>(I1);
  802. DbgInfoIntrinsic *DBI2 = dyn_cast<DbgInfoIntrinsic>(I2);
  803. if (!DBI1 || !DBI2 || !DBI1->isIdenticalToWhenDefined(DBI2)) {
  804. while (isa<DbgInfoIntrinsic>(I1))
  805. I1 = BB1_Itr++;
  806. while (isa<DbgInfoIntrinsic>(I2))
  807. I2 = BB2_Itr++;
  808. }
  809. if (isa<PHINode>(I1) || !I1->isIdenticalToWhenDefined(I2) ||
  810. (isa<InvokeInst>(I1) && !isSafeToHoistInvoke(BB1, BB2, I1, I2)))
  811. return false;
  812. // If we get here, we can hoist at least one instruction.
  813. BasicBlock *BIParent = BI->getParent();
  814. do {
  815. // If we are hoisting the terminator instruction, don't move one (making a
  816. // broken BB), instead clone it, and remove BI.
  817. if (isa<TerminatorInst>(I1))
  818. goto HoistTerminator;
  819. // For a normal instruction, we just move one to right before the branch,
  820. // then replace all uses of the other with the first. Finally, we remove
  821. // the now redundant second instruction.
  822. BIParent->getInstList().splice(BI, BB1->getInstList(), I1);
  823. if (!I2->use_empty())
  824. I2->replaceAllUsesWith(I1);
  825. I1->intersectOptionalDataWith(I2);
  826. I2->eraseFromParent();
  827. I1 = BB1_Itr++;
  828. I2 = BB2_Itr++;
  829. // Skip debug info if it is not identical.
  830. DbgInfoIntrinsic *DBI1 = dyn_cast<DbgInfoIntrinsic>(I1);
  831. DbgInfoIntrinsic *DBI2 = dyn_cast<DbgInfoIntrinsic>(I2);
  832. if (!DBI1 || !DBI2 || !DBI1->isIdenticalToWhenDefined(DBI2)) {
  833. while (isa<DbgInfoIntrinsic>(I1))
  834. I1 = BB1_Itr++;
  835. while (isa<DbgInfoIntrinsic>(I2))
  836. I2 = BB2_Itr++;
  837. }
  838. } while (I1->isIdenticalToWhenDefined(I2));
  839. return true;
  840. HoistTerminator:
  841. // It may not be possible to hoist an invoke.
  842. if (isa<InvokeInst>(I1) && !isSafeToHoistInvoke(BB1, BB2, I1, I2))
  843. return true;
  844. // Okay, it is safe to hoist the terminator.
  845. Instruction *NT = I1->clone();
  846. BIParent->getInstList().insert(BI, NT);
  847. if (!NT->getType()->isVoidTy()) {
  848. I1->replaceAllUsesWith(NT);
  849. I2->replaceAllUsesWith(NT);
  850. NT->takeName(I1);
  851. }
  852. IRBuilder<true, NoFolder> Builder(NT);
  853. // Hoisting one of the terminators from our successor is a great thing.
  854. // Unfortunately, the successors of the if/else blocks may have PHI nodes in
  855. // them. If they do, all PHI entries for BB1/BB2 must agree for all PHI
  856. // nodes, so we insert select instruction to compute the final result.
  857. std::map<std::pair<Value*,Value*>, SelectInst*> InsertedSelects;
  858. for (succ_iterator SI = succ_begin(BB1), E = succ_end(BB1); SI != E; ++SI) {
  859. PHINode *PN;
  860. for (BasicBlock::iterator BBI = SI->begin();
  861. (PN = dyn_cast<PHINode>(BBI)); ++BBI) {
  862. Value *BB1V = PN->getIncomingValueForBlock(BB1);
  863. Value *BB2V = PN->getIncomingValueForBlock(BB2);
  864. if (BB1V == BB2V) continue;
  865. // These values do not agree. Insert a select instruction before NT
  866. // that determines the right value.
  867. SelectInst *&SI = InsertedSelects[std::make_pair(BB1V, BB2V)];
  868. if (SI == 0)
  869. SI = cast<SelectInst>
  870. (Builder.CreateSelect(BI->getCondition(), BB1V, BB2V,
  871. BB1V->getName()+"."+BB2V->getName()));
  872. // Make the PHI node use the select for all incoming values for BB1/BB2
  873. for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
  874. if (PN->getIncomingBlock(i) == BB1 || PN->getIncomingBlock(i) == BB2)
  875. PN->setIncomingValue(i, SI);
  876. }
  877. }
  878. // Update any PHI nodes in our new successors.
  879. for (succ_iterator SI = succ_begin(BB1), E = succ_end(BB1); SI != E; ++SI)
  880. AddPredecessorToBlock(*SI, BIParent, BB1);
  881. EraseTerminatorInstAndDCECond(BI);
  882. return true;
  883. }
  884. /// SpeculativelyExecuteBB - Given a conditional branch that goes to BB1
  885. /// and an BB2 and the only successor of BB1 is BB2, hoist simple code
  886. /// (for now, restricted to a single instruction that's side effect free) from
  887. /// the BB1 into the branch block to speculatively execute it.
  888. ///
  889. /// Turn
  890. /// BB:
  891. /// %t1 = icmp
  892. /// br i1 %t1, label %BB1, label %BB2
  893. /// BB1:
  894. /// %t3 = add %t2, c
  895. /// br label BB2
  896. /// BB2:
  897. /// =>
  898. /// BB:
  899. /// %t1 = icmp
  900. /// %t4 = add %t2, c
  901. /// %t3 = select i1 %t1, %t2, %t3
  902. static bool SpeculativelyExecuteBB(BranchInst *BI, BasicBlock *BB1) {
  903. // Only speculatively execution a single instruction (not counting the
  904. // terminator) for now.
  905. Instruction *HInst = NULL;
  906. Instruction *Term = BB1->getTerminator();
  907. for (BasicBlock::iterator BBI = BB1->begin(), BBE = BB1->end();
  908. BBI != BBE; ++BBI) {
  909. Instruction *I = BBI;
  910. // Skip debug info.
  911. if (isa<DbgInfoIntrinsic>(I)) continue;
  912. if (I == Term) break;
  913. if (HInst)
  914. return false;
  915. HInst = I;
  916. }
  917. BasicBlock *BIParent = BI->getParent();
  918. // Check the instruction to be hoisted, if there is one.
  919. if (HInst) {
  920. // Don't hoist the instruction if it's unsafe or expensive.
  921. if (!isSafeToSpeculativelyExecute(HInst))
  922. return false;
  923. if (ComputeSpeculationCost(HInst) > PHINodeFoldingThreshold)
  924. return false;
  925. // Do not hoist the instruction if any of its operands are defined but not
  926. // used in this BB. The transformation will prevent the operand from
  927. // being sunk into the use block.
  928. for (User::op_iterator i = HInst->op_begin(), e = HInst->op_end();
  929. i != e; ++i) {
  930. Instruction *OpI = dyn_cast<Instruction>(*i);
  931. if (OpI && OpI->getParent() == BIParent &&
  932. !OpI->mayHaveSideEffects() &&
  933. !OpI->isUsedInBasicBlock(BIParent))
  934. return false;
  935. }
  936. }
  937. // Be conservative for now. FP select instruction can often be expensive.
  938. Value *BrCond = BI->getCondition();
  939. if (isa<FCmpInst>(BrCond))
  940. return false;
  941. // If BB1 is actually on the false edge of the conditional branch, remember
  942. // to swap the select operands later.
  943. bool Invert = false;
  944. if (BB1 != BI->getSuccessor(0)) {
  945. assert(BB1 == BI->getSuccessor(1) && "No edge from 'if' block?");
  946. Invert = true;
  947. }
  948. // Collect interesting PHIs, and scan for hazards.
  949. SmallSetVector<std::pair<Value *, Value *>, 4> PHIs;
  950. BasicBlock *BB2 = BB1->getTerminator()->getSuccessor(0);
  951. for (BasicBlock::iterator I = BB2->begin();
  952. PHINode *PN = dyn_cast<PHINode>(I); ++I) {
  953. Value *BB1V = PN->getIncomingValueForBlock(BB1);
  954. Value *BIParentV = PN->getIncomingValueForBlock(BIParent);
  955. // Skip PHIs which are trivial.
  956. if (BB1V == BIParentV)
  957. continue;
  958. // Check for saftey.
  959. if (ConstantExpr *CE = dyn_cast<ConstantExpr>(BB1V)) {
  960. // An unfolded ConstantExpr could end up getting expanded into
  961. // Instructions. Don't speculate this and another instruction at
  962. // the same time.
  963. if (HInst)
  964. return false;
  965. if (!isSafeToSpeculativelyExecute(CE))
  966. return false;
  967. if (ComputeSpeculationCost(CE) > PHINodeFoldingThreshold)
  968. return false;
  969. }
  970. // Ok, we may insert a select for this PHI.
  971. PHIs.insert(std::make_pair(BB1V, BIParentV));
  972. }
  973. // If there are no PHIs to process, bail early. This helps ensure idempotence
  974. // as well.
  975. if (PHIs.empty())
  976. return false;
  977. // If we get here, we can hoist the instruction and if-convert.
  978. DEBUG(dbgs() << "SPECULATIVELY EXECUTING BB" << *BB1 << "\n";);
  979. // Hoist the instruction.
  980. if (HInst)
  981. BIParent->getInstList().splice(BI, BB1->getInstList(), HInst);
  982. // Insert selects and rewrite the PHI operands.
  983. IRBuilder<true, NoFolder> Builder(BI);
  984. for (unsigned i = 0, e = PHIs.size(); i != e; ++i) {
  985. Value *TrueV = PHIs[i].first;
  986. Value *FalseV = PHIs[i].second;
  987. // Create a select whose true value is the speculatively executed value and
  988. // false value is the previously determined FalseV.
  989. SelectInst *SI;
  990. if (Invert)
  991. SI = cast<SelectInst>
  992. (Builder.CreateSelect(BrCond, FalseV, TrueV,
  993. FalseV->getName() + "." + TrueV->getName()));
  994. else
  995. SI = cast<SelectInst>
  996. (Builder.CreateSelect(BrCond, TrueV, FalseV,
  997. TrueV->getName() + "." + FalseV->getName()));
  998. // Make the PHI node use the select for all incoming values for "then" and
  999. // "if" blocks.
  1000. for (BasicBlock::iterator I = BB2->begin();
  1001. PHINode *PN = dyn_cast<PHINode>(I); ++I) {
  1002. unsigned BB1I = PN->getBasicBlockIndex(BB1);
  1003. unsigned BIParentI = PN->getBasicBlockIndex(BIParent);
  1004. Value *BB1V = PN->getIncomingValue(BB1I);
  1005. Value *BIParentV = PN->getIncomingValue(BIParentI);
  1006. if (TrueV == BB1V && FalseV == BIParentV) {
  1007. PN->setIncomingValue(BB1I, SI);
  1008. PN->setIncomingValue(BIParentI, SI);
  1009. }
  1010. }
  1011. }
  1012. ++NumSpeculations;
  1013. return true;
  1014. }
  1015. /// BlockIsSimpleEnoughToThreadThrough - Return true if we can thread a branch
  1016. /// across this block.
  1017. static bool BlockIsSimpleEnoughToThreadThrough(BasicBlock *BB) {
  1018. BranchInst *BI = cast<BranchInst>(BB->getTerminator());
  1019. unsigned Size = 0;
  1020. for (BasicBlock::iterator BBI = BB->begin(); &*BBI != BI; ++BBI) {
  1021. if (isa<DbgInfoIntrinsic>(BBI))
  1022. continue;
  1023. if (Size > 10) return false; // Don't clone large BB's.
  1024. ++Size;
  1025. // We can only support instructions that do not define values that are
  1026. // live outside of the current basic block.
  1027. for (Value::use_iterator UI = BBI->use_begin(), E = BBI->use_end();
  1028. UI != E; ++UI) {
  1029. Instruction *U = cast<Instruction>(*UI);
  1030. if (U->getParent() != BB || isa<PHINode>(U)) return false;
  1031. }
  1032. // Looks ok, continue checking.
  1033. }
  1034. return true;
  1035. }
  1036. /// FoldCondBranchOnPHI - If we have a conditional branch on a PHI node value
  1037. /// that is defined in the same block as the branch and if any PHI entries are
  1038. /// constants, thread edges corresponding to that entry to be branches to their
  1039. /// ultimate destination.
  1040. static bool FoldCondBranchOnPHI(BranchInst *BI, const TargetData *TD) {
  1041. BasicBlock *BB = BI->getParent();
  1042. PHINode *PN = dyn_cast<PHINode>(BI->getCondition());
  1043. // NOTE: we currently cannot transform this case if the PHI node is used
  1044. // outside of the block.
  1045. if (!PN || PN->getParent() != BB || !PN->hasOneUse())
  1046. return false;
  1047. // Degenerate case of a single entry PHI.
  1048. if (PN->getNumIncomingValues() == 1) {
  1049. FoldSingleEntryPHINodes(PN->getParent());
  1050. return true;
  1051. }
  1052. // Now we know that this block has multiple preds and two succs.
  1053. if (!BlockIsSimpleEnoughToThreadThrough(BB)) return false;
  1054. // Okay, this is a simple enough basic block. See if any phi values are
  1055. // constants.
  1056. for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
  1057. ConstantInt *CB = dyn_cast<ConstantInt>(PN->getIncomingValue(i));
  1058. if (CB == 0 || !CB->getType()->isIntegerTy(1)) continue;
  1059. // Okay, we now know that all edges from PredBB should be revectored to
  1060. // branch to RealDest.
  1061. BasicBlock *PredBB = PN->getIncomingBlock(i);
  1062. BasicBlock *RealDest = BI->getSuccessor(!CB->getZExtValue());
  1063. if (RealDest == BB) continue; // Skip self loops.
  1064. // Skip if the predecessor's terminator is an indirect branch.
  1065. if (isa<IndirectBrInst>(PredBB->getTerminator())) continue;
  1066. // The dest block might have PHI nodes, other predecessors and other
  1067. // difficult cases. Instead of being smart about this, just insert a new
  1068. // block that jumps to the destination block, effectively splitting
  1069. // the edge we are about to create.
  1070. BasicBlock *EdgeBB = BasicBlock::Create(BB->getContext(),
  1071. RealDest->getName()+".critedge",
  1072. RealDest->getParent(), RealDest);
  1073. BranchInst::Create(RealDest, EdgeBB);
  1074. // Update PHI nodes.
  1075. AddPredecessorToBlock(RealDest, EdgeBB, BB);
  1076. // BB may have instructions that are being threaded over. Clone these
  1077. // instructions into EdgeBB. We know that there will be no uses of the
  1078. // cloned instructions outside of EdgeBB.
  1079. BasicBlock::iterator InsertPt = EdgeBB->begin();
  1080. DenseMap<Value*, Value*> TranslateMap; // Track translated values.
  1081. for (BasicBlock::iterator BBI = BB->begin(); &*BBI != BI; ++BBI) {
  1082. if (PHINode *PN = dyn_cast<PHINode>(BBI)) {
  1083. TranslateMap[PN] = PN->getIncomingValueForBlock(PredBB);
  1084. continue;
  1085. }
  1086. // Clone the instruction.
  1087. Instruction *N = BBI->clone();
  1088. if (BBI->hasName()) N->setName(BBI->getName()+".c");
  1089. // Update operands due to translation.
  1090. for (User::op_iterator i = N->op_begin(), e = N->op_end();
  1091. i != e; ++i) {
  1092. DenseMap<Value*, Value*>::iterator PI = TranslateMap.find(*i);
  1093. if (PI != TranslateMap.end())
  1094. *i = PI->second;
  1095. }
  1096. // Check for trivial simplification.
  1097. if (Value *V = SimplifyInstruction(N, TD)) {
  1098. TranslateMap[BBI] = V;
  1099. delete N; // Instruction folded away, don't need actual inst
  1100. } else {
  1101. // Insert the new instruction into its new home.
  1102. EdgeBB->getInstList().insert(InsertPt, N);
  1103. if (!BBI->use_empty())
  1104. TranslateMap[BBI] = N;
  1105. }
  1106. }
  1107. // Loop over all of the edges from PredBB to BB, changing them to branch
  1108. // to EdgeBB instead.
  1109. TerminatorInst *PredBBTI = PredBB->getTerminator();
  1110. for (unsigned i = 0, e = PredBBTI->getNumSuccessors(); i != e; ++i)
  1111. if (PredBBTI->getSuccessor(i) == BB) {
  1112. BB->removePredecessor(PredBB);
  1113. PredBBTI->setSuccessor(i, EdgeBB);
  1114. }
  1115. // Recurse, simplifying any other constants.
  1116. return FoldCondBranchOnPHI(BI, TD) | true;
  1117. }
  1118. return false;
  1119. }
  1120. /// FoldTwoEntryPHINode - Given a BB that starts with the specified two-entry
  1121. /// PHI node, see if we can eliminate it.
  1122. static bool FoldTwoEntryPHINode(PHINode *PN, const TargetData *TD) {
  1123. // Ok, this is a two entry PHI node. Check to see if this is a simple "if
  1124. // statement", which has a very simple dominance structure. Basically, we
  1125. // are trying to find the condition that is being branched on, which
  1126. // subsequently causes this merge to happen. We really want control
  1127. // dependence information for this check, but simplifycfg can't keep it up
  1128. // to date, and this catches most of the cases we care about anyway.
  1129. BasicBlock *BB = PN->getParent();
  1130. BasicBlock *IfTrue, *IfFalse;
  1131. Value *IfCond = GetIfCondition(BB, IfTrue, IfFalse);
  1132. if (!IfCond ||
  1133. // Don't bother if the branch will be constant folded trivially.
  1134. isa<ConstantInt>(IfCond))
  1135. return false;
  1136. // Okay, we found that we can merge this two-entry phi node into a select.
  1137. // Doing so would require us to fold *all* two entry phi nodes in this block.
  1138. // At some point this becomes non-profitable (particularly if the target
  1139. // doesn't support cmov's). Only do this transformation if there are two or
  1140. // fewer PHI nodes in this block.
  1141. unsigned NumPhis = 0;
  1142. for (BasicBlock::iterator I = BB->begin(); isa<PHINode>(I); ++NumPhis, ++I)
  1143. if (NumPhis > 2)
  1144. return false;
  1145. // Loop over the PHI's seeing if we can promote them all to select
  1146. // instructions. While we are at it, keep track of the instructions
  1147. // that need to be moved to the dominating block.
  1148. SmallPtrSet<Instruction*, 4> AggressiveInsts;
  1149. unsigned MaxCostVal0 = PHINodeFoldingThreshold,
  1150. MaxCostVal1 = PHINodeFoldingThreshold;
  1151. for (BasicBlock::iterator II = BB->begin(); isa<PHINode>(II);) {
  1152. PHINode *PN = cast<PHINode>(II++);
  1153. if (Value *V = SimplifyInstruction(PN, TD)) {
  1154. PN->replaceAllUsesWith(V);
  1155. PN->eraseFromParent();
  1156. continue;
  1157. }
  1158. if (!DominatesMergePoint(PN->getIncomingValue(0), BB, &AggressiveInsts,
  1159. MaxCostVal0) ||
  1160. !DominatesMergePoint(PN->getIncomingValue(1), BB, &AggressiveInsts,
  1161. MaxCostVal1))
  1162. return false;
  1163. }
  1164. // If we folded the the first phi, PN dangles at this point. Refresh it. If
  1165. // we ran out of PHIs then we simplified them all.
  1166. PN = dyn_cast<PHINode>(BB->begin());
  1167. if (PN == 0) return true;
  1168. // Don't fold i1 branches on PHIs which contain binary operators. These can
  1169. // often be turned into switches and other things.
  1170. if (PN->getType()->isIntegerTy(1) &&
  1171. (isa<BinaryOperator>(PN->getIncomingValue(0)) ||
  1172. isa<BinaryOperator>(PN->getIncomingValue(1)) ||
  1173. isa<BinaryOperator>(IfCond)))
  1174. return false;
  1175. // If we all PHI nodes are promotable, check to make sure that all
  1176. // instructions in the predecessor blocks can be promoted as well. If
  1177. // not, we won't be able to get rid of the control flow, so it's not
  1178. // worth promoting to select instructions.
  1179. BasicBlock *DomBlock = 0;
  1180. BasicBlock *IfBlock1 = PN->getIncomingBlock(0);
  1181. BasicBlock *IfBlock2 = PN->getIncomingBlock(1);
  1182. if (cast<BranchInst>(IfBlock1->getTerminator())->isConditional()) {
  1183. IfBlock1 = 0;
  1184. } else {
  1185. DomBlock = *pred_begin(IfBlock1);
  1186. for (BasicBlock::iterator I = IfBlock1->begin();!isa<TerminatorInst>(I);++I)
  1187. if (!AggressiveInsts.count(I) && !isa<DbgInfoIntrinsic>(I)) {
  1188. // This is not an aggressive instruction that we can promote.
  1189. // Because of this, we won't be able to get rid of the control
  1190. // flow, so the xform is not worth it.
  1191. return false;
  1192. }
  1193. }
  1194. if (cast<BranchInst>(IfBlock2->getTerminator())->isConditional()) {
  1195. IfBlock2 = 0;
  1196. } else {
  1197. DomBlock = *pred_begin(IfBlock2);
  1198. for (BasicBlock::iterator I = IfBlock2->begin();!isa<TerminatorInst>(I);++I)
  1199. if (!AggressiveInsts.count(I) && !isa<DbgInfoIntrinsic>(I)) {
  1200. // This is not an aggressive instruction that we can promote.
  1201. // Because of this, we won't be able to get rid of the control
  1202. // flow, so the xform is not worth it.
  1203. return false;
  1204. }
  1205. }
  1206. DEBUG(dbgs() << "FOUND IF CONDITION! " << *IfCond << " T: "
  1207. << IfTrue->getName() << " F: " << IfFalse->getName() << "\n");
  1208. // If we can still promote the PHI nodes after this gauntlet of tests,
  1209. // do all of the PHI's now.
  1210. Instruction *InsertPt = DomBlock->getTerminator();
  1211. IRBuilder<true, NoFolder> Builder(InsertPt);
  1212. // Move all 'aggressive' instructions, which are defined in the
  1213. // conditional parts of the if's up to the dominating block.
  1214. if (IfBlock1)
  1215. DomBlock->getInstList().splice(InsertPt,
  1216. IfBlock1->getInstList(), IfBlock1->begin(),
  1217. IfBlock1->getTerminator());
  1218. if (IfBlock2)
  1219. DomBlock->getInstList().splice(InsertPt,
  1220. IfBlock2->getInstList(), IfBlock2->begin(),
  1221. IfBlock2->getTerminator());
  1222. while (PHINode *PN = dyn_cast<PHINode>(BB->begin())) {
  1223. // Change the PHI node into a select instruction.
  1224. Value *TrueVal = PN->getIncomingValue(PN->getIncomingBlock(0) == IfFalse);
  1225. Value *FalseVal = PN->getIncomingValue(PN->getIncomingBlock(0) == IfTrue);
  1226. SelectInst *NV =
  1227. cast<SelectInst>(Builder.CreateSelect(IfCond, TrueVal, FalseVal, ""));
  1228. PN->replaceAllUsesWith(NV);
  1229. NV->takeName(PN);
  1230. PN->eraseFromParent();
  1231. }
  1232. // At this point, IfBlock1 and IfBlock2 are both empty, so our if statement
  1233. // has been flattened. Change DomBlock to jump directly to our new block to
  1234. // avoid other simplifycfg's kicking in on the diamond.
  1235. TerminatorInst *OldTI = DomBlock->getTerminator();
  1236. Builder.SetInsertPoint(OldTI);
  1237. Builder.CreateBr(BB);
  1238. OldTI->eraseFromParent();
  1239. return true;
  1240. }
  1241. /// SimplifyCondBranchToTwoReturns - If we found a conditional branch that goes
  1242. /// to two returning blocks, try to merge them together into one return,
  1243. /// introducing a select if the return values disagree.
  1244. static bool SimplifyCondBranchToTwoReturns(BranchInst *BI,
  1245. IRBuilder<> &Builder) {
  1246. assert(BI->isConditional() && "Must be a conditional branch");
  1247. BasicBlock *TrueSucc = BI->getSuccessor(0);
  1248. BasicBlock *FalseSucc = BI->getSuccessor(1);
  1249. ReturnInst *TrueRet = cast<ReturnInst>(TrueSucc->getTerminator());
  1250. ReturnInst *FalseRet = cast<ReturnInst>(FalseSucc->getTerminator());
  1251. // Check to ensure both blocks are empty (just a return) or optionally empty
  1252. // with PHI nodes. If there are other instructions, merging would cause extra
  1253. // computation on one path or the other.
  1254. if (!TrueSucc->getFirstNonPHIOrDbg()->isTerminator())
  1255. return false;
  1256. if (!FalseSucc->getFirstNonPHIOrDbg()->isTerminator())
  1257. return false;
  1258. Builder.SetInsertPoint(BI);
  1259. // Okay, we found a branch that is going to two return nodes. If
  1260. // there is no return value for this function, just change the
  1261. // branch into a return.
  1262. if (FalseRet->getNumOperands() == 0) {
  1263. TrueSucc->removePredecessor(BI->getParent());
  1264. FalseSucc->removePredecessor(BI->getParent());
  1265. Builder.CreateRetVoid();
  1266. EraseTerminatorInstAndDCECond(BI);
  1267. return true;
  1268. }
  1269. // Otherwise, figure out what the true and false return values are
  1270. // so we can insert a new select instruction.
  1271. Value *TrueValue = TrueRet->getReturnValue();
  1272. Value *FalseValue = FalseRet->getReturnValue();
  1273. // Unwrap any PHI nodes in the return blocks.
  1274. if (PHINode *TVPN = dyn_cast_or_null<PHINode>(TrueValue))
  1275. if (TVPN->getParent() == TrueSucc)
  1276. TrueValue = TVPN->getIncomingValueForBlock(BI->getParent());
  1277. if (PHINode *FVPN = dyn_cast_or_null<PHINode>(FalseValue))
  1278. if (FVPN->getParent() == FalseSucc)
  1279. FalseValue = FVPN->getIncomingValueForBlock(BI->getParent());
  1280. // In order for this transformation to be safe, we must be able to
  1281. // unconditionally execute both operands to the return. This is
  1282. // normally the case, but we could have a potentially-trapping
  1283. // constant expression that prevents this transformation from being
  1284. // safe.
  1285. if (ConstantExpr *TCV = dyn_cast_or_null<ConstantExpr>(TrueValue))
  1286. if (TCV->canTrap())
  1287. return false;
  1288. if (ConstantExpr *FCV = dyn_cast_or_null<ConstantExpr>(FalseValue))
  1289. if (FCV->canTrap())
  1290. return false;
  1291. // Okay, we collected all the mapped values and checked them for sanity, and
  1292. // defined to really do this transformation. First, update the CFG.
  1293. TrueSucc->removePredecessor(BI->getParent());
  1294. FalseSucc->removePredecessor(BI->getParent());
  1295. // Insert select instructions where needed.
  1296. Value *BrCond = BI->getCondition();
  1297. if (TrueValue) {
  1298. // Insert a select if the results differ.
  1299. if (TrueValue == FalseValue || isa<UndefValue>(FalseValue)) {
  1300. } else if (isa<UndefValue>(TrueValue)) {
  1301. TrueValue = FalseValue;
  1302. } else {
  1303. TrueValue = Builder.CreateSelect(BrCond, TrueValue,
  1304. FalseValue, "retval");
  1305. }
  1306. }
  1307. Value *RI = !TrueValue ?
  1308. Builder.CreateRetVoid() : Builder.CreateRet(TrueValue);
  1309. (void) RI;
  1310. DEBUG(dbgs() << "\nCHANGING BRANCH TO TWO RETURNS INTO SELECT:"
  1311. << "\n " << *BI << "NewRet = " << *RI
  1312. << "TRUEBLOCK: " << *TrueSucc << "FALSEBLOCK: "<< *FalseSucc);
  1313. EraseTerminatorInstAndDCECond(BI);
  1314. return true;
  1315. }
  1316. /// ExtractBranchMetadata - Given a conditional BranchInstruction, retrieve the
  1317. /// probabilities of the branch taking each edge. Fills in the two APInt
  1318. /// parameters and return true, or returns false if no or invalid metadata was
  1319. /// found.
  1320. static bool ExtractBranchMetadata(BranchInst *BI,
  1321. APInt &ProbTrue, APInt &ProbFalse) {
  1322. assert(BI->isConditional() &&
  1323. "Looking for probabilities on unconditional branch?");
  1324. MDNode *ProfileData = BI->getMetadata(LLVMContext::MD_prof);
  1325. if (!ProfileData || ProfileData->getNumOperands() != 3) return false;
  1326. ConstantInt *CITrue = dyn_cast<ConstantInt>(ProfileData->getOperand(1));
  1327. ConstantInt *CIFalse = dyn_cast<ConstantInt>(ProfileData->getOperand(2));
  1328. if (!CITrue || !CIFalse) return false;
  1329. ProbTrue = CITrue->getValue();
  1330. ProbFalse = CIFalse->getValue();
  1331. assert(ProbTrue.getBitWidth() == 32 && ProbFalse.getBitWidth() == 32 &&
  1332. "Branch probability metadata must be 32-bit integers");
  1333. return true;
  1334. }
  1335. /// MultiplyAndLosePrecision - Multiplies A and B, then returns the result. In
  1336. /// the event of overflow, logically-shifts all four inputs right until the
  1337. /// multiply fits.
  1338. static APInt MultiplyAndLosePrecision(APInt &A, APInt &B, APInt &C, APInt &D,
  1339. unsigned &BitsLost) {
  1340. BitsLost = 0;
  1341. bool Overflow = false;
  1342. APInt Result = A.umul_ov(B, Overflow);
  1343. if (Overflow) {
  1344. APInt MaxB = APInt::getMaxValue(A.getBitWidth()).udiv(A);
  1345. do {
  1346. B = B.lshr(1);
  1347. ++BitsLost;
  1348. } while (B.ugt(MaxB));
  1349. A = A.lshr(BitsLost);
  1350. C = C.lshr(BitsLost);
  1351. D = D.lshr(BitsLost);
  1352. Result = A * B;
  1353. }
  1354. return Result;
  1355. }
  1356. /// checkCSEInPredecessor - Return true if the given instruction is available
  1357. /// in its predecessor block. If yes, the instruction will be removed.
  1358. ///
  1359. bool checkCSEInPredecessor(Instruction *Inst, BasicBlock *PB) {
  1360. if (!isa<BinaryOperator>(Inst) && !isa<CmpInst>(Inst))
  1361. return false;
  1362. for (BasicBlock::iterator I = PB->begin(), E = PB->end(); I != E; I++) {
  1363. Instruction *PBI = &*I;
  1364. // Check whether Inst and PBI generate the same value.
  1365. if (Inst->isIdenticalTo(PBI)) {
  1366. Inst->replaceAllUsesWith(PBI);
  1367. Inst->eraseFromParent();
  1368. return true;
  1369. }
  1370. }
  1371. return false;
  1372. }
  1373. /// FoldBranchToCommonDest - If this basic block is simple enough, and if a
  1374. /// predecessor branches to us and one of our successors, fold the block into
  1375. /// the predecessor and use logical operations to pick the right destination.
  1376. bool llvm::FoldBranchToCommonDest(BranchInst *BI) {
  1377. BasicBlock *BB = BI->getParent();
  1378. Instruction *Cond = 0;
  1379. if (BI->isConditional())
  1380. Cond = dyn_cast<Instruction>(BI->getCondition());
  1381. else {
  1382. // For unconditional branch, check for a simple CFG pattern, where
  1383. // BB has a single predecessor and BB's successor is also its predecessor's
  1384. // successor. If such pattern exisits, check for CSE between BB and its
  1385. // predecessor.
  1386. if (BasicBlock *PB = BB->getSinglePredecessor())
  1387. if (BranchInst *PBI = dyn_cast<BranchInst>(PB->getTerminator()))
  1388. if (PBI->isConditional() &&
  1389. (BI->getSuccessor(0) == PBI->getSuccessor(0) ||
  1390. BI->getSuccessor(0) == PBI->getSuccessor(1))) {
  1391. for (BasicBlock::iterator I = BB->begin(), E = BB->end();
  1392. I != E; ) {
  1393. Instruction *Curr = I++;
  1394. if (isa<CmpInst>(Curr)) {
  1395. Cond = Curr;
  1396. break;
  1397. }
  1398. // Quit if we can't remove this instruction.
  1399. if (!checkCSEInPredecessor(Curr, PB))
  1400. return false;
  1401. }
  1402. }
  1403. if (Cond == 0)
  1404. return false;
  1405. }
  1406. if (Cond == 0 || (!isa<CmpInst>(Cond) && !isa<BinaryOperator>(Cond)) ||
  1407. Cond->getParent() != BB || !Cond->hasOneUse())
  1408. return false;
  1409. // Only allow this if the condition is a simple instruction that can be
  1410. // executed unconditionally. It must be in the same block as the branch, and
  1411. // must be at the front of the block.
  1412. BasicBlock::iterator FrontIt = BB->front();
  1413. // Ignore dbg intrinsics.
  1414. while (isa<DbgInfoIntrinsic>(FrontIt)) ++FrontIt;
  1415. // Allow a single instruction to be hoisted in addition to the compare
  1416. // that feeds the branch. We later ensure that any values that _it_ uses
  1417. // were also live in the predecessor, so that we don't unnecessarily create
  1418. // register pressure or inhibit out-of-order execution.
  1419. Instruction *BonusInst = 0;
  1420. if (&*FrontIt != Cond &&
  1421. FrontIt->hasOneUse() && *FrontIt->use_begin() == Cond &&
  1422. isSafeToSpeculativelyExecute(FrontIt)) {
  1423. BonusInst = &*FrontIt;
  1424. ++FrontIt;
  1425. // Ignore dbg intrinsics.
  1426. while (isa<DbgInfoIntrinsic>(FrontIt)) ++FrontIt;
  1427. }
  1428. // Only a single bonus inst is allowed.
  1429. if (&*FrontIt != Cond)
  1430. return false;
  1431. // Make sure the instruction after the condition is the cond branch.
  1432. BasicBlock::iterator CondIt = Cond; ++CondIt;
  1433. // Ingore dbg intrinsics.
  1434. while (isa<DbgInfoIntrinsic>(CondIt)) ++CondIt;
  1435. if (&*CondIt != BI)
  1436. return false;
  1437. // Cond is known to be a compare or binary operator. Check to make sure that
  1438. // neither operand is a potentially-trapping constant expression.
  1439. if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Cond->getOperand(0)))
  1440. if (CE->canTrap())
  1441. return false;
  1442. if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Cond->getOperand(1)))
  1443. if (CE->canTrap())
  1444. return false;
  1445. // Finally, don't infinitely unroll conditional loops.
  1446. BasicBlock *TrueDest = BI->getSuccessor(0);
  1447. BasicBlock *FalseDest = (BI->isConditional()) ? BI->getSuccessor(1) : 0;
  1448. if (TrueDest == BB || FalseDest == BB)
  1449. return false;
  1450. for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) {
  1451. BasicBlock *PredBlock = *PI;
  1452. BranchInst *PBI = dyn_cast<BranchInst>(PredBlock->getTerminator());
  1453. // Check that we have two conditional branches. If there is a PHI node in
  1454. // the common successor, verify that the same value flows in from both
  1455. // blocks.
  1456. SmallVector<PHINode*, 4> PHIs;
  1457. if (PBI == 0 || PBI->isUnconditional() ||
  1458. (BI->isConditional() &&
  1459. !SafeToMergeTerminators(BI, PBI)) ||
  1460. (!BI->isConditional() &&
  1461. !isProfitableToFoldUnconditional(BI, PBI, Cond, PHIs)))
  1462. continue;
  1463. // Determine if the two branches share a common destination.
  1464. Instruction::BinaryOps Opc;
  1465. bool InvertPredCond = false;
  1466. if (BI->isConditional()) {
  1467. if (PBI->getSuccessor(0) == TrueDest)
  1468. Opc = Instruction::Or;
  1469. else if (PBI->getSuccessor(1) == FalseDest)
  1470. Opc = Instruction::And;
  1471. else if (PBI->getSuccessor(0) == FalseDest)
  1472. Opc = Instruction::And, InvertPredCond = true;
  1473. else if (PBI->getSuccessor(1) == TrueDest)
  1474. Opc = Instruction::Or, InvertPredCond = true;
  1475. else
  1476. continue;
  1477. } else {
  1478. if (PBI->getSuccessor(0) != TrueDest && PBI->getSuccessor(1) != TrueDest)
  1479. continue;
  1480. }
  1481. // Ensure that any values used in the bonus instruction are also used
  1482. // by the terminator of the predecessor. This means that those values
  1483. // must already have been resolved, so we won't be inhibiting the
  1484. // out-of-order core by speculating them earlier.
  1485. if (BonusInst) {
  1486. // Collect the values used by the bonus inst
  1487. SmallPtrSet<Value*, 4> UsedValues;
  1488. for (Instruction::op_iterator OI = BonusInst->op_begin(),
  1489. OE = BonusInst->op_end(); OI != OE; ++OI) {
  1490. Value *V = *OI;
  1491. if (!isa<Constant>(V))
  1492. UsedValues.insert(V);
  1493. }
  1494. SmallVector<std::pair<Value*, unsigned>, 4> Worklist;
  1495. Worklist.push_back(std::make_pair(PBI->getOperand(0), 0));
  1496. // Walk up to four levels back up the use-def chain of the predecessor's
  1497. // terminator to see if all those values were used. The choice of four
  1498. // levels is arbitrary, to provide a compile-time-cost bound.
  1499. while (!Worklist.empty()) {
  1500. std::pair<Value*, unsigned> Pair = Worklist.back();
  1501. Worklist.pop_back();
  1502. if (Pair.second >= 4) continue;
  1503. UsedValues.erase(Pair.first);
  1504. if (UsedValues.empty()) break;
  1505. if (Instruction *I = dyn_cast<Instruction>(Pair.first)) {
  1506. for (Instruction::op_iterator OI = I->op_begin(), OE = I->op_end();
  1507. OI != OE; ++OI)
  1508. Worklist.push_back(std::make_pair(OI->get(), Pair.second+1));
  1509. }
  1510. }
  1511. if (!UsedValues.empty()) return false;
  1512. }
  1513. DEBUG(dbgs() << "FOLDING BRANCH TO COMMON DEST:\n" << *PBI << *BB);
  1514. IRBuilder<> Builder(PBI);
  1515. // If we need to invert the condition in the pred block to match, do so now.
  1516. if (InvertPredCond) {
  1517. Value *NewCond = PBI->getCondition();
  1518. if (NewCond->hasOneUse() && isa<CmpInst>(NewCond)) {
  1519. CmpInst *CI = cast<CmpInst>(NewCond);
  1520. CI->setPredicate(CI->getInversePredicate());
  1521. } else {
  1522. NewCond = Builder.CreateNot(NewCond,
  1523. PBI->getCondition()->getName()+".not");
  1524. }
  1525. PBI->setCondition(NewCond);
  1526. PBI->swapSuccessors();
  1527. }
  1528. // If we have a bonus inst, clone it into the predecessor block.
  1529. Instruction *NewBonus = 0;
  1530. if (BonusInst) {
  1531. NewBonus = BonusInst->clone();
  1532. PredBlock->getInstList().insert(PBI, NewBonus);
  1533. NewBonus->takeName(BonusInst);
  1534. BonusInst->setName(BonusInst->getName()+".old");
  1535. }
  1536. // Clone Cond into the predecessor basic block, and or/and the
  1537. // two conditions together.
  1538. Instruction *New = Cond->clone();
  1539. if (BonusInst) New->replaceUsesOfWith(BonusInst, NewBonus);
  1540. PredBlock->getInstList().insert(PBI, New);
  1541. New->takeName(Cond);
  1542. Cond->setName(New->getName()+".old");
  1543. if (BI->isConditional()) {
  1544. Instruction *NewCond =
  1545. cast<Instruction>(Builder.CreateBinOp(Opc, PBI->getCondition(),
  1546. New, "or.cond"));
  1547. PBI->setCondition(NewCond);
  1548. if (PBI->getSuccessor(0) == BB) {
  1549. AddPredecessorToBlock(TrueDest, PredBlock, BB);
  1550. PBI->setSuccessor(0, TrueDest);
  1551. }
  1552. if (PBI->getSuccessor(1) == BB) {
  1553. AddPredecessorToBlock(FalseDest, PredBlock, BB);
  1554. PBI->setSuccessor(1, FalseDest);
  1555. }
  1556. } else {
  1557. // Update PHI nodes in the common successors.
  1558. for (unsigned i = 0, e = PHIs.size(); i != e; ++i) {
  1559. ConstantInt *PBI_C = cast<ConstantInt>(
  1560. PHIs[i]->getIncomingValueForBlock(PBI->getParent()));
  1561. assert(PBI_C->getType()->isIntegerTy(1));
  1562. Instruction *MergedCond = 0;
  1563. if (PBI->getSuccessor(0) == TrueDest) {
  1564. // Create (PBI_Cond and PBI_C) or (!PBI_Cond and BI_Value)
  1565. // PBI_C is true: PBI_Cond or (!PBI_Cond and BI_Value)
  1566. // is false: !PBI_Cond and BI_Value
  1567. Instruction *NotCond =
  1568. cast<Instruction>(Builder.CreateNot(PBI->getCondition(),
  1569. "not.cond"));
  1570. MergedCond =
  1571. cast<Instruction>(Builder.CreateBinOp(Instruction::And,
  1572. NotCond, New,
  1573. "and.cond"));
  1574. if (PBI_C->isOne())
  1575. MergedCond =
  1576. cast<Instruction>(Builder.CreateBinOp(Instruction::Or,
  1577. PBI->getCondition(), MergedCond,
  1578. "or.cond"));
  1579. } else {
  1580. // Create (PBI_Cond and BI_Value) or (!PBI_Cond and PBI_C)
  1581. // PBI_C is true: (PBI_Cond and BI_Value) or (!PBI_Cond)
  1582. // is false: PBI_Cond and BI_Value
  1583. MergedCond =
  1584. cast<Instruction>(Builder.CreateBinOp(Instruction::And,
  1585. PBI->getCondition(), New,
  1586. "and.cond"));
  1587. if (PBI_C->isOne()) {
  1588. Instruction *NotCond =
  1589. cast<Instruction>(Builder.CreateNot(PBI->getCondition(),
  1590. "not.cond"));
  1591. MergedCond =
  1592. cast<Instruction>(Builder.CreateBinOp(Instruction::Or,
  1593. NotCond, MergedCond,
  1594. "or.cond"));
  1595. }
  1596. }
  1597. // Update PHI Node.
  1598. PHIs[i]->setIncomingValue(PHIs[i]->getBasicBlockIndex(PBI->getParent()),
  1599. MergedCond);
  1600. }
  1601. // Change PBI from Conditional to Unconditional.
  1602. BranchInst *New_PBI = BranchInst::Create(TrueDest, PBI);
  1603. EraseTerminatorInstAndDCECond(PBI);
  1604. PBI = New_PBI;
  1605. }
  1606. // TODO: If BB is reachable from all paths through PredBlock, then we
  1607. // could replace PBI's branch probabilities with BI's.
  1608. // Merge probability data into PredBlock's branch.
  1609. APInt A, B, C, D;
  1610. if (PBI->isConditional() && BI->isConditional() &&
  1611. ExtractBranchMetadata(PBI, C, D) && ExtractBranchMetadata(BI, A, B)) {
  1612. // Given IR which does:
  1613. // bbA:
  1614. // br i1 %x, label %bbB, label %bbC
  1615. // bbB:
  1616. // br i1 %y, label %bbD, label %bbC
  1617. // Let's call the probability that we take the edge from %bbA to %bbB
  1618. // 'a', from %bbA to %bbC, 'b', from %bbB to %bbD 'c' and from %bbB to
  1619. // %bbC probability 'd'.
  1620. //
  1621. // We transform the IR into:
  1622. // bbA:
  1623. // br i1 %z, label %bbD, label %bbC
  1624. // where the probability of going to %bbD is (a*c) and going to bbC is
  1625. // (b+a*d).
  1626. //
  1627. // Probabilities aren't stored as ratios directly. Using branch weights,
  1628. // we get:
  1629. // (a*c)% = A*C, (b+(a*d))% = A*D+B*C+B*D.
  1630. // In the event of overflow, we want to drop the LSB of the input
  1631. // probabilities.
  1632. unsigned BitsLost;
  1633. // Ignore overflow result on ProbTrue.
  1634. APInt ProbTrue = MultiplyAndLosePrecision(A, C, B, D, BitsLost);
  1635. APInt Tmp1 = MultiplyAndLosePrecision(B, D, A, C, BitsLost);
  1636. if (BitsLost) {
  1637. ProbTrue = ProbTrue.lshr(BitsLost*2);
  1638. }
  1639. APInt Tmp2 = MultiplyAndLosePrecision(A, D, C, B, BitsLost);
  1640. if (BitsLost) {
  1641. ProbTrue = ProbTrue.lshr(BitsLost*2);
  1642. Tmp1 = Tmp1.lshr(BitsLost*2);
  1643. }
  1644. APInt Tmp3 = MultiplyAndLosePrecision(B, C, A, D, BitsLost);
  1645. if (BitsLost) {
  1646. ProbTrue = ProbTrue.lshr(BitsLost*2);
  1647. Tmp1 = Tmp1.lshr(BitsLost*2);
  1648. Tmp2 = Tmp2.lshr(BitsLost*2);
  1649. }
  1650. bool Overflow1 = false, Overflow2 = false;
  1651. APInt Tmp4 = Tmp2.uadd_ov(Tmp3, Overflow1);
  1652. APInt ProbFalse = Tmp4.uadd_ov(Tmp1, Overflow2);
  1653. if (Overflow1 || Overflow2) {
  1654. ProbTrue = ProbTrue.lshr(1);
  1655. Tmp1 = Tmp1.lshr(1);
  1656. Tmp2 = Tmp2.lshr(1);
  1657. Tmp3 = Tmp3.lshr(1);
  1658. Tmp4 = Tmp2 + Tmp3;
  1659. ProbFalse = Tmp4 + Tmp1;
  1660. }
  1661. // The sum of branch weights must fit in 32-bits.
  1662. if (ProbTrue.isNegative() && ProbFalse.isNegative()) {
  1663. ProbTrue = ProbTrue.lshr(1);
  1664. ProbFalse = ProbFalse.lshr(1);
  1665. }
  1666. if (ProbTrue != ProbFalse) {
  1667. // Normalize the result.
  1668. APInt GCD = APIntOps::GreatestCommonDivisor(ProbTrue, ProbFalse);
  1669. ProbTrue = ProbTrue.udiv(GCD);
  1670. ProbFalse = ProbFalse.udiv(GCD);
  1671. MDBuilder MDB(BI->getContext());
  1672. MDNode *N = MDB.createBranchWeights(ProbTrue.getZExtValue(),
  1673. ProbFalse.getZExtValue());
  1674. PBI->setMetadata(LLVMContext::MD_prof, N);
  1675. } else {
  1676. PBI->setMetadata(LLVMContext::MD_prof, NULL);
  1677. }
  1678. } else {
  1679. PBI->setMetadata(LLVMContext::MD_prof, NULL);
  1680. }
  1681. // Copy any debug value intrinsics into the end of PredBlock.
  1682. for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
  1683. if (isa<DbgInfoIntrinsic>(*I))
  1684. I->clone()->insertBefore(PBI);
  1685. return true;
  1686. }
  1687. return false;
  1688. }
  1689. /// SimplifyCondBranchToCondBranch - If we have a conditional branch as a
  1690. /// predecessor of another block, this function tries to simplify it. We know
  1691. /// that PBI and BI are both conditional branches, and BI is in one of the
  1692. /// successor blocks of PBI - PBI branches to BI.
  1693. static bool SimplifyCondBranchToCondBranch(BranchInst *PBI, BranchInst *BI) {
  1694. assert(PBI->isConditional() && BI->isConditional());
  1695. BasicBlock *BB = BI->getParent();
  1696. // If this block ends with a branch instruction, and if there is a
  1697. // predecessor that ends on a branch of the same condition, make
  1698. // this conditional branch redundant.
  1699. if (PBI->getCondition() == BI->getCondition() &&
  1700. PBI->getSuccessor(0) != PBI->getSuccessor(1)) {
  1701. // Okay, the outcome of this conditional branch is statically
  1702. // knowable. If this block had a single pred, handle specially.
  1703. if (BB->getSinglePredecessor()) {
  1704. // Turn this into a branch on constant.
  1705. bool CondIsTrue = PBI->getSuccessor(0) == BB;
  1706. BI->setCondition(ConstantInt::get(Type::getInt1Ty(BB->getContext()),
  1707. CondIsTrue));
  1708. return true; // Nuke the branch on constant.
  1709. }
  1710. // Otherwise, if there are multiple predecessors, insert a PHI that merges
  1711. // in the constant and simplify the block result. Subsequent passes of
  1712. // simplifycfg will thread the block.
  1713. if (BlockIsSimpleEnoughToThreadThrough(BB)) {
  1714. pred_iterator PB = pred_begin(BB), PE = pred_end(BB);
  1715. PHINode *NewPN = PHINode::Create(Type::getInt1Ty(BB->getContext()),
  1716. std::distance(PB, PE),
  1717. BI->getCondition()->getName() + ".pr",
  1718. BB->begin());
  1719. // Okay, we're going to insert the PHI node. Since PBI is not the only
  1720. // predecessor, compute the PHI'd conditional value for all of the preds.
  1721. // Any predecessor where the condition is not computable we keep symbolic.
  1722. for (pred_iterator PI = PB; PI != PE; ++PI) {
  1723. BasicBlock *P = *PI;
  1724. if ((PBI = dyn_cast<BranchInst>(P->getTerminator())) &&
  1725. PBI != BI && PBI->isConditional() &&
  1726. PBI->getCondition() == BI->getCondition() &&
  1727. PBI->getSuccessor(0) != PBI->getSuccessor(1)) {
  1728. bool CondIsTrue = PBI->getSuccessor(0) == BB;
  1729. NewPN->addIncoming(ConstantInt::get(Type::getInt1Ty(BB->getContext()),
  1730. CondIsTrue), P);
  1731. } else {
  1732. NewPN->addIncoming(BI->getCondition(), P);
  1733. }
  1734. }
  1735. BI->setCondition(NewPN);
  1736. return true;
  1737. }
  1738. }
  1739. // If this is a conditional branch in an empty block, and if any
  1740. // predecessors is a conditional branch to one of our destinations,
  1741. // fold the conditions into logical ops and one cond br.
  1742. BasicBlock::iterator BBI = BB->begin();
  1743. // Ignore dbg intrinsics.
  1744. while (isa<DbgInfoIntrinsic>(BBI))
  1745. ++BBI;
  1746. if (&*BBI != BI)
  1747. return false;
  1748. if (ConstantExpr *CE = dyn_cast<ConstantExpr>(BI->getCondition()))
  1749. if (CE->canTrap())
  1750. return false;
  1751. int PBIOp, BIOp;
  1752. if (PBI->getSuccessor(0) == BI->getSuccessor(0))
  1753. PBIOp = BIOp = 0;
  1754. else if (PBI->getSuccessor(0) == BI->getSuccessor(1))
  1755. PBIOp = 0, BIOp = 1;
  1756. else if (PBI->getSuccessor(1) == BI->getSuccessor(0))
  1757. PBIOp = 1, BIOp = 0;
  1758. else if (PBI->getSuccessor(1) == BI->getSuccessor(1))
  1759. PBIOp = BIOp = 1;
  1760. else
  1761. return false;
  1762. // Check to make sure that the other destination of this branch
  1763. // isn't BB itself. If so, this is an infinite loop that will
  1764. // keep getting unwound.
  1765. if (PBI->getSuccessor(PBIOp) == BB)
  1766. return false;
  1767. // Do not perform this transformation if it would require
  1768. // insertion of a large number of select instructions. For targets
  1769. // without predication/cmovs, this is a big pessimization.
  1770. BasicBlock *CommonDest = PBI->getSuccessor(PBIOp);
  1771. unsigned NumPhis = 0;
  1772. for (BasicBlock::iterator II = CommonDest->begin();
  1773. isa<PHINode>(II); ++II, ++NumPhis)
  1774. if (NumPhis > 2) // Disable this xform.
  1775. return false;
  1776. // Finally, if everything is ok, fold the branches to logical ops.
  1777. BasicBlock *OtherDest = BI->getSuccessor(BIOp ^ 1);
  1778. DEBUG(dbgs() << "FOLDING BRs:" << *PBI->getParent()
  1779. << "AND: " << *BI->getParent());
  1780. // If OtherDest *is* BB, then BB is a basic block with a single conditional
  1781. // branch in it, where one edge (OtherDest) goes back to itself but the other
  1782. // exits. We don't *know* that the program avoids the infinite loop
  1783. // (even though that seems likely). If we do this xform naively, we'll end up
  1784. // recursively unpeeling the loop. Since we know that (after the xform is
  1785. // done) that the block *is* infinite if reached, we just make it an obviously
  1786. // infinite loop with no cond branch.
  1787. if (OtherDest == BB) {
  1788. // Insert it at the end of the function, because it's either code,
  1789. // or it won't matter if it's hot. :)
  1790. BasicBlock *InfLoopBlock = BasicBlock::Create(BB->getContext(),
  1791. "infloop", BB->getParent());
  1792. BranchInst::Create(InfLoopBlock, InfLoopBlock);
  1793. OtherDest = InfLoopBlock;
  1794. }
  1795. DEBUG(dbgs() << *PBI->getParent()->getParent());
  1796. // BI may have other predecessors. Because of this, we leave
  1797. // it alone, but modify PBI.
  1798. // Make sure we get to CommonDest on True&True directions.
  1799. Value *PBICond = PBI->getCondition();
  1800. IRBuilder<true, NoFolder> Builder(PBI);
  1801. if (PBIOp)
  1802. PBICond = Builder.CreateNot(PBICond, PBICond->getName()+".not");
  1803. Value *BICond = BI->getCondition();
  1804. if (BIOp)
  1805. BICond = Builder.CreateNot(BICond, BICond->getName()+".not");
  1806. // Merge the conditions.
  1807. Value *Cond = Builder.CreateOr(PBICond, BICond, "brmerge");
  1808. // Modify PBI to branch on the new condition to the new dests.
  1809. PBI->setCondition(Cond);
  1810. PBI->setSuccessor(0, CommonDest);
  1811. PBI->setSuccessor(1, OtherDest);
  1812. // OtherDest may have phi nodes. If so, add an entry from PBI's
  1813. // block that are identical to the entries for BI's block.
  1814. AddPredecessorToBlock(OtherDest, PBI->getParent(), BB);
  1815. // We know that the CommonDest already had an edge from PBI to
  1816. // it. If it has PHIs though, the PHIs may have different
  1817. // entries for BB and PBI's BB. If so, insert a select to make
  1818. // them agree.
  1819. PHINode *PN;
  1820. for (BasicBlock::iterator II = CommonDest->begin();
  1821. (PN = dyn_cast<PHINode>(II)); ++II) {
  1822. Value *BIV = PN->getIncomingValueForBlock(BB);
  1823. unsigned PBBIdx = PN->getBasicBlockIndex(PBI->getParent());
  1824. Value *PBIV = PN->getIncomingValue(PBBIdx);
  1825. if (BIV != PBIV) {
  1826. // Insert a select in PBI to pick the right value.
  1827. Value *NV = cast<SelectInst>
  1828. (Builder.CreateSelect(PBICond, PBIV, BIV, PBIV->getName()+".mux"));
  1829. PN->setIncomingValue(PBBIdx, NV);
  1830. }
  1831. }
  1832. DEBUG(dbgs() << "INTO: " << *PBI->getParent());
  1833. DEBUG(dbgs() << *PBI->getParent()->getParent());
  1834. // This basic block is probably dead. We know it has at least
  1835. // one fewer predecessor.
  1836. return true;
  1837. }
  1838. // SimplifyTerminatorOnSelect - Simplifies a terminator by replacing it with a
  1839. // branch to TrueBB if Cond is true or to FalseBB if Cond is false.
  1840. // Takes care of updating the successors and removing the old terminator.
  1841. // Also makes sure not to introduce new successors by assuming that edges to
  1842. // non-successor TrueBBs and FalseBBs aren't reachable.
  1843. static bool SimplifyTerminatorOnSelect(TerminatorInst *OldTerm, Value *Cond,
  1844. BasicBlock *TrueBB, BasicBlock *FalseBB){
  1845. // Remove any superfluous successor edges from the CFG.
  1846. // First, figure out which successors to preserve.
  1847. // If TrueBB and FalseBB are equal, only try to preserve one copy of that
  1848. // successor.
  1849. BasicBlock *KeepEdge1 = TrueBB;
  1850. BasicBlock *KeepEdge2 = TrueBB != FalseBB ? FalseBB : 0;
  1851. // Then remove the rest.
  1852. for (unsigned I = 0, E = OldTerm->getNumSuccessors(); I != E; ++I) {
  1853. BasicBlock *Succ = OldTerm->getSuccessor(I);
  1854. // Make sure only to keep exactly one copy of each edge.
  1855. if (Succ == KeepEdge1)
  1856. KeepEdge1 = 0;
  1857. else if (Succ == KeepEdge2)
  1858. KeepEdge2 = 0;
  1859. else
  1860. Succ->removePredecessor(OldTerm->getParent());
  1861. }
  1862. IRBuilder<> Builder(OldTerm);
  1863. Builder.SetCurrentDebugLocation(OldTerm->getDebugLoc());
  1864. // Insert an appropriate new terminator.
  1865. if ((KeepEdge1 == 0) && (KeepEdge2 == 0)) {
  1866. if (TrueBB == FalseBB)
  1867. // We were only looking for one successor, and it was present.
  1868. // Create an unconditional branch to it.
  1869. Builder.CreateBr(TrueBB);
  1870. else
  1871. // We found both of the successors we were looking for.
  1872. // Create a conditional branch sharing the condition of the select.
  1873. Builder.CreateCondBr(Cond, TrueBB, FalseBB);
  1874. } else if (KeepEdge1 && (KeepEdge2 || TrueBB == FalseBB)) {
  1875. // Neither of the selected blocks were successors, so this
  1876. // terminator must be unreachable.
  1877. new UnreachableInst(OldTerm->getContext(), OldTerm);
  1878. } else {
  1879. // One of the selected values was a successor, but the other wasn't.
  1880. // Insert an unconditional branch to the one that was found;
  1881. // the edge to the one that wasn't must be unreachable.
  1882. if (KeepEdge1 == 0)
  1883. // Only TrueBB was found.
  1884. Builder.CreateBr(TrueBB);
  1885. else
  1886. // Only FalseBB was found.
  1887. Builder.CreateBr(FalseBB);
  1888. }
  1889. EraseTerminatorInstAndDCECond(OldTerm);
  1890. return true;
  1891. }
  1892. // SimplifySwitchOnSelect - Replaces
  1893. // (switch (select cond, X, Y)) on constant X, Y
  1894. // with a branch - conditional if X and Y lead to distinct BBs,
  1895. // unconditional otherwise.
  1896. static bool SimplifySwitchOnSelect(SwitchInst *SI, SelectInst *Select) {
  1897. // Check for constant integer values in the select.
  1898. ConstantInt *TrueVal = dyn_cast<ConstantInt>(Select->getTrueValue());
  1899. ConstantInt *FalseVal = dyn_cast<ConstantInt>(Select->getFalseValue());
  1900. if (!TrueVal || !FalseVal)
  1901. return false;
  1902. // Find the relevant condition and destinations.
  1903. Value *Condition = Select->getCondition();
  1904. BasicBlock *TrueBB = SI->findCaseValue(TrueVal).getCaseSuccessor();
  1905. BasicBlock *FalseBB = SI->findCaseValue(FalseVal).getCaseSuccessor();
  1906. // Perform the actual simplification.
  1907. return SimplifyTerminatorOnSelect(SI, Condition, TrueBB, FalseBB);
  1908. }
  1909. // SimplifyIndirectBrOnSelect - Replaces
  1910. // (indirectbr (select cond, blockaddress(@fn, BlockA),
  1911. // blockaddress(@fn, BlockB)))
  1912. // with
  1913. // (br cond, BlockA, BlockB).
  1914. static bool SimplifyIndirectBrOnSelect(IndirectBrInst *IBI, SelectInst *SI) {
  1915. // Check that both operands of the select are block addresses.
  1916. BlockAddress *TBA = dyn_cast<BlockAddress>(SI->getTrueValue());
  1917. BlockAddress *FBA = dyn_cast<BlockAddress>(SI->getFalseValue());
  1918. if (!TBA || !FBA)
  1919. return false;
  1920. // Extract the actual blocks.
  1921. BasicBlock *TrueBB = TBA->getBasicBlock();
  1922. BasicBlock *FalseBB = FBA->getBasicBlock();
  1923. // Perform the actual simplification.
  1924. return SimplifyTerminatorOnSelect(IBI, SI->getCondition(), TrueBB, FalseBB);
  1925. }
  1926. /// TryToSimplifyUncondBranchWithICmpInIt - This is called when we find an icmp
  1927. /// instruction (a seteq/setne with a constant) as the only instruction in a
  1928. /// block that ends with an uncond branch. We are looking for a very specific
  1929. /// pattern that occurs when "A == 1 || A == 2 || A == 3" gets simplified. In
  1930. /// this case, we merge the first two "or's of icmp" into a switch, but then the
  1931. /// default value goes to an uncond block with a seteq in it, we get something
  1932. /// like:
  1933. ///
  1934. /// switch i8 %A, label %DEFAULT [ i8 1, label %end i8 2, label %end ]
  1935. /// DEFAULT:
  1936. /// %tmp = icmp eq i8 %A, 92
  1937. /// br label %end
  1938. /// end:
  1939. /// ... = phi i1 [ true, %entry ], [ %tmp, %DEFAULT ], [ true, %entry ]
  1940. ///
  1941. /// We prefer to split the edge to 'end' so that there is a true/false entry to
  1942. /// the PHI, merging the third icmp into the switch.
  1943. static bool TryToSimplifyUncondBranchWithICmpInIt(ICmpInst *ICI,
  1944. const TargetData *TD,
  1945. IRBuilder<> &Builder) {
  1946. BasicBlock *BB = ICI->getParent();
  1947. // If the block has any PHIs in it or the icmp has multiple uses, it is too
  1948. // complex.
  1949. if (isa<PHINode>(BB->begin()) || !ICI->hasOneUse()) return false;
  1950. Value *V = ICI->getOperand(0);
  1951. ConstantInt *Cst = cast<ConstantInt>(ICI->getOperand(1));
  1952. // The pattern we're looking for is where our only predecessor is a switch on
  1953. // 'V' and this block is the default case for the switch. In this case we can
  1954. // fold the compared value into the switch to simplify things.
  1955. BasicBlock *Pred = BB->getSinglePredecessor();
  1956. if (Pred == 0 || !isa<SwitchInst>(Pred->getTerminator())) return false;
  1957. SwitchInst *SI = cast<SwitchInst>(Pred->getTerminator());
  1958. if (SI->getCondition() != V)
  1959. return false;
  1960. // If BB is reachable on a non-default case, then we simply know the value of
  1961. // V in this block. Substitute it and constant fold the icmp instruction
  1962. // away.
  1963. if (SI->getDefaultDest() != BB) {
  1964. ConstantInt *VVal = SI->findCaseDest(BB);
  1965. assert(VVal && "Should have a unique destination value");
  1966. ICI->setOperand(0, VVal);
  1967. if (Value *V = SimplifyInstruction(ICI, TD)) {
  1968. ICI->replaceAllUsesWith(V);
  1969. ICI->eraseFromParent();
  1970. }
  1971. // BB is now empty, so it is likely to simplify away.
  1972. return SimplifyCFG(BB) | true;
  1973. }
  1974. // Ok, the block is reachable from the default dest. If the constant we're
  1975. // comparing exists in one of the other edges, then we can constant fold ICI
  1976. // and zap it.
  1977. if (SI->findCaseValue(Cst) != SI->case_default()) {
  1978. Value *V;
  1979. if (ICI->getPredicate() == ICmpInst::ICMP_EQ)
  1980. V = ConstantInt::getFalse(BB->getContext());
  1981. else
  1982. V = ConstantInt::getTrue(BB->getContext());
  1983. ICI->replaceAllUsesWith(V);
  1984. ICI->eraseFromParent();
  1985. // BB is now empty, so it is likely to simplify away.
  1986. return SimplifyCFG(BB) | true;
  1987. }
  1988. // The use of the icmp has to be in the 'end' block, by the only PHI node in
  1989. // the block.
  1990. BasicBlock *SuccBlock = BB->getTerminator()->getSuccessor(0);
  1991. PHINode *PHIUse = dyn_cast<PHINode>(ICI->use_back());
  1992. if (PHIUse == 0 || PHIUse != &SuccBlock->front() ||
  1993. isa<PHINode>(++BasicBlock::iterator(PHIUse)))
  1994. return false;
  1995. // If the icmp is a SETEQ, then the default dest gets false, the new edge gets
  1996. // true in the PHI.
  1997. Constant *DefaultCst = ConstantInt::getTrue(BB->getContext());
  1998. Constant *NewCst = ConstantInt::getFalse(BB->getContext());
  1999. if (ICI->getPredicate() == ICmpInst::ICMP_EQ)
  2000. std::swap(DefaultCst, NewCst);
  2001. // Replace ICI (which is used by the PHI for the default value) with true or
  2002. // false depending on if it is EQ or NE.
  2003. ICI->replaceAllUsesWith(DefaultCst);
  2004. ICI->eraseFromParent();
  2005. // Okay, the switch goes to this block on a default value. Add an edge from
  2006. // the switch to the merge point on the compared value.
  2007. BasicBlock *NewBB = BasicBlock::Create(BB->getContext(), "switch.edge",
  2008. BB->getParent(), BB);
  2009. SI->addCase(Cst, NewBB);
  2010. // NewBB branches to the phi block, add the uncond branch and the phi entry.
  2011. Builder.SetInsertPoint(NewBB);
  2012. Builder.SetCurrentDebugLocation(SI->getDebugLoc());
  2013. Builder.CreateBr(SuccBlock);
  2014. PHIUse->addIncoming(NewCst, NewBB);
  2015. return true;
  2016. }
  2017. /// SimplifyBranchOnICmpChain - The specified branch is a conditional branch.
  2018. /// Check to see if it is branching on an or/and chain of icmp instructions, and
  2019. /// fold it into a switch instruction if so.
  2020. static bool SimplifyBranchOnICmpChain(BranchInst *BI, const TargetData *TD,
  2021. IRBuilder<> &Builder) {
  2022. Instruction *Cond = dyn_cast<Instruction>(BI->getCondition());
  2023. if (Cond == 0) return false;
  2024. // Change br (X == 0 | X == 1), T, F into a switch instruction.
  2025. // If this is a bunch of seteq's or'd together, or if it's a bunch of
  2026. // 'setne's and'ed together, collect them.
  2027. Value *CompVal = 0;
  2028. std::vector<ConstantInt*> Values;
  2029. bool TrueWhenEqual = true;
  2030. Value *ExtraCase = 0;
  2031. unsigned UsedICmps = 0;
  2032. if (Cond->getOpcode() == Instruction::Or) {
  2033. CompVal = GatherConstantCompares(Cond, Values, ExtraCase, TD, true,
  2034. UsedICmps);
  2035. } else if (Cond->getOpcode() == Instruction::And) {
  2036. CompVal = GatherConstantCompares(Cond, Values, ExtraCase, TD, false,
  2037. UsedICmps);
  2038. TrueWhenEqual = false;
  2039. }
  2040. // If we didn't have a multiply compared value, fail.
  2041. if (CompVal == 0) return false;
  2042. // Avoid turning single icmps into a switch.
  2043. if (UsedICmps <= 1)
  2044. return false;
  2045. // There might be duplicate constants in the list, which the switch
  2046. // instruction can't handle, remove them now.
  2047. array_pod_sort(Values.begin(), Values.end(), ConstantIntSortPredicate);
  2048. Values.erase(std::unique(Values.begin(), Values.end()), Values.end());
  2049. // If Extra was used, we require at least two switch values to do the
  2050. // transformation. A switch with one value is just an cond branch.
  2051. if (ExtraCase && Values.size() < 2) return false;
  2052. // Figure out which block is which destination.
  2053. BasicBlock *DefaultBB = BI->getSuccessor(1);
  2054. BasicBlock *EdgeBB = BI->getSuccessor(0);
  2055. if (!TrueWhenEqual) std::swap(DefaultBB, EdgeBB);
  2056. BasicBlock *BB = BI->getParent();
  2057. DEBUG(dbgs() << "Converting 'icmp' chain with " << Values.size()
  2058. << " cases into SWITCH. BB is:\n" << *BB);
  2059. // If there are any extra values that couldn't be folded into the switch
  2060. // then we evaluate them with an explicit branch first. Split the block
  2061. // right before the condbr to handle it.
  2062. if (ExtraCase) {
  2063. BasicBlock *NewBB = BB->splitBasicBlock(BI, "switch.early.test");
  2064. // Remove the uncond branch added to the old block.
  2065. TerminatorInst *OldTI = BB->getTerminator();
  2066. Builder.SetInsertPoint(OldTI);
  2067. if (TrueWhenEqual)
  2068. Builder.CreateCondBr(ExtraCase, EdgeBB, NewBB);
  2069. else
  2070. Builder.CreateCondBr(ExtraCase, NewBB, EdgeBB);
  2071. OldTI->eraseFromParent();
  2072. // If there are PHI nodes in EdgeBB, then we need to add a new entry to them
  2073. // for the edge we just added.
  2074. AddPredecessorToBlock(EdgeBB, BB, NewBB);
  2075. DEBUG(dbgs() << " ** 'icmp' chain unhandled condition: " << *ExtraCase
  2076. << "\nEXTRABB = " << *BB);
  2077. BB = NewBB;
  2078. }
  2079. Builder.SetInsertPoint(BI);
  2080. // Convert pointer to int before we switch.
  2081. if (CompVal->getType()->isPointerTy()) {
  2082. assert(TD && "Cannot switch on pointer without TargetData");
  2083. CompVal = Builder.CreatePtrToInt(CompVal,
  2084. TD->getIntPtrType(CompVal->getContext()),
  2085. "magicptr");
  2086. }
  2087. // Create the new switch instruction now.
  2088. SwitchInst *New = Builder.CreateSwitch(CompVal, DefaultBB, Values.size());
  2089. // Add all of the 'cases' to the switch instruction.
  2090. for (unsigned i = 0, e = Values.size(); i != e; ++i)
  2091. New->addCase(Values[i], EdgeBB);
  2092. // We added edges from PI to the EdgeBB. As such, if there were any
  2093. // PHI nodes in EdgeBB, they need entries to be added corresponding to
  2094. // the number of edges added.
  2095. for (BasicBlock::iterator BBI = EdgeBB->begin();
  2096. isa<PHINode>(BBI); ++BBI) {
  2097. PHINode *PN = cast<PHINode>(BBI);
  2098. Value *InVal = PN->getIncomingValueForBlock(BB);
  2099. for (unsigned i = 0, e = Values.size()-1; i != e; ++i)
  2100. PN->addIncoming(InVal, BB);
  2101. }
  2102. // Erase the old branch instruction.
  2103. EraseTerminatorInstAndDCECond(BI);
  2104. DEBUG(dbgs() << " ** 'icmp' chain result is:\n" << *BB << '\n');
  2105. return true;
  2106. }
  2107. bool SimplifyCFGOpt::SimplifyResume(ResumeInst *RI, IRBuilder<> &Builder) {
  2108. // If this is a trivial landing pad that just continues unwinding the caught
  2109. // exception then zap the landing pad, turning its invokes into calls.
  2110. BasicBlock *BB = RI->getParent();
  2111. LandingPadInst *LPInst = dyn_cast<LandingPadInst>(BB->getFirstNonPHI());
  2112. if (RI->getValue() != LPInst)
  2113. // Not a landing pad, or the resume is not unwinding the exception that
  2114. // caused control to branch here.
  2115. return false;
  2116. // Check that there are no other instructions except for debug intrinsics.
  2117. BasicBlock::iterator I = LPInst, E = RI;
  2118. while (++I != E)
  2119. if (!isa<DbgInfoIntrinsic>(I))
  2120. return false;
  2121. // Turn all invokes that unwind here into calls and delete the basic block.
  2122. for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB); PI != PE;) {
  2123. InvokeInst *II = cast<InvokeInst>((*PI++)->getTerminator());
  2124. SmallVector<Value*, 8> Args(II->op_begin(), II->op_end() - 3);
  2125. // Insert a call instruction before the invoke.
  2126. CallInst *Call = CallInst::Create(II->getCalledValue(), Args, "", II);
  2127. Call->takeName(II);
  2128. Call->setCallingConv(II->getCallingConv());
  2129. Call->setAttributes(II->getAttributes());
  2130. Call->setDebugLoc(II->getDebugLoc());
  2131. // Anything that used the value produced by the invoke instruction now uses
  2132. // the value produced by the call instruction. Note that we do this even
  2133. // for void functions and calls with no uses so that the callgraph edge is
  2134. // updated.
  2135. II->replaceAllUsesWith(Call);
  2136. BB->removePredecessor(II->getParent());
  2137. // Insert a branch to the normal destination right before the invoke.
  2138. BranchInst::Create(II->getNormalDest(), II);
  2139. // Finally, delete the invoke instruction!
  2140. II->eraseFromParent();
  2141. }
  2142. // The landingpad is now unreachable. Zap it.
  2143. BB->eraseFromParent();
  2144. return true;
  2145. }
  2146. bool SimplifyCFGOpt::SimplifyReturn(ReturnInst *RI, IRBuilder<> &Builder) {
  2147. BasicBlock *BB = RI->getParent();
  2148. if (!BB->getFirstNonPHIOrDbg()->isTerminator()) return false;
  2149. // Find predecessors that end with branches.
  2150. SmallVector<BasicBlock*, 8> UncondBranchPreds;
  2151. SmallVector<BranchInst*, 8> CondBranchPreds;
  2152. for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) {
  2153. BasicBlock *P = *PI;
  2154. TerminatorInst *PTI = P->getTerminator();
  2155. if (BranchInst *BI = dyn_cast<BranchInst>(PTI)) {
  2156. if (BI->isUnconditional())
  2157. UncondBranchPreds.push_back(P);
  2158. else
  2159. CondBranchPreds.push_back(BI);
  2160. }
  2161. }
  2162. // If we found some, do the transformation!
  2163. if (!UncondBranchPreds.empty() && DupRet) {
  2164. while (!UncondBranchPreds.empty()) {
  2165. BasicBlock *Pred = UncondBranchPreds.pop_back_val();
  2166. DEBUG(dbgs() << "FOLDING: " << *BB
  2167. << "INTO UNCOND BRANCH PRED: " << *Pred);
  2168. (void)FoldReturnIntoUncondBranch(RI, BB, Pred);
  2169. }
  2170. // If we eliminated all predecessors of the block, delete the block now.
  2171. if (pred_begin(BB) == pred_end(BB))
  2172. // We know there are no successors, so just nuke the block.
  2173. BB->eraseFromParent();
  2174. return true;
  2175. }
  2176. // Check out all of the conditional branches going to this return
  2177. // instruction. If any of them just select between returns, change the
  2178. // branch itself into a select/return pair.
  2179. while (!CondBranchPreds.empty()) {
  2180. BranchInst *BI = CondBranchPreds.pop_back_val();
  2181. // Check to see if the non-BB successor is also a return block.
  2182. if (isa<ReturnInst>(BI->getSuccessor(0)->getTerminator()) &&
  2183. isa<ReturnInst>(BI->getSuccessor(1)->getTerminator()) &&
  2184. SimplifyCondBranchToTwoReturns(BI, Builder))
  2185. return true;
  2186. }
  2187. return false;
  2188. }
  2189. bool SimplifyCFGOpt::SimplifyUnreachable(UnreachableInst *UI) {
  2190. BasicBlock *BB = UI->getParent();
  2191. bool Changed = false;
  2192. // If there are any instructions immediately before the unreachable that can
  2193. // be removed, do so.
  2194. while (UI != BB->begin()) {
  2195. BasicBlock::iterator BBI = UI;
  2196. --BBI;
  2197. // Do not delete instructions that can have side effects which might cause
  2198. // the unreachable to not be reachable; specifically, calls and volatile
  2199. // operations may have this effect.
  2200. if (isa<CallInst>(BBI) && !isa<DbgInfoIntrinsic>(BBI)) break;
  2201. if (BBI->mayHaveSideEffects()) {
  2202. if (StoreInst *SI = dyn_cast<StoreInst>(BBI)) {
  2203. if (SI->isVolatile())
  2204. break;
  2205. } else if (LoadInst *LI = dyn_cast<LoadInst>(BBI)) {
  2206. if (LI->isVolatile())
  2207. break;
  2208. } else if (AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(BBI)) {
  2209. if (RMWI->isVolatile())
  2210. break;
  2211. } else if (AtomicCmpXchgInst *CXI = dyn_cast<AtomicCmpXchgInst>(BBI)) {
  2212. if (CXI->isVolatile())
  2213. break;
  2214. } else if (!isa<FenceInst>(BBI) && !isa<VAArgInst>(BBI) &&
  2215. !isa<LandingPadInst>(BBI)) {
  2216. break;
  2217. }
  2218. // Note that deleting LandingPad's here is in fact okay, although it
  2219. // involves a bit of subtle reasoning. If this inst is a LandingPad,
  2220. // all the predecessors of this block will be the unwind edges of Invokes,
  2221. // and we can therefore guarantee this block will be erased.
  2222. }
  2223. // Delete this instruction (any uses are guaranteed to be dead)
  2224. if (!BBI->use_empty())
  2225. BBI->replaceAllUsesWith(UndefValue::get(BBI->getType()));
  2226. BBI->eraseFromParent();
  2227. Changed = true;
  2228. }
  2229. // If the unreachable instruction is the first in the block, take a gander
  2230. // at all of the predecessors of this instruction, and simplify them.
  2231. if (&BB->front() != UI) return Changed;
  2232. SmallVector<BasicBlock*, 8> Preds(pred_begin(BB), pred_end(BB));
  2233. for (unsigned i = 0, e = Preds.size(); i != e; ++i) {
  2234. TerminatorInst *TI = Preds[i]->getTerminator();
  2235. IRBuilder<> Builder(TI);
  2236. if (BranchInst *BI = dyn_cast<BranchInst>(TI)) {
  2237. if (BI->isUnconditional()) {
  2238. if (BI->getSuccessor(0) == BB) {
  2239. new UnreachableInst(TI->getContext(), TI);
  2240. TI->eraseFromParent();
  2241. Changed = true;
  2242. }
  2243. } else {
  2244. if (BI->getSuccessor(0) == BB) {
  2245. Builder.CreateBr(BI->getSuccessor(1));
  2246. EraseTerminatorInstAndDCECond(BI);
  2247. } else if (BI->getSuccessor(1) == BB) {
  2248. Builder.CreateBr(BI->getSuccessor(0));
  2249. EraseTerminatorInstAndDCECond(BI);
  2250. Changed = true;
  2251. }
  2252. }
  2253. } else if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) {
  2254. for (SwitchInst::CaseIt i = SI->case_begin(), e = SI->case_end();
  2255. i != e; ++i)
  2256. if (i.getCaseSuccessor() == BB) {
  2257. BB->removePredecessor(SI->getParent());
  2258. SI->removeCase(i);
  2259. --i; --e;
  2260. Changed = true;
  2261. }
  2262. // If the default value is unreachable, figure out the most popular
  2263. // destination and make it the default.
  2264. if (SI->getDefaultDest() == BB) {
  2265. std::map<BasicBlock*, std::pair<unsigned, unsigned> > Popularity;
  2266. for (SwitchInst::CaseIt i = SI->case_begin(), e = SI->case_end();
  2267. i != e; ++i) {
  2268. std::pair<unsigned, unsigned> &entry =
  2269. Popularity[i.getCaseSuccessor()];
  2270. if (entry.first == 0) {
  2271. entry.first = 1;
  2272. entry.second = i.getCaseIndex();
  2273. } else {
  2274. entry.first++;
  2275. }
  2276. }
  2277. // Find the most popular block.
  2278. unsigned MaxPop = 0;
  2279. unsigned MaxIndex = 0;
  2280. BasicBlock *MaxBlock = 0;
  2281. for (std::map<BasicBlock*, std::pair<unsigned, unsigned> >::iterator
  2282. I = Popularity.begin(), E = Popularity.end(); I != E; ++I) {
  2283. if (I->second.first > MaxPop ||
  2284. (I->second.first == MaxPop && MaxIndex > I->second.second)) {
  2285. MaxPop = I->second.first;
  2286. MaxIndex = I->second.second;
  2287. MaxBlock = I->first;
  2288. }
  2289. }
  2290. if (MaxBlock) {
  2291. // Make this the new default, allowing us to delete any explicit
  2292. // edges to it.
  2293. SI->setDefaultDest(MaxBlock);
  2294. Changed = true;
  2295. // If MaxBlock has phinodes in it, remove MaxPop-1 entries from
  2296. // it.
  2297. if (isa<PHINode>(MaxBlock->begin()))
  2298. for (unsigned i = 0; i != MaxPop-1; ++i)
  2299. MaxBlock->removePredecessor(SI->getParent());
  2300. for (SwitchInst::CaseIt i = SI->case_begin(), e = SI->case_end();
  2301. i != e; ++i)
  2302. if (i.getCaseSuccessor() == MaxBlock) {
  2303. SI->removeCase(i);
  2304. --i; --e;
  2305. }
  2306. }
  2307. }
  2308. } else if (InvokeInst *II = dyn_cast<InvokeInst>(TI)) {
  2309. if (II->getUnwindDest() == BB) {
  2310. // Convert the invoke to a call instruction. This would be a good
  2311. // place to note that the call does not throw though.
  2312. BranchInst *BI = Builder.CreateBr(II->getNormalDest());
  2313. II->removeFromParent(); // Take out of symbol table
  2314. // Insert the call now...
  2315. SmallVector<Value*, 8> Args(II->op_begin(), II->op_end()-3);
  2316. Builder.SetInsertPoint(BI);
  2317. CallInst *CI = Builder.CreateCall(II->getCalledValue(),
  2318. Args, II->getName());
  2319. CI->setCallingConv(II->getCallingConv());
  2320. CI->setAttributes(II->getAttributes());
  2321. // If the invoke produced a value, the call does now instead.
  2322. II->replaceAllUsesWith(CI);
  2323. delete II;
  2324. Changed = true;
  2325. }
  2326. }
  2327. }
  2328. // If this block is now dead, remove it.
  2329. if (pred_begin(BB) == pred_end(BB) &&
  2330. BB != &BB->getParent()->getEntryBlock()) {
  2331. // We know there are no successors, so just nuke the block.
  2332. BB->eraseFromParent();
  2333. return true;
  2334. }
  2335. return Changed;
  2336. }
  2337. /// TurnSwitchRangeIntoICmp - Turns a switch with that contains only a
  2338. /// integer range comparison into a sub, an icmp and a branch.
  2339. static bool TurnSwitchRangeIntoICmp(SwitchInst *SI, IRBuilder<> &Builder) {
  2340. assert(SI->getNumCases() > 1 && "Degenerate switch?");
  2341. // Make sure all cases point to the same destination and gather the values.
  2342. SmallVector<ConstantInt *, 16> Cases;
  2343. SwitchInst::CaseIt I = SI->case_begin();
  2344. Cases.push_back(I.getCaseValue());
  2345. SwitchInst::CaseIt PrevI = I++;
  2346. for (SwitchInst::CaseIt E = SI->case_end(); I != E; PrevI = I++) {
  2347. if (PrevI.getCaseSuccessor() != I.getCaseSuccessor())
  2348. return false;
  2349. Cases.push_back(I.getCaseValue());
  2350. }
  2351. assert(Cases.size() == SI->getNumCases() && "Not all cases gathered");
  2352. // Sort the case values, then check if they form a range we can transform.
  2353. array_pod_sort(Cases.begin(), Cases.end(), ConstantIntSortPredicate);
  2354. for (unsigned I = 1, E = Cases.size(); I != E; ++I) {
  2355. if (Cases[I-1]->getValue() != Cases[I]->getValue()+1)
  2356. return false;
  2357. }
  2358. Constant *Offset = ConstantExpr::getNeg(Cases.back());
  2359. Constant *NumCases = ConstantInt::get(Offset->getType(), SI->getNumCases());
  2360. Value *Sub = SI->getCondition();
  2361. if (!Offset->isNullValue())
  2362. Sub = Builder.CreateAdd(Sub, Offset, Sub->getName()+".off");
  2363. Value *Cmp = Builder.CreateICmpULT(Sub, NumCases, "switch");
  2364. Builder.CreateCondBr(
  2365. Cmp, SI->case_begin().getCaseSuccessor(), SI->getDefaultDest());
  2366. // Prune obsolete incoming values off the successor's PHI nodes.
  2367. for (BasicBlock::iterator BBI = SI->case_begin().getCaseSuccessor()->begin();
  2368. isa<PHINode>(BBI); ++BBI) {
  2369. for (unsigned I = 0, E = SI->getNumCases()-1; I != E; ++I)
  2370. cast<PHINode>(BBI)->removeIncomingValue(SI->getParent());
  2371. }
  2372. SI->eraseFromParent();
  2373. return true;
  2374. }
  2375. /// EliminateDeadSwitchCases - Compute masked bits for the condition of a switch
  2376. /// and use it to remove dead cases.
  2377. static bool EliminateDeadSwitchCases(SwitchInst *SI) {
  2378. Value *Cond = SI->getCondition();
  2379. unsigned Bits = cast<IntegerType>(Cond->getType())->getBitWidth();
  2380. APInt KnownZero(Bits, 0), KnownOne(Bits, 0);
  2381. ComputeMaskedBits(Cond, KnownZero, KnownOne);
  2382. // Gather dead cases.
  2383. SmallVector<ConstantInt*, 8> DeadCases;
  2384. for (SwitchInst::CaseIt I = SI->case_begin(), E = SI->case_end(); I != E; ++I) {
  2385. if ((I.getCaseValue()->getValue() & KnownZero) != 0 ||
  2386. (I.getCaseValue()->getValue() & KnownOne) != KnownOne) {
  2387. DeadCases.push_back(I.getCaseValue());
  2388. DEBUG(dbgs() << "SimplifyCFG: switch case '"
  2389. << I.getCaseValue() << "' is dead.\n");
  2390. }
  2391. }
  2392. // Remove dead cases from the switch.
  2393. for (unsigned I = 0, E = DeadCases.size(); I != E; ++I) {
  2394. SwitchInst::CaseIt Case = SI->findCaseValue(DeadCases[I]);
  2395. assert(Case != SI->case_default() &&
  2396. "Case was not found. Probably mistake in DeadCases forming.");
  2397. // Prune unused values from PHI nodes.
  2398. Case.getCaseSuccessor()->removePredecessor(SI->getParent());
  2399. SI->removeCase(Case);
  2400. }
  2401. return !DeadCases.empty();
  2402. }
  2403. /// FindPHIForConditionForwarding - If BB would be eligible for simplification
  2404. /// by TryToSimplifyUncondBranchFromEmptyBlock (i.e. it is empty and terminated
  2405. /// by an unconditional branch), look at the phi node for BB in the successor
  2406. /// block and see if the incoming value is equal to CaseValue. If so, return
  2407. /// the phi node, and set PhiIndex to BB's index in the phi node.
  2408. static PHINode *FindPHIForConditionForwarding(ConstantInt *CaseValue,
  2409. BasicBlock *BB,
  2410. int *PhiIndex) {
  2411. if (BB->getFirstNonPHIOrDbg() != BB->getTerminator())
  2412. return NULL; // BB must be empty to be a candidate for simplification.
  2413. if (!BB->getSinglePredecessor())
  2414. return NULL; // BB must be dominated by the switch.
  2415. BranchInst *Branch = dyn_cast<BranchInst>(BB->getTerminator());
  2416. if (!Branch || !Branch->isUnconditional())
  2417. return NULL; // Terminator must be unconditional branch.
  2418. BasicBlock *Succ = Branch->getSuccessor(0);
  2419. BasicBlock::iterator I = Succ->begin();
  2420. while (PHINode *PHI = dyn_cast<PHINode>(I++)) {
  2421. int Idx = PHI->getBasicBlockIndex(BB);
  2422. assert(Idx >= 0 && "PHI has no entry for predecessor?");
  2423. Value *InValue = PHI->getIncomingValue(Idx);
  2424. if (InValue != CaseValue) continue;
  2425. *PhiIndex = Idx;
  2426. return PHI;
  2427. }
  2428. return NULL;
  2429. }
  2430. /// ForwardSwitchConditionToPHI - Try to forward the condition of a switch
  2431. /// instruction to a phi node dominated by the switch, if that would mean that
  2432. /// some of the destination blocks of the switch can be folded away.
  2433. /// Returns true if a change is made.
  2434. static bool ForwardSwitchConditionToPHI(SwitchInst *SI) {
  2435. typedef DenseMap<PHINode*, SmallVector<int,4> > ForwardingNodesMap;
  2436. ForwardingNodesMap ForwardingNodes;
  2437. for (SwitchInst::CaseIt I = SI->case_begin(), E = SI->case_end(); I != E; ++I) {
  2438. ConstantInt *CaseValue = I.getCaseValue();
  2439. BasicBlock *CaseDest = I.getCaseSuccessor();
  2440. int PhiIndex;
  2441. PHINode *PHI = FindPHIForConditionForwarding(CaseValue, CaseDest,
  2442. &PhiIndex);
  2443. if (!PHI) continue;
  2444. ForwardingNodes[PHI].push_back(PhiIndex);
  2445. }
  2446. bool Changed = false;
  2447. for (ForwardingNodesMap::iterator I = ForwardingNodes.begin(),
  2448. E = ForwardingNodes.end(); I != E; ++I) {
  2449. PHINode *Phi = I->first;
  2450. SmallVector<int,4> &Indexes = I->second;
  2451. if (Indexes.size() < 2) continue;
  2452. for (size_t I = 0, E = Indexes.size(); I != E; ++I)
  2453. Phi->setIncomingValue(Indexes[I], SI->getCondition());
  2454. Changed = true;
  2455. }
  2456. return Changed;
  2457. }
  2458. bool SimplifyCFGOpt::SimplifySwitch(SwitchInst *SI, IRBuilder<> &Builder) {
  2459. // If this switch is too complex to want to look at, ignore it.
  2460. if (!isValueEqualityComparison(SI))
  2461. return false;
  2462. BasicBlock *BB = SI->getParent();
  2463. // If we only have one predecessor, and if it is a branch on this value,
  2464. // see if that predecessor totally determines the outcome of this switch.
  2465. if (BasicBlock *OnlyPred = BB->getSinglePredecessor())
  2466. if (SimplifyEqualityComparisonWithOnlyPredecessor(SI, OnlyPred, Builder))
  2467. return SimplifyCFG(BB) | true;
  2468. Value *Cond = SI->getCondition();
  2469. if (SelectInst *Select = dyn_cast<SelectInst>(Cond))
  2470. if (SimplifySwitchOnSelect(SI, Select))
  2471. return SimplifyCFG(BB) | true;
  2472. // If the block only contains the switch, see if we can fold the block
  2473. // away into any preds.
  2474. BasicBlock::iterator BBI = BB->begin();
  2475. // Ignore dbg intrinsics.
  2476. while (isa<DbgInfoIntrinsic>(BBI))
  2477. ++BBI;
  2478. if (SI == &*BBI)
  2479. if (FoldValueComparisonIntoPredecessors(SI, Builder))
  2480. return SimplifyCFG(BB) | true;
  2481. // Try to transform the switch into an icmp and a branch.
  2482. if (TurnSwitchRangeIntoICmp(SI, Builder))
  2483. return SimplifyCFG(BB) | true;
  2484. // Remove unreachable cases.
  2485. if (EliminateDeadSwitchCases(SI))
  2486. return SimplifyCFG(BB) | true;
  2487. if (ForwardSwitchConditionToPHI(SI))
  2488. return SimplifyCFG(BB) | true;
  2489. return false;
  2490. }
  2491. bool SimplifyCFGOpt::SimplifyIndirectBr(IndirectBrInst *IBI) {
  2492. BasicBlock *BB = IBI->getParent();
  2493. bool Changed = false;
  2494. // Eliminate redundant destinations.
  2495. SmallPtrSet<Value *, 8> Succs;
  2496. for (unsigned i = 0, e = IBI->getNumDestinations(); i != e; ++i) {
  2497. BasicBlock *Dest = IBI->getDestination(i);
  2498. if (!Dest->hasAddressTaken() || !Succs.insert(Dest)) {
  2499. Dest->removePredecessor(BB);
  2500. IBI->removeDestination(i);
  2501. --i; --e;
  2502. Changed = true;
  2503. }
  2504. }
  2505. if (IBI->getNumDestinations() == 0) {
  2506. // If the indirectbr has no successors, change it to unreachable.
  2507. new UnreachableInst(IBI->getContext(), IBI);
  2508. EraseTerminatorInstAndDCECond(IBI);
  2509. return true;
  2510. }
  2511. if (IBI->getNumDestinations() == 1) {
  2512. // If the indirectbr has one successor, change it to a direct branch.
  2513. BranchInst::Create(IBI->getDestination(0), IBI);
  2514. EraseTerminatorInstAndDCECond(IBI);
  2515. return true;
  2516. }
  2517. if (SelectInst *SI = dyn_cast<SelectInst>(IBI->getAddress())) {
  2518. if (SimplifyIndirectBrOnSelect(IBI, SI))
  2519. return SimplifyCFG(BB) | true;
  2520. }
  2521. return Changed;
  2522. }
  2523. bool SimplifyCFGOpt::SimplifyUncondBranch(BranchInst *BI, IRBuilder<> &Builder){
  2524. BasicBlock *BB = BI->getParent();
  2525. // If the Terminator is the only non-phi instruction, simplify the block.
  2526. BasicBlock::iterator I = BB->getFirstNonPHIOrDbgOrLifetime();
  2527. if (I->isTerminator() && BB != &BB->getParent()->getEntryBlock() &&
  2528. TryToSimplifyUncondBranchFromEmptyBlock(BB))
  2529. return true;
  2530. // If the only instruction in the block is a seteq/setne comparison
  2531. // against a constant, try to simplify the block.
  2532. if (ICmpInst *ICI = dyn_cast<ICmpInst>(I))
  2533. if (ICI->isEquality() && isa<ConstantInt>(ICI->getOperand(1))) {
  2534. for (++I; isa<DbgInfoIntrinsic>(I); ++I)
  2535. ;
  2536. if (I->isTerminator() &&
  2537. TryToSimplifyUncondBranchWithICmpInIt(ICI, TD, Builder))
  2538. return true;
  2539. }
  2540. // If this basic block is ONLY a compare and a branch, and if a predecessor
  2541. // branches to us and our successor, fold the comparison into the
  2542. // predecessor and use logical operations to update the incoming value
  2543. // for PHI nodes in common successor.
  2544. if (FoldBranchToCommonDest(BI))
  2545. return SimplifyCFG(BB) | true;
  2546. return false;
  2547. }
  2548. bool SimplifyCFGOpt::SimplifyCondBranch(BranchInst *BI, IRBuilder<> &Builder) {
  2549. BasicBlock *BB = BI->getParent();
  2550. // Conditional branch
  2551. if (isValueEqualityComparison(BI)) {
  2552. // If we only have one predecessor, and if it is a branch on this value,
  2553. // see if that predecessor totally determines the outcome of this
  2554. // switch.
  2555. if (BasicBlock *OnlyPred = BB->getSinglePredecessor())
  2556. if (SimplifyEqualityComparisonWithOnlyPredecessor(BI, OnlyPred, Builder))
  2557. return SimplifyCFG(BB) | true;
  2558. // This block must be empty, except for the setcond inst, if it exists.
  2559. // Ignore dbg intrinsics.
  2560. BasicBlock::iterator I = BB->begin();
  2561. // Ignore dbg intrinsics.
  2562. while (isa<DbgInfoIntrinsic>(I))
  2563. ++I;
  2564. if (&*I == BI) {
  2565. if (FoldValueComparisonIntoPredecessors(BI, Builder))
  2566. return SimplifyCFG(BB) | true;
  2567. } else if (&*I == cast<Instruction>(BI->getCondition())){
  2568. ++I;
  2569. // Ignore dbg intrinsics.
  2570. while (isa<DbgInfoIntrinsic>(I))
  2571. ++I;
  2572. if (&*I == BI && FoldValueComparisonIntoPredecessors(BI, Builder))
  2573. return SimplifyCFG(BB) | true;
  2574. }
  2575. }
  2576. // Try to turn "br (X == 0 | X == 1), T, F" into a switch instruction.
  2577. if (SimplifyBranchOnICmpChain(BI, TD, Builder))
  2578. return true;
  2579. // If this basic block is ONLY a compare and a branch, and if a predecessor
  2580. // branches to us and one of our successors, fold the comparison into the
  2581. // predecessor and use logical operations to pick the right destination.
  2582. if (FoldBranchToCommonDest(BI))
  2583. return SimplifyCFG(BB) | true;
  2584. // We have a conditional branch to two blocks that are only reachable
  2585. // from BI. We know that the condbr dominates the two blocks, so see if
  2586. // there is any identical code in the "then" and "else" blocks. If so, we
  2587. // can hoist it up to the branching block.
  2588. if (BI->getSuccessor(0)->getSinglePredecessor() != 0) {
  2589. if (BI->getSuccessor(1)->getSinglePredecessor() != 0) {
  2590. if (HoistThenElseCodeToIf(BI))
  2591. return SimplifyCFG(BB) | true;
  2592. } else {
  2593. // If Successor #1 has multiple preds, we may be able to conditionally
  2594. // execute Successor #0 if it branches to successor #1.
  2595. TerminatorInst *Succ0TI = BI->getSuccessor(0)->getTerminator();
  2596. if (Succ0TI->getNumSuccessors() == 1 &&
  2597. Succ0TI->getSuccessor(0) == BI->getSuccessor(1))
  2598. if (SpeculativelyExecuteBB(BI, BI->getSuccessor(0)))
  2599. return SimplifyCFG(BB) | true;
  2600. }
  2601. } else if (BI->getSuccessor(1)->getSinglePredecessor() != 0) {
  2602. // If Successor #0 has multiple preds, we may be able to conditionally
  2603. // execute Successor #1 if it branches to successor #0.
  2604. TerminatorInst *Succ1TI = BI->getSuccessor(1)->getTerminator();
  2605. if (Succ1TI->getNumSuccessors() == 1 &&
  2606. Succ1TI->getSuccessor(0) == BI->getSuccessor(0))
  2607. if (SpeculativelyExecuteBB(BI, BI->getSuccessor(1)))
  2608. return SimplifyCFG(BB) | true;
  2609. }
  2610. // If this is a branch on a phi node in the current block, thread control
  2611. // through this block if any PHI node entries are constants.
  2612. if (PHINode *PN = dyn_cast<PHINode>(BI->getCondition()))
  2613. if (PN->getParent() == BI->getParent())
  2614. if (FoldCondBranchOnPHI(BI, TD))
  2615. return SimplifyCFG(BB) | true;
  2616. // Scan predecessor blocks for conditional branches.
  2617. for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI)
  2618. if (BranchInst *PBI = dyn_cast<BranchInst>((*PI)->getTerminator()))
  2619. if (PBI != BI && PBI->isConditional())
  2620. if (SimplifyCondBranchToCondBranch(PBI, BI))
  2621. return SimplifyCFG(BB) | true;
  2622. return false;
  2623. }
  2624. /// Check if passing a value to an instruction will cause undefined behavior.
  2625. static bool passingValueIsAlwaysUndefined(Value *V, Instruction *I) {
  2626. Constant *C = dyn_cast<Constant>(V);
  2627. if (!C)
  2628. return false;
  2629. if (!I->hasOneUse()) // Only look at single-use instructions, for compile time
  2630. return false;
  2631. if (C->isNullValue()) {
  2632. Instruction *Use = I->use_back();
  2633. // Now make sure that there are no instructions in between that can alter
  2634. // control flow (eg. calls)
  2635. for (BasicBlock::iterator i = ++BasicBlock::iterator(I); &*i != Use; ++i)
  2636. if (i == I->getParent()->end() || i->mayHaveSideEffects())
  2637. return false;
  2638. // Look through GEPs. A load from a GEP derived from NULL is still undefined
  2639. if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Use))
  2640. if (GEP->getPointerOperand() == I)
  2641. return passingValueIsAlwaysUndefined(V, GEP);
  2642. // Look through bitcasts.
  2643. if (BitCastInst *BC = dyn_cast<BitCastInst>(Use))
  2644. return passingValueIsAlwaysUndefined(V, BC);
  2645. // Load from null is undefined.
  2646. if (LoadInst *LI = dyn_cast<LoadInst>(Use))
  2647. return LI->getPointerAddressSpace() == 0;
  2648. // Store to null is undefined.
  2649. if (StoreInst *SI = dyn_cast<StoreInst>(Use))
  2650. return SI->getPointerAddressSpace() == 0 && SI->getPointerOperand() == I;
  2651. }
  2652. return false;
  2653. }
  2654. /// If BB has an incoming value that will always trigger undefined behavior
  2655. /// (eg. null pointer dereference), remove the branch leading here.
  2656. static bool removeUndefIntroducingPredecessor(BasicBlock *BB) {
  2657. for (BasicBlock::iterator i = BB->begin();
  2658. PHINode *PHI = dyn_cast<PHINode>(i); ++i)
  2659. for (unsigned i = 0, e = PHI->getNumIncomingValues(); i != e; ++i)
  2660. if (passingValueIsAlwaysUndefined(PHI->getIncomingValue(i), PHI)) {
  2661. TerminatorInst *T = PHI->getIncomingBlock(i)->getTerminator();
  2662. IRBuilder<> Builder(T);
  2663. if (BranchInst *BI = dyn_cast<BranchInst>(T)) {
  2664. BB->removePredecessor(PHI->getIncomingBlock(i));
  2665. // Turn uncoditional branches into unreachables and remove the dead
  2666. // destination from conditional branches.
  2667. if (BI->isUnconditional())
  2668. Builder.CreateUnreachable();
  2669. else
  2670. Builder.CreateBr(BI->getSuccessor(0) == BB ? BI->getSuccessor(1) :
  2671. BI->getSuccessor(0));
  2672. BI->eraseFromParent();
  2673. return true;
  2674. }
  2675. // TODO: SwitchInst.
  2676. }
  2677. return false;
  2678. }
  2679. bool SimplifyCFGOpt::run(BasicBlock *BB) {
  2680. bool Changed = false;
  2681. assert(BB && BB->getParent() && "Block not embedded in function!");
  2682. assert(BB->getTerminator() && "Degenerate basic block encountered!");
  2683. // Remove basic blocks that have no predecessors (except the entry block)...
  2684. // or that just have themself as a predecessor. These are unreachable.
  2685. if ((pred_begin(BB) == pred_end(BB) &&
  2686. BB != &BB->getParent()->getEntryBlock()) ||
  2687. BB->getSinglePredecessor() == BB) {
  2688. DEBUG(dbgs() << "Removing BB: \n" << *BB);
  2689. DeleteDeadBlock(BB);
  2690. return true;
  2691. }
  2692. // Check to see if we can constant propagate this terminator instruction
  2693. // away...
  2694. Changed |= ConstantFoldTerminator(BB, true);
  2695. // Check for and eliminate duplicate PHI nodes in this block.
  2696. Changed |= EliminateDuplicatePHINodes(BB);
  2697. // Check for and remove branches that will always cause undefined behavior.
  2698. Changed |= removeUndefIntroducingPredecessor(BB);
  2699. // Merge basic blocks into their predecessor if there is only one distinct
  2700. // pred, and if there is only one distinct successor of the predecessor, and
  2701. // if there are no PHI nodes.
  2702. //
  2703. if (MergeBlockIntoPredecessor(BB))
  2704. return true;
  2705. IRBuilder<> Builder(BB);
  2706. // If there is a trivial two-entry PHI node in this basic block, and we can
  2707. // eliminate it, do so now.
  2708. if (PHINode *PN = dyn_cast<PHINode>(BB->begin()))
  2709. if (PN->getNumIncomingValues() == 2)
  2710. Changed |= FoldTwoEntryPHINode(PN, TD);
  2711. Builder.SetInsertPoint(BB->getTerminator());
  2712. if (BranchInst *BI = dyn_cast<BranchInst>(BB->getTerminator())) {
  2713. if (BI->isUnconditional()) {
  2714. if (SimplifyUncondBranch(BI, Builder)) return true;
  2715. } else {
  2716. if (SimplifyCondBranch(BI, Builder)) return true;
  2717. }
  2718. } else if (ReturnInst *RI = dyn_cast<ReturnInst>(BB->getTerminator())) {
  2719. if (SimplifyReturn(RI, Builder)) return true;
  2720. } else if (ResumeInst *RI = dyn_cast<ResumeInst>(BB->getTerminator())) {
  2721. if (SimplifyResume(RI, Builder)) return true;
  2722. } else if (SwitchInst *SI = dyn_cast<SwitchInst>(BB->getTerminator())) {
  2723. if (SimplifySwitch(SI, Builder)) return true;
  2724. } else if (UnreachableInst *UI =
  2725. dyn_cast<UnreachableInst>(BB->getTerminator())) {
  2726. if (SimplifyUnreachable(UI)) return true;
  2727. } else if (IndirectBrInst *IBI =
  2728. dyn_cast<IndirectBrInst>(BB->getTerminator())) {
  2729. if (SimplifyIndirectBr(IBI)) return true;
  2730. }
  2731. return Changed;
  2732. }
  2733. /// SimplifyCFG - This function is used to do simplification of a CFG. For
  2734. /// example, it adjusts branches to branches to eliminate the extra hop, it
  2735. /// eliminates unreachable basic blocks, and does other "peephole" optimization
  2736. /// of the CFG. It returns true if a modification was made.
  2737. ///
  2738. bool llvm::SimplifyCFG(BasicBlock *BB, const TargetData *TD) {
  2739. return SimplifyCFGOpt(TD).run(BB);
  2740. }