-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.cpp
156 lines (133 loc) · 3.98 KB
/
utils.cpp
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
#include "utils.h"
#include <iostream>
//pointerOperand would be the operand for either a store inst or a load inst
std::set<Value*> findCoreOperand(Value* pointerOperand, Value** coreOperand, const Type** coreOperandType)
{
assert(pointerOperand != NULL && "target of store inst obtained incorrectly");
std::set<Value*> indices;
if (isa<GetElementPtrInst>(pointerOperand) || isa<CastInst>(pointerOperand))
{
Value* operand = pointerOperand;
Value* previous;
bool more;
do
{
more = false;
if (GetElementPtrInst* getElementPtrInst = dyn_cast<GetElementPtrInst>(operand))
{
for (User::op_iterator index = getElementPtrInst->idx_begin(); index != getElementPtrInst->idx_end(); ++index)
{
indices.insert(*index);
}
more = true;
previous = operand;
operand = getElementPtrInst->getPointerOperand();
}
else if (CastInst* castInst = dyn_cast<CastInst>(operand))
{
int j = 0;
Value* op = NULL;
for (User::op_iterator i = castInst->op_begin(); i != castInst->op_end(); ++i)
{
op = *i;
++j;
}
assert(j == 1);
more = true;
previous = operand;
operand = op;
}
else if (LoadInst* loadInst = dyn_cast<LoadInst>(operand))
{
previous = operand;
operand = loadInst->getPointerOperand();
more = true;
}
} while (more);
//get - operand
assert(operand != NULL);
const PointerType* pointerType = dyn_cast<PointerType>(operand->getType());
if (pointerType == NULL)
{
*coreOperand = NULL;
}
else
{
*coreOperand = operand;
if (coreOperandType != NULL)
{
*coreOperandType = pointerType->getElementType();
}
}
}
else
{
*coreOperand = pointerOperand;
if (coreOperandType != NULL)
{
*coreOperandType = pointerOperand->getType();
}
switch (pointerOperand->getType()->getTypeID())
{
case Type::IntegerTyID:
// std::cout << "integer\n";
break;
case Type::PointerTyID:
// std::cout << "pointer\n";
break;
default:
std::cout << "unhandled\n";
assert(false);
}
}
return indices;
}
std::pair<int, int> getLineNumber(Loop* loop)
{
int min = INT_MAX;
int max = INT_MIN;
for (LoopBase<BasicBlock, Loop>::block_iterator block = loop->block_begin(); block != loop->block_end(); ++block)
{
for (BasicBlock::iterator instr = (*block)->begin(); instr != (*block)->end(); ++instr)
{
int line = getLineNumber(instr);
if (line != -1)
{
if (min > line)
{
min = line;
}
if (max < line)
{
max = line;
}
}
}
}
return std::pair<int, int>(min, max);
//return getLineNumber(loop->getHeader()->getFirstNonPHI());
}
int getLineNumber(Instruction* inst)
{
if (MDNode* node = inst->getMetadata("dbg"))
{
DILocation loc(node);
return loc.getLineNumber();
}
//assert(false);
return -1;
}
const char* getSourceFile(Loop* loop)
{
return getSourceFile(loop->getHeader()->getFirstNonPHI());
}
const char* getSourceFile(Instruction* inst)
{
if (MDNode* node = inst->getMetadata("dbg"))
{
DILocation loc(node);
return loc.getFilename().str().c_str();
}
//assert(false);
return "";
}