LoopUnswitch.cpp 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227
  1. //===-- LoopUnswitch.cpp - Hoist loop-invariant conditionals in loop ------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This pass transforms loops that contain branches on loop-invariant conditions
  11. // to have multiple loops. For example, it turns the left into the right code:
  12. //
  13. // for (...) if (lic)
  14. // A for (...)
  15. // if (lic) A; B; C
  16. // B else
  17. // C for (...)
  18. // A; C
  19. //
  20. // This can increase the size of the code exponentially (doubling it every time
  21. // a loop is unswitched) so we only unswitch if the resultant code will be
  22. // smaller than a threshold.
  23. //
  24. // This pass expects LICM to be run before it to hoist invariant conditions out
  25. // of the loop, to make the unswitching opportunity obvious.
  26. //
  27. //===----------------------------------------------------------------------===//
  28. #include "llvm/Transforms/Scalar.h"
  29. #include "llvm/ADT/STLExtras.h"
  30. #include "llvm/ADT/SmallPtrSet.h"
  31. #include "llvm/ADT/Statistic.h"
  32. #include "llvm/Analysis/AssumptionCache.h"
  33. #include "llvm/Analysis/CodeMetrics.h"
  34. #include "llvm/Analysis/InstructionSimplify.h"
  35. #include "llvm/Analysis/LoopInfo.h"
  36. #include "llvm/Analysis/LoopPass.h"
  37. #include "llvm/Analysis/ScalarEvolution.h"
  38. #include "llvm/Analysis/TargetTransformInfo.h"
  39. #include "llvm/IR/Constants.h"
  40. #include "llvm/IR/DerivedTypes.h"
  41. #include "llvm/IR/Dominators.h"
  42. #include "llvm/IR/Function.h"
  43. #include "llvm/IR/Instructions.h"
  44. #include "llvm/IR/Module.h"
  45. #include "llvm/IR/MDBuilder.h"
  46. #include "llvm/Support/CommandLine.h"
  47. #include "llvm/Support/Debug.h"
  48. #include "llvm/Support/raw_ostream.h"
  49. #include "llvm/Transforms/Utils/BasicBlockUtils.h"
  50. #include "llvm/Transforms/Utils/Cloning.h"
  51. #include "llvm/Transforms/Utils/Local.h"
  52. #include <algorithm>
  53. #include <map>
  54. #include <set>
  55. using namespace llvm;
  56. #define DEBUG_TYPE "loop-unswitch"
  57. STATISTIC(NumBranches, "Number of branches unswitched");
  58. STATISTIC(NumSwitches, "Number of switches unswitched");
  59. STATISTIC(NumSelects , "Number of selects unswitched");
  60. STATISTIC(NumTrivial , "Number of unswitches that are trivial");
  61. STATISTIC(NumSimplify, "Number of simplifications of unswitched code");
  62. STATISTIC(TotalInsts, "Total number of instructions analyzed");
  63. // The specific value of 100 here was chosen based only on intuition and a
  64. // few specific examples.
  65. static cl::opt<unsigned>
  66. Threshold("loop-unswitch-threshold", cl::desc("Max loop size to unswitch"),
  67. cl::init(100), cl::Hidden);
  68. namespace {
  69. class LUAnalysisCache {
  70. typedef DenseMap<const SwitchInst*, SmallPtrSet<const Value *, 8> >
  71. UnswitchedValsMap;
  72. typedef UnswitchedValsMap::iterator UnswitchedValsIt;
  73. struct LoopProperties {
  74. unsigned CanBeUnswitchedCount;
  75. unsigned WasUnswitchedCount;
  76. unsigned SizeEstimation;
  77. UnswitchedValsMap UnswitchedVals;
  78. };
  79. // Here we use std::map instead of DenseMap, since we need to keep valid
  80. // LoopProperties pointer for current loop for better performance.
  81. typedef std::map<const Loop*, LoopProperties> LoopPropsMap;
  82. typedef LoopPropsMap::iterator LoopPropsMapIt;
  83. LoopPropsMap LoopsProperties;
  84. UnswitchedValsMap *CurLoopInstructions;
  85. LoopProperties *CurrentLoopProperties;
  86. // A loop unswitching with an estimated cost above this threshold
  87. // is not performed. MaxSize is turned into unswitching quota for
  88. // the current loop, and reduced correspondingly, though note that
  89. // the quota is returned by releaseMemory() when the loop has been
  90. // processed, so that MaxSize will return to its previous
  91. // value. So in most cases MaxSize will equal the Threshold flag
  92. // when a new loop is processed. An exception to that is that
  93. // MaxSize will have a smaller value while processing nested loops
  94. // that were introduced due to loop unswitching of an outer loop.
  95. //
  96. // FIXME: The way that MaxSize works is subtle and depends on the
  97. // pass manager processing loops and calling releaseMemory() in a
  98. // specific order. It would be good to find a more straightforward
  99. // way of doing what MaxSize does.
  100. unsigned MaxSize;
  101. public:
  102. LUAnalysisCache()
  103. : CurLoopInstructions(nullptr), CurrentLoopProperties(nullptr),
  104. MaxSize(Threshold) {}
  105. // Analyze loop. Check its size, calculate is it possible to unswitch
  106. // it. Returns true if we can unswitch this loop.
  107. bool countLoop(const Loop *L, const TargetTransformInfo &TTI,
  108. AssumptionCache *AC);
  109. // Clean all data related to given loop.
  110. void forgetLoop(const Loop *L);
  111. // Mark case value as unswitched.
  112. // Since SI instruction can be partly unswitched, in order to avoid
  113. // extra unswitching in cloned loops keep track all unswitched values.
  114. void setUnswitched(const SwitchInst *SI, const Value *V);
  115. // Check was this case value unswitched before or not.
  116. bool isUnswitched(const SwitchInst *SI, const Value *V);
  117. // Returns true if another unswitching could be done within the cost
  118. // threshold.
  119. bool CostAllowsUnswitching();
  120. // Clone all loop-unswitch related loop properties.
  121. // Redistribute unswitching quotas.
  122. // Note, that new loop data is stored inside the VMap.
  123. void cloneData(const Loop *NewLoop, const Loop *OldLoop,
  124. const ValueToValueMapTy &VMap);
  125. };
  126. class LoopUnswitch : public LoopPass {
  127. LoopInfo *LI; // Loop information
  128. LPPassManager *LPM;
  129. AssumptionCache *AC;
  130. // LoopProcessWorklist - Used to check if second loop needs processing
  131. // after RewriteLoopBodyWithConditionConstant rewrites first loop.
  132. std::vector<Loop*> LoopProcessWorklist;
  133. LUAnalysisCache BranchesInfo;
  134. bool OptimizeForSize;
  135. bool redoLoop;
  136. Loop *currentLoop;
  137. DominatorTree *DT;
  138. BasicBlock *loopHeader;
  139. BasicBlock *loopPreheader;
  140. // LoopBlocks contains all of the basic blocks of the loop, including the
  141. // preheader of the loop, the body of the loop, and the exit blocks of the
  142. // loop, in that order.
  143. std::vector<BasicBlock*> LoopBlocks;
  144. // NewBlocks contained cloned copy of basic blocks from LoopBlocks.
  145. std::vector<BasicBlock*> NewBlocks;
  146. public:
  147. static char ID; // Pass ID, replacement for typeid
  148. explicit LoopUnswitch(bool Os = false) :
  149. LoopPass(ID), OptimizeForSize(Os), redoLoop(false),
  150. currentLoop(nullptr), DT(nullptr), loopHeader(nullptr),
  151. loopPreheader(nullptr) {
  152. initializeLoopUnswitchPass(*PassRegistry::getPassRegistry());
  153. }
  154. bool runOnLoop(Loop *L, LPPassManager &LPM) override;
  155. bool processCurrentLoop();
  156. /// This transformation requires natural loop information & requires that
  157. /// loop preheaders be inserted into the CFG.
  158. ///
  159. void getAnalysisUsage(AnalysisUsage &AU) const override {
  160. AU.addRequired<AssumptionCacheTracker>();
  161. AU.addRequiredID(LoopSimplifyID);
  162. AU.addPreservedID(LoopSimplifyID);
  163. AU.addRequired<LoopInfoWrapperPass>();
  164. AU.addPreserved<LoopInfoWrapperPass>();
  165. AU.addRequiredID(LCSSAID);
  166. AU.addPreservedID(LCSSAID);
  167. AU.addPreserved<DominatorTreeWrapperPass>();
  168. AU.addPreserved<ScalarEvolution>();
  169. AU.addRequired<TargetTransformInfoWrapperPass>();
  170. }
  171. private:
  172. void releaseMemory() override {
  173. BranchesInfo.forgetLoop(currentLoop);
  174. }
  175. void initLoopData() {
  176. loopHeader = currentLoop->getHeader();
  177. loopPreheader = currentLoop->getLoopPreheader();
  178. }
  179. /// Split all of the edges from inside the loop to their exit blocks.
  180. /// Update the appropriate Phi nodes as we do so.
  181. void SplitExitEdges(Loop *L, const SmallVectorImpl<BasicBlock *> &ExitBlocks);
  182. bool UnswitchIfProfitable(Value *LoopCond, Constant *Val,
  183. TerminatorInst *TI = nullptr);
  184. void UnswitchTrivialCondition(Loop *L, Value *Cond, Constant *Val,
  185. BasicBlock *ExitBlock, TerminatorInst *TI);
  186. void UnswitchNontrivialCondition(Value *LIC, Constant *OnVal, Loop *L,
  187. TerminatorInst *TI);
  188. void RewriteLoopBodyWithConditionConstant(Loop *L, Value *LIC,
  189. Constant *Val, bool isEqual);
  190. void EmitPreheaderBranchOnCondition(Value *LIC, Constant *Val,
  191. BasicBlock *TrueDest,
  192. BasicBlock *FalseDest,
  193. Instruction *InsertPt,
  194. TerminatorInst *TI);
  195. void SimplifyCode(std::vector<Instruction*> &Worklist, Loop *L);
  196. bool IsTrivialUnswitchCondition(Value *Cond, Constant **Val = nullptr,
  197. BasicBlock **LoopExit = nullptr);
  198. };
  199. }
  200. // Analyze loop. Check its size, calculate is it possible to unswitch
  201. // it. Returns true if we can unswitch this loop.
  202. bool LUAnalysisCache::countLoop(const Loop *L, const TargetTransformInfo &TTI,
  203. AssumptionCache *AC) {
  204. LoopPropsMapIt PropsIt;
  205. bool Inserted;
  206. std::tie(PropsIt, Inserted) =
  207. LoopsProperties.insert(std::make_pair(L, LoopProperties()));
  208. LoopProperties &Props = PropsIt->second;
  209. if (Inserted) {
  210. // New loop.
  211. // Limit the number of instructions to avoid causing significant code
  212. // expansion, and the number of basic blocks, to avoid loops with
  213. // large numbers of branches which cause loop unswitching to go crazy.
  214. // This is a very ad-hoc heuristic.
  215. SmallPtrSet<const Value *, 32> EphValues;
  216. CodeMetrics::collectEphemeralValues(L, AC, EphValues);
  217. // FIXME: This is overly conservative because it does not take into
  218. // consideration code simplification opportunities and code that can
  219. // be shared by the resultant unswitched loops.
  220. CodeMetrics Metrics;
  221. for (Loop::block_iterator I = L->block_begin(), E = L->block_end(); I != E;
  222. ++I)
  223. Metrics.analyzeBasicBlock(*I, TTI, EphValues);
  224. Props.SizeEstimation = Metrics.NumInsts;
  225. Props.CanBeUnswitchedCount = MaxSize / (Props.SizeEstimation);
  226. Props.WasUnswitchedCount = 0;
  227. MaxSize -= Props.SizeEstimation * Props.CanBeUnswitchedCount;
  228. if (Metrics.notDuplicatable) {
  229. DEBUG(dbgs() << "NOT unswitching loop %"
  230. << L->getHeader()->getName() << ", contents cannot be "
  231. << "duplicated!\n");
  232. return false;
  233. }
  234. }
  235. // Be careful. This links are good only before new loop addition.
  236. CurrentLoopProperties = &Props;
  237. CurLoopInstructions = &Props.UnswitchedVals;
  238. return true;
  239. }
  240. // Clean all data related to given loop.
  241. void LUAnalysisCache::forgetLoop(const Loop *L) {
  242. LoopPropsMapIt LIt = LoopsProperties.find(L);
  243. if (LIt != LoopsProperties.end()) {
  244. LoopProperties &Props = LIt->second;
  245. MaxSize += (Props.CanBeUnswitchedCount + Props.WasUnswitchedCount) *
  246. Props.SizeEstimation;
  247. LoopsProperties.erase(LIt);
  248. }
  249. CurrentLoopProperties = nullptr;
  250. CurLoopInstructions = nullptr;
  251. }
  252. // Mark case value as unswitched.
  253. // Since SI instruction can be partly unswitched, in order to avoid
  254. // extra unswitching in cloned loops keep track all unswitched values.
  255. void LUAnalysisCache::setUnswitched(const SwitchInst *SI, const Value *V) {
  256. (*CurLoopInstructions)[SI].insert(V);
  257. }
  258. // Check was this case value unswitched before or not.
  259. bool LUAnalysisCache::isUnswitched(const SwitchInst *SI, const Value *V) {
  260. return (*CurLoopInstructions)[SI].count(V);
  261. }
  262. bool LUAnalysisCache::CostAllowsUnswitching() {
  263. return CurrentLoopProperties->CanBeUnswitchedCount > 0;
  264. }
  265. // Clone all loop-unswitch related loop properties.
  266. // Redistribute unswitching quotas.
  267. // Note, that new loop data is stored inside the VMap.
  268. void LUAnalysisCache::cloneData(const Loop *NewLoop, const Loop *OldLoop,
  269. const ValueToValueMapTy &VMap) {
  270. LoopProperties &NewLoopProps = LoopsProperties[NewLoop];
  271. LoopProperties &OldLoopProps = *CurrentLoopProperties;
  272. UnswitchedValsMap &Insts = OldLoopProps.UnswitchedVals;
  273. // Reallocate "can-be-unswitched quota"
  274. --OldLoopProps.CanBeUnswitchedCount;
  275. ++OldLoopProps.WasUnswitchedCount;
  276. NewLoopProps.WasUnswitchedCount = 0;
  277. unsigned Quota = OldLoopProps.CanBeUnswitchedCount;
  278. NewLoopProps.CanBeUnswitchedCount = Quota / 2;
  279. OldLoopProps.CanBeUnswitchedCount = Quota - Quota / 2;
  280. NewLoopProps.SizeEstimation = OldLoopProps.SizeEstimation;
  281. // Clone unswitched values info:
  282. // for new loop switches we clone info about values that was
  283. // already unswitched and has redundant successors.
  284. for (UnswitchedValsIt I = Insts.begin(); I != Insts.end(); ++I) {
  285. const SwitchInst *OldInst = I->first;
  286. Value *NewI = VMap.lookup(OldInst);
  287. const SwitchInst *NewInst = cast_or_null<SwitchInst>(NewI);
  288. assert(NewInst && "All instructions that are in SrcBB must be in VMap.");
  289. NewLoopProps.UnswitchedVals[NewInst] = OldLoopProps.UnswitchedVals[OldInst];
  290. }
  291. }
  292. char LoopUnswitch::ID = 0;
  293. INITIALIZE_PASS_BEGIN(LoopUnswitch, "loop-unswitch", "Unswitch loops",
  294. false, false)
  295. INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass)
  296. INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
  297. INITIALIZE_PASS_DEPENDENCY(LoopSimplify)
  298. INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
  299. INITIALIZE_PASS_DEPENDENCY(LCSSA)
  300. INITIALIZE_PASS_END(LoopUnswitch, "loop-unswitch", "Unswitch loops",
  301. false, false)
  302. Pass *llvm::createLoopUnswitchPass(bool Os) {
  303. return new LoopUnswitch(Os);
  304. }
  305. /// FindLIVLoopCondition - Cond is a condition that occurs in L. If it is
  306. /// invariant in the loop, or has an invariant piece, return the invariant.
  307. /// Otherwise, return null.
  308. static Value *FindLIVLoopCondition(Value *Cond, Loop *L, bool &Changed) {
  309. // We started analyze new instruction, increment scanned instructions counter.
  310. ++TotalInsts;
  311. // We can never unswitch on vector conditions.
  312. if (Cond->getType()->isVectorTy())
  313. return nullptr;
  314. // Constants should be folded, not unswitched on!
  315. if (isa<Constant>(Cond)) return nullptr;
  316. // TODO: Handle: br (VARIANT|INVARIANT).
  317. // Hoist simple values out.
  318. if (L->makeLoopInvariant(Cond, Changed))
  319. return Cond;
  320. if (BinaryOperator *BO = dyn_cast<BinaryOperator>(Cond))
  321. if (BO->getOpcode() == Instruction::And ||
  322. BO->getOpcode() == Instruction::Or) {
  323. // If either the left or right side is invariant, we can unswitch on this,
  324. // which will cause the branch to go away in one loop and the condition to
  325. // simplify in the other one.
  326. if (Value *LHS = FindLIVLoopCondition(BO->getOperand(0), L, Changed))
  327. return LHS;
  328. if (Value *RHS = FindLIVLoopCondition(BO->getOperand(1), L, Changed))
  329. return RHS;
  330. }
  331. return nullptr;
  332. }
  333. bool LoopUnswitch::runOnLoop(Loop *L, LPPassManager &LPM_Ref) {
  334. if (skipOptnoneFunction(L))
  335. return false;
  336. AC = &getAnalysis<AssumptionCacheTracker>().getAssumptionCache(
  337. *L->getHeader()->getParent());
  338. LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
  339. LPM = &LPM_Ref;
  340. DominatorTreeWrapperPass *DTWP =
  341. getAnalysisIfAvailable<DominatorTreeWrapperPass>();
  342. DT = DTWP ? &DTWP->getDomTree() : nullptr;
  343. currentLoop = L;
  344. Function *F = currentLoop->getHeader()->getParent();
  345. bool Changed = false;
  346. do {
  347. assert(currentLoop->isLCSSAForm(*DT));
  348. redoLoop = false;
  349. Changed |= processCurrentLoop();
  350. } while(redoLoop);
  351. if (Changed) {
  352. // FIXME: Reconstruct dom info, because it is not preserved properly.
  353. if (DT)
  354. DT->recalculate(*F);
  355. }
  356. return Changed;
  357. }
  358. /// processCurrentLoop - Do actual work and unswitch loop if possible
  359. /// and profitable.
  360. bool LoopUnswitch::processCurrentLoop() {
  361. bool Changed = false;
  362. initLoopData();
  363. // If LoopSimplify was unable to form a preheader, don't do any unswitching.
  364. if (!loopPreheader)
  365. return false;
  366. // Loops with indirectbr cannot be cloned.
  367. if (!currentLoop->isSafeToClone())
  368. return false;
  369. // Without dedicated exits, splitting the exit edge may fail.
  370. if (!currentLoop->hasDedicatedExits())
  371. return false;
  372. LLVMContext &Context = loopHeader->getContext();
  373. // Probably we reach the quota of branches for this loop. If so
  374. // stop unswitching.
  375. if (!BranchesInfo.countLoop(
  376. currentLoop, getAnalysis<TargetTransformInfoWrapperPass>().getTTI(
  377. *currentLoop->getHeader()->getParent()),
  378. AC))
  379. return false;
  380. // Loop over all of the basic blocks in the loop. If we find an interior
  381. // block that is branching on a loop-invariant condition, we can unswitch this
  382. // loop.
  383. for (Loop::block_iterator I = currentLoop->block_begin(),
  384. E = currentLoop->block_end(); I != E; ++I) {
  385. TerminatorInst *TI = (*I)->getTerminator();
  386. if (BranchInst *BI = dyn_cast<BranchInst>(TI)) {
  387. // If this isn't branching on an invariant condition, we can't unswitch
  388. // it.
  389. if (BI->isConditional()) {
  390. // See if this, or some part of it, is loop invariant. If so, we can
  391. // unswitch on it if we desire.
  392. Value *LoopCond = FindLIVLoopCondition(BI->getCondition(),
  393. currentLoop, Changed);
  394. if (LoopCond &&
  395. UnswitchIfProfitable(LoopCond, ConstantInt::getTrue(Context), TI)) {
  396. ++NumBranches;
  397. return true;
  398. }
  399. }
  400. } else if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) {
  401. Value *LoopCond = FindLIVLoopCondition(SI->getCondition(),
  402. currentLoop, Changed);
  403. unsigned NumCases = SI->getNumCases();
  404. if (LoopCond && NumCases) {
  405. // Find a value to unswitch on:
  406. // FIXME: this should chose the most expensive case!
  407. // FIXME: scan for a case with a non-critical edge?
  408. Constant *UnswitchVal = nullptr;
  409. // Do not process same value again and again.
  410. // At this point we have some cases already unswitched and
  411. // some not yet unswitched. Let's find the first not yet unswitched one.
  412. for (SwitchInst::CaseIt i = SI->case_begin(), e = SI->case_end();
  413. i != e; ++i) {
  414. Constant *UnswitchValCandidate = i.getCaseValue();
  415. if (!BranchesInfo.isUnswitched(SI, UnswitchValCandidate)) {
  416. UnswitchVal = UnswitchValCandidate;
  417. break;
  418. }
  419. }
  420. if (!UnswitchVal)
  421. continue;
  422. if (UnswitchIfProfitable(LoopCond, UnswitchVal)) {
  423. ++NumSwitches;
  424. return true;
  425. }
  426. }
  427. }
  428. // Scan the instructions to check for unswitchable values.
  429. for (BasicBlock::iterator BBI = (*I)->begin(), E = (*I)->end();
  430. BBI != E; ++BBI)
  431. if (SelectInst *SI = dyn_cast<SelectInst>(BBI)) {
  432. Value *LoopCond = FindLIVLoopCondition(SI->getCondition(),
  433. currentLoop, Changed);
  434. if (LoopCond && UnswitchIfProfitable(LoopCond,
  435. ConstantInt::getTrue(Context))) {
  436. ++NumSelects;
  437. return true;
  438. }
  439. }
  440. }
  441. return Changed;
  442. }
  443. /// isTrivialLoopExitBlock - Check to see if all paths from BB exit the
  444. /// loop with no side effects (including infinite loops).
  445. ///
  446. /// If true, we return true and set ExitBB to the block we
  447. /// exit through.
  448. ///
  449. static bool isTrivialLoopExitBlockHelper(Loop *L, BasicBlock *BB,
  450. BasicBlock *&ExitBB,
  451. std::set<BasicBlock*> &Visited) {
  452. if (!Visited.insert(BB).second) {
  453. // Already visited. Without more analysis, this could indicate an infinite
  454. // loop.
  455. return false;
  456. }
  457. if (!L->contains(BB)) {
  458. // Otherwise, this is a loop exit, this is fine so long as this is the
  459. // first exit.
  460. if (ExitBB) return false;
  461. ExitBB = BB;
  462. return true;
  463. }
  464. // Otherwise, this is an unvisited intra-loop node. Check all successors.
  465. for (succ_iterator SI = succ_begin(BB), E = succ_end(BB); SI != E; ++SI) {
  466. // Check to see if the successor is a trivial loop exit.
  467. if (!isTrivialLoopExitBlockHelper(L, *SI, ExitBB, Visited))
  468. return false;
  469. }
  470. // Okay, everything after this looks good, check to make sure that this block
  471. // doesn't include any side effects.
  472. for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
  473. if (I->mayHaveSideEffects())
  474. return false;
  475. return true;
  476. }
  477. /// isTrivialLoopExitBlock - Return true if the specified block unconditionally
  478. /// leads to an exit from the specified loop, and has no side-effects in the
  479. /// process. If so, return the block that is exited to, otherwise return null.
  480. static BasicBlock *isTrivialLoopExitBlock(Loop *L, BasicBlock *BB) {
  481. std::set<BasicBlock*> Visited;
  482. Visited.insert(L->getHeader()); // Branches to header make infinite loops.
  483. BasicBlock *ExitBB = nullptr;
  484. if (isTrivialLoopExitBlockHelper(L, BB, ExitBB, Visited))
  485. return ExitBB;
  486. return nullptr;
  487. }
  488. /// IsTrivialUnswitchCondition - Check to see if this unswitch condition is
  489. /// trivial: that is, that the condition controls whether or not the loop does
  490. /// anything at all. If this is a trivial condition, unswitching produces no
  491. /// code duplications (equivalently, it produces a simpler loop and a new empty
  492. /// loop, which gets deleted).
  493. ///
  494. /// If this is a trivial condition, return true, otherwise return false. When
  495. /// returning true, this sets Cond and Val to the condition that controls the
  496. /// trivial condition: when Cond dynamically equals Val, the loop is known to
  497. /// exit. Finally, this sets LoopExit to the BB that the loop exits to when
  498. /// Cond == Val.
  499. ///
  500. bool LoopUnswitch::IsTrivialUnswitchCondition(Value *Cond, Constant **Val,
  501. BasicBlock **LoopExit) {
  502. BasicBlock *Header = currentLoop->getHeader();
  503. TerminatorInst *HeaderTerm = Header->getTerminator();
  504. LLVMContext &Context = Header->getContext();
  505. BasicBlock *LoopExitBB = nullptr;
  506. if (BranchInst *BI = dyn_cast<BranchInst>(HeaderTerm)) {
  507. // If the header block doesn't end with a conditional branch on Cond, we
  508. // can't handle it.
  509. if (!BI->isConditional() || BI->getCondition() != Cond)
  510. return false;
  511. // Check to see if a successor of the branch is guaranteed to
  512. // exit through a unique exit block without having any
  513. // side-effects. If so, determine the value of Cond that causes it to do
  514. // this.
  515. if ((LoopExitBB = isTrivialLoopExitBlock(currentLoop,
  516. BI->getSuccessor(0)))) {
  517. if (Val) *Val = ConstantInt::getTrue(Context);
  518. } else if ((LoopExitBB = isTrivialLoopExitBlock(currentLoop,
  519. BI->getSuccessor(1)))) {
  520. if (Val) *Val = ConstantInt::getFalse(Context);
  521. }
  522. } else if (SwitchInst *SI = dyn_cast<SwitchInst>(HeaderTerm)) {
  523. // If this isn't a switch on Cond, we can't handle it.
  524. if (SI->getCondition() != Cond) return false;
  525. // Check to see if a successor of the switch is guaranteed to go to the
  526. // latch block or exit through a one exit block without having any
  527. // side-effects. If so, determine the value of Cond that causes it to do
  528. // this.
  529. // Note that we can't trivially unswitch on the default case or
  530. // on already unswitched cases.
  531. for (SwitchInst::CaseIt i = SI->case_begin(), e = SI->case_end();
  532. i != e; ++i) {
  533. BasicBlock *LoopExitCandidate;
  534. if ((LoopExitCandidate = isTrivialLoopExitBlock(currentLoop,
  535. i.getCaseSuccessor()))) {
  536. // Okay, we found a trivial case, remember the value that is trivial.
  537. ConstantInt *CaseVal = i.getCaseValue();
  538. // Check that it was not unswitched before, since already unswitched
  539. // trivial vals are looks trivial too.
  540. if (BranchesInfo.isUnswitched(SI, CaseVal))
  541. continue;
  542. LoopExitBB = LoopExitCandidate;
  543. if (Val) *Val = CaseVal;
  544. break;
  545. }
  546. }
  547. }
  548. // If we didn't find a single unique LoopExit block, or if the loop exit block
  549. // contains phi nodes, this isn't trivial.
  550. if (!LoopExitBB || isa<PHINode>(LoopExitBB->begin()))
  551. return false; // Can't handle this.
  552. if (LoopExit) *LoopExit = LoopExitBB;
  553. // We already know that nothing uses any scalar values defined inside of this
  554. // loop. As such, we just have to check to see if this loop will execute any
  555. // side-effecting instructions (e.g. stores, calls, volatile loads) in the
  556. // part of the loop that the code *would* execute. We already checked the
  557. // tail, check the header now.
  558. for (BasicBlock::iterator I = Header->begin(), E = Header->end(); I != E; ++I)
  559. if (I->mayHaveSideEffects())
  560. return false;
  561. return true;
  562. }
  563. /// UnswitchIfProfitable - We have found that we can unswitch currentLoop when
  564. /// LoopCond == Val to simplify the loop. If we decide that this is profitable,
  565. /// unswitch the loop, reprocess the pieces, then return true.
  566. bool LoopUnswitch::UnswitchIfProfitable(Value *LoopCond, Constant *Val,
  567. TerminatorInst *TI) {
  568. Function *F = loopHeader->getParent();
  569. Constant *CondVal = nullptr;
  570. BasicBlock *ExitBlock = nullptr;
  571. if (IsTrivialUnswitchCondition(LoopCond, &CondVal, &ExitBlock)) {
  572. // If the condition is trivial, always unswitch. There is no code growth
  573. // for this case.
  574. UnswitchTrivialCondition(currentLoop, LoopCond, CondVal, ExitBlock, TI);
  575. return true;
  576. }
  577. // Check to see if it would be profitable to unswitch current loop.
  578. if (!BranchesInfo.CostAllowsUnswitching()) {
  579. DEBUG(dbgs() << "NOT unswitching loop %"
  580. << currentLoop->getHeader()->getName()
  581. << " at non-trivial condition '" << *Val
  582. << "' == " << *LoopCond << "\n"
  583. << ". Cost too high.\n");
  584. return false;
  585. }
  586. // Do not do non-trivial unswitch while optimizing for size.
  587. if (OptimizeForSize || F->hasFnAttribute(Attribute::OptimizeForSize))
  588. return false;
  589. UnswitchNontrivialCondition(LoopCond, Val, currentLoop, TI);
  590. return true;
  591. }
  592. /// CloneLoop - Recursively clone the specified loop and all of its children,
  593. /// mapping the blocks with the specified map.
  594. static Loop *CloneLoop(Loop *L, Loop *PL, ValueToValueMapTy &VM,
  595. LoopInfo *LI, LPPassManager *LPM) {
  596. Loop *New = new Loop();
  597. LPM->insertLoop(New, PL);
  598. // Add all of the blocks in L to the new loop.
  599. for (Loop::block_iterator I = L->block_begin(), E = L->block_end();
  600. I != E; ++I)
  601. if (LI->getLoopFor(*I) == L)
  602. New->addBasicBlockToLoop(cast<BasicBlock>(VM[*I]), *LI);
  603. // Add all of the subloops to the new loop.
  604. for (Loop::iterator I = L->begin(), E = L->end(); I != E; ++I)
  605. CloneLoop(*I, New, VM, LI, LPM);
  606. return New;
  607. }
  608. static void copyMetadata(Instruction *DstInst, const Instruction *SrcInst,
  609. bool Swapped) {
  610. if (!SrcInst || !SrcInst->hasMetadata())
  611. return;
  612. SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
  613. SrcInst->getAllMetadata(MDs);
  614. for (auto &MD : MDs) {
  615. switch (MD.first) {
  616. default:
  617. break;
  618. case LLVMContext::MD_prof:
  619. if (Swapped && MD.second->getNumOperands() == 3 &&
  620. isa<MDString>(MD.second->getOperand(0))) {
  621. MDString *MDName = cast<MDString>(MD.second->getOperand(0));
  622. if (MDName->getString() == "branch_weights") {
  623. auto *ValT = cast_or_null<ConstantAsMetadata>(
  624. MD.second->getOperand(1))->getValue();
  625. auto *ValF = cast_or_null<ConstantAsMetadata>(
  626. MD.second->getOperand(2))->getValue();
  627. assert(ValT && ValF && "Invalid Operands of branch_weights");
  628. auto NewMD =
  629. MDBuilder(DstInst->getParent()->getContext())
  630. .createBranchWeights(cast<ConstantInt>(ValF)->getZExtValue(),
  631. cast<ConstantInt>(ValT)->getZExtValue());
  632. MD.second = NewMD;
  633. }
  634. }
  635. // fallthrough.
  636. case LLVMContext::MD_dbg:
  637. DstInst->setMetadata(MD.first, MD.second);
  638. }
  639. }
  640. }
  641. /// EmitPreheaderBranchOnCondition - Emit a conditional branch on two values
  642. /// if LIC == Val, branch to TrueDst, otherwise branch to FalseDest. Insert the
  643. /// code immediately before InsertPt.
  644. void LoopUnswitch::EmitPreheaderBranchOnCondition(Value *LIC, Constant *Val,
  645. BasicBlock *TrueDest,
  646. BasicBlock *FalseDest,
  647. Instruction *InsertPt,
  648. TerminatorInst *TI) {
  649. // Insert a conditional branch on LIC to the two preheaders. The original
  650. // code is the true version and the new code is the false version.
  651. Value *BranchVal = LIC;
  652. bool Swapped = false;
  653. if (!isa<ConstantInt>(Val) ||
  654. Val->getType() != Type::getInt1Ty(LIC->getContext()))
  655. BranchVal = new ICmpInst(InsertPt, ICmpInst::ICMP_EQ, LIC, Val);
  656. else if (Val != ConstantInt::getTrue(Val->getContext())) {
  657. // We want to enter the new loop when the condition is true.
  658. std::swap(TrueDest, FalseDest);
  659. Swapped = true;
  660. }
  661. // Insert the new branch.
  662. BranchInst *BI = BranchInst::Create(TrueDest, FalseDest, BranchVal, InsertPt);
  663. copyMetadata(BI, TI, Swapped);
  664. // If either edge is critical, split it. This helps preserve LoopSimplify
  665. // form for enclosing loops.
  666. auto Options = CriticalEdgeSplittingOptions(DT, LI).setPreserveLCSSA();
  667. SplitCriticalEdge(BI, 0, Options);
  668. SplitCriticalEdge(BI, 1, Options);
  669. }
  670. /// UnswitchTrivialCondition - Given a loop that has a trivial unswitchable
  671. /// condition in it (a cond branch from its header block to its latch block,
  672. /// where the path through the loop that doesn't execute its body has no
  673. /// side-effects), unswitch it. This doesn't involve any code duplication, just
  674. /// moving the conditional branch outside of the loop and updating loop info.
  675. void LoopUnswitch::UnswitchTrivialCondition(Loop *L, Value *Cond, Constant *Val,
  676. BasicBlock *ExitBlock,
  677. TerminatorInst *TI) {
  678. DEBUG(dbgs() << "loop-unswitch: Trivial-Unswitch loop %"
  679. << loopHeader->getName() << " [" << L->getBlocks().size()
  680. << " blocks] in Function "
  681. << L->getHeader()->getParent()->getName() << " on cond: " << *Val
  682. << " == " << *Cond << "\n");
  683. // First step, split the preheader, so that we know that there is a safe place
  684. // to insert the conditional branch. We will change loopPreheader to have a
  685. // conditional branch on Cond.
  686. BasicBlock *NewPH = SplitEdge(loopPreheader, loopHeader, DT, LI);
  687. // Now that we have a place to insert the conditional branch, create a place
  688. // to branch to: this is the exit block out of the loop that we should
  689. // short-circuit to.
  690. // Split this block now, so that the loop maintains its exit block, and so
  691. // that the jump from the preheader can execute the contents of the exit block
  692. // without actually branching to it (the exit block should be dominated by the
  693. // loop header, not the preheader).
  694. assert(!L->contains(ExitBlock) && "Exit block is in the loop?");
  695. BasicBlock *NewExit = SplitBlock(ExitBlock, ExitBlock->begin(), DT, LI);
  696. // Okay, now we have a position to branch from and a position to branch to,
  697. // insert the new conditional branch.
  698. EmitPreheaderBranchOnCondition(Cond, Val, NewExit, NewPH,
  699. loopPreheader->getTerminator(), TI);
  700. LPM->deleteSimpleAnalysisValue(loopPreheader->getTerminator(), L);
  701. loopPreheader->getTerminator()->eraseFromParent();
  702. // We need to reprocess this loop, it could be unswitched again.
  703. redoLoop = true;
  704. // Now that we know that the loop is never entered when this condition is a
  705. // particular value, rewrite the loop with this info. We know that this will
  706. // at least eliminate the old branch.
  707. RewriteLoopBodyWithConditionConstant(L, Cond, Val, false);
  708. ++NumTrivial;
  709. }
  710. /// SplitExitEdges - Split all of the edges from inside the loop to their exit
  711. /// blocks. Update the appropriate Phi nodes as we do so.
  712. void LoopUnswitch::SplitExitEdges(Loop *L,
  713. const SmallVectorImpl<BasicBlock *> &ExitBlocks){
  714. for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i) {
  715. BasicBlock *ExitBlock = ExitBlocks[i];
  716. SmallVector<BasicBlock *, 4> Preds(pred_begin(ExitBlock),
  717. pred_end(ExitBlock));
  718. // Although SplitBlockPredecessors doesn't preserve loop-simplify in
  719. // general, if we call it on all predecessors of all exits then it does.
  720. SplitBlockPredecessors(ExitBlock, Preds, ".us-lcssa",
  721. /*AliasAnalysis*/ nullptr, DT, LI,
  722. /*PreserveLCSSA*/ true);
  723. }
  724. }
  725. /// UnswitchNontrivialCondition - We determined that the loop is profitable
  726. /// to unswitch when LIC equal Val. Split it into loop versions and test the
  727. /// condition outside of either loop. Return the loops created as Out1/Out2.
  728. void LoopUnswitch::UnswitchNontrivialCondition(Value *LIC, Constant *Val,
  729. Loop *L, TerminatorInst *TI) {
  730. Function *F = loopHeader->getParent();
  731. DEBUG(dbgs() << "loop-unswitch: Unswitching loop %"
  732. << loopHeader->getName() << " [" << L->getBlocks().size()
  733. << " blocks] in Function " << F->getName()
  734. << " when '" << *Val << "' == " << *LIC << "\n");
  735. if (ScalarEvolution *SE = getAnalysisIfAvailable<ScalarEvolution>())
  736. SE->forgetLoop(L);
  737. LoopBlocks.clear();
  738. NewBlocks.clear();
  739. // First step, split the preheader and exit blocks, and add these blocks to
  740. // the LoopBlocks list.
  741. BasicBlock *NewPreheader = SplitEdge(loopPreheader, loopHeader, DT, LI);
  742. LoopBlocks.push_back(NewPreheader);
  743. // We want the loop to come after the preheader, but before the exit blocks.
  744. LoopBlocks.insert(LoopBlocks.end(), L->block_begin(), L->block_end());
  745. SmallVector<BasicBlock*, 8> ExitBlocks;
  746. L->getUniqueExitBlocks(ExitBlocks);
  747. // Split all of the edges from inside the loop to their exit blocks. Update
  748. // the appropriate Phi nodes as we do so.
  749. SplitExitEdges(L, ExitBlocks);
  750. // The exit blocks may have been changed due to edge splitting, recompute.
  751. ExitBlocks.clear();
  752. L->getUniqueExitBlocks(ExitBlocks);
  753. // Add exit blocks to the loop blocks.
  754. LoopBlocks.insert(LoopBlocks.end(), ExitBlocks.begin(), ExitBlocks.end());
  755. // Next step, clone all of the basic blocks that make up the loop (including
  756. // the loop preheader and exit blocks), keeping track of the mapping between
  757. // the instructions and blocks.
  758. NewBlocks.reserve(LoopBlocks.size());
  759. ValueToValueMapTy VMap;
  760. for (unsigned i = 0, e = LoopBlocks.size(); i != e; ++i) {
  761. BasicBlock *NewBB = CloneBasicBlock(LoopBlocks[i], VMap, ".us", F);
  762. NewBlocks.push_back(NewBB);
  763. VMap[LoopBlocks[i]] = NewBB; // Keep the BB mapping.
  764. LPM->cloneBasicBlockSimpleAnalysis(LoopBlocks[i], NewBB, L);
  765. }
  766. // Splice the newly inserted blocks into the function right before the
  767. // original preheader.
  768. F->getBasicBlockList().splice(NewPreheader, F->getBasicBlockList(),
  769. NewBlocks[0], F->end());
  770. // FIXME: We could register any cloned assumptions instead of clearing the
  771. // whole function's cache.
  772. AC->clear();
  773. // Now we create the new Loop object for the versioned loop.
  774. Loop *NewLoop = CloneLoop(L, L->getParentLoop(), VMap, LI, LPM);
  775. // Recalculate unswitching quota, inherit simplified switches info for NewBB,
  776. // Probably clone more loop-unswitch related loop properties.
  777. BranchesInfo.cloneData(NewLoop, L, VMap);
  778. Loop *ParentLoop = L->getParentLoop();
  779. if (ParentLoop) {
  780. // Make sure to add the cloned preheader and exit blocks to the parent loop
  781. // as well.
  782. ParentLoop->addBasicBlockToLoop(NewBlocks[0], *LI);
  783. }
  784. for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i) {
  785. BasicBlock *NewExit = cast<BasicBlock>(VMap[ExitBlocks[i]]);
  786. // The new exit block should be in the same loop as the old one.
  787. if (Loop *ExitBBLoop = LI->getLoopFor(ExitBlocks[i]))
  788. ExitBBLoop->addBasicBlockToLoop(NewExit, *LI);
  789. assert(NewExit->getTerminator()->getNumSuccessors() == 1 &&
  790. "Exit block should have been split to have one successor!");
  791. BasicBlock *ExitSucc = NewExit->getTerminator()->getSuccessor(0);
  792. // If the successor of the exit block had PHI nodes, add an entry for
  793. // NewExit.
  794. for (BasicBlock::iterator I = ExitSucc->begin();
  795. PHINode *PN = dyn_cast<PHINode>(I); ++I) {
  796. Value *V = PN->getIncomingValueForBlock(ExitBlocks[i]);
  797. ValueToValueMapTy::iterator It = VMap.find(V);
  798. if (It != VMap.end()) V = It->second;
  799. PN->addIncoming(V, NewExit);
  800. }
  801. if (LandingPadInst *LPad = NewExit->getLandingPadInst()) {
  802. PHINode *PN = PHINode::Create(LPad->getType(), 0, "",
  803. ExitSucc->getFirstInsertionPt());
  804. for (pred_iterator I = pred_begin(ExitSucc), E = pred_end(ExitSucc);
  805. I != E; ++I) {
  806. BasicBlock *BB = *I;
  807. LandingPadInst *LPI = BB->getLandingPadInst();
  808. LPI->replaceAllUsesWith(PN);
  809. PN->addIncoming(LPI, BB);
  810. }
  811. }
  812. }
  813. // Rewrite the code to refer to itself.
  814. for (unsigned i = 0, e = NewBlocks.size(); i != e; ++i)
  815. for (BasicBlock::iterator I = NewBlocks[i]->begin(),
  816. E = NewBlocks[i]->end(); I != E; ++I)
  817. RemapInstruction(I, VMap,RF_NoModuleLevelChanges|RF_IgnoreMissingEntries);
  818. // Rewrite the original preheader to select between versions of the loop.
  819. BranchInst *OldBR = cast<BranchInst>(loopPreheader->getTerminator());
  820. assert(OldBR->isUnconditional() && OldBR->getSuccessor(0) == LoopBlocks[0] &&
  821. "Preheader splitting did not work correctly!");
  822. // Emit the new branch that selects between the two versions of this loop.
  823. EmitPreheaderBranchOnCondition(LIC, Val, NewBlocks[0], LoopBlocks[0], OldBR,
  824. TI);
  825. LPM->deleteSimpleAnalysisValue(OldBR, L);
  826. OldBR->eraseFromParent();
  827. LoopProcessWorklist.push_back(NewLoop);
  828. redoLoop = true;
  829. // Keep a WeakVH holding onto LIC. If the first call to RewriteLoopBody
  830. // deletes the instruction (for example by simplifying a PHI that feeds into
  831. // the condition that we're unswitching on), we don't rewrite the second
  832. // iteration.
  833. WeakVH LICHandle(LIC);
  834. // Now we rewrite the original code to know that the condition is true and the
  835. // new code to know that the condition is false.
  836. RewriteLoopBodyWithConditionConstant(L, LIC, Val, false);
  837. // It's possible that simplifying one loop could cause the other to be
  838. // changed to another value or a constant. If its a constant, don't simplify
  839. // it.
  840. if (!LoopProcessWorklist.empty() && LoopProcessWorklist.back() == NewLoop &&
  841. LICHandle && !isa<Constant>(LICHandle))
  842. RewriteLoopBodyWithConditionConstant(NewLoop, LICHandle, Val, true);
  843. }
  844. /// RemoveFromWorklist - Remove all instances of I from the worklist vector
  845. /// specified.
  846. static void RemoveFromWorklist(Instruction *I,
  847. std::vector<Instruction*> &Worklist) {
  848. Worklist.erase(std::remove(Worklist.begin(), Worklist.end(), I),
  849. Worklist.end());
  850. }
  851. /// ReplaceUsesOfWith - When we find that I really equals V, remove I from the
  852. /// program, replacing all uses with V and update the worklist.
  853. static void ReplaceUsesOfWith(Instruction *I, Value *V,
  854. std::vector<Instruction*> &Worklist,
  855. Loop *L, LPPassManager *LPM) {
  856. DEBUG(dbgs() << "Replace with '" << *V << "': " << *I);
  857. // Add uses to the worklist, which may be dead now.
  858. for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
  859. if (Instruction *Use = dyn_cast<Instruction>(I->getOperand(i)))
  860. Worklist.push_back(Use);
  861. // Add users to the worklist which may be simplified now.
  862. for (User *U : I->users())
  863. Worklist.push_back(cast<Instruction>(U));
  864. LPM->deleteSimpleAnalysisValue(I, L);
  865. RemoveFromWorklist(I, Worklist);
  866. I->replaceAllUsesWith(V);
  867. I->eraseFromParent();
  868. ++NumSimplify;
  869. }
  870. // RewriteLoopBodyWithConditionConstant - We know either that the value LIC has
  871. // the value specified by Val in the specified loop, or we know it does NOT have
  872. // that value. Rewrite any uses of LIC or of properties correlated to it.
  873. void LoopUnswitch::RewriteLoopBodyWithConditionConstant(Loop *L, Value *LIC,
  874. Constant *Val,
  875. bool IsEqual) {
  876. assert(!isa<Constant>(LIC) && "Why are we unswitching on a constant?");
  877. // FIXME: Support correlated properties, like:
  878. // for (...)
  879. // if (li1 < li2)
  880. // ...
  881. // if (li1 > li2)
  882. // ...
  883. // FOLD boolean conditions (X|LIC), (X&LIC). Fold conditional branches,
  884. // selects, switches.
  885. std::vector<Instruction*> Worklist;
  886. LLVMContext &Context = Val->getContext();
  887. // If we know that LIC == Val, or that LIC == NotVal, just replace uses of LIC
  888. // in the loop with the appropriate one directly.
  889. if (IsEqual || (isa<ConstantInt>(Val) &&
  890. Val->getType()->isIntegerTy(1))) {
  891. Value *Replacement;
  892. if (IsEqual)
  893. Replacement = Val;
  894. else
  895. Replacement = ConstantInt::get(Type::getInt1Ty(Val->getContext()),
  896. !cast<ConstantInt>(Val)->getZExtValue());
  897. for (User *U : LIC->users()) {
  898. Instruction *UI = dyn_cast<Instruction>(U);
  899. if (!UI || !L->contains(UI))
  900. continue;
  901. Worklist.push_back(UI);
  902. }
  903. for (std::vector<Instruction*>::iterator UI = Worklist.begin(),
  904. UE = Worklist.end(); UI != UE; ++UI)
  905. (*UI)->replaceUsesOfWith(LIC, Replacement);
  906. SimplifyCode(Worklist, L);
  907. return;
  908. }
  909. // Otherwise, we don't know the precise value of LIC, but we do know that it
  910. // is certainly NOT "Val". As such, simplify any uses in the loop that we
  911. // can. This case occurs when we unswitch switch statements.
  912. for (User *U : LIC->users()) {
  913. Instruction *UI = dyn_cast<Instruction>(U);
  914. if (!UI || !L->contains(UI))
  915. continue;
  916. Worklist.push_back(UI);
  917. // TODO: We could do other simplifications, for example, turning
  918. // 'icmp eq LIC, Val' -> false.
  919. // If we know that LIC is not Val, use this info to simplify code.
  920. SwitchInst *SI = dyn_cast<SwitchInst>(UI);
  921. if (!SI || !isa<ConstantInt>(Val)) continue;
  922. SwitchInst::CaseIt DeadCase = SI->findCaseValue(cast<ConstantInt>(Val));
  923. // Default case is live for multiple values.
  924. if (DeadCase == SI->case_default()) continue;
  925. // Found a dead case value. Don't remove PHI nodes in the
  926. // successor if they become single-entry, those PHI nodes may
  927. // be in the Users list.
  928. BasicBlock *Switch = SI->getParent();
  929. BasicBlock *SISucc = DeadCase.getCaseSuccessor();
  930. BasicBlock *Latch = L->getLoopLatch();
  931. BranchesInfo.setUnswitched(SI, Val);
  932. if (!SI->findCaseDest(SISucc)) continue; // Edge is critical.
  933. // If the DeadCase successor dominates the loop latch, then the
  934. // transformation isn't safe since it will delete the sole predecessor edge
  935. // to the latch.
  936. if (Latch && DT->dominates(SISucc, Latch))
  937. continue;
  938. // FIXME: This is a hack. We need to keep the successor around
  939. // and hooked up so as to preserve the loop structure, because
  940. // trying to update it is complicated. So instead we preserve the
  941. // loop structure and put the block on a dead code path.
  942. SplitEdge(Switch, SISucc, DT, LI);
  943. // Compute the successors instead of relying on the return value
  944. // of SplitEdge, since it may have split the switch successor
  945. // after PHI nodes.
  946. BasicBlock *NewSISucc = DeadCase.getCaseSuccessor();
  947. BasicBlock *OldSISucc = *succ_begin(NewSISucc);
  948. // Create an "unreachable" destination.
  949. BasicBlock *Abort = BasicBlock::Create(Context, "us-unreachable",
  950. Switch->getParent(),
  951. OldSISucc);
  952. new UnreachableInst(Context, Abort);
  953. // Force the new case destination to branch to the "unreachable"
  954. // block while maintaining a (dead) CFG edge to the old block.
  955. NewSISucc->getTerminator()->eraseFromParent();
  956. BranchInst::Create(Abort, OldSISucc,
  957. ConstantInt::getTrue(Context), NewSISucc);
  958. // Release the PHI operands for this edge.
  959. for (BasicBlock::iterator II = NewSISucc->begin();
  960. PHINode *PN = dyn_cast<PHINode>(II); ++II)
  961. PN->setIncomingValue(PN->getBasicBlockIndex(Switch),
  962. UndefValue::get(PN->getType()));
  963. // Tell the domtree about the new block. We don't fully update the
  964. // domtree here -- instead we force it to do a full recomputation
  965. // after the pass is complete -- but we do need to inform it of
  966. // new blocks.
  967. if (DT)
  968. DT->addNewBlock(Abort, NewSISucc);
  969. }
  970. SimplifyCode(Worklist, L);
  971. }
  972. /// SimplifyCode - Okay, now that we have simplified some instructions in the
  973. /// loop, walk over it and constant prop, dce, and fold control flow where
  974. /// possible. Note that this is effectively a very simple loop-structure-aware
  975. /// optimizer. During processing of this loop, L could very well be deleted, so
  976. /// it must not be used.
  977. ///
  978. /// FIXME: When the loop optimizer is more mature, separate this out to a new
  979. /// pass.
  980. ///
  981. void LoopUnswitch::SimplifyCode(std::vector<Instruction*> &Worklist, Loop *L) {
  982. const DataLayout &DL = L->getHeader()->getModule()->getDataLayout();
  983. while (!Worklist.empty()) {
  984. Instruction *I = Worklist.back();
  985. Worklist.pop_back();
  986. // Simple DCE.
  987. if (isInstructionTriviallyDead(I)) {
  988. DEBUG(dbgs() << "Remove dead instruction '" << *I);
  989. // Add uses to the worklist, which may be dead now.
  990. for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
  991. if (Instruction *Use = dyn_cast<Instruction>(I->getOperand(i)))
  992. Worklist.push_back(Use);
  993. LPM->deleteSimpleAnalysisValue(I, L);
  994. RemoveFromWorklist(I, Worklist);
  995. I->eraseFromParent();
  996. ++NumSimplify;
  997. continue;
  998. }
  999. // See if instruction simplification can hack this up. This is common for
  1000. // things like "select false, X, Y" after unswitching made the condition be
  1001. // 'false'. TODO: update the domtree properly so we can pass it here.
  1002. if (Value *V = SimplifyInstruction(I, DL))
  1003. if (LI->replacementPreservesLCSSAForm(I, V)) {
  1004. ReplaceUsesOfWith(I, V, Worklist, L, LPM);
  1005. continue;
  1006. }
  1007. // Special case hacks that appear commonly in unswitched code.
  1008. if (BranchInst *BI = dyn_cast<BranchInst>(I)) {
  1009. if (BI->isUnconditional()) {
  1010. // If BI's parent is the only pred of the successor, fold the two blocks
  1011. // together.
  1012. BasicBlock *Pred = BI->getParent();
  1013. BasicBlock *Succ = BI->getSuccessor(0);
  1014. BasicBlock *SinglePred = Succ->getSinglePredecessor();
  1015. if (!SinglePred) continue; // Nothing to do.
  1016. assert(SinglePred == Pred && "CFG broken");
  1017. DEBUG(dbgs() << "Merging blocks: " << Pred->getName() << " <- "
  1018. << Succ->getName() << "\n");
  1019. // Resolve any single entry PHI nodes in Succ.
  1020. while (PHINode *PN = dyn_cast<PHINode>(Succ->begin()))
  1021. ReplaceUsesOfWith(PN, PN->getIncomingValue(0), Worklist, L, LPM);
  1022. // If Succ has any successors with PHI nodes, update them to have
  1023. // entries coming from Pred instead of Succ.
  1024. Succ->replaceAllUsesWith(Pred);
  1025. // Move all of the successor contents from Succ to Pred.
  1026. Pred->getInstList().splice(BI, Succ->getInstList(), Succ->begin(),
  1027. Succ->end());
  1028. LPM->deleteSimpleAnalysisValue(BI, L);
  1029. BI->eraseFromParent();
  1030. RemoveFromWorklist(BI, Worklist);
  1031. // Remove Succ from the loop tree.
  1032. LI->removeBlock(Succ);
  1033. LPM->deleteSimpleAnalysisValue(Succ, L);
  1034. Succ->eraseFromParent();
  1035. ++NumSimplify;
  1036. continue;
  1037. }
  1038. continue;
  1039. }
  1040. }
  1041. }