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