Full name: Conditional execution and the If-Then (IT) block of the Arm Thumb instruction set.
Definition
Conditional execution means that an instruction takes effect only if a condition on the processor’s condition flags §4.42 holds, and otherwise does nothing; the flags are status bits that earlier instructions set to describe their results. In the Arm Thumb instruction set §4.40, the IT (If-Then) instruction makes up to four following instructions conditional, and those instructions form its IT block. Each instruction in the block receives either the IT instruction’s condition (then) or the opposite condition (else). The IT instruction itself changes no flags.
The idea
Programs often make small two-way choices: set a variable to 7 if a value is zero and to 9 otherwise. With branches, the processor compares, jumps over one of two moves, and jumps again at the end. Every taken branch makes a pipelined processor discard instructions it has already fetched, so on a small core the jumps can cost more cycles than the moves. Conditional execution removes the jumps: the processor runs straight through both moves, and the condition decides which of them takes effect.
The original 32-bit Arm instruction set gives almost every instruction a 4-bit condition field. The 16-bit Thumb encodings have no room for one. The IT instruction supplies the conditions instead: one 16-bit instruction states the conditions for up to four following instructions, and the processor keeps them in an internal field while it steps through the block. One can picture the IT instruction as stamping each of the next few instructions with either the condition or its opposite; an instruction whose stamp the current flags do not satisfy passes through without effect.
How it works
The condition flags are four bits of the Application Program Status Register (APSR): N (negative, the top bit of the last flag-setting result), Z (zero, that result was zero), C (carry; after a subtraction, 1 means no borrow occurred) and V (overflow, the result did not fit as a signed number). The compare instruction CMP computes a subtraction and keeps only the flags. The Condition flags (APSR) entry treats them fully.
Each condition has a 4-bit code and a two-letter suffix, and is tested against the flags at the moment the instruction runs:
EQ(equal): Z = 1;NE(not equal): Z = 0.HS(unsigned higher or same, also writtenCS, carry set): C = 1;LO(unsigned lower, also writtenCC, carry clear): C = 0.MI(minus, negative): N = 1;PL(plus, positive or zero): N = 0.VS(overflow set): V = 1;VC(overflow clear): V = 0.HI(unsigned higher): C = 1 and Z = 0;LS(unsigned lower or same): C = 0 or Z = 1.GE(signed greater than or equal): N = V;LT(signed less than): N and V differ.GT(signed greater than): Z = 0 and N = V;LE(signed less than or equal): Z = 1, or N and V differ.AL(always): no test.
The two conditions of each pair differ only in the lowest bit of their 4-bit code, so flipping that bit turns a condition into its opposite.
In assembly, IT is followed by up to three letters, each T (then) or E (else), and a condition. The first instruction of the block always gets the condition itself, and each letter gives the condition or its opposite to one further instruction: ITTE EQ makes three instructions conditional on EQ, EQ and NE. The instructions inside are written with matching suffixes, such as MOVEQ. In memory, IT is one 16-bit halfword: 0xBF in bits 15 to 8, the first condition in bits 7 to 4, and in bits 3 to 0 a mask whose lowest 1 bit marks the block length; ITE EQ is 0xBF0C.
Execution proceeds as follows.
The IT instruction always executes. It writes the conditions into the IT state, an 8-bit field of the EPSR (execution program status register), and changes no flags and no general-purpose register.
For each instruction of the block in turn, the processor takes that instruction’s condition from the IT state and tests it against the flags as they are at that moment, including any change an earlier instruction of the block has made.
If the condition holds, the instruction executes normally: it writes its results, accesses memory, and sets flags if its encoding sets flags.
If the condition fails, the instruction behaves as a NOP (no operation): no register, memory location or flag changes, no memory access happens, and no exception is raised, not even for an encoding that would otherwise be undefined.
Either way, execution moves past the instruction by its length, 2 or 4 bytes, and the IT state moves on to the next instruction’s condition. After the last instruction of the block the IT state is cleared, and execution is unconditional again.
If an interrupt §6.11 or fault arrives in the middle of a block, the processor saves the IT state with the program status register and restores it on return, so the block continues where it stopped.
Inside an IT block, an executed instruction sets the flags in these cases only.
CMP(compare),CMN(compare negative, which adds instead of subtracting),TST(test, a bitwise AND) and the 32-bitTEQ(test equivalence, a bitwise exclusive or) always set flags; that is their only effect.A 32-bit data-processing instruction sets flags exactly when its S bit, the bit that requests a flag update, is 1, which assembly shows as an S suffix, as in
ADDS.W. Being inside a block makes no difference to it.No other 16-bit instruction sets flags inside a block, although many 16-bit encodings set them outside one. The halfword
0x2107ismovs r1, #7outside a block (set r1 to 7, and set N and Z) and a conditionalmov r1, #7inside one (set r1 only).MSR(move to special register) can write the flags directly.On processors with the floating-point extension,
VMRSwith the destinationAPSR_nzcvcopies the flags of the last floating-point comparison into N, Z, C and V.
The architecture restricts what a block may contain.
At most four instructions follow one IT instruction.
An IT instruction may not appear inside a block, and neither may
CBZandCBNZ(compare and branch on zero or nonzero) orCPSIEandCPSID(enable or disable interrupts).An instruction that can write the program counter §2.9, such as a branch or a return, is permitted only as the last instruction of the block. Branch encodings that carry their own condition field are not permitted in a block at all, so an assembler encodes a branch inside a block with an unconditional encoding and lets the IT supply the condition.
No branch may jump into the middle of a block; only a return from an exception handler §6.15 may resume inside one.
BKPT(breakpoint §14.29) executes unconditionally, even inside a block.
Breaking a restriction makes the behavior unpredictable, Arm’s term for behavior that the architecture does not define and that software must not rely on.
IT exists in Armv6T2, the architecture that introduced Thumb-2, in Armv7-M, in the Mainline variant of Armv8-M, and in Thumb code on the A and R profiles of Armv7 and later. Armv6-M cores, such as the Cortex-M0, have no IT instruction and make decisions with conditional branches only.
Worked example
A two-way choice. The four 16-bit instructions below set r1 to 7 if r0 is 0 and to 9 otherwise.
cmp r0, #0compare r0 with 0
ite eqnext instruction ifEQ, the one after it ifNE
moveq r1, #7
movne r1, #9
If r0 = 0: CMP computes and sets Z = 1, N = 0, C = 1 (no borrow) and V = 0. MOVEQ tests Z = 1, which holds, so r1 becomes 7. MOVNE tests Z = 0, which fails, so it does nothing. If r0 = 5: CMP computes and sets Z = 0, N = 0, C = 1 and V = 0. MOVEQ fails and does nothing; MOVNE holds and sets r1 to 9. In both cases all four instructions pass through, bytes of code, and the flags afterwards are those CMP produced, because 16-bit moves inside a block set no flags.
Flags that change inside the block. Start with r2 = 0 and run:
cmp r0, #0
itt eqboth following instructions onEQ
cmpeq r1, #3
moveq r2, #1
r0 = 0, r1 = 3:
CMPsets Z = 1. The condition ofCMPEQholds, so it executes: , so Z = 1.MOVEQtests Z = 1, which holds, so r2 = 1.r0 = 0, r1 = 4:
CMPsets Z = 1.CMPEQexecutes: , so Z = 0.MOVEQnow tests Z = 0, fails, and r2 stays 0.r0 = 5, r1 = 3:
CMPsets Z = 0.CMPEQfails, does nothing and leaves Z = 0.MOVEQfails, and r2 stays 0.
So r2 becomes 1 exactly when r0 = 0 and r1 = 3. Both instructions carry the suffix EQ, but the second is tested against the flags that the first produced. An analysis that evaluated the whole block once, against the flags from before the block, would predict r2 = 1 in the second case and be wrong.
Stated precisely
Let an IT block consist of instructions , where , with conditions : is the IT instruction’s condition, and each later is that condition (for T) or its opposite (for E). Let be the flags when starts and the flags after . Write for the truth value of condition under flags , given by the list above. Then, for each in turn:
if is true, has its normal effect, and is updated as specifies;
otherwise has no effect, and .
In both cases execution continues with the next instruction in memory, unless executed and wrote the program counter, which only may do. Inside the block, updates the flags only if it is CMP, CMN, TST or TEQ, a 32-bit instruction with its S bit set, an MSR that writes them, or, with the floating-point extension, a VMRS that copies the floating-point comparison flags into them.
The statement assumes that the processor implements an architecture with the IT instruction (Armv7-M, the Mainline variant of Armv8-M, or Thumb code on the A and R profiles of Armv7 and later), that the block obeys every restriction listed above, and that execution entered the block through its IT instruction or resumed it by an exception return.
Limits and common mistakes
Do not decide a whole block from the flags before it. Each instruction is tested against the flags at its own turn, and an executed compare inside the block changes the outcome for the instructions after it, as the second example shows.
Do not read flag behavior from the mnemonic. The same 16-bit encoding sets flags outside a block and does not set them inside one, so an analysis must know whether an instruction lies in a block, which in turn requires decoding from a known instruction start, as the Arm Thumb and Thumb-2 entry explains. A disassembly that starts after the IT instruction shows the block’s instructions without their conditions, as if they were unconditional.
A false condition makes a valid instruction harmless, but it does not repair a malformed block. A branch in the middle of a block, a jump into a block, or an IT instruction inside a block is unpredictable whatever the flags are.
A skipped instruction is still fetched and decoded. Conditional execution replaces branches with straight-line code; it does not make the skipped instructions free, and it is a gain only for short choices.
Statements about IT blocks apply to Thumb code only. In the original 32-bit Arm instruction set each instruction carries its own condition field, and no IT instruction is needed.
Questions answered
Which instructions execute when the condition is false, and which may change flags?
When an instruction’s condition is false, that instruction does not execute: in an IT (If-Then) block it behaves as a NOP (no operation), writing no register, no memory and no flags and raising no exception, while execution still moves past it and the block moves on to the next instruction’s condition. What still executes is everything not governed by that false condition: the IT instruction itself, which always executes and changes no flags; the other instructions of the block whose own conditions hold, each tested against the flags as they are at its turn; BKPT (breakpoint), which is unconditional even inside a block; and the code after the block.
Only instructions that actually execute may change the flags. Inside an IT block these are CMP, CMN, TST and the 32-bit TEQ, which always set flags; 32-bit data-processing instructions whose S bit is set (written with an S suffix); MSR when it writes the flags; and, on processors with the floating-point extension, VMRS when it copies the flags of a floating-point comparison into them. A 16-bit instruction other than CMP, CMN and TST does not set flags inside a block, even when the same encoding sets them outside one. A flag change made by an executed instruction affects the conditions of the later instructions of the same block.
Related entries: Condition flags (APSR) (§4.42), Arm Thumb and Thumb-2 (§4.40), Path sensitivity (§3.11). Further reading: [6, 81].