Table of Contents

  • 1. TargetMachine class
  • 2. Subtarget class
  • 3. TargetPassConfig class
  • 4. InstrInfo class
  • 5. TargetLowering class
    • 5.1. LowerCall () :
    • 5.2. LowerFormalArguments () :
    • 5.3. LowerReturn () :
  • 6. FrameLowering class
  • 7. SelectionDAGISel class

1 TargetMachine class

holds a lot of the details required for codegen, and it also acts as a factory for other backend classes, most notably for the Subtarget class and the TargetPassConfig.

2 Subtarget class

holds the configuration for the codegen, such as which features are enable.

3 TargetPassConfig class

configures the machine passes of the backend.

4 InstrInfo class

returns information about instructions, including registers.

5 TargetLowering class

provides a lowering of call-related instructions and also allows adding custom DAG rules. To support the lowering with target-specific operations. To enable function calls, we must implement the following methods.

5.1 LowerCall():

Handle call IR.e.g. %c = call @add(i32 %a, i32 %b). the add() is the call IR.

  1. Prepare the arugments for the call in register or memory.
  2. do the call.
  3. Get the call result back from register of memory.

Lower a call to a callseqstart + CALL + callseqend chain, and add and output parameter nodes.

  1. Analyze the operands of the call, assigning locations to each operand.
  2. Get a count of how many bytes are to be pushed on the stack.
  3. Create local copies for byval args
  4. Copy argument values to their designated locations.
  5. Join the stores, which are independent of one another.
  6. Build a sequence of copy-to-reg nodes, chained and glued together.
  7. The first call operand is the chain and the second is the target address.
  8. Add a register mask operand representing the call-preserved registers.
  9. Add argument registers to the end of the list so that they are known live into the call.
  10. Glue the call to the argument copies, if any.
  11. Emit the call.
  12. Mark the end of the call, which is glued to the call itself.
  13. Assign locations to each value returned by this call.
  14. Copy all of the result registers out of their specified physreg.

5.2 LowerFormalArguments():

Handle arguments passing from Caller to Callee via register or memory, in order to prepare arguments for the use in function body. e.g. add(i32 %a, i32 %b). the %a and %b are the incoming arugments for lowering.

  1. analyze the incoming argument with the help of the CCState class.
  2. loop over the return arguments with CCValAssign class, which tells us how to treat that arugment.
  3. copy the value into a register for accepting it, or get load the value into a register for accepting it if that value is passed via memory. Because based on the Calling Convention, the incoming value should be placed into a register, or a specific memory area, therefore, need to get the incoming value from the right place.
  4. further handling for incoming value if needed.
  5. return the updated chain SDValue to ensure the scheduling order.

5.3 LowerReturn():

Handle ret IR. e.g. ret i32 %a. %a is the return value of this ret IR.

  1. analyze the return arguments with the help of the CCState class.
  2. loop over the return arguments with CCValAssign class, which tells us how to treat that arugment.
  3. Handle each return value.
  4. copy the value into a register for returning it and chain and glue the copies together. because based on the Calling Convention, the return value should be placed into a register, or a specific memory area, therefore, need to move the return value to the right place.
  5. after looping over arguments, update the chain and glue for the return DAG Node.
  6. return the return DAG Node connecting the result of the lowering.

6 FrameLowering class

This class is responsible for handling the stack frame. This includes generating the prologue and epilogue code, handling register spills, and more.

Prologue and Epilogue insertions.

The standard function prologue performs any or all of the following tasks, as needed: • Allocates stack space • Saves return address • Saves the address of the memory return value • Saves Callee Registers.

The standard function epilogue performs the following tasks, as needed: • Either loads the return value or copies the result to the area pointed to by the address of the memory return value. • Restores Callee Saved Registers. • Deallocates local stack space

7 SelectionDAGISel class

DAG-based instruction selection.