diff --git a/llvm/lib/Analysis/InstructionSimplify.cpp b/llvm/lib/Analysis/InstructionSimplify.cpp index c7e1724ac9bc..981fb976c02a 100644 --- a/llvm/lib/Analysis/InstructionSimplify.cpp +++ b/llvm/lib/Analysis/InstructionSimplify.cpp @@ -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(Op)) + if (auto *C = dyn_cast(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(Op)) + if (isa(CI) || isa(CI)) + if (CI->getOperand(0)->getType() == Ty) + return CI->getOperand(0); + return nullptr; }