[llvm-dev] Instruction selection for 'load' based on static vs. dynamic data

Krzysztof Parzyszek via llvm-dev llvm-dev at lists.llvm.org
Tue May 9 06:01:43 PDT 2017


On 5/9/2017 6:51 AM, Dr. ERDI Gergo wrote:
> On Mon, 8 May 2017, Krzysztof Parzyszek via llvm-dev wrote:
> 
> However, what's still missing with that plan is how to propagate that 
> information "upwards". By the time it gets to instruction selection, the 
> `getelementptr` is already expanded into some arithmetic expression of a 
> base address and some arbitrary offset calculations.

At this point it's a problem of writing appropriate selection patterns.

> So for example, with this code:
> [...]
> 
> (add
>    (sign_extend (CopyFromReg %vreg2))
>    (WRAPPER TargetGlobalAddress<@switch.table>))
> 
> not a WRAPPER directly.

Such cases can be treated with conjunction with the load or store 
instruction, for example:

def: Pat<(ld (add (WRAPPER RC:$addr), (sign_extend RC:$offset))),
          (load_instruction_rr RC:$addr, RC:$offset)>;

Where "load_instruction" is a machine load instruction with base address 
and an offset, both in registers, and RC is the corresponding register 
class.

For cases where the offset is an immediate, you can have

def: Pat<(ld (add (WRAPPER RC:$addr), imm:$offset)),
          (load_instruction_ri RC:$addr, imm:$offset)>;

The "imm" is a general predicate for any immediate. If you want to only 
match a subset of immediates (e.g. those that are within some range), 
you can define your own PatLeaf and then use it in place of imm:

def Test : PatLeaf<(i32 imm), [{
   int64_t V = N->getSExtValue();
   return V > -17 && V < 12;   // Only true for [-16, 11].
}]>;

def: Pat<(ld (add (WRAPPER RC:$addr), Test:$offset)),
          (load_instruction_ri RC:$addr, imm:$offset)>;

The "imm" in the second line can remain, it's the first part of the 
"Pat" that matters, but if you want, you can have "Test" in both.

There is also a target hook "isOffsetFoldingLegal" in TargetLowering, 
which tells the DAG combiner if it can make the immediate offset a part 
of the global address itself.


-Krzysztof

-- 
Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum, 
hosted by The Linux Foundation


More information about the llvm-dev mailing list