Instruction does not dominate all uses?

This has been driving me crazy. In order to JIT stuff in a simple REPL I'm
trying to wrap them in anonymous functions and then evaluate them (as per
recommendation of the Kaleidoscope guide).

With simple expressions it works fine. Like if I try and add 4 and 5, I get
this from the bare llvm::Value dump:

double 9.000000e+00

and this after it's wrapped in a function:

define double @0() {
  ret double 9.000000e+00

Then I define a function (ignores its one parameter and returns 4):

define double @test(double %x) {
  ret double 4.000000e+00

And get the dump from the llvm::Value call to it (passing 10 as the arg)

%calltmp = call double @test(double 1.000000e+01) ; <double> [#uses=1]

the dump from the call wrapped in a function:

define double @1() {
  ret double %calltmp

but when I try to verify the wrapping function, I get:

Instruction does not dominate all uses!
  %calltmp = call double @test(double 1.000000e+01) ; <double> [#uses=1]
  ret double %calltmp

The code that I'm using to create the wrapping functions:

What's going on here? From what I understand , "Instruction does not
dominate all uses!" has to do with using a variable before its definition? I
don't see where/how that's happening here.

You need to insert v into the basic block before the return. Note
that %calltmp is absent from the dump of the function. The verifier
is complaining because it found this pointer to %calltmp, but it
didn't find the definition anywhere above its use.


Ah, ok, so the call to builder.CreateCall needs to come after builder.SetInsertPoint for the wrapping function to work properly? I think I get it now.