Chris Lattner | d213f0f | 2001-06-20 19:27:11 +0000 | [diff] [blame] | 1 | //===- InductionVars.cpp - Induction Variable Cannonicalization code --------=// |
| 2 | // |
| 3 | // This file implements induction variable cannonicalization of loops. |
| 4 | // |
| 5 | // Specifically, after this executes, the following is true: |
| 6 | // - There is a single induction variable for each loop (that used to contain |
| 7 | // at least one induction variable) |
| 8 | // - This induction variable starts at 0 and steps by 1 per iteration |
| 9 | // - All other preexisting induction variables are adjusted to operate in |
| 10 | // terms of this primary induction variable |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "llvm/Analysis/Intervals.h" |
| 15 | #include "llvm/Opt/AllOpts.h" |
| 16 | #include "llvm/Assembly/Writer.h" |
Chris Lattner | da95680 | 2001-06-21 05:27:22 +0000 | [diff] [blame] | 17 | #include "llvm/Tools/STLExtras.h" |
Chris Lattner | d213f0f | 2001-06-20 19:27:11 +0000 | [diff] [blame] | 18 | |
Chris Lattner | da95680 | 2001-06-21 05:27:22 +0000 | [diff] [blame] | 19 | static bool ProcessInterval(cfg::Interval *Int) { |
| 20 | if (!Int->isLoop()) return false; // Not a loop? Ignore it! |
Chris Lattner | d213f0f | 2001-06-20 19:27:11 +0000 | [diff] [blame] | 21 | |
Chris Lattner | da95680 | 2001-06-21 05:27:22 +0000 | [diff] [blame] | 22 | cerr << "Found Looping Interval: " << Int; //->HeaderNode; |
| 23 | return false; |
| 24 | } |
| 25 | |
| 26 | static bool ProcessIntervalPartition(cfg::IntervalPartition &IP) { |
| 27 | // This currently just prints out information about the interval structure |
| 28 | // of the method... |
| 29 | static unsigned N = 0; |
| 30 | cerr << "\n***********Interval Partition #" << (++N) << "************\n\n"; |
| 31 | copy(IP.begin(), IP.end(), ostream_iterator<cfg::Interval*>(cerr, "\n")); |
| 32 | |
| 33 | cerr << "\n*********** PERFORMING WORK ************\n\n"; |
| 34 | |
| 35 | // Loop over all of the intervals in the partition and look for induction |
| 36 | // variables in intervals that represent loops. |
| 37 | // |
| 38 | return reduce_apply(IP.begin(), IP.end(), bitwise_or<bool>(), false, |
| 39 | ptr_fun(ProcessInterval)); |
Chris Lattner | d213f0f | 2001-06-20 19:27:11 +0000 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | // DoInductionVariableCannonicalize - Simplify induction variables in loops |
| 43 | // |
| 44 | bool DoInductionVariableCannonicalize(Method *M) { |
Chris Lattner | da95680 | 2001-06-21 05:27:22 +0000 | [diff] [blame] | 45 | cfg::IntervalPartition *IP = new cfg::IntervalPartition(M); |
| 46 | bool Changed = false; |
Chris Lattner | d213f0f | 2001-06-20 19:27:11 +0000 | [diff] [blame] | 47 | |
Chris Lattner | da95680 | 2001-06-21 05:27:22 +0000 | [diff] [blame] | 48 | while (!IP->isDegeneratePartition()) { |
| 49 | Changed |= ProcessIntervalPartition(*IP); |
Chris Lattner | 5683205 | 2001-06-20 22:44:38 +0000 | [diff] [blame] | 50 | |
Chris Lattner | da95680 | 2001-06-21 05:27:22 +0000 | [diff] [blame] | 51 | // Calculate the reduced version of this graph until we get to an |
| 52 | // irreducible graph or a degenerate graph... |
| 53 | // |
| 54 | cfg::IntervalPartition *NewIP = new cfg::IntervalPartition(*IP, false); |
| 55 | if (NewIP->size() == IP->size()) { |
| 56 | cerr << "IRREDUCIBLE GRAPH FOUND!!!\n"; |
| 57 | return Changed; |
| 58 | } |
| 59 | delete IP; |
| 60 | IP = NewIP; |
| 61 | } |
Chris Lattner | 5683205 | 2001-06-20 22:44:38 +0000 | [diff] [blame] | 62 | |
Chris Lattner | da95680 | 2001-06-21 05:27:22 +0000 | [diff] [blame] | 63 | delete IP; |
| 64 | return Changed; |
Chris Lattner | d213f0f | 2001-06-20 19:27:11 +0000 | [diff] [blame] | 65 | } |