Currently InstCombiner can eliminate truncate when entire expression tree can be evaluated in a narrower type without adding additional instructions.
However there are some cases when it is profitable to evaluate expression in a narrower type, even if it will require adding additional instructions. (See test case in the patch)
In this patch I split "CanEvaluateTruncated" function into two: "EstimateCostForTruncatedEvaluation" and "IsProfitableToEvaluateTruncated". First one is basically an old "CanEvaluateTruncated", but it also calculates cost for truncating expression tree. Second function basically just calls "EstimateCostForTruncatedEvaluation" and compares cost of the truncation with some threshold.
Currently cost model is very simple - we want to truncate expression tree only if we will remove more instructions than we will add. Probably it can be somehow adjusted in the future.
It isn't clear what 'truncating expression type' means here. Can you turn this into an assert?