loop only executes once

I'm trying to get to grips with the c interface of llvm
intending to eventually develop a front end for Purebasic
Though I've hit the wall already writing an iterative Fibonacci
While I think the module dump looks ok it doesn't work
for any input > 2 the function returns 2, the loop only executes once

Hopefully someone can see what the problem is from the output and the
source. Does the input ArgX require to be cast?


module dump
verifying module
; ModuleID = 'test fib'

define fastcc i32 @fib2(i32 %ArgX) {
  %A = alloca i32
  %B = alloca i32
  %C = alloca i32
  %D = alloca i32
  store i32 1, i32* %A
  store i32 1, i32* %B
  store i32 0, i32* %C
  store i32 %ArgX, i32* %D
  %cond = icmp sle i32 %ArgX, 2
  br i1 %cond, label %IF, label %Else

IF: ; preds = %EntryBlock
  ret i32 1

Else: ; preds = %while, %
  %PD = load i32* %D
  %cond1 = icmp sgt i32 %PD, 2
  br i1 %cond1, label %while, label %ewhile

while: ; preds = %Else
  %pA = load i32* %A
  %pB = load i32* %B
  %addab = add i32 %pA, %pB
  store i32 %addab, i32* %C
  store i32 %pA, i32* %B
  store i32 %addab, i32* %A
  %sub = sub i32 %PD, 1
  store i32 %sub, i32* %D
  br label %Else

ewhile: ; preds = %Else
  %CR = load i32* %C
  ret i32 %CR

Source of function

Procedure CreateFibFunction(M.i,Context.i)
intType = LLVMInt32Type()
//Function returns an int And takes an int As the only parameter.
FunctionReturnType = LLVMFunctionType(IntType,@IntType,1,0)
//Create the fib function definition And insert it into the module M.
Fib2 = LLVMAddFunction(M, "fib2",FunctionReturnType)
//Set the function call convention To FastCall so it can utilize tail
//Get a pointer To the ArgX.i And add To function...
ArgX = LLVMGetFirstParam(Fib2) // Get the arg.
LLVMSetValueName(ArgX, "ArgX") // Give it a symbolic name.

Builder = LLVMCreateBuilderInContext(Context)
BB = LLVMAppendBasicBlockInContext(Context, Fib2, "EntryBlock")
LLVMPositionBuilderAtEnd(Builder, BB)
//Protected a,b,c
A = LLVMBuildAlloca(Builder, IntType,"A")//
B = LLVMBuildAlloca(Builder, IntType,"B")//
C = LLVMBuildAlloca(Builder, IntType,"C")//
D = LLVMBuildAlloca(Builder, IntType,"D")//
One = LLVMConstInt(IntType, 1, 0)
Zero = LLVMConstInt(IntType, 0, 0)
//If argx <= 2 : Return 1 : Goto Else
IFBB = LLVMAppendBasicBlockInContext(Context, Fib2, "IF")
ElseBB = LLVMAppendBasicBlockInContext(Context, Fib2, "Else")
Two = LLVMConstInt(IntType, 2, 0)
CondInst = LLVMBuildICmp(Builder, #LLVMIntSLE, ArgX,Two, "cond")
LLVMBuildCondBr(Builder, CondInst, IFBB, ElseBB)
LLVMPositionBuilderAtEnd(Builder, IFBB)
LLVMPositionBuilderAtEnd(Builder, ElseBB)

WhileBB = LLVMAppendBasicBlockInContext(Context, Fib2, "while")
EWhileBB = LLVMAppendBasicBlockInContext(Context, Fib2, "ewhile")
// While argx > 2
PD = LLVMBuildLoad(Builder,D,"PD")//
CondInst1 = LLVMBuildICmp(Builder, #LLVMIntSGT, PD, Two, "cond1")
LLVMBuildCondBr(Builder, CondInst1, WhileBB, EWhileBB)
LLVMPositionBuilderAtEnd(Builder, WhileBB)
//c = a + b//
//b = a//
//a = c//
PA = LLVMBuildLoad(Builder,A,"pA")//
PB = LLVMBuildLoad(Builder,B,"pB")//
AddAB = LLVMBuildAdd(Builder,PA,PB,"addab")
sub = LLVMBuildSub(Builder,PD,One,"sub")

LLVMPositionBuilderAtEnd(Builder, EWhileBB)
CR = LLVMBuildLoad(Builder,C,"CR")//
//ProcedureReturn C
LLVMBuildRet(Builder, CR)

  //Don't forget to free the builder.
    //Return the function ref
    ProcedureReturn Fib2

I just tested the given IR dump, and it seems to compute Fibonacci
numbers correctly... your issue is probably somewhere else.