[LLVMdev] Pointers in Load and Store

John Criswell criswell at illinois.edu
Thu Jan 20 20:12:24 PST 2011


On 1/20/2011 10:02 PM, Surinder wrote:
> When I compile C programs into llvm, it produces load instructions in
> two different flavours.
>
> (1)    %8 = load i8** %bp, align 8
>
> (2)    %1 = load i8* getelementptr inbounds ([4 x i8]* @.str, i64 0,
> i64 0), align 1
>
> I know that %bp in first case and the entire "getelementptr inbounds
> ([4 x i8]* @.str, i64 0, i64 0)" in second case can be obtained by
> dump'ing I.getOperand(0)
>
> However, I want to find out which of the two forms of load have been
> produced because in the second case, I want to insert checks for array
> bounds.
>
> How can I find out when I am in Instruction object I and I.getOpcode()
> == 29 whether I am dealing with type (1) or type (2) above.

The second load instruction is not really a "special" load instruction.  
Rather, its pointer argument is an LLVM constant expression (class 
llvm::ConstExpr).  The Getelementptr (GEP), which would normally be a 
GEP instruction, is instead a constant expression that will be converted 
into a constant numeric value at code generation time.

So, what you need to do is to examine the LoadInst's operand and see if 
its a ConstExpr, and then see whether the ConstExpr's opcode is a GEP 
opcode.

However, there's an easier way to handle this.  SAFECode 
(http://safecode.cs.illinois.edu) has an LLVM pass which converts 
constant expression GEPs into GEP instructions.  If you run it on your 
code first, you'll get the following instruction sequence:

%tmp = getelementptr inbounds ([4 x i8]* @.str, i64 0,i64 0), align 1
%1 = load i8* %tmp

You then merely search for GEP instructions and put run-time checks on 
those (which you have to do anyway if you're adding array bounds 
checking).  The only ConstantExpr GEPs that aren't converted, I think, 
are those in global variable initializers.

Now, regarding the insertion of array bounds checks, SAFECode does that, 
too (it is a memory safety compiler for C code).  It also provides a 
simple static array bounds checker and some array bounds check 
optimization passes.

I can direct you to the relevant portions of the source code if you're 
interested.

-- John T.

> Thanks.
>
> Surinder Kumar Jain
> _______________________________________________
> LLVM Developers mailing list
> LLVMdev at cs.uiuc.edu         http://llvm.cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev




More information about the llvm-dev mailing list