1 //===-- AutoUpgrade.cpp - Implement auto-upgrade helper functions ---------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the auto-upgrade helper functions
12 //===----------------------------------------------------------------------===//
14 #include "llvm/AutoUpgrade.h"
15 #include "llvm/Constants.h"
16 #include "llvm/Function.h"
17 #include "llvm/Module.h"
18 #include "llvm/Instructions.h"
19 #include "llvm/Intrinsics.h"
20 #include "llvm/ADT/SmallVector.h"
25 static bool UpgradeIntrinsicFunction1(Function *F, Function *&NewFn) {
26 assert(F && "Illegal to upgrade a non-existent Function.");
28 // Get the Function's name.
29 const std::string& Name = F->getName();
32 const FunctionType *FTy = F->getFunctionType();
34 // Quickly eliminate it, if it's not a candidate.
35 if (Name.length() <= 8 || Name[0] != 'l' || Name[1] != 'l' ||
36 Name[2] != 'v' || Name[3] != 'm' || Name[4] != '.')
39 Module *M = F->getParent();
43 // This upgrades the name of the llvm.bswap intrinsic function to only use
44 // a single type name for overloading. We only care about the old format
45 // 'llvm.bswap.i*.i*', so check for 'bswap.' and then for there being
46 // a '.' after 'bswap.'
47 if (Name.compare(5,6,"bswap.",6) == 0) {
48 std::string::size_type delim = Name.find('.',11);
50 if (delim != std::string::npos) {
51 // Construct the new name as 'llvm.bswap' + '.i*'
52 F->setName(Name.substr(0,10)+Name.substr(delim));
60 // We only want to fix the 'llvm.ct*' intrinsics which do not have the
61 // correct return type, so we check for the name, and then check if the
62 // return type does not match the parameter type.
63 if ( (Name.compare(5,5,"ctpop",5) == 0 ||
64 Name.compare(5,4,"ctlz",4) == 0 ||
65 Name.compare(5,4,"cttz",4) == 0) &&
66 FTy->getReturnType() != FTy->getParamType(0)) {
67 // We first need to change the name of the old (bad) intrinsic, because
68 // its type is incorrect, but we cannot overload that name. We
69 // arbitrarily unique it here allowing us to construct a correctly named
70 // and typed function below.
73 // Now construct the new intrinsic with the correct name and type. We
74 // leave the old function around in order to query its type, whatever it
75 // may be, and correctly convert up to the new type.
76 NewFn = cast<Function>(M->getOrInsertFunction(Name,
85 // This upgrades the llvm.part.select overloaded intrinsic names to only
86 // use one type specifier in the name. We only care about the old format
87 // 'llvm.part.select.i*.i*', and solve as above with bswap.
88 if (Name.compare(5,12,"part.select.",12) == 0) {
89 std::string::size_type delim = Name.find('.',17);
91 if (delim != std::string::npos) {
92 // Construct a new name as 'llvm.part.select' + '.i*'
93 F->setName(Name.substr(0,16)+Name.substr(delim));
100 // This upgrades the llvm.part.set intrinsics similarly as above, however
101 // we care about 'llvm.part.set.i*.i*.i*', but only the first two types
102 // must match. There is an additional type specifier after these two
103 // matching types that we must retain when upgrading. Thus, we require
104 // finding 2 periods, not just one, after the intrinsic name.
105 if (Name.compare(5,9,"part.set.",9) == 0) {
106 std::string::size_type delim = Name.find('.',14);
108 if (delim != std::string::npos &&
109 Name.find('.',delim+1) != std::string::npos) {
110 // Construct a new name as 'llvm.part.select' + '.i*.i*'
111 F->setName(Name.substr(0,13)+Name.substr(delim));
120 // This fixes all MMX shift intrinsic instructions to take a
121 // v1i64 instead of a v2i32 as the second parameter.
122 if (Name.compare(5,10,"x86.mmx.ps",10) == 0 &&
123 (Name.compare(13,4,"psll", 4) == 0 ||
124 Name.compare(13,4,"psra", 4) == 0 ||
125 Name.compare(13,4,"psrl", 4) == 0) && Name[17] != 'i') {
127 const llvm::Type *VT = VectorType::get(IntegerType::get(64), 1);
129 // We don't have to do anything if the parameter already has
131 if (FTy->getParamType(1) == VT)
134 // We first need to change the name of the old (bad) intrinsic, because
135 // its type is incorrect, but we cannot overload that name. We
136 // arbitrarily unique it here allowing us to construct a correctly named
137 // and typed function below.
140 assert(FTy->getNumParams() == 2 && "MMX shift intrinsics take 2 args!");
142 // Now construct the new intrinsic with the correct name and type. We
143 // leave the old function around in order to query its type, whatever it
144 // may be, and correctly convert up to the new type.
145 NewFn = cast<Function>(M->getOrInsertFunction(Name,
146 FTy->getReturnType(),
147 FTy->getParamType(0),
151 } else if (Name.compare(5,17,"x86.sse2.loadh.pd",17) == 0 ||
152 Name.compare(5,17,"x86.sse2.loadl.pd",17) == 0 ||
153 Name.compare(5,16,"x86.sse2.movl.dq",16) == 0) {
154 // Calls to these intrinsics are transformed into ShuffleVector's.
162 // This may not belong here. This function is effectively being overloaded
163 // to both detect an intrinsic which needs upgrading, and to provide the
164 // upgraded form of the intrinsic. We should perhaps have two separate
165 // functions for this.
169 bool llvm::UpgradeIntrinsicFunction(Function *F, Function *&NewFn) {
171 bool Upgraded = UpgradeIntrinsicFunction1(F, NewFn);
173 // Upgrade intrinsic attributes. This does not change the function.
176 if (unsigned id = F->getIntrinsicID(true))
177 F->setParamAttrs(Intrinsic::getParamAttrs((Intrinsic::ID)id));
181 // UpgradeIntrinsicCall - Upgrade a call to an old intrinsic to be a call the
182 // upgraded intrinsic. All argument and return casting must be provided in
183 // order to seamlessly integrate with existing context.
184 void llvm::UpgradeIntrinsicCall(CallInst *CI, Function *NewFn) {
185 Function *F = CI->getCalledFunction();
186 assert(F && "CallInst has no function associated with it.");
189 bool isLoadH = false, isLoadL = false, isMovL = false;
190 if (strcmp(F->getNameStart(), "llvm.x86.sse2.loadh.pd") == 0)
192 else if (strcmp(F->getNameStart(), "llvm.x86.sse2.loadl.pd") == 0)
194 else if (strcmp(F->getNameStart(), "llvm.x86.sse2.movl.dq") == 0)
197 if (isLoadH || isLoadL || isMovL) {
198 std::vector<Constant*> Idxs;
199 Value *Op0 = CI->getOperand(1);
200 ShuffleVectorInst *SI;
201 if (isLoadH || isLoadL) {
202 Value *Op1 = UndefValue::get(Op0->getType());
203 Value *Addr = new BitCastInst(CI->getOperand(2),
204 PointerType::getUnqual(Type::DoubleTy),
206 Value *Load = new LoadInst(Addr, "upgraded.", false, 8, CI);
207 Value *Idx = ConstantInt::get(Type::Int32Ty, 0);
208 Op1 = InsertElementInst::Create(Op1, Load, Idx, "upgraded.", CI);
211 Idxs.push_back(ConstantInt::get(Type::Int32Ty, 0));
212 Idxs.push_back(ConstantInt::get(Type::Int32Ty, 2));
214 Idxs.push_back(ConstantInt::get(Type::Int32Ty, 2));
215 Idxs.push_back(ConstantInt::get(Type::Int32Ty, 1));
217 Value *Mask = ConstantVector::get(Idxs);
218 SI = new ShuffleVectorInst(Op0, Op1, Mask, "upgraded.", CI);
220 Constant *Zero = ConstantInt::get(Type::Int32Ty, 0);
221 Idxs.push_back(Zero);
222 Idxs.push_back(Zero);
223 Idxs.push_back(Zero);
224 Idxs.push_back(Zero);
225 Value *ZeroV = ConstantVector::get(Idxs);
228 Idxs.push_back(ConstantInt::get(Type::Int32Ty, 4));
229 Idxs.push_back(ConstantInt::get(Type::Int32Ty, 5));
230 Idxs.push_back(ConstantInt::get(Type::Int32Ty, 2));
231 Idxs.push_back(ConstantInt::get(Type::Int32Ty, 3));
232 Value *Mask = ConstantVector::get(Idxs);
233 SI = new ShuffleVectorInst(ZeroV, Op0, Mask, "upgraded.", CI);
236 // Handle any uses of the old CallInst.
237 if (!CI->use_empty())
238 // Replace all uses of the old call with the new cast which has the
240 CI->replaceAllUsesWith(SI);
242 // Clean up the old call now that it has been completely upgraded.
243 CI->eraseFromParent();
245 assert(0 && "Unknown function for CallInst upgrade.");
250 switch (NewFn->getIntrinsicID()) {
251 default: assert(0 && "Unknown function for CallInst upgrade.");
252 case Intrinsic::x86_mmx_psll_d:
253 case Intrinsic::x86_mmx_psll_q:
254 case Intrinsic::x86_mmx_psll_w:
255 case Intrinsic::x86_mmx_psra_d:
256 case Intrinsic::x86_mmx_psra_w:
257 case Intrinsic::x86_mmx_psrl_d:
258 case Intrinsic::x86_mmx_psrl_q:
259 case Intrinsic::x86_mmx_psrl_w: {
262 Operands[0] = CI->getOperand(1);
264 // Cast the second parameter to the correct type.
265 BitCastInst *BC = new BitCastInst(CI->getOperand(2),
266 NewFn->getFunctionType()->getParamType(1),
270 // Construct a new CallInst
271 CallInst *NewCI = CallInst::Create(NewFn, Operands, Operands+2,
272 "upgraded."+CI->getName(), CI);
273 NewCI->setTailCall(CI->isTailCall());
274 NewCI->setCallingConv(CI->getCallingConv());
276 // Handle any uses of the old CallInst.
277 if (!CI->use_empty())
278 // Replace all uses of the old call with the new cast which has the
280 CI->replaceAllUsesWith(NewCI);
282 // Clean up the old call now that it has been completely upgraded.
283 CI->eraseFromParent();
286 case Intrinsic::ctlz:
287 case Intrinsic::ctpop:
288 case Intrinsic::cttz: {
289 // Build a small vector of the 1..(N-1) operands, which are the
291 SmallVector<Value*, 8> Operands(CI->op_begin()+1, CI->op_end());
293 // Construct a new CallInst
294 CallInst *NewCI = CallInst::Create(NewFn, Operands.begin(), Operands.end(),
295 "upgraded."+CI->getName(), CI);
296 NewCI->setTailCall(CI->isTailCall());
297 NewCI->setCallingConv(CI->getCallingConv());
299 // Handle any uses of the old CallInst.
300 if (!CI->use_empty()) {
301 // Check for sign extend parameter attributes on the return values.
302 bool SrcSExt = NewFn->getParamAttrs().paramHasAttr(0, ParamAttr::SExt);
303 bool DestSExt = F->getParamAttrs().paramHasAttr(0, ParamAttr::SExt);
305 // Construct an appropriate cast from the new return type to the old.
306 CastInst *RetCast = CastInst::Create(
307 CastInst::getCastOpcode(NewCI, SrcSExt,
310 NewCI, F->getReturnType(),
311 NewCI->getName(), CI);
312 NewCI->moveBefore(RetCast);
314 // Replace all uses of the old call with the new cast which has the
316 CI->replaceAllUsesWith(RetCast);
319 // Clean up the old call now that it has been completely upgraded.
320 CI->eraseFromParent();
326 // This tests each Function to determine if it needs upgrading. When we find
327 // one we are interested in, we then upgrade all calls to reflect the new
329 void llvm::UpgradeCallsToIntrinsic(Function* F) {
330 assert(F && "Illegal attempt to upgrade a non-existent intrinsic.");
332 // Upgrade the function and check if it is a totaly new function.
334 if (UpgradeIntrinsicFunction(F, NewFn)) {
336 // Replace all uses to the old function with the new one if necessary.
337 for (Value::use_iterator UI = F->use_begin(), UE = F->use_end();
339 if (CallInst* CI = dyn_cast<CallInst>(*UI++))
340 UpgradeIntrinsicCall(CI, NewFn);
342 // Remove old function, no longer used, from the module.
343 F->eraseFromParent();