【问题标题】:Insert a load and a store instruction before the first instruction of the first basicblock of a function在函数的第一个基本块的第一条指令之前插入加载和存储指令
【发布时间】:2016-08-14 05:47:40
【问题描述】:

我想在函数的第一个基本块的第一条指令之前插入一个加载和存储指令(用于模拟我们工作的性能开销)。 LLVM pass 的写法如下:

    Value *One = llvm::ConstantInt::get(Type::getInt32Ty(Context),1); 
    for(Function::iterator bb = tmp->begin(); bb != tmp->end(); ++bb        {                                                      
    //for every instruction of the block
    for (BasicBlock::iterator inst = bb->begin(); inst != bb->end(); ++inst){
       if(inst == bb->begin() && bb == tmp->begin()){
           BasicBlock* bp = &*bb;
           Instruction* pinst = &*inst;
           AllocaInst *pa = new AllocaInst(Int32Ty, "buf", pinst);
           StoreInst* newstore = new StoreInst(One, pa, pinst);
           LoadInst* newload = new LoadInst(pa, "loadvalue", pinst);
       }
     }
   }

插入的加载和存储指令可以在xx.ll文件中看到:

    define i32 @fun() #0 {
    entry:
      %buf = alloca i32
      %loadvalue = load i32, i32* %buf
      store i32 %loadvalue, i32* %buf         
      %call = tail call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
      ret i32 1
   }

但是,插入的指令在目标可执行文件中消失了。

我该如何解决这个问题?

【问题讨论】:

    标签: llvm


    【解决方案1】:

    它们可能已被优化消除,因为它们没有任何可见的效果。尝试将加载和存储标记为 volatile。

    一般而言,您的算法将不起作用,因为 LLVM 期望函​​数中的所有分配都是第一条指令。您应该扫描第一个基本块并找到第一个非 alloca 指令并在那里插入新代码,确保首先添加 allocas(就像您所做的那样)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-13
      • 2017-12-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多