This should do it:
void SelectionDAGBuilder::visitTrunc(const User &I) {
// TruncInst cannot be a no-op cast because sizeof(src) > sizeof(dest).
SDValue N = getValue(I.getOperand(0));
EVT DestVT = DAG.getTargetLoweringInfo().getValueType(DAG.getDataLayout(),
I.getType());
SDNodeFlags Flags;
if (auto *TI = dyn_cast<TruncInst>(&I)) {
Flags.setNoSignedWrap(TI->hasNoSignedWrap());
Flags.setNoUnsignedWrap(TI->hasNoUnsignedWrap());
}
setValue(&I, DAG.getNode(ISD::TRUNCATE, getCurSDLoc(), DestVT, N, Flags));
}
But then we need to add a lot of general test coverage: https://alive2.llvm.org/ce/z/-wXZPE (a couple of trivial examples)
CC @elhewaty @nikic @goldsteinn