LCOV - code coverage report
Current view: top level - lib/IR - AutoUpgrade.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 1319 1379 95.6 %
Date: 2018-05-20 00:06:23 Functions: 40 40 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       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             : // This is where deprecated IR intrinsics and other IR features are updated to
      12             : // current specifications.
      13             : //
      14             : //===----------------------------------------------------------------------===//
      15             : 
      16             : #include "llvm/IR/AutoUpgrade.h"
      17             : #include "llvm/ADT/StringSwitch.h"
      18             : #include "llvm/IR/Constants.h"
      19             : #include "llvm/IR/DIBuilder.h"
      20             : #include "llvm/IR/DebugInfo.h"
      21             : #include "llvm/IR/DiagnosticInfo.h"
      22             : #include "llvm/IR/Function.h"
      23             : #include "llvm/IR/IRBuilder.h"
      24             : #include "llvm/IR/Instruction.h"
      25             : #include "llvm/IR/IntrinsicInst.h"
      26             : #include "llvm/IR/LLVMContext.h"
      27             : #include "llvm/IR/Module.h"
      28             : #include "llvm/IR/Verifier.h"
      29             : #include "llvm/Support/ErrorHandling.h"
      30             : #include "llvm/Support/Regex.h"
      31             : #include <cstring>
      32             : using namespace llvm;
      33             : 
      34        1046 : static void rename(GlobalValue *GV) { GV->setName(GV->getName() + ".old"); }
      35             : 
      36             : // Upgrade the declarations of the SSE4.1 ptest intrinsics whose arguments have
      37             : // changed their type from v4f32 to v2i64.
      38         178 : static bool UpgradePTESTIntrinsic(Function* F, Intrinsic::ID IID,
      39             :                                   Function *&NewFn) {
      40             :   // Check whether this is an old version of the function, which received
      41             :   // v4f32 arguments.
      42         178 :   Type *Arg0Type = F->getFunctionType()->getParamType(0);
      43         178 :   if (Arg0Type != VectorType::get(Type::getFloatTy(F->getContext()), 4))
      44             :     return false;
      45             : 
      46             :   // Yes, it's old, replace it with new version.
      47           8 :   rename(F);
      48           8 :   NewFn = Intrinsic::getDeclaration(F->getParent(), IID);
      49           8 :   return true;
      50             : }
      51             : 
      52             : // Upgrade the declarations of intrinsic functions whose 8-bit immediate mask
      53             : // arguments have changed their type from i32 to i8.
      54         165 : static bool UpgradeX86IntrinsicsWith8BitMask(Function *F, Intrinsic::ID IID,
      55             :                                              Function *&NewFn) {
      56             :   // Check that the last argument is an i32.
      57             :   Type *LastArgType = F->getFunctionType()->getParamType(
      58         330 :      F->getFunctionType()->getNumParams() - 1);
      59         165 :   if (!LastArgType->isIntegerTy(32))
      60             :     return false;
      61             : 
      62             :   // Move this function aside and map down.
      63          15 :   rename(F);
      64             :   NewFn = Intrinsic::getDeclaration(F->getParent(), IID);
      65          15 :   return true;
      66             : }
      67             : 
      68             : // Upgrade the declaration of fp compare intrinsics that change return type
      69             : // from scalar to vXi1 mask.
      70          35 : static bool UpgradeX86MaskedFPCompare(Function *F, Intrinsic::ID IID,
      71             :                                       Function *&NewFn) {
      72             :   // Check if the return type is a vector.
      73          35 :   if (F->getReturnType()->isVectorTy())
      74             :     return false;
      75             : 
      76           6 :   rename(F);
      77           6 :   NewFn = Intrinsic::getDeclaration(F->getParent(), IID);
      78           6 :   return true;
      79             : }
      80             : 
      81       12619 : static bool ShouldUpgradeX86Intrinsic(Function *F, StringRef Name) {
      82             :   // All of the intrinsics matches below should be marked with which llvm
      83             :   // version started autoupgrading them. At some point in the future we would
      84             :   // like to use this information to remove upgrade code for some older
      85             :   // intrinsics. It is currently undecided how we will determine that future
      86             :   // point.
      87             :   if (Name=="ssse3.pabs.b.128" || // Added in 6.0
      88             :       Name=="ssse3.pabs.w.128" || // Added in 6.0
      89             :       Name=="ssse3.pabs.d.128" || // Added in 6.0
      90             :       Name.startswith("fma.vfmsub.") || // Added in 7.0
      91             :       Name.startswith("fma.vfmsubadd.") || // Added in 7.0
      92             :       Name.startswith("fma.vfnmadd.") || // Added in 7.0
      93             :       Name.startswith("fma.vfnmsub.") || // Added in 7.0
      94             :       Name.startswith("avx512.mask.shuf.i") || // Added in 6.0
      95             :       Name.startswith("avx512.mask.shuf.f") || // Added in 6.0
      96             :       Name.startswith("avx512.kunpck") || //added in 6.0 
      97             :       Name.startswith("avx2.pabs.") || // Added in 6.0
      98             :       Name.startswith("avx512.mask.pabs.") || // Added in 6.0
      99             :       Name.startswith("avx512.broadcastm") || // Added in 6.0
     100             :       Name.startswith("avx512.mask.pbroadcast") || // Added in 6.0
     101             :       Name.startswith("sse2.pcmpeq.") || // Added in 3.1
     102             :       Name.startswith("sse2.pcmpgt.") || // Added in 3.1
     103             :       Name.startswith("avx2.pcmpeq.") || // Added in 3.1
     104             :       Name.startswith("avx2.pcmpgt.") || // Added in 3.1
     105             :       Name.startswith("avx512.mask.pcmpeq.") || // Added in 3.9
     106             :       Name.startswith("avx512.mask.pcmpgt.") || // Added in 3.9
     107             :       Name.startswith("avx.vperm2f128.") || // Added in 6.0
     108             :       Name == "avx2.vperm2i128" || // Added in 6.0
     109             :       Name == "sse.add.ss" || // Added in 4.0
     110             :       Name == "sse2.add.sd" || // Added in 4.0
     111             :       Name == "sse.sub.ss" || // Added in 4.0
     112             :       Name == "sse2.sub.sd" || // Added in 4.0
     113             :       Name == "sse.mul.ss" || // Added in 4.0
     114             :       Name == "sse2.mul.sd" || // Added in 4.0
     115             :       Name == "sse.div.ss" || // Added in 4.0
     116             :       Name == "sse2.div.sd" || // Added in 4.0
     117             :       Name == "sse41.pmaxsb" || // Added in 3.9
     118             :       Name == "sse2.pmaxs.w" || // Added in 3.9
     119             :       Name == "sse41.pmaxsd" || // Added in 3.9
     120             :       Name == "sse2.pmaxu.b" || // Added in 3.9
     121             :       Name == "sse41.pmaxuw" || // Added in 3.9
     122             :       Name == "sse41.pmaxud" || // Added in 3.9
     123             :       Name == "sse41.pminsb" || // Added in 3.9
     124             :       Name == "sse2.pmins.w" || // Added in 3.9
     125             :       Name == "sse41.pminsd" || // Added in 3.9
     126             :       Name == "sse2.pminu.b" || // Added in 3.9
     127             :       Name == "sse41.pminuw" || // Added in 3.9
     128             :       Name == "sse41.pminud" || // Added in 3.9
     129             :       Name == "avx512.kand.w" || // Added in 7.0
     130             :       Name == "avx512.kandn.w" || // Added in 7.0
     131             :       Name == "avx512.knot.w" || // Added in 7.0
     132             :       Name == "avx512.kor.w" || // Added in 7.0
     133             :       Name == "avx512.kxor.w" || // Added in 7.0
     134             :       Name == "avx512.kxnor.w" || // Added in 7.0
     135             :       Name == "avx512.kortestc.w" || // Added in 7.0
     136             :       Name == "avx512.kortestz.w" || // Added in 7.0
     137             :       Name.startswith("avx512.mask.pshuf.b.") || // Added in 4.0
     138             :       Name.startswith("avx2.pmax") || // Added in 3.9
     139             :       Name.startswith("avx2.pmin") || // Added in 3.9
     140             :       Name.startswith("avx512.mask.pmax") || // Added in 4.0
     141             :       Name.startswith("avx512.mask.pmin") || // Added in 4.0
     142             :       Name.startswith("avx2.vbroadcast") || // Added in 3.8
     143             :       Name.startswith("avx2.pbroadcast") || // Added in 3.8
     144             :       Name.startswith("avx.vpermil.") || // Added in 3.1
     145             :       Name.startswith("sse2.pshuf") || // Added in 3.9
     146             :       Name.startswith("avx512.pbroadcast") || // Added in 3.9
     147             :       Name.startswith("avx512.mask.broadcast.s") || // Added in 3.9
     148             :       Name.startswith("avx512.mask.movddup") || // Added in 3.9
     149             :       Name.startswith("avx512.mask.movshdup") || // Added in 3.9
     150             :       Name.startswith("avx512.mask.movsldup") || // Added in 3.9
     151             :       Name.startswith("avx512.mask.pshuf.d.") || // Added in 3.9
     152             :       Name.startswith("avx512.mask.pshufl.w.") || // Added in 3.9
     153             :       Name.startswith("avx512.mask.pshufh.w.") || // Added in 3.9
     154             :       Name.startswith("avx512.mask.shuf.p") || // Added in 4.0
     155             :       Name.startswith("avx512.mask.vpermil.p") || // Added in 3.9
     156             :       Name.startswith("avx512.mask.perm.df.") || // Added in 3.9
     157             :       Name.startswith("avx512.mask.perm.di.") || // Added in 3.9
     158             :       Name.startswith("avx512.mask.punpckl") || // Added in 3.9
     159             :       Name.startswith("avx512.mask.punpckh") || // Added in 3.9
     160             :       Name.startswith("avx512.mask.unpckl.") || // Added in 3.9
     161             :       Name.startswith("avx512.mask.unpckh.") || // Added in 3.9
     162             :       Name.startswith("avx512.mask.pand.") || // Added in 3.9
     163             :       Name.startswith("avx512.mask.pandn.") || // Added in 3.9
     164             :       Name.startswith("avx512.mask.por.") || // Added in 3.9
     165             :       Name.startswith("avx512.mask.pxor.") || // Added in 3.9
     166             :       Name.startswith("avx512.mask.and.") || // Added in 3.9
     167             :       Name.startswith("avx512.mask.andn.") || // Added in 3.9
     168             :       Name.startswith("avx512.mask.or.") || // Added in 3.9
     169             :       Name.startswith("avx512.mask.xor.") || // Added in 3.9
     170             :       Name.startswith("avx512.mask.padd.") || // Added in 4.0
     171             :       Name.startswith("avx512.mask.psub.") || // Added in 4.0
     172             :       Name.startswith("avx512.mask.pmull.") || // Added in 4.0
     173             :       Name.startswith("avx512.mask.cvtdq2pd.") || // Added in 4.0
     174             :       Name.startswith("avx512.mask.cvtudq2pd.") || // Added in 4.0
     175             :       Name == "avx512.mask.cvtdq2ps.128" || // Added in 7.0
     176             :       Name == "avx512.mask.cvtdq2ps.256" || // Added in 7.0
     177             :       Name == "avx512.mask.cvtpd2dq.256" || // Added in 7.0
     178             :       Name == "avx512.mask.cvtpd2ps.256" || // Added in 7.0
     179             :       Name == "avx512.mask.cvttpd2dq.256" || // Added in 7.0
     180             :       Name == "avx512.mask.cvttps2dq.128" || // Added in 7.0
     181             :       Name == "avx512.mask.cvttps2dq.256" || // Added in 7.0
     182             :       Name == "avx512.mask.cvtps2pd.128" || // Added in 7.0
     183             :       Name == "avx512.mask.cvtps2pd.256" || // Added in 7.0
     184             :       Name == "avx512.cvtusi2sd" || // Added in 7.0
     185             :       Name == "avx512.mask.permvar.sf.256" || // Added in 7.0
     186             :       Name == "avx512.mask.permvar.si.256" || // Added in 7.0
     187             :       Name == "sse2.pmulu.dq" || // Added in 7.0
     188             :       Name == "sse41.pmuldq" || // Added in 7.0
     189             :       Name == "avx2.pmulu.dq" || // Added in 7.0
     190             :       Name == "avx2.pmul.dq" || // Added in 7.0
     191             :       Name == "avx512.pmulu.dq.512" || // Added in 7.0
     192             :       Name == "avx512.pmul.dq.512" || // Added in 7.0
     193             :       Name.startswith("avx512.mask.pmul.dq.") || // Added in 4.0
     194             :       Name.startswith("avx512.mask.pmulu.dq.") || // Added in 4.0
     195             :       Name.startswith("avx512.mask.pmul.hr.sw.") || // Added in 7.0
     196             :       Name.startswith("avx512.mask.pmulh.w.") || // Added in 7.0
     197             :       Name.startswith("avx512.mask.pmulhu.w.") || // Added in 7.0
     198             :       Name.startswith("avx512.mask.pmaddw.d.") || // Added in 7.0
     199             :       Name.startswith("avx512.mask.pmaddubs.w.") || // Added in 7.0
     200             :       Name.startswith("avx512.mask.packsswb.") || // Added in 5.0
     201             :       Name.startswith("avx512.mask.packssdw.") || // Added in 5.0
     202             :       Name.startswith("avx512.mask.packuswb.") || // Added in 5.0
     203             :       Name.startswith("avx512.mask.packusdw.") || // Added in 5.0
     204             :       Name.startswith("avx512.mask.cmp.b") || // Added in 5.0
     205             :       Name.startswith("avx512.mask.cmp.d") || // Added in 5.0
     206             :       Name.startswith("avx512.mask.cmp.q") || // Added in 5.0
     207             :       Name.startswith("avx512.mask.cmp.w") || // Added in 5.0
     208             :       Name.startswith("avx512.mask.ucmp.") || // Added in 5.0
     209             :       Name.startswith("avx512.cvtb2mask.") || // Added in 7.0
     210             :       Name.startswith("avx512.cvtw2mask.") || // Added in 7.0
     211             :       Name.startswith("avx512.cvtd2mask.") || // Added in 7.0
     212             :       Name.startswith("avx512.cvtq2mask.") || // Added in 7.0
     213             :       Name == "avx512.mask.add.pd.128" || // Added in 4.0
     214             :       Name == "avx512.mask.add.pd.256" || // Added in 4.0
     215             :       Name == "avx512.mask.add.ps.128" || // Added in 4.0
     216             :       Name == "avx512.mask.add.ps.256" || // Added in 4.0
     217             :       Name == "avx512.mask.div.pd.128" || // Added in 4.0
     218             :       Name == "avx512.mask.div.pd.256" || // Added in 4.0
     219             :       Name == "avx512.mask.div.ps.128" || // Added in 4.0
     220             :       Name == "avx512.mask.div.ps.256" || // Added in 4.0
     221             :       Name == "avx512.mask.mul.pd.128" || // Added in 4.0
     222             :       Name == "avx512.mask.mul.pd.256" || // Added in 4.0
     223             :       Name == "avx512.mask.mul.ps.128" || // Added in 4.0
     224             :       Name == "avx512.mask.mul.ps.256" || // Added in 4.0
     225             :       Name == "avx512.mask.sub.pd.128" || // Added in 4.0
     226             :       Name == "avx512.mask.sub.pd.256" || // Added in 4.0
     227             :       Name == "avx512.mask.sub.ps.128" || // Added in 4.0
     228             :       Name == "avx512.mask.sub.ps.256" || // Added in 4.0
     229             :       Name == "avx512.mask.max.pd.128" || // Added in 5.0
     230             :       Name == "avx512.mask.max.pd.256" || // Added in 5.0
     231             :       Name == "avx512.mask.max.ps.128" || // Added in 5.0
     232             :       Name == "avx512.mask.max.ps.256" || // Added in 5.0
     233             :       Name == "avx512.mask.min.pd.128" || // Added in 5.0
     234             :       Name == "avx512.mask.min.pd.256" || // Added in 5.0
     235             :       Name == "avx512.mask.min.ps.128" || // Added in 5.0
     236             :       Name == "avx512.mask.min.ps.256" || // Added in 5.0
     237             :       Name.startswith("avx512.mask.vpermilvar.") || // Added in 4.0
     238             :       Name.startswith("avx512.mask.psll.d") || // Added in 4.0
     239             :       Name.startswith("avx512.mask.psll.q") || // Added in 4.0
     240             :       Name.startswith("avx512.mask.psll.w") || // Added in 4.0
     241             :       Name.startswith("avx512.mask.psra.d") || // Added in 4.0
     242             :       Name.startswith("avx512.mask.psra.q") || // Added in 4.0
     243             :       Name.startswith("avx512.mask.psra.w") || // Added in 4.0
     244             :       Name.startswith("avx512.mask.psrl.d") || // Added in 4.0
     245             :       Name.startswith("avx512.mask.psrl.q") || // Added in 4.0
     246             :       Name.startswith("avx512.mask.psrl.w") || // Added in 4.0
     247             :       Name.startswith("avx512.mask.pslli") || // Added in 4.0
     248             :       Name.startswith("avx512.mask.psrai") || // Added in 4.0
     249             :       Name.startswith("avx512.mask.psrli") || // Added in 4.0
     250             :       Name.startswith("avx512.mask.psllv") || // Added in 4.0
     251             :       Name.startswith("avx512.mask.psrav") || // Added in 4.0
     252             :       Name.startswith("avx512.mask.psrlv") || // Added in 4.0
     253             :       Name.startswith("sse41.pmovsx") || // Added in 3.8
     254             :       Name.startswith("sse41.pmovzx") || // Added in 3.9
     255             :       Name.startswith("avx2.pmovsx") || // Added in 3.9
     256             :       Name.startswith("avx2.pmovzx") || // Added in 3.9
     257             :       Name.startswith("avx512.mask.pmovsx") || // Added in 4.0
     258             :       Name.startswith("avx512.mask.pmovzx") || // Added in 4.0
     259             :       Name.startswith("avx512.mask.lzcnt.") || // Added in 5.0
     260             :       Name == "sse.cvtsi2ss" || // Added in 7.0
     261             :       Name == "sse.cvtsi642ss" || // Added in 7.0
     262             :       Name == "sse2.cvtsi2sd" || // Added in 7.0
     263             :       Name == "sse2.cvtsi642sd" || // Added in 7.0
     264             :       Name == "sse2.cvtss2sd" || // Added in 7.0
     265             :       Name == "sse2.cvtdq2pd" || // Added in 3.9
     266             :       Name == "sse2.cvtps2pd" || // Added in 3.9
     267             :       Name == "avx.cvtdq2.pd.256" || // Added in 3.9
     268             :       Name == "avx.cvt.ps2.pd.256" || // Added in 3.9
     269             :       Name.startswith("avx.vinsertf128.") || // Added in 3.7
     270             :       Name == "avx2.vinserti128" || // Added in 3.7
     271             :       Name.startswith("avx512.mask.insert") || // Added in 4.0
     272             :       Name.startswith("avx.vextractf128.") || // Added in 3.7
     273             :       Name == "avx2.vextracti128" || // Added in 3.7
     274             :       Name.startswith("avx512.mask.vextract") || // Added in 4.0
     275             :       Name.startswith("sse4a.movnt.") || // Added in 3.9
     276             :       Name.startswith("avx.movnt.") || // Added in 3.2
     277             :       Name.startswith("avx512.storent.") || // Added in 3.9
     278             :       Name == "sse41.movntdqa" || // Added in 5.0
     279             :       Name == "avx2.movntdqa" || // Added in 5.0
     280             :       Name == "avx512.movntdqa" || // Added in 5.0
     281             :       Name == "sse2.storel.dq" || // Added in 3.9
     282             :       Name.startswith("sse.storeu.") || // Added in 3.9
     283             :       Name.startswith("sse2.storeu.") || // Added in 3.9
     284             :       Name.startswith("avx.storeu.") || // Added in 3.9
     285             :       Name.startswith("avx512.mask.storeu.") || // Added in 3.9
     286             :       Name.startswith("avx512.mask.store.p") || // Added in 3.9
     287             :       Name.startswith("avx512.mask.store.b.") || // Added in 3.9
     288             :       Name.startswith("avx512.mask.store.w.") || // Added in 3.9
     289             :       Name.startswith("avx512.mask.store.d.") || // Added in 3.9
     290             :       Name.startswith("avx512.mask.store.q.") || // Added in 3.9
     291             :       Name == "avx512.mask.store.ss" || // Added in 7.0
     292             :       Name.startswith("avx512.mask.loadu.") || // Added in 3.9
     293             :       Name.startswith("avx512.mask.load.") || // Added in 3.9
     294             :       Name == "sse42.crc32.64.8" || // Added in 3.4
     295             :       Name.startswith("avx.vbroadcast.s") || // Added in 3.5
     296             :       Name.startswith("avx512.vbroadcast.s") || // Added in 7.0
     297             :       Name.startswith("avx512.mask.palignr.") || // Added in 3.9
     298             :       Name.startswith("avx512.mask.valign.") || // Added in 4.0
     299             :       Name.startswith("sse2.psll.dq") || // Added in 3.7
     300             :       Name.startswith("sse2.psrl.dq") || // Added in 3.7
     301             :       Name.startswith("avx2.psll.dq") || // Added in 3.7
     302             :       Name.startswith("avx2.psrl.dq") || // Added in 3.7
     303             :       Name.startswith("avx512.psll.dq") || // Added in 3.9
     304             :       Name.startswith("avx512.psrl.dq") || // Added in 3.9
     305             :       Name == "sse41.pblendw" || // Added in 3.7
     306             :       Name.startswith("sse41.blendp") || // Added in 3.7
     307             :       Name.startswith("avx.blend.p") || // Added in 3.7
     308             :       Name == "avx2.pblendw" || // Added in 3.7
     309             :       Name.startswith("avx2.pblendd.") || // Added in 3.7
     310             :       Name.startswith("avx.vbroadcastf128") || // Added in 4.0
     311             :       Name == "avx2.vbroadcasti128" || // Added in 3.7
     312             :       Name.startswith("avx512.mask.broadcastf") || // Added in 6.0
     313             :       Name.startswith("avx512.mask.broadcasti") || // Added in 6.0
     314             :       Name == "xop.vpcmov" || // Added in 3.8
     315             :       Name == "xop.vpcmov.256" || // Added in 5.0
     316             :       Name.startswith("avx512.mask.move.s") || // Added in 4.0
     317             :       Name.startswith("avx512.cvtmask2") || // Added in 5.0
     318         192 :       (Name.startswith("xop.vpcom") && // Added in 3.2
     319         192 :        F->arg_size() == 2) ||
     320             :       Name.startswith("avx512.ptestm") || //Added in 6.0
     321             :       Name.startswith("avx512.ptestnm") || //Added in 6.0
     322             :       Name.startswith("sse2.pavg") || // Added in 6.0
     323             :       Name.startswith("avx2.pavg") || // Added in 6.0
     324             :       Name.startswith("avx512.mask.pavg")) // Added in 6.0
     325             :     return true;
     326             : 
     327             :   return false;
     328             : }
     329             : 
     330       12695 : static bool UpgradeX86IntrinsicFunction(Function *F, StringRef Name,
     331             :                                         Function *&NewFn) {
     332             :   // Only handle intrinsics that start with "x86.".
     333             :   if (!Name.startswith("x86."))
     334             :     return false;
     335             :   // Remove "x86." prefix.
     336       12619 :   Name = Name.substr(4);
     337             : 
     338       12619 :   if (ShouldUpgradeX86Intrinsic(F, Name)) {
     339        2392 :     NewFn = nullptr;
     340        2392 :     return true;
     341             :   }
     342             : 
     343             :   // SSE4.1 ptest functions may have an old signature.
     344             :   if (Name.startswith("sse41.ptest")) { // Added in 3.2
     345             :     if (Name.substr(11) == "c")
     346          73 :       return UpgradePTESTIntrinsic(F, Intrinsic::x86_sse41_ptestc, NewFn);
     347             :     if (Name.substr(11) == "z")
     348          54 :       return UpgradePTESTIntrinsic(F, Intrinsic::x86_sse41_ptestz, NewFn);
     349             :     if (Name.substr(11) == "nzc")
     350          51 :       return UpgradePTESTIntrinsic(F, Intrinsic::x86_sse41_ptestnzc, NewFn);
     351             :   }
     352             :   // Several blend and other instructions with masks used the wrong number of
     353             :   // bits.
     354             :   if (Name == "sse41.insertps") // Added in 3.6
     355             :     return UpgradeX86IntrinsicsWith8BitMask(F, Intrinsic::x86_sse41_insertps,
     356          43 :                                             NewFn);
     357             :   if (Name == "sse41.dppd") // Added in 3.6
     358             :     return UpgradeX86IntrinsicsWith8BitMask(F, Intrinsic::x86_sse41_dppd,
     359          27 :                                             NewFn);
     360             :   if (Name == "sse41.dpps") // Added in 3.6
     361             :     return UpgradeX86IntrinsicsWith8BitMask(F, Intrinsic::x86_sse41_dpps,
     362          27 :                                             NewFn);
     363             :   if (Name == "sse41.mpsadbw") // Added in 3.6
     364             :     return UpgradeX86IntrinsicsWith8BitMask(F, Intrinsic::x86_sse41_mpsadbw,
     365          27 :                                             NewFn);
     366             :   if (Name == "avx.dp.ps.256") // Added in 3.6
     367             :     return UpgradeX86IntrinsicsWith8BitMask(F, Intrinsic::x86_avx_dp_ps_256,
     368          20 :                                             NewFn);
     369             :   if (Name == "avx2.mpsadbw") // Added in 3.6
     370             :     return UpgradeX86IntrinsicsWith8BitMask(F, Intrinsic::x86_avx2_mpsadbw,
     371          21 :                                             NewFn);
     372             :   if (Name == "avx512.mask.cmp.pd.128") // Added in 7.0
     373             :     return UpgradeX86MaskedFPCompare(F, Intrinsic::x86_avx512_mask_cmp_pd_128,
     374           5 :                                      NewFn);
     375             :   if (Name == "avx512.mask.cmp.pd.256") // Added in 7.0
     376             :     return UpgradeX86MaskedFPCompare(F, Intrinsic::x86_avx512_mask_cmp_pd_256,
     377           5 :                                      NewFn);
     378             :   if (Name == "avx512.mask.cmp.pd.512") // Added in 7.0
     379             :     return UpgradeX86MaskedFPCompare(F, Intrinsic::x86_avx512_mask_cmp_pd_512,
     380           7 :                                      NewFn);
     381             :   if (Name == "avx512.mask.cmp.ps.128") // Added in 7.0
     382             :     return UpgradeX86MaskedFPCompare(F, Intrinsic::x86_avx512_mask_cmp_ps_128,
     383           5 :                                      NewFn);
     384             :   if (Name == "avx512.mask.cmp.ps.256") // Added in 7.0
     385             :     return UpgradeX86MaskedFPCompare(F, Intrinsic::x86_avx512_mask_cmp_ps_256,
     386           5 :                                      NewFn);
     387             :   if (Name == "avx512.mask.cmp.ps.512") // Added in 7.0
     388             :     return UpgradeX86MaskedFPCompare(F, Intrinsic::x86_avx512_mask_cmp_ps_512,
     389           8 :                                      NewFn);
     390             : 
     391             :   // frcz.ss/sd may need to have an argument dropped. Added in 3.2
     392           5 :   if (Name.startswith("xop.vfrcz.ss") && F->arg_size() == 2) {
     393           0 :     rename(F);
     394           0 :     NewFn = Intrinsic::getDeclaration(F->getParent(),
     395             :                                       Intrinsic::x86_xop_vfrcz_ss);
     396           0 :     return true;
     397             :   }
     398           5 :   if (Name.startswith("xop.vfrcz.sd") && F->arg_size() == 2) {
     399           0 :     rename(F);
     400           0 :     NewFn = Intrinsic::getDeclaration(F->getParent(),
     401             :                                       Intrinsic::x86_xop_vfrcz_sd);
     402           0 :     return true;
     403             :   }
     404             :   // Upgrade any XOP PERMIL2 index operand still using a float/double vector.
     405             :   if (Name.startswith("xop.vpermil2")) { // Added in 3.9
     406          48 :     auto Idx = F->getFunctionType()->getParamType(2);
     407             :     if (Idx->isFPOrFPVectorTy()) {
     408           4 :       rename(F);
     409           4 :       unsigned IdxSize = Idx->getPrimitiveSizeInBits();
     410           4 :       unsigned EltSize = Idx->getScalarSizeInBits();
     411             :       Intrinsic::ID Permil2ID;
     412           4 :       if (EltSize == 64 && IdxSize == 128)
     413             :         Permil2ID = Intrinsic::x86_xop_vpermil2pd;
     414           3 :       else if (EltSize == 32 && IdxSize == 128)
     415             :         Permil2ID = Intrinsic::x86_xop_vpermil2ps;
     416           2 :       else if (EltSize == 64 && IdxSize == 256)
     417             :         Permil2ID = Intrinsic::x86_xop_vpermil2pd_256;
     418             :       else
     419             :         Permil2ID = Intrinsic::x86_xop_vpermil2ps_256;
     420           4 :       NewFn = Intrinsic::getDeclaration(F->getParent(), Permil2ID);
     421           4 :       return true;
     422             :     }
     423             :   }
     424             : 
     425             :   return false;
     426             : }
     427             : 
     428      337627 : static bool UpgradeIntrinsicFunction1(Function *F, Function *&NewFn) {
     429             :   assert(F && "Illegal to upgrade a non-existent Function.");
     430             : 
     431             :   // Quickly eliminate it, if it's not a candidate.
     432      337627 :   StringRef Name = F->getName();
     433      337627 :   if (Name.size() <= 8 || !Name.startswith("llvm."))
     434             :     return false;
     435       42476 :   Name = Name.substr(5); // Strip off "llvm."
     436             : 
     437       42476 :   switch (Name[0]) {
     438             :   default: break;
     439             :   case 'a': {
     440             :     if (Name.startswith("arm.rbit") || Name.startswith("aarch64.rbit")) {
     441           3 :       NewFn = Intrinsic::getDeclaration(F->getParent(), Intrinsic::bitreverse,
     442           6 :                                         F->arg_begin()->getType());
     443          26 :       return true;
     444             :     }
     445             :     if (Name.startswith("arm.neon.vclz")) {
     446             :       Type* args[2] = {
     447           2 :         F->arg_begin()->getType(),
     448           2 :         Type::getInt1Ty(F->getContext())
     449           4 :       };
     450             :       // Can't use Intrinsic::getDeclaration here as it adds a ".i1" to
     451             :       // the end of the name. Change name from llvm.arm.neon.vclz.* to
     452             :       //  llvm.ctlz.*
     453           2 :       FunctionType* fType = FunctionType::get(F->getReturnType(), args, false);
     454           4 :       NewFn = Function::Create(fType, F->getLinkage(),
     455           4 :                                "llvm.ctlz." + Name.substr(14), F->getParent());
     456             :       return true;
     457             :     }
     458             :     if (Name.startswith("arm.neon.vcnt")) {
     459           2 :       NewFn = Intrinsic::getDeclaration(F->getParent(), Intrinsic::ctpop,
     460           4 :                                         F->arg_begin()->getType());
     461           2 :       return true;
     462             :     }
     463        7210 :     Regex vldRegex("^arm\\.neon\\.vld([1234]|[234]lane)\\.v[a-z0-9]*$");
     464        7210 :     if (vldRegex.match(Name)) {
     465             :       auto fArgs = F->getFunctionType()->params();
     466             :       SmallVector<Type *, 4> Tys(fArgs.begin(), fArgs.end());
     467             :       // Can't use Intrinsic::getDeclaration here as the return types might
     468             :       // then only be structurally equal.
     469           7 :       FunctionType* fType = FunctionType::get(F->getReturnType(), Tys, false);
     470          14 :       NewFn = Function::Create(fType, F->getLinkage(),
     471           7 :                                "llvm." + Name + ".p0i8", F->getParent());
     472             :       return true;
     473             :     }
     474        7203 :     Regex vstRegex("^arm\\.neon\\.vst([1234]|[234]lane)\\.v[a-z0-9]*$");
     475        7203 :     if (vstRegex.match(Name)) {
     476             :       static const Intrinsic::ID StoreInts[] = {Intrinsic::arm_neon_vst1,
     477             :                                                 Intrinsic::arm_neon_vst2,
     478             :                                                 Intrinsic::arm_neon_vst3,
     479             :                                                 Intrinsic::arm_neon_vst4};
     480             : 
     481             :       static const Intrinsic::ID StoreLaneInts[] = {
     482             :         Intrinsic::arm_neon_vst2lane, Intrinsic::arm_neon_vst3lane,
     483             :         Intrinsic::arm_neon_vst4lane
     484             :       };
     485             : 
     486             :       auto fArgs = F->getFunctionType()->params();
     487           7 :       Type *Tys[] = {fArgs[0], fArgs[1]};
     488           7 :       if (Name.find("lane") == StringRef::npos)
     489           4 :         NewFn = Intrinsic::getDeclaration(F->getParent(),
     490           4 :                                           StoreInts[fArgs.size() - 3], Tys);
     491             :       else
     492           3 :         NewFn = Intrinsic::getDeclaration(F->getParent(),
     493           3 :                                           StoreLaneInts[fArgs.size() - 5], Tys);
     494             :       return true;
     495             :     }
     496             :     if (Name == "aarch64.thread.pointer" || Name == "arm.thread.pointer") {
     497           2 :       NewFn = Intrinsic::getDeclaration(F->getParent(), Intrinsic::thread_pointer);
     498           2 :       return true;
     499             :     }
     500        7194 :     break;
     501             :   }
     502             : 
     503             :   case 'c': {
     504         734 :     if (Name.startswith("ctlz.") && F->arg_size() == 1) {
     505          32 :       rename(F);
     506          32 :       NewFn = Intrinsic::getDeclaration(F->getParent(), Intrinsic::ctlz,
     507          64 :                                         F->arg_begin()->getType());
     508          32 :       return true;
     509             :     }
     510         565 :     if (Name.startswith("cttz.") && F->arg_size() == 1) {
     511          18 :       rename(F);
     512          18 :       NewFn = Intrinsic::getDeclaration(F->getParent(), Intrinsic::cttz,
     513          36 :                                         F->arg_begin()->getType());
     514          18 :       return true;
     515             :     }
     516             :     break;
     517             :   }
     518             :   case 'd': {
     519         486 :     if (Name == "dbg.value" && F->arg_size() == 4) {
     520         127 :       rename(F);
     521         127 :       NewFn = Intrinsic::getDeclaration(F->getParent(), Intrinsic::dbg_value);
     522         127 :       return true;
     523             :     }
     524             :     break;
     525             :   }
     526             :   case 'i':
     527             :   case 'l': {
     528             :     bool IsLifetimeStart = Name.startswith("lifetime.start");
     529             :     if (IsLifetimeStart || Name.startswith("invariant.start")) {
     530         409 :       Intrinsic::ID ID = IsLifetimeStart ?
     531             :         Intrinsic::lifetime_start : Intrinsic::invariant_start;
     532             :       auto Args = F->getFunctionType()->params();
     533         409 :       Type* ObjectPtr[1] = {Args[1]};
     534        1227 :       if (F->getName() != Intrinsic::getName(ID, ObjectPtr)) {
     535          72 :         rename(F);
     536          72 :         NewFn = Intrinsic::getDeclaration(F->getParent(), ID, ObjectPtr);
     537          72 :         return true;
     538             :       }
     539             :     }
     540             : 
     541             :     bool IsLifetimeEnd = Name.startswith("lifetime.end");
     542             :     if (IsLifetimeEnd || Name.startswith("invariant.end")) {
     543         377 :       Intrinsic::ID ID = IsLifetimeEnd ?
     544             :         Intrinsic::lifetime_end : Intrinsic::invariant_end;
     545             : 
     546             :       auto Args = F->getFunctionType()->params();
     547         754 :       Type* ObjectPtr[1] = {Args[IsLifetimeEnd ? 1 : 2]};
     548        1131 :       if (F->getName() != Intrinsic::getName(ID, ObjectPtr)) {
     549          55 :         rename(F);
     550          55 :         NewFn = Intrinsic::getDeclaration(F->getParent(), ID, ObjectPtr);
     551          55 :         return true;
     552             :       }
     553             :     }
     554             :     if (Name.startswith("invariant.group.barrier")) {
     555             :       // Rename invariant.group.barrier to launder.invariant.group
     556             :       auto Args = F->getFunctionType()->params();
     557           3 :       Type* ObjectPtr[1] = {Args[0]};
     558           3 :       rename(F);
     559           3 :       NewFn = Intrinsic::getDeclaration(F->getParent(),
     560             :           Intrinsic::launder_invariant_group, ObjectPtr);
     561             :       return true;
     562             : 
     563             :     }
     564             : 
     565             :     break;
     566             :   }
     567             :   case 'm': {
     568             :     if (Name.startswith("masked.load.")) {
     569         420 :       Type *Tys[] = { F->getReturnType(), F->arg_begin()->getType() };
     570         630 :       if (F->getName() != Intrinsic::getName(Intrinsic::masked_load, Tys)) {
     571          10 :         rename(F);
     572          10 :         NewFn = Intrinsic::getDeclaration(F->getParent(),
     573             :                                           Intrinsic::masked_load,
     574             :                                           Tys);
     575          10 :         return true;
     576             :       }
     577             :     }
     578             :     if (Name.startswith("masked.store.")) {
     579             :       auto Args = F->getFunctionType()->params();
     580         198 :       Type *Tys[] = { Args[0], Args[1] };
     581         594 :       if (F->getName() != Intrinsic::getName(Intrinsic::masked_store, Tys)) {
     582          10 :         rename(F);
     583          10 :         NewFn = Intrinsic::getDeclaration(F->getParent(),
     584             :                                           Intrinsic::masked_store,
     585             :                                           Tys);
     586          10 :         return true;
     587             :       }
     588             :     }
     589             :     // Renaming gather/scatter intrinsics with no address space overloading
     590             :     // to the new overload which includes an address space
     591             :     if (Name.startswith("masked.gather.")) {
     592         498 :       Type *Tys[] = {F->getReturnType(), F->arg_begin()->getType()};
     593         747 :       if (F->getName() != Intrinsic::getName(Intrinsic::masked_gather, Tys)) {
     594          44 :         rename(F);
     595          44 :         NewFn = Intrinsic::getDeclaration(F->getParent(),
     596             :                                           Intrinsic::masked_gather, Tys);
     597          44 :         return true;
     598             :       }
     599             :     }
     600             :     if (Name.startswith("masked.scatter.")) {
     601             :       auto Args = F->getFunctionType()->params();
     602         137 :       Type *Tys[] = {Args[0], Args[1]};
     603         411 :       if (F->getName() != Intrinsic::getName(Intrinsic::masked_scatter, Tys)) {
     604           4 :         rename(F);
     605           4 :         NewFn = Intrinsic::getDeclaration(F->getParent(),
     606             :                                           Intrinsic::masked_scatter, Tys);
     607           4 :         return true;
     608             :       }
     609             :     }
     610             :     // Updating the memory intrinsics (memcpy/memmove/memset) that have an
     611             :     // alignment parameter to embedding the alignment as an attribute of
     612             :     // the pointer args.
     613         757 :     if (Name.startswith("memcpy.") && F->arg_size() == 5) {
     614          28 :       rename(F);
     615             :       // Get the types of dest, src, and len
     616          28 :       ArrayRef<Type *> ParamTypes = F->getFunctionType()->params().slice(0, 3);
     617          28 :       NewFn = Intrinsic::getDeclaration(F->getParent(), Intrinsic::memcpy,
     618             :                                         ParamTypes);
     619             :       return true;
     620             :     }
     621         131 :     if (Name.startswith("memmove.") && F->arg_size() == 5) {
     622           5 :       rename(F);
     623             :       // Get the types of dest, src, and len
     624           5 :       ArrayRef<Type *> ParamTypes = F->getFunctionType()->params().slice(0, 3);
     625           5 :       NewFn = Intrinsic::getDeclaration(F->getParent(), Intrinsic::memmove,
     626             :                                         ParamTypes);
     627             :       return true;
     628             :     }
     629         405 :     if (Name.startswith("memset.") && F->arg_size() == 5) {
     630          46 :       rename(F);
     631             :       // Get the types of dest, and len
     632             :       const auto *FT = F->getFunctionType();
     633             :       Type *ParamTypes[2] = {
     634          46 :           FT->getParamType(0), // Dest
     635             :           FT->getParamType(2)  // len
     636          92 :       };
     637          46 :       NewFn = Intrinsic::getDeclaration(F->getParent(), Intrinsic::memset,
     638             :                                         ParamTypes);
     639             :       return true;
     640             :     }
     641             :     break;
     642             :   }
     643             :   case 'n': {
     644             :     if (Name.startswith("nvvm.")) {
     645         547 :       Name = Name.substr(5);
     646             : 
     647             :       // The following nvvm intrinsics correspond exactly to an LLVM intrinsic.
     648         547 :       Intrinsic::ID IID = StringSwitch<Intrinsic::ID>(Name)
     649             :                               .Cases("brev32", "brev64", Intrinsic::bitreverse)
     650             :                               .Case("clz.i", Intrinsic::ctlz)
     651             :                               .Case("popc.i", Intrinsic::ctpop)
     652             :                               .Default(Intrinsic::not_intrinsic);
     653           8 :       if (IID != Intrinsic::not_intrinsic && F->arg_size() == 1) {
     654          16 :         NewFn = Intrinsic::getDeclaration(F->getParent(), IID,
     655             :                                           {F->getReturnType()});
     656           8 :         return true;
     657             :       }
     658             : 
     659             :       // The following nvvm intrinsics correspond exactly to an LLVM idiom, but
     660             :       // not to an intrinsic alone.  We expand them in UpgradeIntrinsicCall.
     661             :       //
     662             :       // TODO: We could add lohi.i2d.
     663         539 :       bool Expand = StringSwitch<bool>(Name)
     664             :                         .Cases("abs.i", "abs.ll", true)
     665             :                         .Cases("clz.ll", "popc.ll", "h2f", true)
     666             :                         .Cases("max.i", "max.ll", "max.ui", "max.ull", true)
     667             :                         .Cases("min.i", "min.ll", "min.ui", "min.ull", true)
     668             :                         .Default(false);
     669          26 :       if (Expand) {
     670          26 :         NewFn = nullptr;
     671          26 :         return true;
     672             :       }
     673             :     }
     674             :     break;
     675             :   }
     676             :   case 'o':
     677             :     // We only need to change the name to match the mangling including the
     678             :     // address space.
     679             :     if (Name.startswith("objectsize.")) {
     680         246 :       Type *Tys[2] = { F->getReturnType(), F->arg_begin()->getType() };
     681         246 :       if (F->arg_size() == 2 ||
     682         335 :           F->getName() != Intrinsic::getName(Intrinsic::objectsize, Tys)) {
     683          36 :         rename(F);
     684          36 :         NewFn = Intrinsic::getDeclaration(F->getParent(), Intrinsic::objectsize,
     685             :                                           Tys);
     686          36 :         return true;
     687             :       }
     688             :     }
     689             :     break;
     690             : 
     691             :   case 's':
     692             :     if (Name == "stackprotectorcheck") {
     693           6 :       NewFn = nullptr;
     694           6 :       return true;
     695             :     }
     696             :     break;
     697             : 
     698       12695 :   case 'x':
     699       12695 :     if (UpgradeX86IntrinsicFunction(F, Name, NewFn))
     700             :       return true;
     701             :   }
     702             :   // Remangle our intrinsic since we upgrade the mangling
     703       39498 :   auto Result = llvm::Intrinsic::remangleIntrinsicFunction(F);
     704       39498 :   if (Result != None) {
     705          96 :     NewFn = Result.getValue();
     706          96 :     return true;
     707             :   }
     708             : 
     709             :   //  This may not belong here. This function is effectively being overloaded
     710             :   //  to both detect an intrinsic which needs upgrading, and to provide the
     711             :   //  upgraded form of the intrinsic. We should perhaps have two separate
     712             :   //  functions for this.
     713             :   return false;
     714             : }
     715             : 
     716      337627 : bool llvm::UpgradeIntrinsicFunction(Function *F, Function *&NewFn) {
     717      337627 :   NewFn = nullptr;
     718      337627 :   bool Upgraded = UpgradeIntrinsicFunction1(F, NewFn);
     719             :   assert(F != NewFn && "Intrinsic function upgraded to the same function");
     720             : 
     721             :   // Upgrade intrinsic attributes.  This does not change the function.
     722      337627 :   if (NewFn)
     723             :     F = NewFn;
     724      337627 :   if (Intrinsic::ID id = F->getIntrinsicID())
     725       39677 :     F->setAttributes(Intrinsic::getAttributes(F->getContext(), id));
     726      337627 :   return Upgraded;
     727             : }
     728             : 
     729       36631 : bool llvm::UpgradeGlobalVariable(GlobalVariable *GV) {
     730             :   // Nothing to do yet.
     731       36631 :   return false;
     732             : }
     733             : 
     734             : // Handles upgrading SSE2/AVX2/AVX512BW PSLLDQ intrinsics by converting them
     735             : // to byte shuffles.
     736          18 : static Value *UpgradeX86PSLLDQIntrinsics(IRBuilder<> &Builder,
     737             :                                          Value *Op, unsigned Shift) {
     738          18 :   Type *ResultTy = Op->getType();
     739          18 :   unsigned NumElts = ResultTy->getVectorNumElements() * 8;
     740             : 
     741             :   // Bitcast from a 64-bit element type to a byte element type.
     742          36 :   Type *VecTy = VectorType::get(Builder.getInt8Ty(), NumElts);
     743          18 :   Op = Builder.CreateBitCast(Op, VecTy, "cast");
     744             : 
     745             :   // We'll be shuffling in zeroes.
     746          18 :   Value *Res = Constant::getNullValue(VecTy);
     747             : 
     748             :   // If shift is less than 16, emit a shuffle to move the bytes. Otherwise,
     749             :   // we'll just return the zero vector.
     750          18 :   if (Shift < 16) {
     751             :     uint32_t Idxs[64];
     752             :     // 256/512-bit version is split into 2/4 16-byte lanes.
     753         106 :     for (unsigned l = 0; l != NumElts; l += 16)
     754        1452 :       for (unsigned i = 0; i != 16; ++i) {
     755         704 :         unsigned Idx = NumElts + i - Shift;
     756         704 :         if (Idx < NumElts)
     757         202 :           Idx -= NumElts - 16; // end of lane, switch operand.
     758         704 :         Idxs[l + i] = Idx + l;
     759             :       }
     760             : 
     761          18 :     Res = Builder.CreateShuffleVector(Res, Op, makeArrayRef(Idxs, NumElts));
     762             :   }
     763             : 
     764             :   // Bitcast back to a 64-bit element type.
     765          18 :   return Builder.CreateBitCast(Res, ResultTy, "cast");
     766             : }
     767             : 
     768             : // Handles upgrading SSE2/AVX2/AVX512BW PSRLDQ intrinsics by converting them
     769             : // to byte shuffles.
     770         112 : static Value *UpgradeX86PSRLDQIntrinsics(IRBuilder<> &Builder, Value *Op,
     771             :                                          unsigned Shift) {
     772         112 :   Type *ResultTy = Op->getType();
     773         112 :   unsigned NumElts = ResultTy->getVectorNumElements() * 8;
     774             : 
     775             :   // Bitcast from a 64-bit element type to a byte element type.
     776         224 :   Type *VecTy = VectorType::get(Builder.getInt8Ty(), NumElts);
     777         112 :   Op = Builder.CreateBitCast(Op, VecTy, "cast");
     778             : 
     779             :   // We'll be shuffling in zeroes.
     780         112 :   Value *Res = Constant::getNullValue(VecTy);
     781             : 
     782             :   // If shift is less than 16, emit a shuffle to move the bytes. Otherwise,
     783             :   // we'll just return the zero vector.
     784         112 :   if (Shift < 16) {
     785             :     uint32_t Idxs[64];
     786             :     // 256/512-bit version is split into 2/4 16-byte lanes.
     787         388 :     for (unsigned l = 0; l != NumElts; l += 16)
     788        4554 :       for (unsigned i = 0; i != 16; ++i) {
     789        2208 :         unsigned Idx = i + Shift;
     790        2208 :         if (Idx >= 16)
     791         618 :           Idx += NumElts - 16; // end of lane, switch operand.
     792        2208 :         Idxs[l + i] = Idx + l;
     793             :       }
     794             : 
     795         112 :     Res = Builder.CreateShuffleVector(Op, Res, makeArrayRef(Idxs, NumElts));
     796             :   }
     797             : 
     798             :   // Bitcast back to a 64-bit element type.
     799         112 :   return Builder.CreateBitCast(Res, ResultTy, "cast");
     800             : }
     801             : 
     802        1545 : static Value *getX86MaskVec(IRBuilder<> &Builder, Value *Mask,
     803             :                             unsigned NumElts) {
     804        3090 :   llvm::VectorType *MaskTy = llvm::VectorType::get(Builder.getInt1Ty(),
     805        1545 :                              cast<IntegerType>(Mask->getType())->getBitWidth());
     806        1545 :   Mask = Builder.CreateBitCast(Mask, MaskTy);
     807             : 
     808             :   // If we have less than 8 elements, then the starting mask was an i8 and
     809             :   // we need to extract down to the right number of elements.
     810        1545 :   if (NumElts < 8) {
     811             :     uint32_t Indices[4];
     812        3383 :     for (unsigned i = 0; i != NumElts; ++i)
     813        1478 :       Indices[i] = i;
     814         427 :     Mask = Builder.CreateShuffleVector(Mask, Mask,
     815             :                                        makeArrayRef(Indices, NumElts),
     816             :                                        "extract");
     817             :   }
     818             : 
     819        1545 :   return Mask;
     820             : }
     821             : 
     822        1773 : static Value *EmitX86Select(IRBuilder<> &Builder, Value *Mask,
     823             :                             Value *Op0, Value *Op1) {
     824             :   // If the mask is all ones just emit the align operation.
     825             :   if (const auto *C = dyn_cast<Constant>(Mask))
     826         657 :     if (C->isAllOnesValue())
     827             :       return Op0;
     828             : 
     829        2232 :   Mask = getX86MaskVec(Builder, Mask, Op0->getType()->getVectorNumElements());
     830        1116 :   return Builder.CreateSelect(Mask, Op0, Op1);
     831             : }
     832             : 
     833             : // Handle autoupgrade for masked PALIGNR and VALIGND/Q intrinsics.
     834             : // PALIGNR handles large immediates by shifting while VALIGN masks the immediate
     835             : // so we need to handle both cases. VALIGN also doesn't have 128-bit lanes.
     836          24 : static Value *UpgradeX86ALIGNIntrinsics(IRBuilder<> &Builder, Value *Op0,
     837             :                                         Value *Op1, Value *Shift,
     838             :                                         Value *Passthru, Value *Mask,
     839             :                                         bool IsVALIGN) {
     840          24 :   unsigned ShiftVal = cast<llvm::ConstantInt>(Shift)->getZExtValue();
     841             : 
     842          24 :   unsigned NumElts = Op0->getType()->getVectorNumElements();
     843             :   assert((IsVALIGN || NumElts % 16 == 0) && "Illegal NumElts for PALIGNR!");
     844             :   assert((!IsVALIGN || NumElts <= 16) && "NumElts too large for VALIGN!");
     845             :   assert(isPowerOf2_32(NumElts) && "NumElts not a power of 2!");
     846             : 
     847             :   // Mask the immediate for VALIGN.
     848          24 :   if (IsVALIGN)
     849          12 :     ShiftVal &= (NumElts - 1);
     850             : 
     851             :   // If palignr is shifting the pair of vectors more than the size of two
     852             :   // lanes, emit zero.
     853          24 :   if (ShiftVal >= 32)
     854           0 :     return llvm::Constant::getNullValue(Op0->getType());
     855             : 
     856             :   // If palignr is shifting the pair of input vectors more than one lane,
     857             :   // but less than two lanes, convert to shifting in zeroes.
     858          24 :   if (ShiftVal > 16) {
     859           0 :     ShiftVal -= 16;
     860             :     Op1 = Op0;
     861           0 :     Op0 = llvm::Constant::getNullValue(Op0->getType());
     862             :   }
     863             : 
     864             :   uint32_t Indices[64];
     865             :   // 256-bit palignr operates on 128-bit lanes so we need to handle that
     866         114 :   for (unsigned l = 0; l < NumElts; l += 16) {
     867        1485 :     for (unsigned i = 0; i != 16; ++i) {
     868         720 :       unsigned Idx = ShiftVal + i;
     869         720 :       if (!IsVALIGN && Idx >= 16) // Disable wrap for VALIGN.
     870          66 :         Idx += NumElts - 16; // End of lane, switch operand.
     871         720 :       Indices[l + i] = Idx + l;
     872             :     }
     873             :   }
     874             : 
     875          24 :   Value *Align = Builder.CreateShuffleVector(Op1, Op0,
     876             :                                              makeArrayRef(Indices, NumElts),
     877          24 :                                              "palignr");
     878             : 
     879          24 :   return EmitX86Select(Builder, Mask, Align, Passthru);
     880             : }
     881             : 
     882          65 : static Value *UpgradeMaskedStore(IRBuilder<> &Builder,
     883             :                                  Value *Ptr, Value *Data, Value *Mask,
     884             :                                  bool Aligned) {
     885             :   // Cast the pointer to the right type.
     886          65 :   Ptr = Builder.CreateBitCast(Ptr,
     887          65 :                               llvm::PointerType::getUnqual(Data->getType()));
     888             :   unsigned Align =
     889          89 :     Aligned ? cast<VectorType>(Data->getType())->getBitWidth() / 8 : 1;
     890             : 
     891             :   // If the mask is all ones just emit a regular store.
     892             :   if (const auto *C = dyn_cast<Constant>(Mask))
     893          32 :     if (C->isAllOnesValue())
     894          32 :       return Builder.CreateAlignedStore(Data, Ptr, Align);
     895             : 
     896             :   // Convert the mask from an integer type to a vector of i1.
     897          33 :   unsigned NumElts = Data->getType()->getVectorNumElements();
     898          33 :   Mask = getX86MaskVec(Builder, Mask, NumElts);
     899          33 :   return Builder.CreateMaskedStore(Data, Ptr, Align, Mask);
     900             : }
     901             : 
     902          96 : static Value *UpgradeMaskedLoad(IRBuilder<> &Builder,
     903             :                                 Value *Ptr, Value *Passthru, Value *Mask,
     904             :                                 bool Aligned) {
     905             :   // Cast the pointer to the right type.
     906          96 :   Ptr = Builder.CreateBitCast(Ptr,
     907          96 :                              llvm::PointerType::getUnqual(Passthru->getType()));
     908             :   unsigned Align =
     909         132 :     Aligned ? cast<VectorType>(Passthru->getType())->getBitWidth() / 8 : 1;
     910             : 
     911             :   // If the mask is all ones just emit a regular store.
     912             :   if (const auto *C = dyn_cast<Constant>(Mask))
     913          32 :     if (C->isAllOnesValue())
     914          32 :       return Builder.CreateAlignedLoad(Ptr, Align);
     915             : 
     916             :   // Convert the mask from an integer type to a vector of i1.
     917          64 :   unsigned NumElts = Passthru->getType()->getVectorNumElements();
     918          64 :   Mask = getX86MaskVec(Builder, Mask, NumElts);
     919         128 :   return Builder.CreateMaskedLoad(Ptr, Align, Mask, Passthru);
     920             : }
     921             : 
     922         246 : static Value *upgradeAbs(IRBuilder<> &Builder, CallInst &CI) {
     923         246 :   Value *Op0 = CI.getArgOperand(0);
     924         246 :   llvm::Type *Ty = Op0->getType();
     925         246 :   Value *Zero = llvm::Constant::getNullValue(Ty);
     926         246 :   Value *Cmp = Builder.CreateICmp(ICmpInst::ICMP_SGT, Op0, Zero);
     927         246 :   Value *Neg = Builder.CreateNeg(Op0);
     928         246 :   Value *Res = Builder.CreateSelect(Cmp, Op0, Neg);
     929             : 
     930         246 :   if (CI.getNumArgOperands() == 3)
     931          42 :     Res = EmitX86Select(Builder,CI.getArgOperand(2), Res, CI.getArgOperand(1));
     932             : 
     933         246 :   return Res;
     934             : }
     935             : 
     936         970 : static Value *upgradeIntMinMax(IRBuilder<> &Builder, CallInst &CI,
     937             :                                ICmpInst::Predicate Pred) {
     938         970 :   Value *Op0 = CI.getArgOperand(0);
     939             :   Value *Op1 = CI.getArgOperand(1);
     940         970 :   Value *Cmp = Builder.CreateICmp(Pred, Op0, Op1);
     941         970 :   Value *Res = Builder.CreateSelect(Cmp, Op0, Op1);
     942             : 
     943         970 :   if (CI.getNumArgOperands() == 4)
     944         124 :     Res = EmitX86Select(Builder, CI.getArgOperand(3), Res, CI.getArgOperand(2));
     945             : 
     946         970 :   return Res;
     947             : }
     948             : 
     949         252 : static Value *upgradePMULDQ(IRBuilder<> &Builder, CallInst &CI, bool IsSigned) {
     950         252 :   Type *Ty = CI.getType();
     951             : 
     952             :   // Arguments have a vXi32 type so cast to vXi64.
     953         504 :   Value *LHS = Builder.CreateBitCast(CI.getArgOperand(0), Ty);
     954         252 :   Value *RHS = Builder.CreateBitCast(CI.getArgOperand(1), Ty);
     955             : 
     956         252 :   if (IsSigned) {
     957             :     // Shift left then arithmetic shift right.
     958         110 :     Constant *ShiftAmt = ConstantInt::get(Ty, 32);
     959         110 :     LHS = Builder.CreateShl(LHS, ShiftAmt);
     960         110 :     LHS = Builder.CreateAShr(LHS, ShiftAmt);
     961         110 :     RHS = Builder.CreateShl(RHS, ShiftAmt);
     962         110 :     RHS = Builder.CreateAShr(RHS, ShiftAmt);
     963             :   } else {
     964             :     // Clear the upper bits.
     965         142 :     Constant *Mask = ConstantInt::get(Ty, 0xffffffff);
     966         142 :     LHS = Builder.CreateAnd(LHS, Mask);
     967         142 :     RHS = Builder.CreateAnd(RHS, Mask);
     968             :   }
     969             : 
     970         252 :   Value *Res = Builder.CreateMul(LHS, RHS);
     971             : 
     972         252 :   if (CI.getNumArgOperands() == 4)
     973          54 :     Res = EmitX86Select(Builder, CI.getArgOperand(3), Res, CI.getArgOperand(2));
     974             : 
     975         252 :   return Res;
     976             : }
     977             : 
     978             : // Applying mask on vector of i1's and make sure result is at least 8 bits wide.
     979         586 : static Value *ApplyX86MaskOn1BitsVec(IRBuilder<> &Builder,Value *Vec, Value *Mask,
     980             :                                      unsigned NumElts) {
     981         586 :   if (Mask) {
     982             :     const auto *C = dyn_cast<Constant>(Mask);
     983         287 :     if (!C || !C->isAllOnesValue())
     984         283 :       Vec = Builder.CreateAnd(Vec, getX86MaskVec(Builder, Mask, NumElts));
     985             :   }
     986             : 
     987         586 :   if (NumElts < 8) {
     988             :     uint32_t Indices[8];
     989         975 :     for (unsigned i = 0; i != NumElts; ++i)
     990         424 :       Indices[i] = i;
     991        1311 :     for (unsigned i = NumElts; i != 8; ++i)
     992         592 :       Indices[i] = NumElts + i % NumElts;
     993         127 :     Vec = Builder.CreateShuffleVector(Vec,
     994         127 :                                       Constant::getNullValue(Vec->getType()),
     995             :                                       Indices);
     996             :   }
     997        1758 :   return Builder.CreateBitCast(Vec, Builder.getIntNTy(std::max(NumElts, 8U)));
     998             : }
     999             : 
    1000         504 : static Value *upgradeMaskedCompare(IRBuilder<> &Builder, CallInst &CI,
    1001             :                                    unsigned CC, bool Signed) {
    1002         504 :   Value *Op0 = CI.getArgOperand(0);
    1003         504 :   unsigned NumElts = Op0->getType()->getVectorNumElements();
    1004             : 
    1005             :   Value *Cmp;
    1006         504 :   if (CC == 3) {
    1007         112 :     Cmp = Constant::getNullValue(llvm::VectorType::get(Builder.getInt1Ty(), NumElts));
    1008         448 :   } else if (CC == 7) {
    1009         112 :     Cmp = Constant::getAllOnesValue(llvm::VectorType::get(Builder.getInt1Ty(), NumElts));
    1010             :   } else {
    1011             :     ICmpInst::Predicate Pred;
    1012         392 :     switch (CC) {
    1013           0 :     default: llvm_unreachable("Unknown condition code");
    1014             :     case 0: Pred = ICmpInst::ICMP_EQ;  break;
    1015          56 :     case 1: Pred = Signed ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT; break;
    1016          56 :     case 2: Pred = Signed ? ICmpInst::ICMP_SLE : ICmpInst::ICMP_ULE; break;
    1017          56 :     case 4: Pred = ICmpInst::ICMP_NE;  break;
    1018          56 :     case 5: Pred = Signed ? ICmpInst::ICMP_SGE : ICmpInst::ICMP_UGE; break;
    1019          84 :     case 6: Pred = Signed ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT; break;
    1020             :     }
    1021         392 :     Cmp = Builder.CreateICmp(Pred, Op0, CI.getArgOperand(1));
    1022             :   }
    1023             : 
    1024         504 :   Value *Mask = CI.getArgOperand(CI.getNumArgOperands() - 1);
    1025             : 
    1026         504 :   return ApplyX86MaskOn1BitsVec(Builder, Cmp, Mask, NumElts);
    1027             : }
    1028             : 
    1029             : // Replace a masked intrinsic with an older unmasked intrinsic.
    1030         278 : static Value *UpgradeX86MaskedShift(IRBuilder<> &Builder, CallInst &CI,
    1031             :                                     Intrinsic::ID IID) {
    1032         556 :   Function *Intrin = Intrinsic::getDeclaration(CI.getModule(), IID);
    1033         834 :   Value *Rep = Builder.CreateCall(Intrin,
    1034         278 :                                  { CI.getArgOperand(0), CI.getArgOperand(1) });
    1035         278 :   return EmitX86Select(Builder, CI.getArgOperand(3), Rep, CI.getArgOperand(2));
    1036             : }
    1037             : 
    1038           4 : static Value* upgradeMaskedMove(IRBuilder<> &Builder, CallInst &CI) {
    1039           4 :   Value* A = CI.getArgOperand(0);
    1040             :   Value* B = CI.getArgOperand(1);
    1041             :   Value* Src = CI.getArgOperand(2);
    1042             :   Value* Mask = CI.getArgOperand(3);
    1043             : 
    1044           8 :   Value* AndNode = Builder.CreateAnd(Mask, APInt(8, 1));
    1045           4 :   Value* Cmp = Builder.CreateIsNotNull(AndNode);
    1046           4 :   Value* Extract1 = Builder.CreateExtractElement(B, (uint64_t)0);
    1047           4 :   Value* Extract2 = Builder.CreateExtractElement(Src, (uint64_t)0);
    1048           4 :   Value* Select = Builder.CreateSelect(Cmp, Extract1, Extract2);
    1049           4 :   return Builder.CreateInsertElement(A, Select, (uint64_t)0);
    1050             : }
    1051             : 
    1052             : 
    1053          14 : static Value* UpgradeMaskToInt(IRBuilder<> &Builder, CallInst &CI) {
    1054          14 :   Value* Op = CI.getArgOperand(0);
    1055          14 :   Type* ReturnOp = CI.getType();
    1056             :   unsigned NumElts = CI.getType()->getVectorNumElements();
    1057          14 :   Value *Mask = getX86MaskVec(Builder, Op, NumElts);
    1058          14 :   return Builder.CreateSExt(Mask, ReturnOp, "vpmovm2");
    1059             : }
    1060             : 
    1061             : // Replace intrinsic with unmasked version and a select.
    1062         284 : static bool upgradeAVX512MaskToSelect(StringRef Name, IRBuilder<> &Builder,
    1063             :                                       CallInst &CI, Value *&Rep) {
    1064         284 :   Name = Name.substr(12); // Remove avx512.mask.
    1065             : 
    1066         284 :   unsigned VecWidth = CI.getType()->getPrimitiveSizeInBits();
    1067         284 :   unsigned EltWidth = CI.getType()->getScalarSizeInBits();
    1068             :   Intrinsic::ID IID;
    1069             :   if (Name.startswith("max.p")) {
    1070           6 :     if (VecWidth == 128 && EltWidth == 32)
    1071             :       IID = Intrinsic::x86_sse_max_ps;
    1072           3 :     else if (VecWidth == 128 && EltWidth == 64)
    1073             :       IID = Intrinsic::x86_sse2_max_pd;
    1074           3 :     else if (VecWidth == 256 && EltWidth == 32)
    1075             :       IID = Intrinsic::x86_avx_max_ps_256;
    1076           0 :     else if (VecWidth == 256 && EltWidth == 64)
    1077             :       IID = Intrinsic::x86_avx_max_pd_256;
    1078             :     else
    1079           0 :       llvm_unreachable("Unexpected intrinsic");
    1080             :   } else if (Name.startswith("min.p")) {
    1081           6 :     if (VecWidth == 128 && EltWidth == 32)
    1082             :       IID = Intrinsic::x86_sse_min_ps;
    1083           3 :     else if (VecWidth == 128 && EltWidth == 64)
    1084             :       IID = Intrinsic::x86_sse2_min_pd;
    1085           3 :     else if (VecWidth == 256 && EltWidth == 32)
    1086             :       IID = Intrinsic::x86_avx_min_ps_256;
    1087           0 :     else if (VecWidth == 256 && EltWidth == 64)
    1088             :       IID = Intrinsic::x86_avx_min_pd_256;
    1089             :     else
    1090           0 :       llvm_unreachable("Unexpected intrinsic");
    1091             :   } else if (Name.startswith("pshuf.b.")) {
    1092          33 :     if (VecWidth == 128)
    1093             :       IID = Intrinsic::x86_ssse3_pshuf_b_128;
    1094          28 :     else if (VecWidth == 256)
    1095             :       IID = Intrinsic::x86_avx2_pshuf_b;
    1096          23 :     else if (VecWidth == 512)
    1097             :       IID = Intrinsic::x86_avx512_pshuf_b_512;
    1098             :     else
    1099           0 :       llvm_unreachable("Unexpected intrinsic");
    1100             :   } else if (Name.startswith("pmul.hr.sw.")) {
    1101           8 :     if (VecWidth == 128)
    1102             :       IID = Intrinsic::x86_ssse3_pmul_hr_sw_128;
    1103           6 :     else if (VecWidth == 256)
    1104             :       IID = Intrinsic::x86_avx2_pmul_hr_sw;
    1105           4 :     else if (VecWidth == 512)
    1106             :       IID = Intrinsic::x86_avx512_pmul_hr_sw_512;
    1107             :     else
    1108           0 :       llvm_unreachable("Unexpected intrinsic");
    1109             :   } else if (Name.startswith("pmulh.w.")) {
    1110           8 :     if (VecWidth == 128)
    1111             :       IID = Intrinsic::x86_sse2_pmulh_w;
    1112           6 :     else if (VecWidth == 256)
    1113             :       IID = Intrinsic::x86_avx2_pmulh_w;
    1114           4 :     else if (VecWidth == 512)
    1115             :       IID = Intrinsic::x86_avx512_pmulh_w_512;
    1116             :     else
    1117           0 :       llvm_unreachable("Unexpected intrinsic");
    1118             :   } else if (Name.startswith("pmulhu.w.")) {
    1119           8 :     if (VecWidth == 128)
    1120             :       IID = Intrinsic::x86_sse2_pmulhu_w;
    1121           6 :     else if (VecWidth == 256)
    1122             :       IID = Intrinsic::x86_avx2_pmulhu_w;
    1123           4 :     else if (VecWidth == 512)
    1124             :       IID = Intrinsic::x86_avx512_pmulhu_w_512;
    1125             :     else
    1126           0 :       llvm_unreachable("Unexpected intrinsic");
    1127             :   } else if (Name.startswith("pmaddw.d.")) {
    1128          11 :     if (VecWidth == 128)
    1129             :       IID = Intrinsic::x86_sse2_pmadd_wd;
    1130           9 :     else if (VecWidth == 256)
    1131             :       IID = Intrinsic::x86_avx2_pmadd_wd;
    1132           7 :     else if (VecWidth == 512)
    1133             :       IID = Intrinsic::x86_avx512_pmaddw_d_512;
    1134             :     else
    1135           0 :       llvm_unreachable("Unexpected intrinsic");
    1136             :   } else if (Name.startswith("pmaddubs.w.")) {
    1137          11 :     if (VecWidth == 128)
    1138             :       IID = Intrinsic::x86_ssse3_pmadd_ub_sw_128;
    1139           9 :     else if (VecWidth == 256)
    1140             :       IID = Intrinsic::x86_avx2_pmadd_ub_sw;
    1141           7 :     else if (VecWidth == 512)
    1142             :       IID = Intrinsic::x86_avx512_pmaddubs_w_512;
    1143             :     else
    1144           0 :       llvm_unreachable("Unexpected intrinsic");
    1145             :   } else if (Name.startswith("packsswb.")) {
    1146          25 :     if (VecWidth == 128)
    1147             :       IID = Intrinsic::x86_sse2_packsswb_128;
    1148          19 :     else if (VecWidth == 256)
    1149             :       IID = Intrinsic::x86_avx2_packsswb;
    1150          13 :     else if (VecWidth == 512)
    1151             :       IID = Intrinsic::x86_avx512_packsswb_512;
    1152             :     else
    1153           0 :       llvm_unreachable("Unexpected intrinsic");
    1154             :   } else if (Name.startswith("packssdw.")) {
    1155          37 :     if (VecWidth == 128)
    1156             :       IID = Intrinsic::x86_sse2_packssdw_128;
    1157          28 :     else if (VecWidth == 256)
    1158             :       IID = Intrinsic::x86_avx2_packssdw;
    1159          19 :     else if (VecWidth == 512)
    1160             :       IID = Intrinsic::x86_avx512_packssdw_512;
    1161             :     else
    1162           0 :       llvm_unreachable("Unexpected intrinsic");
    1163             :   } else if (Name.startswith("packuswb.")) {
    1164          25 :     if (VecWidth == 128)
    1165             :       IID = Intrinsic::x86_sse2_packuswb_128;
    1166          19 :     else if (VecWidth == 256)
    1167             :       IID = Intrinsic::x86_avx2_packuswb;
    1168          13 :     else if (VecWidth == 512)
    1169             :       IID = Intrinsic::x86_avx512_packuswb_512;
    1170             :     else
    1171           0 :       llvm_unreachable("Unexpected intrinsic");
    1172             :   } else if (Name.startswith("packusdw.")) {
    1173          39 :     if (VecWidth == 128)
    1174             :       IID = Intrinsic::x86_sse41_packusdw;
    1175          30 :     else if (VecWidth == 256)
    1176             :       IID = Intrinsic::x86_avx2_packusdw;
    1177          21 :     else if (VecWidth == 512)
    1178             :       IID = Intrinsic::x86_avx512_packusdw_512;
    1179             :     else
    1180           0 :       llvm_unreachable("Unexpected intrinsic");
    1181             :   } else if (Name.startswith("vpermilvar.")) {
    1182          31 :     if (VecWidth == 128 && EltWidth == 32)
    1183             :       IID = Intrinsic::x86_avx_vpermilvar_ps;
    1184          28 :     else if (VecWidth == 128 && EltWidth == 64)
    1185             :       IID = Intrinsic::x86_avx_vpermilvar_pd;
    1186          25 :     else if (VecWidth == 256 && EltWidth == 32)
    1187             :       IID = Intrinsic::x86_avx_vpermilvar_ps_256;
    1188          22 :     else if (VecWidth == 256 && EltWidth == 64)
    1189             :       IID = Intrinsic::x86_avx_vpermilvar_pd_256;
    1190          19 :     else if (VecWidth == 512 && EltWidth == 32)
    1191             :       IID = Intrinsic::x86_avx512_vpermilvar_ps_512;
    1192           6 :     else if (VecWidth == 512 && EltWidth == 64)
    1193             :       IID = Intrinsic::x86_avx512_vpermilvar_pd_512;
    1194             :     else
    1195           0 :       llvm_unreachable("Unexpected intrinsic");
    1196             :   } else if (Name == "cvtdq2ps.128") {
    1197             :     IID = Intrinsic::x86_sse2_cvtdq2ps;
    1198             :   } else if (Name == "cvtdq2ps.256") {
    1199             :     IID = Intrinsic::x86_avx_cvtdq2_ps_256;
    1200             :   } else if (Name == "cvtpd2dq.256") {
    1201             :     IID = Intrinsic::x86_avx_cvt_pd2dq_256;
    1202             :   } else if (Name == "cvtpd2ps.256") {
    1203             :     IID = Intrinsic::x86_avx_cvt_pd2_ps_256;
    1204             :   } else if (Name == "cvttpd2dq.256") {
    1205             :     IID = Intrinsic::x86_avx_cvtt_pd2dq_256;
    1206             :   } else if (Name == "cvttps2dq.128") {
    1207             :     IID = Intrinsic::x86_sse2_cvttps2dq;
    1208             :   } else if (Name == "cvttps2dq.256") {
    1209             :     IID = Intrinsic::x86_avx_cvtt_ps2dq_256;
    1210             :   } else if (Name == "permvar.sf.256") {
    1211             :     IID = Intrinsic::x86_avx2_permps;
    1212             :   } else if (Name == "permvar.si.256") {
    1213             :     IID = Intrinsic::x86_avx2_permd;
    1214             :   } else
    1215             :     return false;
    1216             : 
    1217         568 :   SmallVector<Value *, 4> Args(CI.arg_operands().begin(),
    1218         852 :                                CI.arg_operands().end());
    1219             :   Args.pop_back();
    1220             :   Args.pop_back();
    1221         852 :   Rep = Builder.CreateCall(Intrinsic::getDeclaration(CI.getModule(), IID),
    1222             :                            Args);
    1223             :   unsigned NumArgs = CI.getNumArgOperands();
    1224         852 :   Rep = EmitX86Select(Builder, CI.getArgOperand(NumArgs - 1), Rep,
    1225             :                       CI.getArgOperand(NumArgs - 2));
    1226             :   return true;
    1227             : }
    1228             : 
    1229             : /// Upgrade comment in call to inline asm that represents an objc retain release
    1230             : /// marker.
    1231          61 : void llvm::UpgradeInlineAsmString(std::string *AsmStr) {
    1232             :   size_t Pos;
    1233           1 :   if (AsmStr->find("mov\tfp") == 0 &&
    1234          62 :       AsmStr->find("objc_retainAutoreleaseReturnValue") != std::string::npos &&
    1235             :       (Pos = AsmStr->find("# marker")) != std::string::npos) {
    1236           1 :     AsmStr->replace(Pos, 1, ";");
    1237             :   }
    1238          61 :   return;
    1239             : }
    1240             : 
    1241             : /// Upgrade a call to an old intrinsic. All argument and return casting must be
    1242             : /// provided to seamlessly integrate with existing context.
    1243        6360 : void llvm::UpgradeIntrinsicCall(CallInst *CI, Function *NewFn) {
    1244        6360 :   Function *F = CI->getCalledFunction();
    1245        6360 :   LLVMContext &C = CI->getContext();
    1246             :   IRBuilder<> Builder(C);
    1247        6360 :   Builder.SetInsertPoint(CI->getParent(), CI->getIterator());
    1248             : 
    1249             :   assert(F && "Intrinsic call is not direct?");
    1250             : 
    1251        6360 :   if (!NewFn) {
    1252             :     // Get the Function's name.
    1253        5160 :     StringRef Name = F->getName();
    1254             : 
    1255             :     assert(Name.startswith("llvm.") && "Intrinsic doesn't start with 'llvm.'");
    1256        5160 :     Name = Name.substr(5);
    1257             : 
    1258             :     bool IsX86 = Name.startswith("x86.");
    1259             :     if (IsX86)
    1260        5128 :       Name = Name.substr(4);
    1261             :     bool IsNVVM = Name.startswith("nvvm.");
    1262             :     if (IsNVVM)
    1263          26 :       Name = Name.substr(5);
    1264             : 
    1265        5160 :     if (IsX86 && Name.startswith("sse4a.movnt.")) {
    1266          14 :       Module *M = F->getParent();
    1267             :       SmallVector<Metadata *, 1> Elts;
    1268          14 :       Elts.push_back(
    1269          42 :           ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(C), 1)));
    1270             :       MDNode *Node = MDNode::get(C, Elts);
    1271             : 
    1272          14 :       Value *Arg0 = CI->getArgOperand(0);
    1273             :       Value *Arg1 = CI->getArgOperand(1);
    1274             : 
    1275             :       // Nontemporal (unaligned) store of the 0'th element of the float/double
    1276             :       // vector.
    1277          14 :       Type *SrcEltTy = cast<VectorType>(Arg1->getType())->getElementType();
    1278             :       PointerType *EltPtrTy = PointerType::getUnqual(SrcEltTy);
    1279          14 :       Value *Addr = Builder.CreateBitCast(Arg0, EltPtrTy, "cast");
    1280             :       Value *Extract =
    1281          14 :           Builder.CreateExtractElement(Arg1, (uint64_t)0, "extractelement");
    1282             : 
    1283             :       StoreInst *SI = Builder.CreateAlignedStore(Extract, Addr, 1);
    1284          28 :       SI->setMetadata(M->getMDKindID("nontemporal"), Node);
    1285             : 
    1286             :       // Remove intrinsic.
    1287          14 :       CI->eraseFromParent();
    1288             :       return;
    1289             :     }
    1290             : 
    1291        5146 :     if (IsX86 && (Name.startswith("avx.movnt.") ||
    1292             :                   Name.startswith("avx512.storent."))) {
    1293          15 :       Module *M = F->getParent();
    1294             :       SmallVector<Metadata *, 1> Elts;
    1295          15 :       Elts.push_back(
    1296          45 :           ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(C), 1)));
    1297             :       MDNode *Node = MDNode::get(C, Elts);
    1298             : 
    1299          15 :       Value *Arg0 = CI->getArgOperand(0);
    1300             :       Value *Arg1 = CI->getArgOperand(1);
    1301             : 
    1302             :       // Convert the type of the pointer to a pointer to the stored type.
    1303          15 :       Value *BC = Builder.CreateBitCast(Arg0,
    1304          15 :                                         PointerType::getUnqual(Arg1->getType()),
    1305             :                                         "cast");
    1306          15 :       VectorType *VTy = cast<VectorType>(Arg1->getType());
    1307          15 :       StoreInst *SI = Builder.CreateAlignedStore(Arg1, BC,
    1308             :                                                  VTy->getBitWidth() / 8);
    1309          30 :       SI->setMetadata(M->getMDKindID("nontemporal"), Node);
    1310             : 
    1311             :       // Remove intrinsic.
    1312          15 :       CI->eraseFromParent();
    1313             :       return;
    1314             :     }
    1315             : 
    1316        5131 :     if (IsX86 && Name == "sse2.storel.dq") {
    1317           1 :       Value *Arg0 = CI->getArgOperand(0);
    1318             :       Value *Arg1 = CI->getArgOperand(1);
    1319             : 
    1320           1 :       Type *NewVecTy = VectorType::get(Type::getInt64Ty(C), 2);
    1321           1 :       Value *BC0 = Builder.CreateBitCast(Arg1, NewVecTy, "cast");
    1322           1 :       Value *Elt = Builder.CreateExtractElement(BC0, (uint64_t)0);
    1323           1 :       Value *BC = Builder.CreateBitCast(Arg0,
    1324           1 :                                         PointerType::getUnqual(Elt->getType()),
    1325             :                                         "cast");
    1326             :       Builder.CreateAlignedStore(Elt, BC, 1);
    1327             : 
    1328             :       // Remove intrinsic.
    1329           1 :       CI->eraseFromParent();
    1330           1 :       return;
    1331             :     }
    1332             : 
    1333        5130 :     if (IsX86 && (Name.startswith("sse.storeu.") ||
    1334             :                   Name.startswith("sse2.storeu.") ||
    1335             :                   Name.startswith("avx.storeu."))) {
    1336          58 :       Value *Arg0 = CI->getArgOperand(0);
    1337             :       Value *Arg1 = CI->getArgOperand(1);
    1338             : 
    1339          58 :       Arg0 = Builder.CreateBitCast(Arg0,
    1340          58 :                                    PointerType::getUnqual(Arg1->getType()),
    1341             :                                    "cast");
    1342             :       Builder.CreateAlignedStore(Arg1, Arg0, 1);
    1343             : 
    1344             :       // Remove intrinsic.
    1345          58 :       CI->eraseFromParent();
    1346          58 :       return;
    1347             :     }
    1348             : 
    1349        5072 :     if (IsX86 && Name == "avx512.mask.store.ss") {
    1350           2 :       Value *Mask = Builder.CreateAnd(CI->getArgOperand(2), Builder.getInt8(1));
    1351           1 :       UpgradeMaskedStore(Builder, CI->getArgOperand(0), CI->getArgOperand(1),
    1352             :                          Mask, false);
    1353             : 
    1354             :       // Remove intrinsic.
    1355           1 :       CI->eraseFromParent();
    1356           1 :       return;
    1357             :     }
    1358             : 
    1359        5071 :     if (IsX86 && (Name.startswith("avx512.mask.store"))) {
    1360             :       // "avx512.mask.storeu." or "avx512.mask.store."
    1361          64 :       bool Aligned = Name[17] != 'u'; // "avx512.mask.storeu".
    1362         128 :       UpgradeMaskedStore(Builder, CI->getArgOperand(0), CI->getArgOperand(1),
    1363             :                          CI->getArgOperand(2), Aligned);
    1364             : 
    1365             :       // Remove intrinsic.
    1366          64 :       CI->eraseFromParent();
    1367          64 :       return;
    1368             :     }
    1369             : 
    1370             :     Value *Rep;
    1371             :     // Upgrade packed integer vector compare intrinsics to compare instructions.
    1372        5007 :     if (IsX86 && (Name.startswith("sse2.pcmp") ||
    1373             :                   Name.startswith("avx2.pcmp"))) {
    1374             :       // "sse2.pcpmpeq." "sse2.pcmpgt." "avx2.pcmpeq." or "avx2.pcmpgt."
    1375           1 :       bool CmpEq = Name[9] == 'e';
    1376           2 :       Rep = Builder.CreateICmp(CmpEq ? ICmpInst::ICMP_EQ : ICmpInst::ICMP_SGT,
    1377             :                                CI->getArgOperand(0), CI->getArgOperand(1));
    1378           2 :       Rep = Builder.CreateSExt(Rep, CI->getType(), "");
    1379        5006 :     } else if (IsX86 && (Name.startswith("avx512.broadcastm"))) {
    1380           6 :       Type *ExtTy = Type::getInt32Ty(C);
    1381          12 :       if (CI->getOperand(0)->getType()->isIntegerTy(8))
    1382           3 :         ExtTy = Type::getInt64Ty(C);
    1383           6 :       unsigned NumElts = CI->getType()->getPrimitiveSizeInBits() /
    1384           6 :                          ExtTy->getPrimitiveSizeInBits();
    1385           6 :       Rep = Builder.CreateZExt(CI->getArgOperand(0), ExtTy);
    1386           6 :       Rep = Builder.CreateVectorSplat(NumElts, Rep);
    1387        5000 :     } else if (IsX86 && (Name.startswith("avx512.ptestm") ||
    1388             :                          Name.startswith("avx512.ptestnm"))) {
    1389          60 :       Value *Op0 = CI->getArgOperand(0);
    1390             :       Value *Op1 = CI->getArgOperand(1);
    1391             :       Value *Mask = CI->getArgOperand(2);
    1392          60 :       Rep = Builder.CreateAnd(Op0, Op1);
    1393          60 :       llvm::Type *Ty = Op0->getType();
    1394          60 :       Value *Zero = llvm::Constant::getNullValue(Ty);
    1395             :       ICmpInst::Predicate Pred =
    1396             :         Name.startswith("avx512.ptestm") ? ICmpInst::ICMP_NE : ICmpInst::ICMP_EQ;
    1397          60 :       Rep = Builder.CreateICmp(Pred, Rep, Zero);
    1398          60 :       unsigned NumElts = Op0->getType()->getVectorNumElements();
    1399          60 :       Rep = ApplyX86MaskOn1BitsVec(Builder, Rep, Mask, NumElts);
    1400        4940 :     } else if (IsX86 && (Name.startswith("avx512.mask.pbroadcast"))){
    1401             :       unsigned NumElts =
    1402          84 :           CI->getArgOperand(1)->getType()->getVectorNumElements();
    1403          42 :       Rep = Builder.CreateVectorSplat(NumElts, CI->getArgOperand(0));
    1404          42 :       Rep = EmitX86Select(Builder, CI->getArgOperand(2), Rep,
    1405             :                           CI->getArgOperand(1));
    1406        4898 :     } else if (IsX86 && (Name.startswith("avx512.kunpck"))) {
    1407           5 :       unsigned NumElts = CI->getType()->getScalarSizeInBits();
    1408          10 :       Value *LHS = getX86MaskVec(Builder, CI->getArgOperand(0), NumElts);
    1409           5 :       Value *RHS = getX86MaskVec(Builder, CI->getArgOperand(1), NumElts);
    1410             :       uint32_t Indices[64];
    1411         421 :       for (unsigned i = 0; i != NumElts; ++i)
    1412         208 :         Indices[i] = i;
    1413             : 
    1414             :       // First extract half of each vector. This gives better codegen than
    1415             :       // doing it in a single shuffle.
    1416           5 :       LHS = Builder.CreateShuffleVector(LHS, LHS,
    1417           5 :                                         makeArrayRef(Indices, NumElts / 2));
    1418           5 :       RHS = Builder.CreateShuffleVector(RHS, RHS,
    1419             :                                         makeArrayRef(Indices, NumElts / 2));
    1420             :       // Concat the vectors.
    1421             :       // NOTE: Operands have to be swapped to match intrinsic definition.
    1422           5 :       Rep = Builder.CreateShuffleVector(RHS, LHS,
    1423             :                                         makeArrayRef(Indices, NumElts));
    1424          10 :       Rep = Builder.CreateBitCast(Rep, CI->getType());
    1425        4893 :     } else if (IsX86 && Name == "avx512.kand.w") {
    1426           4 :       Value *LHS = getX86MaskVec(Builder, CI->getArgOperand(0), 16);
    1427           2 :       Value *RHS = getX86MaskVec(Builder, CI->getArgOperand(1), 16);
    1428           2 :       Rep = Builder.CreateAnd(LHS, RHS);
    1429           4 :       Rep = Builder.CreateBitCast(Rep, CI->getType());
    1430        4891 :     } else if (IsX86 && Name == "avx512.kandn.w") {
    1431           4 :       Value *LHS = getX86MaskVec(Builder, CI->getArgOperand(0), 16);
    1432           2 :       Value *RHS = getX86MaskVec(Builder, CI->getArgOperand(1), 16);
    1433           2 :       LHS = Builder.CreateNot(LHS);
    1434           2 :       Rep = Builder.CreateAnd(LHS, RHS);
    1435           4 :       Rep = Builder.CreateBitCast(Rep, CI->getType());
    1436        4889 :     } else if (IsX86 && Name == "avx512.kor.w") {
    1437           4 :       Value *LHS = getX86MaskVec(Builder, CI->getArgOperand(0), 16);
    1438           2 :       Value *RHS = getX86MaskVec(Builder, CI->getArgOperand(1), 16);
    1439           2 :       Rep = Builder.CreateOr(LHS, RHS);
    1440           4 :       Rep = Builder.CreateBitCast(Rep, CI->getType());
    1441        4887 :     } else if (IsX86 && Name == "avx512.kxor.w") {
    1442           4 :       Value *LHS = getX86MaskVec(Builder, CI->getArgOperand(0), 16);
    1443           2 :       Value *RHS = getX86MaskVec(Builder, CI->getArgOperand(1), 16);
    1444           2 :       Rep = Builder.CreateXor(LHS, RHS);
    1445           4 :       Rep = Builder.CreateBitCast(Rep, CI->getType());
    1446        4885 :     } else if (IsX86 && Name == "avx512.kxnor.w") {
    1447           4 :       Value *LHS = getX86MaskVec(Builder, CI->getArgOperand(0), 16);
    1448           2 :       Value *RHS = getX86MaskVec(Builder, CI->getArgOperand(1), 16);
    1449           2 :       LHS = Builder.CreateNot(LHS);
    1450           2 :       Rep = Builder.CreateXor(LHS, RHS);
    1451           4 :       Rep = Builder.CreateBitCast(Rep, CI->getType());
    1452        4883 :     } else if (IsX86 && Name == "avx512.knot.w") {
    1453           2 :       Rep = getX86MaskVec(Builder, CI->getArgOperand(0), 16);
    1454           1 :       Rep = Builder.CreateNot(Rep);
    1455           2 :       Rep = Builder.CreateBitCast(Rep, CI->getType());
    1456        4882 :     } else if (IsX86 &&
    1457             :                (Name == "avx512.kortestz.w" || Name == "avx512.kortestc.w")) {
    1458           4 :       Value *LHS = getX86MaskVec(Builder, CI->getArgOperand(0), 16);
    1459           2 :       Value *RHS = getX86MaskVec(Builder, CI->getArgOperand(1), 16);
    1460           2 :       Rep = Builder.CreateOr(LHS, RHS);
    1461           6 :       Rep = Builder.CreateBitCast(Rep, Builder.getInt16Ty());
    1462             :       Value *C;
    1463           4 :       if (Name[14] == 'c')
    1464           0 :         C = ConstantInt::getAllOnesValue(Builder.getInt16Ty());
    1465             :       else
    1466           4 :         C = ConstantInt::getNullValue(Builder.getInt16Ty());
    1467           4 :       Rep = Builder.CreateICmpEQ(Rep, C);
    1468           6 :       Rep = Builder.CreateZExt(Rep, Builder.getInt32Ty());
    1469        4880 :     } else if (IsX86 && (Name == "sse.add.ss" || Name == "sse2.add.sd")) {
    1470          11 :       Type *I32Ty = Type::getInt32Ty(C);
    1471          22 :       Value *Elt0 = Builder.CreateExtractElement(CI->getArgOperand(0),
    1472          22 :                                                  ConstantInt::get(I32Ty, 0));
    1473          11 :       Value *Elt1 = Builder.CreateExtractElement(CI->getArgOperand(1),
    1474          22 :                                                  ConstantInt::get(I32Ty, 0));
    1475          22 :       Rep = Builder.CreateInsertElement(CI->getArgOperand(0),
    1476             :                                         Builder.CreateFAdd(Elt0, Elt1),
    1477          11 :                                         ConstantInt::get(I32Ty, 0));
    1478        4869 :     } else if (IsX86 && (Name == "sse.sub.ss" || Name == "sse2.sub.sd")) {
    1479          17 :       Type *I32Ty = Type::getInt32Ty(C);
    1480          34 :       Value *Elt0 = Builder.CreateExtractElement(CI->getArgOperand(0),
    1481          34 :                                                  ConstantInt::get(I32Ty, 0));
    1482          17 :       Value *Elt1 = Builder.CreateExtractElement(CI->getArgOperand(1),
    1483          34 :                                                  ConstantInt::get(I32Ty, 0));
    1484          34 :       Rep = Builder.CreateInsertElement(CI->getArgOperand(0),
    1485             :                                         Builder.CreateFSub(Elt0, Elt1),
    1486          17 :                                         ConstantInt::get(I32Ty, 0));
    1487        4852 :     } else if (IsX86 && (Name == "sse.mul.ss" || Name == "sse2.mul.sd")) {
    1488          17 :       Type *I32Ty = Type::getInt32Ty(C);
    1489          34 :       Value *Elt0 = Builder.CreateExtractElement(CI->getArgOperand(0),
    1490          34 :                                                  ConstantInt::get(I32Ty, 0));
    1491          17 :       Value *Elt1 = Builder.CreateExtractElement(CI->getArgOperand(1),
    1492          34 :                                                  ConstantInt::get(I32Ty, 0));
    1493          34 :       Rep = Builder.CreateInsertElement(CI->getArgOperand(0),
    1494             :                                         Builder.CreateFMul(Elt0, Elt1),
    1495          17 :                                         ConstantInt::get(I32Ty, 0));
    1496        4835 :     } else if (IsX86 && (Name == "sse.div.ss" || Name == "sse2.div.sd")) {
    1497          10 :       Type *I32Ty = Type::getInt32Ty(C);
    1498          20 :       Value *Elt0 = Builder.CreateExtractElement(CI->getArgOperand(0),
    1499          20 :                                                  ConstantInt::get(I32Ty, 0));
    1500          10 :       Value *Elt1 = Builder.CreateExtractElement(CI->getArgOperand(1),
    1501          20 :                                                  ConstantInt::get(I32Ty, 0));
    1502          20 :       Rep = Builder.CreateInsertElement(CI->getArgOperand(0),
    1503             :                                         Builder.CreateFDiv(Elt0, Elt1),
    1504          10 :                                         ConstantInt::get(I32Ty, 0));
    1505        4825 :     } else if (IsX86 && Name.startswith("avx512.mask.pcmp")) {
    1506             :       // "avx512.mask.pcmpeq." or "avx512.mask.pcmpgt."
    1507             :       bool CmpEq = Name[16] == 'e';
    1508          56 :       Rep = upgradeMaskedCompare(Builder, *CI, CmpEq ? 0 : 6, true);
    1509        4769 :     } else if (IsX86 && Name.startswith("avx512.mask.cmp")) {
    1510         448 :       unsigned Imm = cast<ConstantInt>(CI->getArgOperand(2))->getZExtValue();
    1511         224 :       Rep = upgradeMaskedCompare(Builder, *CI, Imm, true);
    1512        4545 :     } else if (IsX86 && Name.startswith("avx512.mask.ucmp")) {
    1513         448 :       unsigned Imm = cast<ConstantInt>(CI->getArgOperand(2))->getZExtValue();
    1514         224 :       Rep = upgradeMaskedCompare(Builder, *CI, Imm, false);
    1515        4321 :     } else if (IsX86 && (Name.startswith("avx512.cvtb2mask.") ||
    1516             :                          Name.startswith("avx512.cvtw2mask.") ||
    1517             :                          Name.startswith("avx512.cvtd2mask.") ||
    1518             :                          Name.startswith("avx512.cvtq2mask."))) {
    1519          16 :       Value *Op = CI->getArgOperand(0);
    1520          16 :       Value *Zero = llvm::Constant::getNullValue(Op->getType());
    1521          16 :       Rep = Builder.CreateICmp(ICmpInst::ICMP_SLT, Op, Zero);
    1522          32 :       Rep = ApplyX86MaskOn1BitsVec(Builder, Rep, nullptr,
    1523             :                                    Op->getType()->getVectorNumElements());
    1524        4305 :     } else if(IsX86 && (Name == "ssse3.pabs.b.128" ||
    1525             :                         Name == "ssse3.pabs.w.128" ||
    1526             :                         Name == "ssse3.pabs.d.128" ||
    1527             :                         Name.startswith("avx2.pabs") ||
    1528             :                         Name.startswith("avx512.mask.pabs"))) {
    1529         246 :       Rep = upgradeAbs(Builder, *CI);
    1530        4059 :     } else if (IsX86 && (Name == "sse41.pmaxsb" ||
    1531             :                          Name == "sse2.pmaxs.w" ||
    1532             :                          Name == "sse41.pmaxsd" ||
    1533             :                          Name.startswith("avx2.pmaxs") ||
    1534             :                          Name.startswith("avx512.mask.pmaxs"))) {
    1535         234 :       Rep = upgradeIntMinMax(Builder, *CI, ICmpInst::ICMP_SGT);
    1536        3825 :     } else if (IsX86 && (Name == "sse2.pmaxu.b" ||
    1537             :                          Name == "sse41.pmaxuw" ||
    1538             :                          Name == "sse41.pmaxud" ||
    1539             :                          Name.startswith("avx2.pmaxu") ||
    1540             :                          Name.startswith("avx512.mask.pmaxu"))) {
    1541         275 :       Rep = upgradeIntMinMax(Builder, *CI, ICmpInst::ICMP_UGT);
    1542        3550 :     } else if (IsX86 && (Name == "sse41.pminsb" ||
    1543             :                          Name == "sse2.pmins.w" ||
    1544             :                          Name == "sse41.pminsd" ||
    1545             :                          Name.startswith("avx2.pmins") ||
    1546             :                          Name.startswith("avx512.mask.pmins"))) {
    1547         230 :       Rep = upgradeIntMinMax(Builder, *CI, ICmpInst::ICMP_SLT);
    1548        3320 :     } else if (IsX86 && (Name == "sse2.pminu.b" ||
    1549             :                          Name == "sse41.pminuw" ||
    1550             :                          Name == "sse41.pminud" ||
    1551             :                          Name.startswith("avx2.pminu") ||
    1552             :                          Name.startswith("avx512.mask.pminu"))) {
    1553         231 :       Rep = upgradeIntMinMax(Builder, *CI, ICmpInst::ICMP_ULT);
    1554        3089 :     } else if (IsX86 && (Name == "sse2.pmulu.dq" ||
    1555             :                          Name == "avx2.pmulu.dq" ||
    1556             :                          Name == "avx512.pmulu.dq.512" ||
    1557             :                          Name.startswith("avx512.mask.pmulu.dq."))) {
    1558         142 :       Rep = upgradePMULDQ(Builder, *CI, /*Signed*/false);
    1559        2947 :     } else if (IsX86 && (Name == "sse41.pmuldq" ||
    1560             :                          Name == "avx2.pmul.dq" ||
    1561             :                          Name == "avx512.pmul.dq.512" ||
    1562             :                          Name.startswith("avx512.mask.pmul.dq."))) {
    1563         110 :       Rep = upgradePMULDQ(Builder, *CI, /*Signed*/true);
    1564        2837 :     } else if (IsX86 && (Name == "sse.cvtsi2ss" ||
    1565             :                          Name == "sse2.cvtsi2sd" ||
    1566             :                          Name == "sse.cvtsi642ss" ||
    1567             :                          Name == "sse2.cvtsi642sd")) {
    1568          84 :       Rep = Builder.CreateSIToFP(CI->getArgOperand(1),
    1569             :                                  CI->getType()->getVectorElementType());
    1570          28 :       Rep = Builder.CreateInsertElement(CI->getArgOperand(0), Rep, (uint64_t)0);
    1571        2809 :     } else if (IsX86 && Name == "avx512.cvtusi2sd") {
    1572           3 :       Rep = Builder.CreateUIToFP(CI->getArgOperand(1),
    1573             :                                  CI->getType()->getVectorElementType());
    1574           1 :       Rep = Builder.CreateInsertElement(CI->getArgOperand(0), Rep, (uint64_t)0);
    1575        2808 :     } else if (IsX86 && Name == "sse2.cvtss2sd") {
    1576          14 :       Rep = Builder.CreateExtractElement(CI->getArgOperand(1), (uint64_t)0);
    1577          14 :       Rep = Builder.CreateFPExt(Rep, CI->getType()->getVectorElementType());
    1578           7 :       Rep = Builder.CreateInsertElement(CI->getArgOperand(0), Rep, (uint64_t)0);
    1579        2801 :     } else if (IsX86 && (Name == "sse2.cvtdq2pd" ||
    1580             :                          Name == "sse2.cvtps2pd" ||
    1581             :                          Name == "avx.cvtdq2.pd.256" ||
    1582             :                          Name == "avx.cvt.ps2.pd.256" ||
    1583             :                          Name == "avx512.mask.cvtps2pd.128" ||
    1584             :                          Name == "avx512.mask.cvtps2pd.256" ||
    1585             :                          Name.startswith("avx512.mask.cvtdq2pd.") ||
    1586             :                          Name.startswith("avx512.mask.cvtudq2pd."))) {
    1587             :       // Lossless i32/float to double conversion.
    1588             :       // Extract the bottom elements if necessary and convert to double vector.
    1589          35 :       Value *Src = CI->getArgOperand(0);
    1590          35 :       VectorType *SrcTy = cast<VectorType>(Src->getType());
    1591          35 :       VectorType *DstTy = cast<VectorType>(CI->getType());
    1592          35 :       Rep = CI->getArgOperand(0);
    1593             : 
    1594          35 :       unsigned NumDstElts = DstTy->getNumElements();
    1595          35 :       if (NumDstElts < SrcTy->getNumElements()) {
    1596             :         assert(NumDstElts == 2 && "Unexpected vector size");
    1597          16 :         uint32_t ShuffleMask[2] = { 0, 1 };
    1598          16 :         Rep = Builder.CreateShuffleVector(Rep, UndefValue::get(SrcTy),
    1599             :                                           ShuffleMask);
    1600             :       }
    1601             : 
    1602          35 :       bool SInt2Double = (StringRef::npos != Name.find("cvtdq2"));
    1603          35 :       bool UInt2Double = (StringRef::npos != Name.find("cvtudq2"));
    1604          35 :       if (SInt2Double)
    1605          34 :         Rep = Builder.CreateSIToFP(Rep, DstTy, "cvtdq2pd");
    1606          18 :       else if (UInt2Double)
    1607          12 :         Rep = Builder.CreateUIToFP(Rep, DstTy, "cvtudq2pd");
    1608             :       else
    1609          24 :         Rep = Builder.CreateFPExt(Rep, DstTy, "cvtps2pd");
    1610             : 
    1611          70 :       if (CI->getNumArgOperands() == 3)
    1612          32 :         Rep = EmitX86Select(Builder, CI->getArgOperand(2), Rep,
    1613             :                             CI->getArgOperand(1));
    1614        2766 :     } else if (IsX86 && (Name.startswith("avx512.mask.loadu."))) {
    1615         120 :       Rep = UpgradeMaskedLoad(Builder, CI->getArgOperand(0),
    1616             :                               CI->getArgOperand(1), CI->getArgOperand(2),
    1617             :                               /*Aligned*/false);
    1618        2706 :     } else if (IsX86 && (Name.startswith("avx512.mask.load."))) {
    1619          72 :       Rep = UpgradeMaskedLoad(Builder, CI->getArgOperand(0),
    1620             :                               CI->getArgOperand(1),CI->getArgOperand(2),
    1621             :                               /*Aligned*/true);
    1622        2670 :     } else if (IsX86 && Name.startswith("xop.vpcom")) {
    1623             :       Intrinsic::ID intID;
    1624             :       if (Name.endswith("ub"))
    1625             :         intID = Intrinsic::x86_xop_vpcomub;
    1626             :       else if (Name.endswith("uw"))
    1627             :         intID = Intrinsic::x86_xop_vpcomuw;
    1628             :       else if (Name.endswith("ud"))
    1629             :         intID = Intrinsic::x86_xop_vpcomud;
    1630             :       else if (Name.endswith("uq"))
    1631             :         intID = Intrinsic::x86_xop_vpcomuq;
    1632             :       else if (Name.endswith("b"))
    1633             :         intID = Intrinsic::x86_xop_vpcomb;
    1634             :       else if (Name.endswith("w"))
    1635             :         intID = Intrinsic::x86_xop_vpcomw;
    1636             :       else if (Name.endswith("d"))
    1637             :         intID = Intrinsic::x86_xop_vpcomd;
    1638             :       else if (Name.endswith("q"))
    1639             :         intID = Intrinsic::x86_xop_vpcomq;
    1640             :       else
    1641           0 :         llvm_unreachable("Unknown suffix");
    1642             : 
    1643          81 :       Name = Name.substr(9); // strip off "xop.vpcom"
    1644             :       unsigned Imm;
    1645             :       if (Name.startswith("lt"))
    1646             :         Imm = 0;
    1647             :       else if (Name.startswith("le"))
    1648             :         Imm = 1;
    1649             :       else if (Name.startswith("gt"))
    1650             :         Imm = 2;
    1651             :       else if (Name.startswith("ge"))
    1652             :         Imm = 3;
    1653             :       else if (Name.startswith("eq"))
    1654             :         Imm = 4;
    1655             :       else if (Name.startswith("ne"))
    1656             :         Imm = 5;
    1657             :       else if (Name.startswith("false"))
    1658             :         Imm = 6;
    1659             :       else if (Name.startswith("true"))
    1660             :         Imm = 7;
    1661             :       else
    1662           0 :         llvm_unreachable("Unknown condition");
    1663             : 
    1664          81 :       Function *VPCOM = Intrinsic::getDeclaration(F->getParent(), intID);
    1665          81 :       Rep =
    1666         405 :           Builder.CreateCall(VPCOM, {CI->getArgOperand(0), CI->getArgOperand(1),
    1667          81 :                                      Builder.getInt8(Imm)});
    1668        2589 :     } else if (IsX86 && Name.startswith("xop.vpcmov")) {
    1669          12 :       Value *Sel = CI->getArgOperand(2);
    1670          12 :       Value *NotSel = Builder.CreateNot(Sel);
    1671          12 :       Value *Sel0 = Builder.CreateAnd(CI->getArgOperand(0), Sel);
    1672          12 :       Value *Sel1 = Builder.CreateAnd(CI->getArgOperand(1), NotSel);
    1673          12 :       Rep = Builder.CreateOr(Sel0, Sel1);
    1674        2577 :     } else if (IsX86 && Name == "sse42.crc32.64.8") {
    1675          80 :       Function *CRC32 = Intrinsic::getDeclaration(F->getParent(),
    1676          40 :                                                Intrinsic::x86_sse42_crc32_32_8);
    1677          80 :       Value *Trunc0 = Builder.CreateTrunc(CI->getArgOperand(0), Type::getInt32Ty(C));
    1678         120 :       Rep = Builder.CreateCall(CRC32, {Trunc0, CI->getArgOperand(1)});
    1679          80 :       Rep = Builder.CreateZExt(Rep, CI->getType(), "");
    1680        2537 :     } else if (IsX86 && (Name.startswith("avx.vbroadcast.s") ||
    1681             :                          Name.startswith("avx512.vbroadcast.s"))) {
    1682             :       // Replace broadcasts with a series of insertelements.
    1683           5 :       Type *VecTy = CI->getType();
    1684           5 :       Type *EltTy = VecTy->getVectorElementType();
    1685             :       unsigned EltNum = VecTy->getVectorNumElements();
    1686          10 :       Value *Cast = Builder.CreateBitCast(CI->getArgOperand(0),
    1687           5 :                                           EltTy->getPointerTo());
    1688           5 :       Value *Load = Builder.CreateLoad(EltTy, Cast);
    1689           5 :       Type *I32Ty = Type::getInt32Ty(C);
    1690           5 :       Rep = UndefValue::get(VecTy);
    1691          85 :       for (unsigned I = 0; I < EltNum; ++I)
    1692          40 :         Rep = Builder.CreateInsertElement(Rep, Load,
    1693          40 :                                           ConstantInt::get(I32Ty, I));
    1694        2532 :     } else if (IsX86 && (Name.startswith("sse41.pmovsx") ||
    1695             :                          Name.startswith("sse41.pmovzx") ||
    1696             :                          Name.startswith("avx2.pmovsx") ||
    1697             :                          Name.startswith("avx2.pmovzx") ||
    1698             :                          Name.startswith("avx512.mask.pmovsx") ||
    1699             :                          Name.startswith("avx512.mask.pmovzx"))) {
    1700         408 :       VectorType *SrcTy = cast<VectorType>(CI->getArgOperand(0)->getType());
    1701         204 :       VectorType *DstTy = cast<VectorType>(CI->getType());
    1702         204 :       unsigned NumDstElts = DstTy->getNumElements();
    1703             : 
    1704             :       // Extract a subvector of the first NumDstElts lanes and sign/zero extend.
    1705         408 :       SmallVector<uint32_t, 8> ShuffleMask(NumDstElts);
    1706        3460 :       for (unsigned i = 0; i != NumDstElts; ++i)
    1707        3256 :         ShuffleMask[i] = i;
    1708             : 
    1709         204 :       Value *SV = Builder.CreateShuffleVector(
    1710         408 :           CI->getArgOperand(0), UndefValue::get(SrcTy), ShuffleMask);
    1711             : 
    1712         204 :       bool DoSext = (StringRef::npos != Name.find("pmovsx"));
    1713         612 :       Rep = DoSext ? Builder.CreateSExt(SV, DstTy)
    1714         204 :                    : Builder.CreateZExt(SV, DstTy);
    1715             :       // If there are 3 arguments, it's a masked intrinsic so we need a select.
    1716         408 :       if (CI->getNumArgOperands() == 3)
    1717         228 :         Rep = EmitX86Select(Builder, CI->getArgOperand(2), Rep,
    1718             :                             CI->getArgOperand(1));
    1719        2328 :     } else if (IsX86 && (Name.startswith("avx.vbroadcastf128") ||
    1720             :                          Name == "avx2.vbroadcasti128")) {
    1721             :       // Replace vbroadcastf128/vbroadcasti128 with a vector load+shuffle.
    1722           5 :       Type *EltTy = CI->getType()->getVectorElementType();
    1723           5 :       unsigned NumSrcElts = 128 / EltTy->getPrimitiveSizeInBits();
    1724           5 :       Type *VT = VectorType::get(EltTy, NumSrcElts);
    1725          10 :       Value *Op = Builder.CreatePointerCast(CI->getArgOperand(0),
    1726           5 :                                             PointerType::getUnqual(VT));
    1727           5 :       Value *Load = Builder.CreateAlignedLoad(Op, 1);
    1728           5 :       if (NumSrcElts == 2)
    1729           3 :         Rep = Builder.CreateShuffleVector(Load, UndefValue::get(Load->getType()),
    1730             :                                           { 0, 1, 0, 1 });
    1731             :       else
    1732           2 :         Rep = Builder.CreateShuffleVector(Load, UndefValue::get(Load->getType()),
    1733             :                                           { 0, 1, 2, 3, 0, 1, 2, 3 });
    1734        2323 :     } else if (IsX86 && (Name.startswith("avx512.mask.shuf.i") ||
    1735             :                          Name.startswith("avx512.mask.shuf.f"))) {
    1736          38 :       unsigned Imm = cast<ConstantInt>(CI->getArgOperand(2))->getZExtValue();
    1737          19 :       Type *VT = CI->getType();
    1738          19 :       unsigned NumLanes = VT->getPrimitiveSizeInBits() / 128;
    1739          19 :       unsigned NumElementsInLane = 128 / VT->getScalarSizeInBits();
    1740          19 :       unsigned ControlBitsMask = NumLanes - 1;
    1741          19 :       unsigned NumControlBits = NumLanes / 2;
    1742          38 :       SmallVector<uint32_t, 8> ShuffleMask(0);
    1743             : 
    1744         131 :       for (unsigned l = 0; l != NumLanes; ++l) {
    1745          56 :         unsigned LaneMask = (Imm >> (l * NumControlBits)) & ControlBitsMask;
    1746             :         // We actually need the other source.
    1747          56 :         if (l >= NumLanes / 2)
    1748          28 :           LaneMask += NumLanes;
    1749         384 :         for (unsigned i = 0; i != NumElementsInLane; ++i)
    1750         164 :           ShuffleMask.push_back(LaneMask * NumElementsInLane + i);
    1751             :       }
    1752          19 :       Rep = Builder.CreateShuffleVector(CI->getArgOperand(0),
    1753             :                                         CI->getArgOperand(1), ShuffleMask);
    1754          19 :       Rep = EmitX86Select(Builder, CI->getArgOperand(4), Rep,
    1755             :                           CI->getArgOperand(3));
    1756        2304 :     }else if (IsX86 && (Name.startswith("avx512.mask.broadcastf") ||
    1757             :                          Name.startswith("avx512.mask.broadcasti"))) {
    1758             :       unsigned NumSrcElts =
    1759         126 :                         CI->getArgOperand(0)->getType()->getVectorNumElements();
    1760          63 :       unsigned NumDstElts = CI->getType()->getVectorNumElements();
    1761             : 
    1762         126 :       SmallVector<uint32_t, 8> ShuffleMask(NumDstElts);
    1763        1335 :       for (unsigned i = 0; i != NumDstElts; ++i)
    1764        1272 :         ShuffleMask[i] = i % NumSrcElts;
    1765             : 
    1766          63 :       Rep = Builder.CreateShuffleVector(CI->getArgOperand(0),
    1767             :                                         CI->getArgOperand(0),
    1768             :                                         ShuffleMask);
    1769          63 :       Rep = EmitX86Select(Builder, CI->getArgOperand(2), Rep,
    1770             :                           CI->getArgOperand(1));
    1771        2241 :     } else if (IsX86 && (Name.startswith("avx2.pbroadcast") ||
    1772             :                          Name.startswith("avx2.vbroadcast") ||
    1773             :                          Name.startswith("avx512.pbroadcast") ||
    1774             :                          Name.startswith("avx512.mask.broadcast.s"))) {
    1775             :       // Replace vp?broadcasts with a vector shuffle.
    1776          95 :       Value *Op = CI->getArgOperand(0);
    1777          95 :       unsigned NumElts = CI->getType()->getVectorNumElements();
    1778          95 :       Type *MaskTy = VectorType::get(Type::getInt32Ty(C), NumElts);
    1779          95 :       Rep = Builder.CreateShuffleVector(Op, UndefValue::get(Op->getType()),
    1780          95 :                                         Constant::getNullValue(MaskTy));
    1781             : 
    1782          95 :       if (CI->getNumArgOperands() == 3)
    1783         102 :         Rep = EmitX86Select(Builder, CI->getArgOperand(2), Rep,
    1784             :                             CI->getArgOperand(1));
    1785        2146 :     } else if (IsX86 && Name.startswith("avx512.mask.palignr.")) {
    1786          24 :       Rep = UpgradeX86ALIGNIntrinsics(Builder, CI->getArgOperand(0),
    1787             :                                       CI->getArgOperand(1),
    1788             :                                       CI->getArgOperand(2),
    1789             :                                       CI->getArgOperand(3),
    1790             :                                       CI->getArgOperand(4),
    1791             :                                       false);
    1792        2134 :     } else if (IsX86 && Name.startswith("avx512.mask.valign.")) {
    1793          24 :       Rep = UpgradeX86ALIGNIntrinsics(Builder, CI->getArgOperand(0),
    1794             :                                       CI->getArgOperand(1),
    1795             :                                       CI->getArgOperand(2),
    1796             :                                       CI->getArgOperand(3),
    1797             :                                       CI->getArgOperand(4),
    1798             :                                       true);
    1799        2122 :     } else if (IsX86 && (Name == "sse2.psll.dq" ||
    1800             :                          Name == "avx2.psll.dq")) {
    1801             :       // 128/256-bit shift left specified in bits.
    1802          14 :       unsigned Shift = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue();
    1803          14 :       Rep = UpgradeX86PSLLDQIntrinsics(Builder, CI->getArgOperand(0),
    1804             :                                        Shift / 8); // Shift is in bits.
    1805        2115 :     } else if (IsX86 && (Name == "sse2.psrl.dq" ||
    1806             :                          Name == "avx2.psrl.dq")) {
    1807             :       // 128/256-bit shift right specified in bits.
    1808         202 :       unsigned Shift = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue();
    1809         202 :       Rep = UpgradeX86PSRLDQIntrinsics(Builder, CI->getArgOperand(0),
    1810             :                                        Shift / 8); // Shift is in bits.
    1811        2014 :     } else if (IsX86 && (Name == "sse2.psll.dq.bs" ||
    1812             :                          Name == "avx2.psll.dq.bs" ||
    1813             :                          Name == "avx512.psll.dq.512")) {
    1814             :       // 128/256/512-bit shift left specified in bytes.
    1815          22 :       unsigned Shift = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue();
    1816          11 :       Rep = UpgradeX86PSLLDQIntrinsics(Builder, CI->getArgOperand(0), Shift);
    1817        2003 :     } else if (IsX86 && (Name == "sse2.psrl.dq.bs" ||
    1818             :                          Name == "avx2.psrl.dq.bs" ||
    1819             :                          Name == "avx512.psrl.dq.512")) {
    1820             :       // 128/256/512-bit shift right specified in bytes.
    1821          22 :       unsigned Shift = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue();
    1822          11 :       Rep = UpgradeX86PSRLDQIntrinsics(Builder, CI->getArgOperand(0), Shift);
    1823        1992 :     } else if (IsX86 && (Name == "sse41.pblendw" ||
    1824             :                          Name.startswith("sse41.blendp") ||
    1825             :                          Name.startswith("avx.blend.p") ||
    1826             :                          Name == "avx2.pblendw" ||
    1827             :                          Name.startswith("avx2.pblendd."))) {
    1828          76 :       Value *Op0 = CI->getArgOperand(0);
    1829             :       Value *Op1 = CI->getArgOperand(1);
    1830          76 :       unsigned Imm = cast <ConstantInt>(CI->getArgOperand(2))->getZExtValue();
    1831          76 :       VectorType *VecTy = cast<VectorType>(CI->getType());
    1832          76 :       unsigned NumElts = VecTy->getNumElements();
    1833             : 
    1834         152 :       SmallVector<uint32_t, 16> Idxs(NumElts);
    1835        1196 :       for (unsigned i = 0; i != NumElts; ++i)
    1836        1120 :         Idxs[i] = ((Imm >> (i%8)) & 1) ? i + NumElts : i;
    1837             : 
    1838          76 :       Rep = Builder.CreateShuffleVector(Op0, Op1, Idxs);
    1839        1916 :     } else if (IsX86 && (Name.startswith("avx.vinsertf128.") ||
    1840             :                          Name == "avx2.vinserti128" ||
    1841             :                          Name.startswith("avx512.mask.insert"))) {
    1842          58 :       Value *Op0 = CI->getArgOperand(0);
    1843             :       Value *Op1 = CI->getArgOperand(1);
    1844          58 :       unsigned Imm = cast<ConstantInt>(CI->getArgOperand(2))->getZExtValue();
    1845          58 :       unsigned DstNumElts = CI->getType()->getVectorNumElements();
    1846          58 :       unsigned SrcNumElts = Op1->getType()->getVectorNumElements();
    1847          58 :       unsigned Scale = DstNumElts / SrcNumElts;
    1848             : 
    1849             :       // Mask off the high bits of the immediate value; hardware ignores those.
    1850          58 :       Imm = Imm % Scale;
    1851             : 
    1852             :       // Extend the second operand into a vector the size of the destination.
    1853          58 :       Value *UndefV = UndefValue::get(Op1->getType());
    1854         116 :       SmallVector<uint32_t, 8> Idxs(DstNumElts);
    1855         494 :       for (unsigned i = 0; i != SrcNumElts; ++i)
    1856         436 :         Idxs[i] = i;
    1857         638 :       for (unsigned i = SrcNumElts; i != DstNumElts; ++i)
    1858         580 :         Idxs[i] = SrcNumElts;
    1859          58 :       Rep = Builder.CreateShuffleVector(Op1, UndefV, Idxs);
    1860             : 
    1861             :       // Insert the second operand into the first operand.
    1862             : 
    1863             :       // Note that there is no guarantee that instruction lowering will actually
    1864             :       // produce a vinsertf128 instruction for the created shuffles. In
    1865             :       // particular, the 0 immediate case involves no lane changes, so it can
    1866             :       // be handled as a blend.
    1867             : 
    1868             :       // Example of shuffle mask for 32-bit elements:
    1869             :       // Imm = 1  <i32 0, i32 1, i32 2,  i32 3,  i32 8, i32 9, i32 10, i32 11>
    1870             :       // Imm = 0  <i32 8, i32 9, i32 10, i32 11, i32 4, i32 5, i32 6,  i32 7 >
    1871             : 
    1872             :       // First fill with identify mask.
    1873        1074 :       for (unsigned i = 0; i != DstNumElts; ++i)
    1874        1016 :         Idxs[i] = i;
    1875             :       // Then replace the elements where we need to insert.
    1876         494 :       for (unsigned i = 0; i != SrcNumElts; ++i)
    1877         436 :         Idxs[i + Imm * SrcNumElts] = i + DstNumElts;
    1878          58 :       Rep = Builder.CreateShuffleVector(Op0, Rep, Idxs);
    1879             : 
    1880             :       // If the intrinsic has a mask operand, handle that.
    1881          58 :       if (CI->getNumArgOperands() == 5)
    1882          72 :         Rep = EmitX86Select(Builder, CI->getArgOperand(4), Rep,
    1883             :                             CI->getArgOperand(3));
    1884        1858 :     } else if (IsX86 && (Name.startswith("avx.vextractf128.") ||
    1885             :                          Name == "avx2.vextracti128" ||
    1886             :                          Name.startswith("avx512.mask.vextract"))) {
    1887          43 :       Value *Op0 = CI->getArgOperand(0);
    1888          43 :       unsigned Imm = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue();
    1889          43 :       unsigned DstNumElts = CI->getType()->getVectorNumElements();
    1890          43 :       unsigned SrcNumElts = Op0->getType()->getVectorNumElements();
    1891          43 :       unsigned Scale = SrcNumElts / DstNumElts;
    1892             : 
    1893             :       // Mask off the high bits of the immediate value; hardware ignores those.
    1894          43 :       Imm = Imm % Scale;
    1895             : 
    1896             :       // Get indexes for the subvector of the input vector.
    1897          86 :       SmallVector<uint32_t, 8> Idxs(DstNumElts);
    1898         347 :       for (unsigned i = 0; i != DstNumElts; ++i) {
    1899         304 :         Idxs[i] = i + (Imm * DstNumElts);
    1900             :       }
    1901          43 :       Rep = Builder.CreateShuffleVector(Op0, Op0, Idxs);
    1902             : 
    1903             :       // If the intrinsic has a mask operand, handle that.
    1904          43 :       if (CI->getNumArgOperands() == 4)
    1905          32 :         Rep = EmitX86Select(Builder, CI->getArgOperand(3), Rep,
    1906             :                             CI->getArgOperand(2));
    1907        1815 :     } else if (!IsX86 && Name == "stackprotectorcheck") {
    1908           6 :       Rep = nullptr;
    1909        1809 :     } else if (IsX86 && (Name.startswith("avx512.mask.perm.df.") ||
    1910             :                          Name.startswith("avx512.mask.perm.di."))) {
    1911          12 :       Value *Op0 = CI->getArgOperand(0);
    1912          12 :       unsigned Imm = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue();
    1913          12 :       VectorType *VecTy = cast<VectorType>(CI->getType());
    1914          12 :       unsigned NumElts = VecTy->getNumElements();
    1915             : 
    1916          24 :       SmallVector<uint32_t, 8> Idxs(NumElts);
    1917         156 :       for (unsigned i = 0; i != NumElts; ++i)
    1918         144 :         Idxs[i] = (i & ~0x3) + ((Imm >> (2 * (i & 0x3))) & 3);
    1919             : 
    1920          12 :       Rep = Builder.CreateShuffleVector(Op0, Op0, Idxs);
    1921             : 
    1922          12 :       if (CI->getNumArgOperands() == 4)
    1923          24 :         Rep = EmitX86Select(Builder, CI->getArgOperand(3), Rep,
    1924             :                             CI->getArgOperand(2));
    1925        1797 :     } else if (IsX86 && (Name.startswith("avx.vperm2f128.") ||
    1926             :                          Name == "avx2.vperm2i128")) {
    1927             :       // The immediate permute control byte looks like this:
    1928             :       //    [1:0] - select 128 bits from sources for low half of destination
    1929             :       //    [2]   - ignore
    1930             :       //    [3]   - zero low half of destination
    1931             :       //    [5:4] - select 128 bits from sources for high half of destination
    1932             :       //    [6]   - ignore
    1933             :       //    [7]   - zero high half of destination
    1934             : 
    1935          20 :       uint8_t Imm = cast<ConstantInt>(CI->getArgOperand(2))->getZExtValue();
    1936             : 
    1937          10 :       unsigned NumElts = CI->getType()->getVectorNumElements();
    1938          10 :       unsigned HalfSize = NumElts / 2;
    1939          20 :       SmallVector<uint32_t, 8> ShuffleMask(NumElts);
    1940             : 
    1941             :       // Determine which operand(s) are actually in use for this instruction.
    1942          10 :       Value *V0 = (Imm & 0x02) ? CI->getArgOperand(1) : CI->getArgOperand(0);
    1943          10 :       Value *V1 = (Imm & 0x20) ? CI->getArgOperand(1) : CI->getArgOperand(0);
    1944             : 
    1945             :       // If needed, replace operands based on zero mask.
    1946          10 :       V0 = (Imm & 0x08) ? ConstantAggregateZero::get(CI->getType()) : V0;
    1947          10 :       V1 = (Imm & 0x80) ? ConstantAggregateZero::get(CI->getType()) : V1;
    1948             : 
    1949             :       // Permute low half of result.
    1950          10 :       unsigned StartIndex = (Imm & 0x01) ? HalfSize : 0;
    1951          66 :       for (unsigned i = 0; i < HalfSize; ++i)
    1952          56 :         ShuffleMask[i] = StartIndex + i;
    1953             : 
    1954             :       // Permute high half of result.
    1955          10 :       StartIndex = (Imm & 0x10) ? HalfSize : 0;
    1956          66 :       for (unsigned i = 0; i < HalfSize; ++i)
    1957          56 :         ShuffleMask[i + HalfSize] = NumElts + StartIndex + i;
    1958             : 
    1959          10 :       Rep = Builder.CreateShuffleVector(V0, V1, ShuffleMask);
    1960             : 
    1961        1787 :     } else if (IsX86 && (Name.startswith("avx.vpermil.") ||
    1962             :                          Name == "sse2.pshuf.d" ||
    1963             :                          Name.startswith("avx512.mask.vpermil.p") ||
    1964             :                          Name.startswith("avx512.mask.pshuf.d."))) {
    1965         108 :       Value *Op0 = CI->getArgOperand(0);
    1966         108 :       unsigned Imm = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue();
    1967         108 :       VectorType *VecTy = cast<VectorType>(CI->getType());
    1968         108 :       unsigned NumElts = VecTy->getNumElements();
    1969             :       // Calculate the size of each index in the immediate.
    1970         108 :       unsigned IdxSize = 64 / VecTy->getScalarSizeInBits();
    1971         108 :       unsigned IdxMask = ((1 << IdxSize) - 1);
    1972             : 
    1973         216 :       SmallVector<uint32_t, 8> Idxs(NumElts);
    1974             :       // Lookup the bits for this element, wrapping around the immediate every
    1975             :       // 8-bits. Elements are grouped into sets of 2 or 4 elements so we need
    1976             :       // to offset by the first index of each group.
    1977        1184 :       for (unsigned i = 0; i != NumElts; ++i)
    1978        1076 :         Idxs[i] = ((Imm >> ((i * IdxSize) % 8)) & IdxMask) | (i & ~IdxMask);
    1979             : 
    1980         108 :       Rep = Builder.CreateShuffleVector(Op0, Op0, Idxs);
    1981             : 
    1982         108 :       if (CI->getNumArgOperands() == 4)
    1983          54 :         Rep = EmitX86Select(Builder, CI->getArgOperand(3), Rep,
    1984             :                             CI->getArgOperand(2));
    1985        1679 :     } else if (IsX86 && (Name == "sse2.pshufl.w" ||
    1986             :                          Name.startswith("avx512.mask.pshufl.w."))) {
    1987          67 :       Value *Op0 = CI->getArgOperand(0);
    1988          67 :       unsigned Imm = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue();
    1989          67 :       unsigned NumElts = CI->getType()->getVectorNumElements();
    1990             : 
    1991         134 :       SmallVector<uint32_t, 16> Idxs(NumElts);
    1992         243 :       for (unsigned l = 0; l != NumElts; l += 8) {
    1993         792 :         for (unsigned i = 0; i != 4; ++i)
    1994         704 :           Idxs[i + l] = ((Imm >> (2 * i)) & 0x3) + l;
    1995         792 :         for (unsigned i = 4; i != 8; ++i)
    1996         704 :           Idxs[i + l] = i + l;
    1997             :       }
    1998             : 
    1999          67 :       Rep = Builder.CreateShuffleVector(Op0, Op0, Idxs);
    2000             : 
    2001          67 :       if (CI->getNumArgOperands() == 4)
    2002          24 :         Rep = EmitX86Select(Builder, CI->getArgOperand(3), Rep,
    2003             :                             CI->getArgOperand(2));
    2004        1612 :     } else if (IsX86 && (Name == "sse2.pshufh.w" ||
    2005             :                          Name.startswith("avx512.mask.pshufh.w."))) {
    2006          49 :       Value *Op0 = CI->getArgOperand(0);
    2007          49 :       unsigned Imm = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue();
    2008          49 :       unsigned NumElts = CI->getType()->getVectorNumElements();
    2009             : 
    2010          98 :       SmallVector<uint32_t, 16> Idxs(NumElts);
    2011         189 :       for (unsigned l = 0; l != NumElts; l += 8) {
    2012         630 :         for (unsigned i = 0; i != 4; ++i)
    2013         560 :           Idxs[i + l] = i + l;
    2014         630 :         for (unsigned i = 0; i != 4; ++i)
    2015         560 :           Idxs[i + l + 4] = ((Imm >> (2 * i)) & 0x3) + 4 + l;
    2016             :       }
    2017             : 
    2018          49 :       Rep = Builder.CreateShuffleVector(Op0, Op0, Idxs);
    2019             : 
    2020          49 :       if (CI->getNumArgOperands() == 4)
    2021          24 :         Rep = EmitX86Select(Builder, CI->getArgOperand(3), Rep,
    2022             :                             CI->getArgOperand(2));
    2023        1563 :     } else if (IsX86 && Name.startswith("avx512.mask.shuf.p")) {
    2024          14 :       Value *Op0 = CI->getArgOperand(0);
    2025             :       Value *Op1 = CI->getArgOperand(1);
    2026          14 :       unsigned Imm = cast<ConstantInt>(CI->getArgOperand(2))->getZExtValue();
    2027          14 :       unsigned NumElts = CI->getType()->getVectorNumElements();
    2028             : 
    2029          14 :       unsigned NumLaneElts = 128/CI->getType()->getScalarSizeInBits();
    2030          14 :       unsigned HalfLaneElts = NumLaneElts / 2;
    2031             : 
    2032          28 :       SmallVector<uint32_t, 16> Idxs(NumElts);
    2033         202 :       for (unsigned i = 0; i != NumElts; ++i) {
    2034             :         // Base index is the starting element of the lane.
    2035         188 :         Idxs[i] = i - (i % NumLaneElts);
    2036             :         // If we are half way through the lane switch to the other source.
    2037          94 :         if ((i % NumLaneElts) >= HalfLaneElts)
    2038          47 :           Idxs[i] += NumElts;
    2039             :         // Now select the specific element. By adding HalfLaneElts bits from
    2040             :         // the immediate. Wrapping around the immediate every 8-bits.
    2041          94 :         Idxs[i] += (Imm >> ((i * HalfLaneElts) % 8)) & ((1 << HalfLaneElts) - 1);
    2042             :       }
    2043             : 
    2044          14 :       Rep = Builder.CreateShuffleVector(Op0, Op1, Idxs);
    2045             : 
    2046          14 :       Rep = EmitX86Select(Builder, CI->getArgOperand(4), Rep,
    2047             :                           CI->getArgOperand(3));
    2048        1549 :     } else if (IsX86 && (Name.startswith("avx512.mask.movddup") ||
    2049             :                          Name.startswith("avx512.mask.movshdup") ||
    2050             :                          Name.startswith("avx512.mask.movsldup"))) {
    2051          27 :       Value *Op0 = CI->getArgOperand(0);
    2052          27 :       unsigned NumElts = CI->getType()->getVectorNumElements();
    2053          27 :       unsigned NumLaneElts = 128/CI->getType()->getScalarSizeInBits();
    2054             : 
    2055             :       unsigned Offset = 0;
    2056             :       if (Name.startswith("avx512.mask.movshdup."))
    2057             :         Offset = 1;
    2058             : 
    2059          54 :       SmallVector<uint32_t, 16> Idxs(NumElts);
    2060         153 :       for (unsigned l = 0; l != NumElts; l += NumLaneElts)
    2061         273 :         for (unsigned i = 0; i != NumLaneElts; i += 2) {
    2062         210 :           Idxs[i + l + 0] = i + l + Offset;
    2063         210 :           Idxs[i + l + 1] = i + l + Offset;
    2064             :         }
    2065             : 
    2066          27 :       Rep = Builder.CreateShuffleVector(Op0, Op0, Idxs);
    2067             : 
    2068          27 :       Rep = EmitX86Select(Builder, CI->getArgOperand(2), Rep,
    2069             :                           CI->getArgOperand(1));
    2070        1522 :     } else if (IsX86 && (Name.startswith("avx512.mask.punpckl") ||
    2071             :                          Name.startswith("avx512.mask.unpckl."))) {
    2072          41 :       Value *Op0 = CI->getArgOperand(0);
    2073             :       Value *Op1 = CI->getArgOperand(1);
    2074          82 :       int NumElts = CI->getType()->getVectorNumElements();
    2075          41 :       int NumLaneElts = 128/CI->getType()->getScalarSizeInBits();
    2076             : 
    2077          82 :       SmallVector<uint32_t, 64> Idxs(NumElts);
    2078         249 :       for (int l = 0; l != NumElts; l += NumLaneElts)
    2079        1512 :         for (int i = 0; i != NumLaneElts; ++i)
    2080        1408 :           Idxs[i + l] = l + (i / 2) + NumElts * (i % 2);
    2081             : 
    2082          41 :       Rep = Builder.CreateShuffleVector(Op0, Op1, Idxs);
    2083             : 
    2084          41 :       Rep = EmitX86Select(Builder, CI->getArgOperand(3), Rep,
    2085             :                           CI->getArgOperand(2));
    2086        1481 :     } else if (IsX86 && (Name.startswith("avx512.mask.punpckh") ||
    2087             :                          Name.startswith("avx512.mask.unpckh."))) {
    2088          40 :       Value *Op0 = CI->getArgOperand(0);
    2089             :       Value *Op1 = CI->getArgOperand(1);
    2090          80 :       int NumElts = CI->getType()->getVectorNumElements();
    2091          40 :       int NumLaneElts = 128/CI->getType()->getScalarSizeInBits();
    2092             : 
    2093          80 :       SmallVector<uint32_t, 64> Idxs(NumElts);
    2094         240 :       for (int l = 0; l != NumElts; l += NumLaneElts)
    2095        1492 :         for (int i = 0; i != NumLaneElts; ++i)
    2096        1392 :           Idxs[i + l] = (NumLaneElts / 2) + l + (i / 2) + NumElts * (i % 2);
    2097             : 
    2098          40 :       Rep = Builder.CreateShuffleVector(Op0, Op1, Idxs);
    2099             : 
    2100          40 :       Rep = EmitX86Select(Builder, CI->getArgOperand(3), Rep,
    2101             :                           CI->getArgOperand(2));
    2102        1441 :     } else if (IsX86 && Name.startswith("avx512.mask.pand.")) {
    2103          44 :       Rep = Builder.CreateAnd(CI->getArgOperand(0), CI->getArgOperand(1));
    2104          22 :       Rep = EmitX86Select(Builder, CI->getArgOperand(3), Rep,
    2105             :                           CI->getArgOperand(2));
    2106        1419 :     } else if (IsX86 && Name.startswith("avx512.mask.pandn.")) {
    2107          72 :       Rep = Builder.CreateAnd(Builder.CreateNot(CI->getArgOperand(0)),
    2108             :                               CI->getArgOperand(1));
    2109          36 :       Rep = EmitX86Select(Builder, CI->getArgOperand(3), Rep,
    2110             :                           CI->getArgOperand(2));
    2111        1383 :     } else if (IsX86 && Name.startswith("avx512.mask.por.")) {
    2112          44 :       Rep = Builder.CreateOr(CI->getArgOperand(0), CI->getArgOperand(1));
    2113          22 :       Rep = EmitX86Select(Builder, CI->getArgOperand(3), Rep,
    2114             :                           CI->getArgOperand(2));
    2115        1361 :     } else if (IsX86 && Name.startswith("avx512.mask.pxor.")) {
    2116          44 :       Rep = Builder.CreateXor(CI->getArgOperand(0), CI->getArgOperand(1));
    2117          22 :       Rep = EmitX86Select(Builder, CI->getArgOperand(3), Rep,
    2118             :                           CI->getArgOperand(2));
    2119        1339 :     } else if (IsX86 && Name.startswith("avx512.mask.and.")) {
    2120          27 :       VectorType *FTy = cast<VectorType>(CI->getType());
    2121          27 :       VectorType *ITy = VectorType::getInteger(FTy);
    2122          54 :       Rep = Builder.CreateAnd(Builder.CreateBitCast(CI->getArgOperand(0), ITy),
    2123             :                               Builder.CreateBitCast(CI->getArgOperand(1), ITy));
    2124          27 :       Rep = Builder.CreateBitCast(Rep, FTy);
    2125          27 :       Rep = EmitX86Select(Builder, CI->getArgOperand(3), Rep,
    2126             :                           CI->getArgOperand(2));
    2127        1312 :     } else if (IsX86 && Name.startswith("avx512.mask.andn.")) {
    2128          27 :       VectorType *FTy = cast<VectorType>(CI->getType());
    2129          27 :       VectorType *ITy = VectorType::getInteger(FTy);
    2130          54 :       Rep = Builder.CreateNot(Builder.CreateBitCast(CI->getArgOperand(0), ITy));
    2131          27 :       Rep = Builder.CreateAnd(Rep,
    2132             :                               Builder.CreateBitCast(CI->getArgOperand(1), ITy));
    2133          27 :       Rep = Builder.CreateBitCast(Rep, FTy);
    2134          27 :       Rep = EmitX86Select(Builder, CI->getArgOperand(3), Rep,
    2135             :                           CI->getArgOperand(2));
    2136        1285 :     } else if (IsX86 && Name.startswith("avx512.mask.or.")) {
    2137          27 :       VectorType *FTy = cast<VectorType>(CI->getType());
    2138          27 :       VectorType *ITy = VectorType::getInteger(FTy);
    2139          54 :       Rep = Builder.CreateOr(Builder.CreateBitCast(CI->getArgOperand(0), ITy),
    2140             :                              Builder.CreateBitCast(CI->getArgOperand(1), ITy));
    2141          27 :       Rep = Builder.CreateBitCast(Rep, FTy);
    2142          27 :       Rep = EmitX86Select(Builder, CI->getArgOperand(3), Rep,
    2143             :                           CI->getArgOperand(2));
    2144        1258 :     } else if (IsX86 && Name.startswith("avx512.mask.xor.")) {
    2145          27 :       VectorType *FTy = cast<VectorType>(CI->getType());
    2146          27 :       VectorType *ITy = VectorType::getInteger(FTy);
    2147          54 :       Rep = Builder.CreateXor(Builder.CreateBitCast(CI->getArgOperand(0), ITy),
    2148             :                               Builder.CreateBitCast(CI->getArgOperand(1), ITy));
    2149          27 :       Rep = Builder.CreateBitCast(Rep, FTy);
    2150          27 :       Rep = EmitX86Select(Builder, CI->getArgOperand(3), Rep,
    2151             :                           CI->getArgOperand(2));
    2152        1231 :     } else if (IsX86 && Name.startswith("avx512.mask.padd.")) {
    2153         108 :       Rep = Builder.CreateAdd(CI->getArgOperand(0), CI->getArgOperand(1));
    2154          54 :       Rep = EmitX86Select(Builder, CI->getArgOperand(3), Rep,
    2155             :                           CI->getArgOperand(2));
    2156        1177 :     } else if (IsX86 && Name.startswith("avx512.mask.psub.")) {
    2157         108 :       Rep = Builder.CreateSub(CI->getArgOperand(0), CI->getArgOperand(1));
    2158          54 :       Rep = EmitX86Select(Builder, CI->getArgOperand(3), Rep,
    2159             :                           CI->getArgOperand(2));
    2160        1123 :     } else if (IsX86 && Name.startswith("avx512.mask.pmull.")) {
    2161         108 :       Rep = Builder.CreateMul(CI->getArgOperand(0), CI->getArgOperand(1));
    2162          54 :       Rep = EmitX86Select(Builder, CI->getArgOperand(3), Rep,
    2163             :                           CI->getArgOperand(2));
    2164        1069 :     } else if (IsX86 && (Name.startswith("avx512.mask.add.p"))) {
    2165          12 :       Rep = Builder.CreateFAdd(CI->getArgOperand(0), CI->getArgOperand(1));
    2166           6 :       Rep = EmitX86Select(Builder, CI->getArgOperand(3), Rep,
    2167             :                           CI->getArgOperand(2));
    2168        1063 :     } else if (IsX86 && Name.startswith("avx512.mask.div.p")) {
    2169          12 :       Rep = Builder.CreateFDiv(CI->getArgOperand(0), CI->getArgOperand(1));
    2170           6 :       Rep = EmitX86Select(Builder, CI->getArgOperand(3), Rep,
    2171             :                           CI->getArgOperand(2));
    2172        1057 :     } else if (IsX86 && Name.startswith("avx512.mask.mul.p")) {
    2173          12 :       Rep = Builder.CreateFMul(CI->getArgOperand(0), CI->getArgOperand(1));
    2174           6 :       Rep = EmitX86Select(Builder, CI->getArgOperand(3), Rep,
    2175             :                           CI->getArgOperand(2));
    2176        1051 :     } else if (IsX86 && Name.startswith("avx512.mask.sub.p")) {
    2177          12 :       Rep = Builder.CreateFSub(CI->getArgOperand(0), CI->getArgOperand(1));
    2178           6 :       Rep = EmitX86Select(Builder, CI->getArgOperand(3), Rep,
    2179             :                           CI->getArgOperand(2));
    2180        1045 :     } else if (IsX86 && Name.startswith("avx512.mask.lzcnt.")) {
    2181          65 :       Rep = Builder.CreateCall(Intrinsic::getDeclaration(F->getParent(),
    2182             :                                                          Intrinsic::ctlz,
    2183          26 :                                                          CI->getType()),
    2184          26 :                                { CI->getArgOperand(0), Builder.getInt1(false) });
    2185          13 :       Rep = EmitX86Select(Builder, CI->getArgOperand(2), Rep,
    2186             :                           CI->getArgOperand(1));
    2187        1032 :     } else if (IsX86 && Name.startswith("avx512.mask.psll")) {
    2188          87 :       bool IsImmediate = Name[16] == 'i' ||
    2189          75 :                          (Name.size() > 18 && Name[18] == 'i');
    2190             :       bool IsVariable = Name[16] == 'v';
    2191          87 :       char Size = Name[16] == '.' ? Name[17] :
    2192          36 :                   Name[17] == '.' ? Name[18] :
    2193          24 :                   Name[18] == '.' ? Name[19] :
    2194             :                                     Name[20];
    2195             : 
    2196             :       Intrinsic::ID IID;
    2197         117 :       if (IsVariable && Name[17] != '.') {
    2198          24 :         if (Size == 'd' && Name[17] == '2') // avx512.mask.psllv2.di
    2199             :           IID = Intrinsic::x86_avx2_psllv_q;
    2200          21 :         else if (Size == 'd' && Name[17] == '4') // avx512.mask.psllv4.di
    2201             :           IID = Intrinsic::x86_avx2_psllv_q_256;
    2202          18 :         else if (Size == 's' && Name[17] == '4') // avx512.mask.psllv4.si
    2203             :           IID = Intrinsic::x86_avx2_psllv_d;
    2204          15 :         else if (Size == 's' && Name[17] == '8') // avx512.mask.psllv8.si
    2205             :           IID = Intrinsic::x86_avx2_psllv_d_256;
    2206          12 :         else if (Size == 'h' && Name[17] == '8') // avx512.mask.psllv8.hi
    2207             :           IID = Intrinsic::x86_avx512_psllv_w_128;
    2208           9 :         else if (Size == 'h' && Name[17] == '1') // avx512.mask.psllv16.hi
    2209             :           IID = Intrinsic::x86_avx512_psllv_w_256;
    2210          12 :         else if (Name[17] == '3' && Name[18] == '2') // avx512.mask.psllv32hi
    2211             :           IID = Intrinsic::x86_avx512_psllv_w_512;
    2212             :         else
    2213           0 :           llvm_unreachable("Unexpected size");
    2214             :       } else if (Name.endswith(".128")) {
    2215          12 :         if (Size == 'd') // avx512.mask.psll.d.128, avx512.mask.psll.di.128
    2216           6 :           IID = IsImmediate ? Intrinsic::x86_sse2_pslli_d
    2217             :                             : Intrinsic::x86_sse2_psll_d;
    2218           6 :         else if (Size == 'q') // avx512.mask.psll.q.128, avx512.mask.psll.qi.128
    2219           0 :           IID = IsImmediate ? Intrinsic::x86_sse2_pslli_q
    2220             :                             : Intrinsic::x86_sse2_psll_q;
    2221           6 :         else if (Size == 'w') // avx512.mask.psll.w.128, avx512.mask.psll.wi.128
    2222           6 :           IID = IsImmediate ? Intrinsic::x86_sse2_pslli_w
    2223             :                             : Intrinsic::x86_sse2_psll_w;
    2224             :         else
    2225           0 :           llvm_unreachable("Unexpected size");
    2226             :       } else if (Name.endswith(".256")) {
    2227          15 :         if (Size == 'd') // avx512.mask.psll.d.256, avx512.mask.psll.di.256
    2228           6 :           IID = IsImmediate ? Intrinsic::x86_avx2_pslli_d
    2229             :                             : Intrinsic::x86_avx2_psll_d;
    2230           9 :         else if (Size == 'q') // avx512.mask.psll.q.256, avx512.mask.psll.qi.256
    2231           3 :           IID = IsImmediate ? Intrinsic::x86_avx2_pslli_q
    2232             :                             : Intrinsic::x86_avx2_psll_q;
    2233           6 :         else if (Size == 'w') // avx512.mask.psll.w.256, avx512.mask.psll.wi.256
    2234           6 :           IID = IsImmediate ? Intrinsic::x86_avx2_pslli_w
    2235             :                             : Intrinsic::x86_avx2_psll_w;
    2236             :         else
    2237           0 :           llvm_unreachable("Unexpected size");
    2238             :       } else {
    2239          36 :         if (Size == 'd') // psll.di.512, pslli.d, psll.d, psllv.d.512
    2240          12 :           IID = IsImmediate ? Intrinsic::x86_avx512_pslli_d_512 :
    2241           6 :                 IsVariable  ? Intrinsic::x86_avx512_psllv_d_512 :
    2242             :                               Intrinsic::x86_avx512_psll_d_512;
    2243          24 :         else if (Size == 'q') // psll.qi.512, pslli.q, psll.q, psllv.q.512
    2244          12 :           IID = IsImmediate ? Intrinsic::x86_avx512_pslli_q_512 :
    2245           6 :                 IsVariable  ? Intrinsic::x86_avx512_psllv_q_512 :
    2246             :                               Intrinsic::x86_avx512_psll_q_512;
    2247          12 :         else if (Size == 'w') // psll.wi.512, pslli.w, psll.w
    2248          12 :           IID = IsImmediate ? Intrinsic::x86_avx512_pslli_w_512
    2249             :                             : Intrinsic::x86_avx512_psll_w_512;
    2250             :         else
    2251           0 :           llvm_unreachable("Unexpected size");
    2252             :       }
    2253             : 
    2254          87 :       Rep = UpgradeX86MaskedShift(Builder, *CI, IID);
    2255         945 :     } else if (IsX86 && Name.startswith("avx512.mask.psrl")) {
    2256          97 :       bool IsImmediate = Name[16] == 'i' ||
    2257          85 :                          (Name.size() > 18 && Name[18] == 'i');
    2258             :       bool IsVariable = Name[16] == 'v';
    2259          97 :       char Size = Name[16] == '.' ? Name[17] :
    2260          37 :                   Name[17] == '.' ? Name[18] :
    2261          24 :                   Name[18] == '.' ? Name[19] :
    2262             :                                     Name[20];
    2263             : 
    2264             :       Intrinsic::ID IID;
    2265         128 :       if (IsVariable && Name[17] != '.') {
    2266          24 :         if (Size == 'd' && Name[17] == '2') // avx512.mask.psrlv2.di
    2267             :           IID = Intrinsic::x86_avx2_psrlv_q;
    2268          21 :         else if (Size == 'd' && Name[17] == '4') // avx512.mask.psrlv4.di
    2269             :           IID = Intrinsic::x86_avx2_psrlv_q_256;
    2270          18 :         else if (Size == 's' && Name[17] == '4') // avx512.mask.psrlv4.si
    2271             :           IID = Intrinsic::x86_avx2_psrlv_d;
    2272          15 :         else if (Size == 's' && Name[17] == '8') // avx512.mask.psrlv8.si
    2273             :           IID = Intrinsic::x86_avx2_psrlv_d_256;
    2274          12 :         else if (Size == 'h' && Name[17] == '8') // avx512.mask.psrlv8.hi
    2275             :           IID = Intrinsic::x86_avx512_psrlv_w_128;
    2276           9 :         else if (Size == 'h' && Name[17] == '1') // avx512.mask.psrlv16.hi
    2277             :           IID = Intrinsic::x86_avx512_psrlv_w_256;
    2278          12 :         else if (Name[17] == '3' && Name[18] == '2') // avx512.mask.psrlv32hi
    2279             :           IID = Intrinsic::x86_avx512_psrlv_w_512;
    2280             :         else
    2281           0 :           llvm_unreachable("Unexpected size");
    2282             :       } else if (Name.endswith(".128")) {
    2283          18 :         if (Size == 'd') // avx512.mask.psrl.d.128, avx512.mask.psrl.di.128
    2284           6 :           IID = IsImmediate ? Intrinsic::x86_sse2_psrli_d
    2285             :                             : Intrinsic::x86_sse2_psrl_d;
    2286          12 :         else if (Size == 'q') // avx512.mask.psrl.q.128, avx512.mask.psrl.qi.128
    2287           6 :           IID = IsImmediate ? Intrinsic::x86_sse2_psrli_q
    2288             :                             : Intrinsic::x86_sse2_psrl_q;
    2289           6 :         else if (Size == 'w') // avx512.mask.psrl.w.128, avx512.mask.psrl.wi.128
    2290           6 :           IID = IsImmediate ? Intrinsic::x86_sse2_psrli_w
    2291             :                             : Intrinsic::x86_sse2_psrl_w;
    2292             :         else
    2293           0 :           llvm_unreachable("Unexpected size");
    2294             :       } else if (Name.endswith(".256")) {
    2295          18 :         if (Size == 'd') // avx512.mask.psrl.d.256, avx512.mask.psrl.di.256
    2296           6 :           IID = IsImmediate ? Intrinsic::x86_avx2_psrli_d
    2297             :                             : Intrinsic::x86_avx2_psrl_d;
    2298          12 :         else if (Size == 'q') // avx512.mask.psrl.q.256, avx512.mask.psrl.qi.256
    2299           6 :           IID = IsImmediate ? Intrinsic::x86_avx2_psrli_q
    2300             :                             : Intrinsic::x86_avx2_psrl_q;
    2301           6 :         else if (Size == 'w') // avx512.mask.psrl.w.256, avx512.mask.psrl.wi.256
    2302           6 :           IID = IsImmediate ? Intrinsic::x86_avx2_psrli_w
    2303             :                             : Intrinsic::x86_avx2_psrl_w;
    2304             :         else
    2305           0 :           llvm_unreachable("Unexpected size");
    2306             :       } else {
    2307          37 :         if (Size == 'd') // psrl.di.512, psrli.d, psrl.d, psrl.d.512
    2308          12 :           IID = IsImmediate ? Intrinsic::x86_avx512_psrli_d_512 :
    2309           6 :                 IsVariable  ? Intrinsic::x86_avx512_psrlv_d_512 :
    2310             :                               Intrinsic::x86_avx512_psrl_d_512;
    2311          25 :         else if (Size == 'q') // psrl.qi.512, psrli.q, psrl.q, psrl.q.512
    2312          13 :           IID = IsImmediate ? Intrinsic::x86_avx512_psrli_q_512 :
    2313           7 :                 IsVariable  ? Intrinsic::x86_avx512_psrlv_q_512 :
    2314             :                               Intrinsic::x86_avx512_psrl_q_512;
    2315          12 :         else if (Size == 'w') // psrl.wi.512, psrli.w, psrl.w)
    2316          12 :           IID = IsImmediate ? Intrinsic::x86_avx512_psrli_w_512
    2317             :                             : Intrinsic::x86_avx512_psrl_w_512;
    2318             :         else
    2319           0 :           llvm_unreachable("Unexpected size");
    2320             :       }
    2321             : 
    2322          97 :       Rep = UpgradeX86MaskedShift(Builder, *CI, IID);
    2323         848 :     } else if (IsX86 && Name.startswith("avx512.mask.psra")) {
    2324          94 :       bool IsImmediate = Name[16] == 'i' ||
    2325          82 :                          (Name.size() > 18 && Name[18] == 'i');
    2326             :       bool IsVariable = Name[16] == 'v';
    2327          94 :       char Size = Name[16] == '.' ? Name[17] :
    2328          40 :                   Name[17] == '.' ? Name[18] :
    2329          21 :                   Name[18] == '.' ? Name[19] :
    2330             :                                     Name[20];
    2331             : 
    2332             :       Intrinsic::ID IID;
    2333         128 :       if (IsVariable && Name[17] != '.') {
    2334          21 :         if (Size == 's' && Name[17] == '4') // avx512.mask.psrav4.si
    2335             :           IID = Intrinsic::x86_avx2_psrav_d;
    2336          18 :         else if (Size == 's' && Name[17] == '8') // avx512.mask.psrav8.si
    2337             :           IID = Intrinsic::x86_avx2_psrav_d_256;
    2338          14 :         else if (Size == 'h' && Name[17] == '8') // avx512.mask.psrav8.hi
    2339             :           IID = Intrinsic::x86_avx512_psrav_w_128;
    2340          11 :         else if (Size == 'h' && Name[17] == '1') // avx512.mask.psrav16.hi
    2341             :           IID = Intrinsic::x86_avx512_psrav_w_256;
    2342          16 :         else if (Name[17] == '3' && Name[18] == '2') // avx512.mask.psrav32hi
    2343             :           IID = Intrinsic::x86_avx512_psrav_w_512;
    2344             :         else
    2345           0 :           llvm_unreachable("Unexpected size");
    2346             :       } else if (Name.endswith(".128")) {
    2347          19 :         if (Size == 'd') // avx512.mask.psra.d.128, avx512.mask.psra.di.128
    2348           3 :           IID = IsImmediate ? Intrinsic::x86_sse2_psrai_d
    2349             :                             : Intrinsic::x86_sse2_psra_d;
    2350          16 :         else if (Size == 'q') // avx512.mask.psra.q.128, avx512.mask.psra.qi.128
    2351          10 :           IID = IsImmediate ? Intrinsic::x86_avx512_psrai_q_128 :
    2352           7 :                 IsVariable  ? Intrinsic::x86_avx512_psrav_q_128 :
    2353             :                               Intrinsic::x86_avx512_psra_q_128;
    2354           6 :         else if (Size == 'w') // avx512.mask.psra.w.128, avx512.mask.psra.wi.128
    2355           6 :           IID = IsImmediate ? Intrinsic::x86_sse2_psrai_w
    2356             :                             : Intrinsic::x86_sse2_psra_w;
    2357             :         else
    2358           0 :           llvm_unreachable("Unexpected size");
    2359             :       } else if (Name.endswith(".256")) {
    2360          18 :         if (Size == 'd') // avx512.mask.psra.d.256, avx512.mask.psra.di.256
    2361           3 :           IID = IsImmediate ? Intrinsic::x86_avx2_psrai_d
    2362             :                             : Intrinsic::x86_avx2_psra_d;
    2363          15 :         else if (Size == 'q') // avx512.mask.psra.q.256, avx512.mask.psra.qi.256
    2364           9 :           IID = IsImmediate ? Intrinsic::x86_avx512_psrai_q_256 :
    2365           6 :                 IsVariable  ? Intrinsic::x86_avx512_psrav_q_256 :
    2366             :                               Intrinsic::x86_avx512_psra_q_256;
    2367           6 :         else if (Size == 'w') // avx512.mask.psra.w.256, avx512.mask.psra.wi.256
    2368           6 :           IID = IsImmediate ? Intrinsic::x86_avx2_psrai_w
    2369             :                             : Intrinsic::x86_avx2_psra_w;
    2370             :         else
    2371           0 :           llvm_unreachable("Unexpected size");
    2372             :       } else {
    2373          36 :         if (Size == 'd') // psra.di.512, psrai.d, psra.d, psrav.d.512
    2374          12 :           IID = IsImmediate ? Intrinsic::x86_avx512_psrai_d_512 :
    2375           6 :                 IsVariable  ? Intrinsic::x86_avx512_psrav_d_512 :
    2376             :                               Intrinsic::x86_avx512_psra_d_512;
    2377          24 :         else if (Size == 'q') // psra.qi.512, psrai.q, psra.q
    2378          12 :           IID = IsImmediate ? Intrinsic::x86_avx512_psrai_q_512 :
    2379           6 :                 IsVariable  ? Intrinsic::x86_avx512_psrav_q_512 :
    2380             :                               Intrinsic::x86_avx512_psra_q_512;
    2381          12 :         else if (Size == 'w') // psra.wi.512, psrai.w, psra.w
    2382          12 :           IID = IsImmediate ? Intrinsic::x86_avx512_psrai_w_512
    2383             :                             : Intrinsic::x86_avx512_psra_w_512;
    2384             :         else
    2385           0 :           llvm_unreachable("Unexpected size");
    2386             :       }
    2387             : 
    2388          94 :       Rep = UpgradeX86MaskedShift(Builder, *CI, IID);
    2389         754 :     } else if (IsX86 && Name.startswith("avx512.mask.move.s")) {
    2390           4 :       Rep = upgradeMaskedMove(Builder, *CI);
    2391         750 :     } else if (IsX86 && Name.startswith("avx512.cvtmask2")) {
    2392          14 :       Rep = UpgradeMaskToInt(Builder, *CI);
    2393         736 :     } else if (IsX86 && Name.endswith(".movntdqa")) {
    2394          34 :       Module *M = F->getParent();
    2395             :       MDNode *Node = MDNode::get(
    2396         102 :           C, ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(C), 1)));
    2397             : 
    2398          34 :       Value *Ptr = CI->getArgOperand(0);
    2399          34 :       VectorType *VTy = cast<VectorType>(CI->getType());
    2400             : 
    2401             :       // Convert the type of the pointer to a pointer to the stored type.
    2402             :       Value *BC =
    2403          34 :           Builder.CreateBitCast(Ptr, PointerType::getUnqual(VTy), "cast");
    2404          68 :       LoadInst *LI = Builder.CreateAlignedLoad(BC, VTy->getBitWidth() / 8);
    2405          68 :       LI->setMetadata(M->getMDKindID("nontemporal"), Node);
    2406          34 :       Rep = LI;
    2407         702 :     } else if (IsX86 &&
    2408             :                (Name.startswith("sse2.pavg") || Name.startswith("avx2.pavg") ||
    2409             :                 Name.startswith("avx512.mask.pavg"))) {
    2410             :       // llvm.x86.sse2.pavg.b/w, llvm.x86.avx2.pavg.b/w,
    2411             :       // llvm.x86.avx512.mask.pavg.b/w
    2412          28 :       Value *A = CI->getArgOperand(0);
    2413             :       Value *B = CI->getArgOperand(1);
    2414          28 :       VectorType *ZextType = VectorType::getExtendedElementVectorType(
    2415          28 :           cast<VectorType>(A->getType()));
    2416          28 :       Value *ExtendedA = Builder.CreateZExt(A, ZextType);
    2417          28 :       Value *ExtendedB = Builder.CreateZExt(B, ZextType);
    2418          28 :       Value *Sum = Builder.CreateAdd(ExtendedA, ExtendedB);
    2419          28 :       Value *AddOne = Builder.CreateAdd(Sum, ConstantInt::get(ZextType, 1));
    2420          28 :       Value *ShiftR = Builder.CreateLShr(AddOne, ConstantInt::get(ZextType, 1));
    2421          56 :       Rep = Builder.CreateTrunc(ShiftR, A->getType());
    2422          28 :       if (CI->getNumArgOperands() > 2) {
    2423          32 :         Rep = EmitX86Select(Builder, CI->getArgOperand(3), Rep,
    2424             :                             CI->getArgOperand(2));
    2425             :       }
    2426         674 :     } else if (IsX86 && Name.startswith("fma.vfmsub")) {
    2427             :       // Handle FMSUB and FSUBADD.
    2428         142 :       unsigned VecWidth = CI->getType()->getPrimitiveSizeInBits();
    2429         142 :       unsigned EltWidth = CI->getType()->getScalarSizeInBits();
    2430             :       Intrinsic::ID IID;
    2431         260 :       if (Name[10] == '.' && Name[11] == 'p') {
    2432             :         // Packed FMSUB
    2433          80 :         if (VecWidth == 128 && EltWidth == 32)
    2434             :           IID = Intrinsic::x86_fma_vfmadd_ps;
    2435          62 :         else if (VecWidth == 128 && EltWidth == 64)
    2436             :           IID = Intrinsic::x86_fma_vfmadd_pd;
    2437          44 :         else if (VecWidth == 256 && EltWidth == 32)
    2438             :           IID = Intrinsic::x86_fma_vfmadd_ps_256;
    2439          18 :         else if (VecWidth == 256 && EltWidth == 64)
    2440             :           IID = Intrinsic::x86_fma_vfmadd_pd_256;
    2441             :         else
    2442           0 :           llvm_unreachable("Unexpected intrinsic");
    2443         100 :       } else if (Name[10] == '.' && Name[11] == 's') {
    2444             :         // Scalar FMSUB
    2445          38 :         if (EltWidth == 32)
    2446             :           IID = Intrinsic::x86_fma_vfmadd_ss;
    2447          19 :         else if (EltWidth == 64)
    2448             :           IID = Intrinsic::x86_fma_vfmadd_sd;
    2449             :         else
    2450           0 :           llvm_unreachable("Unexpected intrinsic");
    2451             :       } else {
    2452             :         // FMSUBADD
    2453          24 :         if (VecWidth == 128 && EltWidth == 32)
    2454             :           IID = Intrinsic::x86_fma_vfmaddsub_ps;
    2455          18 :         else if (VecWidth == 128 && EltWidth == 64)
    2456             :           IID = Intrinsic::x86_fma_vfmaddsub_pd;
    2457          12 :         else if (VecWidth == 256 && EltWidth == 32)
    2458             :           IID = Intrinsic::x86_fma_vfmaddsub_ps_256;
    2459           6 :         else if (VecWidth == 256 && EltWidth == 64)
    2460             :           IID = Intrinsic::x86_fma_vfmaddsub_pd_256;
    2461             :         else
    2462           0 :           llvm_unreachable("Unexpected intrinsic");
    2463             :       }
    2464         284 :       Value *Arg2 = Builder.CreateFNeg(CI->getArgOperand(2));
    2465         284 :       Value *Ops[] = { CI->getArgOperand(0), CI->getArgOperand(1), Arg2 };
    2466         426 :       Rep = Builder.CreateCall(Intrinsic::getDeclaration(CI->getModule(), IID),
    2467             :                                                          Ops);
    2468         532 :     } else if (IsX86 && (Name.startswith("fma.vfnmadd.") ||
    2469             :                          Name.startswith("fma.vfnmsub."))) {
    2470         222 :       Value *Arg0 = CI->getArgOperand(0);
    2471             :       Value *Arg1 = CI->getArgOperand(1);
    2472             :       Value *Arg2 = CI->getArgOperand(2);
    2473         222 :       unsigned VecWidth = CI->getType()->getPrimitiveSizeInBits();
    2474         222 :       unsigned EltWidth = CI->getType()->getScalarSizeInBits();
    2475             :       Intrinsic::ID IID;
    2476         444 :       if (Name[12] == 'p') {
    2477             :         // Packed FNMADD/FNSUB
    2478         144 :         Arg0 = Builder.CreateFNeg(Arg0);
    2479         144 :         if (VecWidth == 128 && EltWidth == 32)
    2480             :           IID = Intrinsic::x86_fma_vfmadd_ps;
    2481         108 :         else if (VecWidth == 128 && EltWidth == 64)
    2482             :           IID = Intrinsic::x86_fma_vfmadd_pd;
    2483          72 :         else if (VecWidth == 256 && EltWidth == 32)
    2484             :           IID = Intrinsic::x86_fma_vfmadd_ps_256;
    2485          36 :         else if (VecWidth == 256 && EltWidth == 64)
    2486             :           IID = Intrinsic::x86_fma_vfmadd_pd_256;
    2487             :         else
    2488           0 :           llvm_unreachable("Unexpected intrinsic");
    2489             :       } else {
    2490             :         // Scalar FNMADD/FNMSUB
    2491          78 :         Arg1 = Builder.CreateFNeg(Arg1); // Arg0 is passthru so invert Arg1.
    2492          78 :         if (EltWidth == 32)
    2493             :           IID = Intrinsic::x86_fma_vfmadd_ss;
    2494          38 :         else if (EltWidth == 64)
    2495             :           IID = Intrinsic::x86_fma_vfmadd_sd;
    2496             :         else
    2497           0 :           llvm_unreachable("Unexpected intrinsic");
    2498             :       }
    2499             :       // Invert for FNMSUB.
    2500         444 :       if (Name[8] == 's')
    2501         110 :         Arg2 = Builder.CreateFNeg(Arg2);
    2502         222 :       Value *Ops[] = { Arg0, Arg1, Arg2 };
    2503         666 :       Rep = Builder.CreateCall(Intrinsic::getDeclaration(CI->getModule(), IID),
    2504             :                                                          Ops);
    2505         594 :     } else if (IsX86 && Name.startswith("avx512.mask.") &&
    2506         284 :                upgradeAVX512MaskToSelect(Name, Builder, *CI, Rep)) {
    2507             :       // Rep will be updated by the call in the condition.
    2508          26 :     } else if (IsNVVM && (Name == "abs.i" || Name == "abs.ll")) {
    2509           4 :       Value *Arg = CI->getArgOperand(0);
    2510           4 :       Value *Neg = Builder.CreateNeg(Arg, "neg");
    2511           4 :       Value *Cmp = Builder.CreateICmpSGE(
    2512           4 :           Arg, llvm::Constant::getNullValue(Arg->getType()), "abs.cond");
    2513           4 :       Rep = Builder.CreateSelect(Cmp, Arg, Neg, "abs");
    2514          22 :     } else if (IsNVVM && (Name == "max.i" || Name == "max.ll" ||
    2515             :                           Name == "max.ui" || Name == "max.ull")) {
    2516           8 :       Value *Arg0 = CI->getArgOperand(0);
    2517             :       Value *Arg1 = CI->getArgOperand(1);
    2518             :       Value *Cmp = Name.endswith(".ui") || Name.endswith(".ull")
    2519          12 :                        ? Builder.CreateICmpUGE(Arg0, Arg1, "max.cond")
    2520           8 :                        : Builder.CreateICmpSGE(Arg0, Arg1, "max.cond");
    2521           8 :       Rep = Builder.CreateSelect(Cmp, Arg0, Arg1, "max");
    2522          14 :     } else if (IsNVVM && (Name == "min.i" || Name == "min.ll" ||
    2523             :                           Name == "min.ui" || Name == "min.ull")) {
    2524           8 :       Value *Arg0 = CI->getArgOperand(0);
    2525             :       Value *Arg1 = CI->getArgOperand(1);
    2526             :       Value *Cmp = Name.endswith(".ui") || Name.endswith(".ull")
    2527          12 :                        ? Builder.CreateICmpULE(Arg0, Arg1, "min.cond")
    2528           8 :                        : Builder.CreateICmpSLE(Arg0, Arg1, "min.cond");
    2529           8 :       Rep = Builder.CreateSelect(Cmp, Arg0, Arg1, "min");
    2530           6 :     } else if (IsNVVM && Name == "clz.ll") {
    2531             :       // llvm.nvvm.clz.ll returns an i32, but llvm.ctlz.i64 and returns an i64.
    2532           2 :       Value *Arg = CI->getArgOperand(0);
    2533          10 :       Value *Ctlz = Builder.CreateCall(
    2534             :           Intrinsic::getDeclaration(F->getParent(), Intrinsic::ctlz,
    2535           2 :                                     {Arg->getType()}),
    2536           2 :           {Arg, Builder.getFalse()}, "ctlz");
    2537           4 :       Rep = Builder.CreateTrunc(Ctlz, Builder.getInt32Ty(), "ctlz.trunc");
    2538           4 :     } else if (IsNVVM && Name == "popc.ll") {
    2539             :       // llvm.nvvm.popc.ll returns an i32, but llvm.ctpop.i64 and returns an
    2540             :       // i64.
    2541           4 :       Value *Arg = CI->getArgOperand(0);
    2542           6 :       Value *Popc = Builder.CreateCall(
    2543             :           Intrinsic::getDeclaration(F->getParent(), Intrinsic::ctpop,
    2544           2 :                                     {Arg->getType()}),
    2545             :           Arg, "ctpop");
    2546           4 :       Rep = Builder.CreateTrunc(Popc, Builder.getInt32Ty(), "ctpop.trunc");
    2547           2 :     } else if (IsNVVM && Name == "h2f") {
    2548           8 :       Rep = Builder.CreateCall(Intrinsic::getDeclaration(
    2549             :                                    F->getParent(), Intrinsic::convert_from_fp16,
    2550           2 :                                    {Builder.getFloatTy()}),
    2551           6 :                                CI->getArgOperand(0), "h2f");
    2552             :     } else {
    2553           0 :       llvm_unreachable("Unknown function for CallInst upgrade.");
    2554             :     }
    2555             : 
    2556        5007 :     if (Rep)
    2557        5001 :       CI->replaceAllUsesWith(Rep);
    2558        5007 :     CI->eraseFromParent();
    2559        5007 :     return;
    2560             :   }
    2561             : 
    2562             :   const auto &DefaultCase = [&NewFn, &CI]() -> void {
    2563             :     // Handle generic mangling change, but nothing else
    2564             :     assert(
    2565             :         (CI->getCalledFunction()->getName() != NewFn->getName()) &&
    2566             :         "Unknown function for CallInst upgrade and isn't just a name change");
    2567             :     CI->setCalledFunction(NewFn);
    2568             :   };
    2569             :   CallInst *NewCall = nullptr;
    2570        1200 :   switch (NewFn->getIntrinsicID()) {
    2571             :   default: {
    2572             :     DefaultCase();
    2573             :     return;
    2574             :   }
    2575             : 
    2576          14 :   case Intrinsic::arm_neon_vld1:
    2577             :   case Intrinsic::arm_neon_vld2:
    2578             :   case Intrinsic::arm_neon_vld3:
    2579             :   case Intrinsic::arm_neon_vld4:
    2580             :   case Intrinsic::arm_neon_vld2lane:
    2581             :   case Intrinsic::arm_neon_vld3lane:
    2582             :   case Intrinsic::arm_neon_vld4lane:
    2583             :   case Intrinsic::arm_neon_vst1:
    2584             :   case Intrinsic::arm_neon_vst2:
    2585             :   case Intrinsic::arm_neon_vst3:
    2586             :   case Intrinsic::arm_neon_vst4:
    2587             :   case Intrinsic::arm_neon_vst2lane:
    2588             :   case Intrinsic::arm_neon_vst3lane:
    2589             :   case Intrinsic::arm_neon_vst4lane: {
    2590          28 :     SmallVector<Value *, 4> Args(CI->arg_operands().begin(),
    2591          42 :                                  CI->arg_operands().end());
    2592          14 :     NewCall = Builder.CreateCall(NewFn, Args);
    2593             :     break;
    2594             :   }
    2595             : 
    2596             :   case Intrinsic::bitreverse:
    2597          24 :     NewCall = Builder.CreateCall(NewFn, {CI->getArgOperand(0)});
    2598           8 :     break;
    2599             : 
    2600             :   case Intrinsic::ctlz:
    2601             :   case Intrinsic::cttz:
    2602             :     assert(CI->getNumArgOperands() == 1 &&
    2603             :            "Mismatch between function args and call args");
    2604             :     NewCall =
    2605         216 :         Builder.CreateCall(NewFn, {CI->getArgOperand(0), Builder.getFalse()});
    2606          54 :     break;
    2607             : 
    2608          74 :   case Intrinsic::objectsize: {
    2609          74 :     Value *NullIsUnknownSize = CI->getNumArgOperands() == 2
    2610          74 :                                    ? Builder.getFalse()
    2611             :                                    : CI->getArgOperand(2);
    2612         222 :     NewCall = Builder.CreateCall(
    2613             :         NewFn, {CI->getArgOperand(0), CI->getArgOperand(1), NullIsUnknownSize});
    2614          74 :     break;
    2615             :   }
    2616             : 
    2617             :   case Intrinsic::ctpop:
    2618          12 :     NewCall = Builder.CreateCall(NewFn, {CI->getArgOperand(0)});
    2619           4 :     break;
    2620             : 
    2621             :   case Intrinsic::convert_from_fp16:
    2622           0 :     NewCall = Builder.CreateCall(NewFn, {CI->getArgOperand(0)});
    2623           0 :     break;
    2624             : 
    2625         450 :   case Intrinsic::dbg_value:
    2626             :     // Upgrade from the old version that had an extra offset argument.
    2627             :     assert(CI->getNumArgOperands() == 4);
    2628             :     // Drop nonzero offsets instead of attempting to upgrade them.
    2629         450 :     if (auto *Offset = dyn_cast_or_null<Constant>(CI->getArgOperand(1)))
    2630         450 :       if (Offset->isZeroValue()) {
    2631        1792 :         NewCall = Builder.CreateCall(
    2632             :             NewFn,
    2633             :             {CI->getArgOperand(0), CI->getArgOperand(2), CI->getArgOperand(3)});
    2634         448 :         break;
    2635             :       }
    2636           2 :     CI->eraseFromParent();
    2637           2 :     return;
    2638             : 
    2639             :   case Intrinsic::x86_xop_vfrcz_ss:
    2640             :   case Intrinsic::x86_xop_vfrcz_sd:
    2641           0 :     NewCall = Builder.CreateCall(NewFn, {CI->getArgOperand(1)});
    2642           0 :     break;
    2643             : 
    2644           8 :   case Intrinsic::x86_xop_vpermil2pd:
    2645             :   case Intrinsic::x86_xop_vpermil2ps:
    2646             :   case Intrinsic::x86_xop_vpermil2pd_256:
    2647             :   case Intrinsic::x86_xop_vpermil2ps_256: {
    2648          16 :     SmallVector<Value *, 4> Args(CI->arg_operands().begin(),
    2649          24 :                                  CI->arg_operands().end());
    2650           8 :     VectorType *FltIdxTy = cast<VectorType>(Args[2]->getType());
    2651           8 :     VectorType *IntIdxTy = VectorType::getInteger(FltIdxTy);
    2652          16 :     Args[2] = Builder.CreateBitCast(Args[2], IntIdxTy);
    2653           8 :     NewCall = Builder.CreateCall(NewFn, Args);
    2654             :     break;
    2655             :   }
    2656             : 
    2657          12 :   case Intrinsic::x86_sse41_ptestc:
    2658             :   case Intrinsic::x86_sse41_ptestz:
    2659             :   case Intrinsic::x86_sse41_ptestnzc: {
    2660             :     // The arguments for these intrinsics used to be v4f32, and changed
    2661             :     // to v2i64. This is purely a nop, since those are bitwise intrinsics.
    2662             :     // So, the only thing required is a bitcast for both arguments.
    2663             :     // First, check the arguments have the old type.
    2664          12 :     Value *Arg0 = CI->getArgOperand(0);
    2665          12 :     if (Arg0->getType() != VectorType::get(Type::getFloatTy(C), 4))
    2666             :       return;
    2667             : 
    2668             :     // Old intrinsic, add bitcasts
    2669             :     Value *Arg1 = CI->getArgOperand(1);
    2670             : 
    2671          12 :     Type *NewVecTy = VectorType::get(Type::getInt64Ty(C), 2);
    2672             : 
    2673          12 :     Value *BC0 = Builder.CreateBitCast(Arg0, NewVecTy, "cast");
    2674          12 :     Value *BC1 = Builder.CreateBitCast(Arg1, NewVecTy, "cast");
    2675             : 
    2676          24 :     NewCall = Builder.CreateCall(NewFn, {BC0, BC1});
    2677          12 :     break;
    2678             :   }
    2679             : 
    2680          49 :   case Intrinsic::x86_sse41_insertps:
    2681             :   case Intrinsic::x86_sse41_dppd:
    2682             :   case Intrinsic::x86_sse41_dpps:
    2683             :   case Intrinsic::x86_sse41_mpsadbw:
    2684             :   case Intrinsic::x86_avx_dp_ps_256:
    2685             :   case Intrinsic::x86_avx2_mpsadbw: {
    2686             :     // Need to truncate the last argument from i32 to i8 -- this argument models
    2687             :     // an inherently 8-bit immediate operand to these x86 instructions.
    2688          98 :     SmallVector<Value *, 4> Args(CI->arg_operands().begin(),
    2689         147 :                                  CI->arg_operands().end());
    2690             : 
    2691             :     // Replace the last argument with a trunc.
    2692          98 :     Args.back() = Builder.CreateTrunc(Args.back(), Type::getInt8Ty(C), "trunc");
    2693          49 :     NewCall = Builder.CreateCall(NewFn, Args);
    2694             :     break;
    2695             :   }
    2696             : 
    2697             :   case Intrinsic::x86_avx512_mask_cmp_pd_128:
    2698             :   case Intrinsic::x86_avx512_mask_cmp_pd_256:
    2699             :   case Intrinsic::x86_avx512_mask_cmp_pd_512:
    2700             :   case Intrinsic::x86_avx512_mask_cmp_ps_128:
    2701             :   case Intrinsic::x86_avx512_mask_cmp_ps_256:
    2702             :   case Intrinsic::x86_avx512_mask_cmp_ps_512: {
    2703             :     SmallVector<Value *, 4> Args;
    2704          12 :     Args.push_back(CI->getArgOperand(0));
    2705           6 :     Args.push_back(CI->getArgOperand(1));
    2706           6 :     Args.push_back(CI->getArgOperand(2));
    2707           6 :     if (CI->getNumArgOperands() == 5)
    2708           2 :       Args.push_back(CI->getArgOperand(4));
    2709             : 
    2710           6 :     NewCall = Builder.CreateCall(NewFn, Args);
    2711           6 :     unsigned NumElts = Args[0]->getType()->getVectorNumElements();
    2712             :     Value *Res = ApplyX86MaskOn1BitsVec(Builder, NewCall, CI->getArgOperand(3),
    2713           6 :                                         NumElts);
    2714             : 
    2715          12 :     std::string Name = CI->getName();
    2716           6 :     if (!Name.empty()) {
    2717          18 :       CI->setName(Name + ".old");
    2718          12 :       NewCall->setName(Name);
    2719             :     }
    2720           6 :     CI->replaceAllUsesWith(Res);
    2721           6 :     CI->eraseFromParent();
    2722             :     return;
    2723             :   }
    2724             : 
    2725             :   case Intrinsic::thread_pointer: {
    2726           2 :     NewCall = Builder.CreateCall(NewFn, {});
    2727           2 :     break;
    2728             :   }
    2729             : 
    2730          85 :   case Intrinsic::invariant_start:
    2731             :   case Intrinsic::invariant_end:
    2732             :   case Intrinsic::masked_load:
    2733             :   case Intrinsic::masked_store:
    2734             :   case Intrinsic::masked_gather:
    2735             :   case Intrinsic::masked_scatter: {
    2736         170 :     SmallVector<Value *, 4> Args(CI->arg_operands().begin(),
    2737         255 :                                  CI->arg_operands().end());
    2738          85 :     NewCall = Builder.CreateCall(NewFn, Args);
    2739             :     break;
    2740             :   }
    2741             : 
    2742         211 :   case Intrinsic::memcpy:
    2743             :   case Intrinsic::memmove:
    2744             :   case Intrinsic::memset: {
    2745             :     // We have to make sure that the call signature is what we're expecting.
    2746             :     // We only want to change the old signatures by removing the alignment arg:
    2747             :     //  @llvm.mem[cpy|move]...(i8*, i8*, i[32|i64], i32, i1)
    2748             :     //    -> @llvm.mem[cpy|move]...(i8*, i8*, i[32|i64], i1)
    2749             :     //  @llvm.memset...(i8*, i8, i[32|64], i32, i1)
    2750             :     //    -> @llvm.memset...(i8*, i8, i[32|64], i1)
    2751             :     // Note: i8*'s in the above can be any pointer type
    2752         422 :     if (CI->getNumArgOperands() != 5) {
    2753             :       DefaultCase();
    2754          17 :       return;
    2755             :     }
    2756             :     // Remove alignment argument (3), and add alignment attributes to the
    2757             :     // dest/src pointers.
    2758             :     Value *Args[4] = {CI->getArgOperand(0), CI->getArgOperand(1),
    2759         776 :                       CI->getArgOperand(2), CI->getArgOperand(4)};
    2760         194 :     NewCall = Builder.CreateCall(NewFn, Args);
    2761             :     auto *MemCI = cast<MemIntrinsic>(NewCall);
    2762             :     // All mem intrinsics support dest alignment.
    2763             :     const ConstantInt *Align = cast<ConstantInt>(CI->getArgOperand(3));
    2764         388 :     MemCI->setDestAlignment(Align->getZExtValue());
    2765             :     // Memcpy/Memmove also support source alignment.
    2766             :     if (auto *MTI = dyn_cast<MemTransferInst>(MemCI))
    2767         258 :       MTI->setSourceAlignment(Align->getZExtValue());
    2768         194 :     break;
    2769             :   }
    2770             :   }
    2771             :   assert(NewCall && "Should have either set this variable or returned through "
    2772             :                     "the default case");
    2773        1904 :   std::string Name = CI->getName();
    2774         952 :   if (!Name.empty()) {
    2775         495 :     CI->setName(Name + ".old");
    2776         330 :     NewCall->setName(Name);
    2777             :   }
    2778         952 :   CI->replaceAllUsesWith(NewCall);
    2779         952 :   CI->eraseFromParent();
    2780             : }
    2781             : 
    2782      285645 : void llvm::UpgradeCallsToIntrinsic(Function *F) {
    2783             :   assert(F && "Illegal attempt to upgrade a non-existent intrinsic.");
    2784             : 
    2785             :   // Check if this function should be upgraded and get the replacement function
    2786             :   // if there is one.
    2787             :   Function *NewFn;
    2788      285645 :   if (UpgradeIntrinsicFunction(F, NewFn)) {
    2789             :     // Replace all users of the old function with the new function or new
    2790             :     // instructions. This is not a range loop because the call is deleted.
    2791        9243 :     for (auto UI = F->user_begin(), UE = F->user_end(); UI != UE; )
    2792             :       if (CallInst *CI = dyn_cast<CallInst>(*UI++))
    2793        6203 :         UpgradeIntrinsicCall(CI, NewFn);
    2794             : 
    2795             :     // Remove old function, no longer used, from the module.
    2796        3040 :     F->eraseFromParent();
    2797             :   }
    2798      285645 : }
    2799             : 
    2800        4825 : MDNode *llvm::UpgradeTBAANode(MDNode &MD) {
    2801             :   // Check if the tag uses struct-path aware TBAA format.
    2802        4626 :   if (isa<MDNode>(MD.getOperand(0)) && MD.getNumOperands() >= 3)
    2803             :     return &MD;
    2804             : 
    2805             :   auto &Context = MD.getContext();
    2806         199 :   if (MD.getNumOperands() == 3) {
    2807          64 :     Metadata *Elts[] = {MD.getOperand(0), MD.getOperand(1)};
    2808             :     MDNode *ScalarType = MDNode::get(Context, Elts);
    2809             :     // Create a MDNode <ScalarType, ScalarType, offset 0, const>
    2810             :     Metadata *Elts2[] = {ScalarType, ScalarType,
    2811          32 :                          ConstantAsMetadata::get(
    2812          32 :                              Constant::getNullValue(Type::getInt64Ty(Context))),
    2813          96 :                          MD.getOperand(2)};
    2814             :     return MDNode::get(Context, Elts2);
    2815             :   }
    2816             :   // Create a MDNode <MD, MD, offset 0>
    2817         167 :   Metadata *Elts[] = {&MD, &MD, ConstantAsMetadata::get(Constant::getNullValue(
    2818         334 :                                     Type::getInt64Ty(Context)))};
    2819             :   return MDNode::get(Context, Elts);
    2820             : }
    2821             : 
    2822       14634 : Instruction *llvm::UpgradeBitCastInst(unsigned Opc, Value *V, Type *DestTy,
    2823             :                                       Instruction *&Temp) {
    2824       14634 :   if (Opc != Instruction::BitCast)
    2825             :     return nullptr;
    2826             : 
    2827       11667 :   Temp = nullptr;
    2828       11667 :   Type *SrcTy = V->getType();
    2829       33035 :   if (SrcTy->isPtrOrPtrVectorTy() && DestTy->isPtrOrPtrVectorTy() &&
    2830             :       SrcTy->getPointerAddressSpace() != DestTy->getPointerAddressSpace()) {
    2831           0 :     LLVMContext &Context = V->getContext();
    2832             : 
    2833             :     // We have no information about target data layout, so we assume that
    2834             :     // the maximum pointer size is 64bit.
    2835           0 :     Type *MidTy = Type::getInt64Ty(Context);
    2836           0 :     Temp = CastInst::Create(Instruction::PtrToInt, V, MidTy);
    2837             : 
    2838           0 :     return CastInst::Create(Instruction::IntToPtr, Temp, DestTy);
    2839             :   }
    2840             : 
    2841             :   return nullptr;
    2842             : }
    2843             : 
    2844        3609 : Value *llvm::UpgradeBitCastExpr(unsigned Opc, Constant *C, Type *DestTy) {
    2845        3609 :   if (Opc != Instruction::BitCast)
    2846             :     return nullptr;
    2847             : 
    2848        3307 :   Type *SrcTy = C->getType();
    2849        9839 :   if (SrcTy->isPtrOrPtrVectorTy() && DestTy->isPtrOrPtrVectorTy() &&
    2850             :       SrcTy->getPointerAddressSpace() != DestTy->getPointerAddressSpace()) {
    2851           0 :     LLVMContext &Context = C->getContext();
    2852             : 
    2853             :     // We have no information about target data layout, so we assume that
    2854             :     // the maximum pointer size is 64bit.
    2855           0 :     Type *MidTy = Type::getInt64Ty(Context);
    2856             : 
    2857           0 :     return ConstantExpr::getIntToPtr(ConstantExpr::getPtrToInt(C, MidTy),
    2858           0 :                                      DestTy);
    2859             :   }
    2860             : 
    2861             :   return nullptr;
    2862             : }
    2863             : 
    2864             : /// Check the debug info version number, if it is out-dated, drop the debug
    2865             : /// info. Return true if module is modified.
    2866       35615 : bool llvm::UpgradeDebugInfo(Module &M) {
    2867       35615 :   unsigned Version = getDebugMetadataVersionFromModule(M);
    2868       35615 :   if (Version == DEBUG_METADATA_VERSION) {
    2869        1922 :     bool BrokenDebugInfo = false;
    2870        1922 :     if (verifyModule(M, &llvm::errs(), &BrokenDebugInfo))
    2871           0 :       report_fatal_error("Broken module found, compilation aborted!");
    2872        1922 :     if (!BrokenDebugInfo)
    2873             :       // Everything is ok.
    2874        1876 :       return false;
    2875             :     else {
    2876             :       // Diagnose malformed debug info.
    2877             :       DiagnosticInfoIgnoringInvalidDebugMetadata Diag(M);
    2878          46 :       M.getContext().diagnose(Diag);
    2879             :     }
    2880             :   }
    2881       33739 :   bool Modified = StripDebugInfo(M);
    2882       33739 :   if (Modified && Version != DEBUG_METADATA_VERSION) {
    2883             :     // Diagnose a version mismatch.
    2884             :     DiagnosticInfoDebugMetadataVersion DiagVersion(M, Version);
    2885          55 :     M.getContext().diagnose(DiagVersion);
    2886             :   }
    2887             :   return Modified;
    2888             : }
    2889             : 
    2890        2488 : bool llvm::UpgradeRetainReleaseMarker(Module &M) {
    2891             :   bool Changed = false;
    2892             :   NamedMDNode *ModRetainReleaseMarker =
    2893        2488 :       M.getNamedMetadata("clang.arc.retainAutoreleasedReturnValueMarker");
    2894        2489 :   if (ModRetainReleaseMarker) {
    2895           1 :     MDNode *Op = ModRetainReleaseMarker->getOperand(0);
    2896           1 :     if (Op) {
    2897             :       MDString *ID = dyn_cast_or_null<MDString>(Op->getOperand(0));
    2898             :       if (ID) {
    2899             :         SmallVector<StringRef, 4> ValueComp;
    2900           2 :         ID->getString().split(ValueComp, "#");
    2901           1 :         if (ValueComp.size() == 2) {
    2902           4 :           std::string NewValue = ValueComp[0].str() + ";" + ValueComp[1].str();
    2903           1 :           Metadata *Ops[1] = {MDString::get(M.getContext(), NewValue)};
    2904           1 :           ModRetainReleaseMarker->setOperand(0,
    2905           1 :                                              MDNode::get(M.getContext(), Ops));
    2906             :           Changed = true;
    2907             :         }
    2908             :       }
    2909             :     }
    2910             :   }
    2911        2489 :   return Changed;
    2912             : }
    2913             : 
    2914       34476 : bool llvm::UpgradeModuleFlags(Module &M) {
    2915       34476 :   NamedMDNode *ModFlags = M.getModuleFlagsMetadata();
    2916       34477 :   if (!ModFlags)
    2917             :     return false;
    2918             : 
    2919             :   bool HasObjCFlag = false, HasClassProperties = false, Changed = false;
    2920        7429 :   for (unsigned I = 0, E = ModFlags->getNumOperands(); I != E; ++I) {
    2921        4845 :     MDNode *Op = ModFlags->getOperand(I);
    2922        4845 :     if (Op->getNumOperands() != 3)
    2923           1 :       continue;
    2924             :     MDString *ID = dyn_cast_or_null<MDString>(Op->getOperand(1));
    2925           3 :     if (!ID)
    2926           3 :       continue;
    2927        4841 :     if (ID->getString() == "Objective-C Image Info Version")
    2928             :       HasObjCFlag = true;
    2929        4841 :     if (ID->getString() == "Objective-C Class Properties")
    2930             :       HasClassProperties = true;
    2931             :     // Upgrade PIC/PIE Module Flags. The module flag behavior for these two
    2932             :     // field was Error and now they are Max.
    2933        9326 :     if (ID->getString() == "PIC Level" || ID->getString() == "PIE Level") {
    2934             :       if (auto *Behavior =
    2935             :               mdconst::dyn_extract_or_null<ConstantInt>(Op->getOperand(0))) {
    2936         403 :         if (Behavior->getLimitedValue() == Module::Error) {
    2937         277 :           Type *Int32Ty = Type::getInt32Ty(M.getContext());
    2938             :           Metadata *Ops[3] = {
    2939         277 :               ConstantAsMetadata::get(ConstantInt::get(Int32Ty, Module::Max)),
    2940         277 :               MDString::get(M.getContext(), ID->getString()),
    2941         831 :               Op->getOperand(2)};
    2942         554 :           ModFlags->setOperand(I, MDNode::get(M.getContext(), Ops));
    2943             :           Changed = true;
    2944             :         }
    2945             :       }
    2946             :     }
    2947             :     // Upgrade Objective-C Image Info Section. Removed the whitespce in the
    2948             :     // section name so that llvm-lto will not complain about mismatching
    2949             :     // module flags that is functionally the same.
    2950        4841 :     if (ID->getString() == "Objective-C Image Info Section") {
    2951             :       if (auto *Value = dyn_cast_or_null<MDString>(Op->getOperand(2))) {
    2952             :         SmallVector<StringRef, 4> ValueComp;
    2953          64 :         Value->getString().split(ValueComp, " ");
    2954          32 :         if (ValueComp.size() != 1) {
    2955             :           std::string NewValue;
    2956         167 :           for (auto &S : ValueComp)
    2957          74 :             NewValue += S.str();
    2958             :           Metadata *Ops[3] = {Op->getOperand(0), Op->getOperand(1),
    2959          57 :                               MDString::get(M.getContext(), NewValue)};
    2960          38 :           ModFlags->setOperand(I, MDNode::get(M.getContext(), Ops));
    2961             :           Changed = true;
    2962             :         }
    2963             :       }
    2964             :     }
    2965             :   }
    2966             : 
    2967             :   // "Objective-C Class Properties" is recently added for Objective-C. We
    2968             :   // upgrade ObjC bitcodes to contain a "Objective-C Class Properties" module
    2969             :   // flag of value 0, so we can correclty downgrade this flag when trying to
    2970             :   // link an ObjC bitcode without this module flag with an ObjC bitcode with
    2971             :   // this module flag.
    2972        2584 :   if (HasObjCFlag && !HasClassProperties) {
    2973          18 :     M.addModuleFlag(llvm::Module::Override, "Objective-C Class Properties",
    2974             :                     (uint32_t)0);
    2975             :     Changed = true;
    2976             :   }
    2977             : 
    2978             :   return Changed;
    2979             : }
    2980             : 
    2981       31988 : void llvm::UpgradeSectionAttributes(Module &M) {
    2982           2 :   auto TrimSpaces = [](StringRef Section) -> std::string {
    2983             :     SmallVector<StringRef, 5> Components;
    2984           2 :     Section.split(Components, ',');
    2985             : 
    2986             :     SmallString<32> Buffer;
    2987             :     raw_svector_ostream OS(Buffer);
    2988             : 
    2989          18 :     for (auto Component : Components)
    2990           8 :       OS << ',' << Component.trim();
    2991             : 
    2992           8 :     return OS.str().substr(1);
    2993             :   };
    2994             : 
    2995       58774 :   for (auto &GV : M.globals()) {
    2996       26786 :     if (!GV.hasSection())
    2997       26240 :       continue;
    2998             : 
    2999         546 :     StringRef Section = GV.getSection();
    3000             : 
    3001         544 :     if (!Section.startswith("__DATA, __objc_catlist"))
    3002         544 :       continue;
    3003             : 
    3004             :     // __DATA, __objc_catlist, regular, no_dead_strip
    3005             :     // __DATA,__objc_catlist,regular,no_dead_strip
    3006           6 :     GV.setSection(TrimSpaces(Section));
    3007             :   }
    3008       31988 : }
    3009             : 
    3010           4 : static bool isOldLoopArgument(Metadata *MD) {
    3011             :   auto *T = dyn_cast_or_null<MDTuple>(MD);
    3012             :   if (!T)
    3013             :     return false;
    3014           4 :   if (T->getNumOperands() < 1)
    3015             :     return false;
    3016           4 :   auto *S = dyn_cast_or_null<MDString>(T->getOperand(0));
    3017             :   if (!S)
    3018             :     return false;
    3019           4 :   return S->getString().startswith("llvm.vectorizer.");
    3020             : }
    3021             : 
    3022           8 : static MDString *upgradeLoopTag(LLVMContext &C, StringRef OldTag) {
    3023             :   StringRef OldPrefix = "llvm.vectorizer.";
    3024             :   assert(OldTag.startswith(OldPrefix) && "Expected old prefix");
    3025             : 
    3026             :   if (OldTag == "llvm.vectorizer.unroll")
    3027           2 :     return MDString::get(C, "llvm.loop.interleave.count");
    3028             : 
    3029           6 :   return MDString::get(
    3030          12 :       C, (Twine("llvm.loop.vectorize.") + OldTag.drop_front(OldPrefix.size()))
    3031          18 :              .str());
    3032             : }
    3033             : 
    3034          10 : static Metadata *upgradeLoopArgument(Metadata *MD) {
    3035             :   auto *T = dyn_cast_or_null<MDTuple>(MD);
    3036             :   if (!T)
    3037             :     return MD;
    3038          10 :   if (T->getNumOperands() < 1)
    3039             :     return MD;
    3040          10 :   auto *OldTag = dyn_cast_or_null<MDString>(T->getOperand(0));
    3041             :   if (!OldTag)
    3042             :     return MD;
    3043           8 :   if (!OldTag->getString().startswith("llvm.vectorizer."))
    3044             :     return MD;
    3045             : 
    3046             :   // This has an old tag.  Upgrade it.
    3047             :   SmallVector<Metadata *, 8> Ops;
    3048           8 :   Ops.reserve(T->getNumOperands());
    3049          16 :   Ops.push_back(upgradeLoopTag(T->getContext(), OldTag->getString()));
    3050          16 :   for (unsigned I = 1, E = T->getNumOperands(); I != E; ++I)
    3051           8 :     Ops.push_back(T->getOperand(I));
    3052             : 
    3053             :   return MDTuple::get(T->getContext(), Ops);
    3054             : }
    3055             : 
    3056           2 : MDNode *llvm::upgradeInstructionLoopAttachment(MDNode &N) {
    3057             :   auto *T = dyn_cast<MDTuple>(&N);
    3058             :   if (!T)
    3059             :     return &N;
    3060             : 
    3061           4 :   if (none_of(T->operands(), isOldLoopArgument))
    3062             :     return &N;
    3063             : 
    3064             :   SmallVector<Metadata *, 8> Ops;
    3065           2 :   Ops.reserve(T->getNumOperands());
    3066          22 :   for (Metadata *MD : T->operands())
    3067          10 :     Ops.push_back(upgradeLoopArgument(MD));
    3068             : 
    3069             :   return MDTuple::get(T->getContext(), Ops);
    3070             : }

Generated by: LCOV version 1.13