}
}
-
return Changed;
}
}
-// CheckIncomingValueFor - Make sure that the specified PHI node has an entry
-// for the provided basic block. If it doesn't, add one and return true.
-//
-static inline void CheckIncomingValueFor(PHINode *PN, BasicBlock *BB) {
- if (PN->getBasicBlockIndex(BB) != -1) return; // Already has value
-
- Value *NewVal = 0;
- const Type *Ty = PN->getType();
-
- if (const PointerType *PT = dyn_cast<PointerType>(Ty))
- NewVal = ConstantPointerNull::get(PT);
- else if (Ty == Type::BoolTy)
- NewVal = ConstantBool::True;
- else if (Ty == Type::FloatTy || Ty == Type::DoubleTy)
- NewVal = ConstantFP::get(Ty, 42);
- else if (Ty->isIntegral())
- NewVal = ConstantInt::get(Ty, 42);
-
- assert(NewVal && "Unknown PHI node type!");
- PN->addIncoming(NewVal, BB);
-}
-
// fixLocalProblems - Loop through the method and fix problems with the PHI
-// nodes in the current method. The two problems that are handled are:
-//
-// 1. PHI nodes with multiple entries for the same predecessor. GCC sometimes
-// generates code that looks like this:
+// nodes in the current method. The problem is that PHI nodes might exist with
+// multiple entries for the same predecessor. GCC sometimes generates code
+// that looks like this:
//
// bb7: br bool %cond1004, label %bb8, label %bb8
// bb8: %reg119 = phi uint [ 0, %bb7 ], [ 1, %bb7 ]
// bb8: %reg119 = phi uint [ 0, %bbX ], [ 1, %bb7 ]
//
//
-// 2. PHI nodes with fewer arguments than predecessors.
-// These can be generated by GCC if a variable is uninitalized over a path
-// in the CFG. We fix this by adding an entry for the missing predecessors
-// that is initialized to either 42 for a numeric/FP value, or null if it's
-// a pointer value. This problem can be generated by code that looks like
-// this:
-// int foo(int y) {
-// int X;
-// if (y) X = 1;
-// return X;
-// }
-//
static bool fixLocalProblems(Method *M) {
bool Changed = false;
// Don't use iterators because invalidation gets messy...
if (isa<PHINode>(BB->front())) {
const vector<BasicBlock*> Preds(pred_begin(BB), pred_end(BB));
- // Handle Problem #1. Sort the list of predecessors so that it is easy to
- // decide whether or not duplicate predecessors exist.
+ // Handle the problem. Sort the list of predecessors so that it is easy
+ // to decide whether or not duplicate predecessors exist.
vector<BasicBlock*> SortedPreds(Preds);
sort(SortedPreds.begin(), SortedPreds.end());
}
LastOne = SortedPreds[i];
}
-
- // Loop over all of the PHI nodes in the current BB. These PHI nodes are
- // guaranteed to be at the beginning of the basic block.
- //
- for (BasicBlock::iterator I = BB->begin();
- PHINode *PN = dyn_cast<PHINode>(*I); ++I) {
-
- // Handle problem #2.
- if (PN->getNumIncomingValues() != Preds.size()) {
- assert(PN->getNumIncomingValues() <= Preds.size() &&
- "Can't handle extra arguments to PHI nodes!");
- for (unsigned i = 0; i < Preds.size(); ++i)
- CheckIncomingValueFor(PN, Preds[i]);
- Changed = true;
- }
- }
}
}
return Changed;