This is an archive of the discontinued LLVM Phabricator instance.

[MC][PredicateExpander] Extend the grammar to support simple switch and return statements.
ClosedPublic

Authored by andreadb on Aug 8 2018, 9:40 AM.

Details

Summary

This patch introduces tablegen class MCStatement.

Currently, an MCStatement can be either a return statement, or a switch statement.

MCStatement:
   MCReturnStatement
   MCOpcodeSwitchStatement

A MCReturnStatement expands to a return statement, and the boolean expression associated with the return statement is described by a MCInstPredicate.

An MCOpcodeSwitchStatement is a switch statement where the condition is a check on the machine opcode. It allows the definition of multiple checks, as well as a default case. More details on the grammar implemented by these two new constructs can be found in the diff for TargetInstrPredicates.td.

With this patch, method "isThreeOperandsLEA" becomes more readable (see below):

static bool isThreeOperandsLEA(const MachineInstr &MI) {
  switch(MI.getOpcode()) {
  case X86::LEA32r :
  case X86::LEA64r :
  case X86::LEA64_32r :
  case X86::LEA16r :
    return (
      MI.getOperand(1).isReg()
      && MI.getOperand(1).getReg() != 0
      && MI.getOperand(3).isReg()
      && MI.getOperand(3).getReg() != 0
      && (
        (
          MI.getOperand(4).isImm()
          && MI.getOperand(4).getImm() != 0
        )
        || (MI.getOperand(4).isGlobal())
      )
    );
  default :
    return false;
  } // end of switch-stmt
}

Before it was like this:

static bool isThreeOperandsLEA(const MachineInstr &MI) {
  return (
    (
      MI.getOpcode() == X86::LEA32r
      || MI.getOpcode() == X86::LEA64r
      || MI.getOpcode() == X86::LEA64_32r
      || MI.getOpcode() == X86::LEA16r
    )
    && MI.getOperand(1).isReg()
    && MI.getOperand(1).getReg() != 0
    && MI.getOperand(3).isReg()
    && MI.getOperand(3).getReg() != 0
    && (
      (
        MI.getOperand(4).isImm()
        && MI.getOperand(4).getImm() != 0
      )
      || (MI.getOperand(4).isGlobal())
    )
  );
}

This patch makes it easier to read the body of auto-generated TargetInstrInfo predicates.

I also plan to reuse/extend the grammar of MCStatements to enable the definition of more complex TII hooks.

For example, I'd like in future to expand this framework to better expose dependency-breaking instructions and also instructions that have an implicit (often undocumented) false dependency on the output register (I will raise a PR for this last task). Knowledge about dep-breaking instructions (and false-deps on the output registers) could then be used by tools like llvm-mca to perform a more accurate data dependency analysis. We could also expose that knowledge to machine schedulers (for example: to tweak the data dependency graph) to improve the schedule at the end of the process.
For now, this is just a first step (mostly a minor cosmetic change to polish the new predicates framework).

Please let me know what you think.

Thanks,
-Andrea

Diff Detail

Repository
rL LLVM

Event Timeline

andreadb created this revision.Aug 8 2018, 9:40 AM
mattd added a comment.Aug 8 2018, 9:59 AM

I really like this change! The result is easier to read.

utils/TableGen/PredicateExpander.cpp
186 ↗(On Diff #159737)

I'm curious, do we assume all cases are fall-through? I don't see a break statement being generated, perhaps I overlooked that?

utils/TableGen/PredicateExpander.h
82 ↗(On Diff #159737)

nit: Don't really need this whitespace.

andreadb added inline comments.Aug 8 2018, 10:12 AM
utils/TableGen/PredicateExpander.cpp
186 ↗(On Diff #159737)

Your analysis is correct.
By construction, this new grammar only allows structures that end with return statements. Since there cannot be a fall-through, I avoided to generate a redundant break at the end of each case.

utils/TableGen/PredicateExpander.h
82 ↗(On Diff #159737)

I will fix it. Thanks.

mattd added inline comments.Aug 8 2018, 10:13 AM
utils/TableGen/PredicateExpander.cpp
186 ↗(On Diff #159737)

Ah, yep. That makes sense, thanks!

mattd accepted this revision.Aug 9 2018, 8:00 AM

Thanks for this patch! LGTM.

This revision is now accepted and ready to land.Aug 9 2018, 8:00 AM
This revision was automatically updated to reflect the committed changes.