Chuck Zhao
2011-May-16 14:35 UTC
[LLVMdev] dyn_cast<Instruction *> returns NULL where it should return a valid instruction
I have the following prototype for a function: void bkp_memory(char *, int); Inside my LLVM IR, I have a callsite looks like the following: tail call void @bkp_memory(i8* bitcast (i32** @P to i8*), i32 4) nounwind When I try to obtain its 1st argument and check whether it is a valid instruction, using: Instruction *Inst = dyn_cast<Instruction *>(I->getOperand(0)); it gives me a NULL Ptr, which is quite a surprise to me. Am I doing anything wrong here? Note that in many other cases, the bkp_memory() callsite won't have the embedded form. They look like: store i32* %21, i32** @P, align 4 %22 = bitcast i32* %21 to i8* tail call void @bkp_memory(i8* %22, i32 4) nounwind And the dyn_cast<> conversion is fine in these cases. Would that be the problem? Thank you Chuck -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20110516/2f1e1f0e/attachment.html>
John Criswell
2011-May-16 14:50 UTC
[LLVMdev] dyn_cast<Instruction *> returns NULL where it should return a valid instruction
On 5/16/11 9:35 AM, Chuck Zhao wrote:> I have the following prototype for a function: > void bkp_memory(char *, int); > > Inside my LLVM IR, I have a callsite looks like the following: > tail call void @bkp_memory(i8* bitcast (i32** @P to i8*), i32 4) nounwind > > > When I try to obtain its 1st argument and check whether it is a valid > instruction, using: > Instruction *Inst = dyn_cast<Instruction *>(I->getOperand(0)); > > it gives me a NULL Ptr, which is quite a surprise to me. > > Am I doing anything wrong here?The bitcast you see above is not an instruction. Rather, it is a constant expression and is represented by a ConstExpr object in the C++ API. That is most likely because @P is a constant (a function pointer, global value, or something similar). So, 1) Use the stripPointerCasts() method of llvm::Value to strip away all pointer casts (whether they are cast instructions or ConstExpr casts): I->getOperand(0)->stripPointerCasts() 2) Realize that not all operands are instructions. In this case, the operand is probably a global variable. Chances are good that: dyn_cast<Instruction>(I->getOperand(0)->stripPointerCasts()) ... will return NULL because the operand is a constant and not an instruction.> > > Note that in many other cases, the bkp_memory() callsite won't have > the embedded form. They look like: > store i32* %21, i32** @P, align 4 > %22 = bitcast i32* %21 to i8* > tail call void @bkp_memory(i8* %22, i32 4) nounwind > > And the dyn_cast<> conversion is fine in these cases. > Would that be the problem?The "embedded" form is a constant expression. The "unembedded" form is an instruction. LLVM has several constant expressions that are modeled after instructions; the difference is that a constant expression's operands are all constants, and therefore the constant expression can also be used like a constant. For more details, please see the LLVM Language Reference manual. -- John T.> > > Thank you > > Chuck > > > _______________________________________________ > LLVM Developers mailing list > LLVMdev at cs.uiuc.edu http://llvm.cs.uiuc.edu > http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20110516/0a29d68d/attachment.html>
Chuck Zhao
2011-May-16 15:12 UTC
[LLVMdev] dyn_cast<Instruction *> returns NULL where it should return a valid instruction
John, Thank you for the quick response, that is precisely the problem. It is a global variable indeed, and the embedded form is not an instruction, but an cast expression :-) Based on your suggestion, I worked out a quick fix for the problem. Thank you very much Chuck On 5/16/2011 10:50 AM, John Criswell wrote:> On 5/16/11 9:35 AM, Chuck Zhao wrote: >> I have the following prototype for a function: >> void bkp_memory(char *, int); >> >> Inside my LLVM IR, I have a callsite looks like the following: >> tail call void @bkp_memory(i8* bitcast (i32** @P to i8*), i32 4) nounwind >> >> >> When I try to obtain its 1st argument and check whether it is a valid >> instruction, using: >> Instruction *Inst = dyn_cast<Instruction *>(I->getOperand(0)); >> >> it gives me a NULL Ptr, which is quite a surprise to me. >> >> Am I doing anything wrong here? > > The bitcast you see above is not an instruction. Rather, it is a > constant expression and is represented by a ConstExpr object in the > C++ API. That is most likely because @P is a constant (a function > pointer, global value, or something similar). > > So, > > 1) Use the stripPointerCasts() method of llvm::Value to strip away all > pointer casts (whether they are cast instructions or ConstExpr casts): > > I->getOperand(0)->stripPointerCasts() > > 2) Realize that not all operands are instructions. In this case, the > operand is probably a global variable. Chances are good that: > > dyn_cast<Instruction>(I->getOperand(0)->stripPointerCasts()) > > ... will return NULL because the operand is a constant and not an > instruction. > > >> >> >> Note that in many other cases, the bkp_memory() callsite won't have >> the embedded form. They look like: >> store i32* %21, i32** @P, align 4 >> %22 = bitcast i32* %21 to i8* >> tail call void @bkp_memory(i8* %22, i32 4) nounwind >> >> And the dyn_cast<> conversion is fine in these cases. >> Would that be the problem? > > The "embedded" form is a constant expression. The "unembedded" form > is an instruction. LLVM has several constant expressions that are > modeled after instructions; the difference is that a constant > expression's operands are all constants, and therefore the constant > expression can also be used like a constant. > > For more details, please see the LLVM Language Reference manual. > > -- John T. > >> >> >> Thank you >> >> Chuck >> >> >> _______________________________________________ >> LLVM Developers mailing list >> LLVMdev at cs.uiuc.edu http://llvm.cs.uiuc.edu >> http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev > > > _______________________________________________ > LLVM Developers mailing list > LLVMdev at cs.uiuc.edu http://llvm.cs.uiuc.edu > http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20110516/e04b56af/attachment.html>
Possibly Parallel Threads
- [LLVMdev] dyn_cast<Instruction *> returns NULL where it should return a valid instruction
- [LLVMdev] identifying all dependent instructions through multi-levels of def-use relationship
- [LLVMdev] identifying all dependent instructions through multi-levels of def-use relationship
- [LLVMdev] <badref> showed up when duplicating a list of dependent instructions
- [LLVMdev] [PATCH] PR2218