Skip to content

Assignments 08 isel

Mugundan edited this page Nov 27, 2026 · 1 revision

Stage 7: Instruction Selection and the First Function

Relevant source files

The following files were used as context for generating this wiki page:

The first real milestone: a function with arithmetic compiles to Toy assembly.

Purpose and Scope

R- and I-format ALU instructions with patterns, immediates, ToyDAGToDAGISel, ToyAsmPrinter, ToyMCInstLower.

Read First

Tasks

  1. Instruction classes for R and I formats with Sched<[]> placeholders.
  2. Patterns for add sub and or xor shl srl sra mul setlt setult; simm16 and uimm16 leaves.
  3. Materialize 32-bit constants with lui + ori (a Pat on imm).
  4. Selector with SelectCode; AsmPrinter with MCInstLower.
  5. Minimal ToyFrameLowering (no frame) so PEI does not crash.

Tests

define i32 @f(i32 %a, i32 %b) { %s = add i32 %a, %b
  %m = mul i32 %s, 12345678
  ret i32 %m }

Expected shape (illustrative): add r0, r0, r1 / lui r1, ... / ori r1, r1, ... / mul r0, r0, r1 / ret. Add lit tests with FileCheck.

Done When

  • All ALU operations selected at -O0 and -O2
  • Large constants work
  • -verify-machineinstrs clean

Stretch

Add -O2 patterns that fold add with an immediate into addi.

Next: stage 8. All Toy code and expected output on this page are illustrative, not LLVM source and not tool output; your exact output may differ.

Start here

Computer Organization & Architecture

Hexagon ISA

Operating Systems

C++ for LLVM Development

LLVM IR

SelectionDAG

Backend Assignments

Target Bring-up

Calling Conventions

Frame Lowering

MC Layer

Register Allocation

Scheduling

Clone this wiki locally