For some reason LLVM doesn't provide this. I have written a few versions of this in the Swift codebase. Makes sense to factor it out before I make another one.