[X86] Fix XOP vpcom intrinsic autoupgrade to map 'true' and 'false' to the correct...
[oota-llvm.git] / lib / IR / AutoUpgrade.cpp
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/IR/AutoUpgrade.h"
15 #include "llvm/IR/CFG.h"
16 #include "llvm/IR/CallSite.h"
17 #include "llvm/IR/Constants.h"
18 #include "llvm/IR/DIBuilder.h"
19 #include "llvm/IR/DebugInfo.h"
20 #include "llvm/IR/DiagnosticInfo.h"
21 #include "llvm/IR/Function.h"
22 #include "llvm/IR/IRBuilder.h"
23 #include "llvm/IR/Instruction.h"
24 #include "llvm/IR/IntrinsicInst.h"
25 #include "llvm/IR/LLVMContext.h"
26 #include "llvm/IR/Module.h"
27 #include "llvm/Support/ErrorHandling.h"
28 #include <cstring>
29 using namespace llvm;
30
31 // Upgrade the declarations of the SSE4.1 functions whose arguments have
32 // changed their type from v4f32 to v2i64.
33 static bool UpgradeSSE41Function(Function* F, Intrinsic::ID IID,
34                                  Function *&NewFn) {
35   // Check whether this is an old version of the function, which received
36   // v4f32 arguments.
37   Type *Arg0Type = F->getFunctionType()->getParamType(0);
38   if (Arg0Type != VectorType::get(Type::getFloatTy(F->getContext()), 4))
39     return false;
40
41   // Yes, it's old, replace it with new version.
42   F->setName(F->getName() + ".old");
43   NewFn = Intrinsic::getDeclaration(F->getParent(), IID);
44   return true;
45 }
46
47 // Upgrade the declarations of intrinsic functions whose 8-bit immediate mask
48 // arguments have changed their type from i32 to i8.
49 static bool UpgradeX86IntrinsicsWith8BitMask(Function *F, Intrinsic::ID IID,
50                                              Function *&NewFn) {
51   // Check that the last argument is an i32.
52   Type *LastArgType = F->getFunctionType()->getParamType(
53      F->getFunctionType()->getNumParams() - 1);
54   if (!LastArgType->isIntegerTy(32))
55     return false;
56
57   // Move this function aside and map down.
58   F->setName(F->getName() + ".old");
59   NewFn = Intrinsic::getDeclaration(F->getParent(), IID);
60   return true;
61 }
62
63 // Upgrade the declarations of AVX-512 cmp intrinsic functions whose 8-bit
64 // immediates have changed their type from i32 to i8.
65 static bool UpgradeAVX512CmpIntrinsic(Function *F, Intrinsic::ID IID,
66                                       Function *&NewFn) {
67   // Check that the last argument is an i32.
68   Type *LastArgType = F->getFunctionType()->getParamType(2);
69   if (!LastArgType->isIntegerTy(32))
70     return false;
71
72   // Move this function aside and map down.
73   F->setName(F->getName() + ".old");
74   NewFn = Intrinsic::getDeclaration(F->getParent(), IID);
75   return true;
76 }
77
78 static bool UpgradeIntrinsicFunction1(Function *F, Function *&NewFn) {
79   assert(F && "Illegal to upgrade a non-existent Function.");
80
81   // Quickly eliminate it, if it's not a candidate.
82   StringRef Name = F->getName();
83   if (Name.size() <= 8 || !Name.startswith("llvm."))
84     return false;
85   Name = Name.substr(5); // Strip off "llvm."
86
87   switch (Name[0]) {
88   default: break;
89   case 'a': {
90     if (Name.startswith("arm.neon.vclz")) {
91       Type* args[2] = {
92         F->arg_begin()->getType(),
93         Type::getInt1Ty(F->getContext())
94       };
95       // Can't use Intrinsic::getDeclaration here as it adds a ".i1" to
96       // the end of the name. Change name from llvm.arm.neon.vclz.* to
97       //  llvm.ctlz.*
98       FunctionType* fType = FunctionType::get(F->getReturnType(), args, false);
99       NewFn = Function::Create(fType, F->getLinkage(),
100                                "llvm.ctlz." + Name.substr(14), F->getParent());
101       return true;
102     }
103     if (Name.startswith("arm.neon.vcnt")) {
104       NewFn = Intrinsic::getDeclaration(F->getParent(), Intrinsic::ctpop,
105                                         F->arg_begin()->getType());
106       return true;
107     }
108     break;
109   }
110   case 'c': {
111     if (Name.startswith("ctlz.") && F->arg_size() == 1) {
112       F->setName(Name + ".old");
113       NewFn = Intrinsic::getDeclaration(F->getParent(), Intrinsic::ctlz,
114                                         F->arg_begin()->getType());
115       return true;
116     }
117     if (Name.startswith("cttz.") && F->arg_size() == 1) {
118       F->setName(Name + ".old");
119       NewFn = Intrinsic::getDeclaration(F->getParent(), Intrinsic::cttz,
120                                         F->arg_begin()->getType());
121       return true;
122     }
123     break;
124   }
125   case 'd': {
126     if (Name.startswith("dbg.declare") && F->arg_size() == 2) {
127       F->setName(Name + ".old");
128       NewFn = Intrinsic::getDeclaration(F->getParent(), Intrinsic::dbg_declare);
129       return true;
130     }
131     if (Name.startswith("dbg.value") && F->arg_size() == 3) {
132       F->setName(Name + ".old");
133       NewFn = Intrinsic::getDeclaration(F->getParent(), Intrinsic::dbg_value);
134       return true;
135     }
136     break;
137   }
138
139   case 'o':
140     // We only need to change the name to match the mangling including the
141     // address space.
142     if (F->arg_size() == 2 && Name.startswith("objectsize.")) {
143       Type *Tys[2] = { F->getReturnType(), F->arg_begin()->getType() };
144       if (F->getName() != Intrinsic::getName(Intrinsic::objectsize, Tys)) {
145         F->setName(Name + ".old");
146         NewFn = Intrinsic::getDeclaration(F->getParent(),
147                                           Intrinsic::objectsize, Tys);
148         return true;
149       }
150     }
151     break;
152
153   case 'x': {
154     if (Name.startswith("x86.sse2.pcmpeq.") ||
155         Name.startswith("x86.sse2.pcmpgt.") ||
156         Name.startswith("x86.avx2.pcmpeq.") ||
157         Name.startswith("x86.avx2.pcmpgt.") ||
158         Name.startswith("x86.avx.vpermil.") ||
159         Name == "x86.avx.movnt.dq.256" ||
160         Name == "x86.avx.movnt.pd.256" ||
161         Name == "x86.avx.movnt.ps.256" ||
162         Name == "x86.sse42.crc32.64.8" ||
163         Name == "x86.avx.vbroadcast.ss" ||
164         Name == "x86.avx.vbroadcast.ss.256" ||
165         Name == "x86.avx.vbroadcast.sd.256" ||
166         Name == "x86.sse2.psll.dq.bs" ||
167         Name == "x86.sse2.psrl.dq.bs" ||
168         (Name.startswith("x86.xop.vpcom") && F->arg_size() == 2)) {
169       NewFn = nullptr;
170       return true;
171     }
172     // SSE4.1 ptest functions may have an old signature.
173     if (Name.startswith("x86.sse41.ptest")) {
174       if (Name == "x86.sse41.ptestc")
175         return UpgradeSSE41Function(F, Intrinsic::x86_sse41_ptestc, NewFn);
176       if (Name == "x86.sse41.ptestz")
177         return UpgradeSSE41Function(F, Intrinsic::x86_sse41_ptestz, NewFn);
178       if (Name == "x86.sse41.ptestnzc")
179         return UpgradeSSE41Function(F, Intrinsic::x86_sse41_ptestnzc, NewFn);
180     }
181     // Several blend and other instructions with maskes used the wrong number of
182     // bits.
183     if (Name == "x86.sse41.pblendw")
184       return UpgradeX86IntrinsicsWith8BitMask(F, Intrinsic::x86_sse41_pblendw,
185                                               NewFn);
186     if (Name == "x86.sse41.blendpd")
187       return UpgradeX86IntrinsicsWith8BitMask(F, Intrinsic::x86_sse41_blendpd,
188                                               NewFn);
189     if (Name == "x86.sse41.blendps")
190       return UpgradeX86IntrinsicsWith8BitMask(F, Intrinsic::x86_sse41_blendps,
191                                               NewFn);
192     if (Name == "x86.sse41.insertps")
193       return UpgradeX86IntrinsicsWith8BitMask(F, Intrinsic::x86_sse41_insertps,
194                                               NewFn);
195     if (Name == "x86.sse41.dppd")
196       return UpgradeX86IntrinsicsWith8BitMask(F, Intrinsic::x86_sse41_dppd,
197                                               NewFn);
198     if (Name == "x86.sse41.dpps")
199       return UpgradeX86IntrinsicsWith8BitMask(F, Intrinsic::x86_sse41_dpps,
200                                               NewFn);
201     if (Name == "x86.sse41.mpsadbw")
202       return UpgradeX86IntrinsicsWith8BitMask(F, Intrinsic::x86_sse41_mpsadbw,
203                                               NewFn);
204     if (Name == "x86.avx.blend.pd.256")
205       return UpgradeX86IntrinsicsWith8BitMask(
206           F, Intrinsic::x86_avx_blend_pd_256, NewFn);
207     if (Name == "x86.avx.blend.ps.256")
208       return UpgradeX86IntrinsicsWith8BitMask(
209           F, Intrinsic::x86_avx_blend_ps_256, NewFn);
210     if (Name == "x86.avx.dp.ps.256")
211       return UpgradeX86IntrinsicsWith8BitMask(F, Intrinsic::x86_avx_dp_ps_256,
212                                               NewFn);
213     if (Name == "x86.avx2.pblendw")
214       return UpgradeX86IntrinsicsWith8BitMask(F, Intrinsic::x86_avx2_pblendw,
215                                               NewFn);
216     if (Name == "x86.avx2.pblendd.128")
217       return UpgradeX86IntrinsicsWith8BitMask(
218           F, Intrinsic::x86_avx2_pblendd_128, NewFn);
219     if (Name == "x86.avx2.pblendd.256")
220       return UpgradeX86IntrinsicsWith8BitMask(
221           F, Intrinsic::x86_avx2_pblendd_256, NewFn);
222     if (Name == "x86.avx2.mpsadbw")
223       return UpgradeX86IntrinsicsWith8BitMask(F, Intrinsic::x86_avx2_mpsadbw,
224                                               NewFn);
225
226     if (Name == "x86.avx512.mask.cmp.ps.512")
227       return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_cmp_ps_512,
228                                        NewFn);
229     if (Name == "x86.avx512.mask.cmp.pd.512")
230       return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_cmp_pd_512,
231                                        NewFn);
232
233     if (Name == "x86.avx512.mask.cmp.b.512")
234       return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_cmp_b_512,
235                                        NewFn);
236     if (Name == "x86.avx512.mask.cmp.w.512")
237       return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_cmp_w_512,
238                                        NewFn);
239     if (Name == "x86.avx512.mask.cmp.d.512")
240       return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_cmp_d_512,
241                                        NewFn);
242     if (Name == "x86.avx512.mask.cmp.q.512")
243       return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_cmp_q_512,
244                                        NewFn);
245     if (Name == "x86.avx512.mask.ucmp.b.512")
246       return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_ucmp_b_512,
247                                        NewFn);
248     if (Name == "x86.avx512.mask.ucmp.w.512")
249       return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_ucmp_w_512,
250                                        NewFn);
251     if (Name == "x86.avx512.mask.ucmp.d.512")
252       return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_ucmp_d_512,
253                                        NewFn);
254     if (Name == "x86.avx512.mask.ucmp.q.512")
255       return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_ucmp_q_512,
256                                        NewFn);
257
258     if (Name == "x86.avx512.mask.cmp.b.256")
259       return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_cmp_b_256,
260                                        NewFn);
261     if (Name == "x86.avx512.mask.cmp.w.256")
262       return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_cmp_w_256,
263                                        NewFn);
264     if (Name == "x86.avx512.mask.cmp.d.256")
265       return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_cmp_d_256,
266                                        NewFn);
267     if (Name == "x86.avx512.mask.cmp.q.256")
268       return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_cmp_q_256,
269                                        NewFn);
270     if (Name == "x86.avx512.mask.ucmp.b.256")
271       return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_ucmp_b_256,
272                                        NewFn);
273     if (Name == "x86.avx512.mask.ucmp.w.256")
274       return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_ucmp_w_256,
275                                        NewFn);
276     if (Name == "x86.avx512.mask.ucmp.d.256")
277       return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_ucmp_d_256,
278                                        NewFn);
279     if (Name == "x86.avx512.mask.ucmp.q.256")
280       return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_ucmp_q_256,
281                                        NewFn);
282
283     if (Name == "x86.avx512.mask.cmp.b.128")
284       return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_cmp_b_128,
285                                        NewFn);
286     if (Name == "x86.avx512.mask.cmp.w.128")
287       return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_cmp_w_128,
288                                        NewFn);
289     if (Name == "x86.avx512.mask.cmp.d.128")
290       return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_cmp_d_128,
291                                        NewFn);
292     if (Name == "x86.avx512.mask.cmp.q.128")
293       return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_cmp_q_128,
294                                        NewFn);
295     if (Name == "x86.avx512.mask.ucmp.b.128")
296       return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_ucmp_b_128,
297                                        NewFn);
298     if (Name == "x86.avx512.mask.ucmp.w.128")
299       return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_ucmp_w_128,
300                                        NewFn);
301     if (Name == "x86.avx512.mask.ucmp.d.128")
302       return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_ucmp_d_128,
303                                        NewFn);
304     if (Name == "x86.avx512.mask.ucmp.q.128")
305       return UpgradeAVX512CmpIntrinsic(F, Intrinsic::x86_avx512_mask_ucmp_q_128,
306                                        NewFn);
307
308     // frcz.ss/sd may need to have an argument dropped
309     if (Name.startswith("x86.xop.vfrcz.ss") && F->arg_size() == 2) {
310       F->setName(Name + ".old");
311       NewFn = Intrinsic::getDeclaration(F->getParent(),
312                                         Intrinsic::x86_xop_vfrcz_ss);
313       return true;
314     }
315     if (Name.startswith("x86.xop.vfrcz.sd") && F->arg_size() == 2) {
316       F->setName(Name + ".old");
317       NewFn = Intrinsic::getDeclaration(F->getParent(),
318                                         Intrinsic::x86_xop_vfrcz_sd);
319       return true;
320     }
321     // Fix the FMA4 intrinsics to remove the 4
322     if (Name.startswith("x86.fma4.")) {
323       F->setName("llvm.x86.fma" + Name.substr(8));
324       NewFn = F;
325       return true;
326     }
327     break;
328   }
329   }
330
331   //  This may not belong here. This function is effectively being overloaded
332   //  to both detect an intrinsic which needs upgrading, and to provide the
333   //  upgraded form of the intrinsic. We should perhaps have two separate
334   //  functions for this.
335   return false;
336 }
337
338 bool llvm::UpgradeIntrinsicFunction(Function *F, Function *&NewFn) {
339   NewFn = nullptr;
340   bool Upgraded = UpgradeIntrinsicFunction1(F, NewFn);
341
342   // Upgrade intrinsic attributes.  This does not change the function.
343   if (NewFn)
344     F = NewFn;
345   if (unsigned id = F->getIntrinsicID())
346     F->setAttributes(Intrinsic::getAttributes(F->getContext(),
347                                               (Intrinsic::ID)id));
348   return Upgraded;
349 }
350
351 bool llvm::UpgradeGlobalVariable(GlobalVariable *GV) {
352   // Nothing to do yet.
353   return false;
354 }
355
356 static MDNode *getNodeField(const MDNode *DbgNode, unsigned Elt) {
357   if (!DbgNode || Elt >= DbgNode->getNumOperands())
358     return nullptr;
359   return dyn_cast_or_null<MDNode>(DbgNode->getOperand(Elt));
360 }
361
362 static MetadataAsValue *getExpression(Value *VarOperand, Function *F) {
363   // Old-style DIVariables have an optional expression as the 8th element.
364   DIExpression Expr(getNodeField(
365       cast<MDNode>(cast<MetadataAsValue>(VarOperand)->getMetadata()), 8));
366   if (!Expr) {
367     DIBuilder DIB(*F->getParent(), /*AllowUnresolved*/ false);
368     Expr = DIB.createExpression();
369   }
370   return MetadataAsValue::get(F->getContext(), Expr);
371 }
372
373 // UpgradeIntrinsicCall - Upgrade a call to an old intrinsic to be a call the
374 // upgraded intrinsic. All argument and return casting must be provided in
375 // order to seamlessly integrate with existing context.
376 void llvm::UpgradeIntrinsicCall(CallInst *CI, Function *NewFn) {
377   Function *F = CI->getCalledFunction();
378   LLVMContext &C = CI->getContext();
379   IRBuilder<> Builder(C);
380   Builder.SetInsertPoint(CI->getParent(), CI);
381
382   assert(F && "Intrinsic call is not direct?");
383
384   if (!NewFn) {
385     // Get the Function's name.
386     StringRef Name = F->getName();
387
388     Value *Rep;
389     // Upgrade packed integer vector compares intrinsics to compare instructions
390     if (Name.startswith("llvm.x86.sse2.pcmpeq.") ||
391         Name.startswith("llvm.x86.avx2.pcmpeq.")) {
392       Rep = Builder.CreateICmpEQ(CI->getArgOperand(0), CI->getArgOperand(1),
393                                  "pcmpeq");
394       // need to sign extend since icmp returns vector of i1
395       Rep = Builder.CreateSExt(Rep, CI->getType(), "");
396     } else if (Name.startswith("llvm.x86.sse2.pcmpgt.") ||
397                Name.startswith("llvm.x86.avx2.pcmpgt.")) {
398       Rep = Builder.CreateICmpSGT(CI->getArgOperand(0), CI->getArgOperand(1),
399                                   "pcmpgt");
400       // need to sign extend since icmp returns vector of i1
401       Rep = Builder.CreateSExt(Rep, CI->getType(), "");
402     } else if (Name == "llvm.x86.avx.movnt.dq.256" ||
403                Name == "llvm.x86.avx.movnt.ps.256" ||
404                Name == "llvm.x86.avx.movnt.pd.256") {
405       IRBuilder<> Builder(C);
406       Builder.SetInsertPoint(CI->getParent(), CI);
407
408       Module *M = F->getParent();
409       SmallVector<Metadata *, 1> Elts;
410       Elts.push_back(
411           ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(C), 1)));
412       MDNode *Node = MDNode::get(C, Elts);
413
414       Value *Arg0 = CI->getArgOperand(0);
415       Value *Arg1 = CI->getArgOperand(1);
416
417       // Convert the type of the pointer to a pointer to the stored type.
418       Value *BC = Builder.CreateBitCast(Arg0,
419                                         PointerType::getUnqual(Arg1->getType()),
420                                         "cast");
421       StoreInst *SI = Builder.CreateStore(Arg1, BC);
422       SI->setMetadata(M->getMDKindID("nontemporal"), Node);
423       SI->setAlignment(16);
424
425       // Remove intrinsic.
426       CI->eraseFromParent();
427       return;
428     } else if (Name.startswith("llvm.x86.xop.vpcom")) {
429       Intrinsic::ID intID;
430       if (Name.endswith("ub"))
431         intID = Intrinsic::x86_xop_vpcomub;
432       else if (Name.endswith("uw"))
433         intID = Intrinsic::x86_xop_vpcomuw;
434       else if (Name.endswith("ud"))
435         intID = Intrinsic::x86_xop_vpcomud;
436       else if (Name.endswith("uq"))
437         intID = Intrinsic::x86_xop_vpcomuq;
438       else if (Name.endswith("b"))
439         intID = Intrinsic::x86_xop_vpcomb;
440       else if (Name.endswith("w"))
441         intID = Intrinsic::x86_xop_vpcomw;
442       else if (Name.endswith("d"))
443         intID = Intrinsic::x86_xop_vpcomd;
444       else if (Name.endswith("q"))
445         intID = Intrinsic::x86_xop_vpcomq;
446       else
447         llvm_unreachable("Unknown suffix");
448
449       Name = Name.substr(18); // strip off "llvm.x86.xop.vpcom"
450       unsigned Imm;
451       if (Name.startswith("lt"))
452         Imm = 0;
453       else if (Name.startswith("le"))
454         Imm = 1;
455       else if (Name.startswith("gt"))
456         Imm = 2;
457       else if (Name.startswith("ge"))
458         Imm = 3;
459       else if (Name.startswith("eq"))
460         Imm = 4;
461       else if (Name.startswith("ne"))
462         Imm = 5;
463       else if (Name.startswith("false"))
464         Imm = 6;
465       else if (Name.startswith("true"))
466         Imm = 7;
467       else
468         llvm_unreachable("Unknown condition");
469
470       Function *VPCOM = Intrinsic::getDeclaration(F->getParent(), intID);
471       Rep = Builder.CreateCall3(VPCOM, CI->getArgOperand(0),
472                                 CI->getArgOperand(1), Builder.getInt8(Imm));
473     } else if (Name == "llvm.x86.sse42.crc32.64.8") {
474       Function *CRC32 = Intrinsic::getDeclaration(F->getParent(),
475                                                Intrinsic::x86_sse42_crc32_32_8);
476       Value *Trunc0 = Builder.CreateTrunc(CI->getArgOperand(0), Type::getInt32Ty(C));
477       Rep = Builder.CreateCall2(CRC32, Trunc0, CI->getArgOperand(1));
478       Rep = Builder.CreateZExt(Rep, CI->getType(), "");
479     } else if (Name.startswith("llvm.x86.avx.vbroadcast")) {
480       // Replace broadcasts with a series of insertelements.
481       Type *VecTy = CI->getType();
482       Type *EltTy = VecTy->getVectorElementType();
483       unsigned EltNum = VecTy->getVectorNumElements();
484       Value *Cast = Builder.CreateBitCast(CI->getArgOperand(0),
485                                           EltTy->getPointerTo());
486       Value *Load = Builder.CreateLoad(Cast);
487       Type *I32Ty = Type::getInt32Ty(C);
488       Rep = UndefValue::get(VecTy);
489       for (unsigned I = 0; I < EltNum; ++I)
490         Rep = Builder.CreateInsertElement(Rep, Load,
491                                           ConstantInt::get(I32Ty, I));
492     } else if (Name == "llvm.x86.sse2.psll.dq.bs") {
493       Value *Op0 = ConstantVector::getSplat(16, Builder.getInt8(0));
494       Value *Op1 = Builder.CreateBitCast(CI->getArgOperand(0),
495                                          VectorType::get(Type::getInt8Ty(C),16),
496                                          "cast");
497
498       unsigned Shift = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue();
499
500       if (Shift < 16) {
501         SmallVector<Constant*, 16> Idxs;
502         for (unsigned i = 16; i != 32; ++i)
503           Idxs.push_back(Builder.getInt32(i - Shift));
504
505         Op0 = Builder.CreateShuffleVector(Op0, Op1, ConstantVector::get(Idxs));
506       }
507
508       Rep = Builder.CreateBitCast(Op0,
509                                   VectorType::get(Type::getInt64Ty(C), 2),
510                                   "cast");
511     } else if (Name == "llvm.x86.sse2.psrl.dq.bs") {
512       Value *Op0 = Builder.CreateBitCast(CI->getArgOperand(0),
513                                          VectorType::get(Type::getInt8Ty(C),16),
514                                          "cast");
515       Value *Op1 = ConstantVector::getSplat(16, Builder.getInt8(0));
516
517       unsigned Shift = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue();
518
519       if (Shift < 16) {
520         SmallVector<Constant*, 16> Idxs;
521         for (unsigned i = 0; i != 16; ++i)
522           Idxs.push_back(Builder.getInt32(i + Shift));
523
524         Op1 = Builder.CreateShuffleVector(Op0, Op1, ConstantVector::get(Idxs));
525       }
526       Rep = Builder.CreateBitCast(Op1,
527                                   VectorType::get(Type::getInt64Ty(C), 2),
528                                   "cast");
529     } else {
530       bool PD128 = false, PD256 = false, PS128 = false, PS256 = false;
531       if (Name == "llvm.x86.avx.vpermil.pd.256")
532         PD256 = true;
533       else if (Name == "llvm.x86.avx.vpermil.pd")
534         PD128 = true;
535       else if (Name == "llvm.x86.avx.vpermil.ps.256")
536         PS256 = true;
537       else if (Name == "llvm.x86.avx.vpermil.ps")
538         PS128 = true;
539
540       if (PD256 || PD128 || PS256 || PS128) {
541         Value *Op0 = CI->getArgOperand(0);
542         unsigned Imm = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue();
543         SmallVector<Constant*, 8> Idxs;
544
545         if (PD128)
546           for (unsigned i = 0; i != 2; ++i)
547             Idxs.push_back(Builder.getInt32((Imm >> i) & 0x1));
548         else if (PD256)
549           for (unsigned l = 0; l != 4; l+=2)
550             for (unsigned i = 0; i != 2; ++i)
551               Idxs.push_back(Builder.getInt32(((Imm >> (l+i)) & 0x1) + l));
552         else if (PS128)
553           for (unsigned i = 0; i != 4; ++i)
554             Idxs.push_back(Builder.getInt32((Imm >> (2 * i)) & 0x3));
555         else if (PS256)
556           for (unsigned l = 0; l != 8; l+=4)
557             for (unsigned i = 0; i != 4; ++i)
558               Idxs.push_back(Builder.getInt32(((Imm >> (2 * i)) & 0x3) + l));
559         else
560           llvm_unreachable("Unexpected function");
561
562         Rep = Builder.CreateShuffleVector(Op0, Op0, ConstantVector::get(Idxs));
563       } else {
564         llvm_unreachable("Unknown function for CallInst upgrade.");
565       }
566     }
567
568     CI->replaceAllUsesWith(Rep);
569     CI->eraseFromParent();
570     return;
571   }
572
573   std::string Name = CI->getName().str();
574   if (!Name.empty())
575     CI->setName(Name + ".old");
576
577   switch (NewFn->getIntrinsicID()) {
578   default:
579     llvm_unreachable("Unknown function for CallInst upgrade.");
580
581   // Upgrade debug intrinsics to use an additional DIExpression argument.
582   case Intrinsic::dbg_declare: {
583     auto NewCI =
584         Builder.CreateCall3(NewFn, CI->getArgOperand(0), CI->getArgOperand(1),
585                             getExpression(CI->getArgOperand(1), F), Name);
586     NewCI->setDebugLoc(CI->getDebugLoc());
587     CI->replaceAllUsesWith(NewCI);
588     CI->eraseFromParent();
589     return;
590   }
591   case Intrinsic::dbg_value: {
592     auto NewCI = Builder.CreateCall4(
593         NewFn, CI->getArgOperand(0), CI->getArgOperand(1), CI->getArgOperand(2),
594         getExpression(CI->getArgOperand(2), F), Name);
595     NewCI->setDebugLoc(CI->getDebugLoc());
596     CI->replaceAllUsesWith(NewCI);
597     CI->eraseFromParent();
598     return;
599   }
600   case Intrinsic::ctlz:
601   case Intrinsic::cttz:
602     assert(CI->getNumArgOperands() == 1 &&
603            "Mismatch between function args and call args");
604     CI->replaceAllUsesWith(Builder.CreateCall2(NewFn, CI->getArgOperand(0),
605                                                Builder.getFalse(), Name));
606     CI->eraseFromParent();
607     return;
608
609   case Intrinsic::objectsize:
610     CI->replaceAllUsesWith(Builder.CreateCall2(NewFn,
611                                                CI->getArgOperand(0),
612                                                CI->getArgOperand(1),
613                                                Name));
614     CI->eraseFromParent();
615     return;
616
617   case Intrinsic::ctpop: {
618     CI->replaceAllUsesWith(Builder.CreateCall(NewFn, CI->getArgOperand(0)));
619     CI->eraseFromParent();
620     return;
621   }
622
623   case Intrinsic::x86_xop_vfrcz_ss:
624   case Intrinsic::x86_xop_vfrcz_sd:
625     CI->replaceAllUsesWith(Builder.CreateCall(NewFn, CI->getArgOperand(1),
626                                               Name));
627     CI->eraseFromParent();
628     return;
629
630   case Intrinsic::x86_sse41_ptestc:
631   case Intrinsic::x86_sse41_ptestz:
632   case Intrinsic::x86_sse41_ptestnzc: {
633     // The arguments for these intrinsics used to be v4f32, and changed
634     // to v2i64. This is purely a nop, since those are bitwise intrinsics.
635     // So, the only thing required is a bitcast for both arguments.
636     // First, check the arguments have the old type.
637     Value *Arg0 = CI->getArgOperand(0);
638     if (Arg0->getType() != VectorType::get(Type::getFloatTy(C), 4))
639       return;
640
641     // Old intrinsic, add bitcasts
642     Value *Arg1 = CI->getArgOperand(1);
643
644     Value *BC0 =
645       Builder.CreateBitCast(Arg0,
646                             VectorType::get(Type::getInt64Ty(C), 2),
647                             "cast");
648     Value *BC1 =
649       Builder.CreateBitCast(Arg1,
650                             VectorType::get(Type::getInt64Ty(C), 2),
651                             "cast");
652
653     CallInst* NewCall = Builder.CreateCall2(NewFn, BC0, BC1, Name);
654     CI->replaceAllUsesWith(NewCall);
655     CI->eraseFromParent();
656     return;
657   }
658
659   case Intrinsic::x86_sse41_pblendw:
660   case Intrinsic::x86_sse41_blendpd:
661   case Intrinsic::x86_sse41_blendps:
662   case Intrinsic::x86_sse41_insertps:
663   case Intrinsic::x86_sse41_dppd:
664   case Intrinsic::x86_sse41_dpps:
665   case Intrinsic::x86_sse41_mpsadbw:
666   case Intrinsic::x86_avx_blend_pd_256:
667   case Intrinsic::x86_avx_blend_ps_256:
668   case Intrinsic::x86_avx_dp_ps_256:
669   case Intrinsic::x86_avx2_pblendw:
670   case Intrinsic::x86_avx2_pblendd_128:
671   case Intrinsic::x86_avx2_pblendd_256:
672   case Intrinsic::x86_avx2_mpsadbw: {
673     // Need to truncate the last argument from i32 to i8 -- this argument models
674     // an inherently 8-bit immediate operand to these x86 instructions.
675     SmallVector<Value *, 4> Args(CI->arg_operands().begin(),
676                                  CI->arg_operands().end());
677
678     // Replace the last argument with a trunc.
679     Args.back() = Builder.CreateTrunc(Args.back(), Type::getInt8Ty(C), "trunc");
680
681     CallInst *NewCall = Builder.CreateCall(NewFn, Args);
682     CI->replaceAllUsesWith(NewCall);
683     CI->eraseFromParent();
684     return;
685   }
686   case Intrinsic::x86_avx512_mask_cmp_ps_512:
687   case Intrinsic::x86_avx512_mask_cmp_pd_512: {
688     // Need to truncate the last argument from i32 to i8 -- this argument models
689     // an inherently 8-bit immediate operand to these x86 instructions.
690     SmallVector<Value *, 5> Args(CI->arg_operands().begin(),
691                                  CI->arg_operands().end());
692
693     // Replace the last argument with a trunc.
694     Args[2] = Builder.CreateTrunc(Args[2], Type::getInt8Ty(C), "trunc");
695
696     CallInst *NewCall = Builder.CreateCall(NewFn, Args);
697     CI->replaceAllUsesWith(NewCall);
698     CI->eraseFromParent();
699     return;
700   }
701   }
702 }
703
704 // This tests each Function to determine if it needs upgrading. When we find
705 // one we are interested in, we then upgrade all calls to reflect the new
706 // function.
707 void llvm::UpgradeCallsToIntrinsic(Function* F) {
708   assert(F && "Illegal attempt to upgrade a non-existent intrinsic.");
709
710   // Upgrade the function and check if it is a totaly new function.
711   Function *NewFn;
712   if (UpgradeIntrinsicFunction(F, NewFn)) {
713     if (NewFn != F) {
714       // Replace all uses to the old function with the new one if necessary.
715       for (Value::user_iterator UI = F->user_begin(), UE = F->user_end();
716            UI != UE; ) {
717         if (CallInst *CI = dyn_cast<CallInst>(*UI++))
718           UpgradeIntrinsicCall(CI, NewFn);
719       }
720       // Remove old function, no longer used, from the module.
721       F->eraseFromParent();
722     }
723   }
724 }
725
726 void llvm::UpgradeInstWithTBAATag(Instruction *I) {
727   MDNode *MD = I->getMetadata(LLVMContext::MD_tbaa);
728   assert(MD && "UpgradeInstWithTBAATag should have a TBAA tag");
729   // Check if the tag uses struct-path aware TBAA format.
730   if (isa<MDNode>(MD->getOperand(0)) && MD->getNumOperands() >= 3)
731     return;
732
733   if (MD->getNumOperands() == 3) {
734     Metadata *Elts[] = {MD->getOperand(0), MD->getOperand(1)};
735     MDNode *ScalarType = MDNode::get(I->getContext(), Elts);
736     // Create a MDNode <ScalarType, ScalarType, offset 0, const>
737     Metadata *Elts2[] = {ScalarType, ScalarType,
738                          ConstantAsMetadata::get(Constant::getNullValue(
739                              Type::getInt64Ty(I->getContext()))),
740                          MD->getOperand(2)};
741     I->setMetadata(LLVMContext::MD_tbaa, MDNode::get(I->getContext(), Elts2));
742   } else {
743     // Create a MDNode <MD, MD, offset 0>
744     Metadata *Elts[] = {MD, MD, ConstantAsMetadata::get(Constant::getNullValue(
745                                     Type::getInt64Ty(I->getContext())))};
746     I->setMetadata(LLVMContext::MD_tbaa, MDNode::get(I->getContext(), Elts));
747   }
748 }
749
750 Instruction *llvm::UpgradeBitCastInst(unsigned Opc, Value *V, Type *DestTy,
751                                       Instruction *&Temp) {
752   if (Opc != Instruction::BitCast)
753     return nullptr;
754
755   Temp = nullptr;
756   Type *SrcTy = V->getType();
757   if (SrcTy->isPtrOrPtrVectorTy() && DestTy->isPtrOrPtrVectorTy() &&
758       SrcTy->getPointerAddressSpace() != DestTy->getPointerAddressSpace()) {
759     LLVMContext &Context = V->getContext();
760
761     // We have no information about target data layout, so we assume that
762     // the maximum pointer size is 64bit.
763     Type *MidTy = Type::getInt64Ty(Context);
764     Temp = CastInst::Create(Instruction::PtrToInt, V, MidTy);
765
766     return CastInst::Create(Instruction::IntToPtr, Temp, DestTy);
767   }
768
769   return nullptr;
770 }
771
772 Value *llvm::UpgradeBitCastExpr(unsigned Opc, Constant *C, Type *DestTy) {
773   if (Opc != Instruction::BitCast)
774     return nullptr;
775
776   Type *SrcTy = C->getType();
777   if (SrcTy->isPtrOrPtrVectorTy() && DestTy->isPtrOrPtrVectorTy() &&
778       SrcTy->getPointerAddressSpace() != DestTy->getPointerAddressSpace()) {
779     LLVMContext &Context = C->getContext();
780
781     // We have no information about target data layout, so we assume that
782     // the maximum pointer size is 64bit.
783     Type *MidTy = Type::getInt64Ty(Context);
784
785     return ConstantExpr::getIntToPtr(ConstantExpr::getPtrToInt(C, MidTy),
786                                      DestTy);
787   }
788
789   return nullptr;
790 }
791
792 /// Check the debug info version number, if it is out-dated, drop the debug
793 /// info. Return true if module is modified.
794 bool llvm::UpgradeDebugInfo(Module &M) {
795   unsigned Version = getDebugMetadataVersionFromModule(M);
796   if (Version == DEBUG_METADATA_VERSION)
797     return false;
798
799   bool RetCode = StripDebugInfo(M);
800   if (RetCode) {
801     DiagnosticInfoDebugMetadataVersion DiagVersion(M, Version);
802     M.getContext().diagnose(DiagVersion);
803   }
804   return RetCode;
805 }
806
807 void llvm::UpgradeMDStringConstant(std::string &String) {
808   const std::string OldPrefix = "llvm.vectorizer.";
809   if (String == "llvm.vectorizer.unroll") {
810     String = "llvm.loop.interleave.count";
811   } else if (String.find(OldPrefix) == 0) {
812     String.replace(0, OldPrefix.size(), "llvm.loop.vectorize.");
813   }
814 }