21 for (Function::iterator b = f.begin(), be = f.end(); b != be; ++b) {
22 BasicBlock::iterator it = b->begin();
24 if (it->getOpcode() == Instruction::PHI) {
25 PHINode *reference = cast<PHINode>(it);
27 std::set<Value*> phis;
28 phis.insert(reference);
30 unsigned numBlocks = reference->getNumIncomingValues();
31 for (++it; isa<PHINode>(*it); ++it) {
32 PHINode *pi = cast<PHINode>(it);
34 assert(numBlocks == pi->getNumIncomingValues());
38 for (i=0; i<numBlocks; i++)
39 if (pi->getIncomingBlock(i) != reference->getIncomingBlock(i))
43 std::vector<Value*> values;
44 values.reserve(numBlocks);
45 for (
unsigned i = 0; i<numBlocks; i++)
46 values.push_back(pi->getIncomingValueForBlock(reference->getIncomingBlock(i)));
47 for (
unsigned i = 0; i<numBlocks; i++) {
48 pi->setIncomingBlock(i, reference->getIncomingBlock(i));
49 pi->setIncomingValue(i, values[i]);
55 for (i=0; i<numBlocks; i++) {
56 Value *value = pi->getIncomingValue(i);
58 if (phis.find(value) != phis.end()) {
67 new BitCastInst(value,
69 value->getName() +
".phiclean",
70 pi->getIncomingBlock(i)->getTerminator());
71 pi->setIncomingValue(i, tmp);
bool runOnFunction(llvm::Function &f) override