Is CPU allowed to perform a speculative execution on nullptr branch conditions?

6 days ago 20
ARTICLE AD BOX

CPU often performs speculative execution on code branches like

if ( a > b)

And discards the result in case of misprediction.

However let's consider the following:

int* ptr = nullptr; //do some work if (ptr != nullptr) { std::cout << *ptr << std::endl; } else { // do something else }

Can CPU speculate on such condition? How does it happen? In this case misprediction would result in dereferencing nullptr and UB. Logically that would exclude possibility of discarding wrong result since the program has crashed.

If speculative execution of such branch is allowed, is it always assumes that there is nullptr (to avoid UB) and jumps to else branch? Or it can speculate for both conditions (== nullptr and != nullptr) depending on euristics? If it can speculate on dereferencing nullptr, what are the mechanics allowing CPU to avoid UB?

Read Entire Article