2001-10-15 17:31:51 +00:00
|
|
|
//===- llvm/Transforms/HoistPHIConstants.h - Normalize PHI nodes ------------=//
|
|
|
|
//
|
|
|
|
// HoistPHIConstants - Remove literal constants that are arguments of PHI nodes
|
|
|
|
// by inserting cast instructions in the preceeding basic blocks, and changing
|
|
|
|
// constant references into references of the casted value.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Transforms/HoistPHIConstants.h"
|
2001-12-03 18:02:31 +00:00
|
|
|
#include "llvm/iPHINode.h"
|
2001-10-15 17:31:51 +00:00
|
|
|
#include "llvm/iOther.h"
|
|
|
|
#include "llvm/BasicBlock.h"
|
|
|
|
#include "llvm/Method.h"
|
|
|
|
#include <map>
|
2001-11-08 05:25:33 +00:00
|
|
|
#include <vector>
|
2001-10-15 17:31:51 +00:00
|
|
|
|
2002-01-20 22:54:45 +00:00
|
|
|
typedef std::pair<BasicBlock *, Value*> BBConstTy;
|
|
|
|
typedef std::map<BBConstTy, CastInst *> CachedCopyMap;
|
2001-10-15 17:31:51 +00:00
|
|
|
|
|
|
|
static Value *NormalizePhiOperand(PHINode *PN, Value *CPV,
|
|
|
|
BasicBlock *Pred, CachedCopyMap &CopyCache) {
|
|
|
|
|
2001-11-03 17:09:59 +00:00
|
|
|
/* NOTE: CahedCopyMap was disabled to insert phi elimination code
|
|
|
|
for all phi args -- Ruchira
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2001-10-15 17:31:51 +00:00
|
|
|
// Check if we've already inserted a copy for this constant in Pred
|
|
|
|
// Note that `copyCache[Pred]' will create an empty vector the first time
|
|
|
|
//
|
2001-11-12 19:32:04 +00:00
|
|
|
CachedCopyMap::iterator CCI = CopyCache.find(BBConstTy(Pred, CPV));
|
|
|
|
if (CCI != CopyCache.end()) return CCI->second;
|
2001-10-15 17:31:51 +00:00
|
|
|
|
|
|
|
// Create a copy instruction and add it to the cache...
|
|
|
|
CastInst *Inst = new CastInst(CPV, CPV->getType());
|
2002-01-20 22:54:45 +00:00
|
|
|
CopyCache.insert(std::make_pair(BBConstTy(Pred, CPV), Inst));
|
2001-10-15 17:31:51 +00:00
|
|
|
|
|
|
|
// Insert the copy just before the terminator inst of the predecessor BB
|
|
|
|
assert(Pred->getTerminator() && "Degenerate BB encountered!");
|
|
|
|
Pred->getInstList().insert(Pred->getInstList().end()-1, Inst);
|
|
|
|
|
|
|
|
return Inst;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
// Entry point for normalizing constant args in PHIs
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
2001-10-18 05:27:33 +00:00
|
|
|
bool HoistPHIConstants::doHoistPHIConstants(Method *M) {
|
2001-10-15 17:31:51 +00:00
|
|
|
CachedCopyMap Cache;
|
2001-10-18 05:27:33 +00:00
|
|
|
bool Changed = false;
|
2001-10-15 17:31:51 +00:00
|
|
|
|
2001-12-03 22:26:30 +00:00
|
|
|
for (Method::iterator BI = M->begin(), BE = M->end(); BI != BE; ++BI) {
|
2002-01-20 22:54:45 +00:00
|
|
|
std::vector<PHINode*> phis; // normalizing invalidates BB iterator
|
2001-10-15 17:31:51 +00:00
|
|
|
|
2001-12-03 22:26:30 +00:00
|
|
|
for (BasicBlock::iterator II = (*BI)->begin(); II != (*BI)->end(); ++II) {
|
|
|
|
if (PHINode *PN = dyn_cast<PHINode>(*II))
|
|
|
|
phis.push_back(PN);
|
|
|
|
else
|
|
|
|
break; // All PHIs occur at top of BB!
|
|
|
|
}
|
2001-11-08 05:25:33 +00:00
|
|
|
|
2002-01-20 22:54:45 +00:00
|
|
|
for (std::vector<PHINode*>::iterator PI=phis.begin(); PI != phis.end();++PI)
|
2001-12-03 22:26:30 +00:00
|
|
|
for (unsigned i = 0; i < (*PI)->getNumIncomingValues(); ++i) {
|
|
|
|
Value *Op = (*PI)->getIncomingValue(i);
|
|
|
|
|
|
|
|
if (isa<Constant>(Op)) {
|
|
|
|
(*PI)->setIncomingValue(i,
|
2001-11-08 05:25:33 +00:00
|
|
|
NormalizePhiOperand((*PI),
|
|
|
|
(*PI)->getIncomingValue(i),
|
|
|
|
(*PI)->getIncomingBlock(i), Cache));
|
2001-12-03 22:26:30 +00:00
|
|
|
Changed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2001-11-08 05:25:33 +00:00
|
|
|
|
2001-10-18 05:27:33 +00:00
|
|
|
return Changed;
|
2001-10-15 17:31:51 +00:00
|
|
|
}
|