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