[LLVMdev] Best way to interface with MSVC _ftol2 runtime function for fptoui?

Joe Groff arcata at gmail.com
Tue Jan 24 17:20:20 PST 2012


On Tue, Jan 24, 2012 at 4:32 PM, Jakob Stoklund Olesen <stoklund at 2pi.dk> wrote:
> Yes, your definition of the new instruction looks sane.
>
> However, you shouldn't expand the instruction right away in EmitInstrWithCustomInserter(), and leaving the pseudo and call instructions side by side is not going to work.
>
> Just leave the pseudo-instruction alone until it hits X86FloatingPoint, where you can rewrite it.
>
> Look at the code handling INLINE_ASM. You need to do the same, except you have fixed arguments STUses=1 and STClobbers=1, ST*=0. That should greatly simplify the code you need.

That makes sense; thanks for the tip. Are the getCopyToReg(ST0) and
addReg(ST0, ImplicitKill) calls on the expanded MI at all necessary
then since X86FloatingPoint seems to manage that all internally?

> When SSE is available, x87 registers are only ever used for f80.

It looks like it always tries to use fisttp when converting to i64.
This bitcode:

define i64 @foo(double %x) nounwind readnone {
init:
  %0 = fptosi double %x to i64
  ret i64 %0
}

gets compiled by LLVM 3.0 to:

_foo:                                   # @foo
# BB#0:                                 # %init
	subl	$20, %esp
	movsd	24(%esp), %xmm0
	movsd	%xmm0, 8(%esp)
	fldl	8(%esp)
	fisttpll	(%esp)
	movl	(%esp), %eax
	movl	4(%esp), %edx
	addl	$20, %esp
	ret

with a seemingly redundant movsd pair before the fisttp instruction.

-Joe



More information about the llvm-dev mailing list