/// This is all of the cached information about this value.
ValueCacheEntryTy &Cache;
- /// NewBlocks - This is a mpping of the new BasicBlocks which have been
+ /// NewBlocks - This is a mapping of the new BasicBlocks which have been
/// added to cache but that are not in sorted order.
DenseMap<BasicBlock*, LVILatticeVal> NewBlockInfo;
public:
// If we've already computed this block's value, return it.
if (!BBLV.isUndefined()) {
- DEBUG(errs() << " reuse BB '" << BB->getName() << "' val=" << BBLV <<'\n');
+ DEBUG(dbgs() << " reuse BB '" << BB->getName() << "' val=" << BBLV <<'\n');
return BBLV;
}
// If we hit overdefined, exit early. The BlockVals entry is already set
// to overdefined.
if (Result.isOverdefined()) {
- DEBUG(errs() << " compute BB '" << BB->getName()
+ DEBUG(dbgs() << " compute BB '" << BB->getName()
<< "' - overdefined because of pred.\n");
return Result;
}
}
- DEBUG(errs() << " compute BB '" << BB->getName()
+ DEBUG(dbgs() << " compute BB '" << BB->getName()
<< "' - overdefined because inst def found.\n");
LVILatticeVal Result;
}
-/// getEdgeValue - This method
+/// getEdgeValue - This method attempts to infer more complex
LVILatticeVal LVIQuery::getEdgeValue(BasicBlock *BBFrom, BasicBlock *BBTo) {
+ // TODO: Handle more complex conditionals. If (v == 0 || v2 < 1) is false, we
+ // know that v != 0.
if (BranchInst *BI = dyn_cast<BranchInst>(BBFrom->getTerminator())) {
// If this is a conditional branch and only one successor goes to BBTo, then
// we maybe able to infer something from the condition.
}
}
}
-
- // TODO: Info from switch.
-
- // TODO: Handle more complex conditionals. If (v == 0 || v2 < 1) is false, we
- // know that v != 0.
+
+ // If the edge was formed by a switch on the value, then we may know exactly
+ // what it is.
+ if (SwitchInst *SI = dyn_cast<SwitchInst>(BBFrom->getTerminator())) {
+ // If BBTo is the default destination of the switch, we don't know anything.
+ // Given a more powerful range analysis we could know stuff.
+ if (SI->getCondition() == Val && SI->getDefaultDest() != BBTo) {
+ // We only know something if there is exactly one value that goes from
+ // BBFrom to BBTo.
+ unsigned NumEdges = 0;
+ ConstantInt *EdgeVal = 0;
+ for (unsigned i = 1, e = SI->getNumSuccessors(); i != e; ++i) {
+ if (SI->getSuccessor(i) != BBTo) continue;
+ if (NumEdges++) break;
+ EdgeVal = SI->getCaseValue(i);
+ }
+ assert(EdgeVal && "Missing successor?");
+ if (NumEdges == 1)
+ return LVILatticeVal::get(EdgeVal);
+ }
+ }
// Otherwise see if the value is known in the block.
return getBlockValue(BBFrom);
if (Constant *VC = dyn_cast<Constant>(V))
return LVILatticeVal::get(VC);
- DEBUG(errs() << "LVI Getting block end value " << *V << " at '"
+ DEBUG(dbgs() << "LVI Getting block end value " << *V << " at '"
<< BB->getName() << "'\n");
LVILatticeVal Result = LVIQuery(V, ValueCache[V]).getBlockValue(BB);
- DEBUG(errs() << " Result = " << Result << "\n");
+ DEBUG(dbgs() << " Result = " << Result << "\n");
return Result;
}
if (Constant *VC = dyn_cast<Constant>(V))
return LVILatticeVal::get(VC);
- DEBUG(errs() << "LVI Getting edge value " << *V << " from '"
+ DEBUG(dbgs() << "LVI Getting edge value " << *V << " from '"
<< FromBB->getName() << "' to '" << ToBB->getName() << "'\n");
LVILatticeVal Result =
LVIQuery(V, ValueCache[V]).getEdgeValue(FromBB, ToBB);
- DEBUG(errs() << " Result = " << Result << "\n");
+ DEBUG(dbgs() << " Result = " << Result << "\n");
return Result;
}
return False;
} else if (Pred == ICmpInst::ICMP_NE) {
// !C1 != C -> true iff C1 == C.
- Res = ConstantFoldCompareInstOperands(ICmpInst::ICMP_EQ,
+ Res = ConstantFoldCompareInstOperands(ICmpInst::ICMP_NE,
Result.getNotConstant(), C, TD);
if (Res->isNullValue())
return True;