Alberto Barbaro via llvm-dev
2019-Mar-09 05:50 UTC
[llvm-dev] Cast a function parameter to GEP
Hi all, I'm still working on the Interpreter class and I would like to understand why an operand cannot be cast to GetElementPtrInst. My code is something like: void MyInterpreter::visitCallInst(CallInst& I) { for(int i = 0; i < I.getNumArgOperands(); i++) { operand = I.getOperand(i); if(GetElementPtrInst* CI dyn_cast<GetElementPtrInst>(operand)) { errs() << "GEP\n"; } else { operand->dump(); } } The specific output is: i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str1, i32 0, i32 0) and str1 is @.str1 = private unnamed_addr constant [7 x i8] c"1.2.34\00", align 1 I think it does not work because str1 is a "private constant". Is there a way to solve it? Thanks -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20190309/a72288a4/attachment.html>
Tim Northover via llvm-dev
2019-Mar-09 06:03 UTC
[llvm-dev] Cast a function parameter to GEP
Hi Alberto, On Sat, 9 Mar 2019 at 05:50, Alberto Barbaro via llvm-dev <llvm-dev at lists.llvm.org> wrote:> i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str1, i32 0, i32 0)Because all of its inputs are Constants, this is actually a GetElementPtrConstantExpr instead of a GetElementPtrInst (the easiest way to tell on sight without context is the extra parens, which the instruction variant never has). The main practical difference between the two is that instances of Constant don't live in a BasicBlock independently, but are referenced directly by each user. When printing the IR this is shown by "inlining" them into the parent Instruction. LLVM has a GEPOperator class to help deal with this issue; it can be used to access the common features of Instructions and Constants. There are similar Operators for other operations because this is quite a common situation.> I think it does not work because str1 is a "private constant". Is there a way to solve it?Broadly true, but the same would apply to any global, not just ones declared constant. Cheers. Tim.
Alberto Barbaro via llvm-dev
2019-Mar-09 06:26 UTC
[llvm-dev] Cast a function parameter to GEP
Thanks Tim, I'll try to solve my problem ASAP, if I cannot maybe I'll some other clarifications. Thanks again On Sat, Mar 9, 2019, 06:03 Tim Northover <t.p.northover at gmail.com> wrote:> Hi Alberto, > > On Sat, 9 Mar 2019 at 05:50, Alberto Barbaro via llvm-dev > <llvm-dev at lists.llvm.org> wrote: > > i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str1, i32 0, i32 0) > > Because all of its inputs are Constants, this is actually a > GetElementPtrConstantExpr instead of a GetElementPtrInst (the easiest > way to tell on sight without context is the extra parens, which the > instruction variant never has). > > The main practical difference between the two is that instances of > Constant don't live in a BasicBlock independently, but are referenced > directly by each user. When printing the IR this is shown by > "inlining" them into the parent Instruction. > > LLVM has a GEPOperator class to help deal with this issue; it can be > used to access the common features of Instructions and Constants. > There are similar Operators for other operations because this is quite > a common situation. > > > I think it does not work because str1 is a "private constant". Is there > a way to solve it? > > Broadly true, but the same would apply to any global, not just ones > declared constant. > > Cheers. > > Tim. >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20190309/024e404f/attachment.html>