about summary refs log tree commit diff homepage
path: root/lib/Module
diff options
context:
space:
mode:
authorMartin Nowack <m.nowack@imperial.ac.uk>2018-07-29 17:12:17 +0100
committerCristian Cadar <c.cadar@imperial.ac.uk>2018-08-03 13:57:37 +0100
commit4e4744750e623f07e8a8dfa4156ba7333e787647 (patch)
treeae45fd1c46370c5ee93d17ac6cf06dd64753cc55 /lib/Module
parentb0fe76437c5bdc3270604b9b8a70dbe067f45c64 (diff)
downloadklee-4e4744750e623f07e8a8dfa4156ba7333e787647.tar.gz
Replace remaining *Inst::Create() calls with llvm::Builder
Replace the remaining occurrences of `Inst::Create()` with
`llvm::Builder` to manage metadata automatically and to fold
instructions.

C++11 it and clang-format
Diffstat (limited to 'lib/Module')
-rw-r--r--lib/Module/IntrinsicCleaner.cpp57
-rw-r--r--lib/Module/KModule.cpp61
-rw-r--r--lib/Module/LowerSwitch.cpp17
3 files changed, 75 insertions, 60 deletions
diff --git a/lib/Module/IntrinsicCleaner.cpp b/lib/Module/IntrinsicCleaner.cpp
index 0f7eb223..c48952c2 100644
--- a/lib/Module/IntrinsicCleaner.cpp
+++ b/lib/Module/IntrinsicCleaner.cpp
@@ -64,40 +64,40 @@ bool IntrinsicCleanerPass::runOnBasicBlock(BasicBlock &b, Module &M) {
         // FIXME: This is much more target dependent than just the word size,
         // however this works for x86-32 and x86-64.
       case Intrinsic::vacopy: { // (dst, src) -> *((i8**) dst) = *((i8**) src)
+        llvm::IRBuilder<> Builder(ii);
         Value *dst = ii->getArgOperand(0);
         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);
-          Value *load = new LoadInst(castedSrc, "vacopy.read", ii);
-          new StoreInst(load, castedDst, false, ii);
+          auto castedDst =
+              Builder.CreatePointerCast(dst, i8pp, "vacopy.cast.dst");
+          auto castedSrc =
+              Builder.CreatePointerCast(src, i8pp, "vacopy.cast.src");
+          auto load = Builder.CreateLoad(castedSrc, "vacopy.read");
+          Builder.CreateStore(load, castedDst, false /* isVolatile */);
         } 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 *off = ConstantInt::get(Type::getInt64Ty(ctx), 1);
-          pDst = GetElementPtrInst::Create(KLEE_LLVM_GEP_TYPE(nullptr)
-              pDst, off, std::string(), ii);
-          pSrc = GetElementPtrInst::Create(KLEE_LLVM_GEP_TYPE(nullptr)
-              pSrc, off, std::string(), ii);
-          val = new LoadInst(pSrc, std::string(), ii);
-          new StoreInst(val, pDst, ii);
-          pDst = GetElementPtrInst::Create(KLEE_LLVM_GEP_TYPE(nullptr)
-              pDst, off, std::string(), ii);
-          pSrc = GetElementPtrInst::Create(KLEE_LLVM_GEP_TYPE(nullptr)
-              pSrc, off, std::string(), ii);
-          val = new LoadInst(pSrc, std::string(), ii);
-          new StoreInst(val, pDst, ii);
+          auto pDst = Builder.CreatePointerCast(dst, i64p, "vacopy.cast.dst");
+          auto pSrc = Builder.CreatePointerCast(src, i64p, "vacopy.cast.src");
+          auto val = Builder.CreateLoad(pSrc, std::string());
+          Builder.CreateStore(val, pDst, ii);
+
+          auto off = ConstantInt::get(Type::getInt64Ty(ctx), 1);
+          pDst = Builder.CreateGEP(KLEE_LLVM_GEP_TYPE(nullptr) pDst, off,
+                                   std::string());
+          pSrc = Builder.CreateGEP(KLEE_LLVM_GEP_TYPE(nullptr) pSrc, off,
+                                   std::string());
+          val = Builder.CreateLoad(pSrc, std::string());
+          Builder.CreateStore(val, pDst);
+          pDst = Builder.CreateGEP(KLEE_LLVM_GEP_TYPE(nullptr) pDst, off,
+                                   std::string());
+          pSrc = Builder.CreateGEP(KLEE_LLVM_GEP_TYPE(nullptr) pSrc, off,
+                                   std::string());
+          val = Builder.CreateLoad(pSrc, std::string());
+          Builder.CreateStore(val, pDst);
         }
         ii->eraseFromParent();
         dirty = true;
