[LLVMdev] Getelementptr woes
Vladimir Prus
ghost at cs.msu.su
Fri Jun 18 04:31:01 PDT 2004
Chris Lattner wrote:
> > So I wonder if I can away by writing two passes which work on LLVM level.
> > The first pass would extract all ConstantExpr* operands from instructions
> > and move them into separate instructions. E.g. the example above would
> > become:
> >
> > %tmp.1 = sbyte* getelementptr ([11 x sbyte]* %.str_1, ..........
> > %tmp.0.i = call int (sbyte*, ...)*
> > %printf(%tmp.1 , .........
>
> While this is feasible and possible, I really wouldn't recommend it. Take
> a look at how the X86 backend handles this stuff. For shift instructions,
> for example, we have:
>
> void ISel::visitShiftInst(ShiftInst &I) {
> MachineBasicBlock::iterator IP = BB->end();
> emitShiftOperation(BB, IP, I.getOperand(0), I.getOperand(1),
> I.getOpcode() == Instruction::Shl, I.getType(),
> getReg(I));
> }
>
> Basically all of the code for emitting shift instructions is in the
> emitShiftOperation which takes two Value* operands to shift and a virtual
> register (the last operand) to set to the result of the shift.
>
> Because of this pattern, emitShiftOperation is used by the constant
> expression support routines (see copyConstantToRegister) to codegen shift
> constant expressions as well as shift instructions themselves.
But I'd still have to write copyConstantToRegister for my backend, which is
code duplication. What I'm trying to avoid is making by backend a blatant
copy-paste.
> I don't think this is TOO completely horrible to support constant
> expressions in general, though it can (and will be in the future)
> certainly be improved.
Ok, I've another idea. I wonder if it's possible to create
'BasicInstructionSelector'. It would have a bunch of pure virtual methods
like emitShiftOperation and
1. Visitors for each operations, written just like above
2. Handling for constant expressions.
So, individual backend would have to implement only emitShiftOperation and
other emit* methods.
> > The second pass would convert getelementptr into casts and pointer
> > arithmetic.
>
> This is something else I would not recommend. The X86 "simple"
> instruction selector is not really simple anymore, so it's not a good
> model to follow for how to emit GEP instructions in a straight-forward
> way. Try taking a look at the emitGEPOperation method (line 3211) of
> revision 175 of X86/InstSelectSimple.cpp though. This version is from
> before the various address optimizations were implemented. Here's a
> direct link:
> http://llvm.cs.uiuc.edu/cvsweb/cvsweb.cgi/llvm/lib/Target/X86/InstSelectSim
>ple.cpp?rev=1.175
>
> To expand out a getelementptr into code it basically just walks through
> the indices turning them into the appropriate scale and add as
> appropriate. It's quite a bit simpler and cleaner to just implement
> support for this instead of hacking it into casts and pointer arithmetic.
>
> :)
Uhm... the code runs from line 2311 till line 2433 so I naturally don't want
to copy-paste it ;-) And the only target-specific parts are instruction
codes. There should be some way to abstract this.... gotta try further.
- Volodya
More information about the llvm-dev
mailing list