[LLVMdev] problem with replacing an instruction

Daniel Berlin dberlin at dberlin.org
Thu Jun 18 15:24:42 PDT 2015


On Thu, Jun 18, 2015 at 1:40 PM, Frank Winter <fwinter at jlab.org> wrote:
> I am trying to change this
>
> define void @main(float* noalias %arg0, float* noalias %arg1, float* noalias
> %arg2) {
> entrypoint:
>   %0 = bitcast float* %arg1 to <4 x float>*
>
> intothis
>
> define void @main(float* noalias %arg0, float* noalias %arg1, float* noalias
> %arg2) {
> entrypoint:
>   %0 = getelementptr float* %arg1, i64 0
>   %1 = bitcast float* %0 to <4 x float>*
>
>
> I must be close but the final instruction replacement results in a segfault.
>
>
>   for (Instruction& I : BB) {
>     if (isa<BitCastInst>(I)) {
>       BitCastInst* BCI = dyn_cast<BitCastInst>(&I);
>       for (Use& U : I.operands()) {
>           Value* V = U.get();
>           if (!dyn_cast<Instruction>(V)) {
>             // must be a function's argument
>             std::vector<llvm::Value*> vect_1;
>             vect_1.push_back(Builder->getInt64(0));
>             llvm::GetElementPtrInst* gep1 =
> llvm::GetElementPtrInst::Create(V, ArrayRef< Value * >(vect_1) );
>             BB.getInstList().insert(&I, gep1);
>
>             llvm::BitCastInst *bci = new BitCastInst (gep1, BCI->getDestTy()
> );
>             llvm::ReplaceInstWithInst( &I , bci );

There is no need to do this.
Use U.set(gep1);



More information about the llvm-dev mailing list