Checks.cpp

Go to the documentation of this file.
00001 //===-- Checks.cpp --------------------------------------------------------===//
00002 //
00003 //                     The KLEE Symbolic Virtual Machine
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 
00010 #include "Passes.h"
00011 
00012 #include "llvm/Constants.h"
00013 #include "llvm/DerivedTypes.h"
00014 #include "llvm/Function.h"
00015 #include "llvm/InstrTypes.h"
00016 #include "llvm/Instruction.h"
00017 #include "llvm/Instructions.h"
00018 #include "llvm/IntrinsicInst.h"
00019 #include "llvm/Module.h"
00020 #include "llvm/Pass.h"
00021 #include "llvm/Type.h"
00022 #include "llvm/Transforms/Scalar.h"
00023 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
00024 #include "llvm/Target/TargetData.h"
00025 
00026 using namespace llvm;
00027 using namespace klee;
00028 
00029 char DivCheckPass::ID;
00030 
00031 bool DivCheckPass::runOnModule(Module &M) { 
00032   Function *divZeroCheckFunction = 0;
00033 
00034   bool moduleChanged = false;
00035   
00036   for (Module::iterator f = M.begin(), fe = M.end(); f != fe; ++f) {
00037     for (Function::iterator b = f->begin(), be = f->end(); b != be; ++b) {
00038       for (BasicBlock::iterator i = b->begin(), ie = b->end(); i != ie; ++i) {     
00039           if (BinaryOperator* binOp = dyn_cast<BinaryOperator>(i)) {
00040           // find all [s|u][div|mod] instructions
00041           Instruction::BinaryOps opcode = binOp->getOpcode();
00042           if (opcode == Instruction::SDiv || opcode == Instruction::UDiv ||
00043               opcode == Instruction::SRem || opcode == Instruction::URem) {
00044             
00045             CastInst *denominator =
00046               CastInst::CreateIntegerCast(i->getOperand(1),
00047                                           (Type*)Type::Int64Ty,
00048                                           false,  /* sign doesn't matter */
00049                                           "int_cast_to_i64",
00050                                           i);
00051             
00052             // Lazily bind the function to avoid always importing it.
00053             if (!divZeroCheckFunction) {
00054               Constant *fc = M.getOrInsertFunction("klee_div_zero_check", 
00055                                                    Type::VoidTy, 
00056                                                    Type::Int64Ty, NULL);
00057               divZeroCheckFunction = cast<Function>(fc);
00058             }
00059 
00060             CallInst::Create(divZeroCheckFunction, denominator, "", &*i);
00061             moduleChanged = true;
00062           }
00063         }
00064       }
00065     }
00066   }
00067   return moduleChanged;
00068 }

Generated on Fri Jun 5 03:31:32 2009 for klee by  doxygen 1.5.8