@Paul_Lamb I’m glad to know it can learn these patterns, and even more so if they can be learned as quickly as you stated on your first post. Thank you for the explanation and demonstration!
@Jose_Cueto Right, this is an important question. I didn’t do a proper research to back what I’m about to answer, but I’ll give some examples to illustrate my point.
There are a lot of ways code can be regular and show a defined behavior for a period of time. In some cases, a pattern is present on the problem itself, but in others, the way the solution is coded lead to suboptimal instructions that follow a pattern, specially when performance is not the focus, which holds true for most of the code produced today.
Example 1: a problem that carries a pattern.
This problem involves painting a table of days with colors that help to identify lines and weekends. See the image below.
This is the C code I’d write to solve it:
Note how the if statements can be easily predicted after some time. If this were repeated for some more months rather than just January (the entire year or more), learning the patterns would become more useful.
Also, note that these patterns can be learned looking only for the history of branches separately, i.e. a local analysis. Of course, this is not always the case.
Example 2: a problem without patterns.
This problem is about counting the number of prime numbers below a certain value. Read the code below. It’s not the most optimized code nor the most readable.
Even though prime numbers are random, there is a pattern in the if statement on line 9. This if statement will evaluate to true only if the break statement on line 7 is not reached.
This time, a global history of instructions is necessary to spot the pattern.
I’ve explored the machine code to show it. See the image below. Only jump instructions and instructions right after jump instructions are shown.
* -> non-jump instruction
@ -> jump instruction without condition
# -> jump instruction with condition (a branch)
Note that the branch instruction highlighted in blue can use previous instructions to predict it’s behaviour after number 4.
If this code kept going to values greater than 19, more and more instructions would separate the patterns pointed in the image, but it would still be there.
What are your thoughts on this analysis? Does anyone see any fault on my reasoning? Next step is testing it.