Predication, Speculation, and Modern CPUs

by Andrew Chasin





Listing One

if (x > 5) then

    printf("x > 5\n");

else

    printf ("x <= 5\n");

cmp x, 5       // compare x with 5

ble l1:        // branch if x <= 5

call printf  "x > 5\n"

b l2:          // unconditionally branch out of if-then-else

l1:

call printf  "x <= 5\n"

l2:





Listing Two

b1, b2 = cmp.gt x, 5   // compute booleans b1 and b2 based

                       //              on comparison of x with 5

(b1) call printf  "x > 5\n"   // only executed if b1 is true

(b2) call printf  "x <= 5\n"  // only executed if b2 is true





Listing Three

(a)

r16 = call rand()

add r16 = r16, 2

mul r16 = r16, 4

st8 [r16]=2

ld8 r17 = addr("x")    // get address of "x"

ld8 r18 = [r17]        // load value of variable x from memory

add r18 = r18, 1       // add 1 to "x"

st8 [r17] = r18        // store "x" to memory



(b)

r16 = call rand()

ld8 r17 = addr("x")   // get address of "x"

ld8 r18 = [r17]       // load value of "x" from memory

add r16 = r16, 2

mul r16=r16, 4

st8 [r16], 2

add r18 = r18, 1     // add 1 to "x"

st8 [r17] = r18      // store "x" to memory





Listing Four

(p15) br.cond  l1   // guard execution of load instruction

ld8 r17 = [r15]     // load r17 from memory pointed to by r15

add r17 = r17, 2    // use the value loaded in r17

l1:





Listing Five

ld8.s r17 = [r15]     // speculatively load r17 from memory pointed to

by r15

(p15) br.cond  l1

chk.s r17, recovery_code        // check NaT bit in r17 and recover

add r17 = r17, 2                // use the value loaded in r17

l1:





Listing Six

(a)

st8 [r15] = r14   // store through pointer r15

ld8 r14 = [r16]   // load through pointer r16

                  // r15 and r16 do not contain the same address



(b)

ld8 r14 = [r16]   // load and store can be

st8 [r15] = r14   // reordered since store

                          // through r15 does not affect the value loaded

                          // through r16



Listing Seven

ld8.a r14 = [r16]   // load moved above store and

st8 [r15] = r14     //        turned into advanced load instruction

ld8.c r14 = [r16]   // non-speculative load replaced

                    //        with check load instruction



Listing Eight

ld8.a r14 = [r16]   // load moved above store and turned into

                    //                     advanced load instruction

add r17 = r14, r12  // the use of r14 is speculated along with the store



st8 [r15] = r14

chk.a r14, recovery_code  // non-speculative load replaced

                          //                with check load instruction







2



