Alexander Poddey
2015-May-27 03:56 UTC
[LLVMdev] convert GetElemtPtr result to pointer on element?
Hi Tim, I forgot to say that I try to do all this in the LLVM API, not IR. I read in a bc'ed program and try to edit it using the API. Alex Tim Northover wrote:>> But I can not succeed in using the getGetElementPtr result in >> constructing an initalizer for another global value (which expects a >> ConstantFP* and not a ConstantExpr*). > > The result of a Constant GEP is going to be the address of a > floating-point value rather than the value itself, so you can't use it > to initialise something that expects a float. For example: > > @arr = constant [2 x float] [float 0.0, float 1.0] > @bad = global float getelementptr([2 x float], [2 x float]* @arr, > i32 0, i32 1) > @good = global float* getelementptr([2 x float], [2 x float]* > @arr, i32 0, i32 1) > > LLVM doesn't really do value aliases, so you'd usually just write > "@bad = global float 1.0" for the second line. You might extract that > 1.0 value by looking into the @arr definition directly using the LLVM > API -- if you can't do that (say because it's not a compile-time > constant) then the chances are what you're trying to write wouldn't be > valid anyway. > > Cheers. > > Tim.
Tim Northover
2015-May-27 14:26 UTC
[LLVMdev] convert GetElemtPtr result to pointer on element?
On 26 May 2015 at 20:56, Alexander Poddey <alexander.poddey at gmx.net> wrote:> I forgot to say that I try to do all this in the LLVM API, not IR. > I read in a bc'ed program and try to edit it using the API.Yep, I already assumed you were. But the first step to deciding what API calls to use is writing down the LLVM IR you want to produce. If that can't be compiled then you haven't got a hope of writing a program to do it. In this case, the GEP gives you a float*. So you either use GlobalVariable::getInitializer and hunt through that Constant for the one you want (probably valid if the GlobalVariable isConstant()) or use a GEP and a load to give you a pointer to that value and load it. Cheers. Tim.
Alexander Poddey
2015-May-27 18:41 UTC
[LLVMdev] convert GetElemtPtr result to pointer on element?
Finally, I solved it using the index information used for the getGetElementPtr call, and descending the initializer (GlobalVariable::getInitializer) index by index using getOperand and dyn_cast<llvm::Constant> for structs and getAggregateElement for arrays. (I guess getAggregateElemnt should work for struct just the same)... I thought it would be possible to get this using the index vector and one call (like getGetElementPtr)... Alex Tim Northover wrote:> On 26 May 2015 at 20:56, Alexander Poddey <alexander.poddey at gmx.net> > wrote: >> I forgot to say that I try to do all this in the LLVM API, not IR. >> I read in a bc'ed program and try to edit it using the API. > > Yep, I already assumed you were. But the first step to deciding what > API calls to use is writing down the LLVM IR you want to produce. If > that can't be compiled then you haven't got a hope of writing a > program to do it. > > In this case, the GEP gives you a float*. So you either use > GlobalVariable::getInitializer and hunt through that Constant for the > one you want (probably valid if the GlobalVariable isConstant()) or > use a GEP and a load to give you a pointer to that value and load it. > > Cheers. > > Tim.