LLVM store operand must be a pointer - c++

I'm trying to store the value of a global variable into a local variable in a function in my custom LLVM Pass.
the global variable is defined as
GlobalVariable* gvar_int32_test = new GlobalVariable(
/*Module=*/ M,
/*Type=*/ IntegerType::get(M.getContext(), 32),
/*isConstant=*/ false,
/*Linkage=*/ GlobalValue::CommonLinkage,
/*Initializer=*/0, // has initializer, specified below
/*Name=*/"global_test_var");
gvar_int32_test->setAlignment(4);
The local variable I intend to store into is originally being used as the store location for a call instruction. I attempt to get this value using
Value* localVar = ci->getOperand(0) //ci is the call instruction
using an IR builder, I attempt to write a store instruction as:
StoreInst* strLocIns = builder.CreateStore(gvar_int32_test, localVar, false);
//my intent is to create an instruction that means localvar = globalvar;
Similarly, earlier in the code I attempt to store the value in the return instruction of a called function to the global variable
Value* value = ri->getReturnValue(); //ri is the return instruction
// Some more code, including setting the IR builder insertion point
StoreInst* strIns = builder.CreateStore(value, gvar_int32_test, false);
//here the intention is globalvar = localvar
when I try to compile my with my pass included, I get the error:
Store operand must be a pointer.
store i32* #global_test_var, i32 %5
I'm not sure what I am doing wrong. Both of the arguments for the variables that I pass to the IR builder are pointers, but the IR is somehow broken. I think that i32 %5 should be i32* %5 to indicate that %5 points to an i32, but I don't know how to fix my code to make that happen. How do I fix this error?

Swap the operands of store: first is what and second - where.

Related

Find pointer to the Instruction that assigns to value in LLVM

Suppose I have a pointer to some Value* val in LLVM. I want to get a pointer to Instruction that assigned value to the variable that val points to. How can I do it?
This is an example of code
%add = add nsw i32 %aa, %bb
%div = sdiv i32 %add, %cc
I have a pointer to variable add and I want to get a pointer to the Instruction that assigned to variable add, which is pointer to %add = add nsw i32 %aa, %bb instruction
So far the only thing I found was a pointer to Basicblock that the variable belongs to. This is how I did it
Instruction * tmpI = dyn_cast<Instruction>(val);
BasicBlock * b = tmpI->getParent();
The correct answer was provided in the comments by jmmartinez:
The LLVM-IR is a Static-Single-Assignment Intermediate Representation. The pointer to the instruction IS the pointer to the assignment. The add instruction and the assignment to the add register have a one-to-one matching since registers can only be assigned once, and instructions can only assign to a single register.
[...] In your code example. if the variable val points to the register %add. It happens that val points to an llvm::Instruction (which inherits from llvm::Value). And this instruction is the addition you're looking for.
To add a bit more detail, the LLVM Value type is a base class of the LLVM Instruction type. For IR values like %add in the example, the IR allocates a specific Instruction subtype, BinaryOperator in the case of an add instruction. So the pointer to a Value for %add is actually a pointer to the same object as the instruction, just using its base class.
LLVM also has a special system for casting these pointers between types in the IR type hierarchy: https://llvm.org/docs/ProgrammersManual.html#the-isa-cast-and-dyn-cast-templates
Here are some example functions that take a specific llvm::Value and return the llvm::Instruction that produces that value:
#include <llvm/IR/Value.h>
#include <llvm/IR/Instruction.h>
using llvm::Value;
using llvm::Instruction;
using llvm::cast;
using llvm::dyn_cast;
using llvm::dyn_cast_or_null;
using llvm::isa;
// Either convert a `Value` to an `Instruction`
// or return null.
auto tryGetInstr(Value* value) -> Instruction* {
return dyn_cast<Instruction>(value);
}
// Just test whether a value was computed with an
// `Instruction` without converting it. Don't use
// this if you'll just end up converting it, use
// the above that combines the two.
auto isInstr(Value* value) -> Instruction* {
return isa<Instruction>(value);
}
// When you already know the value *must* be computed
// by an instruction, you can assert this and convert
// it in a way that will never produce a null.
auto getInstr(Value* value) -> Instruction* {
return cast<Instruction>(value);
}
// If `value` is non-null, try to convert it to an
// `Instruction`. Returns null if `value` is null
// or it isn't referencing an `Instruction`.
auto tryGetInstrIfNonNull(Value* value) -> Instruction* {
return dyn_cast_or_null<Instruction>(value);
}
https://cpp.compiler-explorer.com/z/f13bT4vb5

