about summary refs log tree commit diff homepage
path: root/lib/Module/IntrinsicCleaner.cpp
diff options
context:
space:
mode:
authorJulian Büning <julian.buening@rwth-aachen.de>2018-05-21 22:12:18 -0700
committerCristian Cadar <c.cadar@imperial.ac.uk>2018-05-22 16:24:46 +0100
commit4e0ec744a8170f3d82aa1a8658fd523442781da2 (patch)
treeab8103ed9729d8c3a290ca652d6b35682ceb2064 /lib/Module/IntrinsicCleaner.cpp
parent45dbf0624f6fe37d25ba401c2bf99b8315f8847b (diff)
downloadklee-4e0ec744a8170f3d82aa1a8658fd523442781da2.tar.gz
clang-format on lib/Module/{IntrinsicCleaner.cpp,Passes.h}
Diffstat (limited to 'lib/Module/IntrinsicCleaner.cpp')
-rw-r--r--lib/Module/IntrinsicCleaner.cpp93
1 files changed, 51 insertions, 42 deletions
diff --git a/lib/Module/IntrinsicCleaner.cpp b/lib/Module/IntrinsicCleaner.cpp
index 4e3c3796..2ac9db62 100644
--- a/lib/Module/IntrinsicCleaner.cpp
+++ b/lib/Module/IntrinsicCleaner.cpp
@@ -13,13 +13,13 @@
 #include "llvm/IR/Constants.h"
 #include "llvm/IR/DerivedTypes.h"
 #include "llvm/IR/Function.h"
+#include "llvm/IR/IRBuilder.h"
 #include "llvm/IR/InstrTypes.h"
 #include "llvm/IR/Instruction.h"
 #include "llvm/IR/Instructions.h"
 #include "llvm/IR/IntrinsicInst.h"
 #include "llvm/IR/Module.h"
 #include "llvm/IR/Type.h"
-#include "llvm/IR/IRBuilder.h"
 #include "llvm/Pass.h"
 #include "llvm/Transforms/Scalar.h"
 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
