1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
| bool BogusControlFlowPass::doF(Module &M, Function &F) { Twine *varX = new Twine("x"); Twine *varY = new Twine("y"); Value *x1 = ConstantInt::get(Type::getInt32Ty(M.getContext()), 0, false); Value *y1 = ConstantInt::get(Type::getInt32Ty(M.getContext()), 0, false);
GlobalVariable *x = new GlobalVariable(M, Type::getInt32Ty(M.getContext()), false, GlobalValue::CommonLinkage, (Constant *)x1, *varX); GlobalVariable *y = new GlobalVariable(M, Type::getInt32Ty(M.getContext()), false, GlobalValue::CommonLinkage, (Constant *)y1, *varY);
std::vector<Instruction *> toEdit, toDelete; for (Function::iterator fi = F.begin(), fe = F.end(); fi != fe; ++fi) { Instruction *tbb = fi->getTerminator(); if (tbb->getOpcode() == Instruction::Br) { BranchInst *br = (BranchInst *)(tbb); if (br->isConditional()) { FCmpInst *cond = (FCmpInst *)br->getCondition(); unsigned opcode = cond->getOpcode(); if (opcode == Instruction::FCmp) { if (cond->getPredicate() == FCmpInst::FCMP_TRUE) { toDelete.push_back(cond); toEdit.push_back(tbb); } } } } }
for (std::vector<Instruction *>::iterator i = toEdit.begin(); i != toEdit.end(); ++i) { opX = new LoadInst(Type::getInt32Ty(M.getContext()), (Value *)x, "", (*i)); opY = new LoadInst(Type::getInt32Ty(M.getContext()), (Value *)y, "", (*i));
op = BinaryOperator::Create( Instruction::Sub, (Value *)opX, ConstantInt::get(Type::getInt32Ty(M.getContext()), 1, false), "", (*i)); op1 = BinaryOperator::Create(Instruction::Mul, (Value *)opX, op, "", (*i)); op = BinaryOperator::Create( Instruction::URem, op1, ConstantInt::get(Type::getInt32Ty(M.getContext()), 2, false), "", (*i)); condition = new ICmpInst( (*i), ICmpInst::ICMP_EQ, op, ConstantInt::get(Type::getInt32Ty(M.getContext()), 0, false)); condition2 = new ICmpInst( (*i), ICmpInst::ICMP_SLT, opY, ConstantInt::get(Type::getInt32Ty(M.getContext()), 10, false)); op1 = BinaryOperator::Create(Instruction::Or, (Value *)condition, (Value *)condition2, "", (*i));
BranchInst::Create(((BranchInst *)*i)->getSuccessor(0), ((BranchInst *)*i)->getSuccessor(1), (Value *)op1, ((BranchInst *)*i)->getParent()); (*i)->eraseFromParent(); } for (std::vector<Instruction *>::iterator i = toDelete.begin(); i != toDelete.end(); ++i) { (*i)->eraseFromParent(); }
return true; }
|