1 //===-- AutoUpgrade.cpp - Implement auto-upgrade helper functions ---------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the auto-upgrade helper functions
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/AutoUpgrade.h"
15 #include "llvm/DebugInfo.h"
16 #include "llvm/IR/Constants.h"
17 #include "llvm/IR/Function.h"
18 #include "llvm/IR/IRBuilder.h"
19 #include "llvm/IR/Instruction.h"
20 #include "llvm/IR/IntrinsicInst.h"
21 #include "llvm/IR/LLVMContext.h"
22 #include "llvm/IR/Module.h"
23 #include "llvm/Support/CFG.h"
24 #include "llvm/Support/CallSite.h"
25 #include "llvm/Support/ErrorHandling.h"
26 #include <cstring>
27 using namespace llvm;
28
29 // Upgrade the declarations of the SSE4.1 functions whose arguments have
30 // changed their type from v4f32 to v2i64.
UpgradeSSE41Function(Function * F,Intrinsic::ID IID,Function * & NewFn)31 static bool UpgradeSSE41Function(Function* F, Intrinsic::ID IID,
32 Function *&NewFn) {
33 // Check whether this is an old version of the function, which received
34 // v4f32 arguments.
35 Type *Arg0Type = F->getFunctionType()->getParamType(0);
36 if (Arg0Type != VectorType::get(Type::getFloatTy(F->getContext()), 4))
37 return false;
38
39 // Yes, it's old, replace it with new version.
40 F->setName(F->getName() + ".old");
41 NewFn = Intrinsic::getDeclaration(F->getParent(), IID);
42 return true;
43 }
44
UpgradeIntrinsicFunction1(Function * F,Function * & NewFn)45 static bool UpgradeIntrinsicFunction1(Function *F, Function *&NewFn) {
46 assert(F && "Illegal to upgrade a non-existent Function.");
47
48 // Quickly eliminate it, if it's not a candidate.
49 StringRef Name = F->getName();
50 if (Name.size() <= 8 || !Name.startswith("llvm."))
51 return false;
52 Name = Name.substr(5); // Strip off "llvm."
53
54 switch (Name[0]) {
55 default: break;
56 case 'a': {
57 if (Name.startswith("arm.neon.vclz")) {
58 Type* args[2] = {
59 F->arg_begin()->getType(),
60 Type::getInt1Ty(F->getContext())
61 };
62 // Can't use Intrinsic::getDeclaration here as it adds a ".i1" to
63 // the end of the name. Change name from llvm.arm.neon.vclz.* to
64 // llvm.ctlz.*
65 FunctionType* fType = FunctionType::get(F->getReturnType(), args, false);
66 NewFn = Function::Create(fType, F->getLinkage(),
67 "llvm.ctlz." + Name.substr(14), F->getParent());
68 return true;
69 }
70 if (Name.startswith("arm.neon.vcnt")) {
71 NewFn = Intrinsic::getDeclaration(F->getParent(), Intrinsic::ctpop,
72 F->arg_begin()->getType());
73 return true;
74 }
75 break;
76 }
77 case 'c': {
78 if (Name.startswith("ctlz.") && F->arg_size() == 1) {
79 F->setName(Name + ".old");
80 NewFn = Intrinsic::getDeclaration(F->getParent(), Intrinsic::ctlz,
81 F->arg_begin()->getType());
82 return true;
83 }
84 if (Name.startswith("cttz.") && F->arg_size() == 1) {
85 F->setName(Name + ".old");
86 NewFn = Intrinsic::getDeclaration(F->getParent(), Intrinsic::cttz,
87 F->arg_begin()->getType());
88 return true;
89 }
90 break;
91 }
92 case 'o':
93 // We only need to change the name to match the mangling including the
94 // address space.
95 if (F->arg_size() == 2 && Name.startswith("objectsize.")) {
96 Type *Tys[2] = { F->getReturnType(), F->arg_begin()->getType() };
97 if (F->getName() != Intrinsic::getName(Intrinsic::objectsize, Tys)) {
98 F->setName(Name + ".old");
99 NewFn = Intrinsic::getDeclaration(F->getParent(),
100 Intrinsic::objectsize, Tys);
101 return true;
102 }
103 }
104 break;
105
106 case 'x': {
107 if (Name.startswith("x86.sse2.pcmpeq.") ||
108 Name.startswith("x86.sse2.pcmpgt.") ||
109 Name.startswith("x86.avx2.pcmpeq.") ||
110 Name.startswith("x86.avx2.pcmpgt.") ||
111 Name.startswith("x86.avx.vpermil.") ||
112 Name == "x86.avx.movnt.dq.256" ||
113 Name == "x86.avx.movnt.pd.256" ||
114 Name == "x86.avx.movnt.ps.256" ||
115 Name == "x86.sse42.crc32.64.8" ||
116 (Name.startswith("x86.xop.vpcom") && F->arg_size() == 2)) {
117 NewFn = 0;
118 return true;
119 }
120 // SSE4.1 ptest functions may have an old signature.
121 if (Name.startswith("x86.sse41.ptest")) {
122 if (Name == "x86.sse41.ptestc")
123 return UpgradeSSE41Function(F, Intrinsic::x86_sse41_ptestc, NewFn);
124 if (Name == "x86.sse41.ptestz")
125 return UpgradeSSE41Function(F, Intrinsic::x86_sse41_ptestz, NewFn);
126 if (Name == "x86.sse41.ptestnzc")
127 return UpgradeSSE41Function(F, Intrinsic::x86_sse41_ptestnzc, NewFn);
128 }
129 // frcz.ss/sd may need to have an argument dropped
130 if (Name.startswith("x86.xop.vfrcz.ss") && F->arg_size() == 2) {
131 F->setName(Name + ".old");
132 NewFn = Intrinsic::getDeclaration(F->getParent(),
133 Intrinsic::x86_xop_vfrcz_ss);
134 return true;
135 }
136 if (Name.startswith("x86.xop.vfrcz.sd") && F->arg_size() == 2) {
137 F->setName(Name + ".old");
138 NewFn = Intrinsic::getDeclaration(F->getParent(),
139 Intrinsic::x86_xop_vfrcz_sd);
140 return true;
141 }
142 // Fix the FMA4 intrinsics to remove the 4
143 if (Name.startswith("x86.fma4.")) {
144 F->setName("llvm.x86.fma" + Name.substr(8));
145 NewFn = F;
146 return true;
147 }
148 break;
149 }
150 }
151
152 // This may not belong here. This function is effectively being overloaded
153 // to both detect an intrinsic which needs upgrading, and to provide the
154 // upgraded form of the intrinsic. We should perhaps have two separate
155 // functions for this.
156 return false;
157 }
158
UpgradeIntrinsicFunction(Function * F,Function * & NewFn)159 bool llvm::UpgradeIntrinsicFunction(Function *F, Function *&NewFn) {
160 NewFn = 0;
161 bool Upgraded = UpgradeIntrinsicFunction1(F, NewFn);
162
163 // Upgrade intrinsic attributes. This does not change the function.
164 if (NewFn)
165 F = NewFn;
166 if (unsigned id = F->getIntrinsicID())
167 F->setAttributes(Intrinsic::getAttributes(F->getContext(),
168 (Intrinsic::ID)id));
169 return Upgraded;
170 }
171
UpgradeGlobalVariable(GlobalVariable * GV)172 bool llvm::UpgradeGlobalVariable(GlobalVariable *GV) {
173 // Nothing to do yet.
174 return false;
175 }
176
177 // UpgradeIntrinsicCall - Upgrade a call to an old intrinsic to be a call the
178 // upgraded intrinsic. All argument and return casting must be provided in
179 // order to seamlessly integrate with existing context.
UpgradeIntrinsicCall(CallInst * CI,Function * NewFn)180 void llvm::UpgradeIntrinsicCall(CallInst *CI, Function *NewFn) {
181 Function *F = CI->getCalledFunction();
182 LLVMContext &C = CI->getContext();
183 IRBuilder<> Builder(C);
184 Builder.SetInsertPoint(CI->getParent(), CI);
185
186 assert(F && "Intrinsic call is not direct?");
187
188 if (!NewFn) {
189 // Get the Function's name.
190 StringRef Name = F->getName();
191
192 Value *Rep;
193 // Upgrade packed integer vector compares intrinsics to compare instructions
194 if (Name.startswith("llvm.x86.sse2.pcmpeq.") ||
195 Name.startswith("llvm.x86.avx2.pcmpeq.")) {
196 Rep = Builder.CreateICmpEQ(CI->getArgOperand(0), CI->getArgOperand(1),
197 "pcmpeq");
198 // need to sign extend since icmp returns vector of i1
199 Rep = Builder.CreateSExt(Rep, CI->getType(), "");
200 } else if (Name.startswith("llvm.x86.sse2.pcmpgt.") ||
201 Name.startswith("llvm.x86.avx2.pcmpgt.")) {
202 Rep = Builder.CreateICmpSGT(CI->getArgOperand(0), CI->getArgOperand(1),
203 "pcmpgt");
204 // need to sign extend since icmp returns vector of i1
205 Rep = Builder.CreateSExt(Rep, CI->getType(), "");
206 } else if (Name == "llvm.x86.avx.movnt.dq.256" ||
207 Name == "llvm.x86.avx.movnt.ps.256" ||
208 Name == "llvm.x86.avx.movnt.pd.256") {
209 IRBuilder<> Builder(C);
210 Builder.SetInsertPoint(CI->getParent(), CI);
211
212 Module *M = F->getParent();
213 SmallVector<Value *, 1> Elts;
214 Elts.push_back(ConstantInt::get(Type::getInt32Ty(C), 1));
215 MDNode *Node = MDNode::get(C, Elts);
216
217 Value *Arg0 = CI->getArgOperand(0);
218 Value *Arg1 = CI->getArgOperand(1);
219
220 // Convert the type of the pointer to a pointer to the stored type.
221 Value *BC = Builder.CreateBitCast(Arg0,
222 PointerType::getUnqual(Arg1->getType()),
223 "cast");
224 StoreInst *SI = Builder.CreateStore(Arg1, BC);
225 SI->setMetadata(M->getMDKindID("nontemporal"), Node);
226 SI->setAlignment(16);
227
228 // Remove intrinsic.
229 CI->eraseFromParent();
230 return;
231 } else if (Name.startswith("llvm.x86.xop.vpcom")) {
232 Intrinsic::ID intID;
233 if (Name.endswith("ub"))
234 intID = Intrinsic::x86_xop_vpcomub;
235 else if (Name.endswith("uw"))
236 intID = Intrinsic::x86_xop_vpcomuw;
237 else if (Name.endswith("ud"))
238 intID = Intrinsic::x86_xop_vpcomud;
239 else if (Name.endswith("uq"))
240 intID = Intrinsic::x86_xop_vpcomuq;
241 else if (Name.endswith("b"))
242 intID = Intrinsic::x86_xop_vpcomb;
243 else if (Name.endswith("w"))
244 intID = Intrinsic::x86_xop_vpcomw;
245 else if (Name.endswith("d"))
246 intID = Intrinsic::x86_xop_vpcomd;
247 else if (Name.endswith("q"))
248 intID = Intrinsic::x86_xop_vpcomq;
249 else
250 llvm_unreachable("Unknown suffix");
251
252 Name = Name.substr(18); // strip off "llvm.x86.xop.vpcom"
253 unsigned Imm;
254 if (Name.startswith("lt"))
255 Imm = 0;
256 else if (Name.startswith("le"))
257 Imm = 1;
258 else if (Name.startswith("gt"))
259 Imm = 2;
260 else if (Name.startswith("ge"))
261 Imm = 3;
262 else if (Name.startswith("eq"))
263 Imm = 4;
264 else if (Name.startswith("ne"))
265 Imm = 5;
266 else if (Name.startswith("true"))
267 Imm = 6;
268 else if (Name.startswith("false"))
269 Imm = 7;
270 else
271 llvm_unreachable("Unknown condition");
272
273 Function *VPCOM = Intrinsic::getDeclaration(F->getParent(), intID);
274 Rep = Builder.CreateCall3(VPCOM, CI->getArgOperand(0),
275 CI->getArgOperand(1), Builder.getInt8(Imm));
276 } else if (Name == "llvm.x86.sse42.crc32.64.8") {
277 Function *CRC32 = Intrinsic::getDeclaration(F->getParent(),
278 Intrinsic::x86_sse42_crc32_32_8);
279 Value *Trunc0 = Builder.CreateTrunc(CI->getArgOperand(0), Type::getInt32Ty(C));
280 Rep = Builder.CreateCall2(CRC32, Trunc0, CI->getArgOperand(1));
281 Rep = Builder.CreateZExt(Rep, CI->getType(), "");
282 } else {
283 bool PD128 = false, PD256 = false, PS128 = false, PS256 = false;
284 if (Name == "llvm.x86.avx.vpermil.pd.256")
285 PD256 = true;
286 else if (Name == "llvm.x86.avx.vpermil.pd")
287 PD128 = true;
288 else if (Name == "llvm.x86.avx.vpermil.ps.256")
289 PS256 = true;
290 else if (Name == "llvm.x86.avx.vpermil.ps")
291 PS128 = true;
292
293 if (PD256 || PD128 || PS256 || PS128) {
294 Value *Op0 = CI->getArgOperand(0);
295 unsigned Imm = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue();
296 SmallVector<Constant*, 8> Idxs;
297
298 if (PD128)
299 for (unsigned i = 0; i != 2; ++i)
300 Idxs.push_back(Builder.getInt32((Imm >> i) & 0x1));
301 else if (PD256)
302 for (unsigned l = 0; l != 4; l+=2)
303 for (unsigned i = 0; i != 2; ++i)
304 Idxs.push_back(Builder.getInt32(((Imm >> (l+i)) & 0x1) + l));
305 else if (PS128)
306 for (unsigned i = 0; i != 4; ++i)
307 Idxs.push_back(Builder.getInt32((Imm >> (2 * i)) & 0x3));
308 else if (PS256)
309 for (unsigned l = 0; l != 8; l+=4)
310 for (unsigned i = 0; i != 4; ++i)
311 Idxs.push_back(Builder.getInt32(((Imm >> (2 * i)) & 0x3) + l));
312 else
313 llvm_unreachable("Unexpected function");
314
315 Rep = Builder.CreateShuffleVector(Op0, Op0, ConstantVector::get(Idxs));
316 } else {
317 llvm_unreachable("Unknown function for CallInst upgrade.");
318 }
319 }
320
321 CI->replaceAllUsesWith(Rep);
322 CI->eraseFromParent();
323 return;
324 }
325
326 std::string Name = CI->getName().str();
327 CI->setName(Name + ".old");
328
329 switch (NewFn->getIntrinsicID()) {
330 default:
331 llvm_unreachable("Unknown function for CallInst upgrade.");
332
333 case Intrinsic::ctlz:
334 case Intrinsic::cttz:
335 assert(CI->getNumArgOperands() == 1 &&
336 "Mismatch between function args and call args");
337 CI->replaceAllUsesWith(Builder.CreateCall2(NewFn, CI->getArgOperand(0),
338 Builder.getFalse(), Name));
339 CI->eraseFromParent();
340 return;
341
342 case Intrinsic::objectsize:
343 CI->replaceAllUsesWith(Builder.CreateCall2(NewFn,
344 CI->getArgOperand(0),
345 CI->getArgOperand(1),
346 Name));
347 CI->eraseFromParent();
348 return;
349
350 case Intrinsic::arm_neon_vclz: {
351 // Change name from llvm.arm.neon.vclz.* to llvm.ctlz.*
352 CI->replaceAllUsesWith(Builder.CreateCall2(NewFn, CI->getArgOperand(0),
353 Builder.getFalse(),
354 "llvm.ctlz." + Name.substr(14)));
355 CI->eraseFromParent();
356 return;
357 }
358 case Intrinsic::ctpop: {
359 CI->replaceAllUsesWith(Builder.CreateCall(NewFn, CI->getArgOperand(0)));
360 CI->eraseFromParent();
361 return;
362 }
363
364 case Intrinsic::x86_xop_vfrcz_ss:
365 case Intrinsic::x86_xop_vfrcz_sd:
366 CI->replaceAllUsesWith(Builder.CreateCall(NewFn, CI->getArgOperand(1),
367 Name));
368 CI->eraseFromParent();
369 return;
370
371 case Intrinsic::x86_sse41_ptestc:
372 case Intrinsic::x86_sse41_ptestz:
373 case Intrinsic::x86_sse41_ptestnzc: {
374 // The arguments for these intrinsics used to be v4f32, and changed
375 // to v2i64. This is purely a nop, since those are bitwise intrinsics.
376 // So, the only thing required is a bitcast for both arguments.
377 // First, check the arguments have the old type.
378 Value *Arg0 = CI->getArgOperand(0);
379 if (Arg0->getType() != VectorType::get(Type::getFloatTy(C), 4))
380 return;
381
382 // Old intrinsic, add bitcasts
383 Value *Arg1 = CI->getArgOperand(1);
384
385 Value *BC0 =
386 Builder.CreateBitCast(Arg0,
387 VectorType::get(Type::getInt64Ty(C), 2),
388 "cast");
389 Value *BC1 =
390 Builder.CreateBitCast(Arg1,
391 VectorType::get(Type::getInt64Ty(C), 2),
392 "cast");
393
394 CallInst* NewCall = Builder.CreateCall2(NewFn, BC0, BC1, Name);
395 CI->replaceAllUsesWith(NewCall);
396 CI->eraseFromParent();
397 return;
398 }
399 }
400 }
401
402 // This tests each Function to determine if it needs upgrading. When we find
403 // one we are interested in, we then upgrade all calls to reflect the new
404 // function.
UpgradeCallsToIntrinsic(Function * F)405 void llvm::UpgradeCallsToIntrinsic(Function* F) {
406 assert(F && "Illegal attempt to upgrade a non-existent intrinsic.");
407
408 // Upgrade the function and check if it is a totaly new function.
409 Function *NewFn;
410 if (UpgradeIntrinsicFunction(F, NewFn)) {
411 if (NewFn != F) {
412 // Replace all uses to the old function with the new one if necessary.
413 for (Value::use_iterator UI = F->use_begin(), UE = F->use_end();
414 UI != UE; ) {
415 if (CallInst *CI = dyn_cast<CallInst>(*UI++))
416 UpgradeIntrinsicCall(CI, NewFn);
417 }
418 // Remove old function, no longer used, from the module.
419 F->eraseFromParent();
420 }
421 }
422 }
423
UpgradeInstWithTBAATag(Instruction * I)424 void llvm::UpgradeInstWithTBAATag(Instruction *I) {
425 MDNode *MD = I->getMetadata(LLVMContext::MD_tbaa);
426 assert(MD && "UpgradeInstWithTBAATag should have a TBAA tag");
427 // Check if the tag uses struct-path aware TBAA format.
428 if (isa<MDNode>(MD->getOperand(0)) && MD->getNumOperands() >= 3)
429 return;
430
431 if (MD->getNumOperands() == 3) {
432 Value *Elts[] = {
433 MD->getOperand(0),
434 MD->getOperand(1)
435 };
436 MDNode *ScalarType = MDNode::get(I->getContext(), Elts);
437 // Create a MDNode <ScalarType, ScalarType, offset 0, const>
438 Value *Elts2[] = {
439 ScalarType, ScalarType,
440 Constant::getNullValue(Type::getInt64Ty(I->getContext())),
441 MD->getOperand(2)
442 };
443 I->setMetadata(LLVMContext::MD_tbaa, MDNode::get(I->getContext(), Elts2));
444 } else {
445 // Create a MDNode <MD, MD, offset 0>
446 Value *Elts[] = {MD, MD,
447 Constant::getNullValue(Type::getInt64Ty(I->getContext()))};
448 I->setMetadata(LLVMContext::MD_tbaa, MDNode::get(I->getContext(), Elts));
449 }
450 }
451
UpgradeBitCastInst(unsigned Opc,Value * V,Type * DestTy,Instruction * & Temp)452 Instruction *llvm::UpgradeBitCastInst(unsigned Opc, Value *V, Type *DestTy,
453 Instruction *&Temp) {
454 if (Opc != Instruction::BitCast)
455 return 0;
456
457 Temp = 0;
458 Type *SrcTy = V->getType();
459 if (SrcTy->isPtrOrPtrVectorTy() && DestTy->isPtrOrPtrVectorTy() &&
460 SrcTy->getPointerAddressSpace() != DestTy->getPointerAddressSpace()) {
461 LLVMContext &Context = V->getContext();
462
463 // We have no information about target data layout, so we assume that
464 // the maximum pointer size is 64bit.
465 Type *MidTy = Type::getInt64Ty(Context);
466 Temp = CastInst::Create(Instruction::PtrToInt, V, MidTy);
467
468 return CastInst::Create(Instruction::IntToPtr, Temp, DestTy);
469 }
470
471 return 0;
472 }
473
UpgradeBitCastExpr(unsigned Opc,Constant * C,Type * DestTy)474 Value *llvm::UpgradeBitCastExpr(unsigned Opc, Constant *C, Type *DestTy) {
475 if (Opc != Instruction::BitCast)
476 return 0;
477
478 Type *SrcTy = C->getType();
479 if (SrcTy->isPtrOrPtrVectorTy() && DestTy->isPtrOrPtrVectorTy() &&
480 SrcTy->getPointerAddressSpace() != DestTy->getPointerAddressSpace()) {
481 LLVMContext &Context = C->getContext();
482
483 // We have no information about target data layout, so we assume that
484 // the maximum pointer size is 64bit.
485 Type *MidTy = Type::getInt64Ty(Context);
486
487 return ConstantExpr::getIntToPtr(ConstantExpr::getPtrToInt(C, MidTy),
488 DestTy);
489 }
490
491 return 0;
492 }
493
494 /// Check the debug info version number, if it is out-dated, drop the debug
495 /// info. Return true if module is modified.
UpgradeDebugInfo(Module & M)496 bool llvm::UpgradeDebugInfo(Module &M) {
497 if (getDebugMetadataVersionFromModule(M) == DEBUG_METADATA_VERSION)
498 return false;
499
500 return StripDebugInfo(M);
501 }
502