Example #1
0
    bool visitICmpInst(ICmpInst& inst) {
        // errs() << "got icmp instruction!  " << inst << '\n';

        bool changed = false;
        if (inst.getPredicate() == CmpInst::ICMP_EQ) {
            assert(inst.getNumOperands() == 2);

            if (inst.getOperand(1) == processing) {
                inst.swapOperands();
                changed = true;
                any_changes = true;
            }
            assert(dyn_cast<Instruction>(inst.getOperand(0)) == processing);
            Value* other = inst.getOperand(1);
            if (isa<ConstantPointerNull>(other)) {
                if (VERBOSITY("opt") >= 2) {
                    errs() << inst << '\n';
                    errs() << "replacing with false!\n";
                }

                Value* new_value = ConstantInt::getFalse(other->getContext());
                inst.replaceAllUsesWith(new_value);
                inst.eraseFromParent();
                changed = true;
                any_changes = true;
            }
        }
        return changed;
    }
Example #2
0
void RAFlowFunction::visitBranchInst(BranchInst &BI){
  RALatticePoint* inRLP = new RALatticePoint(*(info_in_casted.back()));
  info_in_casted.pop_back();
  
  if (BI.isUnconditional()) {
    for (std::map<Value *, LatticePoint *>::iterator it=out_map.begin(); it != out_map.end(); ++it){
      Value* elm = it->first;
      out_map[elm] = new RALatticePoint(*inRLP);
    }
  }
  else{
    Value* cond = BI.getCondition();
    if (isa<ICmpInst>(cond)) {
      // may affect elements of our lattice.
      std::pair<Use*, Use *> branches = helper::getBranches(BI);
      Use* true_branch = branches.first;
      Use* false_branch = branches.second;
      /*
      errs() << "Examining instruction" << BI << "\n";
      errs() << "True branch is " << * (true_branch->get()) << "\n";
      errs() << "False branch is " << * (false_branch->get()) << "\n";
      */
      
      ICmpInst* cmp = cast<ICmpInst>(cond);
      std::pair<Use*, Use *> operands = helper::getOperands(*cmp);
      Use* right_hand_side = operands.second;
      Use*  left_hand_side = operands.first;
      
      /*
      errs() << "Comparison " << *cmp << "\n";
      errs() << "Left hand side is " << * (left_hand_side->get()) << "\n";
      errs() << "Right hand side is " << * (right_hand_side->get()) << "\n";
      */
      ConstantRange* lhs_range;
      ConstantRange* rhs_range;
      
      
      if (inRLP->representation.count(left_hand_side->get()) > 0) {
        lhs_range = inRLP->representation[left_hand_side->get()];
      }
      else if (isa<ConstantInt>(left_hand_side->get())) {
        ConstantInt* C2 = cast<ConstantInt>(left_hand_side->get());
        lhs_range = new ConstantRange(C2->getValue());
      }
      else{
        lhs_range = new ConstantRange(32, false);
      }
      
      if (inRLP->representation.count(right_hand_side->get()) > 0) {
        rhs_range = inRLP->representation[right_hand_side->get()];
      }
      else if (isa<ConstantInt>(right_hand_side->get())) {
        ConstantInt* C2 = cast<ConstantInt>(right_hand_side->get());
        rhs_range = new ConstantRange(C2->getValue());
      }
      else{
        rhs_range = new ConstantRange(32, false);
      }
      /*
      errs() << "Left hand side has range ";
      lhs_range->print(errs());
      errs() << "\nRight hand side has range ";
      rhs_range->print(errs());
      errs() << " \n ";
      */
      // First we compute the restrictions that cmp makes upon the regions.
      
      //errs() << " Compare looks like " << *cmp << "\n";

      int predicate = 0;
      
      if (cmp->isSigned()) {
        predicate = cmp->getSignedPredicate();
      }
      else{
        predicate = cmp->getUnsignedPredicate();
      }
      
      ConstantRange true_branch_lhs_restriction = ConstantRange::makeICmpRegion(predicate, *rhs_range);
      
      /*
      errs() << "True branch lhs_restriction: ";
      true_branch_lhs_restriction.print(errs());
      errs() << "\n";
      */
      
      ConstantRange false_branch_lhs_restriction = (ConstantRange(32, true)).difference(true_branch_lhs_restriction);
      
      /*
      errs() << "False branch lhs_restriction: ";
      false_branch_lhs_restriction.print(errs());
      errs() << "\n";
      */
      
      cmp->swapOperands();
      
      //errs() << " After swapping, compare looks like " << *cmp << "\n";

      
      ConstantRange true_branch_rhs_restriction = ConstantRange::makeICmpRegion(predicate,*lhs_range);
      
      /*
      errs() << "\nTrue branch rhs_restriction: ";
      true_branch_rhs_restriction.print(errs());
      errs() << " is it wrapped range? " << true_branch_rhs_restriction.isSignWrappedSet() << " is it empty? " << true_branch_rhs_restriction.isEmptySet() << " what is its size? " << true_branch_rhs_restriction.getSetSize() << "\n";
      */
      
      ConstantRange false_branch_rhs_restriction = (ConstantRange(32, true)).difference(true_branch_rhs_restriction);
      
      /*
      errs() << "False branch rhs_restriction: ";
      false_branch_rhs_restriction.print(errs());
      errs() << "\n";
      
      
      */
      
      cmp->swapOperands();
      
      // Next we intersect the ranges with the resulting restrictions.
      ConstantRange* true_branch_lhs_range = new ConstantRange(lhs_range->getBitWidth(), true);
      ConstantRange* false_branch_lhs_range = new ConstantRange(lhs_range->getBitWidth(), true);
      ConstantRange* true_branch_rhs_range = new ConstantRange(rhs_range->getBitWidth(), true);
      ConstantRange* false_branch_rhs_range = new ConstantRange(rhs_range->getBitWidth(), true);
      
      *true_branch_lhs_range = lhs_range->intersectWith(true_branch_lhs_restriction);
      *false_branch_lhs_range = lhs_range->intersectWith(false_branch_lhs_restriction);
      
      *true_branch_rhs_range = rhs_range->intersectWith(true_branch_rhs_restriction);
      *false_branch_rhs_range = rhs_range->intersectWith(false_branch_rhs_restriction);
      
      /*
      errs() << "True branch lhs range ";
      true_branch_lhs_range->print(errs());
      errs() << "\n";
      
      
      errs() << "False branch lhs range ";
      false_branch_lhs_range->print(errs());
      errs() << "\n";
      
      
      errs() << "True branch rhs range ";
      true_branch_rhs_range->print(errs());
      errs() << "\n";
      
      
      errs() << "False branch rhs range ";
      false_branch_rhs_range->print(errs());
      errs() << "\n";
      */
      
      RALatticePoint* true_branchRLP = new RALatticePoint(*inRLP);
      RALatticePoint* false_branchRLP = new RALatticePoint(*inRLP);
      
      
      true_branchRLP->isBottom = false;
      true_branchRLP->isTop = false;

      false_branchRLP->isBottom = false;
      false_branchRLP->isTop = false;

      if (inRLP->representation.count(left_hand_side->get()) > 0){
        //errs() << "\nIn if statement for lhs \n";

        
        true_branchRLP->representation[left_hand_side->get()] = true_branch_lhs_range;
        false_branchRLP->representation[left_hand_side->get()] = false_branch_lhs_range;
        /*
        errs() << "True branch lhs range ";
        true_branch_lhs_range->print(errs());
        errs() << "\n";
        
        
        errs() << "False branch lhs range ";
        false_branch_lhs_range->print(errs());
        errs() << "\n";
         */
      }
      if (inRLP->representation.count(right_hand_side->get()) > 0){
        //errs() << "\nIn if statement for rhs \n";
        
        true_branchRLP->representation[right_hand_side->get()] = true_branch_rhs_range;
        false_branchRLP->representation[right_hand_side->get()] = false_branch_rhs_range;
        /*
        errs() << "True branch rhs range ";
        true_branch_rhs_range->print(errs());
        errs() << "\n";
        
        
        errs() << "False branch rhs range ";
        false_branch_rhs_range->print(errs());
        errs() << "\n";
        */
      }
      /*
       info_out.push_back(true_branchRLP);
       info_out.push_back(false_branchRLP);
      */
      
      out_map[true_branch->get()] = true_branchRLP;
      /*
      errs() << "\nTrue branch lattice point is ";
      true_branchRLP->printToErrs();
      */
      out_map[false_branch->get()] = false_branchRLP;
      /*
      errs() << "\nFalse branch lattice point is ";
      false_branchRLP->printToErrs();
      */
    }
    else{
      // does not affect our lattice.
      for (std::map<Value *, LatticePoint *>::iterator it=out_map.begin(); it != out_map.end(); ++it){
        Value* elm = it->first;
        out_map[elm] = new RALatticePoint(*inRLP);
      }
    }
  }
}