ConsistentProgrammer
ConsistentProgrammer

Reputation: 1314

How to read local arrays in llvm

I have generated the following bitcode. fuelTank is an array that I pass to a function called getEngineValue(int x[]).

%fuelTank = alloca [5 x i32], align 4
call void @llvm.dbg.declare(metadata !{[5 x i32]* %fuelTank}, metadata !39), !dbg !40
%0 = bitcast [5 x i32]* %fuelTank to i8*, !dbg !40
call void @llvm.memcpy.p0i8.p0i8.i32(i8* %0, i8* bitcast ([5 x i32]* @_ZZ12checkFuelSysvE8fuelTank to i8*), i32 20, i32 4, i1 false), !dbg !40
call void @_Z17getFuelIndicationPi(i32* getelementptr inbounds ([5 x i32]* @piston, i32 0, i32 0)), !dbg !41
%arraydecay = getelementptr inbounds [5 x i32]* %fuelTank, i32 0, i32 0, !dbg !42
call void @_Z14getEngineValuePi(i32* %arraydecay), !dbg !42
ret void, !dbg !43

I want to read the value stored in the fuelTank array. I guess I can read it from the alloca instruction, but couldn't find any success with it.

Note: I know how to access the array using @llvm.memcpy, but I don't want this.

Upvotes: 1

Views: 1070

Answers (1)

Oak
Oak

Reputation: 26868

To get a value of type [5 x i32] from %fuelTank, you can use the load instruction to read its content.

If you just want what is stored in a single array index (of type i32), you can use getelementptr to get the address of a specific index and then load it, or you can load first and then get the single value via an extractvalue instruction.

Upvotes: 1

Related Questions