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> |
| 17 | |
| 18 | typedef pair<BasicBlock *, Value*> BBConstTy; |
| 19 | typedef map<BBConstTy, CastInst *> CachedCopyMap; |
| 20 | |
| 21 | static Value *NormalizePhiOperand(PHINode *PN, Value *CPV, |
| 22 | BasicBlock *Pred, CachedCopyMap &CopyCache) { |
| 23 | |
Ruchira Sasanka | fca59d7 | 2001-11-03 17:09:59 +0000 | [diff] [blame] | 24 | /* NOTE: CahedCopyMap was disabled to insert phi elimination code |
| 25 | for all phi args -- Ruchira |
| 26 | */ |
| 27 | |
| 28 | |
Chris Lattner | 1bffea0 | 2001-10-15 17:31:51 +0000 | [diff] [blame] | 29 | // Check if we've already inserted a copy for this constant in Pred |
| 30 | // Note that `copyCache[Pred]' will create an empty vector the first time |
| 31 | // |
Ruchira Sasanka | fca59d7 | 2001-11-03 17:09:59 +0000 | [diff] [blame] | 32 | //CachedCopyMap::iterator CCI = CopyCache.find(BBConstTy(Pred, CPV)); |
| 33 | //if (CCI != CopyCache.end()) return CCI->second; |
Chris Lattner | 1bffea0 | 2001-10-15 17:31:51 +0000 | [diff] [blame] | 34 | |
| 35 | // Create a copy instruction and add it to the cache... |
| 36 | CastInst *Inst = new CastInst(CPV, CPV->getType()); |
Ruchira Sasanka | fca59d7 | 2001-11-03 17:09:59 +0000 | [diff] [blame] | 37 | //CopyCache.insert(make_pair(BBConstTy(Pred, CPV), Inst)); |
Chris Lattner | 1bffea0 | 2001-10-15 17:31:51 +0000 | [diff] [blame] | 38 | |
| 39 | // Insert the copy just before the terminator inst of the predecessor BB |
| 40 | assert(Pred->getTerminator() && "Degenerate BB encountered!"); |
| 41 | Pred->getInstList().insert(Pred->getInstList().end()-1, Inst); |
| 42 | |
| 43 | return Inst; |
| 44 | } |
| 45 | |
| 46 | |
| 47 | //--------------------------------------------------------------------------- |
| 48 | // Entry point for normalizing constant args in PHIs |
| 49 | //--------------------------------------------------------------------------- |
| 50 | |
Chris Lattner | 42c9c2c | 2001-10-18 05:27:33 +0000 | [diff] [blame] | 51 | bool HoistPHIConstants::doHoistPHIConstants(Method *M) { |
Chris Lattner | 1bffea0 | 2001-10-15 17:31:51 +0000 | [diff] [blame] | 52 | CachedCopyMap Cache; |
Chris Lattner | 42c9c2c | 2001-10-18 05:27:33 +0000 | [diff] [blame] | 53 | bool Changed = false; |
Chris Lattner | 1bffea0 | 2001-10-15 17:31:51 +0000 | [diff] [blame] | 54 | |
| 55 | for (Method::iterator BI = M->begin(), BE = M->end(); BI != BE; ++BI) |
| 56 | for (BasicBlock::iterator II = (*BI)->begin(); II != (*BI)->end(); ++II) { |
| 57 | Instruction *Inst = *II; |
| 58 | if (!isa<PHINode>(Inst)) break; // All PHIs occur at top of BB! |
| 59 | |
| 60 | PHINode *PN = cast<PHINode>(Inst); |
| 61 | for (unsigned i = 0; i < PN->getNumIncomingValues(); ++i) { |
| 62 | Value *Op = PN->getIncomingValue(i); |
Ruchira Sasanka | fca59d7 | 2001-11-03 17:09:59 +0000 | [diff] [blame] | 63 | |
| 64 | //if (isa<ConstPoolVal>(Op)) { --- Do for all phi args -- Ruchira |
| 65 | |
| 66 | PN->setIncomingValue(i, |
| 67 | NormalizePhiOperand(PN, Op, PN->getIncomingBlock(i), Cache)); |
| 68 | Changed = true; |
| 69 | |
| 70 | //} |
Chris Lattner | 1bffea0 | 2001-10-15 17:31:51 +0000 | [diff] [blame] | 71 | } |
| 72 | } |
| 73 | |
Chris Lattner | 42c9c2c | 2001-10-18 05:27:33 +0000 | [diff] [blame] | 74 | return Changed; |
Chris Lattner | 1bffea0 | 2001-10-15 17:31:51 +0000 | [diff] [blame] | 75 | } |