about summary refs log tree commit diff homepage
path: root/lib/Module/RaiseAsm.cpp
blob: b62338a6c73a53c7742848baa631afc7965e683c (plain) (blame)
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
//===-- RaiseAsm.cpp ------------------------------------------------------===//
//
//                     The KLEE Symbolic Virtual Machine
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#include "Passes.h"

#include "llvm/InlineAsm.h"

using namespace llvm;
using namespace klee;

char RaiseAsmPass::ID = 0;

Function *RaiseAsmPass::getIntrinsic(llvm::Module &M,
                                     unsigned IID,
                                     const Type **Tys,
                                     unsigned NumTys) {  
  return Intrinsic::getDeclaration(&M, (llvm::Intrinsic::ID) IID, Tys, NumTys);
}

// FIXME: This should just be implemented as a patch to
// X86TargetAsmInfo.cpp, then everyone will benefit.
bool RaiseAsmPass::runOnInstruction(Module &M, Instruction *I) {
  if (CallInst *ci = dyn_cast<CallInst>(I)) {
    if (InlineAsm *ia = dyn_cast<InlineAsm>(ci->getCalledValue())) {
      const std::string &as = ia->getAsmString();
      const std::string &cs = ia->getConstraintString();
      const llvm::Type *T = ci->getType();

      // bswaps
      if (ci->getNumOperands() == 2 && 
          T == ci->getOperand(1)->getType() &&
          ((T == llvm::Type::getInt16Ty(getGlobalContext()) && 
            as == "rorw $$8, ${0:w}" &&
            cs == "=r,0,~{dirflag},~{fpsr},~{flags},~{cc}") ||
           (T == llvm::Type::getInt32Ty(getGlobalContext()) &&
            as == "rorw $$8, ${0:w};rorl $$16, $0;rorw $$8, ${0:w}" &&
            cs == "=r,0,~{dirflag},~{fpsr},~{flags},~{cc}"))) {
        llvm::Value *Arg0 = ci->getOperand(1);
        Function *F = getIntrinsic(M, Intrinsic::bswap, Arg0->getType());
        ci->setOperand(0, F);
        return true;
      }
    }
  }

  return false;
}

bool RaiseAsmPass::runOnModule(Module &M) {
  bool changed = false;
  
  for (Module::iterator fi = M.begin(), fe = M.end(); fi != fe; ++fi) {
    for (Function::iterator bi = fi->begin(), be = fi->end(); bi != be; ++bi) {
      for (BasicBlock::iterator ii = bi->begin(), ie = bi->end(); ii != ie;) {
        Instruction *i = ii;
        ++ii;  
        changed |= runOnInstruction(M, i);
      }
    }
  }

  return changed;
}