blob: a9e4c5b5cb0fbef445d683b0b3a4e2310e01bde3 [file] [log] [blame]
Chris Lattnerd213f0f2001-06-20 19:27:11 +00001//===- 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 Lattnerda956802001-06-21 05:27:22 +000017#include "llvm/Tools/STLExtras.h"
Chris Lattnerd213f0f2001-06-20 19:27:11 +000018
Chris Lattnerda956802001-06-21 05:27:22 +000019static bool ProcessInterval(cfg::Interval *Int) {
20 if (!Int->isLoop()) return false; // Not a loop? Ignore it!
Chris Lattnerd213f0f2001-06-20 19:27:11 +000021
Chris Lattnerda956802001-06-21 05:27:22 +000022 cerr << "Found Looping Interval: " << Int; //->HeaderNode;
23 return false;
24}
25
26static 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 Lattnerd213f0f2001-06-20 19:27:11 +000040}
41
42// DoInductionVariableCannonicalize - Simplify induction variables in loops
43//
44bool DoInductionVariableCannonicalize(Method *M) {
Chris Lattnerda956802001-06-21 05:27:22 +000045 cfg::IntervalPartition *IP = new cfg::IntervalPartition(M);
46 bool Changed = false;
Chris Lattnerd213f0f2001-06-20 19:27:11 +000047
Chris Lattnerda956802001-06-21 05:27:22 +000048 while (!IP->isDegeneratePartition()) {
49 Changed |= ProcessIntervalPartition(*IP);
Chris Lattner56832052001-06-20 22:44:38 +000050
Chris Lattnerda956802001-06-21 05:27:22 +000051 // 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 Lattner56832052001-06-20 22:44:38 +000062
Chris Lattnerda956802001-06-21 05:27:22 +000063 delete IP;
64 return Changed;
Chris Lattnerd213f0f2001-06-20 19:27:11 +000065}