@@ -46,18 +46,18 @@ bool IntrinsicCleanerPass::runOnModule(Module &M) {
 bool IntrinsicCleanerPass::runOnBasicBlock(BasicBlock &b, Module &M) {
   bool dirty = false;
   LLVMContext &ctx = M.getContext();
-  
+
   unsigned WordSize = DataLayout.getPointerSizeInBits() / 8;
   for (BasicBlock::iterator i = b.begin(), ie = b.end(); i != ie;) {
     IntrinsicInst *ii = dyn_cast<IntrinsicInst>(&*i);
     // increment now since deletion of instructions makes iterator invalid.
     ++i;
-    if(ii) {
+    if (ii) {
       switch (ii->getIntrinsicID()) {
       case Intrinsic::vastart:
       case Intrinsic::vaend:
         break;
-        
+
         // Lower vacopy so that object resolution etc is handled by
         // normal instructions.
         //
@@ -68,24 +68,32 @@ bool IntrinsicCleanerPass::runOnBasicBlock(BasicBlock &b, Module &M) {
         Value *src = ii->getArgOperand(1);
 
         if (WordSize == 4) {
-          Type *i8pp = PointerType::getUnqual(PointerType::getUnqual(Type::getInt8Ty(ctx)));
-          Value *castedDst = CastInst::CreatePointerCast(dst, i8pp, "vacopy.cast.dst", ii);
-          Value *castedSrc = CastInst::CreatePointerCast(src, i8pp, "vacopy.cast.src", ii);
+          Type *i8pp = PointerType::getUnqual(
+              PointerType::getUnqual(Type::getInt8Ty(ctx)));
+          Value *castedDst =
+              CastInst::CreatePointerCast(dst, i8pp, "vacopy.cast.dst", ii);
+          Value *castedSrc =
+              CastInst::CreatePointerCast(src, i8pp, "vacopy.cast.src", ii);
           Value *load = new LoadInst(castedSrc, "vacopy.read", ii);
           new StoreInst(load, castedDst, false, ii);
         } else {
           assert(WordSize == 8 && "Invalid word size!");
           Type *i64p = PointerType::getUnqual(Type::getInt64Ty(ctx));
-          Value *pDst = CastInst::CreatePointerCast(dst, i64p, "vacopy.cast.dst", ii);
-          Value *pSrc = CastInst::CreatePointerCast(src, i64p, "vacopy.cast.src", ii);
-          Value *val = new LoadInst(pSrc, std::string(), ii); new StoreInst(val, pDst, ii);
+          Value *pDst =
+              CastInst::CreatePointerCast(dst, i64p, "vacopy.cast.dst", ii);
+          Value *pSrc =
+              CastInst::CreatePointerCast(src, i64p, "vacopy.cast.src", ii);
+          Value *val = new LoadInst(pSrc, std::string(), ii);
+          new StoreInst(val, pDst, ii);
           Value *off = ConstantInt::get(Type::getInt64Ty(ctx), 1);
           pDst = GetElementPtrInst::Create(pDst, off, std::string(), ii);
           pSrc = GetElementPtrInst::Create(pSrc, off, std::string(), ii);
-          val = new LoadInst(pSrc, std::string(), ii); new StoreInst(val, pDst, ii);
+          val = new LoadInst(pSrc, std::string(), ii);
+          new StoreInst(val, pDst, ii);
           pDst = GetElementPtrInst::Create(pDst, off, std::string(), ii);
           pSrc = GetElementPtrInst::Create(pSrc, off, std::string(), ii);
-          val = new LoadInst(pSrc, std::string(), ii); new StoreInst(val, pDst, ii);
+          val = new LoadInst(pSrc, std::string(), ii);
+          new StoreInst(val, pDst, ii);
         }
         ii->eraseFromParent();
         dirty = true;
@@ -102,32 +110,32 @@ bool IntrinsicCleanerPass::runOnBasicBlock(BasicBlock &b, Module &M) {
 
         Value *op1 = ii->getArgOperand(0);
         Value *op2 = ii->getArgOperand(1);
-        
+
         Value *result = 0;
         Value *result_ext = 0;
         Value *overflow = 0;
 
         unsigned int bw = op1->getType()->getPrimitiveSizeInBits();
-        unsigned int bw2 = op1->getType()->getPrimitiveSizeInBits()*2;
+        unsigned int bw2 = op1->getType()->getPrimitiveSizeInBits() * 2;
 
         if ((ii->getIntrinsicID() == Intrinsic::uadd_with_overflow) ||
             (ii->getIntrinsicID() == Intrinsic::usub_with_overflow) ||
             (ii->getIntrinsicID() == Intrinsic::umul_with_overflow)) {
 
           Value *op1ext =
-            builder.CreateZExt(op1, IntegerType::get(M.getContext(), bw2));
+              builder.CreateZExt(op1, IntegerType::get(M.getContext(), bw2));
           Value *op2ext =
-            builder.CreateZExt(op2, IntegerType::get(M.getContext(), bw2));
+              builder.CreateZExt(op2, IntegerType::get(M.getContext(), bw2));
           Value *int_max_s =
-            ConstantInt::get(op1->getType(), APInt::getMaxValue(bw));
-          Value *int_max =
-            builder.CreateZExt(int_max_s, IntegerType::get(M.getContext(), bw2));
+              ConstantInt::get(op1->getType(), APInt::getMaxValue(bw));
+          Value *int_max = builder.CreateZExt(
+              int_max_s, IntegerType::get(M.getContext(), bw2));
 
-          if (ii->getIntrinsicID() == Intrinsic::uadd_with_overflow){
+          if (ii->getIntrinsicID() == Intrinsic::uadd_with_overflow) {
             result_ext = builder.CreateAdd(op1ext, op2ext);
-          } else if (ii->getIntrinsicID() == Intrinsic::usub_with_overflow){
+          } else if (ii->getIntrinsicID() == Intrinsic::usub_with_overflow) {
             result_ext = builder.CreateSub(op1ext, op2ext);
-          } else if (ii->getIntrinsicID() == Intrinsic::umul_with_overflow){
+          } else if (ii->getIntrinsicID() == Intrinsic::umul_with_overflow) {
             result_ext = builder.CreateMul(op1ext, op2ext);
           }
           overflow = builder.CreateICmpUGT(result_ext, int_max);
@@ -137,27 +145,28 @@ bool IntrinsicCleanerPass::runOnBasicBlock(BasicBlock &b, Module &M) {
                    (ii->getIntrinsicID() == Intrinsic::smul_with_overflow)) {
 
           Value *op1ext =
-            builder.CreateSExt(op1, IntegerType::get(M.getContext(), bw2));
+              builder.CreateSExt(op1, IntegerType::get(M.getContext(), bw2));
           Value *op2ext =
-            builder.CreateSExt(op2, IntegerType::get(M.getContext(), bw2));
+              builder.CreateSExt(op2, IntegerType::get(M.getContext(), bw2));
           Value *int_max_s =
-            ConstantInt::get(op1->getType(), APInt::getSignedMaxValue(bw));
+              ConstantInt::get(op1->getType(), APInt::getSignedMaxValue(bw));
           Value *int_min_s =
-            ConstantInt::get(op1->getType(), APInt::getSignedMinValue(bw));
-          Value *int_max =
-            builder.CreateSExt(int_max_s, IntegerType::get(M.getContext(), bw2));
-          Value *int_min =
-            builder.CreateSExt(int_min_s, IntegerType::get(M.getContext(), bw2));
+              ConstantInt::get(op1->getType(), APInt::getSignedMinValue(bw));
+          Value *int_max = builder.CreateSExt(
+              int_max_s, IntegerType::get(M.getContext(), bw2));
+          Value *int_min = builder.CreateSExt(
+              int_min_s, IntegerType::get(M.getContext(), bw2));
 
-          if (ii->getIntrinsicID() == Intrinsic::sadd_with_overflow){
+          if (ii->getIntrinsicID() == Intrinsic::sadd_with_overflow) {
             result_ext = builder.CreateAdd(op1ext, op2ext);
-          } else if (ii->getIntrinsicID() == Intrinsic::ssub_with_overflow){
+          } else if (ii->getIntrinsicID() == Intrinsic::ssub_with_overflow) {
             result_ext = builder.CreateSub(op1ext, op2ext);
-          } else if (ii->getIntrinsicID() == Intrinsic::smul_with_overflow){
+          } else if (ii->getIntrinsicID() == Intrinsic::smul_with_overflow) {
             result_ext = builder.CreateMul(op1ext, op2ext);
           }
-          overflow = builder.CreateOr(builder.CreateICmpSGT(result_ext, int_max),
-                                      builder.CreateICmpSLT(result_ext, int_min));
+          overflow =
+              builder.CreateOr(builder.CreateICmpSGT(result_ext, int_max),
+                               builder.CreateICmpSLT(result_ext, int_min));
         }
 
         // This trunc cound be replaced by a more general trunc replacement
@@ -169,10 +178,10 @@ bool IntrinsicCleanerPass::runOnBasicBlock(BasicBlock &b, Module &M) {
         //     uint8_t = (uint8_t + uint8_t) & 0xFF;
         // before this, must check if it has side effects on other operations
         result = builder.CreateTrunc(result_ext, op1->getType());
-        Value *resultStruct =
-          builder.CreateInsertValue(UndefValue::get(ii->getType()), result, 0);
+        Value *resultStruct = builder.CreateInsertValue(
+            UndefValue::get(ii->getType()), result, 0);
         resultStruct = builder.CreateInsertValue(resultStruct, overflow, 1);
-        
+
         ii->replaceAllUsesWith(resultStruct);
         ii->eraseFromParent();
         dirty = true;
@@ -193,8 +202,7 @@ bool IntrinsicCleanerPass::runOnBasicBlock(BasicBlock &b, Module &M) {
         // Intrisic instruction "llvm.trap" found. Directly lower it to
         // a call of the abort() function.
         Function *F = cast<Function>(
-          M.getOrInsertFunction(
-            "abort", Type::getVoidTy(ctx), NULL));
+            M.getOrInsertFunction("abort", Type::getVoidTy(ctx), NULL));
         F->setDoesNotReturn();
         F->setDoesNotThrow();
 
@@ -214,7 +222,8 @@ bool IntrinsicCleanerPass::runOnBasicBlock(BasicBlock &b, Module &M) {
         assert(minArg && "Failed to get second argument");
         ConstantInt *minArgAsInt = dyn_cast<ConstantInt>(minArg);
         assert(minArgAsInt && "Second arg is not a ConstantInt");
-        assert(minArgAsInt->getBitWidth() == 1 && "Second argument is not an i1");
+        assert(minArgAsInt->getBitWidth() == 1 &&
+               "Second argument is not an i1");
         Value *replacement = NULL;
         IntegerType *intType = dyn_cast<IntegerType>(ii->getType());
         assert(intType && "intrinsic does not have integer return type");
@@ -240,4 +249,4 @@ bool IntrinsicCleanerPass::runOnBasicBlock(BasicBlock &b, Module &M) {
 
   return dirty;
 }
-}
+} // namespace klee