InstCombine internally caches constant operands that it folds. ConstantFolding also internally caches folded operands, but does so only on the "second level": The direct operands of an instruction/constant are not cached, but the operands of the operands are. Presumably that's an oversight...
This patch allows passing in the folded operand cache as an extra parameter, and changes the ConstantFoldConstant API to also look up from it / write to it by itself. That way all users handle caching correctly, and the cache can be shared across calls (done only in InstCombine for now).
In particular this will also avoid duplicate constant folding between ConstantFoldInstruction and operand constant folding in InstCombine. I haven't run into this as a perf problem myself, but recently ran across D18155, which did identify this as a performance problem for some types of code.
Do you have any idea why we're checking for ConstantVector specifically, instead of ConstantAggregate?