Chris Lattner | 1bffea0 | 2001-10-15 17:31:51 +0000 | [diff] [blame] | 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 | |
Ruchira Sasanka | fca59d7 | 2001-11-03 17:09:59 +0000 | [diff] [blame] | 9 | |
| 10 | |
| 11 | |
Chris Lattner | 1bffea0 | 2001-10-15 17:31:51 +0000 | [diff] [blame] | 12 | #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. Adve | ddf5ff6 | 2001-11-08 05:25:33 +0000 | [diff] [blame^] | 17 | #include <vector> |
Chris Lattner | 1bffea0 | 2001-10-15 17:31:51 +0000 | [diff] [blame] | 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 | |
Ruchira Sasanka | fca59d7 | 2001-11-03 17:09:59 +0000 | [diff] [blame] | 25 | /* NOTE: CahedCopyMap was disabled to insert phi elimination code |
| 26 | for all phi args -- Ruchira |
| 27 | */ |
| 28 | |
| 29 | |
Chris Lattner | 1bffea0 | 2001-10-15 17:31:51 +0000 | [diff] [blame] | 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 | // |
Ruchira Sasanka | fca59d7 | 2001-11-03 17:09:59 +0000 | [diff] [blame] | 33 | //CachedCopyMap::iterator CCI = CopyCache.find(BBConstTy(Pred, CPV)); |
| 34 | //if (CCI != CopyCache.end()) return CCI->second; |
Chris Lattner | 1bffea0 | 2001-10-15 17:31:51 +0000 | [diff] [blame] | 35 | |
| 36 | // Create a copy instruction and add it to the cache... |
| 37 | CastInst *Inst = new CastInst(CPV, CPV->getType()); |
Ruchira Sasanka | fca59d7 | 2001-11-03 17:09:59 +0000 | [diff] [blame] | 38 | //CopyCache.insert(make_pair(BBConstTy(Pred, CPV), Inst)); |
Chris Lattner | 1bffea0 | 2001-10-15 17:31:51 +0000 | [diff] [blame] | 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 | |
Chris Lattner | 42c9c2c | 2001-10-18 05:27:33 +0000 | [diff] [blame] | 52 | bool HoistPHIConstants::doHoistPHIConstants(Method *M) { |
Chris Lattner | 1bffea0 | 2001-10-15 17:31:51 +0000 | [diff] [blame] | 53 | CachedCopyMap Cache; |
Chris Lattner | 42c9c2c | 2001-10-18 05:27:33 +0000 | [diff] [blame] | 54 | bool Changed = false; |
Chris Lattner | 1bffea0 | 2001-10-15 17:31:51 +0000 | [diff] [blame] | 55 | |
| 56 | for (Method::iterator BI = M->begin(), BE = M->end(); BI != BE; ++BI) |
Vikram S. Adve | ddf5ff6 | 2001-11-08 05:25:33 +0000 | [diff] [blame^] | 57 | { |
| 58 | vector<PHINode*> phis; // normalizing invalidates BB iterator |
Chris Lattner | 1bffea0 | 2001-10-15 17:31:51 +0000 | [diff] [blame] | 59 | |
Vikram S. Adve | ddf5ff6 | 2001-11-08 05:25:33 +0000 | [diff] [blame^] | 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 | } |
Chris Lattner | 1bffea0 | 2001-10-15 17:31:51 +0000 | [diff] [blame] | 79 | } |
Vikram S. Adve | ddf5ff6 | 2001-11-08 05:25:33 +0000 | [diff] [blame^] | 80 | |
Chris Lattner | 42c9c2c | 2001-10-18 05:27:33 +0000 | [diff] [blame] | 81 | return Changed; |
Chris Lattner | 1bffea0 | 2001-10-15 17:31:51 +0000 | [diff] [blame] | 82 | } |