Remove false optimization that basically broke everything
[oota-llvm.git] / lib / Transforms / HoistPHIConstants.cpp
1 //===- llvm/Transforms/HoistPHIConstants.h - Normalize PHI nodes ------------=//
2 //
3 // HoistPHIConstants - Remove literal constants that are arguments of PHI nodes
4 // by inserting cast instructions in the preceeding basic blocks, and changing
5 // constant references into references of the casted value.
6 //
7 //===----------------------------------------------------------------------===//
8
9
10
11
12 #include "llvm/Transforms/HoistPHIConstants.h"
13 #include "llvm/iOther.h"
14 #include "llvm/BasicBlock.h"
15 #include "llvm/Method.h"
16 #include <map>
17 #include <vector>
18
19 typedef pair<BasicBlock *, Value*> BBConstTy;
20 typedef map<BBConstTy, CastInst *> CachedCopyMap;
21
22 static Value *NormalizePhiOperand(PHINode *PN, Value *CPV,
23                                   BasicBlock *Pred, CachedCopyMap &CopyCache) {
24   
25   /* NOTE: CahedCopyMap was disabled to insert phi elimination code
26            for all phi args -- Ruchira
27   */
28
29
30   // Check if we've already inserted a copy for this constant in Pred
31   // Note that `copyCache[Pred]' will create an empty vector the first time
32   //
33   //CachedCopyMap::iterator CCI = CopyCache.find(BBConstTy(Pred, CPV));
34   //if (CCI != CopyCache.end()) return CCI->second;
35   
36   // Create a copy instruction and add it to the cache...
37   CastInst *Inst = new CastInst(CPV, CPV->getType());
38   //CopyCache.insert(make_pair(BBConstTy(Pred, CPV), Inst));
39     
40   // Insert the copy just before the terminator inst of the predecessor BB
41   assert(Pred->getTerminator() && "Degenerate BB encountered!");
42   Pred->getInstList().insert(Pred->getInstList().end()-1, Inst);
43   
44   return Inst;
45 }
46
47
48 //---------------------------------------------------------------------------
49 // Entry point for normalizing constant args in PHIs
50 //---------------------------------------------------------------------------
51
52 bool HoistPHIConstants::doHoistPHIConstants(Method *M) {
53   CachedCopyMap Cache;
54   bool Changed = false;
55   
56   for (Method::iterator BI = M->begin(), BE = M->end(); BI != BE; ++BI)
57     {
58       vector<PHINode*> phis;            // normalizing invalidates BB iterator
59       
60       for (BasicBlock::iterator II = (*BI)->begin(); II != (*BI)->end(); ++II)
61         {
62           if (PHINode *PN = dyn_cast<PHINode>(*II))
63             phis.push_back(PN);
64           else
65             break;                      // All PHIs occur at top of BB!
66         }
67       
68       for (vector<PHINode*>::iterator PI=phis.begin(); PI != phis.end(); ++PI)
69         for (unsigned i = 0; i < (*PI)->getNumIncomingValues(); ++i)
70           {
71             //if (isa<ConstPoolVal>(Op)) {--- Do for all phi args -- Ruchira
72             (*PI)->setIncomingValue(i,
73                     NormalizePhiOperand((*PI),
74                                         (*PI)->getIncomingValue(i),
75                                         (*PI)->getIncomingBlock(i), Cache));
76             Changed = true;
77             //}
78           }
79     }
80   
81   return Changed;
82 }