blob: fe7cabf1614a42ba83a179b34829f207f41fa1b2 [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
9#include "llvm/Transforms/HoistPHIConstants.h"
Chris Lattner7061dc52001-12-03 18:02:31 +000010#include "llvm/iPHINode.h"
Chris Lattner1bffea02001-10-15 17:31:51 +000011#include "llvm/iOther.h"
12#include "llvm/BasicBlock.h"
13#include "llvm/Method.h"
14#include <map>
Vikram S. Adveddf5ff62001-11-08 05:25:33 +000015#include <vector>
Chris Lattner1bffea02001-10-15 17:31:51 +000016
17typedef pair<BasicBlock *, Value*> BBConstTy;
18typedef map<BBConstTy, CastInst *> CachedCopyMap;
19
20static Value *NormalizePhiOperand(PHINode *PN, Value *CPV,
21 BasicBlock *Pred, CachedCopyMap &CopyCache) {
22
Ruchira Sasankafca59d72001-11-03 17:09:59 +000023 /* NOTE: CahedCopyMap was disabled to insert phi elimination code
24 for all phi args -- Ruchira
25 */
26
27
Chris Lattner1bffea02001-10-15 17:31:51 +000028 // Check if we've already inserted a copy for this constant in Pred
29 // Note that `copyCache[Pred]' will create an empty vector the first time
30 //
Ruchira Sasanka061b8ed2001-11-12 19:32:04 +000031 CachedCopyMap::iterator CCI = CopyCache.find(BBConstTy(Pred, CPV));
32 if (CCI != CopyCache.end()) return CCI->second;
Chris Lattner1bffea02001-10-15 17:31:51 +000033
34 // Create a copy instruction and add it to the cache...
35 CastInst *Inst = new CastInst(CPV, CPV->getType());
Ruchira Sasanka061b8ed2001-11-12 19:32:04 +000036 CopyCache.insert(make_pair(BBConstTy(Pred, CPV), Inst));
Chris Lattner1bffea02001-10-15 17:31:51 +000037
38 // Insert the copy just before the terminator inst of the predecessor BB
39 assert(Pred->getTerminator() && "Degenerate BB encountered!");
40 Pred->getInstList().insert(Pred->getInstList().end()-1, Inst);
41
42 return Inst;
43}
44
45
46//---------------------------------------------------------------------------
47// Entry point for normalizing constant args in PHIs
48//---------------------------------------------------------------------------
49
Chris Lattner42c9c2c2001-10-18 05:27:33 +000050bool HoistPHIConstants::doHoistPHIConstants(Method *M) {
Chris Lattner1bffea02001-10-15 17:31:51 +000051 CachedCopyMap Cache;
Chris Lattner42c9c2c2001-10-18 05:27:33 +000052 bool Changed = false;
Chris Lattner1bffea02001-10-15 17:31:51 +000053
54 for (Method::iterator BI = M->begin(), BE = M->end(); BI != BE; ++BI)
Vikram S. Adveddf5ff62001-11-08 05:25:33 +000055 {
56 vector<PHINode*> phis; // normalizing invalidates BB iterator
Chris Lattner1bffea02001-10-15 17:31:51 +000057
Vikram S. Adveddf5ff62001-11-08 05:25:33 +000058 for (BasicBlock::iterator II = (*BI)->begin(); II != (*BI)->end(); ++II)
59 {
60 if (PHINode *PN = dyn_cast<PHINode>(*II))
61 phis.push_back(PN);
62 else
63 break; // All PHIs occur at top of BB!
64 }
65
66 for (vector<PHINode*>::iterator PI=phis.begin(); PI != phis.end(); ++PI)
67 for (unsigned i = 0; i < (*PI)->getNumIncomingValues(); ++i)
68 {
Ruchira Sasanka061b8ed2001-11-12 19:32:04 +000069 Value *Op = (*PI)->getIncomingValue(i);
70
71 if (isa<ConstPoolVal>(Op)) {
Vikram S. Adveddf5ff62001-11-08 05:25:33 +000072 (*PI)->setIncomingValue(i,
73 NormalizePhiOperand((*PI),
74 (*PI)->getIncomingValue(i),
75 (*PI)->getIncomingBlock(i), Cache));
76 Changed = true;
Ruchira Sasanka061b8ed2001-11-12 19:32:04 +000077 }
Vikram S. Adveddf5ff62001-11-08 05:25:33 +000078 }
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}