affine_relation.cpp 6.14 KB
Newer Older
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#include "affine_relation.h"
#include "llvm/IR/CFG.h"

using namespace llvm;
using namespace std;

namespace pcpo {

// MARK: - Initializers

AffineRelation::AffineRelation(Function const& f) {
    numberOfVariables = 999; // FIXME: figure this out.
    matrix = Matrix<int>(numberOfVariables);
    for (Argument const& arg: f.args()) {
        index[&arg] = ++lastIndex;
    }
    isBottom = f.arg_empty();
}

AffineRelation::AffineRelation(Function const* callee_func, AffineRelation const& state, CallInst const* call) {
    // TODO
}

// MARK: - AbstractState Interface

void AffineRelation::applyPHINode(BasicBlock const& bb, vector<AffineRelation> pred_values, Instruction const& phi) {
    // TODO
}

void AffineRelation::applyCallInst(Instruction const& inst, BasicBlock const* end_block, AffineRelation const& callee_state) {
    // TODO
}

void AffineRelation::applyReturnInst(Instruction const& inst) {
    // TODO
}

void AffineRelation::applyDefault(Instruction const& inst) {
//    std::vector<LinearEquality> operands;

    if (inst.getNumOperands() != 2) return nonDeterminsticAssignment(&inst);

    // We only deal with integer types
    IntegerType const* type = dyn_cast<IntegerType>(inst.getType());
    if (not type) return nonDeterminsticAssignment(&inst);

    type = dyn_cast<IntegerType>(inst.getOperand(0)->getType());
    if (not type) return nonDeterminsticAssignment(&inst);

    type = dyn_cast<IntegerType>(inst.getOperand(1)->getType());
    if (not type) return nonDeterminsticAssignment(&inst);

//    for (llvm::Value const* value: inst.operand_values()) {
//        operands.push_back(getAbstractValue(*value));
//    }

    switch (inst.getOpcode()) {
        case Instruction::Add:
            return Add(inst);
        case Instruction::Sub:
            return Sub(inst);
        case Instruction::Mul:
            return Mul(inst);
        case Instruction::SDiv:
        case Instruction::UDiv:
        default:
            return nonDeterminsticAssignment(&inst);
    }

//    debug_output(inst, operands);
}

//Matrix<int> AffineRelation::getAbstractValue(Value const& value) const {
//    if (llvm::Constant const* c = llvm::dyn_cast<llvm::Constant>(&value)) {
//        return Matrix(1,1,c->getSExtValue);
//    } else if (values.count(&value)) {
//        return values.at(&value);
//    } else if (isBottom) {
//        // If we are at bottom, there are no values
//        return Matrix();
//    } else {
//        // This branch should only catch 'weird' values, like things we are not handling
//        return AbstractDomain {true};
//    }
//}

bool AffineRelation::merge(Merge_op::Type op, AffineRelation const& other) {
    // TODO
}

// MARK: - Lattice Operations

bool AffineRelation::leastUpperBound(AffineRelation rhs) {
    // TODO
}

// MARK: - Assignments

void AffineRelation::affineAssignment(Value const* xi, int64_t a, Value const* xj, int64_t b) {
    Matrix<int> affineTransformer = Matrix<int>(numberOfVariables);
    affineTransformer(0,index[xi]) = b;
    affineTransformer(index[xi],index[xi]) = 0;
    if (xj != nullptr) {
        affineTransformer(index[xj],index[xi]) = a;
    }
    matrix *= affineTransformer;
}

void AffineRelation::nonDeterminsticAssignment(Value const* xi) {
    // TODO
}

// MARK: - Abstract Operators

void AffineRelation::Add(Instruction const& inst) {
    auto op1 = inst.getOperand(0);
    auto op2 = inst.getOperand(1);

    // [xi := bj + bk]
    if (isa<ConstantInt>(op1) && (isa<ConstantInt>(op2))) {
        auto b1 = dyn_cast<ConstantInt>(op1);
        auto b2 = dyn_cast<ConstantInt>(op2);
        return affineAssignment(&inst, 1, nullptr, b1->getSExtValue() + b2->getSExtValue() );
        // [xi := b + xj]
    }  else if (isa<ConstantInt>(op1) && isa<Value>(op2)) {
        auto b = dyn_cast<ConstantInt>(op1);
        return affineAssignment(&inst, 1, op2, b->getSExtValue());
        // [xi := xj + b]
    } else if (isa<ConstantInt>(op2) && isa<Value>(op1)) {
        auto b = dyn_cast<ConstantInt>(op2);
        return affineAssignment(&inst, 1, op1, b->getSExtValue());
    } else {
        assert(false);
        return nonDeterminsticAssignment(&inst);
    }
}

void AffineRelation::Sub(Instruction const& inst) {
    auto op1 = inst.getOperand(0);
    auto op2 = inst.getOperand(1);

    // [xi := bj - bk]
    if (isa<ConstantInt>(op1) && (isa<ConstantInt>(op2))) {
        auto b1 = dyn_cast<ConstantInt>(op1);
        auto b2 = dyn_cast<ConstantInt>(op2);
        return affineAssignment(&inst, 1, nullptr, b1->getSExtValue() - b2->getSExtValue() );
    // [xi := b - xj]
    } else if (isa<ConstantInt>(op1) && isa<Value>(op2)) {
        auto b = dyn_cast<ConstantInt>(op1);
        return affineAssignment(&inst, 1, op2, -b->getSExtValue());
    // [xi := xj - b]
    } else if (isa<ConstantInt>(op2) && isa<Value>(op1)) {
        auto b = dyn_cast<ConstantInt>(op2);
        return affineAssignment(&inst, 1, op1, -b->getSExtValue());
    } else {
        assert(false);
        return nonDeterminsticAssignment(&inst);
    }
}

void AffineRelation::Mul(Instruction const& inst) {
    auto op1 = inst.getOperand(0);
    auto op2 = inst.getOperand(1);

    // [xi := aj * ak]
    if (isa<ConstantInt>(op1) && (isa<ConstantInt>(op2))) {
        auto b1 = dyn_cast<ConstantInt>(op1);
        auto b2 = dyn_cast<ConstantInt>(op2);
        return affineAssignment(&inst, 1, nullptr, b1->getSExtValue() * b2->getSExtValue() );
    // [xi := a * xj]
    } else if (isa<ConstantInt>(op1) && isa<Value>(op2)) {
        auto a = dyn_cast<ConstantInt>(op1);
        return affineAssignment(&inst, a->getSExtValue(), op2, 0);
    // [xi := xj * a]
    } else if (isa<ConstantInt>(op2) && isa<Value>(op1)) {
        auto a = dyn_cast<ConstantInt>(op2);
        return affineAssignment(&inst, a->getSExtValue(), op1, 0);
    } else {
        assert(false);
        return nonDeterminsticAssignment(&inst);
    }
}

// MARK: - debug output

void AffineRelation::printIncoming(BasicBlock const& bb, raw_ostream& out, int indentation) const {
    // TODO
}

void AffineRelation::printOutgoing(BasicBlock const& bb, raw_ostream& out, int indentation) const {
    // TODO
}

void AffineRelation::debug_output(Instruction const& inst, Matrix<int> operands) {
    // TODO
}

}