IndVarSimplify.cpp revision f451cb870efcf9e0302d25ed05f4cac6bb494e42
16148c02591bd83da7b957589c4bbf6f9720d503fChris Lattner//===- IndVarSimplify.cpp - Induction Variable Elimination ----------------===//
2fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman//
3b576c94c15af9a440f69d9d03c2afead7971118cJohn Criswell//                     The LLVM Compiler Infrastructure
4b576c94c15af9a440f69d9d03c2afead7971118cJohn Criswell//
54ee451de366474b9c228b4e5fa573795a715216dChris Lattner// This file is distributed under the University of Illinois Open Source
64ee451de366474b9c228b4e5fa573795a715216dChris Lattner// License. See LICENSE.TXT for details.
7fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman//
8b576c94c15af9a440f69d9d03c2afead7971118cJohn Criswell//===----------------------------------------------------------------------===//
96148c02591bd83da7b957589c4bbf6f9720d503fChris Lattner//
1040bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner// This transformation analyzes and transforms the induction variables (and
1140bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner// computations derived from them) into simpler forms suitable for subsequent
1240bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner// analysis and transformation.
1340bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner//
1447a53ac726ceb1ac11bc1326be3fbe095f726b0dReid Spencer// This transformation makes the following changes to each loop with an
1540bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner// identifiable induction variable:
1640bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner//   1. All loops are transformed to have a SINGLE canonical induction variable
1740bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner//      which starts at zero and steps by one.
1840bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner//   2. The canonical induction variable is guaranteed to be the first PHI node
1940bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner//      in the loop header block.
20ea73f3c2e14d84bb4cb07bd6a1a3d7915f3aff83Dan Gohman//   3. The canonical induction variable is guaranteed to be in a wide enough
21ea73f3c2e14d84bb4cb07bd6a1a3d7915f3aff83Dan Gohman//      type so that IV expressions need not be (directly) zero-extended or
22ea73f3c2e14d84bb4cb07bd6a1a3d7915f3aff83Dan Gohman//      sign-extended.
23ea73f3c2e14d84bb4cb07bd6a1a3d7915f3aff83Dan Gohman//   4. Any pointer arithmetic recurrences are raised to use array subscripts.
2440bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner//
2540bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner// If the trip count of a loop is computable, this pass also makes the following
2640bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner// changes:
2740bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner//   1. The exit condition for the loop is canonicalized to compare the
2840bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner//      induction value against the exit value.  This turns loops like:
2940bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner//        'for (i = 7; i*i < 1000; ++i)' into 'for (i = 0; i != 25; ++i)'
3040bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner//   2. Any use outside of the loop of an expression derived from the indvar
3140bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner//      is changed to compute the derived value outside of the loop, eliminating
3240bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner//      the dependence on the exit value of the induction variable.  If the only
3340bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner//      purpose of the loop is to compute the exit value of some derived
3440bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner//      expression, this transformation will make the loop dead.
3540bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner//
3640bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner// This transformation should be followed by strength reduction after all of the
37c2c4cbf7b3a0229cf1804ca9d7c18113f75bab46Dan Gohman// desired loop transformations have been performed.
386148c02591bd83da7b957589c4bbf6f9720d503fChris Lattner//
396148c02591bd83da7b957589c4bbf6f9720d503fChris Lattner//===----------------------------------------------------------------------===//
406148c02591bd83da7b957589c4bbf6f9720d503fChris Lattner
410e5f499638c8d277b9dc4a4385712177c53b5681Chris Lattner#define DEBUG_TYPE "indvars"
42022103b3f33febb7e54b8fdf2c9bc461eea78cbaChris Lattner#include "llvm/Transforms/Scalar.h"
4340bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner#include "llvm/BasicBlock.h"
4459fdaeeae8f183e18bb6ad5c382ca23e28e6aaf6Chris Lattner#include "llvm/Constants.h"
4518b3c97bc773b24a66eb779e85da1820b0f16b31Chris Lattner#include "llvm/Instructions.h"
46d672ecb0178c6247a5eaa5b0fb0c3b23cd25bd7cOwen Anderson#include "llvm/LLVMContext.h"
4740bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner#include "llvm/Type.h"
4881db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman#include "llvm/Analysis/Dominators.h"
4981db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman#include "llvm/Analysis/IVUsers.h"
5036f891bdf6cf38fcc655a0930ca18664e18518d4Nate Begeman#include "llvm/Analysis/ScalarEvolutionExpander.h"
5147df12d80db90e125e9f2ff764286ee11665476dJohn Criswell#include "llvm/Analysis/LoopInfo.h"
525ee99979065d75605d150d7e567e4351024aae8fDevang Patel#include "llvm/Analysis/LoopPass.h"
53455889aa79e3463a4b0f2161e3d9d72a683268b6Chris Lattner#include "llvm/Support/CFG.h"
54bdff548e4dd577a72094d57b282de4e765643b96Chris Lattner#include "llvm/Support/CommandLine.h"
55ee4f13a9046c380725cdeab62d57722db375c473Chris Lattner#include "llvm/Support/Debug.h"
56bdff548e4dd577a72094d57b282de4e765643b96Chris Lattner#include "llvm/Support/raw_ostream.h"
5747df12d80db90e125e9f2ff764286ee11665476dJohn Criswell#include "llvm/Transforms/Utils/Local.h"
5881db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman#include "llvm/Transforms/Utils/BasicBlockUtils.h"
59a54b7cbd452b3adb2f51346140d996b29c2cdb30Reid Spencer#include "llvm/ADT/SmallVector.h"
60551ccae044b0ff658fe629dd67edd5ffe75d10e8Reid Spencer#include "llvm/ADT/Statistic.h"
6181db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman#include "llvm/ADT/STLExtras.h"
6247df12d80db90e125e9f2ff764286ee11665476dJohn Criswellusing namespace llvm;
63d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke
640e5f499638c8d277b9dc4a4385712177c53b5681Chris LattnerSTATISTIC(NumRemoved , "Number of aux indvars removed");
650e5f499638c8d277b9dc4a4385712177c53b5681Chris LattnerSTATISTIC(NumInserted, "Number of canonical indvars added");
660e5f499638c8d277b9dc4a4385712177c53b5681Chris LattnerSTATISTIC(NumReplaced, "Number of exit values replaced");
670e5f499638c8d277b9dc4a4385712177c53b5681Chris LattnerSTATISTIC(NumLFTR    , "Number of loop exit tests replaced");
683324e718bc9ac2ede08a14c325848b576849542bChris Lattner
690e5f499638c8d277b9dc4a4385712177c53b5681Chris Lattnernamespace {
703e8b6631e67e01e4960a7ba4668a50c596607473Chris Lattner  class IndVarSimplify : public LoopPass {
7181db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman    IVUsers         *IU;
7240bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner    LoopInfo        *LI;
7340bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner    ScalarEvolution *SE;
74de53dc03f5c1549f3176e979bbeeac965dfa5cbcDan Gohman    DominatorTree   *DT;
7515cad759fe2048ac5eb137c6bb0ab7287538677eChris Lattner    bool Changed;
763324e718bc9ac2ede08a14c325848b576849542bChris Lattner  public:
77794fd75c67a2cdc128d67342c6d88a504d186896Devang Patel
785668cf77a77493ec9f2a9b33f08125e885c8e4cfDan Gohman    static char ID; // Pass identification, replacement for typeid
795668cf77a77493ec9f2a9b33f08125e885c8e4cfDan Gohman    IndVarSimplify() : LoopPass(&ID) {}
80794fd75c67a2cdc128d67342c6d88a504d186896Devang Patel
815668cf77a77493ec9f2a9b33f08125e885c8e4cfDan Gohman    virtual bool runOnLoop(Loop *L, LPPassManager &LPM);
8260f8a63e2502d57e879bf52a4a48505b74fa9716Dan Gohman
835668cf77a77493ec9f2a9b33f08125e885c8e4cfDan Gohman    virtual void getAnalysisUsage(AnalysisUsage &AU) const {
845668cf77a77493ec9f2a9b33f08125e885c8e4cfDan Gohman      AU.addRequired<DominatorTree>();
855668cf77a77493ec9f2a9b33f08125e885c8e4cfDan Gohman      AU.addRequired<LoopInfo>();
865668cf77a77493ec9f2a9b33f08125e885c8e4cfDan Gohman      AU.addRequired<ScalarEvolution>();
875668cf77a77493ec9f2a9b33f08125e885c8e4cfDan Gohman      AU.addRequiredID(LoopSimplifyID);
885668cf77a77493ec9f2a9b33f08125e885c8e4cfDan Gohman      AU.addRequiredID(LCSSAID);
895668cf77a77493ec9f2a9b33f08125e885c8e4cfDan Gohman      AU.addRequired<IVUsers>();
905668cf77a77493ec9f2a9b33f08125e885c8e4cfDan Gohman      AU.addPreserved<ScalarEvolution>();
915668cf77a77493ec9f2a9b33f08125e885c8e4cfDan Gohman      AU.addPreservedID(LoopSimplifyID);
925668cf77a77493ec9f2a9b33f08125e885c8e4cfDan Gohman      AU.addPreservedID(LCSSAID);
935668cf77a77493ec9f2a9b33f08125e885c8e4cfDan Gohman      AU.addPreserved<IVUsers>();
945668cf77a77493ec9f2a9b33f08125e885c8e4cfDan Gohman      AU.setPreservesCFG();
955668cf77a77493ec9f2a9b33f08125e885c8e4cfDan Gohman    }
963324e718bc9ac2ede08a14c325848b576849542bChris Lattner
9740bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner  private:
985ee99979065d75605d150d7e567e4351024aae8fDevang Patel
9960f8a63e2502d57e879bf52a4a48505b74fa9716Dan Gohman    void RewriteNonIntegerIVs(Loop *L);
10060f8a63e2502d57e879bf52a4a48505b74fa9716Dan Gohman
1010bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman    ICmpInst *LinearFunctionTestReplace(Loop *L, const SCEV *BackedgeTakenCount,
102a575871884b247b0946290876ac7d4657b384cf9Dan Gohman                                   Value *IndVar,
103c2390b14c91764cba6e4394d05e58e387a7dfb19Dan Gohman                                   BasicBlock *ExitingBlock,
104c2390b14c91764cba6e4394d05e58e387a7dfb19Dan Gohman                                   BranchInst *BI,
10515cab2817b8f63fec0148609278bc2c1e05abb50Dan Gohman                                   SCEVExpander &Rewriter);
106667d787c0a21cf3f5dfcde03ca471162ba35b614Dan Gohman    void RewriteLoopExitValues(Loop *L, const SCEV *BackedgeTakenCount,
107667d787c0a21cf3f5dfcde03ca471162ba35b614Dan Gohman                               SCEVExpander &Rewriter);
10840bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner
10981db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman    void RewriteIVExpressions(Loop *L, const Type *LargestType,
110667d787c0a21cf3f5dfcde03ca471162ba35b614Dan Gohman                              SCEVExpander &Rewriter);
111d22a849282c45bbf7eb1734c274294d81e49e3a8Devang Patel
112667d787c0a21cf3f5dfcde03ca471162ba35b614Dan Gohman    void SinkUnusedInvariants(Loop *L);
11381db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman
11481db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman    void HandleFloatingPointIV(Loop *L, PHINode *PH);
1153324e718bc9ac2ede08a14c325848b576849542bChris Lattner  };
1165e76140536ba66fadeced1cd892f79616f407e3cChris Lattner}
117394437ff7eaccfe1de92fe14d0022ca0addf3e41Chris Lattner
118844731a7f1909f55935e3514c9e713a62d67662eDan Gohmanchar IndVarSimplify::ID = 0;
119844731a7f1909f55935e3514c9e713a62d67662eDan Gohmanstatic RegisterPass<IndVarSimplify>
120844731a7f1909f55935e3514c9e713a62d67662eDan GohmanX("indvars", "Canonicalize Induction Variables");
121844731a7f1909f55935e3514c9e713a62d67662eDan Gohman
122394f0441e06dafca29f0752cf400990a5b8fe4b1Daniel DunbarPass *llvm::createIndVarSimplifyPass() {
1233324e718bc9ac2ede08a14c325848b576849542bChris Lattner  return new IndVarSimplify();
124394437ff7eaccfe1de92fe14d0022ca0addf3e41Chris Lattner}
125394437ff7eaccfe1de92fe14d0022ca0addf3e41Chris Lattner
12640bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner/// LinearFunctionTestReplace - This method rewrites the exit condition of the
12759fdaeeae8f183e18bb6ad5c382ca23e28e6aaf6Chris Lattner/// loop to be a canonical != comparison against the incremented loop induction
12859fdaeeae8f183e18bb6ad5c382ca23e28e6aaf6Chris Lattner/// variable.  This pass is able to rewrite the exit tests of any loop where the
12959fdaeeae8f183e18bb6ad5c382ca23e28e6aaf6Chris Lattner/// SCEV analysis can determine a loop-invariant trip count of the loop, which
13059fdaeeae8f183e18bb6ad5c382ca23e28e6aaf6Chris Lattner/// is actually a much broader range than just linear tests.
13181db61a2e6d3c95a2738c3559a108e05e9d7a05aDan GohmanICmpInst *IndVarSimplify::LinearFunctionTestReplace(Loop *L,
1320bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman                                   const SCEV *BackedgeTakenCount,
133c2390b14c91764cba6e4394d05e58e387a7dfb19Dan Gohman                                   Value *IndVar,
134c2390b14c91764cba6e4394d05e58e387a7dfb19Dan Gohman                                   BasicBlock *ExitingBlock,
135c2390b14c91764cba6e4394d05e58e387a7dfb19Dan Gohman                                   BranchInst *BI,
13615cab2817b8f63fec0148609278bc2c1e05abb50Dan Gohman                                   SCEVExpander &Rewriter) {
137d244057a48660c3cd30d219118ece3f947947790Chris Lattner  // If the exiting block is not the same as the backedge block, we must compare
138d244057a48660c3cd30d219118ece3f947947790Chris Lattner  // against the preincremented value, otherwise we prefer to compare against
139d244057a48660c3cd30d219118ece3f947947790Chris Lattner  // the post-incremented value.
140c2390b14c91764cba6e4394d05e58e387a7dfb19Dan Gohman  Value *CmpIndVar;
1410bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman  const SCEV *RHS = BackedgeTakenCount;
142c2390b14c91764cba6e4394d05e58e387a7dfb19Dan Gohman  if (ExitingBlock == L->getLoopLatch()) {
14346bdfb0e6bb9de86b19562fc52fddefd7014cf73Dan Gohman    // Add one to the "backedge-taken" count to get the trip count.
14446bdfb0e6bb9de86b19562fc52fddefd7014cf73Dan Gohman    // If this addition may overflow, we have to be more pessimistic and
14546bdfb0e6bb9de86b19562fc52fddefd7014cf73Dan Gohman    // cast the induction variable before doing the add.
1460bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman    const SCEV *Zero = SE->getIntegerSCEV(0, BackedgeTakenCount->getType());
1470bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman    const SCEV *N =
14846bdfb0e6bb9de86b19562fc52fddefd7014cf73Dan Gohman      SE->getAddExpr(BackedgeTakenCount,
14946bdfb0e6bb9de86b19562fc52fddefd7014cf73Dan Gohman                     SE->getIntegerSCEV(1, BackedgeTakenCount->getType()));
150c2390b14c91764cba6e4394d05e58e387a7dfb19Dan Gohman    if ((isa<SCEVConstant>(N) && !N->isZero()) ||
151c2390b14c91764cba6e4394d05e58e387a7dfb19Dan Gohman        SE->isLoopGuardedByCond(L, ICmpInst::ICMP_NE, N, Zero)) {
152c2390b14c91764cba6e4394d05e58e387a7dfb19Dan Gohman      // No overflow. Cast the sum.
15346bdfb0e6bb9de86b19562fc52fddefd7014cf73Dan Gohman      RHS = SE->getTruncateOrZeroExtend(N, IndVar->getType());
154c2390b14c91764cba6e4394d05e58e387a7dfb19Dan Gohman    } else {
155c2390b14c91764cba6e4394d05e58e387a7dfb19Dan Gohman      // Potential overflow. Cast before doing the add.
15646bdfb0e6bb9de86b19562fc52fddefd7014cf73Dan Gohman      RHS = SE->getTruncateOrZeroExtend(BackedgeTakenCount,
15746bdfb0e6bb9de86b19562fc52fddefd7014cf73Dan Gohman                                        IndVar->getType());
15846bdfb0e6bb9de86b19562fc52fddefd7014cf73Dan Gohman      RHS = SE->getAddExpr(RHS,
15946bdfb0e6bb9de86b19562fc52fddefd7014cf73Dan Gohman                           SE->getIntegerSCEV(1, IndVar->getType()));
160c2390b14c91764cba6e4394d05e58e387a7dfb19Dan Gohman    }
161c2390b14c91764cba6e4394d05e58e387a7dfb19Dan Gohman
16246bdfb0e6bb9de86b19562fc52fddefd7014cf73Dan Gohman    // The BackedgeTaken expression contains the number of times that the
16346bdfb0e6bb9de86b19562fc52fddefd7014cf73Dan Gohman    // backedge branches to the loop header.  This is one less than the
16446bdfb0e6bb9de86b19562fc52fddefd7014cf73Dan Gohman    // number of times the loop executes, so use the incremented indvar.
165c2390b14c91764cba6e4394d05e58e387a7dfb19Dan Gohman    CmpIndVar = L->getCanonicalInductionVariableIncrement();
166d244057a48660c3cd30d219118ece3f947947790Chris Lattner  } else {
167d244057a48660c3cd30d219118ece3f947947790Chris Lattner    // We have to use the preincremented value...
16846bdfb0e6bb9de86b19562fc52fddefd7014cf73Dan Gohman    RHS = SE->getTruncateOrZeroExtend(BackedgeTakenCount,
16946bdfb0e6bb9de86b19562fc52fddefd7014cf73Dan Gohman                                      IndVar->getType());
170c2390b14c91764cba6e4394d05e58e387a7dfb19Dan Gohman    CmpIndVar = IndVar;
171d244057a48660c3cd30d219118ece3f947947790Chris Lattner  }
17259fdaeeae8f183e18bb6ad5c382ca23e28e6aaf6Chris Lattner
173667d787c0a21cf3f5dfcde03ca471162ba35b614Dan Gohman  // Expand the code for the iteration count.
17440a5a1b39ee1cd40ff9d04740386b667fb27b340Dan Gohman  assert(RHS->isLoopInvariant(L) &&
17540a5a1b39ee1cd40ff9d04740386b667fb27b340Dan Gohman         "Computed iteration count is not loop invariant!");
176667d787c0a21cf3f5dfcde03ca471162ba35b614Dan Gohman  Value *ExitCnt = Rewriter.expandCodeFor(RHS, IndVar->getType(), BI);
17740bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner
178e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer  // Insert a new icmp_ne or icmp_eq instruction before the branch.
179e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer  ICmpInst::Predicate Opcode;
18040bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner  if (L->contains(BI->getSuccessor(0)))
181e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer    Opcode = ICmpInst::ICMP_NE;
18240bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner  else
183e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer    Opcode = ICmpInst::ICMP_EQ;
18440bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner
185f67ef318aa29e7cc0c7de76349881959936d9eeeDavid Greene  DEBUG(dbgs() << "INDVARS: Rewriting loop exit condition to:\n"
186bdff548e4dd577a72094d57b282de4e765643b96Chris Lattner               << "      LHS:" << *CmpIndVar << '\n'
187bdff548e4dd577a72094d57b282de4e765643b96Chris Lattner               << "       op:\t"
188bdff548e4dd577a72094d57b282de4e765643b96Chris Lattner               << (Opcode == ICmpInst::ICMP_NE ? "!=" : "==") << "\n"
189bdff548e4dd577a72094d57b282de4e765643b96Chris Lattner               << "      RHS:\t" << *RHS << "\n");
190c2390b14c91764cba6e4394d05e58e387a7dfb19Dan Gohman
191333c40096561218bc3597cf153c0a3895274414cOwen Anderson  ICmpInst *Cond = new ICmpInst(BI, Opcode, CmpIndVar, ExitCnt, "exitcond");
19281db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman
19381db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman  Instruction *OrigCond = cast<Instruction>(BI->getCondition());
19495bdbfa0668cc2b475429fbc6046f364bc01edf7Dan Gohman  // It's tempting to use replaceAllUsesWith here to fully replace the old
19595bdbfa0668cc2b475429fbc6046f364bc01edf7Dan Gohman  // comparison, but that's not immediately safe, since users of the old
19695bdbfa0668cc2b475429fbc6046f364bc01edf7Dan Gohman  // comparison may not be dominated by the new comparison. Instead, just
19795bdbfa0668cc2b475429fbc6046f364bc01edf7Dan Gohman  // update the branch to use the new comparison; in the common case this
19895bdbfa0668cc2b475429fbc6046f364bc01edf7Dan Gohman  // will make old comparison dead.
19995bdbfa0668cc2b475429fbc6046f364bc01edf7Dan Gohman  BI->setCondition(Cond);
20081db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman  RecursivelyDeleteTriviallyDeadInstructions(OrigCond);
20181db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman
20240bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner  ++NumLFTR;
20340bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner  Changed = true;
20481db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman  return Cond;
20540bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner}
2063324e718bc9ac2ede08a14c325848b576849542bChris Lattner
20740bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner/// RewriteLoopExitValues - Check to see if this loop has a computable
20840bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner/// loop-invariant execution count.  If so, this means that we can compute the
20940bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner/// final value of any expressions that are recurrent in the loop, and
21040bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner/// substitute the exit values from the loop into any instructions outside of
21140bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner/// the loop that use the final values of the current expressions.
21281db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman///
21381db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman/// This is mostly redundant with the regular IndVarSimplify activities that
21481db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman/// happen later, except that it's more powerful in some cases, because it's
21581db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman/// able to brute-force evaluate arbitrary instructions as long as they have
21681db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman/// constant operands at the beginning of the loop.
217890f92b744fb074465bc2b7006ee753a181f62a4Dan Gohmanvoid IndVarSimplify::RewriteLoopExitValues(Loop *L,
218667d787c0a21cf3f5dfcde03ca471162ba35b614Dan Gohman                                           const SCEV *BackedgeTakenCount,
219667d787c0a21cf3f5dfcde03ca471162ba35b614Dan Gohman                                           SCEVExpander &Rewriter) {
22081db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman  // Verify the input to the pass in already in LCSSA form.
22181db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman  assert(L->isLCSSAForm());
22281db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman
223b7211a2ce13a0365e0e1dd2f27adda2ee3d1288bDevang Patel  SmallVector<BasicBlock*, 8> ExitBlocks;
2249f3d73886612fd06812fb63cf8e6fa10db9b17e7Chris Lattner  L->getUniqueExitBlocks(ExitBlocks);
2259f3d73886612fd06812fb63cf8e6fa10db9b17e7Chris Lattner
2269f3d73886612fd06812fb63cf8e6fa10db9b17e7Chris Lattner  // Find all values that are computed inside the loop, but used outside of it.
2279f3d73886612fd06812fb63cf8e6fa10db9b17e7Chris Lattner  // Because of LCSSA, these values will only occur in LCSSA PHI Nodes.  Scan
2289f3d73886612fd06812fb63cf8e6fa10db9b17e7Chris Lattner  // the exit blocks of the loop to find them.
2299f3d73886612fd06812fb63cf8e6fa10db9b17e7Chris Lattner  for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i) {
2309f3d73886612fd06812fb63cf8e6fa10db9b17e7Chris Lattner    BasicBlock *ExitBB = ExitBlocks[i];
231cafb81337bdf2574049533f5b8a6bc1ed92f1f3eDan Gohman
2329f3d73886612fd06812fb63cf8e6fa10db9b17e7Chris Lattner    // If there are no PHI nodes in this exit block, then no values defined
2339f3d73886612fd06812fb63cf8e6fa10db9b17e7Chris Lattner    // inside the loop are used on this path, skip it.
2349f3d73886612fd06812fb63cf8e6fa10db9b17e7Chris Lattner    PHINode *PN = dyn_cast<PHINode>(ExitBB->begin());
2359f3d73886612fd06812fb63cf8e6fa10db9b17e7Chris Lattner    if (!PN) continue;
236cafb81337bdf2574049533f5b8a6bc1ed92f1f3eDan Gohman
2379f3d73886612fd06812fb63cf8e6fa10db9b17e7Chris Lattner    unsigned NumPreds = PN->getNumIncomingValues();
238cafb81337bdf2574049533f5b8a6bc1ed92f1f3eDan Gohman
2399f3d73886612fd06812fb63cf8e6fa10db9b17e7Chris Lattner    // Iterate over all of the PHI nodes.
2409f3d73886612fd06812fb63cf8e6fa10db9b17e7Chris Lattner    BasicBlock::iterator BBI = ExitBB->begin();
2419f3d73886612fd06812fb63cf8e6fa10db9b17e7Chris Lattner    while ((PN = dyn_cast<PHINode>(BBI++))) {
2423790fb0c036acaa4db50aff83dd8b3bf51f8af6aTorok Edwin      if (PN->use_empty())
2433790fb0c036acaa4db50aff83dd8b3bf51f8af6aTorok Edwin        continue; // dead use, don't replace it
2449f3d73886612fd06812fb63cf8e6fa10db9b17e7Chris Lattner      // Iterate over all of the values in all the PHI nodes.
2459f3d73886612fd06812fb63cf8e6fa10db9b17e7Chris Lattner      for (unsigned i = 0; i != NumPreds; ++i) {
2469f3d73886612fd06812fb63cf8e6fa10db9b17e7Chris Lattner        // If the value being merged in is not integer or is not defined
2479f3d73886612fd06812fb63cf8e6fa10db9b17e7Chris Lattner        // in the loop, skip it.
2489f3d73886612fd06812fb63cf8e6fa10db9b17e7Chris Lattner        Value *InVal = PN->getIncomingValue(i);
2499f3d73886612fd06812fb63cf8e6fa10db9b17e7Chris Lattner        if (!isa<Instruction>(InVal) ||
2509f3d73886612fd06812fb63cf8e6fa10db9b17e7Chris Lattner            // SCEV only supports integer expressions for now.
2512d1be87ee40a4a0241d94448173879d9df2bc5b3Dan Gohman            (!isa<IntegerType>(InVal->getType()) &&
2522d1be87ee40a4a0241d94448173879d9df2bc5b3Dan Gohman             !isa<PointerType>(InVal->getType())))
2539f3d73886612fd06812fb63cf8e6fa10db9b17e7Chris Lattner          continue;
2549f3d73886612fd06812fb63cf8e6fa10db9b17e7Chris Lattner
2559f3d73886612fd06812fb63cf8e6fa10db9b17e7Chris Lattner        // If this pred is for a subloop, not L itself, skip it.
256cafb81337bdf2574049533f5b8a6bc1ed92f1f3eDan Gohman        if (LI->getLoopFor(PN->getIncomingBlock(i)) != L)
2579f3d73886612fd06812fb63cf8e6fa10db9b17e7Chris Lattner          continue; // The Block is in a subloop, skip it.
2589f3d73886612fd06812fb63cf8e6fa10db9b17e7Chris Lattner
2599f3d73886612fd06812fb63cf8e6fa10db9b17e7Chris Lattner        // Check that InVal is defined in the loop.
2609f3d73886612fd06812fb63cf8e6fa10db9b17e7Chris Lattner        Instruction *Inst = cast<Instruction>(InVal);
26192329c7fbe572892c17aa2d2542a10e3ea16132fDan Gohman        if (!L->contains(Inst))
2629f3d73886612fd06812fb63cf8e6fa10db9b17e7Chris Lattner          continue;
263cafb81337bdf2574049533f5b8a6bc1ed92f1f3eDan Gohman
2649f3d73886612fd06812fb63cf8e6fa10db9b17e7Chris Lattner        // Okay, this instruction has a user outside of the current loop
2659f3d73886612fd06812fb63cf8e6fa10db9b17e7Chris Lattner        // and varies predictably *inside* the loop.  Evaluate the value it
2669f3d73886612fd06812fb63cf8e6fa10db9b17e7Chris Lattner        // contains when the loop exits, if possible.
2670bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman        const SCEV *ExitValue = SE->getSCEVAtScope(Inst, L->getParentLoop());
268d594e6f0345b3e1e4b640a7099596ca613da16d6Dan Gohman        if (!ExitValue->isLoopInvariant(L))
2699f3d73886612fd06812fb63cf8e6fa10db9b17e7Chris Lattner          continue;
2709f3d73886612fd06812fb63cf8e6fa10db9b17e7Chris Lattner
2719f3d73886612fd06812fb63cf8e6fa10db9b17e7Chris Lattner        Changed = true;
2729f3d73886612fd06812fb63cf8e6fa10db9b17e7Chris Lattner        ++NumReplaced;
273cafb81337bdf2574049533f5b8a6bc1ed92f1f3eDan Gohman
274667d787c0a21cf3f5dfcde03ca471162ba35b614Dan Gohman        Value *ExitVal = Rewriter.expandCodeFor(ExitValue, PN->getType(), Inst);
275cafb81337bdf2574049533f5b8a6bc1ed92f1f3eDan Gohman
276f67ef318aa29e7cc0c7de76349881959936d9eeeDavid Greene        DEBUG(dbgs() << "INDVARS: RLEV: AfterLoopVal = " << *ExitVal << '\n'
277bdff548e4dd577a72094d57b282de4e765643b96Chris Lattner                     << "  LoopVal = " << *Inst << "\n");
2789caed5440d59dac4b388152fe8b993dc0491e5e9Chris Lattner
2799f3d73886612fd06812fb63cf8e6fa10db9b17e7Chris Lattner        PN->setIncomingValue(i, ExitVal);
280cafb81337bdf2574049533f5b8a6bc1ed92f1f3eDan Gohman
28181db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman        // If this instruction is dead now, delete it.
28281db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman        RecursivelyDeleteTriviallyDeadInstructions(Inst);
283cafb81337bdf2574049533f5b8a6bc1ed92f1f3eDan Gohman
28465d1e2b6e74fcd370093cbe518ebc15cbc445ad4Dan Gohman        if (NumPreds == 1) {
28565d1e2b6e74fcd370093cbe518ebc15cbc445ad4Dan Gohman          // Completely replace a single-pred PHI. This is safe, because the
28665d1e2b6e74fcd370093cbe518ebc15cbc445ad4Dan Gohman          // NewVal won't be variant in the loop, so we don't need an LCSSA phi
28765d1e2b6e74fcd370093cbe518ebc15cbc445ad4Dan Gohman          // node anymore.
2889f3d73886612fd06812fb63cf8e6fa10db9b17e7Chris Lattner          PN->replaceAllUsesWith(ExitVal);
28981db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman          RecursivelyDeleteTriviallyDeadInstructions(PN);
290c9838f25d53d3dd7d38949ef6c28f2505a110f45Chris Lattner        }
2914bd09d70cceb3851f7eb1c2f98338b3071d405f3Chris Lattner      }
29265d1e2b6e74fcd370093cbe518ebc15cbc445ad4Dan Gohman      if (NumPreds != 1) {
293667d787c0a21cf3f5dfcde03ca471162ba35b614Dan Gohman        // Clone the PHI and delete the original one. This lets IVUsers and
294667d787c0a21cf3f5dfcde03ca471162ba35b614Dan Gohman        // any other maps purge the original user from their records.
29550b6e33584f4e4cf75c7795b1f1a90731861c825Devang Patel        PHINode *NewPN = cast<PHINode>(PN->clone());
296667d787c0a21cf3f5dfcde03ca471162ba35b614Dan Gohman        NewPN->takeName(PN);
297667d787c0a21cf3f5dfcde03ca471162ba35b614Dan Gohman        NewPN->insertBefore(PN);
298667d787c0a21cf3f5dfcde03ca471162ba35b614Dan Gohman        PN->replaceAllUsesWith(NewPN);
299667d787c0a21cf3f5dfcde03ca471162ba35b614Dan Gohman        PN->eraseFromParent();
300667d787c0a21cf3f5dfcde03ca471162ba35b614Dan Gohman      }
301c9838f25d53d3dd7d38949ef6c28f2505a110f45Chris Lattner    }
302c9838f25d53d3dd7d38949ef6c28f2505a110f45Chris Lattner  }
30340bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner}
30415cad759fe2048ac5eb137c6bb0ab7287538677eChris Lattner
30560f8a63e2502d57e879bf52a4a48505b74fa9716Dan Gohmanvoid IndVarSimplify::RewriteNonIntegerIVs(Loop *L) {
3062d1be87ee40a4a0241d94448173879d9df2bc5b3Dan Gohman  // First step.  Check to see if there are any floating-point recurrences.
30740bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner  // If there are, change them into integer recurrences, permitting analysis by
30840bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner  // the SCEV routines.
30915cad759fe2048ac5eb137c6bb0ab7287538677eChris Lattner  //
31040bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner  BasicBlock *Header    = L->getHeader();
311fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman
31281db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman  SmallVector<WeakVH, 8> PHIs;
31381db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman  for (BasicBlock::iterator I = Header->begin();
31481db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman       PHINode *PN = dyn_cast<PHINode>(I); ++I)
31581db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman    PHIs.push_back(PN);
31681db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman
31781db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman  for (unsigned i = 0, e = PHIs.size(); i != e; ++i)
31881db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman    if (PHINode *PN = dyn_cast_or_null<PHINode>(PHIs[i]))
31981db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman      HandleFloatingPointIV(L, PN);
32015cad759fe2048ac5eb137c6bb0ab7287538677eChris Lattner
3212d1be87ee40a4a0241d94448173879d9df2bc5b3Dan Gohman  // If the loop previously had floating-point IV, ScalarEvolution
32260f8a63e2502d57e879bf52a4a48505b74fa9716Dan Gohman  // may not have been able to compute a trip count. Now that we've done some
32360f8a63e2502d57e879bf52a4a48505b74fa9716Dan Gohman  // re-writing, the trip count may be computable.
32460f8a63e2502d57e879bf52a4a48505b74fa9716Dan Gohman  if (Changed)
3254c7279ac726e338400626fca5a09b5533426eb6aDan Gohman    SE->forgetLoop(L);
326c671d892ab1d3d8fed18a61f66f3f3a86e73ebd9Dale Johannesen}
327c671d892ab1d3d8fed18a61f66f3f3a86e73ebd9Dale Johannesen
328c2390b14c91764cba6e4394d05e58e387a7dfb19Dan Gohmanbool IndVarSimplify::runOnLoop(Loop *L, LPPassManager &LPM) {
32981db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman  IU = &getAnalysis<IVUsers>();
3305ee99979065d75605d150d7e567e4351024aae8fDevang Patel  LI = &getAnalysis<LoopInfo>();
3315ee99979065d75605d150d7e567e4351024aae8fDevang Patel  SE = &getAnalysis<ScalarEvolution>();
332de53dc03f5c1549f3176e979bbeeac965dfa5cbcDan Gohman  DT = &getAnalysis<DominatorTree>();
3335ee99979065d75605d150d7e567e4351024aae8fDevang Patel  Changed = false;
33460f8a63e2502d57e879bf52a4a48505b74fa9716Dan Gohman
3352d1be87ee40a4a0241d94448173879d9df2bc5b3Dan Gohman  // If there are any floating-point recurrences, attempt to
33660f8a63e2502d57e879bf52a4a48505b74fa9716Dan Gohman  // transform them to use integer recurrences.
33760f8a63e2502d57e879bf52a4a48505b74fa9716Dan Gohman  RewriteNonIntegerIVs(L);
33860f8a63e2502d57e879bf52a4a48505b74fa9716Dan Gohman
33981db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman  BasicBlock *ExitingBlock = L->getExitingBlock(); // may be null
3400bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman  const SCEV *BackedgeTakenCount = SE->getBackedgeTakenCount(L);
3419caed5440d59dac4b388152fe8b993dc0491e5e9Chris Lattner
342667d787c0a21cf3f5dfcde03ca471162ba35b614Dan Gohman  // Create a rewriter object which we'll use to transform the code with.
343667d787c0a21cf3f5dfcde03ca471162ba35b614Dan Gohman  SCEVExpander Rewriter(*SE);
344667d787c0a21cf3f5dfcde03ca471162ba35b614Dan Gohman
34540bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner  // Check to see if this loop has a computable loop-invariant execution count.
34640bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner  // If so, this means that we can compute the final value of any expressions
34740bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner  // that are recurrent in the loop, and substitute the exit values from the
34840bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner  // loop into any instructions outside of the loop that use the final values of
34940bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner  // the current expressions.
350394437ff7eaccfe1de92fe14d0022ca0addf3e41Chris Lattner  //
35146bdfb0e6bb9de86b19562fc52fddefd7014cf73Dan Gohman  if (!isa<SCEVCouldNotCompute>(BackedgeTakenCount))
352667d787c0a21cf3f5dfcde03ca471162ba35b614Dan Gohman    RewriteLoopExitValues(L, BackedgeTakenCount, Rewriter);
35340bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner
35481db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman  // Compute the type of the largest recurrence expression, and decide whether
35581db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman  // a canonical induction variable should be inserted.
356c2390b14c91764cba6e4394d05e58e387a7dfb19Dan Gohman  const Type *LargestType = 0;
35781db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman  bool NeedCannIV = false;
35846bdfb0e6bb9de86b19562fc52fddefd7014cf73Dan Gohman  if (!isa<SCEVCouldNotCompute>(BackedgeTakenCount)) {
35946bdfb0e6bb9de86b19562fc52fddefd7014cf73Dan Gohman    LargestType = BackedgeTakenCount->getType();
360af79fb5f47b0088c6a8973a7fdbaea96973a429dDan Gohman    LargestType = SE->getEffectiveSCEVType(LargestType);
36181db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman    // If we have a known trip count and a single exit block, we'll be
36281db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman    // rewriting the loop exit test condition below, which requires a
36381db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman    // canonical induction variable.
36481db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman    if (ExitingBlock)
36581db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman      NeedCannIV = true;
366f50af088f19f525f3d1026eb61db77e0037a9f43Chris Lattner  }
36781db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman  for (unsigned i = 0, e = IU->StrideOrder.size(); i != e; ++i) {
3680bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman    const SCEV *Stride = IU->StrideOrder[i];
36981db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman    const Type *Ty = SE->getEffectiveSCEVType(Stride->getType());
370c2390b14c91764cba6e4394d05e58e387a7dfb19Dan Gohman    if (!LargestType ||
37181db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman        SE->getTypeSizeInBits(Ty) >
372af79fb5f47b0088c6a8973a7fdbaea96973a429dDan Gohman          SE->getTypeSizeInBits(LargestType))
37381db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman      LargestType = Ty;
37481db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman
3750bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman    std::map<const SCEV *, IVUsersOfOneStride *>::iterator SI =
37681db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman      IU->IVUsesByStride.find(IU->StrideOrder[i]);
37781db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman    assert(SI != IU->IVUsesByStride.end() && "Stride doesn't exist!");
37881db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman
37981db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman    if (!SI->second->Users.empty())
38081db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman      NeedCannIV = true;
381500597a1c39e91a3020587318ed61e737b6c613aChris Lattner  }
382394437ff7eaccfe1de92fe14d0022ca0addf3e41Chris Lattner
383f451cb870efcf9e0302d25ed05f4cac6bb494e42Dan Gohman  // Now that we know the largest of the induction variable expressions
38481db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman  // in this loop, insert a canonical induction variable of the largest size.
385c2390b14c91764cba6e4394d05e58e387a7dfb19Dan Gohman  Value *IndVar = 0;
38681db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman  if (NeedCannIV) {
3874d8414f42033a5e744e8e60d2ca188b424c76168Dan Gohman    // Check to see if the loop already has a canonical-looking induction
3884d8414f42033a5e744e8e60d2ca188b424c76168Dan Gohman    // variable. If one is present and it's wider than the planned canonical
3894d8414f42033a5e744e8e60d2ca188b424c76168Dan Gohman    // induction variable, temporarily remove it, so that the Rewriter
3904d8414f42033a5e744e8e60d2ca188b424c76168Dan Gohman    // doesn't attempt to reuse it.
3914d8414f42033a5e744e8e60d2ca188b424c76168Dan Gohman    PHINode *OldCannIV = L->getCanonicalInductionVariable();
3924d8414f42033a5e744e8e60d2ca188b424c76168Dan Gohman    if (OldCannIV) {
3934d8414f42033a5e744e8e60d2ca188b424c76168Dan Gohman      if (SE->getTypeSizeInBits(OldCannIV->getType()) >
3944d8414f42033a5e744e8e60d2ca188b424c76168Dan Gohman          SE->getTypeSizeInBits(LargestType))
3954d8414f42033a5e744e8e60d2ca188b424c76168Dan Gohman        OldCannIV->removeFromParent();
3964d8414f42033a5e744e8e60d2ca188b424c76168Dan Gohman      else
3974d8414f42033a5e744e8e60d2ca188b424c76168Dan Gohman        OldCannIV = 0;
3984d8414f42033a5e744e8e60d2ca188b424c76168Dan Gohman    }
3994d8414f42033a5e744e8e60d2ca188b424c76168Dan Gohman
400667d787c0a21cf3f5dfcde03ca471162ba35b614Dan Gohman    IndVar = Rewriter.getOrInsertCanonicalInductionVariable(L, LargestType);
4014d8414f42033a5e744e8e60d2ca188b424c76168Dan Gohman
402c2390b14c91764cba6e4394d05e58e387a7dfb19Dan Gohman    ++NumInserted;
403c2390b14c91764cba6e4394d05e58e387a7dfb19Dan Gohman    Changed = true;
404f67ef318aa29e7cc0c7de76349881959936d9eeeDavid Greene    DEBUG(dbgs() << "INDVARS: New CanIV: " << *IndVar << '\n');
4054d8414f42033a5e744e8e60d2ca188b424c76168Dan Gohman
4064d8414f42033a5e744e8e60d2ca188b424c76168Dan Gohman    // Now that the official induction variable is established, reinsert
4074d8414f42033a5e744e8e60d2ca188b424c76168Dan Gohman    // the old canonical-looking variable after it so that the IR remains
4084d8414f42033a5e744e8e60d2ca188b424c76168Dan Gohman    // consistent. It will be deleted as part of the dead-PHI deletion at
4094d8414f42033a5e744e8e60d2ca188b424c76168Dan Gohman    // the end of the pass.
4104d8414f42033a5e744e8e60d2ca188b424c76168Dan Gohman    if (OldCannIV)
4114d8414f42033a5e744e8e60d2ca188b424c76168Dan Gohman      OldCannIV->insertAfter(cast<Instruction>(IndVar));
412d19534add90a2a894af61523b830887097bb780bDan Gohman  }
41340bf8b48cdb9961898dba1bc67320be1e49e3da1Chris Lattner
414c2390b14c91764cba6e4394d05e58e387a7dfb19Dan Gohman  // If we have a trip count expression, rewrite the loop's exit condition
415c2390b14c91764cba6e4394d05e58e387a7dfb19Dan Gohman  // using it.  We can currently only handle loops with a single exit.
41681db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman  ICmpInst *NewICmp = 0;
41781db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman  if (!isa<SCEVCouldNotCompute>(BackedgeTakenCount) && ExitingBlock) {
41881db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman    assert(NeedCannIV &&
41981db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman           "LinearFunctionTestReplace requires a canonical induction variable");
420c2390b14c91764cba6e4394d05e58e387a7dfb19Dan Gohman    // Can't rewrite non-branch yet.
42181db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman    if (BranchInst *BI = dyn_cast<BranchInst>(ExitingBlock->getTerminator()))
42281db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman      NewICmp = LinearFunctionTestReplace(L, BackedgeTakenCount, IndVar,
42381db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman                                          ExitingBlock, BI, Rewriter);
42481db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman  }
425c2390b14c91764cba6e4394d05e58e387a7dfb19Dan Gohman
4263d431384f05c9defc84f36eafe220415cc12ee93Torok Edwin  // Rewrite IV-derived expressions. Clears the rewriter cache.
427667d787c0a21cf3f5dfcde03ca471162ba35b614Dan Gohman  RewriteIVExpressions(L, LargestType, Rewriter);
428fcb81f5f4cbac61851b7dec403961cf88e614aa1Chris Lattner
429667d787c0a21cf3f5dfcde03ca471162ba35b614Dan Gohman  // The Rewriter may not be used from this point on.
4303d431384f05c9defc84f36eafe220415cc12ee93Torok Edwin
43181db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman  // Loop-invariant instructions in the preheader that aren't used in the
43281db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman  // loop may be sunk below the loop to reduce register pressure.
433667d787c0a21cf3f5dfcde03ca471162ba35b614Dan Gohman  SinkUnusedInvariants(L);
43481db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman
43581db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman  // For completeness, inform IVUsers of the IV use in the newly-created
43681db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman  // loop exit test instruction.
43781db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman  if (NewICmp)
43881db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman    IU->AddUsersIfInteresting(cast<Instruction>(NewICmp->getOperand(0)));
43981db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman
44081db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman  // Clean up dead instructions.
4419fff2187a21f765ed87a25c48552a6942450f3e2Dan Gohman  Changed |= DeleteDeadPHIs(L->getHeader());
44281db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman  // Check a post-condition.
44381db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman  assert(L->isLCSSAForm() && "Indvars did not leave the loop in lcssa form!");
44481db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman  return Changed;
44581db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman}
44681db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman
44781db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohmanvoid IndVarSimplify::RewriteIVExpressions(Loop *L, const Type *LargestType,
448667d787c0a21cf3f5dfcde03ca471162ba35b614Dan Gohman                                          SCEVExpander &Rewriter) {
44981db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman  SmallVector<WeakVH, 16> DeadInsts;
45081db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman
45181db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman  // Rewrite all induction variable expressions in terms of the canonical
45281db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman  // induction variable.
45381db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman  //
45481db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman  // If there were induction variables of other sizes or offsets, manually
45581db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman  // add the offsets to the primary induction variable and cast, avoiding
45681db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman  // the need for the code evaluation methods to insert induction variables
45781db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman  // of different sizes.
45881db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman  for (unsigned i = 0, e = IU->StrideOrder.size(); i != e; ++i) {
4590bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman    const SCEV *Stride = IU->StrideOrder[i];
46081db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman
4610bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman    std::map<const SCEV *, IVUsersOfOneStride *>::iterator SI =
46281db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman      IU->IVUsesByStride.find(IU->StrideOrder[i]);
46381db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman    assert(SI != IU->IVUsesByStride.end() && "Stride doesn't exist!");
46481db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman    ilist<IVStrideUse> &List = SI->second->Users;
46581db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman    for (ilist<IVStrideUse>::iterator UI = List.begin(),
46681db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman         E = List.end(); UI != E; ++UI) {
46781db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman      Value *Op = UI->getOperandValToReplace();
4684d8414f42033a5e744e8e60d2ca188b424c76168Dan Gohman      const Type *UseTy = Op->getType();
46981db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman      Instruction *User = UI->getUser();
47081db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman
47181db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman      // Compute the final addrec to expand into code.
4720bba49cebc50c7bd4662a4807bcb3ee7f42cb470Dan Gohman      const SCEV *AR = IU->getReplacementExpr(*UI);
47381db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman
474a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman      // Evaluate the expression out of the loop, if possible.
475a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman      if (!L->contains(UI->getUser())) {
476a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman        const SCEV *ExitVal = SE->getSCEVAtScope(AR, L->getParentLoop());
477a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman        if (ExitVal->isLoopInvariant(L))
478a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman          AR = ExitVal;
479a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman      }
480a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman
48140a5a1b39ee1cd40ff9d04740386b667fb27b340Dan Gohman      // FIXME: It is an extremely bad idea to indvar substitute anything more
48240a5a1b39ee1cd40ff9d04740386b667fb27b340Dan Gohman      // complex than affine induction variables.  Doing so will put expensive
48340a5a1b39ee1cd40ff9d04740386b667fb27b340Dan Gohman      // polynomial evaluations inside of the loop, and the str reduction pass
48440a5a1b39ee1cd40ff9d04740386b667fb27b340Dan Gohman      // currently can only reduce affine polynomials.  For now just disable
48540a5a1b39ee1cd40ff9d04740386b667fb27b340Dan Gohman      // indvar subst on anything more complex than an affine addrec, unless
48640a5a1b39ee1cd40ff9d04740386b667fb27b340Dan Gohman      // it can be expanded to a trivial value.
48740a5a1b39ee1cd40ff9d04740386b667fb27b340Dan Gohman      if (!AR->isLoopInvariant(L) && !Stride->isLoopInvariant(L))
48840a5a1b39ee1cd40ff9d04740386b667fb27b340Dan Gohman        continue;
48940a5a1b39ee1cd40ff9d04740386b667fb27b340Dan Gohman
490de53dc03f5c1549f3176e979bbeeac965dfa5cbcDan Gohman      // Determine the insertion point for this user. By default, insert
491de53dc03f5c1549f3176e979bbeeac965dfa5cbcDan Gohman      // immediately before the user. The SCEVExpander class will automatically
492de53dc03f5c1549f3176e979bbeeac965dfa5cbcDan Gohman      // hoist loop invariants out of the loop. For PHI nodes, there may be
493de53dc03f5c1549f3176e979bbeeac965dfa5cbcDan Gohman      // multiple uses, so compute the nearest common dominator for the
494de53dc03f5c1549f3176e979bbeeac965dfa5cbcDan Gohman      // incoming blocks.
495667d787c0a21cf3f5dfcde03ca471162ba35b614Dan Gohman      Instruction *InsertPt = User;
496667d787c0a21cf3f5dfcde03ca471162ba35b614Dan Gohman      if (PHINode *PHI = dyn_cast<PHINode>(InsertPt))
497de53dc03f5c1549f3176e979bbeeac965dfa5cbcDan Gohman        for (unsigned i = 0, e = PHI->getNumIncomingValues(); i != e; ++i)
498667d787c0a21cf3f5dfcde03ca471162ba35b614Dan Gohman          if (PHI->getIncomingValue(i) == Op) {
499de53dc03f5c1549f3176e979bbeeac965dfa5cbcDan Gohman            if (InsertPt == User)
500de53dc03f5c1549f3176e979bbeeac965dfa5cbcDan Gohman              InsertPt = PHI->getIncomingBlock(i)->getTerminator();
501de53dc03f5c1549f3176e979bbeeac965dfa5cbcDan Gohman            else
502de53dc03f5c1549f3176e979bbeeac965dfa5cbcDan Gohman              InsertPt =
503de53dc03f5c1549f3176e979bbeeac965dfa5cbcDan Gohman                DT->findNearestCommonDominator(InsertPt->getParent(),
504de53dc03f5c1549f3176e979bbeeac965dfa5cbcDan Gohman                                               PHI->getIncomingBlock(i))
505de53dc03f5c1549f3176e979bbeeac965dfa5cbcDan Gohman                      ->getTerminator();
506667d787c0a21cf3f5dfcde03ca471162ba35b614Dan Gohman          }
507667d787c0a21cf3f5dfcde03ca471162ba35b614Dan Gohman
50840a5a1b39ee1cd40ff9d04740386b667fb27b340Dan Gohman      // Now expand it into actual Instructions and patch it into place.
50940a5a1b39ee1cd40ff9d04740386b667fb27b340Dan Gohman      Value *NewVal = Rewriter.expandCodeFor(AR, UseTy, InsertPt);
510c2390b14c91764cba6e4394d05e58e387a7dfb19Dan Gohman
51181db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman      // Patch the new value into place.
51281db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman      if (Op->hasName())
51381db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman        NewVal->takeName(Op);
51481db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman      User->replaceUsesOfWith(Op, NewVal);
51581db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman      UI->setOperandValToReplace(NewVal);
516f67ef318aa29e7cc0c7de76349881959936d9eeeDavid Greene      DEBUG(dbgs() << "INDVARS: Rewrote IV '" << *AR << "' " << *Op << '\n'
517bdff548e4dd577a72094d57b282de4e765643b96Chris Lattner                   << "   into = " << *NewVal << "\n");
51881db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman      ++NumRemoved;
51981db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman      Changed = true;
52081db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman
52181db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman      // The old value may be dead now.
52281db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman      DeadInsts.push_back(Op);
52381db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman    }
524500597a1c39e91a3020587318ed61e737b6c613aChris Lattner  }
525ba4f3f6a419326df190599421fa149c90235cb72Chris Lattner
5263d431384f05c9defc84f36eafe220415cc12ee93Torok Edwin  // Clear the rewriter cache, because values that are in the rewriter's cache
5273d431384f05c9defc84f36eafe220415cc12ee93Torok Edwin  // can be deleted in the loop below, causing the AssertingVH in the cache to
5283d431384f05c9defc84f36eafe220415cc12ee93Torok Edwin  // trigger.
5293d431384f05c9defc84f36eafe220415cc12ee93Torok Edwin  Rewriter.clear();
53081db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman  // Now that we're done iterating through lists, clean up any instructions
53181db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman  // which are now dead.
532a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman  while (!DeadInsts.empty())
533a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman    if (Instruction *Inst =
534a10756ee657a4d43a48cca5c166919093930ed6bDan Gohman          dyn_cast_or_null<Instruction>(DeadInsts.pop_back_val()))
53581db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman      RecursivelyDeleteTriviallyDeadInstructions(Inst);
53681db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman}
53781db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman
53881db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman/// If there's a single exit block, sink any loop-invariant values that
53981db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman/// were defined in the preheader but not used inside the loop into the
54081db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman/// exit block to reduce register pressure in the loop.
541667d787c0a21cf3f5dfcde03ca471162ba35b614Dan Gohmanvoid IndVarSimplify::SinkUnusedInvariants(Loop *L) {
54281db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman  BasicBlock *ExitBlock = L->getExitBlock();
54381db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman  if (!ExitBlock) return;
54481db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman
54581db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman  BasicBlock *Preheader = L->getLoopPreheader();
54603e896bd6073efc4523d8bcd0239d6ed62126db7Dan Gohman  if (!Preheader) return;
54703e896bd6073efc4523d8bcd0239d6ed62126db7Dan Gohman
54803e896bd6073efc4523d8bcd0239d6ed62126db7Dan Gohman  Instruction *InsertPt = ExitBlock->getFirstNonPHI();
54981db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman  BasicBlock::iterator I = Preheader->getTerminator();
55081db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman  while (I != Preheader->begin()) {
55181db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman    --I;
552667d787c0a21cf3f5dfcde03ca471162ba35b614Dan Gohman    // New instructions were inserted at the end of the preheader.
553667d787c0a21cf3f5dfcde03ca471162ba35b614Dan Gohman    if (isa<PHINode>(I))
55481db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman      break;
5550c77db32dd8ae10b5a26d60718dbe03dc2745888Eli Friedman    // Don't move instructions which might have side effects, since the side
5560c77db32dd8ae10b5a26d60718dbe03dc2745888Eli Friedman    // effects need to complete before instructions inside the loop.  Also
5570c77db32dd8ae10b5a26d60718dbe03dc2745888Eli Friedman    // don't move instructions which might read memory, since the loop may
5580c77db32dd8ae10b5a26d60718dbe03dc2745888Eli Friedman    // modify memory. Note that it's okay if the instruction might have
5590c77db32dd8ae10b5a26d60718dbe03dc2745888Eli Friedman    // undefined behavior: LoopSimplify guarantees that the preheader
5600c77db32dd8ae10b5a26d60718dbe03dc2745888Eli Friedman    // dominates the exit block.
5610c77db32dd8ae10b5a26d60718dbe03dc2745888Eli Friedman    if (I->mayHaveSideEffects() || I->mayReadFromMemory())
562667d787c0a21cf3f5dfcde03ca471162ba35b614Dan Gohman      continue;
56376f497a351c562f366b5e93c27d3f1652fb48ff4Dan Gohman    // Don't sink static AllocaInsts out of the entry block, which would
56476f497a351c562f366b5e93c27d3f1652fb48ff4Dan Gohman    // turn them into dynamic allocas!
56576f497a351c562f366b5e93c27d3f1652fb48ff4Dan Gohman    if (AllocaInst *AI = dyn_cast<AllocaInst>(I))
56676f497a351c562f366b5e93c27d3f1652fb48ff4Dan Gohman      if (AI->isStaticAlloca())
56776f497a351c562f366b5e93c27d3f1652fb48ff4Dan Gohman        continue;
56881db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman    // Determine if there is a use in or before the loop (direct or
56981db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman    // otherwise).
57081db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman    bool UsedInLoop = false;
57181db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman    for (Value::use_iterator UI = I->use_begin(), UE = I->use_end();
57281db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman         UI != UE; ++UI) {
57381db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman      BasicBlock *UseBB = cast<Instruction>(UI)->getParent();
57481db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman      if (PHINode *P = dyn_cast<PHINode>(UI)) {
57581db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman        unsigned i =
57681db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman          PHINode::getIncomingValueNumForOperand(UI.getOperandNo());
57781db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman        UseBB = P->getIncomingBlock(i);
57881db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman      }
57981db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman      if (UseBB == Preheader || L->contains(UseBB)) {
58081db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman        UsedInLoop = true;
58181db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman        break;
58281db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman      }
58381db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman    }
58481db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman    // If there is, the def must remain in the preheader.
58581db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman    if (UsedInLoop)
58681db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman      continue;
58781db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman    // Otherwise, sink it to the exit block.
58881db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman    Instruction *ToMove = I;
58981db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman    bool Done = false;
59081db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman    if (I != Preheader->begin())
59181db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman      --I;
59281db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman    else
59381db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman      Done = true;
594667d787c0a21cf3f5dfcde03ca471162ba35b614Dan Gohman    ToMove->moveBefore(InsertPt);
59581db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman    if (Done)
59681db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman      break;
597667d787c0a21cf3f5dfcde03ca471162ba35b614Dan Gohman    InsertPt = ToMove;
59881db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman  }
59981db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman}
60081db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman
60113877bf6c20621541ff71583c626bda68ef09219Devang Patel/// Return true if it is OK to use SIToFPInst for an inducation variable
60213877bf6c20621541ff71583c626bda68ef09219Devang Patel/// with given inital and exit values.
60313877bf6c20621541ff71583c626bda68ef09219Devang Patelstatic bool useSIToFPInst(ConstantFP &InitV, ConstantFP &ExitV,
60413877bf6c20621541ff71583c626bda68ef09219Devang Patel                          uint64_t intIV, uint64_t intEV) {
60513877bf6c20621541ff71583c626bda68ef09219Devang Patel
606cafb81337bdf2574049533f5b8a6bc1ed92f1f3eDan Gohman  if (InitV.getValueAPF().isNegative() || ExitV.getValueAPF().isNegative())
60713877bf6c20621541ff71583c626bda68ef09219Devang Patel    return true;
60813877bf6c20621541ff71583c626bda68ef09219Devang Patel
60913877bf6c20621541ff71583c626bda68ef09219Devang Patel  // If the iteration range can be handled by SIToFPInst then use it.
61013877bf6c20621541ff71583c626bda68ef09219Devang Patel  APInt Max = APInt::getSignedMaxValue(32);
611bae7d6dbeb842b5ed051c50a87bc282f2dec6e1fDale Johannesen  if (Max.getZExtValue() > static_cast<uint64_t>(abs64(intEV - intIV)))
61213877bf6c20621541ff71583c626bda68ef09219Devang Patel    return true;
613cafb81337bdf2574049533f5b8a6bc1ed92f1f3eDan Gohman
61413877bf6c20621541ff71583c626bda68ef09219Devang Patel  return false;
61513877bf6c20621541ff71583c626bda68ef09219Devang Patel}
61613877bf6c20621541ff71583c626bda68ef09219Devang Patel
61713877bf6c20621541ff71583c626bda68ef09219Devang Patel/// convertToInt - Convert APF to an integer, if possible.
618cd40233429fce385ae4b22301ce705273a6951a1Devang Patelstatic bool convertToInt(const APFloat &APF, uint64_t *intVal) {
619cd40233429fce385ae4b22301ce705273a6951a1Devang Patel
620cd40233429fce385ae4b22301ce705273a6951a1Devang Patel  bool isExact = false;
621794a7dbce030f93315b1305f83a374232f09bba5Evan Cheng  if (&APF.getSemantics() == &APFloat::PPCDoubleDouble)
622794a7dbce030f93315b1305f83a374232f09bba5Evan Cheng    return false;
623cafb81337bdf2574049533f5b8a6bc1ed92f1f3eDan Gohman  if (APF.convertToInteger(intVal, 32, APF.isNegative(),
624cd40233429fce385ae4b22301ce705273a6951a1Devang Patel                           APFloat::rmTowardZero, &isExact)
625cd40233429fce385ae4b22301ce705273a6951a1Devang Patel      != APFloat::opOK)
626cd40233429fce385ae4b22301ce705273a6951a1Devang Patel    return false;
627cafb81337bdf2574049533f5b8a6bc1ed92f1f3eDan Gohman  if (!isExact)
628cd40233429fce385ae4b22301ce705273a6951a1Devang Patel    return false;
629cd40233429fce385ae4b22301ce705273a6951a1Devang Patel  return true;
630cd40233429fce385ae4b22301ce705273a6951a1Devang Patel
631cd40233429fce385ae4b22301ce705273a6951a1Devang Patel}
632cd40233429fce385ae4b22301ce705273a6951a1Devang Patel
63358d43d4a41b21085c063bdd21a2abb68056e2a6fDevang Patel/// HandleFloatingPointIV - If the loop has floating induction variable
63458d43d4a41b21085c063bdd21a2abb68056e2a6fDevang Patel/// then insert corresponding integer induction variable if possible.
63584e3515974407a606289c6e762a0419723b7918fDevang Patel/// For example,
63684e3515974407a606289c6e762a0419723b7918fDevang Patel/// for(double i = 0; i < 10000; ++i)
63784e3515974407a606289c6e762a0419723b7918fDevang Patel///   bar(i)
63884e3515974407a606289c6e762a0419723b7918fDevang Patel/// is converted into
63984e3515974407a606289c6e762a0419723b7918fDevang Patel/// for(int i = 0; i < 10000; ++i)
64084e3515974407a606289c6e762a0419723b7918fDevang Patel///   bar((double)i);
64184e3515974407a606289c6e762a0419723b7918fDevang Patel///
64281db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohmanvoid IndVarSimplify::HandleFloatingPointIV(Loop *L, PHINode *PH) {
64384e3515974407a606289c6e762a0419723b7918fDevang Patel
64484e3515974407a606289c6e762a0419723b7918fDevang Patel  unsigned IncomingEdge = L->contains(PH->getIncomingBlock(0));
64584e3515974407a606289c6e762a0419723b7918fDevang Patel  unsigned BackEdge     = IncomingEdge^1;
646cafb81337bdf2574049533f5b8a6bc1ed92f1f3eDan Gohman
64784e3515974407a606289c6e762a0419723b7918fDevang Patel  // Check incoming value.
648cd40233429fce385ae4b22301ce705273a6951a1Devang Patel  ConstantFP *InitValue = dyn_cast<ConstantFP>(PH->getIncomingValue(IncomingEdge));
649cd40233429fce385ae4b22301ce705273a6951a1Devang Patel  if (!InitValue) return;
6501d0be15f89cb5056e20e2d24faa8d6afb1573bcaOwen Anderson  uint64_t newInitValue =
6511d0be15f89cb5056e20e2d24faa8d6afb1573bcaOwen Anderson              Type::getInt32Ty(PH->getContext())->getPrimitiveSizeInBits();
652cd40233429fce385ae4b22301ce705273a6951a1Devang Patel  if (!convertToInt(InitValue->getValueAPF(), &newInitValue))
653cd40233429fce385ae4b22301ce705273a6951a1Devang Patel    return;
654cd40233429fce385ae4b22301ce705273a6951a1Devang Patel
655cd40233429fce385ae4b22301ce705273a6951a1Devang Patel  // Check IV increment. Reject this PH if increement operation is not
656cd40233429fce385ae4b22301ce705273a6951a1Devang Patel  // an add or increment value can not be represented by an integer.
657cafb81337bdf2574049533f5b8a6bc1ed92f1f3eDan Gohman  BinaryOperator *Incr =
65884e3515974407a606289c6e762a0419723b7918fDevang Patel    dyn_cast<BinaryOperator>(PH->getIncomingValue(BackEdge));
65984e3515974407a606289c6e762a0419723b7918fDevang Patel  if (!Incr) return;
660ae3a0be92e33bc716722aa600983fc1535acb122Dan Gohman  if (Incr->getOpcode() != Instruction::FAdd) return;
66184e3515974407a606289c6e762a0419723b7918fDevang Patel  ConstantFP *IncrValue = NULL;
66284e3515974407a606289c6e762a0419723b7918fDevang Patel  unsigned IncrVIndex = 1;
66384e3515974407a606289c6e762a0419723b7918fDevang Patel  if (Incr->getOperand(1) == PH)
66484e3515974407a606289c6e762a0419723b7918fDevang Patel    IncrVIndex = 0;
66584e3515974407a606289c6e762a0419723b7918fDevang Patel  IncrValue = dyn_cast<ConstantFP>(Incr->getOperand(IncrVIndex));
66684e3515974407a606289c6e762a0419723b7918fDevang Patel  if (!IncrValue) return;
6671d0be15f89cb5056e20e2d24faa8d6afb1573bcaOwen Anderson  uint64_t newIncrValue =
6681d0be15f89cb5056e20e2d24faa8d6afb1573bcaOwen Anderson              Type::getInt32Ty(PH->getContext())->getPrimitiveSizeInBits();
669cd40233429fce385ae4b22301ce705273a6951a1Devang Patel  if (!convertToInt(IncrValue->getValueAPF(), &newIncrValue))
670cd40233429fce385ae4b22301ce705273a6951a1Devang Patel    return;
671cafb81337bdf2574049533f5b8a6bc1ed92f1f3eDan Gohman
672cd40233429fce385ae4b22301ce705273a6951a1Devang Patel  // Check Incr uses. One user is PH and the other users is exit condition used
673cd40233429fce385ae4b22301ce705273a6951a1Devang Patel  // by the conditional terminator.
67484e3515974407a606289c6e762a0419723b7918fDevang Patel  Value::use_iterator IncrUse = Incr->use_begin();
67584e3515974407a606289c6e762a0419723b7918fDevang Patel  Instruction *U1 = cast<Instruction>(IncrUse++);
67684e3515974407a606289c6e762a0419723b7918fDevang Patel  if (IncrUse == Incr->use_end()) return;
67784e3515974407a606289c6e762a0419723b7918fDevang Patel  Instruction *U2 = cast<Instruction>(IncrUse++);
67884e3515974407a606289c6e762a0419723b7918fDevang Patel  if (IncrUse != Incr->use_end()) return;
679cafb81337bdf2574049533f5b8a6bc1ed92f1f3eDan Gohman
68084e3515974407a606289c6e762a0419723b7918fDevang Patel  // Find exit condition.
68184e3515974407a606289c6e762a0419723b7918fDevang Patel  FCmpInst *EC = dyn_cast<FCmpInst>(U1);
68284e3515974407a606289c6e762a0419723b7918fDevang Patel  if (!EC)
68384e3515974407a606289c6e762a0419723b7918fDevang Patel    EC = dyn_cast<FCmpInst>(U2);
68484e3515974407a606289c6e762a0419723b7918fDevang Patel  if (!EC) return;
68584e3515974407a606289c6e762a0419723b7918fDevang Patel
68684e3515974407a606289c6e762a0419723b7918fDevang Patel  if (BranchInst *BI = dyn_cast<BranchInst>(EC->getParent()->getTerminator())) {
68784e3515974407a606289c6e762a0419723b7918fDevang Patel    if (!BI->isConditional()) return;
68884e3515974407a606289c6e762a0419723b7918fDevang Patel    if (BI->getCondition() != EC) return;
68958d43d4a41b21085c063bdd21a2abb68056e2a6fDevang Patel  }
69084e3515974407a606289c6e762a0419723b7918fDevang Patel
691cd40233429fce385ae4b22301ce705273a6951a1Devang Patel  // Find exit value. If exit value can not be represented as an interger then
692cd40233429fce385ae4b22301ce705273a6951a1Devang Patel  // do not handle this floating point PH.
69384e3515974407a606289c6e762a0419723b7918fDevang Patel  ConstantFP *EV = NULL;
69484e3515974407a606289c6e762a0419723b7918fDevang Patel  unsigned EVIndex = 1;
69584e3515974407a606289c6e762a0419723b7918fDevang Patel  if (EC->getOperand(1) == Incr)
69684e3515974407a606289c6e762a0419723b7918fDevang Patel    EVIndex = 0;
69784e3515974407a606289c6e762a0419723b7918fDevang Patel  EV = dyn_cast<ConstantFP>(EC->getOperand(EVIndex));
69884e3515974407a606289c6e762a0419723b7918fDevang Patel  if (!EV) return;
6991d0be15f89cb5056e20e2d24faa8d6afb1573bcaOwen Anderson  uint64_t intEV = Type::getInt32Ty(PH->getContext())->getPrimitiveSizeInBits();
700cd40233429fce385ae4b22301ce705273a6951a1Devang Patel  if (!convertToInt(EV->getValueAPF(), &intEV))
70184e3515974407a606289c6e762a0419723b7918fDevang Patel    return;
702cafb81337bdf2574049533f5b8a6bc1ed92f1f3eDan Gohman
70384e3515974407a606289c6e762a0419723b7918fDevang Patel  // Find new predicate for integer comparison.
70484e3515974407a606289c6e762a0419723b7918fDevang Patel  CmpInst::Predicate NewPred = CmpInst::BAD_ICMP_PREDICATE;
70584e3515974407a606289c6e762a0419723b7918fDevang Patel  switch (EC->getPredicate()) {
70684e3515974407a606289c6e762a0419723b7918fDevang Patel  case CmpInst::FCMP_OEQ:
70784e3515974407a606289c6e762a0419723b7918fDevang Patel  case CmpInst::FCMP_UEQ:
70884e3515974407a606289c6e762a0419723b7918fDevang Patel    NewPred = CmpInst::ICMP_EQ;
70984e3515974407a606289c6e762a0419723b7918fDevang Patel    break;
71084e3515974407a606289c6e762a0419723b7918fDevang Patel  case CmpInst::FCMP_OGT:
71184e3515974407a606289c6e762a0419723b7918fDevang Patel  case CmpInst::FCMP_UGT:
71284e3515974407a606289c6e762a0419723b7918fDevang Patel    NewPred = CmpInst::ICMP_UGT;
71384e3515974407a606289c6e762a0419723b7918fDevang Patel    break;
71484e3515974407a606289c6e762a0419723b7918fDevang Patel  case CmpInst::FCMP_OGE:
71584e3515974407a606289c6e762a0419723b7918fDevang Patel  case CmpInst::FCMP_UGE:
71684e3515974407a606289c6e762a0419723b7918fDevang Patel    NewPred = CmpInst::ICMP_UGE;
71784e3515974407a606289c6e762a0419723b7918fDevang Patel    break;
71884e3515974407a606289c6e762a0419723b7918fDevang Patel  case CmpInst::FCMP_OLT:
71984e3515974407a606289c6e762a0419723b7918fDevang Patel  case CmpInst::FCMP_ULT:
72084e3515974407a606289c6e762a0419723b7918fDevang Patel    NewPred = CmpInst::ICMP_ULT;
72184e3515974407a606289c6e762a0419723b7918fDevang Patel    break;
72284e3515974407a606289c6e762a0419723b7918fDevang Patel  case CmpInst::FCMP_OLE:
72384e3515974407a606289c6e762a0419723b7918fDevang Patel  case CmpInst::FCMP_ULE:
72484e3515974407a606289c6e762a0419723b7918fDevang Patel    NewPred = CmpInst::ICMP_ULE;
72584e3515974407a606289c6e762a0419723b7918fDevang Patel    break;
72684e3515974407a606289c6e762a0419723b7918fDevang Patel  default:
72784e3515974407a606289c6e762a0419723b7918fDevang Patel    break;
72858d43d4a41b21085c063bdd21a2abb68056e2a6fDevang Patel  }
72984e3515974407a606289c6e762a0419723b7918fDevang Patel  if (NewPred == CmpInst::BAD_ICMP_PREDICATE) return;
730cafb81337bdf2574049533f5b8a6bc1ed92f1f3eDan Gohman
73184e3515974407a606289c6e762a0419723b7918fDevang Patel  // Insert new integer induction variable.
7321d0be15f89cb5056e20e2d24faa8d6afb1573bcaOwen Anderson  PHINode *NewPHI = PHINode::Create(Type::getInt32Ty(PH->getContext()),
73384e3515974407a606289c6e762a0419723b7918fDevang Patel                                    PH->getName()+".int", PH);
7341d0be15f89cb5056e20e2d24faa8d6afb1573bcaOwen Anderson  NewPHI->addIncoming(ConstantInt::get(Type::getInt32Ty(PH->getContext()),
7351d0be15f89cb5056e20e2d24faa8d6afb1573bcaOwen Anderson                                       newInitValue),
73684e3515974407a606289c6e762a0419723b7918fDevang Patel                      PH->getIncomingBlock(IncomingEdge));
73784e3515974407a606289c6e762a0419723b7918fDevang Patel
738cafb81337bdf2574049533f5b8a6bc1ed92f1f3eDan Gohman  Value *NewAdd = BinaryOperator::CreateAdd(NewPHI,
7391d0be15f89cb5056e20e2d24faa8d6afb1573bcaOwen Anderson                           ConstantInt::get(Type::getInt32Ty(PH->getContext()),
740cd40233429fce385ae4b22301ce705273a6951a1Devang Patel                                                             newIncrValue),
74184e3515974407a606289c6e762a0419723b7918fDevang Patel                                            Incr->getName()+".int", Incr);
74284e3515974407a606289c6e762a0419723b7918fDevang Patel  NewPHI->addIncoming(NewAdd, PH->getIncomingBlock(BackEdge));
74384e3515974407a606289c6e762a0419723b7918fDevang Patel
744617d108a639b29015da2dbf0e54f61bd39c3c33cDale Johannesen  // The back edge is edge 1 of newPHI, whatever it may have been in the
745617d108a639b29015da2dbf0e54f61bd39c3c33cDale Johannesen  // original PHI.
7461d0be15f89cb5056e20e2d24faa8d6afb1573bcaOwen Anderson  ConstantInt *NewEV = ConstantInt::get(Type::getInt32Ty(PH->getContext()),
7471d0be15f89cb5056e20e2d24faa8d6afb1573bcaOwen Anderson                                        intEV);
748617d108a639b29015da2dbf0e54f61bd39c3c33cDale Johannesen  Value *LHS = (EVIndex == 1 ? NewPHI->getIncomingValue(1) : NewEV);
749617d108a639b29015da2dbf0e54f61bd39c3c33cDale Johannesen  Value *RHS = (EVIndex == 1 ? NewEV : NewPHI->getIncomingValue(1));
750333c40096561218bc3597cf153c0a3895274414cOwen Anderson  ICmpInst *NewEC = new ICmpInst(EC->getParent()->getTerminator(),
751460f656475738d1a95a6be95346908ce1597df25Daniel Dunbar                                 NewPred, LHS, RHS, EC->getName());
752cafb81337bdf2574049533f5b8a6bc1ed92f1f3eDan Gohman
75381db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman  // In the following deltions, PH may become dead and may be deleted.
75481db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman  // Use a WeakVH to observe whether this happens.
75581db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman  WeakVH WeakPH = PH;
75681db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman
75784e3515974407a606289c6e762a0419723b7918fDevang Patel  // Delete old, floating point, exit comparision instruction.
75814fba294e1f2d619fe50b72d5fd88da2b17461ddDan Gohman  NewEC->takeName(EC);
75984e3515974407a606289c6e762a0419723b7918fDevang Patel  EC->replaceAllUsesWith(NewEC);
76081db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman  RecursivelyDeleteTriviallyDeadInstructions(EC);
761cafb81337bdf2574049533f5b8a6bc1ed92f1f3eDan Gohman
76284e3515974407a606289c6e762a0419723b7918fDevang Patel  // Delete old, floating point, increment instruction.
7639e9a0d5fc26878e51a58a8b57900fcbf952c2691Owen Anderson  Incr->replaceAllUsesWith(UndefValue::get(Incr->getType()));
76481db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman  RecursivelyDeleteTriviallyDeadInstructions(Incr);
76581db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman
76681db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman  // Replace floating induction variable, if it isn't already deleted.
76781db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman  // Give SIToFPInst preference over UIToFPInst because it is faster on
76881db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman  // platforms that are widely used.
76981db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman  if (WeakPH && !PH->use_empty()) {
77081db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman    if (useSIToFPInst(*InitValue, *EV, newInitValue, intEV)) {
77181db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman      SIToFPInst *Conv = new SIToFPInst(NewPHI, PH->getType(), "indvar.conv",
77281db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman                                        PH->getParent()->getFirstNonPHI());
77381db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman      PH->replaceAllUsesWith(Conv);
77481db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman    } else {
77581db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman      UIToFPInst *Conv = new UIToFPInst(NewPHI, PH->getType(), "indvar.conv",
77681db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman                                        PH->getParent()->getFirstNonPHI());
77781db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman      PH->replaceAllUsesWith(Conv);
77881db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman    }
77981db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman    RecursivelyDeleteTriviallyDeadInstructions(PH);
780cd40233429fce385ae4b22301ce705273a6951a1Devang Patel  }
78158d43d4a41b21085c063bdd21a2abb68056e2a6fDevang Patel
78281db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman  // Add a new IVUsers entry for the newly-created integer PHI.
78381db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman  IU->AddUsersIfInteresting(NewPHI);
78481db61a2e6d3c95a2738c3559a108e05e9d7a05aDan Gohman}
785