[LLVMdev] How to get the string value?

1,155 views
Skip to first unread message

Welson Sun

unread,
Jan 26, 2012, 9:08:20 PM1/26/12
to llv...@cs.uiuc.edu
Hi, if I have some LLVM code like this:

@.str = private unnamed_addr constant [7 x i8] c"in_arr\00", align 1
@.str1 = private unnamed_addr constant [8 x i8] c"in_arr2\00", align 1
@.str2 = private unnamed_addr constant [8 x i8] c"out_arr\00", align 1
...
...

  call void (...)* @_Z16fooz(i8* getelementptr inbounds ([7 x i8]* @.str, i64 0, i64 0), i32 0, i32 1024)
...

I would like to get the string value of the bold argument, but how? I know it's a llvm::Value pointer, but it is not a llvm::GetElementPtrInst?


Thanks,






Duncan Sands

unread,
Jan 27, 2012, 12:04:37 AM1/27/12
to llv...@cs.uiuc.edu
Hi Welson Sun,

> Hi, if I have some LLVM code like this:
>
> @.str = private unnamed_addr constant [7 x i8] c"in_arr\00", align 1
> @.str1 = private unnamed_addr constant [8 x i8] c"in_arr2\00", align 1
> @.str2 = private unnamed_addr constant [8 x i8] c"out_arr\00", align 1
> ...
> ...
>

> call void (...)* @_Z16fooz(i8* *getelementptr inbounds ([7 x i8]* @.str, i64
> 0, i64 0)*, i32 0, i32 1024)


> ...
>
> I would like to get the string value of the bold argument, but how? I know it's
> a llvm::Value pointer, but it is not a llvm::GetElementPtrInst?

it is a ConstantExpr getelementptr. Whenever you see something that looks like
an instruction but is printed inline inside another instruction then that means
it is actually a constant, a ConstantExpr.

Ciao, Duncan.

>
>
> Thanks,
>
>
>
>
>
>
>
>
> _______________________________________________
> LLVM Developers mailing list
> LLV...@cs.uiuc.edu http://llvm.cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev

_______________________________________________
LLVM Developers mailing list
LLV...@cs.uiuc.edu http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev

Welson Sun

unread,
Jan 27, 2012, 12:27:49 AM1/27/12
to Duncan Sands, llv...@cs.uiuc.edu
Thanks Duncan,

Yes, it is a ConstantExpr! Thank you!

Now trying to find a clue in ConstantExpr's functions to get that string :-)

Regards,
Welson 
--
Welson



Welson Sun

unread,
Jan 27, 2012, 1:19:42 AM1/27/12
to Duncan Sands, llv...@cs.uiuc.edu
OK, I guess I need further help. Out of these member functions:
  DECLARE_TRANSPARENT_OPERAND_ACCESSORS (Constant)
  Transparently provide more efficient getOperand methods. 
bool  isCast () const
  Return true if this is a convert constant expression. 
bool  isCompare () const
  Return true if this is a compare constant expression. 
bool  hasIndices () const
  Return true if this is an insertvalue or extractvalue expression, and the getIndices() method may be used. 
bool  isGEPWithNoNotionalOverIndexing () const
  Return true if this is a getelementptr expression and all the index operands are compile-time known integers within the corresponding notional static array extents. Note that this is not equivalant to, a subset of, or a superset of the "inbounds" property. 
unsigned  getOpcode () const
  getOpcode - Return the opcode at the root of this constant expression 
unsigned  getPredicate () const
ArrayRef<unsigned >  getIndices () const
const char *  getOpcodeName () const
  getOpcodeName - Return a string representation for an opcode. 
Constant *  getWithOperandReplaced (unsigned OpNo, Constant *Op) const
Constant *  getWithOperands (ArrayRefConstant * > Ops) const
Constant *  getWithOperands (ArrayRefConstant * > Ops, Type *Ty) const
virtual void  destroyConstant ()
virtual void  replaceUsesOfWithOnConstant (Value *From, Value *To, Use *U)

Only  isGEPWithNoNotionalOverIndexing ()  returns true. But how are you supposed to use others to get the constant value?

Duncan Sands

