Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "llvm/Pass.h"
- #include "llvm/IR/Function.h"
- #include "llvm/IR/BasicBlock.h"
- #include "llvm/IR/Instructions.h"
- #include "llvm/IR/IRBuilder.h"
- #include "llvm/Support/raw_ostream.h"
- #include "llvm/Transforms/Utils/BasicBlockUtils.h"
- using namespace llvm;
- namespace {
- struct Hello : public FunctionPass {
- static char ID;
- Hello() : FunctionPass(ID) {}
- bool runOnFunction(Function &F) override;
- };
- }
- bool Hello::runOnFunction(Function &F) {
- bool changed = false;
- for (auto BI = F.begin(), BE = F.end(); BI != BE; ++BI) {
- for (auto II = BI->begin(), IE = BI->end(); II != IE; ++II) {
- BinaryOperator *BO = dyn_cast<BinaryOperator>(&*II);
- if (!BO)
- continue;
- unsigned opcode = BO->getOpcode();
- errs() << BO->getOpcodeName() << "\n";
- if (opcode != Instruction::Add) {
- continue;
- }
- changed = true;
- IRBuilder<> builder(BO);
- Value* V = builder.CreateAdd(builder.CreateXor(BO->getOperand(0), BO->getOperand(1)),
- builder.CreateMul(ConstantInt::get(BO->getType(), 2),
- builder.CreateAnd(BO->getOperand(0), BO->getOperand(1))));
- ReplaceInstWithValue(BI->getInstList(), II, V);
- }
- }
- return changed;
- }
- char Hello::ID = 0;
- static RegisterPass<Hello> X("hello", "Count opcodes per functions", false, false);
Add Comment
Please, Sign In to add comment