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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
|
//===-- AutoUpgrade.cpp - Implement auto-upgrade helper functions ---------===//
//
// The LLVM Compiler Infrastructure
//
// This file was developed by Reid Spencer and is distributed under the
// University of Illinois Open Source License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file implements the auto-upgrade helper functions
//
//===----------------------------------------------------------------------===//
#include "llvm/Assembly/AutoUpgrade.h"
#include "llvm/DerivedTypes.h"
#include "llvm/Function.h"
#include "llvm/Module.h"
#include "llvm/Instructions.h"
#include "llvm/Intrinsics.h"
#include "llvm/SymbolTable.h"
#include <iostream>
using namespace llvm;
// Utility function for getting the correct suffix given a type
static inline const char* get_suffix(const Type* Ty) {
switch (Ty->getTypeID()) {
case Type::UIntTyID: return ".i32";
case Type::UShortTyID: return ".i16";
case Type::UByteTyID: return ".i8";
case Type::ULongTyID: return ".i64";
case Type::FloatTyID: return ".f32";
case Type::DoubleTyID: return ".f64";
default: break;
}
return 0;
}
static inline const Type* getTypeFromFunctionName(Function* F) {
// If there's no function, we can't get the argument type.
if (!F)
return 0;
// Get the Function's name.
const std::string& Name = F->getName();
// Quickly eliminate it, if it's not a candidate.
if (Name.length() <= 8 || Name[0] != 'l' || Name[1] != 'l' || Name[2] !=
'v' || Name[3] != 'm' || Name[4] != '.')
return 0;
switch (Name[5]) {
case 'b':
if (Name == "llvm.bswap")
return F->getReturnType();
break;
case 'c':
if (Name == "llvm.ctpop" || Name == "llvm.ctlz" || Name == "llvm.cttz")
return F->getReturnType();
break;
case 'i':
if (Name == "llvm.isunordered") {
Function::const_arg_iterator ArgIt = F->arg_begin();
if (ArgIt != F->arg_end())
return ArgIt->getType();
}
break;
case 's':
if (Name == "llvm.sqrt")
return F->getReturnType();
break;
default:
break;
}
return 0;
}
// This assumes the Function is one of the intrinsics we upgraded.
static inline const Type* getTypeFromFunction(Function *F) {
const Type* Ty = F->getReturnType();
if (Ty->isFloatingPoint())
return Ty;
if (Ty->isSigned())
return Ty->getUnsignedVersion();
if (Ty->isInteger())
return Ty;
if (Ty == Type::BoolTy) {
Function::const_arg_iterator ArgIt = F->arg_begin();
if (ArgIt != F->arg_end())
return ArgIt->getType();
}
return 0;
}
bool llvm::IsUpgradeableIntrinsicName(const std::string& Name) {
// Quickly eliminate it, if it's not a candidate.
if (Name.length() <= 8 || Name[0] != 'l' || Name[1] != 'l' || Name[2] !=
'v' || Name[3] != 'm' || Name[4] != '.')
return false;
switch (Name[5]) {
case 'b':
if (Name == "llvm.bswap")
return true;
break;
case 'c':
if (Name == "llvm.ctpop" || Name == "llvm.ctlz" || Name == "llvm.cttz")
return true;
break;
case 'i':
if (Name == "llvm.isunordered")
return true;
break;
case 's':
if (Name == "llvm.sqrt")
return true;
break;
default:
break;
}
return false;
}
// UpgradeIntrinsicFunction - Convert overloaded intrinsic function names to
// their non-overloaded variants by appending the appropriate suffix based on
// the argument types.
Function* llvm::UpgradeIntrinsicFunction(Function* F) {
// See if its one of the name's we're interested in.
if (const Type* Ty = getTypeFromFunctionName(F)) {
const char* suffix =
get_suffix((Ty->isSigned() ? Ty->getUnsignedVersion() : Ty));
assert(suffix && "Intrinsic parameter type not recognized");
const std::string& Name = F->getName();
std::string new_name = Name + suffix;
std::cerr << "WARNING: change " << Name << " to " << new_name << "\n";
SymbolTable& SymTab = F->getParent()->getSymbolTable();
if (Value* V = SymTab.lookup(F->getType(),new_name))
if (Function* OtherF = dyn_cast<Function>(V))
return OtherF;
// There wasn't an existing function for the intrinsic, so now make sure the
// signedness of the arguments is correct.
if (Ty->isSigned()) {
const Type* newTy = Ty->getUnsignedVersion();
std::vector<const Type*> Params;
Params.push_back(newTy);
FunctionType* FT = FunctionType::get(newTy, Params,false);
return new Function(FT, GlobalValue::ExternalLinkage, new_name,
F->getParent());
}
// The argument was the correct type (unsigned or floating), so just
// rename the function to its correct name and return it.
F->setName(new_name);
return F;
}
return 0;
}
Instruction* llvm::MakeUpgradedCall(
Function* F, const std::vector<Value*>& Params, BasicBlock* BB,
bool isTailCall, unsigned CallingConv) {
assert(F && "Need a Function to make a CallInst");
assert(BB && "Need a BasicBlock to make a CallInst");
// Convert the params
bool signedArg = false;
std::vector<Value*> Oprnds;
for (std::vector<Value*>::const_iterator PI = Params.begin(),
PE = Params.end(); PI != PE; ++PI) {
const Type* opTy = (*PI)->getType();
if (opTy->isSigned()) {
signedArg = true;
CastInst* cast =
new CastInst(*PI,opTy->getUnsignedVersion(), "autoupgrade_cast");
BB->getInstList().push_back(cast);
Oprnds.push_back(cast);
}
else
Oprnds.push_back(*PI);
}
Instruction* result = new CallInst(F,Oprnds,"autoupgrade_call");
if (isTailCall) cast<CallInst>(result)->setTailCall();
if (CallingConv) cast<CallInst>(result)->setCallingConv(CallingConv);
if (signedArg) {
const Type* newTy = F->getReturnType()->getUnsignedVersion();
CastInst* final = new CastInst(result, newTy, "autoupgrade_uncast");
BB->getInstList().push_back(result);
result = final;
}
return result;
}
Instruction* llvm::UpgradeIntrinsicCall(CallInst *CI, Function* newF) {
Function *F = CI->getCalledFunction();
if (const Type* Ty =
(newF ? getTypeFromFunction(newF) : getTypeFromFunctionName(F))) {
std::vector<Value*> Oprnds;
User::op_iterator OI = CI->op_begin();
++OI;
for (User::op_iterator OE = CI->op_end() ; OI != OE; ++OI) {
const Type* opTy = OI->get()->getType();
if (opTy->isSigned())
Oprnds.push_back(
new CastInst(OI->get(),opTy->getUnsignedVersion(),
"autoupgrade_cast",CI));
else
Oprnds.push_back(*OI);
}
CallInst* newCI = new CallInst((newF?newF:F),Oprnds,"autoupgrade_call",CI);
newCI->setTailCall(CI->isTailCall());
newCI->setCallingConv(CI->getCallingConv());
if (const Type* oldType = CI->getCalledFunction()->getReturnType())
<
|