summaryrefslogtreecommitdiffstats
path: root/llvm/pass/ReplaceIntrinsic.cpp
blob: 62505f4840fd78b1003628a145e1ca77c51bd256 (plain)
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
 *  (C) 2016 by Computer System Laboratory, IIS, Academia Sinica, Taiwan.
 *      See COPYRIGHT in top-level directory.
 */

#include "llvm-types.h"
#include "llvm-debug.h"
#include "llvm-target.h"
#include "llvm-pass.h"


#define PASS_NAME  "ReplaceIntrinsic"

/*
 * HQEMU does not allow helpers to contain any memory or debug intrinsics.
 * This pass substitutes memory intrinsics to load/store instuctions and
 * removes debug intrinsics (generated by Clang with -g flag).
 */
class ReplaceIntrinsic : public FunctionPass {
    IVec toErase;
public:
    static char ID;
    explicit ReplaceIntrinsic() : FunctionPass(ID) {}

    Value *ConvertType(Value *V, Type *T, Instruction *InsertPos) {
        if (likely(V->getType() == T))
            return V;
        return new BitCastInst(V, T, "", InsertPos);
    }

    bool replaceMemoryIntrinsic(IntrinsicInst *I);
    bool runOnFunction(Function &F);
};

char ReplaceIntrinsic::ID = 0;
INITIALIZE_PASS(ReplaceIntrinsic, "replaceintrinsic",
                "Replace memory and debug intrinsics generated by clang",
                false, false)

FunctionPass *llvm::createReplaceIntrinsic()
{
    return new ReplaceIntrinsic();
}


/*
 *  Transform memcpy/memmove/memset to load/store instruction.
 *  Clang attempts to move memory data using LLVM memory intrinsic instructions.
 *  This causes the statemapping pass to miss some guest states. (Statemapping
 *  only considers guest states accessed by general load/store insts).
 *  So, we simply rewrite the memory intrinsics to load/store instuctions.
 */
bool ReplaceIntrinsic::replaceMemoryIntrinsic(IntrinsicInst *I)
{
    switch (I->getIntrinsicID()) {
    case Intrinsic::memset:
    case Intrinsic::memcpy:
    case Intrinsic::memmove:
        break;
    default:
        return false;
    }

    LLVMContext &Context = I->getContext();
    Type *Int8PtrTy = Type::getInt8PtrTy(Context);
    CallInst *CI = cast<CallInst>(I);

    if (MemTransferInst *MTI = dyn_cast<MemTransferInst>(I)) {
        /* memcpy/memmove */
        Value *Src = MTI->getSource();
        Value *Dst = MTI->getDest();
        Value *NumBytes = MTI->getLength();

        if (CI->getArgOperand(0)->getType() != Int8PtrTy ||
            CI->getArgOperand(1)->getType() != Int8PtrTy ||
            !isa<ConstantInt>(NumBytes) ||
            MTI->isVolatile())
            return false;

        /* Remove this instruction if the access size is zero. */
        size_t Len = cast<ConstantInt>(NumBytes)->getZExtValue();
        if (Len == 0)
            goto done;

        Type *Ty = Type::getIntNPtrTy(Context, Len * 8);
        Src = ConvertType(Src, Ty, I);
        Dst = ConvertType(Dst, Ty, I);
        Src = new LoadInst(Src, "", false, I);
        new StoreInst(Src, Dst, false, I);
    } else if (MemSetInst *MSI = dyn_cast<MemSetInst>(I)) {
        /* memset */
        Value *Src = MSI->getValue();
        Value *Dst = MSI->getDest();
        Value *NumBytes = MSI->getLength();

        if (CI->getArgOperand(0)->getType() != Int8PtrTy ||
            !isa<ConstantInt>(Src) ||
            !isa<ConstantInt>(NumBytes) ||
            MSI->isVolatile())
            return false;

        size_t Val = cast<ConstantInt>(Src)->getZExtValue();
        size_t Len = cast<ConstantInt>(NumBytes)->getZExtValue();
        if (Val != 0)
            return false;
        if (Len == 0)
            goto done;

        Type *Ty = Type::getIntNPtrTy(Context, Len * 8);
        Src = ConstantInt::get(Type::getIntNTy(Context, Len * 8), 0);
        Dst = ConvertType(Dst, Ty, I);
        new StoreInst(Src, Dst, false, I);
    }

done:
    toErase.push_back(I);
    return true;
}

bool ReplaceIntrinsic::runOnFunction(Function &F)
{
    for (auto I = inst_begin(&F), E = inst_end(&F); I != E; ++I) {
        Instruction *Inst = &*I;
        if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(Inst)) {
            if (replaceMemoryIntrinsic(II))
                continue;
            if (isa<DbgInfoIntrinsic>(II))
                toErase.push_back(II);
        }
    }
    ProcessErase(toErase);
    return true;
}

/*
 * vim: ts=8 sts=4 sw=4 expandtab
 */
OpenPOWER on IntegriCloud