[llvm-dev] Branches which return values in SelectionDAG

Nagurne, James via llvm-dev llvm-dev at lists.llvm.org
Fri Aug 7 15:57:43 PDT 2020

Hi all,

I am working on modeling an instruction similar to SystemZ's 'BRCT', which takes a register, decrements it, and branches if the register is nonzero. I saw that the LLVM backend for SystemZ generates the instruction in a MachineFunctionPass as part of a pass intended to eliminate or combine compares.

I then looked at ARM, where it uses the HardwareLoops pass first, and then a combine that occurs in the ARM ISel stage. It replaces branch instructions with special 'WLS' and 'LE' nodes that are custom selected into t2WhileLoopStart and t2LoopEnd pseudo instructions with isBranch and isTerminator set. These pseudo instructions are finalized in a later MachineFunctionPass.

I had originally intended to use the HardwareLoops pass to do most of the initial transformation and bookkeeping, allowing me to utilize the generated intrinsics in my own pass to further transform and customize the loop.
What I found out, however, is that I don't know enough about the SelectionDAG to know if this is possible.
Trying to combine the two concepts (Value-returning branches and handling them in the selection DAG), I wrote my backend to generate:

                %InitialVal = N
%IndVar = PHI(%InitialVal, %header, %DecVal, %body)
%MultipleReturns = call {i32, i1} compare_and_maybe_decrement(%IndVar, 1)
%DecVal = extract {i32, i1} %MultipleReturns 0
%Cond = extract {i32, i1} %MultipleReturns 1
br %Cond, body, exit

Then, I attempted to combine the intrinsic, extractions, and branch together in the SelectionDAG.
What I found, however, is that this concept, which seems fine in the LLVM IR, is not fine in the DAG.

Specifically, there is a CopyToReg in the DAG that occurs between the intrinsic and the branch that saves off %DecVal. I presume it's there because the value is leaving the DAG (to be copied from in the next iteration). With the branch node returning that value instead, it seems like there's no legal location in which to place this necessary CopyToReg. If you order it after the fused branch, I believe it's illegal because it's logically incorrect (only copy if we're terminating the loop?). If you order it before, I don't think the DAG makes sense anymore:

                                t1 = CopyToReg %1, t2 ; Copying a value before it's defined???
                t2 = Target::BR_DEC ...

Indeed, I get the abort "Operand not processed?" for the CopyToReg when I tried it, indicating something was amiss.
I'm more than willing to provide more context such as DAG dumps if people have ideas, I just didn't want to fill this email with debug.

Is what I'm doing possible? Or does it make sense to keep the special and separate compare_and_maybe_decrement operation until after selection is finished so that I can fuse using MachineInstrs instead?

Thanks for any help!
J.B. Nagurne
Code Generation
Texas Instruments
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20200807/5ebc2826/attachment.html>

More information about the llvm-dev mailing list