Repository navigation
Hexagon ISA 05 instructions
Relevant source files
The following files were used as context for generating this wiki page:
- llvm/lib/Target/Hexagon/HexagonDepInstrInfo.td
- llvm/lib/Target/Hexagon/HexagonRegisterInfo.td
- Qualcomm: Hexagon V6x/V7x Programmer's Reference Manual, instruction set chapters
This page tours the scalar instruction set by job: computing on data, moving data to and from memory, and changing control flow. It uses the algebraic syntax from Encoding and Mnemonics.
Assembly on this page is illustrative and was not assembled.
This page covers ALU and multiply instructions, saturation and DSP-style operations, loads and stores with their addressing modes, atomics, and branches, calls, returns and hardware loops. Vector and floating-point instructions are on FP, HVX and Alignment.
flowchart TD
ISA["Scalar Hexagon ISA"] --> D["Data"]
ISA --> M["Memory"]
ISA --> C["Control"]
D --> D1["ALU32: add, sub, and, or, xor, mux, cmp"]
D --> D2["XTYPE: mpy, shifts, bit ops, 64-bit, saturating DSP ops"]
M --> M1["Loads: memb/memub/memh/memuh/memw/memd"]
M --> M2["Stores, new-value stores"]
M --> M3["Atomics: memw_locked, barriers"]
C --> C1["jump, call, jumpr, dealloc_return"]
C --> C2["Compare-and-jump, loop0/loop1"]
| Instruction | Meaning |
|---|---|
r0 = add(r1, r2) / r0 = add(r1, #-4)
|
Add register or immediate |
r0 = sub(r1, r2) |
Subtract |
r0 = and(r1, r2), or, xor, r0 = not(r1)
|
Logic |
r0 = mux(p0, r1, r2) |
Select: p0 ? r1 : r2
|
p0 = cmp.gt(r1, r2), cmp.eq, cmp.gtu
|
Compare into a predicate (signed/unsigned) |
r0 = mpyi(r1, r2) |
32-bit multiply, low result |
r1:0 = mpy(r2, r3) |
32×32 → 64-bit multiply into a pair |
r0 += mpyi(r1, r2) |
Multiply-accumulate |
r0 = asl(r1, #3), asr, lsr
|
Shifts |
r0 = add(r1, r2):sat |
Saturating add: clamps instead of wrapping |
r1:0 = add(r3:2, r5:4) |
64-bit add on pairs |
r0 = vaddh(r1, r2) |
Two 16-bit adds packed in 32 bits (small scalar SIMD) |
Saturation and packed halfword/byte operations are why Hexagon suits audio and fixed-point DSP: overflow clamps to the maximum instead of wrapping (overflow is recorded in usr).
| Load | Size | Store |
|---|---|---|
r0 = memb(...) / memub
|
8-bit, signed / unsigned | memb(...) = r0 |
r0 = memh(...) / memuh
|
16-bit | memh(...) = r0 |
r0 = memw(...) |
32-bit | memw(...) = r0 |
r1:0 = memd(...) |
64-bit into a pair | memd(...) = r1:0 |
| Mode | Syntax | Use |
|---|---|---|
| Base + offset | memw(r1 + #8) |
Struct fields |
| Base + scaled index | memw(r1 + r2 << #2) |
Arrays |
| Post-increment | memw(r1++#4) |
Walking arrays in loops |
| Post-increment by modifier | memw(r1++m0) |
Step held in m0/m1
|
| Circular | memw(r1++#4:circ(m0)) |
Ring buffers using cs0/cs1 and m0
|
| Absolute / GP-relative |
memw(#global), memw(gp + #x)
|
Globals and small data |
flowchart LR
B["r1 = buffer start"] --> L["r0 = memw(r1++#4)"]
L --> R["load from r1, then r1 = r1 + 4"]
R --> L
Loads and stores run in slots 0 and 1, so a packet holds at most two memory operations. Accesses must be naturally aligned (FP, HVX and Alignment).
Hexagon uses a load-locked / store-conditional pair:
// illustrative: atomic increment of *r1
retry:
r2 = memw_locked(r1)
r2 = add(r2, #1)
memw_locked(r1, p0) = r2 // p0 = true if the store succeeded
if (!p0) jump retry
barrier orders memory operations between threads.
Sources: llvm/lib/Target/Hexagon/HexagonDepInstrInfo.td:21720, llvm/lib/Target/Hexagon/HexagonDepInstrInfo.td:21995, llvm/lib/Target/Hexagon/HexagonRegisterInfo.td:166-178
| Instruction | Meaning |
|---|---|
jump label |
Unconditional branch |
if (p0) jump:t label |
Conditional branch with a taken hint |
p0 = cmp.eq(r0, #0); if (p0.new) jump:nt label |
Compare and branch in one packet |
call func |
Call: return address to r31 (lr) |
jumpr r31 |
Return (jump to register) |
allocframe(#n) / deallocframe
|
Push/pop fp and lr and reserve stack |
dealloc_return |
Restore frame and return in one instruction |
loop0(label, #N) / :endloop0
|
Zero-overhead loop |
sequenceDiagram
participant A as caller
participant F as func
A->>F: call func (r31 = return address)
F->>F: allocframe(#16): save fp, lr; sp -= 16+8
F->>F: body
F->>A: dealloc_return: restore fp, lr, sp; jump to lr
A function in this style:
// illustrative: int sum(int *p, int n)
sum:
{ loop0(.L1, r1)
r2 = #0 }
.L1:
{ r3 = memw(r0++#4)
r2 = add(r2, r3) }:endloop0
{ r0 = r2
jumpr r31 }
Sources: llvm/lib/Target/Hexagon/HexagonDepInstrInfo.td:5020, llvm/lib/Target/Hexagon/HexagonDepInstrInfo.td:5639-5656
- Data instructions include saturating, packed and multiply-accumulate forms for DSP work.
- Memory instructions have rich addressing: post-increment, modifiers, circular buffers; at most two per packet.
- Control uses predicates, compare-and-jump in one packet,
call/jumpr r31, frame helpers and hardware loops.
- Computer Architecture Overview
- Fundamentals: CPU, Memory and Instructions
- Pipelining
- Branches and Remedies
- Instruction-Level Parallelism
- Memory and Ordering
- Caches and Coherence
- Multicore
- Multithreading in Hardware
- SIMD and GPUs
- Glossary and Summary
- Hexagon Overview and Processor Properties
- ISA Fundamentals: Packets, Slots and Threads
- Registers
- Instruction Encoding and Mnemonics
- Data, Memory and Control Instructions
- Floating Point, HVX and Alignment
- Caches, MMU and Glossary
- Operating Systems Overview
- Processes
- Threads
- CPU Scheduling
- Synchronization
- Deadlock
- Memory Management
- Virtual Memory
- Files, Storage and I/O
- Kernel and System Calls
- Interview Q&A and Glossary
- C++ for LLVM Development: Overview
- C++ Basics
- Classes and RAII
- Ownership and Move Semantics
- Templates and Lambdas
- LLVM Data Structures (ADT)
- LLVM-Style RTTI: isa, cast, dyn_cast
- Errors, Output and Assertions
- Iterators and Ranges
- Building, Debugging and Exercises
- LLVM IR Overview
- Structure: Modules, Functions, Blocks and Values
- Type System
- Arithmetic, Bitwise and Cast Instructions
- Compare and Select
- Memory: alloca, load, store and getelementptr
- Control Flow: br, switch, ret and call
- SSA and Phi
- Control Flow Graph and Loops
- Dominance, Dominator Tree and Dominance Frontier
- Data-flow Analysis
- Writing an IR Pass
- Common Passes
- Exercises, Tooling and Glossary
- SelectionDAG Overview
- Pipeline, Phases and Traversal Orders
- Core Data Structures
- DAG Construction
- DAG Combiner
- Legalization
- Type Legalization
- Operation and Vector Legalization
- Instruction Selection
- TableGen Pattern Language
- Matcher Interpreter and Folding
- AArch64 Instruction Selection
- Scheduling and MIR Emission
- PHI Node Resolution
- AArch64 Walkthroughs
- Arithmetic and Data Movement
- Memory Instructions
- Control Flow and Calls
- End-to-End Example
- Tooling, Debugging and Extending
- Selector Comparison and Glossary
- Assignments: Build the Toy Backend
- Stage 1: Build Setup
- Stage 2: Target Registration
- Stage 3: TableGen Skeleton
- Stage 4: MC Layer for Assembly Output
- Stage 5: TargetMachine and Subtarget
- Stage 6: TargetLowering, Arguments and Returns
- Stage 7: Instruction Selection and the First Function
- Stage 8: Registers, Copies and Spills
- Stage 9: Frame Lowering
- Stage 10: Memory, Globals and Control Flow
- Stage 11: Calls
- Stage 12: Scheduling Model
- Stage 13: Object Files
- Stage 14: Capstone, a Complete Toy Backend
- Target Bring-up Overview
- Directory Layout, CMake and Registration
- The Target .td File
- TargetMachine and Subtarget
- TargetLowering Setup
- Calling Conventions
- Instructions and Instruction Selection
- Registers and Allocation Hooks
- Frame Lowering
- AsmPrinter and the MC Layer
- Scheduling
- A Minimal Toy Target
- Bring-up Checklist and AArch64 File Map
- Tooling and Glossary
- Calling Conventions Overview
- The CallingConv.td Language
- CCState and CCValAssign
- LowerFormalArguments
- LowerCall
- LowerReturn and CanLowerReturn
- Varargs, byval, sret and swifterror
- Tail Calls and Sibling Calls
- Custom Backend Guide and Checklist
- AArch64 Walkthroughs
- Tooling and Glossary
- Frame Lowering Overview
- MachineFrameInfo
- TargetFrameLowering Hooks
- The PEI Pass
- Callee-Saved Registers and Stack Layout
- Frame Index Elimination and the Register Scavenger
- Shrink Wrapping
- CFI and Unwind Information
- Custom Backend Guide and Checklist
- AArch64 Walkthroughs
- Tooling and Glossary
- MC Layer Overview
- Core Data Structures
- Streamers
- Assembler and Layout
- Fixups and Relocations
- Writing an MC Layer for a Custom Backend
- MCAsmInfo and the Instruction Printer
- Code Emitter
- AsmBackend and Object Writer
- Assembly Parser and Disassembler
- Bring-up Checklist and Common Failures
- Tooling and Glossary
- Register Allocation Overview
- The Register Allocation Pipeline
- Registers, Virtual Registers and Register Classes
- Liveness: Slot Indexes and Live Intervals
- Preparing Code for Allocation
- PHI Elimination and Two-Address Lowering
- Register Coalescer
- Allocation Framework
- The Register Allocators
- Fast Register Allocator
- Basic Register Allocator
- Greedy Register Allocator
- PBQP Register Allocator
- Spilling and Splitting
- Spill Weights and Copy Hints
- The Inline Spiller
- Live Range Splitting
- Rewriting and Post-RA Passes
- Writing Register Allocation Support for a Custom Backend
- Register Definitions in TableGen
- TargetRegisterInfo Hooks
- TargetInstrInfo Hooks
- Pass Pipeline Hooks
- Bring-up Checklist and Common Failures
- AArch64 Walkthroughs
- Tooling and Debugging
- Glossary and Where Do I Look