Adding instrumentation to perform a null check in LLVM IR when dereferencing a pointer

I am writing an LLVM pass where I want to print out the values of function arguments. I am only focusing on integers and pointers to integers (which will also involve char, I guess but that is not a problem for my purposes). If the argument is a pointer, I want to dereference it and print the value that is pointed to by the pointer. To guard against null pointers, in normal C code I can do:
function foo(int* bar, int** baz) {
if (bar) {
printf("bar: %d\n", *bar);
}
if (baz) {
printf("baz: %d\n", **baz);
}
}
Is there a way to do something similar in LLVM? Currently I am doing the following to actually load the value by dereferencing the pointer. I am working off a StoreInst that LLVM generates to set up the values of function parameters. For example, for the function above, there would be something like:
%bar.addr = alloca i32*, align 8
%baz.addr = alloca i32**, align 8
store i32* %bar, i32** %bar.addr, align 8
store i32** %baz, i32*** %baz.addr, align 8
So using that StoreInst, I am doing something like this:
auto *value = store->getValueOperand();
auto *type = value->getType();
int indirection = 0;
while (type->isPointerTy()) {
indirection++;
type = type->getPointerElementType();
}
Value* load = value;
unsigned int bitWidth = type->getIntegerBitWidth(); // I have already verified earlier on that
// this eventually resolves to an IntegerType
while (indirection > 0) {
load = (indirection == 1) ? irb.CreateLoad(getIntegerType(bitWidth), load)
: irb.CreateLoad(getPointerToIntegerType(indirection - 1, bitWidth), load);
--indirection;
}
value = load;
The getIntegerType is just a convenience function that returns an IntegerType with the provided bit-width and using the current LLVM context. The getPointerToIntegerType function is as follows:
Type* getPointerToIntegerType(int indirection, unsigned int bitWidth) {
Type* type = getIntegerType(bitWidth);
while (indirection > 0) {
type = PointerType::get(type, 0);
--indirection;
}
return type;
}
Using this I get the following load instructions after the store instructions (I've omitted some stuff from the call instructions for brevity):
store i32* %bar, i32** %bar.addr, align 8
%1 = load i32, i32* %bar
call void (i8, ...) #__print_argument_value(...)
store i32** %baz, i32*** %baz.addr, align 8
%2 = load i32*, i32** %baz
%3 = load i32, i32* %2
call void (i8, ...) #__print_argument_value(...)
This works perfectly as long as the pointers are not null. Is there an easy way I can generate IR to guard the call to __print_argument_value? I did write out an explicit if (bar) { ... } and a if (baz) { ... } and then looked at the generated IR to see how LLVM generates it. I saw that it performs an icmp ne of the pointer (the .addr variable) against null. Then if the result is true, it breaks to a label if.then where it calls the function, or else to the label if.end which skips over it.
The problem is that I'm having a hard time trying to figure out how I can generate IR like that from my LLVM pass. Are there any examples I can look at? I did look up the documentation but I still can't figure out how to put the pieces together using the LLVM API even though I know what the IR should look like. I saw some documentation about to PHI nodes which I don't see in my IR from the explicit null check, but I am not entirely clear on what they are and how they would help me.
PS: Please excuse any weirdness in the C/C++ code; I usually program in Java.

what will happen if I pass a not pointer type to store instruction?

When using the store instruction in LLVM IR code, I found this in the document:
There are two arguments to the store instruction: a value to store and
an address at which to store it. The type of the operand
must be a pointer to the first class type of the operand.
But the truth is that if I pass a llvm value which type is not a pointer to the functionbuilder.CreateStore(constInt0, v1); (v1 is a 32bits integer type, not a pointer type), the builder didn't assert error, but generate the IR code:
store i32 0, i32 %1
while normally code for storage is more like:
store i32 0, i32* %1
But if I pass a not pointer type value to builder.CreateLoad(v1);, the builder will assert an error:
typename cast_retty<X, Y *>::ret_type llvm::cast(Y *) [X = llvm::PointerType, Y = llvm::Type]: Assertion `isa<X>(Val) && "cast<Ty>() argument of incompatible type!"' failed.
So why builder allowed me to pass a non-pointer value? And what will happen if the store i32 0, i32 %1 runs?

LLVM: Updating a Data structure at runtime

I want to store data about each stack memory that is being allocated and changed.
To do so, I thought I'd need a data structure of some sort in which I can store the related data, such as the value of the local variable, and any change of it, e.g. before a store instruction.
Moreover, I should be able to check the value with the one saved in the data structure before any read of the local variable (e.g. load instruction).
If we assume that the IR looks like this
%1 = alloca i32,
[...]
%20 = store i32 0, %1
I need to change the above code to look like the below
%18 = call __Checking
%19 = <storing the return value from __Checking into a data structure>
%20 = store i32 0, %1
My problem is I cannot figure out how to store the returned value from the library called __Checking into a StructType I defined at the beginning.
My relevant code
if (StoreInst *SI = dyn_cast<StoreInst>(&I)){
Value* newValue = IRB.CreateCall(....);
Type* strTy[] = { Type::getInt64Ty(m.getContext()),
Type::getInt64Ty(m.getContext()) };
Type* t = StructType::create(strTy);
}
You didn't provide any specific information on the type returned by __Checking (or even where does __Checking come from) and the internals of the StructType, so I'm going to suggest to abstract that behaviour away in a runtime library (most likely in C).
This will allow you to setup function calls in those parts of the IR that you require to store (or compare, etc.) a value. The function(s) would take as arguments the value and the StructType (and anything else required) and perform the desired action in plain C (e.g. store the value).
For example, using part of your posted IR (names prefixed with myruntime_ should be defined in your runtime library):
%0 = alloca %myruntime_struct
[...]
%18 = call #__Checking
%19 = call #myruntime_storevalue(%2, %18, /* other arguments? */)
%20 = store i32 0, %1

Get pointer to llvm::Value previously allocated for CreateLoad function

I'm new to llvm and I'm writing a small llvm IR Builder.
I use the IRBuilder and all these Create* functions to generate my IR.
What I'm trying to do is to create a load instruction which create a new SSA local variable with value of a previously allocated llvm::Value.
What I expected to have :
%2 = load i32* %1
With %2 results of load instruction and %1 my previously allocated Value (CreateAlloca)
Here is what I tried :
// Get Ptr from Val
Value* ptr = ConstantExpr::getIntToPtr((Constant*)loc[n],PointerType::getUnqual(builder->getInt32Ty()));
// Générate load instruction with the new Ptr
builder->CreateLoad(ptr);
And here is what I have :
%2 = load i32* null
loc is an array which contains all my llvm::Value*
Can you please tell me what I'm doing wrong ? Or maybe if I'm on a bad way ?
Thanks.
ConstantExpr::getIntToPtr() creates a constant expression. So in effect, what you're trying to generate is equivalent to this IR:
%2 = load i32* inttoptr (i32 %1 to i32*)
But this is illegal since a constant expression, as hinted by its name, only supports constants, and %1 isn't a constant. ConstantExpr::getIntToPtr() requires a Constant as a first argument to verify it, but you passed it a non-constant value which was forcefully cast to a constant.
The correct way to convert a non-constant integer to a pointer is with IRBuilder::createIntToPtr. However, since you say the previous value (loc[n]) was created via an alloca then it's already a pointer, and you don't need to perform any conversion: just do builder->CreateLoad(loc[n]).
By the way, the proper way to cast a Value to a Constant in LLVM is not via a c-style cast but via cast<>, like so: cast<Constant>(loc[n]).