blob: 6e903498f57ee5a7a528d9eb578cd24a573aa17b [file] [log] [blame]
Chris Lattner1bffea02001-10-15 17:31:51 +00001//===- 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
Ruchira Sasankafca59d72001-11-03 17:09:59 +00009
10
11
Chris Lattner1bffea02001-10-15 17:31:51 +000012#include "llvm/Transforms/HoistPHIConstants.h"
13#include "llvm/iOther.h"
14#include "llvm/BasicBlock.h"
15#include "llvm/Method.h"
16#include <map>
Vikram S. Adveddf5ff62001-11-08 05:25:33 +000017#include <vector>
Chris Lattner1bffea02001-10-15 17:31:51 +000018
19typedef pair<BasicBlock *, Value*> BBConstTy;
20typedef map<BBConstTy, CastInst *> CachedCopyMap;
21
22static Value *NormalizePhiOperand(PHINode *PN, Value *CPV,
23 BasicBlock *Pred, CachedCopyMap &CopyCache) {
24
Ruchira Sasankafca59d72001-11-03 17:09:59 +000025 /* NOTE: CahedCopyMap was disabled to insert phi elimination code
26 for all phi args -- Ruchira
27 */
28
29
Chris Lattner1bffea02001-10-15 17:31:51 +000030 // 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 //
Ruchira Sasankafca59d72001-11-03 17:09:59 +000033 //CachedCopyMap::iterator CCI = CopyCache.find(BBConstTy(Pred, CPV));
34 //if (CCI != CopyCache.end()) return CCI->second;
Chris Lattner1bffea02001-10-15 17:31:51 +000035
36 // Create a copy instruction and add it to the cache...
37 CastInst *Inst = new CastInst(CPV, CPV->getType());
Ruchira Sasankafca59d72001-11-03 17:09:59 +000038 //CopyCache.insert(make_pair(BBConstTy(Pred, CPV), Inst));
Chris Lattner1bffea02001-10-15 17:31:51 +000039
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
Chris Lattner42c9c2c2001-10-18 05:27:33 +000052bool HoistPHIConstants::doHoistPHIConstants(Method *M) {
Chris Lattner1bffea02001-10-15 17:31:51 +000053 CachedCopyMap Cache;
Chris Lattner42c9c2c2001-10-18 05:27:33 +000054 bool Changed = false;
Chris Lattner1bffea02001-10-15 17:31:51 +000055
56 for (Method::iterator BI = M->begin(), BE = M->end(); BI != BE; ++BI)
Vikram S. Adveddf5ff62001-11-08 05:25:33 +000057 {
58 vector<PHINode*> phis; // normalizing invalidates BB iterator
Chris Lattner1bffea02001-10-15 17:31:51 +000059
Vikram S. Adveddf5ff62001-11-08 05:25:33 +000060 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 }
Chris Lattner1bffea02001-10-15 17:31:51 +000079 }
Vikram S. Adveddf5ff62001-11-08 05:25:33 +000080
Chris Lattner42c9c2c2001-10-18 05:27:33 +000081 return Changed;
Chris Lattner1bffea02001-10-15 17:31:51 +000082}