jeudi 28 janvier 2016

Write a pass to replace all llvm instructions

I'm trying to write an llvm pass to replace all BinaryOperator instructions by a multiplication, the problem is that only the 1st instruction is replaced:

virtual bool runOnFunction(Function &F) {
  for (auto &B : F) {
     for (BasicBlock::iterator DI = B.begin(); DI != B.end(); ) {
      Instruction *Inst = DI++;

      if (auto *op = dyn_cast<BinaryOperator>(&*Inst)) {
        // Insert at the point where the instruction `op` appears.
        IRBuilder<> builder(op);

        // Make a multiply with the same operands as `op`.
        Value *lhs = op->getOperand(0);
        Value *rhs = op->getOperand(1);
        Value *mul = builder.CreateMul(lhs, rhs);

        // Everywhere the old instruction was used as an operand, use our
        // new multiply instruction instead.
        for (auto &U : op->uses()) {
          User *user = U.getUser();  // A User is anything with operands.
          user->setOperand(U.getOperandNo(), mul);
        }


        // We modified the code.
        return true;
      }


    }
  }

  return false;
}

Aucun commentaire:

Enregistrer un commentaire