unread,
Jan 28, 2012, 9:41:23 AM1/28/12
to Welson Sun, llv...@cs.uiuc.edu
Hi Welson,

> Yes, it is a ConstantExpr! Thank you!
>
> Now trying to find a clue in ConstantExpr's functions to get that string :-)

you can get the pointer operand by doing: getOperand(0)
The i'th index is getOperand(i+1).

Ciao, Duncan.

> > LLV...@cs.uiuc.edu <mailto:LLV...@cs.uiuc.edu> http://llvm.cs.uiuc.edu


> > http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
>
> _______________________________________________
> LLVM Developers mailing list

> LLV...@cs.uiuc.edu <mailto:LLV...@cs.uiuc.edu> http://llvm.cs.uiuc.edu

> Email: welso...@gmail.com <mailto:welso...@gmail.com>

Welson Sun

unread,
Jan 28, 2012, 11:29:37 AM1/28/12
to Duncan Sands, llv...@cs.uiuc.edu
Hey Duncan,

Thanks! I figured out this piece of code finally:

            Value *gep = call->getArgOperand(0);
            if ( ConstantExpr *pCE = dyn_cast<ConstantExpr>(gep) ) {
              Value *firstop = pCE->getOperand(0);
              if (GlobalVariable *GV = dyn_cast<GlobalVariable>(firstop)){
                Constant *v = GV->getInitializer();
                if (ConstantArray *CA = dyn_cast<ConstantArray>(v)) {
                  StringRef portName(CA->getAsString());

That's some learning experience :-)

Regards,
Welson

Duncan Sands

unread,
Jan 29, 2012, 7:52:22 AM1/29/12
to Welson Sun, llv...@cs.uiuc.edu
Hi Welson,

> Thanks! I figured out this piece of code finally:
>
> Value *gep = call->getArgOperand(0);
> if ( ConstantExpr *pCE = dyn_cast<ConstantExpr>(gep) ) {
> Value *firstop = pCE->getOperand(0);
> if (GlobalVariable *GV = dyn_cast<GlobalVariable>(firstop)){
> Constant *v = GV->getInitializer();
> if (ConstantArray *CA = dyn_cast<ConstantArray>(v)) {
> StringRef portName(CA->getAsString());

you can use stripPointerCasts to get rid of the GEP and similar pointer casts.

Ciao, Duncan.

>
> That's some learning experience :-)
>
> Regards,
> Welson
>
>
>
> On Sat, Jan 28, 2012 at 6:41 AM, Duncan Sands <bald...@free.fr
> <mailto:bald...@free.fr>> wrote:
>
> Hi Welson,
>
>
> Yes, it is a ConstantExpr! Thank you!
>
> Now trying to find a clue in ConstantExpr's functions to get that string :-)
>
>
> you can get the pointer operand by doing: getOperand(0)
> The i'th index is getOperand(i+1).
>
> Ciao, Duncan.
>
>
> Regards,
> Welson
>
> On Thu, Jan 26, 2012 at 9:04 PM, Duncan Sands <bald...@free.fr
> <mailto:bald...@free.fr>

> <mailto:LLV...@cs.uiuc.edu <mailto:LLV...@cs.uiuc.edu>>


> http://llvm.cs.uiuc.edu
>
> > http://lists.cs.uiuc.edu/ mailman/listinfo/llvmdev

> <http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev>
>
> ______________________________ _________________


> LLVM Developers mailing list
> LLV...@cs.uiuc.edu <mailto:LLV...@cs.uiuc.edu>

> <mailto:LLV...@cs.uiuc.edu <mailto:LLV...@cs.uiuc.edu>>

> <http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev>
>
>
>
>
> --
> Welson
>
> Phone: (408) 418-8385 <tel:%28408%29%20418-8385>
> Email: welso...@gmail.com <mailto:welso...@gmail.com>
> <mailto:welso...@gmail.com <mailto:welso...@gmail.com>>


>
>
>
>
>
>
> --
> Welson
>
> Phone: (408) 418-8385

> Email: welso...@gmail.com <mailto:welso...@gmail.com>
>
>

_______________________________________________
LLVM Developers mailing list

LLV...@cs.uiuc.edu http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev

Reply all
Reply to author
Forward
0 new messages