[InstSimplify] Fold trunc([zs]ext(%V)) -> %V

Truncates can completely cancel out a zext or sext instruction.

llvm-svn: 276604
This commit is contained in:
David Majnemer
2016-07-25 03:39:21 +00:00
parent aedcbf898b
commit 126de5d4b4

View File

@@ -3790,9 +3790,15 @@ static Value *SimplifyPHINode(PHINode *PN, const Query &Q) {
}
static Value *SimplifyTruncInst(Value *Op, Type *Ty, const Query &Q, unsigned) {
if (Constant *C = dyn_cast<Constant>(Op))
if (auto *C = dyn_cast<Constant>(Op))
return ConstantFoldCastOperand(Instruction::Trunc, C, Ty, Q.DL);
// trunc([zs]ext(x)) -> x if the trunc undoes the work of the [zs]ext.
if (auto *CI = dyn_cast<CastInst>(Op))
if (isa<ZExtInst>(CI) || isa<SExtInst>(CI))
if (CI->getOperand(0)->getType() == Ty)
return CI->getOperand(0);
return nullptr;
}