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" |
Jeroen Ketema | ab99b59 | 2015-09-30 10:56:37 +0000 | [diff] [blame] | 30 | #include "llvm/Support/Regex.h" |
Anton Korobeynikov | 579f071 | 2008-02-20 11:08:44 +0000 | [diff] [blame] | 31 | #include <cstring> |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 32 | using namespace llvm; |
| 33 | |
Nadav Rotem | 17ee58a | 2012-06-10 18:42:51 +0000 | [diff] [blame] | 34 | // Upgrade the declarations of the SSE4.1 functions whose arguments have |
| 35 | // changed their type from v4f32 to v2i64. |
| 36 | static bool UpgradeSSE41Function(Function* F, Intrinsic::ID IID, |
| 37 | Function *&NewFn) { |
| 38 | // Check whether this is an old version of the function, which received |
| 39 | // v4f32 arguments. |
| 40 | Type *Arg0Type = F->getFunctionType()->getParamType(0); |
| 41 | if (Arg0Type != VectorType::get(Type::getFloatTy(F->getContext()), 4)) |
| 42 | return false; |
| 43 | |
| 44 | // Yes, it's old, replace it with new version. |
| 45 | F->setName(F->getName() + ".old"); |
| 46 | NewFn = Intrinsic::getDeclaration(F->getParent(), IID); |
| 47 | return true; |
| 48 | } |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 49 | |
Chandler Carruth | 373b2b1 | 2014-09-06 10:00:01 +0000 | [diff] [blame] | 50 | // Upgrade the declarations of intrinsic functions whose 8-bit immediate mask |
| 51 | // arguments have changed their type from i32 to i8. |
| 52 | static bool UpgradeX86IntrinsicsWith8BitMask(Function *F, Intrinsic::ID IID, |
| 53 | Function *&NewFn) { |
| 54 | // Check that the last argument is an i32. |
| 55 | Type *LastArgType = F->getFunctionType()->getParamType( |
| 56 | F->getFunctionType()->getNumParams() - 1); |
| 57 | if (!LastArgType->isIntegerTy(32)) |
| 58 | return false; |
| 59 | |
| 60 | // Move this function aside and map down. |
| 61 | F->setName(F->getName() + ".old"); |
| 62 | NewFn = Intrinsic::getDeclaration(F->getParent(), IID); |
| 63 | return true; |
| 64 | } |
| 65 | |
Evan Cheng | 0e179d0 | 2007-12-17 22:33:23 +0000 | [diff] [blame] | 66 | static bool UpgradeIntrinsicFunction1(Function *F, Function *&NewFn) { |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 67 | assert(F && "Illegal to upgrade a non-existent Function."); |
| 68 | |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 69 | // Quickly eliminate it, if it's not a candidate. |
Chris Lattner | b372f66 | 2011-06-18 18:56:39 +0000 | [diff] [blame] | 70 | StringRef Name = F->getName(); |
| 71 | if (Name.size() <= 8 || !Name.startswith("llvm.")) |
Evan Cheng | 0e179d0 | 2007-12-17 22:33:23 +0000 | [diff] [blame] | 72 | return false; |
Chris Lattner | b372f66 | 2011-06-18 18:56:39 +0000 | [diff] [blame] | 73 | Name = Name.substr(5); // Strip off "llvm." |
Chris Lattner | 0bcbde4 | 2011-11-27 08:42:07 +0000 | [diff] [blame] | 74 | |
Chris Lattner | b372f66 | 2011-06-18 18:56:39 +0000 | [diff] [blame] | 75 | switch (Name[0]) { |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 76 | default: break; |
Joel Jones | 43cb878 | 2012-07-13 23:25:25 +0000 | [diff] [blame] | 77 | case 'a': { |
| 78 | if (Name.startswith("arm.neon.vclz")) { |
| 79 | Type* args[2] = { |
Matt Arsenault | c4c9226 | 2013-07-20 17:46:00 +0000 | [diff] [blame] | 80 | F->arg_begin()->getType(), |
Joel Jones | 43cb878 | 2012-07-13 23:25:25 +0000 | [diff] [blame] | 81 | Type::getInt1Ty(F->getContext()) |
| 82 | }; |
| 83 | // Can't use Intrinsic::getDeclaration here as it adds a ".i1" to |
| 84 | // the end of the name. Change name from llvm.arm.neon.vclz.* to |
| 85 | // llvm.ctlz.* |
| 86 | FunctionType* fType = FunctionType::get(F->getReturnType(), args, false); |
Matt Arsenault | c4c9226 | 2013-07-20 17:46:00 +0000 | [diff] [blame] | 87 | NewFn = Function::Create(fType, F->getLinkage(), |
Joel Jones | 43cb878 | 2012-07-13 23:25:25 +0000 | [diff] [blame] | 88 | "llvm.ctlz." + Name.substr(14), F->getParent()); |
| 89 | return true; |
| 90 | } |
Joel Jones | b84f7be | 2012-07-18 00:02:16 +0000 | [diff] [blame] | 91 | if (Name.startswith("arm.neon.vcnt")) { |
| 92 | NewFn = Intrinsic::getDeclaration(F->getParent(), Intrinsic::ctpop, |
| 93 | F->arg_begin()->getType()); |
| 94 | return true; |
| 95 | } |
Jeroen Ketema | ab99b59 | 2015-09-30 10:56:37 +0000 | [diff] [blame] | 96 | Regex vldRegex("^arm\\.neon\\.vld([1234]|[234]lane)\\.v[a-z0-9]*$"); |
| 97 | if (vldRegex.match(Name)) { |
| 98 | auto fArgs = F->getFunctionType()->params(); |
| 99 | SmallVector<Type *, 4> Tys(fArgs.begin(), fArgs.end()); |
| 100 | // Can't use Intrinsic::getDeclaration here as the return types might |
| 101 | // then only be structurally equal. |
| 102 | FunctionType* fType = FunctionType::get(F->getReturnType(), Tys, false); |
| 103 | NewFn = Function::Create(fType, F->getLinkage(), |
| 104 | "llvm." + Name + ".p0i8", F->getParent()); |
| 105 | return true; |
| 106 | } |
| 107 | Regex vstRegex("^arm\\.neon\\.vst([1234]|[234]lane)\\.v[a-z0-9]*$"); |
| 108 | if (vstRegex.match(Name)) { |
Craig Topper | 2626094 | 2015-10-18 05:15:34 +0000 | [diff] [blame] | 109 | static const Intrinsic::ID StoreInts[] = {Intrinsic::arm_neon_vst1, |
| 110 | Intrinsic::arm_neon_vst2, |
| 111 | Intrinsic::arm_neon_vst3, |
| 112 | Intrinsic::arm_neon_vst4}; |
Jeroen Ketema | ab99b59 | 2015-09-30 10:56:37 +0000 | [diff] [blame] | 113 | |
Craig Topper | 2626094 | 2015-10-18 05:15:34 +0000 | [diff] [blame] | 114 | static const Intrinsic::ID StoreLaneInts[] = { |
| 115 | Intrinsic::arm_neon_vst2lane, Intrinsic::arm_neon_vst3lane, |
| 116 | Intrinsic::arm_neon_vst4lane |
| 117 | }; |
Jeroen Ketema | ab99b59 | 2015-09-30 10:56:37 +0000 | [diff] [blame] | 118 | |
| 119 | auto fArgs = F->getFunctionType()->params(); |
| 120 | Type *Tys[] = {fArgs[0], fArgs[1]}; |
| 121 | if (Name.find("lane") == StringRef::npos) |
| 122 | NewFn = Intrinsic::getDeclaration(F->getParent(), |
| 123 | StoreInts[fArgs.size() - 3], Tys); |
| 124 | else |
| 125 | NewFn = Intrinsic::getDeclaration(F->getParent(), |
| 126 | StoreLaneInts[fArgs.size() - 5], Tys); |
| 127 | return true; |
| 128 | } |
Marcin Koscielnicki | 3fdc257 | 2016-04-19 20:51:05 +0000 | [diff] [blame] | 129 | if (Name == "aarch64.thread.pointer" || Name == "arm.thread.pointer") { |
| 130 | NewFn = Intrinsic::getDeclaration(F->getParent(), Intrinsic::thread_pointer); |
| 131 | return true; |
| 132 | } |
Joel Jones | 43cb878 | 2012-07-13 23:25:25 +0000 | [diff] [blame] | 133 | break; |
| 134 | } |
Jeroen Ketema | ab99b59 | 2015-09-30 10:56:37 +0000 | [diff] [blame] | 135 | |
Chandler Carruth | 58a71ed | 2011-12-12 04:26:04 +0000 | [diff] [blame] | 136 | case 'c': { |
Chandler Carruth | 58a71ed | 2011-12-12 04:26:04 +0000 | [diff] [blame] | 137 | if (Name.startswith("ctlz.") && F->arg_size() == 1) { |
| 138 | F->setName(Name + ".old"); |
Chandler Carruth | d4a0240 | 2011-12-12 10:57:20 +0000 | [diff] [blame] | 139 | NewFn = Intrinsic::getDeclaration(F->getParent(), Intrinsic::ctlz, |
| 140 | F->arg_begin()->getType()); |
Chandler Carruth | 58a71ed | 2011-12-12 04:26:04 +0000 | [diff] [blame] | 141 | return true; |
| 142 | } |
| 143 | if (Name.startswith("cttz.") && F->arg_size() == 1) { |
| 144 | F->setName(Name + ".old"); |
Chandler Carruth | d4a0240 | 2011-12-12 10:57:20 +0000 | [diff] [blame] | 145 | NewFn = Intrinsic::getDeclaration(F->getParent(), Intrinsic::cttz, |
| 146 | F->arg_begin()->getType()); |
Chandler Carruth | 58a71ed | 2011-12-12 04:26:04 +0000 | [diff] [blame] | 147 | return true; |
| 148 | } |
| 149 | break; |
| 150 | } |
Adrian Prantl | 87b7eb9 | 2014-10-01 18:55:02 +0000 | [diff] [blame] | 151 | |
Matt Arsenault | fbcbce4 | 2013-10-07 18:06:48 +0000 | [diff] [blame] | 152 | case 'o': |
| 153 | // We only need to change the name to match the mangling including the |
| 154 | // address space. |
| 155 | if (F->arg_size() == 2 && Name.startswith("objectsize.")) { |
| 156 | Type *Tys[2] = { F->getReturnType(), F->arg_begin()->getType() }; |
| 157 | if (F->getName() != Intrinsic::getName(Intrinsic::objectsize, Tys)) { |
| 158 | F->setName(Name + ".old"); |
| 159 | NewFn = Intrinsic::getDeclaration(F->getParent(), |
| 160 | Intrinsic::objectsize, Tys); |
| 161 | return true; |
| 162 | } |
| 163 | } |
| 164 | break; |
| 165 | |
Tim Shen | 0012756 | 2016-04-08 21:26:31 +0000 | [diff] [blame] | 166 | case 's': |
| 167 | if (Name == "stackprotectorcheck") { |
| 168 | NewFn = nullptr; |
| 169 | return true; |
| 170 | } |
| 171 | |
Craig Topper | 3b1817d | 2012-02-03 06:10:55 +0000 | [diff] [blame] | 172 | case 'x': { |
| 173 | if (Name.startswith("x86.sse2.pcmpeq.") || |
| 174 | Name.startswith("x86.sse2.pcmpgt.") || |
| 175 | Name.startswith("x86.avx2.pcmpeq.") || |
Craig Topper | d3c9e40 | 2012-04-18 05:24:00 +0000 | [diff] [blame] | 176 | Name.startswith("x86.avx2.pcmpgt.") || |
Ahmed Bougacha | 1a498705 | 2015-08-20 20:36:19 +0000 | [diff] [blame] | 177 | Name.startswith("x86.avx2.vbroadcast") || |
| 178 | Name.startswith("x86.avx2.pbroadcast") || |
Craig Topper | 7daf897 | 2012-05-08 06:58:15 +0000 | [diff] [blame] | 179 | Name.startswith("x86.avx.vpermil.") || |
Craig Topper | 1067986 | 2016-06-12 14:11:32 +0000 | [diff] [blame] | 180 | Name.startswith("x86.sse2.pshuf") || |
Craig Topper | 13cf7ca | 2016-06-13 02:36:48 +0000 | [diff] [blame] | 181 | Name.startswith("x86.avx512.mask.pshuf.d.") || |
| 182 | Name.startswith("x86.avx512.mask.pshufl.w.") || |
| 183 | Name.startswith("x86.avx512.mask.pshufh.w.") || |
Simon Pilgrim | 9cb018b | 2015-09-23 08:48:33 +0000 | [diff] [blame] | 184 | Name.startswith("x86.sse41.pmovsx") || |
Simon Pilgrim | 9602d67 | 2016-05-28 18:03:41 +0000 | [diff] [blame] | 185 | Name.startswith("x86.sse41.pmovzx") || |
| 186 | Name.startswith("x86.avx2.pmovsx") || |
| 187 | Name.startswith("x86.avx2.pmovzx") || |
Simon Pilgrim | 4298d06 | 2016-05-25 08:59:18 +0000 | [diff] [blame] | 188 | Name == "x86.sse2.cvtdq2pd" || |
| 189 | Name == "x86.sse2.cvtps2pd" || |
| 190 | Name == "x86.avx.cvtdq2.pd.256" || |
| 191 | Name == "x86.avx.cvt.ps2.pd.256" || |
Simon Pilgrim | 0afd5a4 | 2016-06-02 10:55:21 +0000 | [diff] [blame] | 192 | Name == "x86.sse2.cvttps2dq" || |
| 193 | Name.startswith("x86.avx.cvtt.") || |
Craig Topper | f9f1ecc | 2016-05-30 22:54:05 +0000 | [diff] [blame] | 194 | Name.startswith("x86.avx.vinsertf128.") || |
Sanjay Patel | 4339abe | 2015-03-12 23:16:18 +0000 | [diff] [blame] | 195 | Name == "x86.avx2.vinserti128" || |
Craig Topper | f9f1ecc | 2016-05-30 22:54:05 +0000 | [diff] [blame] | 196 | Name.startswith("x86.avx.vextractf128.") || |
Sanjay Patel | 4339abe | 2015-03-12 23:16:18 +0000 | [diff] [blame] | 197 | Name == "x86.avx2.vextracti128" || |
Craig Topper | f9f1ecc | 2016-05-30 22:54:05 +0000 | [diff] [blame] | 198 | Name.startswith("x86.avx.movnt.") || |
Craig Topper | 12e322a | 2016-05-25 06:56:32 +0000 | [diff] [blame] | 199 | Name == "x86.sse2.storel.dq" || |
Craig Topper | 8287fd8 | 2016-05-30 23:15:56 +0000 | [diff] [blame] | 200 | Name.startswith("x86.sse.storeu.") || |
| 201 | Name.startswith("x86.sse2.storeu.") || |
| 202 | Name.startswith("x86.avx.storeu.") || |
Craig Topper | 50f85c2 | 2016-05-31 01:50:02 +0000 | [diff] [blame] | 203 | Name.startswith("x86.avx512.mask.storeu.p") || |
| 204 | Name.startswith("x86.avx512.mask.storeu.b.") || |
| 205 | Name.startswith("x86.avx512.mask.storeu.w.") || |
| 206 | Name.startswith("x86.avx512.mask.storeu.d.") || |
| 207 | Name.startswith("x86.avx512.mask.storeu.q.") || |
| 208 | Name.startswith("x86.avx512.mask.store.p") || |
| 209 | Name.startswith("x86.avx512.mask.store.b.") || |
| 210 | Name.startswith("x86.avx512.mask.store.w.") || |
| 211 | Name.startswith("x86.avx512.mask.store.d.") || |
| 212 | Name.startswith("x86.avx512.mask.store.q.") || |
Craig Topper | f10fbfa | 2016-06-02 04:19:36 +0000 | [diff] [blame] | 213 | Name.startswith("x86.avx512.mask.loadu.p") || |
| 214 | Name.startswith("x86.avx512.mask.loadu.b.") || |
| 215 | Name.startswith("x86.avx512.mask.loadu.w.") || |
| 216 | Name.startswith("x86.avx512.mask.loadu.d.") || |
| 217 | Name.startswith("x86.avx512.mask.loadu.q.") || |
| 218 | Name.startswith("x86.avx512.mask.load.p") || |
| 219 | Name.startswith("x86.avx512.mask.load.b.") || |
| 220 | Name.startswith("x86.avx512.mask.load.w.") || |
| 221 | Name.startswith("x86.avx512.mask.load.d.") || |
| 222 | Name.startswith("x86.avx512.mask.load.q.") || |
Craig Topper | ef9e993 | 2013-10-15 05:20:47 +0000 | [diff] [blame] | 223 | Name == "x86.sse42.crc32.64.8" || |
Craig Topper | f9f1ecc | 2016-05-30 22:54:05 +0000 | [diff] [blame] | 224 | Name.startswith("x86.avx.vbroadcast.s") || |
Craig Topper | 33350cc | 2016-06-06 06:12:54 +0000 | [diff] [blame] | 225 | Name.startswith("x86.avx512.mask.palignr.") || |
Craig Topper | f9f1ecc | 2016-05-30 22:54:05 +0000 | [diff] [blame] | 226 | Name.startswith("x86.sse2.psll.dq") || |
| 227 | Name.startswith("x86.sse2.psrl.dq") || |
| 228 | Name.startswith("x86.avx2.psll.dq") || |
| 229 | Name.startswith("x86.avx2.psrl.dq") || |
Simon Pilgrim | f718682 | 2016-06-09 21:09:03 +0000 | [diff] [blame] | 230 | Name.startswith("x86.avx512.psll.dq") || |
| 231 | Name.startswith("x86.avx512.psrl.dq") || |
Craig Topper | 782d620 | 2015-02-28 19:33:17 +0000 | [diff] [blame] | 232 | Name == "x86.sse41.pblendw" || |
Craig Topper | f9f1ecc | 2016-05-30 22:54:05 +0000 | [diff] [blame] | 233 | Name.startswith("x86.sse41.blendp") || |
| 234 | Name.startswith("x86.avx.blend.p") || |
Craig Topper | 782d620 | 2015-02-28 19:33:17 +0000 | [diff] [blame] | 235 | Name == "x86.avx2.pblendw" || |
Craig Topper | f9f1ecc | 2016-05-30 22:54:05 +0000 | [diff] [blame] | 236 | Name.startswith("x86.avx2.pblendd.") || |
Juergen Ributzka | 1f7a176 | 2015-03-04 00:13:25 +0000 | [diff] [blame] | 237 | Name == "x86.avx2.vbroadcasti128" || |
Simon Pilgrim | e88dc04 | 2015-11-03 20:27:01 +0000 | [diff] [blame] | 238 | Name == "x86.xop.vpcmov" || |
Craig Topper | 3352ba5 | 2012-06-09 16:46:13 +0000 | [diff] [blame] | 239 | (Name.startswith("x86.xop.vpcom") && F->arg_size() == 2)) { |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 240 | NewFn = nullptr; |
Craig Topper | 3b1817d | 2012-02-03 06:10:55 +0000 | [diff] [blame] | 241 | return true; |
| 242 | } |
Nadav Rotem | 17ee58a | 2012-06-10 18:42:51 +0000 | [diff] [blame] | 243 | // SSE4.1 ptest functions may have an old signature. |
| 244 | if (Name.startswith("x86.sse41.ptest")) { |
| 245 | if (Name == "x86.sse41.ptestc") |
| 246 | return UpgradeSSE41Function(F, Intrinsic::x86_sse41_ptestc, NewFn); |
| 247 | if (Name == "x86.sse41.ptestz") |
| 248 | return UpgradeSSE41Function(F, Intrinsic::x86_sse41_ptestz, NewFn); |
| 249 | if (Name == "x86.sse41.ptestnzc") |
| 250 | return UpgradeSSE41Function(F, Intrinsic::x86_sse41_ptestnzc, NewFn); |
| 251 | } |
Sanjay Patel | 1c3eaec | 2015-02-28 22:25:06 +0000 | [diff] [blame] | 252 | // Several blend and other instructions with masks used the wrong number of |
Chandler Carruth | 373b2b1 | 2014-09-06 10:00:01 +0000 | [diff] [blame] | 253 | // bits. |
Chandler Carruth | 373b2b1 | 2014-09-06 10:00:01 +0000 | [diff] [blame] | 254 | if (Name == "x86.sse41.insertps") |
| 255 | return UpgradeX86IntrinsicsWith8BitMask(F, Intrinsic::x86_sse41_insertps, |
| 256 | NewFn); |
| 257 | if (Name == "x86.sse41.dppd") |
| 258 | return UpgradeX86IntrinsicsWith8BitMask(F, Intrinsic::x86_sse41_dppd, |
| 259 | NewFn); |
| 260 | if (Name == "x86.sse41.dpps") |
| 261 | return UpgradeX86IntrinsicsWith8BitMask(F, Intrinsic::x86_sse41_dpps, |
| 262 | NewFn); |
| 263 | if (Name == "x86.sse41.mpsadbw") |
| 264 | return UpgradeX86IntrinsicsWith8BitMask(F, Intrinsic::x86_sse41_mpsadbw, |
| 265 | NewFn); |
Chandler Carruth | 373b2b1 | 2014-09-06 10:00:01 +0000 | [diff] [blame] | 266 | if (Name == "x86.avx.dp.ps.256") |
| 267 | return UpgradeX86IntrinsicsWith8BitMask(F, Intrinsic::x86_avx_dp_ps_256, |
| 268 | NewFn); |
Chandler Carruth | 373b2b1 | 2014-09-06 10:00:01 +0000 | [diff] [blame] | 269 | if (Name == "x86.avx2.mpsadbw") |
| 270 | return UpgradeX86IntrinsicsWith8BitMask(F, Intrinsic::x86_avx2_mpsadbw, |
| 271 | NewFn); |
Craig Topper | 29f2e95 | 2015-01-25 23:26:02 +0000 | [diff] [blame] | 272 | |
Craig Topper | 71dc02d | 2012-06-13 07:18:53 +0000 | [diff] [blame] | 273 | // frcz.ss/sd may need to have an argument dropped |
| 274 | if (Name.startswith("x86.xop.vfrcz.ss") && F->arg_size() == 2) { |
| 275 | F->setName(Name + ".old"); |
| 276 | NewFn = Intrinsic::getDeclaration(F->getParent(), |
| 277 | Intrinsic::x86_xop_vfrcz_ss); |
| 278 | return true; |
| 279 | } |
| 280 | if (Name.startswith("x86.xop.vfrcz.sd") && F->arg_size() == 2) { |
| 281 | F->setName(Name + ".old"); |
| 282 | NewFn = Intrinsic::getDeclaration(F->getParent(), |
| 283 | Intrinsic::x86_xop_vfrcz_sd); |
| 284 | return true; |
| 285 | } |
Craig Topper | 720c7bd | 2012-06-03 08:07:25 +0000 | [diff] [blame] | 286 | // Fix the FMA4 intrinsics to remove the 4 |
| 287 | if (Name.startswith("x86.fma4.")) { |
Craig Topper | 2c5ccd8 | 2012-06-03 16:48:52 +0000 | [diff] [blame] | 288 | F->setName("llvm.x86.fma" + Name.substr(8)); |
| 289 | NewFn = F; |
| 290 | return true; |
Craig Topper | 720c7bd | 2012-06-03 08:07:25 +0000 | [diff] [blame] | 291 | } |
Simon Pilgrim | e85506b | 2016-06-03 08:06:03 +0000 | [diff] [blame] | 292 | // Upgrade any XOP PERMIL2 index operand still using a float/double vector. |
| 293 | if (Name.startswith("x86.xop.vpermil2")) { |
| 294 | auto Params = F->getFunctionType()->params(); |
| 295 | auto Idx = Params[2]; |
| 296 | if (Idx->getScalarType()->isFloatingPointTy()) { |
| 297 | F->setName(Name + ".old"); |
| 298 | unsigned IdxSize = Idx->getPrimitiveSizeInBits(); |
| 299 | unsigned EltSize = Idx->getScalarSizeInBits(); |
| 300 | Intrinsic::ID Permil2ID; |
| 301 | if (EltSize == 64 && IdxSize == 128) |
| 302 | Permil2ID = Intrinsic::x86_xop_vpermil2pd; |
| 303 | else if (EltSize == 32 && IdxSize == 128) |
| 304 | Permil2ID = Intrinsic::x86_xop_vpermil2ps; |
| 305 | else if (EltSize == 64 && IdxSize == 256) |
| 306 | Permil2ID = Intrinsic::x86_xop_vpermil2pd_256; |
| 307 | else |
| 308 | Permil2ID = Intrinsic::x86_xop_vpermil2ps_256; |
| 309 | NewFn = Intrinsic::getDeclaration(F->getParent(), Permil2ID); |
| 310 | return true; |
| 311 | } |
| 312 | } |
Craig Topper | 3b1817d | 2012-02-03 06:10:55 +0000 | [diff] [blame] | 313 | break; |
| 314 | } |
Chris Lattner | b372f66 | 2011-06-18 18:56:39 +0000 | [diff] [blame] | 315 | } |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 316 | |
Nadav Rotem | 17ee58a | 2012-06-10 18:42:51 +0000 | [diff] [blame] | 317 | // This may not belong here. This function is effectively being overloaded |
| 318 | // to both detect an intrinsic which needs upgrading, and to provide the |
| 319 | // upgraded form of the intrinsic. We should perhaps have two separate |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 320 | // functions for this. |
Evan Cheng | 0e179d0 | 2007-12-17 22:33:23 +0000 | [diff] [blame] | 321 | return false; |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 322 | } |
| 323 | |
Evan Cheng | 0e179d0 | 2007-12-17 22:33:23 +0000 | [diff] [blame] | 324 | bool llvm::UpgradeIntrinsicFunction(Function *F, Function *&NewFn) { |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 325 | NewFn = nullptr; |
Evan Cheng | 0e179d0 | 2007-12-17 22:33:23 +0000 | [diff] [blame] | 326 | bool Upgraded = UpgradeIntrinsicFunction1(F, NewFn); |
Filipe Cabecinhas | 0011c58 | 2015-07-03 20:12:01 +0000 | [diff] [blame] | 327 | assert(F != NewFn && "Intrinsic function upgraded to the same function"); |
Duncan Sands | 38ef3a8 | 2007-12-03 20:06:50 +0000 | [diff] [blame] | 328 | |
| 329 | // Upgrade intrinsic attributes. This does not change the function. |
Evan Cheng | 0e179d0 | 2007-12-17 22:33:23 +0000 | [diff] [blame] | 330 | if (NewFn) |
| 331 | F = NewFn; |
Pete Cooper | 9e1d335 | 2015-05-20 17:16:39 +0000 | [diff] [blame] | 332 | if (Intrinsic::ID id = F->getIntrinsicID()) |
| 333 | F->setAttributes(Intrinsic::getAttributes(F->getContext(), id)); |
Duncan Sands | 38ef3a8 | 2007-12-03 20:06:50 +0000 | [diff] [blame] | 334 | return Upgraded; |
| 335 | } |
| 336 | |
Bill Wendling | e26fffc | 2010-09-10 18:51:56 +0000 | [diff] [blame] | 337 | bool llvm::UpgradeGlobalVariable(GlobalVariable *GV) { |
Chris Lattner | 80ed9dc | 2011-06-18 06:05:24 +0000 | [diff] [blame] | 338 | // Nothing to do yet. |
Bill Wendling | e26fffc | 2010-09-10 18:51:56 +0000 | [diff] [blame] | 339 | return false; |
| 340 | } |
| 341 | |
Simon Pilgrim | f718682 | 2016-06-09 21:09:03 +0000 | [diff] [blame] | 342 | // Handles upgrading SSE2/AVX2/AVX512BW PSLLDQ intrinsics by converting them |
Craig Topper | b324e43 | 2015-02-18 06:24:44 +0000 | [diff] [blame] | 343 | // to byte shuffles. |
| 344 | static Value *UpgradeX86PSLLDQIntrinsics(IRBuilder<> &Builder, LLVMContext &C, |
Craig Topper | 7355ac3 | 2016-05-29 06:37:33 +0000 | [diff] [blame] | 345 | Value *Op, unsigned Shift) { |
| 346 | Type *ResultTy = Op->getType(); |
| 347 | unsigned NumElts = ResultTy->getVectorNumElements() * 8; |
Craig Topper | b324e43 | 2015-02-18 06:24:44 +0000 | [diff] [blame] | 348 | |
| 349 | // Bitcast from a 64-bit element type to a byte element type. |
Craig Topper | 7355ac3 | 2016-05-29 06:37:33 +0000 | [diff] [blame] | 350 | Type *VecTy = VectorType::get(Type::getInt8Ty(C), NumElts); |
| 351 | Op = Builder.CreateBitCast(Op, VecTy, "cast"); |
| 352 | |
Craig Topper | b324e43 | 2015-02-18 06:24:44 +0000 | [diff] [blame] | 353 | // We'll be shuffling in zeroes. |
Craig Topper | 7355ac3 | 2016-05-29 06:37:33 +0000 | [diff] [blame] | 354 | Value *Res = Constant::getNullValue(VecTy); |
Craig Topper | b324e43 | 2015-02-18 06:24:44 +0000 | [diff] [blame] | 355 | |
| 356 | // If shift is less than 16, emit a shuffle to move the bytes. Otherwise, |
| 357 | // we'll just return the zero vector. |
| 358 | if (Shift < 16) { |
Craig Topper | 99d1eab | 2016-06-12 00:41:19 +0000 | [diff] [blame] | 359 | uint32_t Idxs[64]; |
Simon Pilgrim | f718682 | 2016-06-09 21:09:03 +0000 | [diff] [blame] | 360 | // 256/512-bit version is split into 2/4 16-byte lanes. |
Craig Topper | b324e43 | 2015-02-18 06:24:44 +0000 | [diff] [blame] | 361 | for (unsigned l = 0; l != NumElts; l += 16) |
| 362 | for (unsigned i = 0; i != 16; ++i) { |
| 363 | unsigned Idx = NumElts + i - Shift; |
| 364 | if (Idx < NumElts) |
| 365 | Idx -= NumElts - 16; // end of lane, switch operand. |
Craig Topper | 7355ac3 | 2016-05-29 06:37:33 +0000 | [diff] [blame] | 366 | Idxs[l + i] = Idx + l; |
Craig Topper | b324e43 | 2015-02-18 06:24:44 +0000 | [diff] [blame] | 367 | } |
| 368 | |
Craig Topper | 7355ac3 | 2016-05-29 06:37:33 +0000 | [diff] [blame] | 369 | Res = Builder.CreateShuffleVector(Res, Op, makeArrayRef(Idxs, NumElts)); |
Craig Topper | b324e43 | 2015-02-18 06:24:44 +0000 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | // Bitcast back to a 64-bit element type. |
Craig Topper | 7355ac3 | 2016-05-29 06:37:33 +0000 | [diff] [blame] | 373 | return Builder.CreateBitCast(Res, ResultTy, "cast"); |
Craig Topper | b324e43 | 2015-02-18 06:24:44 +0000 | [diff] [blame] | 374 | } |
| 375 | |
Craig Topper | ea703ae | 2016-06-13 02:36:42 +0000 | [diff] [blame] | 376 | // Handles upgrading SSE2/AVX2/AVX512BW PSRLDQ intrinsics by converting them |
| 377 | // to byte shuffles. |
| 378 | static Value *UpgradeX86PSRLDQIntrinsics(IRBuilder<> &Builder, LLVMContext &C, |
| 379 | Value *Op, |
| 380 | unsigned Shift) { |
| 381 | Type *ResultTy = Op->getType(); |
| 382 | unsigned NumElts = ResultTy->getVectorNumElements() * 8; |
| 383 | |
| 384 | // Bitcast from a 64-bit element type to a byte element type. |
| 385 | Type *VecTy = VectorType::get(Type::getInt8Ty(C), NumElts); |
| 386 | Op = Builder.CreateBitCast(Op, VecTy, "cast"); |
| 387 | |
| 388 | // We'll be shuffling in zeroes. |
| 389 | Value *Res = Constant::getNullValue(VecTy); |
| 390 | |
| 391 | // If shift is less than 16, emit a shuffle to move the bytes. Otherwise, |
| 392 | // we'll just return the zero vector. |
| 393 | if (Shift < 16) { |
| 394 | uint32_t Idxs[64]; |
| 395 | // 256/512-bit version is split into 2/4 16-byte lanes. |
| 396 | for (unsigned l = 0; l != NumElts; l += 16) |
| 397 | for (unsigned i = 0; i != 16; ++i) { |
| 398 | unsigned Idx = i + Shift; |
| 399 | if (Idx >= 16) |
| 400 | Idx += NumElts - 16; // end of lane, switch operand. |
| 401 | Idxs[l + i] = Idx + l; |
| 402 | } |
| 403 | |
| 404 | Res = Builder.CreateShuffleVector(Op, Res, makeArrayRef(Idxs, NumElts)); |
| 405 | } |
| 406 | |
| 407 | // Bitcast back to a 64-bit element type. |
| 408 | return Builder.CreateBitCast(Res, ResultTy, "cast"); |
| 409 | } |
| 410 | |
| 411 | static Value *getX86MaskVec(IRBuilder<> &Builder, Value *Mask, |
| 412 | unsigned NumElts) { |
| 413 | llvm::VectorType *MaskTy = llvm::VectorType::get(Builder.getInt1Ty(), |
| 414 | cast<IntegerType>(Mask->getType())->getBitWidth()); |
| 415 | Mask = Builder.CreateBitCast(Mask, MaskTy); |
| 416 | |
| 417 | // If we have less than 8 elements, then the starting mask was an i8 and |
| 418 | // we need to extract down to the right number of elements. |
| 419 | if (NumElts < 8) { |
| 420 | uint32_t Indices[4]; |
| 421 | for (unsigned i = 0; i != NumElts; ++i) |
| 422 | Indices[i] = i; |
| 423 | Mask = Builder.CreateShuffleVector(Mask, Mask, |
| 424 | makeArrayRef(Indices, NumElts), |
| 425 | "extract"); |
| 426 | } |
| 427 | |
| 428 | return Mask; |
| 429 | } |
| 430 | |
| 431 | static Value *EmitX86Select(IRBuilder<> &Builder, Value *Mask, |
| 432 | Value *Op0, Value *Op1) { |
| 433 | // If the mask is all ones just emit the align operation. |
| 434 | if (const auto *C = dyn_cast<Constant>(Mask)) |
| 435 | if (C->isAllOnesValue()) |
| 436 | return Op0; |
| 437 | |
| 438 | Mask = getX86MaskVec(Builder, Mask, Op0->getType()->getVectorNumElements()); |
| 439 | return Builder.CreateSelect(Mask, Op0, Op1); |
| 440 | } |
| 441 | |
Craig Topper | 33350cc | 2016-06-06 06:12:54 +0000 | [diff] [blame] | 442 | static Value *UpgradeX86PALIGNRIntrinsics(IRBuilder<> &Builder, LLVMContext &C, |
| 443 | Value *Op0, Value *Op1, Value *Shift, |
| 444 | Value *Passthru, Value *Mask) { |
| 445 | unsigned ShiftVal = cast<llvm::ConstantInt>(Shift)->getZExtValue(); |
| 446 | |
| 447 | unsigned NumElts = Op0->getType()->getVectorNumElements(); |
| 448 | assert(NumElts % 16 == 0); |
| 449 | |
| 450 | // If palignr is shifting the pair of vectors more than the size of two |
| 451 | // lanes, emit zero. |
| 452 | if (ShiftVal >= 32) |
| 453 | return llvm::Constant::getNullValue(Op0->getType()); |
| 454 | |
| 455 | // If palignr is shifting the pair of input vectors more than one lane, |
| 456 | // but less than two lanes, convert to shifting in zeroes. |
| 457 | if (ShiftVal > 16) { |
| 458 | ShiftVal -= 16; |
| 459 | Op1 = Op0; |
| 460 | Op0 = llvm::Constant::getNullValue(Op0->getType()); |
| 461 | } |
| 462 | |
Craig Topper | 99d1eab | 2016-06-12 00:41:19 +0000 | [diff] [blame] | 463 | uint32_t Indices[64]; |
Craig Topper | 33350cc | 2016-06-06 06:12:54 +0000 | [diff] [blame] | 464 | // 256-bit palignr operates on 128-bit lanes so we need to handle that |
| 465 | for (unsigned l = 0; l != NumElts; l += 16) { |
| 466 | for (unsigned i = 0; i != 16; ++i) { |
| 467 | unsigned Idx = ShiftVal + i; |
| 468 | if (Idx >= 16) |
| 469 | Idx += NumElts - 16; // End of lane, switch operand. |
| 470 | Indices[l + i] = Idx + l; |
| 471 | } |
| 472 | } |
| 473 | |
| 474 | Value *Align = Builder.CreateShuffleVector(Op1, Op0, |
| 475 | makeArrayRef(Indices, NumElts), |
| 476 | "palignr"); |
| 477 | |
Craig Topper | ea703ae | 2016-06-13 02:36:42 +0000 | [diff] [blame] | 478 | return EmitX86Select(Builder, Mask, Align, Passthru); |
Craig Topper | b324e43 | 2015-02-18 06:24:44 +0000 | [diff] [blame] | 479 | } |
| 480 | |
Craig Topper | 50f85c2 | 2016-05-31 01:50:02 +0000 | [diff] [blame] | 481 | static Value *UpgradeMaskedStore(IRBuilder<> &Builder, LLVMContext &C, |
| 482 | Value *Ptr, Value *Data, Value *Mask, |
| 483 | bool Aligned) { |
| 484 | // Cast the pointer to the right type. |
| 485 | Ptr = Builder.CreateBitCast(Ptr, |
| 486 | llvm::PointerType::getUnqual(Data->getType())); |
| 487 | unsigned Align = |
| 488 | Aligned ? cast<VectorType>(Data->getType())->getBitWidth() / 8 : 1; |
| 489 | |
| 490 | // If the mask is all ones just emit a regular store. |
| 491 | if (const auto *C = dyn_cast<Constant>(Mask)) |
| 492 | if (C->isAllOnesValue()) |
| 493 | return Builder.CreateAlignedStore(Data, Ptr, Align); |
| 494 | |
| 495 | // Convert the mask from an integer type to a vector of i1. |
| 496 | unsigned NumElts = Data->getType()->getVectorNumElements(); |
Craig Topper | ea703ae | 2016-06-13 02:36:42 +0000 | [diff] [blame] | 497 | Mask = getX86MaskVec(Builder, Mask, NumElts); |
Craig Topper | 50f85c2 | 2016-05-31 01:50:02 +0000 | [diff] [blame] | 498 | return Builder.CreateMaskedStore(Data, Ptr, Align, Mask); |
| 499 | } |
| 500 | |
Craig Topper | f10fbfa | 2016-06-02 04:19:36 +0000 | [diff] [blame] | 501 | static Value *UpgradeMaskedLoad(IRBuilder<> &Builder, LLVMContext &C, |
| 502 | Value *Ptr, Value *Passthru, Value *Mask, |
| 503 | bool Aligned) { |
| 504 | // Cast the pointer to the right type. |
| 505 | Ptr = Builder.CreateBitCast(Ptr, |
| 506 | llvm::PointerType::getUnqual(Passthru->getType())); |
| 507 | unsigned Align = |
| 508 | Aligned ? cast<VectorType>(Passthru->getType())->getBitWidth() / 8 : 1; |
| 509 | |
| 510 | // If the mask is all ones just emit a regular store. |
| 511 | if (const auto *C = dyn_cast<Constant>(Mask)) |
| 512 | if (C->isAllOnesValue()) |
| 513 | return Builder.CreateAlignedLoad(Ptr, Align); |
| 514 | |
| 515 | // Convert the mask from an integer type to a vector of i1. |
| 516 | unsigned NumElts = Passthru->getType()->getVectorNumElements(); |
Craig Topper | ea703ae | 2016-06-13 02:36:42 +0000 | [diff] [blame] | 517 | Mask = getX86MaskVec(Builder, Mask, NumElts); |
Craig Topper | f10fbfa | 2016-06-02 04:19:36 +0000 | [diff] [blame] | 518 | return Builder.CreateMaskedLoad(Ptr, Align, Mask, Passthru); |
| 519 | } |
| 520 | |
Sanjay Patel | 595098f | 2016-06-15 22:01:28 +0000 | [diff] [blame] | 521 | /// Upgrade a call to an old intrinsic. All argument and return casting must be |
| 522 | /// provided to seamlessly integrate with existing context. |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 523 | void llvm::UpgradeIntrinsicCall(CallInst *CI, Function *NewFn) { |
Craig Topper | 3b1817d | 2012-02-03 06:10:55 +0000 | [diff] [blame] | 524 | Function *F = CI->getCalledFunction(); |
Nick Lewycky | 2eb3ade | 2011-12-12 22:59:34 +0000 | [diff] [blame] | 525 | LLVMContext &C = CI->getContext(); |
Chandler Carruth | 58a71ed | 2011-12-12 04:26:04 +0000 | [diff] [blame] | 526 | IRBuilder<> Builder(C); |
Duncan P. N. Exon Smith | 52888a6 | 2015-10-08 23:49:46 +0000 | [diff] [blame] | 527 | Builder.SetInsertPoint(CI->getParent(), CI->getIterator()); |
Chandler Carruth | 58a71ed | 2011-12-12 04:26:04 +0000 | [diff] [blame] | 528 | |
Craig Topper | 3b1817d | 2012-02-03 06:10:55 +0000 | [diff] [blame] | 529 | assert(F && "Intrinsic call is not direct?"); |
| 530 | |
| 531 | if (!NewFn) { |
| 532 | // Get the Function's name. |
| 533 | StringRef Name = F->getName(); |
| 534 | |
| 535 | Value *Rep; |
Sanjay Patel | 595098f | 2016-06-15 22:01:28 +0000 | [diff] [blame] | 536 | // Upgrade packed integer vector compare intrinsics to compare instructions. |
Craig Topper | 3b1817d | 2012-02-03 06:10:55 +0000 | [diff] [blame] | 537 | if (Name.startswith("llvm.x86.sse2.pcmpeq.") || |
| 538 | Name.startswith("llvm.x86.avx2.pcmpeq.")) { |
| 539 | Rep = Builder.CreateICmpEQ(CI->getArgOperand(0), CI->getArgOperand(1), |
| 540 | "pcmpeq"); |
Craig Topper | 3b1817d | 2012-02-03 06:10:55 +0000 | [diff] [blame] | 541 | Rep = Builder.CreateSExt(Rep, CI->getType(), ""); |
| 542 | } else if (Name.startswith("llvm.x86.sse2.pcmpgt.") || |
| 543 | Name.startswith("llvm.x86.avx2.pcmpgt.")) { |
| 544 | Rep = Builder.CreateICmpSGT(CI->getArgOperand(0), CI->getArgOperand(1), |
| 545 | "pcmpgt"); |
Craig Topper | 3b1817d | 2012-02-03 06:10:55 +0000 | [diff] [blame] | 546 | Rep = Builder.CreateSExt(Rep, CI->getType(), ""); |
Simon Pilgrim | 4298d06 | 2016-05-25 08:59:18 +0000 | [diff] [blame] | 547 | } else if (Name == "llvm.x86.sse2.cvtdq2pd" || |
| 548 | Name == "llvm.x86.sse2.cvtps2pd" || |
| 549 | Name == "llvm.x86.avx.cvtdq2.pd.256" || |
| 550 | Name == "llvm.x86.avx.cvt.ps2.pd.256") { |
| 551 | // Lossless i32/float to double conversion. |
| 552 | // Extract the bottom elements if necessary and convert to double vector. |
| 553 | Value *Src = CI->getArgOperand(0); |
| 554 | VectorType *SrcTy = cast<VectorType>(Src->getType()); |
| 555 | VectorType *DstTy = cast<VectorType>(CI->getType()); |
| 556 | Rep = CI->getArgOperand(0); |
| 557 | |
| 558 | unsigned NumDstElts = DstTy->getNumElements(); |
| 559 | if (NumDstElts < SrcTy->getNumElements()) { |
| 560 | assert(NumDstElts == 2 && "Unexpected vector size"); |
Craig Topper | 99d1eab | 2016-06-12 00:41:19 +0000 | [diff] [blame] | 561 | uint32_t ShuffleMask[2] = { 0, 1 }; |
| 562 | Rep = Builder.CreateShuffleVector(Rep, UndefValue::get(SrcTy), |
| 563 | ShuffleMask); |
Simon Pilgrim | 4298d06 | 2016-05-25 08:59:18 +0000 | [diff] [blame] | 564 | } |
| 565 | |
| 566 | bool Int2Double = (StringRef::npos != Name.find("cvtdq2")); |
| 567 | if (Int2Double) |
| 568 | Rep = Builder.CreateSIToFP(Rep, DstTy, "cvtdq2pd"); |
| 569 | else |
| 570 | Rep = Builder.CreateFPExt(Rep, DstTy, "cvtps2pd"); |
Simon Pilgrim | 0afd5a4 | 2016-06-02 10:55:21 +0000 | [diff] [blame] | 571 | } else if (Name == "llvm.x86.sse2.cvttps2dq" || |
| 572 | Name.startswith("llvm.x86.avx.cvtt.")) { |
| 573 | // Truncation (round to zero) float/double to i32 vector conversion. |
| 574 | Value *Src = CI->getArgOperand(0); |
| 575 | VectorType *DstTy = cast<VectorType>(CI->getType()); |
| 576 | Rep = Builder.CreateFPToSI(Src, DstTy, "cvtt"); |
Craig Topper | f9f1ecc | 2016-05-30 22:54:05 +0000 | [diff] [blame] | 577 | } else if (Name.startswith("llvm.x86.avx.movnt.")) { |
Craig Topper | 7daf897 | 2012-05-08 06:58:15 +0000 | [diff] [blame] | 578 | Module *M = F->getParent(); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 579 | SmallVector<Metadata *, 1> Elts; |
| 580 | Elts.push_back( |
| 581 | ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(C), 1))); |
Craig Topper | 7daf897 | 2012-05-08 06:58:15 +0000 | [diff] [blame] | 582 | MDNode *Node = MDNode::get(C, Elts); |
| 583 | |
| 584 | Value *Arg0 = CI->getArgOperand(0); |
| 585 | Value *Arg1 = CI->getArgOperand(1); |
| 586 | |
| 587 | // Convert the type of the pointer to a pointer to the stored type. |
| 588 | Value *BC = Builder.CreateBitCast(Arg0, |
| 589 | PointerType::getUnqual(Arg1->getType()), |
| 590 | "cast"); |
Craig Topper | 29ce55d | 2016-05-30 22:54:12 +0000 | [diff] [blame] | 591 | StoreInst *SI = Builder.CreateAlignedStore(Arg1, BC, 32); |
Craig Topper | 7daf897 | 2012-05-08 06:58:15 +0000 | [diff] [blame] | 592 | SI->setMetadata(M->getMDKindID("nontemporal"), Node); |
Craig Topper | 7daf897 | 2012-05-08 06:58:15 +0000 | [diff] [blame] | 593 | |
| 594 | // Remove intrinsic. |
| 595 | CI->eraseFromParent(); |
| 596 | return; |
Craig Topper | 12e322a | 2016-05-25 06:56:32 +0000 | [diff] [blame] | 597 | } else if (Name == "llvm.x86.sse2.storel.dq") { |
Craig Topper | 12e322a | 2016-05-25 06:56:32 +0000 | [diff] [blame] | 598 | Value *Arg0 = CI->getArgOperand(0); |
| 599 | Value *Arg1 = CI->getArgOperand(1); |
| 600 | |
| 601 | Type *NewVecTy = VectorType::get(Type::getInt64Ty(C), 2); |
| 602 | Value *BC0 = Builder.CreateBitCast(Arg1, NewVecTy, "cast"); |
| 603 | Value *Elt = Builder.CreateExtractElement(BC0, (uint64_t)0); |
| 604 | Value *BC = Builder.CreateBitCast(Arg0, |
| 605 | PointerType::getUnqual(Elt->getType()), |
| 606 | "cast"); |
Craig Topper | 29ce55d | 2016-05-30 22:54:12 +0000 | [diff] [blame] | 607 | Builder.CreateAlignedStore(Elt, BC, 1); |
Craig Topper | 12e322a | 2016-05-25 06:56:32 +0000 | [diff] [blame] | 608 | |
| 609 | // Remove intrinsic. |
| 610 | CI->eraseFromParent(); |
| 611 | return; |
Craig Topper | 8287fd8 | 2016-05-30 23:15:56 +0000 | [diff] [blame] | 612 | } else if (Name.startswith("llvm.x86.sse.storeu.") || |
| 613 | Name.startswith("llvm.x86.sse2.storeu.") || |
| 614 | Name.startswith("llvm.x86.avx.storeu.")) { |
| 615 | Value *Arg0 = CI->getArgOperand(0); |
| 616 | Value *Arg1 = CI->getArgOperand(1); |
| 617 | |
| 618 | Arg0 = Builder.CreateBitCast(Arg0, |
| 619 | PointerType::getUnqual(Arg1->getType()), |
| 620 | "cast"); |
| 621 | Builder.CreateAlignedStore(Arg1, Arg0, 1); |
| 622 | |
| 623 | // Remove intrinsic. |
| 624 | CI->eraseFromParent(); |
| 625 | return; |
Craig Topper | 50f85c2 | 2016-05-31 01:50:02 +0000 | [diff] [blame] | 626 | } else if (Name.startswith("llvm.x86.avx512.mask.storeu.p") || |
| 627 | Name.startswith("llvm.x86.avx512.mask.storeu.b.") || |
| 628 | Name.startswith("llvm.x86.avx512.mask.storeu.w.") || |
| 629 | Name.startswith("llvm.x86.avx512.mask.storeu.d.") || |
| 630 | Name.startswith("llvm.x86.avx512.mask.storeu.q.")) { |
| 631 | UpgradeMaskedStore(Builder, C, CI->getArgOperand(0), CI->getArgOperand(1), |
| 632 | CI->getArgOperand(2), /*Aligned*/false); |
| 633 | |
| 634 | // Remove intrinsic. |
| 635 | CI->eraseFromParent(); |
| 636 | return; |
| 637 | } else if (Name.startswith("llvm.x86.avx512.mask.store.p") || |
| 638 | Name.startswith("llvm.x86.avx512.mask.store.b.") || |
| 639 | Name.startswith("llvm.x86.avx512.mask.store.w.") || |
| 640 | Name.startswith("llvm.x86.avx512.mask.store.d.") || |
| 641 | Name.startswith("llvm.x86.avx512.mask.store.q.")) { |
| 642 | UpgradeMaskedStore(Builder, C, CI->getArgOperand(0), CI->getArgOperand(1), |
| 643 | CI->getArgOperand(2), /*Aligned*/true); |
| 644 | |
| 645 | // Remove intrinsic. |
| 646 | CI->eraseFromParent(); |
| 647 | return; |
Craig Topper | f10fbfa | 2016-06-02 04:19:36 +0000 | [diff] [blame] | 648 | } else if (Name.startswith("llvm.x86.avx512.mask.loadu.p") || |
| 649 | Name.startswith("llvm.x86.avx512.mask.loadu.b.") || |
| 650 | Name.startswith("llvm.x86.avx512.mask.loadu.w.") || |
| 651 | Name.startswith("llvm.x86.avx512.mask.loadu.d.") || |
| 652 | Name.startswith("llvm.x86.avx512.mask.loadu.q.")) { |
| 653 | Rep = UpgradeMaskedLoad(Builder, C, CI->getArgOperand(0), |
| 654 | CI->getArgOperand(1), CI->getArgOperand(2), |
| 655 | /*Aligned*/false); |
| 656 | } else if (Name.startswith("llvm.x86.avx512.mask.load.p") || |
| 657 | Name.startswith("llvm.x86.avx512.mask.load.b.") || |
| 658 | Name.startswith("llvm.x86.avx512.mask.load.w.") || |
| 659 | Name.startswith("llvm.x86.avx512.mask.load.d.") || |
| 660 | Name.startswith("llvm.x86.avx512.mask.load.q.")) { |
| 661 | Rep = UpgradeMaskedLoad(Builder, C, CI->getArgOperand(0), |
| 662 | CI->getArgOperand(1),CI->getArgOperand(2), |
| 663 | /*Aligned*/true); |
Craig Topper | 3352ba5 | 2012-06-09 16:46:13 +0000 | [diff] [blame] | 664 | } else if (Name.startswith("llvm.x86.xop.vpcom")) { |
| 665 | Intrinsic::ID intID; |
| 666 | if (Name.endswith("ub")) |
| 667 | intID = Intrinsic::x86_xop_vpcomub; |
| 668 | else if (Name.endswith("uw")) |
| 669 | intID = Intrinsic::x86_xop_vpcomuw; |
| 670 | else if (Name.endswith("ud")) |
| 671 | intID = Intrinsic::x86_xop_vpcomud; |
| 672 | else if (Name.endswith("uq")) |
| 673 | intID = Intrinsic::x86_xop_vpcomuq; |
| 674 | else if (Name.endswith("b")) |
| 675 | intID = Intrinsic::x86_xop_vpcomb; |
| 676 | else if (Name.endswith("w")) |
| 677 | intID = Intrinsic::x86_xop_vpcomw; |
| 678 | else if (Name.endswith("d")) |
| 679 | intID = Intrinsic::x86_xop_vpcomd; |
| 680 | else if (Name.endswith("q")) |
| 681 | intID = Intrinsic::x86_xop_vpcomq; |
| 682 | else |
| 683 | llvm_unreachable("Unknown suffix"); |
| 684 | |
| 685 | Name = Name.substr(18); // strip off "llvm.x86.xop.vpcom" |
| 686 | unsigned Imm; |
| 687 | if (Name.startswith("lt")) |
| 688 | Imm = 0; |
| 689 | else if (Name.startswith("le")) |
| 690 | Imm = 1; |
| 691 | else if (Name.startswith("gt")) |
| 692 | Imm = 2; |
| 693 | else if (Name.startswith("ge")) |
| 694 | Imm = 3; |
| 695 | else if (Name.startswith("eq")) |
| 696 | Imm = 4; |
| 697 | else if (Name.startswith("ne")) |
| 698 | Imm = 5; |
Craig Topper | 3352ba5 | 2012-06-09 16:46:13 +0000 | [diff] [blame] | 699 | else if (Name.startswith("false")) |
Craig Topper | e32546d | 2015-02-13 07:42:15 +0000 | [diff] [blame] | 700 | Imm = 6; |
| 701 | else if (Name.startswith("true")) |
Craig Topper | 3352ba5 | 2012-06-09 16:46:13 +0000 | [diff] [blame] | 702 | Imm = 7; |
| 703 | else |
| 704 | llvm_unreachable("Unknown condition"); |
| 705 | |
| 706 | Function *VPCOM = Intrinsic::getDeclaration(F->getParent(), intID); |
David Blaikie | ff6409d | 2015-05-18 22:13:54 +0000 | [diff] [blame] | 707 | Rep = |
| 708 | Builder.CreateCall(VPCOM, {CI->getArgOperand(0), CI->getArgOperand(1), |
| 709 | Builder.getInt8(Imm)}); |
Simon Pilgrim | e88dc04 | 2015-11-03 20:27:01 +0000 | [diff] [blame] | 710 | } else if (Name == "llvm.x86.xop.vpcmov") { |
| 711 | Value *Arg0 = CI->getArgOperand(0); |
| 712 | Value *Arg1 = CI->getArgOperand(1); |
| 713 | Value *Sel = CI->getArgOperand(2); |
| 714 | unsigned NumElts = CI->getType()->getVectorNumElements(); |
| 715 | Constant *MinusOne = ConstantVector::getSplat(NumElts, Builder.getInt64(-1)); |
| 716 | Value *NotSel = Builder.CreateXor(Sel, MinusOne); |
| 717 | Value *Sel0 = Builder.CreateAnd(Arg0, Sel); |
| 718 | Value *Sel1 = Builder.CreateAnd(Arg1, NotSel); |
| 719 | Rep = Builder.CreateOr(Sel0, Sel1); |
Craig Topper | ef9e993 | 2013-10-15 05:20:47 +0000 | [diff] [blame] | 720 | } else if (Name == "llvm.x86.sse42.crc32.64.8") { |
| 721 | Function *CRC32 = Intrinsic::getDeclaration(F->getParent(), |
| 722 | Intrinsic::x86_sse42_crc32_32_8); |
| 723 | Value *Trunc0 = Builder.CreateTrunc(CI->getArgOperand(0), Type::getInt32Ty(C)); |
David Blaikie | ff6409d | 2015-05-18 22:13:54 +0000 | [diff] [blame] | 724 | Rep = Builder.CreateCall(CRC32, {Trunc0, CI->getArgOperand(1)}); |
Craig Topper | ef9e993 | 2013-10-15 05:20:47 +0000 | [diff] [blame] | 725 | Rep = Builder.CreateZExt(Rep, CI->getType(), ""); |
Adam Nemet | 3906680 | 2014-05-29 23:35:33 +0000 | [diff] [blame] | 726 | } else if (Name.startswith("llvm.x86.avx.vbroadcast")) { |
| 727 | // Replace broadcasts with a series of insertelements. |
| 728 | Type *VecTy = CI->getType(); |
| 729 | Type *EltTy = VecTy->getVectorElementType(); |
| 730 | unsigned EltNum = VecTy->getVectorNumElements(); |
| 731 | Value *Cast = Builder.CreateBitCast(CI->getArgOperand(0), |
| 732 | EltTy->getPointerTo()); |
David Blaikie | 0c28fd7 | 2015-05-20 21:46:30 +0000 | [diff] [blame] | 733 | Value *Load = Builder.CreateLoad(EltTy, Cast); |
Adam Nemet | 3906680 | 2014-05-29 23:35:33 +0000 | [diff] [blame] | 734 | Type *I32Ty = Type::getInt32Ty(C); |
| 735 | Rep = UndefValue::get(VecTy); |
| 736 | for (unsigned I = 0; I < EltNum; ++I) |
| 737 | Rep = Builder.CreateInsertElement(Rep, Load, |
| 738 | ConstantInt::get(I32Ty, I)); |
Simon Pilgrim | 9602d67 | 2016-05-28 18:03:41 +0000 | [diff] [blame] | 739 | } else if (Name.startswith("llvm.x86.sse41.pmovsx") || |
| 740 | Name.startswith("llvm.x86.sse41.pmovzx") || |
| 741 | Name.startswith("llvm.x86.avx2.pmovsx") || |
| 742 | Name.startswith("llvm.x86.avx2.pmovzx")) { |
Simon Pilgrim | 9cb018b | 2015-09-23 08:48:33 +0000 | [diff] [blame] | 743 | VectorType *SrcTy = cast<VectorType>(CI->getArgOperand(0)->getType()); |
| 744 | VectorType *DstTy = cast<VectorType>(CI->getType()); |
| 745 | unsigned NumDstElts = DstTy->getNumElements(); |
| 746 | |
Simon Pilgrim | 9602d67 | 2016-05-28 18:03:41 +0000 | [diff] [blame] | 747 | // Extract a subvector of the first NumDstElts lanes and sign/zero extend. |
Craig Topper | c0a5fa0 | 2016-06-12 04:48:00 +0000 | [diff] [blame] | 748 | SmallVector<uint32_t, 8> ShuffleMask(NumDstElts); |
Craig Topper | 99d1eab | 2016-06-12 00:41:19 +0000 | [diff] [blame] | 749 | for (unsigned i = 0; i != NumDstElts; ++i) |
Craig Topper | c0a5fa0 | 2016-06-12 04:48:00 +0000 | [diff] [blame] | 750 | ShuffleMask[i] = i; |
Simon Pilgrim | 9cb018b | 2015-09-23 08:48:33 +0000 | [diff] [blame] | 751 | |
| 752 | Value *SV = Builder.CreateShuffleVector( |
| 753 | CI->getArgOperand(0), UndefValue::get(SrcTy), ShuffleMask); |
Simon Pilgrim | 9602d67 | 2016-05-28 18:03:41 +0000 | [diff] [blame] | 754 | |
| 755 | bool DoSext = (StringRef::npos != Name.find("pmovsx")); |
| 756 | Rep = DoSext ? Builder.CreateSExt(SV, DstTy) |
| 757 | : Builder.CreateZExt(SV, DstTy); |
Juergen Ributzka | 1f7a176 | 2015-03-04 00:13:25 +0000 | [diff] [blame] | 758 | } else if (Name == "llvm.x86.avx2.vbroadcasti128") { |
| 759 | // Replace vbroadcasts with a vector shuffle. |
David Blaikie | 0c28fd7 | 2015-05-20 21:46:30 +0000 | [diff] [blame] | 760 | Type *VT = VectorType::get(Type::getInt64Ty(C), 2); |
| 761 | Value *Op = Builder.CreatePointerCast(CI->getArgOperand(0), |
| 762 | PointerType::getUnqual(VT)); |
| 763 | Value *Load = Builder.CreateLoad(VT, Op); |
Craig Topper | 99d1eab | 2016-06-12 00:41:19 +0000 | [diff] [blame] | 764 | uint32_t Idxs[4] = { 0, 1, 0, 1 }; |
Juergen Ributzka | 1f7a176 | 2015-03-04 00:13:25 +0000 | [diff] [blame] | 765 | Rep = Builder.CreateShuffleVector(Load, UndefValue::get(Load->getType()), |
Sanjay Patel | 2db6d38 | 2015-03-12 15:27:07 +0000 | [diff] [blame] | 766 | Idxs); |
Ahmed Bougacha | 1a498705 | 2015-08-20 20:36:19 +0000 | [diff] [blame] | 767 | } else if (Name.startswith("llvm.x86.avx2.pbroadcast") || |
| 768 | Name.startswith("llvm.x86.avx2.vbroadcast")) { |
| 769 | // Replace vp?broadcasts with a vector shuffle. |
| 770 | Value *Op = CI->getArgOperand(0); |
| 771 | unsigned NumElts = CI->getType()->getVectorNumElements(); |
| 772 | Type *MaskTy = VectorType::get(Type::getInt32Ty(C), NumElts); |
| 773 | Rep = Builder.CreateShuffleVector(Op, UndefValue::get(Op->getType()), |
| 774 | Constant::getNullValue(MaskTy)); |
Craig Topper | 33350cc | 2016-06-06 06:12:54 +0000 | [diff] [blame] | 775 | } else if (Name.startswith("llvm.x86.avx512.mask.palignr.")) { |
| 776 | Rep = UpgradeX86PALIGNRIntrinsics(Builder, C, CI->getArgOperand(0), |
| 777 | CI->getArgOperand(1), |
| 778 | CI->getArgOperand(2), |
| 779 | CI->getArgOperand(3), |
| 780 | CI->getArgOperand(4)); |
Craig Topper | 7355ac3 | 2016-05-29 06:37:33 +0000 | [diff] [blame] | 781 | } else if (Name == "llvm.x86.sse2.psll.dq" || |
| 782 | Name == "llvm.x86.avx2.psll.dq") { |
| 783 | // 128/256-bit shift left specified in bits. |
Craig Topper | b324e43 | 2015-02-18 06:24:44 +0000 | [diff] [blame] | 784 | unsigned Shift = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue(); |
Craig Topper | 7355ac3 | 2016-05-29 06:37:33 +0000 | [diff] [blame] | 785 | Rep = UpgradeX86PSLLDQIntrinsics(Builder, C, CI->getArgOperand(0), |
Craig Topper | b324e43 | 2015-02-18 06:24:44 +0000 | [diff] [blame] | 786 | Shift / 8); // Shift is in bits. |
Craig Topper | 7355ac3 | 2016-05-29 06:37:33 +0000 | [diff] [blame] | 787 | } else if (Name == "llvm.x86.sse2.psrl.dq" || |
| 788 | Name == "llvm.x86.avx2.psrl.dq") { |
| 789 | // 128/256-bit shift right specified in bits. |
Craig Topper | b324e43 | 2015-02-18 06:24:44 +0000 | [diff] [blame] | 790 | unsigned Shift = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue(); |
Craig Topper | 7355ac3 | 2016-05-29 06:37:33 +0000 | [diff] [blame] | 791 | Rep = UpgradeX86PSRLDQIntrinsics(Builder, C, CI->getArgOperand(0), |
Craig Topper | b324e43 | 2015-02-18 06:24:44 +0000 | [diff] [blame] | 792 | Shift / 8); // Shift is in bits. |
Craig Topper | 7355ac3 | 2016-05-29 06:37:33 +0000 | [diff] [blame] | 793 | } else if (Name == "llvm.x86.sse2.psll.dq.bs" || |
Simon Pilgrim | f718682 | 2016-06-09 21:09:03 +0000 | [diff] [blame] | 794 | Name == "llvm.x86.avx2.psll.dq.bs" || |
| 795 | Name == "llvm.x86.avx512.psll.dq.512") { |
| 796 | // 128/256/512-bit shift left specified in bytes. |
Craig Topper | b324e43 | 2015-02-18 06:24:44 +0000 | [diff] [blame] | 797 | unsigned Shift = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue(); |
Craig Topper | 7355ac3 | 2016-05-29 06:37:33 +0000 | [diff] [blame] | 798 | Rep = UpgradeX86PSLLDQIntrinsics(Builder, C, CI->getArgOperand(0), Shift); |
| 799 | } else if (Name == "llvm.x86.sse2.psrl.dq.bs" || |
Simon Pilgrim | f718682 | 2016-06-09 21:09:03 +0000 | [diff] [blame] | 800 | Name == "llvm.x86.avx2.psrl.dq.bs" || |
| 801 | Name == "llvm.x86.avx512.psrl.dq.512") { |
| 802 | // 128/256/512-bit shift right specified in bytes. |
Craig Topper | b324e43 | 2015-02-18 06:24:44 +0000 | [diff] [blame] | 803 | unsigned Shift = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue(); |
Craig Topper | 7355ac3 | 2016-05-29 06:37:33 +0000 | [diff] [blame] | 804 | Rep = UpgradeX86PSRLDQIntrinsics(Builder, C, CI->getArgOperand(0), Shift); |
Craig Topper | 782d620 | 2015-02-28 19:33:17 +0000 | [diff] [blame] | 805 | } else if (Name == "llvm.x86.sse41.pblendw" || |
Craig Topper | f9f1ecc | 2016-05-30 22:54:05 +0000 | [diff] [blame] | 806 | Name.startswith("llvm.x86.sse41.blendp") || |
| 807 | Name.startswith("llvm.x86.avx.blend.p") || |
Craig Topper | 782d620 | 2015-02-28 19:33:17 +0000 | [diff] [blame] | 808 | Name == "llvm.x86.avx2.pblendw" || |
Craig Topper | f9f1ecc | 2016-05-30 22:54:05 +0000 | [diff] [blame] | 809 | Name.startswith("llvm.x86.avx2.pblendd.")) { |
Craig Topper | 782d620 | 2015-02-28 19:33:17 +0000 | [diff] [blame] | 810 | Value *Op0 = CI->getArgOperand(0); |
| 811 | Value *Op1 = CI->getArgOperand(1); |
| 812 | unsigned Imm = cast <ConstantInt>(CI->getArgOperand(2))->getZExtValue(); |
| 813 | VectorType *VecTy = cast<VectorType>(CI->getType()); |
| 814 | unsigned NumElts = VecTy->getNumElements(); |
| 815 | |
Craig Topper | c0a5fa0 | 2016-06-12 04:48:00 +0000 | [diff] [blame] | 816 | SmallVector<uint32_t, 16> Idxs(NumElts); |
| 817 | for (unsigned i = 0; i != NumElts; ++i) |
| 818 | Idxs[i] = ((Imm >> (i%8)) & 1) ? i + NumElts : i; |
Craig Topper | 782d620 | 2015-02-28 19:33:17 +0000 | [diff] [blame] | 819 | |
Craig Topper | 2f56182 | 2016-06-12 01:05:59 +0000 | [diff] [blame] | 820 | Rep = Builder.CreateShuffleVector(Op0, Op1, Idxs); |
Craig Topper | f9f1ecc | 2016-05-30 22:54:05 +0000 | [diff] [blame] | 821 | } else if (Name.startswith("llvm.x86.avx.vinsertf128.") || |
Sanjay Patel | 4339abe | 2015-03-12 23:16:18 +0000 | [diff] [blame] | 822 | Name == "llvm.x86.avx2.vinserti128") { |
Sanjay Patel | 19792fb | 2015-03-10 16:08:36 +0000 | [diff] [blame] | 823 | Value *Op0 = CI->getArgOperand(0); |
| 824 | Value *Op1 = CI->getArgOperand(1); |
| 825 | unsigned Imm = cast<ConstantInt>(CI->getArgOperand(2))->getZExtValue(); |
| 826 | VectorType *VecTy = cast<VectorType>(CI->getType()); |
| 827 | unsigned NumElts = VecTy->getNumElements(); |
Simon Pilgrim | 9cb018b | 2015-09-23 08:48:33 +0000 | [diff] [blame] | 828 | |
Sanjay Patel | 19792fb | 2015-03-10 16:08:36 +0000 | [diff] [blame] | 829 | // Mask off the high bits of the immediate value; hardware ignores those. |
| 830 | Imm = Imm & 1; |
Simon Pilgrim | 9cb018b | 2015-09-23 08:48:33 +0000 | [diff] [blame] | 831 | |
Sanjay Patel | 19792fb | 2015-03-10 16:08:36 +0000 | [diff] [blame] | 832 | // Extend the second operand into a vector that is twice as big. |
| 833 | Value *UndefV = UndefValue::get(Op1->getType()); |
Craig Topper | c0a5fa0 | 2016-06-12 04:48:00 +0000 | [diff] [blame] | 834 | SmallVector<uint32_t, 8> Idxs(NumElts); |
| 835 | for (unsigned i = 0; i != NumElts; ++i) |
| 836 | Idxs[i] = i; |
Craig Topper | 2f56182 | 2016-06-12 01:05:59 +0000 | [diff] [blame] | 837 | Rep = Builder.CreateShuffleVector(Op1, UndefV, Idxs); |
Sanjay Patel | 19792fb | 2015-03-10 16:08:36 +0000 | [diff] [blame] | 838 | |
| 839 | // Insert the second operand into the first operand. |
| 840 | |
| 841 | // Note that there is no guarantee that instruction lowering will actually |
| 842 | // produce a vinsertf128 instruction for the created shuffles. In |
| 843 | // particular, the 0 immediate case involves no lane changes, so it can |
| 844 | // be handled as a blend. |
| 845 | |
| 846 | // Example of shuffle mask for 32-bit elements: |
| 847 | // Imm = 1 <i32 0, i32 1, i32 2, i32 3, i32 8, i32 9, i32 10, i32 11> |
| 848 | // Imm = 0 <i32 8, i32 9, i32 10, i32 11, i32 4, i32 5, i32 6, i32 7 > |
| 849 | |
Sanjay Patel | 19792fb | 2015-03-10 16:08:36 +0000 | [diff] [blame] | 850 | // The low half of the result is either the low half of the 1st operand |
| 851 | // or the low half of the 2nd operand (the inserted vector). |
Craig Topper | c0a5fa0 | 2016-06-12 04:48:00 +0000 | [diff] [blame] | 852 | for (unsigned i = 0; i != NumElts / 2; ++i) |
| 853 | Idxs[i] = Imm ? i : (i + NumElts); |
Sanjay Patel | 19792fb | 2015-03-10 16:08:36 +0000 | [diff] [blame] | 854 | // The high half of the result is either the low half of the 2nd operand |
| 855 | // (the inserted vector) or the high half of the 1st operand. |
Craig Topper | c0a5fa0 | 2016-06-12 04:48:00 +0000 | [diff] [blame] | 856 | for (unsigned i = NumElts / 2; i != NumElts; ++i) |
| 857 | Idxs[i] = Imm ? (i + NumElts / 2) : i; |
Craig Topper | 2f56182 | 2016-06-12 01:05:59 +0000 | [diff] [blame] | 858 | Rep = Builder.CreateShuffleVector(Op0, Rep, Idxs); |
Craig Topper | f9f1ecc | 2016-05-30 22:54:05 +0000 | [diff] [blame] | 859 | } else if (Name.startswith("llvm.x86.avx.vextractf128.") || |
Sanjay Patel | 4339abe | 2015-03-12 23:16:18 +0000 | [diff] [blame] | 860 | Name == "llvm.x86.avx2.vextracti128") { |
Sanjay Patel | af1846c | 2015-03-12 15:15:19 +0000 | [diff] [blame] | 861 | Value *Op0 = CI->getArgOperand(0); |
| 862 | unsigned Imm = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue(); |
| 863 | VectorType *VecTy = cast<VectorType>(CI->getType()); |
| 864 | unsigned NumElts = VecTy->getNumElements(); |
Simon Pilgrim | 9cb018b | 2015-09-23 08:48:33 +0000 | [diff] [blame] | 865 | |
Sanjay Patel | af1846c | 2015-03-12 15:15:19 +0000 | [diff] [blame] | 866 | // Mask off the high bits of the immediate value; hardware ignores those. |
| 867 | Imm = Imm & 1; |
| 868 | |
| 869 | // Get indexes for either the high half or low half of the input vector. |
Craig Topper | 2f56182 | 2016-06-12 01:05:59 +0000 | [diff] [blame] | 870 | SmallVector<uint32_t, 4> Idxs(NumElts); |
Sanjay Patel | af1846c | 2015-03-12 15:15:19 +0000 | [diff] [blame] | 871 | for (unsigned i = 0; i != NumElts; ++i) { |
Craig Topper | 2f56182 | 2016-06-12 01:05:59 +0000 | [diff] [blame] | 872 | Idxs[i] = Imm ? (i + NumElts) : i; |
Sanjay Patel | af1846c | 2015-03-12 15:15:19 +0000 | [diff] [blame] | 873 | } |
| 874 | |
| 875 | Value *UndefV = UndefValue::get(Op0->getType()); |
Craig Topper | 2f56182 | 2016-06-12 01:05:59 +0000 | [diff] [blame] | 876 | Rep = Builder.CreateShuffleVector(Op0, UndefV, Idxs); |
Tim Shen | 0012756 | 2016-04-08 21:26:31 +0000 | [diff] [blame] | 877 | } else if (Name == "llvm.stackprotectorcheck") { |
| 878 | Rep = nullptr; |
Craig Topper | 1067986 | 2016-06-12 14:11:32 +0000 | [diff] [blame] | 879 | } else if (Name.startswith("llvm.x86.avx.vpermil.") || |
Craig Topper | 13cf7ca | 2016-06-13 02:36:48 +0000 | [diff] [blame] | 880 | Name == "llvm.x86.sse2.pshuf.d" || |
| 881 | Name.startswith("llvm.x86.avx512.mask.pshuf.d.")) { |
Craig Topper | 8a10505 | 2016-06-12 03:10:47 +0000 | [diff] [blame] | 882 | Value *Op0 = CI->getArgOperand(0); |
| 883 | unsigned Imm = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue(); |
| 884 | VectorType *VecTy = cast<VectorType>(CI->getType()); |
| 885 | unsigned NumElts = VecTy->getNumElements(); |
| 886 | // Calcuate the size of each index in the immediate. |
| 887 | unsigned IdxSize = 64 / VecTy->getScalarSizeInBits(); |
| 888 | unsigned IdxMask = ((1 << IdxSize) - 1); |
| 889 | |
| 890 | SmallVector<uint32_t, 8> Idxs(NumElts); |
| 891 | // Lookup the bits for this element, wrapping around the immediate every |
| 892 | // 8-bits. Elements are grouped into sets of 2 or 4 elements so we need |
| 893 | // to offset by the first index of each group. |
| 894 | for (unsigned i = 0; i != NumElts; ++i) |
| 895 | Idxs[i] = ((Imm >> ((i * IdxSize) % 8)) & IdxMask) | (i & ~IdxMask); |
| 896 | |
| 897 | Rep = Builder.CreateShuffleVector(Op0, Op0, Idxs); |
Craig Topper | 13cf7ca | 2016-06-13 02:36:48 +0000 | [diff] [blame] | 898 | |
| 899 | if (CI->getNumArgOperands() == 4) |
| 900 | Rep = EmitX86Select(Builder, CI->getArgOperand(3), Rep, |
| 901 | CI->getArgOperand(2)); |
| 902 | } else if (Name == "llvm.x86.sse2.pshufl.w" || |
| 903 | Name.startswith("llvm.x86.avx512.mask.pshufl.w.")) { |
Craig Topper | 1067986 | 2016-06-12 14:11:32 +0000 | [diff] [blame] | 904 | Value *Op0 = CI->getArgOperand(0); |
| 905 | unsigned Imm = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue(); |
| 906 | unsigned NumElts = CI->getType()->getVectorNumElements(); |
| 907 | |
| 908 | SmallVector<uint32_t, 16> Idxs(NumElts); |
| 909 | for (unsigned l = 0; l != NumElts; l += 8) { |
| 910 | for (unsigned i = 0; i != 4; ++i) |
| 911 | Idxs[i + l] = ((Imm >> (2 * i)) & 0x3) + l; |
| 912 | for (unsigned i = 4; i != 8; ++i) |
| 913 | Idxs[i + l] = i + l; |
| 914 | } |
| 915 | |
| 916 | Rep = Builder.CreateShuffleVector(Op0, Op0, Idxs); |
Craig Topper | 13cf7ca | 2016-06-13 02:36:48 +0000 | [diff] [blame] | 917 | |
| 918 | if (CI->getNumArgOperands() == 4) |
| 919 | Rep = EmitX86Select(Builder, CI->getArgOperand(3), Rep, |
| 920 | CI->getArgOperand(2)); |
| 921 | } else if (Name == "llvm.x86.sse2.pshufh.w" || |
| 922 | Name.startswith("llvm.x86.avx512.mask.pshufh.w.")) { |
Craig Topper | 1067986 | 2016-06-12 14:11:32 +0000 | [diff] [blame] | 923 | Value *Op0 = CI->getArgOperand(0); |
| 924 | unsigned Imm = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue(); |
| 925 | unsigned NumElts = CI->getType()->getVectorNumElements(); |
| 926 | |
| 927 | SmallVector<uint32_t, 16> Idxs(NumElts); |
| 928 | for (unsigned l = 0; l != NumElts; l += 8) { |
| 929 | for (unsigned i = 0; i != 4; ++i) |
| 930 | Idxs[i + l] = i + l; |
| 931 | for (unsigned i = 0; i != 4; ++i) |
| 932 | Idxs[i + l + 4] = ((Imm >> (2 * i)) & 0x3) + 4 + l; |
| 933 | } |
| 934 | |
| 935 | Rep = Builder.CreateShuffleVector(Op0, Op0, Idxs); |
Craig Topper | 13cf7ca | 2016-06-13 02:36:48 +0000 | [diff] [blame] | 936 | |
| 937 | if (CI->getNumArgOperands() == 4) |
| 938 | Rep = EmitX86Select(Builder, CI->getArgOperand(3), Rep, |
| 939 | CI->getArgOperand(2)); |
Craig Topper | 3b1817d | 2012-02-03 06:10:55 +0000 | [diff] [blame] | 940 | } else { |
Craig Topper | 8a10505 | 2016-06-12 03:10:47 +0000 | [diff] [blame] | 941 | llvm_unreachable("Unknown function for CallInst upgrade."); |
Craig Topper | 3b1817d | 2012-02-03 06:10:55 +0000 | [diff] [blame] | 942 | } |
| 943 | |
Tim Shen | 0012756 | 2016-04-08 21:26:31 +0000 | [diff] [blame] | 944 | if (Rep) |
| 945 | CI->replaceAllUsesWith(Rep); |
Craig Topper | 3b1817d | 2012-02-03 06:10:55 +0000 | [diff] [blame] | 946 | CI->eraseFromParent(); |
| 947 | return; |
| 948 | } |
| 949 | |
Yaron Keren | d1fdbe7 | 2015-03-30 16:10:39 +0000 | [diff] [blame] | 950 | std::string Name = CI->getName(); |
Adrian Prantl | 87b7eb9 | 2014-10-01 18:55:02 +0000 | [diff] [blame] | 951 | if (!Name.empty()) |
| 952 | CI->setName(Name + ".old"); |
Nadav Rotem | 17ee58a | 2012-06-10 18:42:51 +0000 | [diff] [blame] | 953 | |
Chandler Carruth | 58a71ed | 2011-12-12 04:26:04 +0000 | [diff] [blame] | 954 | switch (NewFn->getIntrinsicID()) { |
| 955 | default: |
Chris Lattner | 0bcbde4 | 2011-11-27 08:42:07 +0000 | [diff] [blame] | 956 | llvm_unreachable("Unknown function for CallInst upgrade."); |
Chandler Carruth | 58a71ed | 2011-12-12 04:26:04 +0000 | [diff] [blame] | 957 | |
Jeroen Ketema | ab99b59 | 2015-09-30 10:56:37 +0000 | [diff] [blame] | 958 | case Intrinsic::arm_neon_vld1: |
| 959 | case Intrinsic::arm_neon_vld2: |
| 960 | case Intrinsic::arm_neon_vld3: |
| 961 | case Intrinsic::arm_neon_vld4: |
| 962 | case Intrinsic::arm_neon_vld2lane: |
| 963 | case Intrinsic::arm_neon_vld3lane: |
| 964 | case Intrinsic::arm_neon_vld4lane: |
| 965 | case Intrinsic::arm_neon_vst1: |
| 966 | case Intrinsic::arm_neon_vst2: |
| 967 | case Intrinsic::arm_neon_vst3: |
| 968 | case Intrinsic::arm_neon_vst4: |
| 969 | case Intrinsic::arm_neon_vst2lane: |
| 970 | case Intrinsic::arm_neon_vst3lane: |
| 971 | case Intrinsic::arm_neon_vst4lane: { |
| 972 | SmallVector<Value *, 4> Args(CI->arg_operands().begin(), |
| 973 | CI->arg_operands().end()); |
| 974 | CI->replaceAllUsesWith(Builder.CreateCall(NewFn, Args)); |
| 975 | CI->eraseFromParent(); |
| 976 | return; |
| 977 | } |
| 978 | |
Chandler Carruth | 58a71ed | 2011-12-12 04:26:04 +0000 | [diff] [blame] | 979 | case Intrinsic::ctlz: |
Nuno Lopes | ad40c0a | 2012-05-22 15:25:31 +0000 | [diff] [blame] | 980 | case Intrinsic::cttz: |
Chandler Carruth | 58a71ed | 2011-12-12 04:26:04 +0000 | [diff] [blame] | 981 | assert(CI->getNumArgOperands() == 1 && |
| 982 | "Mismatch between function args and call args"); |
David Blaikie | ff6409d | 2015-05-18 22:13:54 +0000 | [diff] [blame] | 983 | CI->replaceAllUsesWith(Builder.CreateCall( |
| 984 | NewFn, {CI->getArgOperand(0), Builder.getFalse()}, Name)); |
Chandler Carruth | 58a71ed | 2011-12-12 04:26:04 +0000 | [diff] [blame] | 985 | CI->eraseFromParent(); |
| 986 | return; |
Nadav Rotem | 17ee58a | 2012-06-10 18:42:51 +0000 | [diff] [blame] | 987 | |
Matt Arsenault | fbcbce4 | 2013-10-07 18:06:48 +0000 | [diff] [blame] | 988 | case Intrinsic::objectsize: |
David Blaikie | ff6409d | 2015-05-18 22:13:54 +0000 | [diff] [blame] | 989 | CI->replaceAllUsesWith(Builder.CreateCall( |
| 990 | NewFn, {CI->getArgOperand(0), CI->getArgOperand(1)}, Name)); |
Matt Arsenault | fbcbce4 | 2013-10-07 18:06:48 +0000 | [diff] [blame] | 991 | CI->eraseFromParent(); |
| 992 | return; |
| 993 | |
Joel Jones | b84f7be | 2012-07-18 00:02:16 +0000 | [diff] [blame] | 994 | case Intrinsic::ctpop: { |
David Blaikie | ff6409d | 2015-05-18 22:13:54 +0000 | [diff] [blame] | 995 | CI->replaceAllUsesWith(Builder.CreateCall(NewFn, {CI->getArgOperand(0)})); |
Joel Jones | b84f7be | 2012-07-18 00:02:16 +0000 | [diff] [blame] | 996 | CI->eraseFromParent(); |
| 997 | return; |
| 998 | } |
Joel Jones | 43cb878 | 2012-07-13 23:25:25 +0000 | [diff] [blame] | 999 | |
Craig Topper | 71dc02d | 2012-06-13 07:18:53 +0000 | [diff] [blame] | 1000 | case Intrinsic::x86_xop_vfrcz_ss: |
| 1001 | case Intrinsic::x86_xop_vfrcz_sd: |
David Blaikie | ff6409d | 2015-05-18 22:13:54 +0000 | [diff] [blame] | 1002 | CI->replaceAllUsesWith( |
| 1003 | Builder.CreateCall(NewFn, {CI->getArgOperand(1)}, Name)); |
Craig Topper | 71dc02d | 2012-06-13 07:18:53 +0000 | [diff] [blame] | 1004 | CI->eraseFromParent(); |
| 1005 | return; |
| 1006 | |
Simon Pilgrim | e85506b | 2016-06-03 08:06:03 +0000 | [diff] [blame] | 1007 | case Intrinsic::x86_xop_vpermil2pd: |
| 1008 | case Intrinsic::x86_xop_vpermil2ps: |
| 1009 | case Intrinsic::x86_xop_vpermil2pd_256: |
| 1010 | case Intrinsic::x86_xop_vpermil2ps_256: { |
| 1011 | SmallVector<Value *, 4> Args(CI->arg_operands().begin(), |
| 1012 | CI->arg_operands().end()); |
| 1013 | VectorType *FltIdxTy = cast<VectorType>(Args[2]->getType()); |
| 1014 | VectorType *IntIdxTy = VectorType::getInteger(FltIdxTy); |
| 1015 | Args[2] = Builder.CreateBitCast(Args[2], IntIdxTy); |
| 1016 | CI->replaceAllUsesWith(Builder.CreateCall(NewFn, Args, Name)); |
| 1017 | CI->eraseFromParent(); |
| 1018 | return; |
| 1019 | } |
| 1020 | |
Nadav Rotem | 17ee58a | 2012-06-10 18:42:51 +0000 | [diff] [blame] | 1021 | case Intrinsic::x86_sse41_ptestc: |
| 1022 | case Intrinsic::x86_sse41_ptestz: |
Craig Topper | 71dc02d | 2012-06-13 07:18:53 +0000 | [diff] [blame] | 1023 | case Intrinsic::x86_sse41_ptestnzc: { |
Nadav Rotem | 17ee58a | 2012-06-10 18:42:51 +0000 | [diff] [blame] | 1024 | // The arguments for these intrinsics used to be v4f32, and changed |
| 1025 | // to v2i64. This is purely a nop, since those are bitwise intrinsics. |
| 1026 | // So, the only thing required is a bitcast for both arguments. |
| 1027 | // First, check the arguments have the old type. |
| 1028 | Value *Arg0 = CI->getArgOperand(0); |
| 1029 | if (Arg0->getType() != VectorType::get(Type::getFloatTy(C), 4)) |
| 1030 | return; |
| 1031 | |
| 1032 | // Old intrinsic, add bitcasts |
| 1033 | Value *Arg1 = CI->getArgOperand(1); |
| 1034 | |
David Blaikie | 5bacf37 | 2015-04-24 21:16:07 +0000 | [diff] [blame] | 1035 | Type *NewVecTy = VectorType::get(Type::getInt64Ty(C), 2); |
Nadav Rotem | 17ee58a | 2012-06-10 18:42:51 +0000 | [diff] [blame] | 1036 | |
David Blaikie | 5bacf37 | 2015-04-24 21:16:07 +0000 | [diff] [blame] | 1037 | Value *BC0 = Builder.CreateBitCast(Arg0, NewVecTy, "cast"); |
| 1038 | Value *BC1 = Builder.CreateBitCast(Arg1, NewVecTy, "cast"); |
| 1039 | |
David Blaikie | ff6409d | 2015-05-18 22:13:54 +0000 | [diff] [blame] | 1040 | CallInst *NewCall = Builder.CreateCall(NewFn, {BC0, BC1}, Name); |
Nadav Rotem | 17ee58a | 2012-06-10 18:42:51 +0000 | [diff] [blame] | 1041 | CI->replaceAllUsesWith(NewCall); |
| 1042 | CI->eraseFromParent(); |
| 1043 | return; |
Evan Cheng | 0e179d0 | 2007-12-17 22:33:23 +0000 | [diff] [blame] | 1044 | } |
Chandler Carruth | 373b2b1 | 2014-09-06 10:00:01 +0000 | [diff] [blame] | 1045 | |
Chandler Carruth | 373b2b1 | 2014-09-06 10:00:01 +0000 | [diff] [blame] | 1046 | case Intrinsic::x86_sse41_insertps: |
| 1047 | case Intrinsic::x86_sse41_dppd: |
| 1048 | case Intrinsic::x86_sse41_dpps: |
| 1049 | case Intrinsic::x86_sse41_mpsadbw: |
Chandler Carruth | 373b2b1 | 2014-09-06 10:00:01 +0000 | [diff] [blame] | 1050 | case Intrinsic::x86_avx_dp_ps_256: |
Chandler Carruth | 373b2b1 | 2014-09-06 10:00:01 +0000 | [diff] [blame] | 1051 | case Intrinsic::x86_avx2_mpsadbw: { |
| 1052 | // Need to truncate the last argument from i32 to i8 -- this argument models |
| 1053 | // an inherently 8-bit immediate operand to these x86 instructions. |
| 1054 | SmallVector<Value *, 4> Args(CI->arg_operands().begin(), |
| 1055 | CI->arg_operands().end()); |
| 1056 | |
| 1057 | // Replace the last argument with a trunc. |
| 1058 | Args.back() = Builder.CreateTrunc(Args.back(), Type::getInt8Ty(C), "trunc"); |
| 1059 | |
| 1060 | CallInst *NewCall = Builder.CreateCall(NewFn, Args); |
| 1061 | CI->replaceAllUsesWith(NewCall); |
| 1062 | CI->eraseFromParent(); |
| 1063 | return; |
| 1064 | } |
Marcin Koscielnicki | 3fdc257 | 2016-04-19 20:51:05 +0000 | [diff] [blame] | 1065 | |
| 1066 | case Intrinsic::thread_pointer: { |
| 1067 | CI->replaceAllUsesWith(Builder.CreateCall(NewFn, {})); |
| 1068 | CI->eraseFromParent(); |
| 1069 | return; |
| 1070 | } |
Craig Topper | 71dc02d | 2012-06-13 07:18:53 +0000 | [diff] [blame] | 1071 | } |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 1072 | } |
| 1073 | |
Sanjay Patel | fdf0d5f | 2016-04-18 19:11:57 +0000 | [diff] [blame] | 1074 | void llvm::UpgradeCallsToIntrinsic(Function *F) { |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 1075 | assert(F && "Illegal attempt to upgrade a non-existent intrinsic."); |
| 1076 | |
Sanjay Patel | fdf0d5f | 2016-04-18 19:11:57 +0000 | [diff] [blame] | 1077 | // Check if this function should be upgraded and get the replacement function |
| 1078 | // if there is one. |
Chris Lattner | 80ed9dc | 2011-06-18 06:05:24 +0000 | [diff] [blame] | 1079 | Function *NewFn; |
Evan Cheng | 0e179d0 | 2007-12-17 22:33:23 +0000 | [diff] [blame] | 1080 | if (UpgradeIntrinsicFunction(F, NewFn)) { |
Sanjay Patel | fdf0d5f | 2016-04-18 19:11:57 +0000 | [diff] [blame] | 1081 | // Replace all users of the old function with the new function or new |
| 1082 | // instructions. This is not a range loop because the call is deleted. |
| 1083 | for (auto UI = F->user_begin(), UE = F->user_end(); UI != UE; ) |
Duncan P. N. Exon Smith | 93f53c4 | 2016-04-17 03:59:37 +0000 | [diff] [blame] | 1084 | if (CallInst *CI = dyn_cast<CallInst>(*UI++)) |
Filipe Cabecinhas | 0011c58 | 2015-07-03 20:12:01 +0000 | [diff] [blame] | 1085 | UpgradeIntrinsicCall(CI, NewFn); |
Sanjay Patel | fdf0d5f | 2016-04-18 19:11:57 +0000 | [diff] [blame] | 1086 | |
Filipe Cabecinhas | 0011c58 | 2015-07-03 20:12:01 +0000 | [diff] [blame] | 1087 | // Remove old function, no longer used, from the module. |
| 1088 | F->eraseFromParent(); |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 1089 | } |
| 1090 | } |
Devang Patel | 80ae349 | 2009-08-28 23:24:31 +0000 | [diff] [blame] | 1091 | |
Manman Ren | 209b17c | 2013-09-28 00:22:27 +0000 | [diff] [blame] | 1092 | void llvm::UpgradeInstWithTBAATag(Instruction *I) { |
Duncan P. N. Exon Smith | de36e80 | 2014-11-11 21:30:22 +0000 | [diff] [blame] | 1093 | MDNode *MD = I->getMetadata(LLVMContext::MD_tbaa); |
Manman Ren | 209b17c | 2013-09-28 00:22:27 +0000 | [diff] [blame] | 1094 | assert(MD && "UpgradeInstWithTBAATag should have a TBAA tag"); |
| 1095 | // Check if the tag uses struct-path aware TBAA format. |
| 1096 | if (isa<MDNode>(MD->getOperand(0)) && MD->getNumOperands() >= 3) |
| 1097 | return; |
| 1098 | |
| 1099 | if (MD->getNumOperands() == 3) { |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 1100 | Metadata *Elts[] = {MD->getOperand(0), MD->getOperand(1)}; |
Manman Ren | 209b17c | 2013-09-28 00:22:27 +0000 | [diff] [blame] | 1101 | MDNode *ScalarType = MDNode::get(I->getContext(), Elts); |
| 1102 | // Create a MDNode <ScalarType, ScalarType, offset 0, const> |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 1103 | Metadata *Elts2[] = {ScalarType, ScalarType, |
| 1104 | ConstantAsMetadata::get(Constant::getNullValue( |
| 1105 | Type::getInt64Ty(I->getContext()))), |
| 1106 | MD->getOperand(2)}; |
Manman Ren | 209b17c | 2013-09-28 00:22:27 +0000 | [diff] [blame] | 1107 | I->setMetadata(LLVMContext::MD_tbaa, MDNode::get(I->getContext(), Elts2)); |
| 1108 | } else { |
| 1109 | // Create a MDNode <MD, MD, offset 0> |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 1110 | Metadata *Elts[] = {MD, MD, ConstantAsMetadata::get(Constant::getNullValue( |
| 1111 | Type::getInt64Ty(I->getContext())))}; |
Manman Ren | 209b17c | 2013-09-28 00:22:27 +0000 | [diff] [blame] | 1112 | I->setMetadata(LLVMContext::MD_tbaa, MDNode::get(I->getContext(), Elts)); |
| 1113 | } |
| 1114 | } |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 1115 | |
| 1116 | Instruction *llvm::UpgradeBitCastInst(unsigned Opc, Value *V, Type *DestTy, |
| 1117 | Instruction *&Temp) { |
| 1118 | if (Opc != Instruction::BitCast) |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 1119 | return nullptr; |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 1120 | |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 1121 | Temp = nullptr; |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 1122 | Type *SrcTy = V->getType(); |
| 1123 | if (SrcTy->isPtrOrPtrVectorTy() && DestTy->isPtrOrPtrVectorTy() && |
| 1124 | SrcTy->getPointerAddressSpace() != DestTy->getPointerAddressSpace()) { |
| 1125 | LLVMContext &Context = V->getContext(); |
| 1126 | |
| 1127 | // We have no information about target data layout, so we assume that |
| 1128 | // the maximum pointer size is 64bit. |
| 1129 | Type *MidTy = Type::getInt64Ty(Context); |
| 1130 | Temp = CastInst::Create(Instruction::PtrToInt, V, MidTy); |
| 1131 | |
| 1132 | return CastInst::Create(Instruction::IntToPtr, Temp, DestTy); |
| 1133 | } |
| 1134 | |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 1135 | return nullptr; |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 1136 | } |
| 1137 | |
| 1138 | Value *llvm::UpgradeBitCastExpr(unsigned Opc, Constant *C, Type *DestTy) { |
| 1139 | if (Opc != Instruction::BitCast) |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 1140 | return nullptr; |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 1141 | |
| 1142 | Type *SrcTy = C->getType(); |
| 1143 | if (SrcTy->isPtrOrPtrVectorTy() && DestTy->isPtrOrPtrVectorTy() && |
| 1144 | SrcTy->getPointerAddressSpace() != DestTy->getPointerAddressSpace()) { |
| 1145 | LLVMContext &Context = C->getContext(); |
| 1146 | |
| 1147 | // We have no information about target data layout, so we assume that |
| 1148 | // the maximum pointer size is 64bit. |
| 1149 | Type *MidTy = Type::getInt64Ty(Context); |
| 1150 | |
| 1151 | return ConstantExpr::getIntToPtr(ConstantExpr::getPtrToInt(C, MidTy), |
| 1152 | DestTy); |
| 1153 | } |
| 1154 | |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 1155 | return nullptr; |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 1156 | } |
Manman Ren | 8b4306c | 2013-12-02 21:29:56 +0000 | [diff] [blame] | 1157 | |
| 1158 | /// Check the debug info version number, if it is out-dated, drop the debug |
| 1159 | /// info. Return true if module is modified. |
| 1160 | bool llvm::UpgradeDebugInfo(Module &M) { |
Manman Ren | 2ebfb42 | 2014-01-16 01:51:12 +0000 | [diff] [blame] | 1161 | unsigned Version = getDebugMetadataVersionFromModule(M); |
| 1162 | if (Version == DEBUG_METADATA_VERSION) |
Manman Ren | 8b4306c | 2013-12-02 21:29:56 +0000 | [diff] [blame] | 1163 | return false; |
| 1164 | |
Manman Ren | 2ebfb42 | 2014-01-16 01:51:12 +0000 | [diff] [blame] | 1165 | bool RetCode = StripDebugInfo(M); |
| 1166 | if (RetCode) { |
| 1167 | DiagnosticInfoDebugMetadataVersion DiagVersion(M, Version); |
| 1168 | M.getContext().diagnose(DiagVersion); |
| 1169 | } |
| 1170 | return RetCode; |
Manman Ren | 8b4306c | 2013-12-02 21:29:56 +0000 | [diff] [blame] | 1171 | } |
Eli Bendersky | 5d5e18d | 2014-06-25 15:41:00 +0000 | [diff] [blame] | 1172 | |
Manman Ren | b5d7ff4 | 2016-05-25 23:14:48 +0000 | [diff] [blame] | 1173 | bool llvm::UpgradeModuleFlags(Module &M) { |
| 1174 | const NamedMDNode *ModFlags = M.getModuleFlagsMetadata(); |
| 1175 | if (!ModFlags) |
| 1176 | return false; |
| 1177 | |
| 1178 | bool HasObjCFlag = false, HasClassProperties = false; |
| 1179 | for (unsigned I = 0, E = ModFlags->getNumOperands(); I != E; ++I) { |
| 1180 | MDNode *Op = ModFlags->getOperand(I); |
| 1181 | if (Op->getNumOperands() < 2) |
| 1182 | continue; |
| 1183 | MDString *ID = dyn_cast_or_null<MDString>(Op->getOperand(1)); |
| 1184 | if (!ID) |
| 1185 | continue; |
| 1186 | if (ID->getString() == "Objective-C Image Info Version") |
| 1187 | HasObjCFlag = true; |
| 1188 | if (ID->getString() == "Objective-C Class Properties") |
| 1189 | HasClassProperties = true; |
| 1190 | } |
| 1191 | // "Objective-C Class Properties" is recently added for Objective-C. We |
| 1192 | // upgrade ObjC bitcodes to contain a "Objective-C Class Properties" module |
| 1193 | // flag of value 0, so we can correclty report error when trying to link |
| 1194 | // an ObjC bitcode without this module flag with an ObjC bitcode with this |
| 1195 | // module flag. |
| 1196 | if (HasObjCFlag && !HasClassProperties) { |
| 1197 | M.addModuleFlag(llvm::Module::Error, "Objective-C Class Properties", |
| 1198 | (uint32_t)0); |
| 1199 | return true; |
| 1200 | } |
| 1201 | return false; |
| 1202 | } |
| 1203 | |
Duncan P. N. Exon Smith | efe16c8 | 2016-03-25 00:56:13 +0000 | [diff] [blame] | 1204 | static bool isOldLoopArgument(Metadata *MD) { |
| 1205 | auto *T = dyn_cast_or_null<MDTuple>(MD); |
| 1206 | if (!T) |
| 1207 | return false; |
| 1208 | if (T->getNumOperands() < 1) |
| 1209 | return false; |
| 1210 | auto *S = dyn_cast_or_null<MDString>(T->getOperand(0)); |
| 1211 | if (!S) |
| 1212 | return false; |
| 1213 | return S->getString().startswith("llvm.vectorizer."); |
| 1214 | } |
| 1215 | |
| 1216 | static MDString *upgradeLoopTag(LLVMContext &C, StringRef OldTag) { |
| 1217 | StringRef OldPrefix = "llvm.vectorizer."; |
| 1218 | assert(OldTag.startswith(OldPrefix) && "Expected old prefix"); |
| 1219 | |
| 1220 | if (OldTag == "llvm.vectorizer.unroll") |
| 1221 | return MDString::get(C, "llvm.loop.interleave.count"); |
| 1222 | |
| 1223 | return MDString::get( |
| 1224 | C, (Twine("llvm.loop.vectorize.") + OldTag.drop_front(OldPrefix.size())) |
| 1225 | .str()); |
| 1226 | } |
| 1227 | |
| 1228 | static Metadata *upgradeLoopArgument(Metadata *MD) { |
| 1229 | auto *T = dyn_cast_or_null<MDTuple>(MD); |
| 1230 | if (!T) |
| 1231 | return MD; |
| 1232 | if (T->getNumOperands() < 1) |
| 1233 | return MD; |
| 1234 | auto *OldTag = dyn_cast_or_null<MDString>(T->getOperand(0)); |
| 1235 | if (!OldTag) |
| 1236 | return MD; |
| 1237 | if (!OldTag->getString().startswith("llvm.vectorizer.")) |
| 1238 | return MD; |
| 1239 | |
| 1240 | // This has an old tag. Upgrade it. |
| 1241 | SmallVector<Metadata *, 8> Ops; |
| 1242 | Ops.reserve(T->getNumOperands()); |
| 1243 | Ops.push_back(upgradeLoopTag(T->getContext(), OldTag->getString())); |
| 1244 | for (unsigned I = 1, E = T->getNumOperands(); I != E; ++I) |
| 1245 | Ops.push_back(T->getOperand(I)); |
| 1246 | |
| 1247 | return MDTuple::get(T->getContext(), Ops); |
| 1248 | } |
| 1249 | |
| 1250 | MDNode *llvm::upgradeInstructionLoopAttachment(MDNode &N) { |
| 1251 | auto *T = dyn_cast<MDTuple>(&N); |
| 1252 | if (!T) |
| 1253 | return &N; |
| 1254 | |
| 1255 | if (!llvm::any_of(T->operands(), isOldLoopArgument)) |
| 1256 | return &N; |
| 1257 | |
| 1258 | SmallVector<Metadata *, 8> Ops; |
| 1259 | Ops.reserve(T->getNumOperands()); |
| 1260 | for (Metadata *MD : T->operands()) |
| 1261 | Ops.push_back(upgradeLoopArgument(MD)); |
| 1262 | |
| 1263 | return MDTuple::get(T->getContext(), Ops); |
Eli Bendersky | 5d5e18d | 2014-06-25 15:41:00 +0000 | [diff] [blame] | 1264 | } |