9

I'm trying to detect array declarations and build a symbol value table for static sized arrays. It will contain a name-arraySize pairing. I have several questions:

  1. Given an instruction such as %a = alloca [200 x i8], align 16
    how can I extract a, the name of the array from it?

  2. I'm trying to extract the 200 as the array size but this code:

    if(AllocaInst *allocInst = dyn_cast<AllocaInst>(&*I)){
         PointerType *p = allocInst->getType();
         if(p->getElementType()->isArrayTy()){
              Value* v = allocInst->getOperand(0);
              errs() << *v ;
         }
    } 
    

    yields me i32 1 when I print v.
    Does anyone know why this is?
    I didn't think there was anything 32bit about this except maybe the address.

5
  • Okay so I figured out how to get size: ArrayType *a = cast<ArrayType>(p->getElementType()); a->getNumElements() Commented Mar 22, 2012 at 18:06
  • 3
    okay...so... I figured out how to get the name too... allocInst->getName() Why is that simply posting on stackoverflow helps me find answers faster... :P Commented Mar 22, 2012 at 18:12
  • 7
    It's best if you just post an answer containing this information, instead of comments. This way the question will be properly marked as answered. Commented Oct 18, 2012 at 13:11
  • Realn0wherman, I agree with @Oak, please post these comments as the answer and accept them, because people usually scroll down to look for that lovely green check before investing time reading the comments. Commented Jan 23, 2014 at 12:20
  • What about multi-dimensional arrays. If i take arr[100][100], then a->getNumElements() is giving 100. How to get total size?. Commented Nov 7, 2014 at 13:06

1 Answer 1

3

Some of the answers are in the comments, but here is a more full explanation.

There are two sources of size in an alloca: the size of the allocated type and the number of elements of that type which are allocated. If you don't specify a number explicitly, you get the implicit default of allocating a single element. This is the i32 1 value you get out of operand #0. If the allocated type is an array type (use dyn_cast<...> to test for this, as cast<...> will assert), then you also need to account for that size.

In LLVM, the optimizer canonicalizes alloca instructions with a static size greater than one into an alloca instruction of a single array with that size. So you most often see the alloca size as a constant one.

There are more friendly APIs for this as well: http://llvm.org/docs/doxygen/html/classllvm_1_1AllocaInst.html

In particular, AllocaInst::getArraySize() will get you the number of elements (usually 1) and AllocaInst::getAllocatedType() will get you the type of the allocated element (sometimes an array).

Finally, a note about using the name: LLVM doesn't make any guarantees about the names of instructions. Various parts of the optimizer will destroy the names or change them. Just be careful using them for production code as you may be surprised when they go away.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.