@@ -207,15 +207,16 @@ bool IntrinsicCleanerPass::runOnBasicBlock(BasicBlock &b, Module &M) {
       }
 
       case Intrinsic::trap: {
-        // Intrisic instruction "llvm.trap" found. Directly lower it to
+        // Intrinsic 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));
         F->setDoesNotReturn();
         F->setDoesNotThrow();
 
-        CallInst::Create(F, Twine(), ii);
-        new UnreachableInst(ctx, ii);
+        llvm::IRBuilder<> Builder(ii);
+        Builder.CreateCall(F);
+        Builder.CreateUnreachable();
 
         ii->eraseFromParent();
 
diff --git a/lib/Module/KModule.cpp b/lib/Module/KModule.cpp
index cbcd395c..74d91a8f 100644
--- a/lib/Module/KModule.cpp
+++ b/lib/Module/KModule.cpp
@@ -22,11 +22,12 @@
 #include "klee/Internal/Support/ModuleUtil.h"
 
 #include "llvm/Bitcode/ReaderWriter.h"
+#include "llvm/IR/DataLayout.h"
+#include "llvm/IR/IRBuilder.h"
 #include "llvm/IR/Instructions.h"
 #include "llvm/IR/LLVMContext.h"
 #include "llvm/IR/Module.h"
 #include "llvm/IR/ValueSymbolTable.h"
-#include "llvm/IR/DataLayout.h"
 
 #if LLVM_VERSION_CODE < LLVM_VERSION(3, 5)
 #include "llvm/Analysis/Verifier.h"
@@ -105,14 +106,15 @@ static Function *getStubFunctionForCtorList(Module *m,
 				  name,
                               m);
   BasicBlock *bb = BasicBlock::Create(m->getContext(), "entry", fn);
-  
+  llvm::IRBuilder<> Builder(bb);
+
   // From lli:
   // Should be an array of '{ int, void ()* }' structs.  The first value is
   // the init priority, which we ignore.
-  ConstantArray *arr = dyn_cast<ConstantArray>(gv->getInitializer());
+  auto arr = dyn_cast<ConstantArray>(gv->getInitializer());
   if (arr) {
     for (unsigned i=0; i<arr->getNumOperands(); i++) {
-      ConstantStruct *cs = cast<ConstantStruct>(arr->getOperand(i));
+      auto cs = cast<ConstantStruct>(arr->getOperand(i));
 #if LLVM_VERSION_CODE >= LLVM_VERSION(3, 5)
       // There is a third *optional* element in global_ctor elements (``i8
       // @data``).
@@ -121,21 +123,21 @@ static Function *getStubFunctionForCtorList(Module *m,
 #else
       assert(cs->getNumOperands()==2 && "unexpected element in ctor initializer list");
 #endif
-      Constant *fp = cs->getOperand(1);      
+      auto fp = cs->getOperand(1);
       if (!fp->isNullValue()) {
-        if (llvm::ConstantExpr *ce = dyn_cast<llvm::ConstantExpr>(fp))
+        if (auto ce = dyn_cast<llvm::ConstantExpr>(fp))
           fp = ce->getOperand(0);
 
-        if (Function *f = dyn_cast<Function>(fp)) {
-	  CallInst::Create(f, "", bb);
+        if (auto f = dyn_cast<Function>(fp)) {
+          Builder.CreateCall(f);
         } else {
           assert(0 && "unable to get function pointer from ctor initializer list");
         }
       }
     }
   }
-  
-  ReturnInst::Create(m->getContext(), bb);
+
+  Builder.CreateRetVoid();
 
   return fn;
 }
@@ -143,21 +145,30 @@ static Function *getStubFunctionForCtorList(Module *m,
 static void injectStaticConstructorsAndDestructors(Module *m) {
   GlobalVariable *ctors = m->getNamedGlobal("llvm.global_ctors");
   GlobalVariable *dtors = m->getNamedGlobal("llvm.global_dtors");
-  
-  if (ctors || dtors) {
-    Function *mainFn = m->getFunction("main");
-    if (!mainFn)
-      klee_error("Could not find main() function.");
-
-    if (ctors)
-    CallInst::Create(getStubFunctionForCtorList(m, ctors, "klee.ctor_stub"),
-		     "", &*(mainFn->begin()->begin()));
-    if (dtors) {
-      Function *dtorStub = getStubFunctionForCtorList(m, dtors, "klee.dtor_stub");
-      for (Function::iterator it = mainFn->begin(), ie = mainFn->end();
-           it != ie; ++it) {
-        if (isa<ReturnInst>(it->getTerminator()))
-	  CallInst::Create(dtorStub, "", it->getTerminator());
+
+  if (!ctors && !dtors)
+    return;
+
+  Function *mainFn = m->getFunction("main");
+  if (!mainFn)
+    klee_error("Could not find main() function.");
+
+  if (ctors) {
+#if LLVM_VERSION_CODE >= LLVM_VERSION(3, 8)
+    llvm::IRBuilder<> Builder(&*mainFn->begin()->begin());
+#else
+    llvm::IRBuilder<> Builder(mainFn->begin()->begin());
+#endif
+    Builder.CreateCall(getStubFunctionForCtorList(m, ctors, "klee.ctor_stub"));
+  }
+
+  if (dtors) {
+    Function *dtorStub = getStubFunctionForCtorList(m, dtors, "klee.dtor_stub");
+    for (Function::iterator it = mainFn->begin(), ie = mainFn->end(); it != ie;
+         ++it) {
+      if (isa<ReturnInst>(it->getTerminator())) {
+        llvm::IRBuilder<> Builder(it->getTerminator());
+        Builder.CreateCall(dtorStub);
       }
     }
   }
diff --git a/lib/Module/LowerSwitch.cpp b/lib/Module/LowerSwitch.cpp
index 05688521..4d80ddd8 100644
--- a/lib/Module/LowerSwitch.cpp
+++ b/lib/Module/LowerSwitch.cpp
@@ -16,6 +16,7 @@
 
 #include "Passes.h"
 #include "klee/Config/Version.h"
+#include "llvm/IR/IRBuilder.h"
 #include "llvm/IR/LLVMContext.h"
 #include <algorithm>
 
@@ -60,7 +61,8 @@ void LowerSwitchPass::switchConvert(CaseItr begin, CaseItr end,
 {
   BasicBlock *curHead = defaultBlock;
   Function *F = origBlock->getParent();
-  
+  llvm::IRBuilder<> Builder(defaultBlock);
+
   // iterate through all the cases, creating a new BasicBlock for each
   for (CaseItr it = begin; it < end; ++it) {
     BasicBlock *newBlock = BasicBlock::Create(F->getContext(), "NodeBlock");
@@ -70,10 +72,9 @@ void LowerSwitchPass::switchConvert(CaseItr begin, CaseItr end,
     Function::iterator FI = origBlock;
 #endif
     F->getBasicBlockList().insert(++FI, newBlock);
-    
-    ICmpInst *cmpInst = 
-      new ICmpInst(*newBlock, ICmpInst::ICMP_EQ, value, it->value, "case.cmp");
-    BranchInst::Create(it->block, curHead, cmpInst, newBlock);
+    Builder.SetInsertPoint(newBlock);
+    auto cmpValue = Builder.CreateICmpEQ(value, it->value, "case.cmp");
+    Builder.CreateCondBr(cmpValue, it->block, curHead);
 
     // If there were any PHI nodes in this successor, rewrite one entry
     // from origBlock to come from newBlock.
@@ -89,7 +90,8 @@ void LowerSwitchPass::switchConvert(CaseItr begin, CaseItr end,
   }
 
   // Branch to our shiny new if-then stuff...
-  BranchInst::Create(curHead, origBlock);
+  Builder.SetInsertPoint(origBlock);
+  Builder.CreateBr(curHead);
 }
 
 // processSwitchInst - Replace the specified switch instruction with a sequence
@@ -104,13 +106,14 @@ void LowerSwitchPass::processSwitchInst(SwitchInst *SI) {
   // Create a new, empty default block so that the new hierarchy of
   // if-then statements go to this and the PHI nodes are happy.
   BasicBlock* newDefault = BasicBlock::Create(F->getContext(), "newDefault");
+  llvm::IRBuilder<> Builder(newDefault);
 
 #if LLVM_VERSION_CODE >= LLVM_VERSION(3, 8)
   F->getBasicBlockList().insert(defaultBlock->getIterator(), newDefault);
 #else
   F->getBasicBlockList().insert(defaultBlock, newDefault);
 #endif
-  BranchInst::Create(defaultBlock, newDefault);
+  Builder.CreateBr(defaultBlock);
 
   // If there is an entry in any PHI nodes for the default edge, make sure
   // to update them as well.