[Scalar Evolution] Builder Insertion Point is PHI at the top of the loop resulting in an assert.

Hi All,

I’ve been looking at this bug:
https://bugs.llvm.org/show_bug.cgi?id=37004

In this function:
SCEVExpander::ReuseOrCreateCast
we use the builder insertion point (BIP in the code) to check dominance in an assert. Namely, the inserted cast must dominate the BIP.
However, I’ve come across a situation where the BIP is actually the PHI at the top of the loop basic block. Since it’s a PHI inside a loop nothing inside that loop will dominate it and the assert will fail.

When tracking down this bug I am assuming that the BIP being a PHI is not correct and that SCEVExpander::ReuseOrCreateCast should not be called with a PHI as the insertion point.
To those who understand the SCEV Expander does that assumption make sense? Or am I not understanding how this works?

Thank you,
Stefan