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.") || |
Simon Pilgrim | 9cb018b | 2015-09-23 08:48:33 +0000 | [diff] [blame] | 180 | Name.startswith("x86.sse41.pmovsx") || |
Simon Pilgrim | 9602d67 | 2016-05-28 18:03:41 +0000 | [diff] [blame] | 181 | Name.startswith("x86.sse41.pmovzx") || |
| 182 | Name.startswith("x86.avx2.pmovsx") || |
| 183 | Name.startswith("x86.avx2.pmovzx") || |
Simon Pilgrim | 4298d06 | 2016-05-25 08:59:18 +0000 | [diff] [blame] | 184 | Name == "x86.sse2.cvtdq2pd" || |
| 185 | Name == "x86.sse2.cvtps2pd" || |
| 186 | Name == "x86.avx.cvtdq2.pd.256" || |
| 187 | Name == "x86.avx.cvt.ps2.pd.256" || |
Craig Topper | f9f1ecc | 2016-05-30 22:54:05 +0000 | [diff] [blame] | 188 | Name.startswith("x86.avx.vinsertf128.") || |
Sanjay Patel | 4339abe | 2015-03-12 23:16:18 +0000 | [diff] [blame] | 189 | Name == "x86.avx2.vinserti128" || |
Craig Topper | f9f1ecc | 2016-05-30 22:54:05 +0000 | [diff] [blame] | 190 | Name.startswith("x86.avx.vextractf128.") || |
Sanjay Patel | 4339abe | 2015-03-12 23:16:18 +0000 | [diff] [blame] | 191 | Name == "x86.avx2.vextracti128" || |
Craig Topper | f9f1ecc | 2016-05-30 22:54:05 +0000 | [diff] [blame] | 192 | Name.startswith("x86.avx.movnt.") || |
Craig Topper | 12e322a | 2016-05-25 06:56:32 +0000 | [diff] [blame] | 193 | Name == "x86.sse2.storel.dq" || |
Craig Topper | ef9e993 | 2013-10-15 05:20:47 +0000 | [diff] [blame] | 194 | Name == "x86.sse42.crc32.64.8" || |
Craig Topper | f9f1ecc | 2016-05-30 22:54:05 +0000 | [diff] [blame] | 195 | Name.startswith("x86.avx.vbroadcast.s") || |
| 196 | Name.startswith("x86.sse2.psll.dq") || |
| 197 | Name.startswith("x86.sse2.psrl.dq") || |
| 198 | Name.startswith("x86.avx2.psll.dq") || |
| 199 | Name.startswith("x86.avx2.psrl.dq") || |
Craig Topper | 782d620 | 2015-02-28 19:33:17 +0000 | [diff] [blame] | 200 | Name == "x86.sse41.pblendw" || |
Craig Topper | f9f1ecc | 2016-05-30 22:54:05 +0000 | [diff] [blame] | 201 | Name.startswith("x86.sse41.blendp") || |
| 202 | Name.startswith("x86.avx.blend.p") || |
Craig Topper | 782d620 | 2015-02-28 19:33:17 +0000 | [diff] [blame] | 203 | Name == "x86.avx2.pblendw" || |
Craig Topper | f9f1ecc | 2016-05-30 22:54:05 +0000 | [diff] [blame] | 204 | Name.startswith("x86.avx2.pblendd.") || |
Juergen Ributzka | 1f7a176 | 2015-03-04 00:13:25 +0000 | [diff] [blame] | 205 | Name == "x86.avx2.vbroadcasti128" || |
Simon Pilgrim | e88dc04 | 2015-11-03 20:27:01 +0000 | [diff] [blame] | 206 | Name == "x86.xop.vpcmov" || |
Craig Topper | 3352ba5 | 2012-06-09 16:46:13 +0000 | [diff] [blame] | 207 | (Name.startswith("x86.xop.vpcom") && F->arg_size() == 2)) { |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 208 | NewFn = nullptr; |
Craig Topper | 3b1817d | 2012-02-03 06:10:55 +0000 | [diff] [blame] | 209 | return true; |
| 210 | } |
Nadav Rotem | 17ee58a | 2012-06-10 18:42:51 +0000 | [diff] [blame] | 211 | // SSE4.1 ptest functions may have an old signature. |
| 212 | if (Name.startswith("x86.sse41.ptest")) { |
| 213 | if (Name == "x86.sse41.ptestc") |
| 214 | return UpgradeSSE41Function(F, Intrinsic::x86_sse41_ptestc, NewFn); |
| 215 | if (Name == "x86.sse41.ptestz") |
| 216 | return UpgradeSSE41Function(F, Intrinsic::x86_sse41_ptestz, NewFn); |
| 217 | if (Name == "x86.sse41.ptestnzc") |
| 218 | return UpgradeSSE41Function(F, Intrinsic::x86_sse41_ptestnzc, NewFn); |
| 219 | } |
Sanjay Patel | 1c3eaec | 2015-02-28 22:25:06 +0000 | [diff] [blame] | 220 | // Several blend and other instructions with masks used the wrong number of |
Chandler Carruth | 373b2b1 | 2014-09-06 10:00:01 +0000 | [diff] [blame] | 221 | // bits. |
Chandler Carruth | 373b2b1 | 2014-09-06 10:00:01 +0000 | [diff] [blame] | 222 | if (Name == "x86.sse41.insertps") |
| 223 | return UpgradeX86IntrinsicsWith8BitMask(F, Intrinsic::x86_sse41_insertps, |
| 224 | NewFn); |
| 225 | if (Name == "x86.sse41.dppd") |
| 226 | return UpgradeX86IntrinsicsWith8BitMask(F, Intrinsic::x86_sse41_dppd, |
| 227 | NewFn); |
| 228 | if (Name == "x86.sse41.dpps") |
| 229 | return UpgradeX86IntrinsicsWith8BitMask(F, Intrinsic::x86_sse41_dpps, |
| 230 | NewFn); |
| 231 | if (Name == "x86.sse41.mpsadbw") |
| 232 | return UpgradeX86IntrinsicsWith8BitMask(F, Intrinsic::x86_sse41_mpsadbw, |
| 233 | NewFn); |
Chandler Carruth | 373b2b1 | 2014-09-06 10:00:01 +0000 | [diff] [blame] | 234 | if (Name == "x86.avx.dp.ps.256") |
| 235 | return UpgradeX86IntrinsicsWith8BitMask(F, Intrinsic::x86_avx_dp_ps_256, |
| 236 | NewFn); |
Chandler Carruth | 373b2b1 | 2014-09-06 10:00:01 +0000 | [diff] [blame] | 237 | if (Name == "x86.avx2.mpsadbw") |
| 238 | return UpgradeX86IntrinsicsWith8BitMask(F, Intrinsic::x86_avx2_mpsadbw, |
| 239 | NewFn); |
Craig Topper | 29f2e95 | 2015-01-25 23:26:02 +0000 | [diff] [blame] | 240 | |
Craig Topper | 71dc02d | 2012-06-13 07:18:53 +0000 | [diff] [blame] | 241 | // frcz.ss/sd may need to have an argument dropped |
| 242 | if (Name.startswith("x86.xop.vfrcz.ss") && F->arg_size() == 2) { |
| 243 | F->setName(Name + ".old"); |
| 244 | NewFn = Intrinsic::getDeclaration(F->getParent(), |
| 245 | Intrinsic::x86_xop_vfrcz_ss); |
| 246 | return true; |
| 247 | } |
| 248 | if (Name.startswith("x86.xop.vfrcz.sd") && F->arg_size() == 2) { |
| 249 | F->setName(Name + ".old"); |
| 250 | NewFn = Intrinsic::getDeclaration(F->getParent(), |
| 251 | Intrinsic::x86_xop_vfrcz_sd); |
| 252 | return true; |
| 253 | } |
Craig Topper | 720c7bd | 2012-06-03 08:07:25 +0000 | [diff] [blame] | 254 | // Fix the FMA4 intrinsics to remove the 4 |
| 255 | if (Name.startswith("x86.fma4.")) { |
Craig Topper | 2c5ccd8 | 2012-06-03 16:48:52 +0000 | [diff] [blame] | 256 | F->setName("llvm.x86.fma" + Name.substr(8)); |
| 257 | NewFn = F; |
| 258 | return true; |
Craig Topper | 720c7bd | 2012-06-03 08:07:25 +0000 | [diff] [blame] | 259 | } |
Craig Topper | 3b1817d | 2012-02-03 06:10:55 +0000 | [diff] [blame] | 260 | break; |
| 261 | } |
Chris Lattner | b372f66 | 2011-06-18 18:56:39 +0000 | [diff] [blame] | 262 | } |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 263 | |
Nadav Rotem | 17ee58a | 2012-06-10 18:42:51 +0000 | [diff] [blame] | 264 | // This may not belong here. This function is effectively being overloaded |
| 265 | // to both detect an intrinsic which needs upgrading, and to provide the |
| 266 | // upgraded form of the intrinsic. We should perhaps have two separate |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 267 | // functions for this. |
Evan Cheng | 0e179d0 | 2007-12-17 22:33:23 +0000 | [diff] [blame] | 268 | return false; |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 269 | } |
| 270 | |
Evan Cheng | 0e179d0 | 2007-12-17 22:33:23 +0000 | [diff] [blame] | 271 | bool llvm::UpgradeIntrinsicFunction(Function *F, Function *&NewFn) { |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 272 | NewFn = nullptr; |
Evan Cheng | 0e179d0 | 2007-12-17 22:33:23 +0000 | [diff] [blame] | 273 | bool Upgraded = UpgradeIntrinsicFunction1(F, NewFn); |
Filipe Cabecinhas | 0011c58 | 2015-07-03 20:12:01 +0000 | [diff] [blame] | 274 | assert(F != NewFn && "Intrinsic function upgraded to the same function"); |
Duncan Sands | 38ef3a8 | 2007-12-03 20:06:50 +0000 | [diff] [blame] | 275 | |
| 276 | // Upgrade intrinsic attributes. This does not change the function. |
Evan Cheng | 0e179d0 | 2007-12-17 22:33:23 +0000 | [diff] [blame] | 277 | if (NewFn) |
| 278 | F = NewFn; |
Pete Cooper | 9e1d335 | 2015-05-20 17:16:39 +0000 | [diff] [blame] | 279 | if (Intrinsic::ID id = F->getIntrinsicID()) |
| 280 | F->setAttributes(Intrinsic::getAttributes(F->getContext(), id)); |
Duncan Sands | 38ef3a8 | 2007-12-03 20:06:50 +0000 | [diff] [blame] | 281 | return Upgraded; |
| 282 | } |
| 283 | |
Bill Wendling | e26fffc | 2010-09-10 18:51:56 +0000 | [diff] [blame] | 284 | bool llvm::UpgradeGlobalVariable(GlobalVariable *GV) { |
Chris Lattner | 80ed9dc | 2011-06-18 06:05:24 +0000 | [diff] [blame] | 285 | // Nothing to do yet. |
Bill Wendling | e26fffc | 2010-09-10 18:51:56 +0000 | [diff] [blame] | 286 | return false; |
| 287 | } |
| 288 | |
Craig Topper | b324e43 | 2015-02-18 06:24:44 +0000 | [diff] [blame] | 289 | // Handles upgrading SSE2 and AVX2 PSLLDQ intrinsics by converting them |
| 290 | // to byte shuffles. |
| 291 | static Value *UpgradeX86PSLLDQIntrinsics(IRBuilder<> &Builder, LLVMContext &C, |
Craig Topper | 7355ac3 | 2016-05-29 06:37:33 +0000 | [diff] [blame] | 292 | Value *Op, unsigned Shift) { |
| 293 | Type *ResultTy = Op->getType(); |
| 294 | unsigned NumElts = ResultTy->getVectorNumElements() * 8; |
Craig Topper | b324e43 | 2015-02-18 06:24:44 +0000 | [diff] [blame] | 295 | |
| 296 | // Bitcast from a 64-bit element type to a byte element type. |
Craig Topper | 7355ac3 | 2016-05-29 06:37:33 +0000 | [diff] [blame] | 297 | Type *VecTy = VectorType::get(Type::getInt8Ty(C), NumElts); |
| 298 | Op = Builder.CreateBitCast(Op, VecTy, "cast"); |
| 299 | |
Craig Topper | b324e43 | 2015-02-18 06:24:44 +0000 | [diff] [blame] | 300 | // We'll be shuffling in zeroes. |
Craig Topper | 7355ac3 | 2016-05-29 06:37:33 +0000 | [diff] [blame] | 301 | Value *Res = Constant::getNullValue(VecTy); |
Craig Topper | b324e43 | 2015-02-18 06:24:44 +0000 | [diff] [blame] | 302 | |
| 303 | // If shift is less than 16, emit a shuffle to move the bytes. Otherwise, |
| 304 | // we'll just return the zero vector. |
| 305 | if (Shift < 16) { |
Craig Topper | 7355ac3 | 2016-05-29 06:37:33 +0000 | [diff] [blame] | 306 | int Idxs[32]; |
Craig Topper | b324e43 | 2015-02-18 06:24:44 +0000 | [diff] [blame] | 307 | // 256-bit version is split into two 16-byte lanes. |
| 308 | for (unsigned l = 0; l != NumElts; l += 16) |
| 309 | for (unsigned i = 0; i != 16; ++i) { |
| 310 | unsigned Idx = NumElts + i - Shift; |
| 311 | if (Idx < NumElts) |
| 312 | Idx -= NumElts - 16; // end of lane, switch operand. |
Craig Topper | 7355ac3 | 2016-05-29 06:37:33 +0000 | [diff] [blame] | 313 | Idxs[l + i] = Idx + l; |
Craig Topper | b324e43 | 2015-02-18 06:24:44 +0000 | [diff] [blame] | 314 | } |
| 315 | |
Craig Topper | 7355ac3 | 2016-05-29 06:37:33 +0000 | [diff] [blame] | 316 | Res = Builder.CreateShuffleVector(Res, Op, makeArrayRef(Idxs, NumElts)); |
Craig Topper | b324e43 | 2015-02-18 06:24:44 +0000 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | // Bitcast back to a 64-bit element type. |
Craig Topper | 7355ac3 | 2016-05-29 06:37:33 +0000 | [diff] [blame] | 320 | return Builder.CreateBitCast(Res, ResultTy, "cast"); |
Craig Topper | b324e43 | 2015-02-18 06:24:44 +0000 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | // Handles upgrading SSE2 and AVX2 PSRLDQ intrinsics by converting them |
| 324 | // to byte shuffles. |
| 325 | static Value *UpgradeX86PSRLDQIntrinsics(IRBuilder<> &Builder, LLVMContext &C, |
Craig Topper | 7355ac3 | 2016-05-29 06:37:33 +0000 | [diff] [blame] | 326 | Value *Op, |
Craig Topper | b324e43 | 2015-02-18 06:24:44 +0000 | [diff] [blame] | 327 | unsigned Shift) { |
Craig Topper | 7355ac3 | 2016-05-29 06:37:33 +0000 | [diff] [blame] | 328 | Type *ResultTy = Op->getType(); |
| 329 | unsigned NumElts = ResultTy->getVectorNumElements() * 8; |
Craig Topper | b324e43 | 2015-02-18 06:24:44 +0000 | [diff] [blame] | 330 | |
| 331 | // Bitcast from a 64-bit element type to a byte element type. |
Craig Topper | 7355ac3 | 2016-05-29 06:37:33 +0000 | [diff] [blame] | 332 | Type *VecTy = VectorType::get(Type::getInt8Ty(C), NumElts); |
| 333 | Op = Builder.CreateBitCast(Op, VecTy, "cast"); |
| 334 | |
Craig Topper | b324e43 | 2015-02-18 06:24:44 +0000 | [diff] [blame] | 335 | // We'll be shuffling in zeroes. |
Craig Topper | 7355ac3 | 2016-05-29 06:37:33 +0000 | [diff] [blame] | 336 | Value *Res = Constant::getNullValue(VecTy); |
Craig Topper | b324e43 | 2015-02-18 06:24:44 +0000 | [diff] [blame] | 337 | |
| 338 | // If shift is less than 16, emit a shuffle to move the bytes. Otherwise, |
| 339 | // we'll just return the zero vector. |
| 340 | if (Shift < 16) { |
Craig Topper | 7355ac3 | 2016-05-29 06:37:33 +0000 | [diff] [blame] | 341 | int Idxs[32]; |
Craig Topper | b324e43 | 2015-02-18 06:24:44 +0000 | [diff] [blame] | 342 | // 256-bit version is split into two 16-byte lanes. |
| 343 | for (unsigned l = 0; l != NumElts; l += 16) |
| 344 | for (unsigned i = 0; i != 16; ++i) { |
| 345 | unsigned Idx = i + Shift; |
| 346 | if (Idx >= 16) |
| 347 | Idx += NumElts - 16; // end of lane, switch operand. |
Craig Topper | 7355ac3 | 2016-05-29 06:37:33 +0000 | [diff] [blame] | 348 | Idxs[l + i] = Idx + l; |
Craig Topper | b324e43 | 2015-02-18 06:24:44 +0000 | [diff] [blame] | 349 | } |
| 350 | |
Craig Topper | 7355ac3 | 2016-05-29 06:37:33 +0000 | [diff] [blame] | 351 | Res = Builder.CreateShuffleVector(Op, Res, makeArrayRef(Idxs, NumElts)); |
Craig Topper | b324e43 | 2015-02-18 06:24:44 +0000 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | // Bitcast back to a 64-bit element type. |
Craig Topper | 7355ac3 | 2016-05-29 06:37:33 +0000 | [diff] [blame] | 355 | return Builder.CreateBitCast(Res, ResultTy, "cast"); |
Craig Topper | b324e43 | 2015-02-18 06:24:44 +0000 | [diff] [blame] | 356 | } |
| 357 | |
Nadav Rotem | 17ee58a | 2012-06-10 18:42:51 +0000 | [diff] [blame] | 358 | // UpgradeIntrinsicCall - Upgrade a call to an old intrinsic to be a call the |
| 359 | // upgraded intrinsic. All argument and return casting must be provided in |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 360 | // order to seamlessly integrate with existing context. |
| 361 | void llvm::UpgradeIntrinsicCall(CallInst *CI, Function *NewFn) { |
Craig Topper | 3b1817d | 2012-02-03 06:10:55 +0000 | [diff] [blame] | 362 | Function *F = CI->getCalledFunction(); |
Nick Lewycky | 2eb3ade | 2011-12-12 22:59:34 +0000 | [diff] [blame] | 363 | LLVMContext &C = CI->getContext(); |
Chandler Carruth | 58a71ed | 2011-12-12 04:26:04 +0000 | [diff] [blame] | 364 | IRBuilder<> Builder(C); |
Duncan P. N. Exon Smith | 52888a6 | 2015-10-08 23:49:46 +0000 | [diff] [blame] | 365 | Builder.SetInsertPoint(CI->getParent(), CI->getIterator()); |
Chandler Carruth | 58a71ed | 2011-12-12 04:26:04 +0000 | [diff] [blame] | 366 | |
Craig Topper | 3b1817d | 2012-02-03 06:10:55 +0000 | [diff] [blame] | 367 | assert(F && "Intrinsic call is not direct?"); |
| 368 | |
| 369 | if (!NewFn) { |
| 370 | // Get the Function's name. |
| 371 | StringRef Name = F->getName(); |
| 372 | |
| 373 | Value *Rep; |
| 374 | // Upgrade packed integer vector compares intrinsics to compare instructions |
| 375 | if (Name.startswith("llvm.x86.sse2.pcmpeq.") || |
| 376 | Name.startswith("llvm.x86.avx2.pcmpeq.")) { |
| 377 | Rep = Builder.CreateICmpEQ(CI->getArgOperand(0), CI->getArgOperand(1), |
| 378 | "pcmpeq"); |
| 379 | // need to sign extend since icmp returns vector of i1 |
| 380 | Rep = Builder.CreateSExt(Rep, CI->getType(), ""); |
| 381 | } else if (Name.startswith("llvm.x86.sse2.pcmpgt.") || |
| 382 | Name.startswith("llvm.x86.avx2.pcmpgt.")) { |
| 383 | Rep = Builder.CreateICmpSGT(CI->getArgOperand(0), CI->getArgOperand(1), |
| 384 | "pcmpgt"); |
| 385 | // need to sign extend since icmp returns vector of i1 |
| 386 | Rep = Builder.CreateSExt(Rep, CI->getType(), ""); |
Simon Pilgrim | 4298d06 | 2016-05-25 08:59:18 +0000 | [diff] [blame] | 387 | } else if (Name == "llvm.x86.sse2.cvtdq2pd" || |
| 388 | Name == "llvm.x86.sse2.cvtps2pd" || |
| 389 | Name == "llvm.x86.avx.cvtdq2.pd.256" || |
| 390 | Name == "llvm.x86.avx.cvt.ps2.pd.256") { |
| 391 | // Lossless i32/float to double conversion. |
| 392 | // Extract the bottom elements if necessary and convert to double vector. |
| 393 | Value *Src = CI->getArgOperand(0); |
| 394 | VectorType *SrcTy = cast<VectorType>(Src->getType()); |
| 395 | VectorType *DstTy = cast<VectorType>(CI->getType()); |
| 396 | Rep = CI->getArgOperand(0); |
| 397 | |
| 398 | unsigned NumDstElts = DstTy->getNumElements(); |
| 399 | if (NumDstElts < SrcTy->getNumElements()) { |
| 400 | assert(NumDstElts == 2 && "Unexpected vector size"); |
| 401 | const int ShuffleMask[2] = { 0, 1 }; |
| 402 | Rep = Builder.CreateShuffleVector(Rep, UndefValue::get(SrcTy), ShuffleMask); |
| 403 | } |
| 404 | |
| 405 | bool Int2Double = (StringRef::npos != Name.find("cvtdq2")); |
| 406 | if (Int2Double) |
| 407 | Rep = Builder.CreateSIToFP(Rep, DstTy, "cvtdq2pd"); |
| 408 | else |
| 409 | Rep = Builder.CreateFPExt(Rep, DstTy, "cvtps2pd"); |
Craig Topper | f9f1ecc | 2016-05-30 22:54:05 +0000 | [diff] [blame] | 410 | } else if (Name.startswith("llvm.x86.avx.movnt.")) { |
Craig Topper | 7daf897 | 2012-05-08 06:58:15 +0000 | [diff] [blame] | 411 | Module *M = F->getParent(); |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 412 | SmallVector<Metadata *, 1> Elts; |
| 413 | Elts.push_back( |
| 414 | ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(C), 1))); |
Craig Topper | 7daf897 | 2012-05-08 06:58:15 +0000 | [diff] [blame] | 415 | MDNode *Node = MDNode::get(C, Elts); |
| 416 | |
| 417 | Value *Arg0 = CI->getArgOperand(0); |
| 418 | Value *Arg1 = CI->getArgOperand(1); |
| 419 | |
| 420 | // Convert the type of the pointer to a pointer to the stored type. |
| 421 | Value *BC = Builder.CreateBitCast(Arg0, |
| 422 | PointerType::getUnqual(Arg1->getType()), |
| 423 | "cast"); |
Craig Topper | 29ce55d | 2016-05-30 22:54:12 +0000 | [diff] [blame^] | 424 | StoreInst *SI = Builder.CreateAlignedStore(Arg1, BC, 32); |
Craig Topper | 7daf897 | 2012-05-08 06:58:15 +0000 | [diff] [blame] | 425 | SI->setMetadata(M->getMDKindID("nontemporal"), Node); |
Craig Topper | 7daf897 | 2012-05-08 06:58:15 +0000 | [diff] [blame] | 426 | |
| 427 | // Remove intrinsic. |
| 428 | CI->eraseFromParent(); |
| 429 | return; |
Craig Topper | 12e322a | 2016-05-25 06:56:32 +0000 | [diff] [blame] | 430 | } else if (Name == "llvm.x86.sse2.storel.dq") { |
Craig Topper | 12e322a | 2016-05-25 06:56:32 +0000 | [diff] [blame] | 431 | Value *Arg0 = CI->getArgOperand(0); |
| 432 | Value *Arg1 = CI->getArgOperand(1); |
| 433 | |
| 434 | Type *NewVecTy = VectorType::get(Type::getInt64Ty(C), 2); |
| 435 | Value *BC0 = Builder.CreateBitCast(Arg1, NewVecTy, "cast"); |
| 436 | Value *Elt = Builder.CreateExtractElement(BC0, (uint64_t)0); |
| 437 | Value *BC = Builder.CreateBitCast(Arg0, |
| 438 | PointerType::getUnqual(Elt->getType()), |
| 439 | "cast"); |
Craig Topper | 29ce55d | 2016-05-30 22:54:12 +0000 | [diff] [blame^] | 440 | Builder.CreateAlignedStore(Elt, BC, 1); |
Craig Topper | 12e322a | 2016-05-25 06:56:32 +0000 | [diff] [blame] | 441 | |
| 442 | // Remove intrinsic. |
| 443 | CI->eraseFromParent(); |
| 444 | return; |
Craig Topper | 3352ba5 | 2012-06-09 16:46:13 +0000 | [diff] [blame] | 445 | } else if (Name.startswith("llvm.x86.xop.vpcom")) { |
| 446 | Intrinsic::ID intID; |
| 447 | if (Name.endswith("ub")) |
| 448 | intID = Intrinsic::x86_xop_vpcomub; |
| 449 | else if (Name.endswith("uw")) |
| 450 | intID = Intrinsic::x86_xop_vpcomuw; |
| 451 | else if (Name.endswith("ud")) |
| 452 | intID = Intrinsic::x86_xop_vpcomud; |
| 453 | else if (Name.endswith("uq")) |
| 454 | intID = Intrinsic::x86_xop_vpcomuq; |
| 455 | else if (Name.endswith("b")) |
| 456 | intID = Intrinsic::x86_xop_vpcomb; |
| 457 | else if (Name.endswith("w")) |
| 458 | intID = Intrinsic::x86_xop_vpcomw; |
| 459 | else if (Name.endswith("d")) |
| 460 | intID = Intrinsic::x86_xop_vpcomd; |
| 461 | else if (Name.endswith("q")) |
| 462 | intID = Intrinsic::x86_xop_vpcomq; |
| 463 | else |
| 464 | llvm_unreachable("Unknown suffix"); |
| 465 | |
| 466 | Name = Name.substr(18); // strip off "llvm.x86.xop.vpcom" |
| 467 | unsigned Imm; |
| 468 | if (Name.startswith("lt")) |
| 469 | Imm = 0; |
| 470 | else if (Name.startswith("le")) |
| 471 | Imm = 1; |
| 472 | else if (Name.startswith("gt")) |
| 473 | Imm = 2; |
| 474 | else if (Name.startswith("ge")) |
| 475 | Imm = 3; |
| 476 | else if (Name.startswith("eq")) |
| 477 | Imm = 4; |
| 478 | else if (Name.startswith("ne")) |
| 479 | Imm = 5; |
Craig Topper | 3352ba5 | 2012-06-09 16:46:13 +0000 | [diff] [blame] | 480 | else if (Name.startswith("false")) |
Craig Topper | e32546d | 2015-02-13 07:42:15 +0000 | [diff] [blame] | 481 | Imm = 6; |
| 482 | else if (Name.startswith("true")) |
Craig Topper | 3352ba5 | 2012-06-09 16:46:13 +0000 | [diff] [blame] | 483 | Imm = 7; |
| 484 | else |
| 485 | llvm_unreachable("Unknown condition"); |
| 486 | |
| 487 | Function *VPCOM = Intrinsic::getDeclaration(F->getParent(), intID); |
David Blaikie | ff6409d | 2015-05-18 22:13:54 +0000 | [diff] [blame] | 488 | Rep = |
| 489 | Builder.CreateCall(VPCOM, {CI->getArgOperand(0), CI->getArgOperand(1), |
| 490 | Builder.getInt8(Imm)}); |
Simon Pilgrim | e88dc04 | 2015-11-03 20:27:01 +0000 | [diff] [blame] | 491 | } else if (Name == "llvm.x86.xop.vpcmov") { |
| 492 | Value *Arg0 = CI->getArgOperand(0); |
| 493 | Value *Arg1 = CI->getArgOperand(1); |
| 494 | Value *Sel = CI->getArgOperand(2); |
| 495 | unsigned NumElts = CI->getType()->getVectorNumElements(); |
| 496 | Constant *MinusOne = ConstantVector::getSplat(NumElts, Builder.getInt64(-1)); |
| 497 | Value *NotSel = Builder.CreateXor(Sel, MinusOne); |
| 498 | Value *Sel0 = Builder.CreateAnd(Arg0, Sel); |
| 499 | Value *Sel1 = Builder.CreateAnd(Arg1, NotSel); |
| 500 | Rep = Builder.CreateOr(Sel0, Sel1); |
Craig Topper | ef9e993 | 2013-10-15 05:20:47 +0000 | [diff] [blame] | 501 | } else if (Name == "llvm.x86.sse42.crc32.64.8") { |
| 502 | Function *CRC32 = Intrinsic::getDeclaration(F->getParent(), |
| 503 | Intrinsic::x86_sse42_crc32_32_8); |
| 504 | Value *Trunc0 = Builder.CreateTrunc(CI->getArgOperand(0), Type::getInt32Ty(C)); |
David Blaikie | ff6409d | 2015-05-18 22:13:54 +0000 | [diff] [blame] | 505 | Rep = Builder.CreateCall(CRC32, {Trunc0, CI->getArgOperand(1)}); |
Craig Topper | ef9e993 | 2013-10-15 05:20:47 +0000 | [diff] [blame] | 506 | Rep = Builder.CreateZExt(Rep, CI->getType(), ""); |
Adam Nemet | 3906680 | 2014-05-29 23:35:33 +0000 | [diff] [blame] | 507 | } else if (Name.startswith("llvm.x86.avx.vbroadcast")) { |
| 508 | // Replace broadcasts with a series of insertelements. |
| 509 | Type *VecTy = CI->getType(); |
| 510 | Type *EltTy = VecTy->getVectorElementType(); |
| 511 | unsigned EltNum = VecTy->getVectorNumElements(); |
| 512 | Value *Cast = Builder.CreateBitCast(CI->getArgOperand(0), |
| 513 | EltTy->getPointerTo()); |
David Blaikie | 0c28fd7 | 2015-05-20 21:46:30 +0000 | [diff] [blame] | 514 | Value *Load = Builder.CreateLoad(EltTy, Cast); |
Adam Nemet | 3906680 | 2014-05-29 23:35:33 +0000 | [diff] [blame] | 515 | Type *I32Ty = Type::getInt32Ty(C); |
| 516 | Rep = UndefValue::get(VecTy); |
| 517 | for (unsigned I = 0; I < EltNum; ++I) |
| 518 | Rep = Builder.CreateInsertElement(Rep, Load, |
| 519 | ConstantInt::get(I32Ty, I)); |
Simon Pilgrim | 9602d67 | 2016-05-28 18:03:41 +0000 | [diff] [blame] | 520 | } else if (Name.startswith("llvm.x86.sse41.pmovsx") || |
| 521 | Name.startswith("llvm.x86.sse41.pmovzx") || |
| 522 | Name.startswith("llvm.x86.avx2.pmovsx") || |
| 523 | Name.startswith("llvm.x86.avx2.pmovzx")) { |
Simon Pilgrim | 9cb018b | 2015-09-23 08:48:33 +0000 | [diff] [blame] | 524 | VectorType *SrcTy = cast<VectorType>(CI->getArgOperand(0)->getType()); |
| 525 | VectorType *DstTy = cast<VectorType>(CI->getType()); |
| 526 | unsigned NumDstElts = DstTy->getNumElements(); |
| 527 | |
Simon Pilgrim | 9602d67 | 2016-05-28 18:03:41 +0000 | [diff] [blame] | 528 | // Extract a subvector of the first NumDstElts lanes and sign/zero extend. |
Simon Pilgrim | 9cb018b | 2015-09-23 08:48:33 +0000 | [diff] [blame] | 529 | SmallVector<int, 8> ShuffleMask; |
| 530 | for (int i = 0; i != (int)NumDstElts; ++i) |
| 531 | ShuffleMask.push_back(i); |
| 532 | |
| 533 | Value *SV = Builder.CreateShuffleVector( |
| 534 | CI->getArgOperand(0), UndefValue::get(SrcTy), ShuffleMask); |
Simon Pilgrim | 9602d67 | 2016-05-28 18:03:41 +0000 | [diff] [blame] | 535 | |
| 536 | bool DoSext = (StringRef::npos != Name.find("pmovsx")); |
| 537 | Rep = DoSext ? Builder.CreateSExt(SV, DstTy) |
| 538 | : Builder.CreateZExt(SV, DstTy); |
Juergen Ributzka | 1f7a176 | 2015-03-04 00:13:25 +0000 | [diff] [blame] | 539 | } else if (Name == "llvm.x86.avx2.vbroadcasti128") { |
| 540 | // Replace vbroadcasts with a vector shuffle. |
David Blaikie | 0c28fd7 | 2015-05-20 21:46:30 +0000 | [diff] [blame] | 541 | Type *VT = VectorType::get(Type::getInt64Ty(C), 2); |
| 542 | Value *Op = Builder.CreatePointerCast(CI->getArgOperand(0), |
| 543 | PointerType::getUnqual(VT)); |
| 544 | Value *Load = Builder.CreateLoad(VT, Op); |
Sanjay Patel | 7b079fb | 2015-03-12 16:29:58 +0000 | [diff] [blame] | 545 | const int Idxs[4] = { 0, 1, 0, 1 }; |
Juergen Ributzka | 1f7a176 | 2015-03-04 00:13:25 +0000 | [diff] [blame] | 546 | Rep = Builder.CreateShuffleVector(Load, UndefValue::get(Load->getType()), |
Sanjay Patel | 2db6d38 | 2015-03-12 15:27:07 +0000 | [diff] [blame] | 547 | Idxs); |
Ahmed Bougacha | 1a498705 | 2015-08-20 20:36:19 +0000 | [diff] [blame] | 548 | } else if (Name.startswith("llvm.x86.avx2.pbroadcast") || |
| 549 | Name.startswith("llvm.x86.avx2.vbroadcast")) { |
| 550 | // Replace vp?broadcasts with a vector shuffle. |
| 551 | Value *Op = CI->getArgOperand(0); |
| 552 | unsigned NumElts = CI->getType()->getVectorNumElements(); |
| 553 | Type *MaskTy = VectorType::get(Type::getInt32Ty(C), NumElts); |
| 554 | Rep = Builder.CreateShuffleVector(Op, UndefValue::get(Op->getType()), |
| 555 | Constant::getNullValue(MaskTy)); |
Craig Topper | 7355ac3 | 2016-05-29 06:37:33 +0000 | [diff] [blame] | 556 | } else if (Name == "llvm.x86.sse2.psll.dq" || |
| 557 | Name == "llvm.x86.avx2.psll.dq") { |
| 558 | // 128/256-bit shift left specified in bits. |
Craig Topper | b324e43 | 2015-02-18 06:24:44 +0000 | [diff] [blame] | 559 | unsigned Shift = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue(); |
Craig Topper | 7355ac3 | 2016-05-29 06:37:33 +0000 | [diff] [blame] | 560 | Rep = UpgradeX86PSLLDQIntrinsics(Builder, C, CI->getArgOperand(0), |
Craig Topper | b324e43 | 2015-02-18 06:24:44 +0000 | [diff] [blame] | 561 | Shift / 8); // Shift is in bits. |
Craig Topper | 7355ac3 | 2016-05-29 06:37:33 +0000 | [diff] [blame] | 562 | } else if (Name == "llvm.x86.sse2.psrl.dq" || |
| 563 | Name == "llvm.x86.avx2.psrl.dq") { |
| 564 | // 128/256-bit shift right specified in bits. |
Craig Topper | b324e43 | 2015-02-18 06:24:44 +0000 | [diff] [blame] | 565 | unsigned Shift = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue(); |
Craig Topper | 7355ac3 | 2016-05-29 06:37:33 +0000 | [diff] [blame] | 566 | Rep = UpgradeX86PSRLDQIntrinsics(Builder, C, CI->getArgOperand(0), |
Craig Topper | b324e43 | 2015-02-18 06:24:44 +0000 | [diff] [blame] | 567 | Shift / 8); // Shift is in bits. |
Craig Topper | 7355ac3 | 2016-05-29 06:37:33 +0000 | [diff] [blame] | 568 | } else if (Name == "llvm.x86.sse2.psll.dq.bs" || |
| 569 | Name == "llvm.x86.avx2.psll.dq.bs") { |
| 570 | // 128/256-bit shift left specified in bytes. |
Craig Topper | b324e43 | 2015-02-18 06:24:44 +0000 | [diff] [blame] | 571 | unsigned Shift = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue(); |
Craig Topper | 7355ac3 | 2016-05-29 06:37:33 +0000 | [diff] [blame] | 572 | Rep = UpgradeX86PSLLDQIntrinsics(Builder, C, CI->getArgOperand(0), Shift); |
| 573 | } else if (Name == "llvm.x86.sse2.psrl.dq.bs" || |
| 574 | Name == "llvm.x86.avx2.psrl.dq.bs") { |
| 575 | // 128/256-bit shift right specified in bytes. |
Craig Topper | b324e43 | 2015-02-18 06:24:44 +0000 | [diff] [blame] | 576 | unsigned Shift = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue(); |
Craig Topper | 7355ac3 | 2016-05-29 06:37:33 +0000 | [diff] [blame] | 577 | Rep = UpgradeX86PSRLDQIntrinsics(Builder, C, CI->getArgOperand(0), Shift); |
Craig Topper | 782d620 | 2015-02-28 19:33:17 +0000 | [diff] [blame] | 578 | } else if (Name == "llvm.x86.sse41.pblendw" || |
Craig Topper | f9f1ecc | 2016-05-30 22:54:05 +0000 | [diff] [blame] | 579 | Name.startswith("llvm.x86.sse41.blendp") || |
| 580 | Name.startswith("llvm.x86.avx.blend.p") || |
Craig Topper | 782d620 | 2015-02-28 19:33:17 +0000 | [diff] [blame] | 581 | Name == "llvm.x86.avx2.pblendw" || |
Craig Topper | f9f1ecc | 2016-05-30 22:54:05 +0000 | [diff] [blame] | 582 | Name.startswith("llvm.x86.avx2.pblendd.")) { |
Craig Topper | 782d620 | 2015-02-28 19:33:17 +0000 | [diff] [blame] | 583 | Value *Op0 = CI->getArgOperand(0); |
| 584 | Value *Op1 = CI->getArgOperand(1); |
| 585 | unsigned Imm = cast <ConstantInt>(CI->getArgOperand(2))->getZExtValue(); |
| 586 | VectorType *VecTy = cast<VectorType>(CI->getType()); |
| 587 | unsigned NumElts = VecTy->getNumElements(); |
| 588 | |
| 589 | SmallVector<Constant*, 16> Idxs; |
| 590 | for (unsigned i = 0; i != NumElts; ++i) { |
| 591 | unsigned Idx = ((Imm >> (i%8)) & 1) ? i + NumElts : i; |
| 592 | Idxs.push_back(Builder.getInt32(Idx)); |
| 593 | } |
| 594 | |
| 595 | Rep = Builder.CreateShuffleVector(Op0, Op1, ConstantVector::get(Idxs)); |
Craig Topper | f9f1ecc | 2016-05-30 22:54:05 +0000 | [diff] [blame] | 596 | } else if (Name.startswith("llvm.x86.avx.vinsertf128.") || |
Sanjay Patel | 4339abe | 2015-03-12 23:16:18 +0000 | [diff] [blame] | 597 | Name == "llvm.x86.avx2.vinserti128") { |
Sanjay Patel | 19792fb | 2015-03-10 16:08:36 +0000 | [diff] [blame] | 598 | Value *Op0 = CI->getArgOperand(0); |
| 599 | Value *Op1 = CI->getArgOperand(1); |
| 600 | unsigned Imm = cast<ConstantInt>(CI->getArgOperand(2))->getZExtValue(); |
| 601 | VectorType *VecTy = cast<VectorType>(CI->getType()); |
| 602 | unsigned NumElts = VecTy->getNumElements(); |
Simon Pilgrim | 9cb018b | 2015-09-23 08:48:33 +0000 | [diff] [blame] | 603 | |
Sanjay Patel | 19792fb | 2015-03-10 16:08:36 +0000 | [diff] [blame] | 604 | // Mask off the high bits of the immediate value; hardware ignores those. |
| 605 | Imm = Imm & 1; |
Simon Pilgrim | 9cb018b | 2015-09-23 08:48:33 +0000 | [diff] [blame] | 606 | |
Sanjay Patel | 19792fb | 2015-03-10 16:08:36 +0000 | [diff] [blame] | 607 | // Extend the second operand into a vector that is twice as big. |
| 608 | Value *UndefV = UndefValue::get(Op1->getType()); |
| 609 | SmallVector<Constant*, 8> Idxs; |
| 610 | for (unsigned i = 0; i != NumElts; ++i) { |
| 611 | Idxs.push_back(Builder.getInt32(i)); |
| 612 | } |
| 613 | Rep = Builder.CreateShuffleVector(Op1, UndefV, ConstantVector::get(Idxs)); |
| 614 | |
| 615 | // Insert the second operand into the first operand. |
| 616 | |
| 617 | // Note that there is no guarantee that instruction lowering will actually |
| 618 | // produce a vinsertf128 instruction for the created shuffles. In |
| 619 | // particular, the 0 immediate case involves no lane changes, so it can |
| 620 | // be handled as a blend. |
| 621 | |
| 622 | // Example of shuffle mask for 32-bit elements: |
| 623 | // Imm = 1 <i32 0, i32 1, i32 2, i32 3, i32 8, i32 9, i32 10, i32 11> |
| 624 | // Imm = 0 <i32 8, i32 9, i32 10, i32 11, i32 4, i32 5, i32 6, i32 7 > |
| 625 | |
| 626 | SmallVector<Constant*, 8> Idxs2; |
| 627 | // The low half of the result is either the low half of the 1st operand |
| 628 | // or the low half of the 2nd operand (the inserted vector). |
| 629 | for (unsigned i = 0; i != NumElts / 2; ++i) { |
| 630 | unsigned Idx = Imm ? i : (i + NumElts); |
| 631 | Idxs2.push_back(Builder.getInt32(Idx)); |
| 632 | } |
| 633 | // The high half of the result is either the low half of the 2nd operand |
| 634 | // (the inserted vector) or the high half of the 1st operand. |
| 635 | for (unsigned i = NumElts / 2; i != NumElts; ++i) { |
| 636 | unsigned Idx = Imm ? (i + NumElts / 2) : i; |
| 637 | Idxs2.push_back(Builder.getInt32(Idx)); |
| 638 | } |
| 639 | Rep = Builder.CreateShuffleVector(Op0, Rep, ConstantVector::get(Idxs2)); |
Craig Topper | f9f1ecc | 2016-05-30 22:54:05 +0000 | [diff] [blame] | 640 | } else if (Name.startswith("llvm.x86.avx.vextractf128.") || |
Sanjay Patel | 4339abe | 2015-03-12 23:16:18 +0000 | [diff] [blame] | 641 | Name == "llvm.x86.avx2.vextracti128") { |
Sanjay Patel | af1846c | 2015-03-12 15:15:19 +0000 | [diff] [blame] | 642 | Value *Op0 = CI->getArgOperand(0); |
| 643 | unsigned Imm = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue(); |
| 644 | VectorType *VecTy = cast<VectorType>(CI->getType()); |
| 645 | unsigned NumElts = VecTy->getNumElements(); |
Simon Pilgrim | 9cb018b | 2015-09-23 08:48:33 +0000 | [diff] [blame] | 646 | |
Sanjay Patel | af1846c | 2015-03-12 15:15:19 +0000 | [diff] [blame] | 647 | // Mask off the high bits of the immediate value; hardware ignores those. |
| 648 | Imm = Imm & 1; |
| 649 | |
| 650 | // Get indexes for either the high half or low half of the input vector. |
| 651 | SmallVector<Constant*, 4> Idxs(NumElts); |
| 652 | for (unsigned i = 0; i != NumElts; ++i) { |
| 653 | unsigned Idx = Imm ? (i + NumElts) : i; |
| 654 | Idxs[i] = Builder.getInt32(Idx); |
| 655 | } |
| 656 | |
| 657 | Value *UndefV = UndefValue::get(Op0->getType()); |
| 658 | Rep = Builder.CreateShuffleVector(Op0, UndefV, ConstantVector::get(Idxs)); |
Tim Shen | 0012756 | 2016-04-08 21:26:31 +0000 | [diff] [blame] | 659 | } else if (Name == "llvm.stackprotectorcheck") { |
| 660 | Rep = nullptr; |
Craig Topper | 3b1817d | 2012-02-03 06:10:55 +0000 | [diff] [blame] | 661 | } else { |
Craig Topper | d3c9e40 | 2012-04-18 05:24:00 +0000 | [diff] [blame] | 662 | bool PD128 = false, PD256 = false, PS128 = false, PS256 = false; |
Craig Topper | 7daf897 | 2012-05-08 06:58:15 +0000 | [diff] [blame] | 663 | if (Name == "llvm.x86.avx.vpermil.pd.256") |
Craig Topper | d3c9e40 | 2012-04-18 05:24:00 +0000 | [diff] [blame] | 664 | PD256 = true; |
Craig Topper | 7daf897 | 2012-05-08 06:58:15 +0000 | [diff] [blame] | 665 | else if (Name == "llvm.x86.avx.vpermil.pd") |
Craig Topper | d3c9e40 | 2012-04-18 05:24:00 +0000 | [diff] [blame] | 666 | PD128 = true; |
Craig Topper | 7daf897 | 2012-05-08 06:58:15 +0000 | [diff] [blame] | 667 | else if (Name == "llvm.x86.avx.vpermil.ps.256") |
Craig Topper | d3c9e40 | 2012-04-18 05:24:00 +0000 | [diff] [blame] | 668 | PS256 = true; |
Craig Topper | 7daf897 | 2012-05-08 06:58:15 +0000 | [diff] [blame] | 669 | else if (Name == "llvm.x86.avx.vpermil.ps") |
Craig Topper | d3c9e40 | 2012-04-18 05:24:00 +0000 | [diff] [blame] | 670 | PS128 = true; |
| 671 | |
| 672 | if (PD256 || PD128 || PS256 || PS128) { |
| 673 | Value *Op0 = CI->getArgOperand(0); |
| 674 | unsigned Imm = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue(); |
| 675 | SmallVector<Constant*, 8> Idxs; |
| 676 | |
| 677 | if (PD128) |
| 678 | for (unsigned i = 0; i != 2; ++i) |
| 679 | Idxs.push_back(Builder.getInt32((Imm >> i) & 0x1)); |
| 680 | else if (PD256) |
| 681 | for (unsigned l = 0; l != 4; l+=2) |
| 682 | for (unsigned i = 0; i != 2; ++i) |
| 683 | Idxs.push_back(Builder.getInt32(((Imm >> (l+i)) & 0x1) + l)); |
| 684 | else if (PS128) |
| 685 | for (unsigned i = 0; i != 4; ++i) |
| 686 | Idxs.push_back(Builder.getInt32((Imm >> (2 * i)) & 0x3)); |
| 687 | else if (PS256) |
| 688 | for (unsigned l = 0; l != 8; l+=4) |
| 689 | for (unsigned i = 0; i != 4; ++i) |
| 690 | Idxs.push_back(Builder.getInt32(((Imm >> (2 * i)) & 0x3) + l)); |
| 691 | else |
| 692 | llvm_unreachable("Unexpected function"); |
| 693 | |
| 694 | Rep = Builder.CreateShuffleVector(Op0, Op0, ConstantVector::get(Idxs)); |
| 695 | } else { |
| 696 | llvm_unreachable("Unknown function for CallInst upgrade."); |
| 697 | } |
Craig Topper | 3b1817d | 2012-02-03 06:10:55 +0000 | [diff] [blame] | 698 | } |
| 699 | |
Tim Shen | 0012756 | 2016-04-08 21:26:31 +0000 | [diff] [blame] | 700 | if (Rep) |
| 701 | CI->replaceAllUsesWith(Rep); |
Craig Topper | 3b1817d | 2012-02-03 06:10:55 +0000 | [diff] [blame] | 702 | CI->eraseFromParent(); |
| 703 | return; |
| 704 | } |
| 705 | |
Yaron Keren | d1fdbe7 | 2015-03-30 16:10:39 +0000 | [diff] [blame] | 706 | std::string Name = CI->getName(); |
Adrian Prantl | 87b7eb9 | 2014-10-01 18:55:02 +0000 | [diff] [blame] | 707 | if (!Name.empty()) |
| 708 | CI->setName(Name + ".old"); |
Nadav Rotem | 17ee58a | 2012-06-10 18:42:51 +0000 | [diff] [blame] | 709 | |
Chandler Carruth | 58a71ed | 2011-12-12 04:26:04 +0000 | [diff] [blame] | 710 | switch (NewFn->getIntrinsicID()) { |
| 711 | default: |
Chris Lattner | 0bcbde4 | 2011-11-27 08:42:07 +0000 | [diff] [blame] | 712 | llvm_unreachable("Unknown function for CallInst upgrade."); |
Chandler Carruth | 58a71ed | 2011-12-12 04:26:04 +0000 | [diff] [blame] | 713 | |
Jeroen Ketema | ab99b59 | 2015-09-30 10:56:37 +0000 | [diff] [blame] | 714 | case Intrinsic::arm_neon_vld1: |
| 715 | case Intrinsic::arm_neon_vld2: |
| 716 | case Intrinsic::arm_neon_vld3: |
| 717 | case Intrinsic::arm_neon_vld4: |
| 718 | case Intrinsic::arm_neon_vld2lane: |
| 719 | case Intrinsic::arm_neon_vld3lane: |
| 720 | case Intrinsic::arm_neon_vld4lane: |
| 721 | case Intrinsic::arm_neon_vst1: |
| 722 | case Intrinsic::arm_neon_vst2: |
| 723 | case Intrinsic::arm_neon_vst3: |
| 724 | case Intrinsic::arm_neon_vst4: |
| 725 | case Intrinsic::arm_neon_vst2lane: |
| 726 | case Intrinsic::arm_neon_vst3lane: |
| 727 | case Intrinsic::arm_neon_vst4lane: { |
| 728 | SmallVector<Value *, 4> Args(CI->arg_operands().begin(), |
| 729 | CI->arg_operands().end()); |
| 730 | CI->replaceAllUsesWith(Builder.CreateCall(NewFn, Args)); |
| 731 | CI->eraseFromParent(); |
| 732 | return; |
| 733 | } |
| 734 | |
Chandler Carruth | 58a71ed | 2011-12-12 04:26:04 +0000 | [diff] [blame] | 735 | case Intrinsic::ctlz: |
Nuno Lopes | ad40c0a | 2012-05-22 15:25:31 +0000 | [diff] [blame] | 736 | case Intrinsic::cttz: |
Chandler Carruth | 58a71ed | 2011-12-12 04:26:04 +0000 | [diff] [blame] | 737 | assert(CI->getNumArgOperands() == 1 && |
| 738 | "Mismatch between function args and call args"); |
David Blaikie | ff6409d | 2015-05-18 22:13:54 +0000 | [diff] [blame] | 739 | CI->replaceAllUsesWith(Builder.CreateCall( |
| 740 | NewFn, {CI->getArgOperand(0), Builder.getFalse()}, Name)); |
Chandler Carruth | 58a71ed | 2011-12-12 04:26:04 +0000 | [diff] [blame] | 741 | CI->eraseFromParent(); |
| 742 | return; |
Nadav Rotem | 17ee58a | 2012-06-10 18:42:51 +0000 | [diff] [blame] | 743 | |
Matt Arsenault | fbcbce4 | 2013-10-07 18:06:48 +0000 | [diff] [blame] | 744 | case Intrinsic::objectsize: |
David Blaikie | ff6409d | 2015-05-18 22:13:54 +0000 | [diff] [blame] | 745 | CI->replaceAllUsesWith(Builder.CreateCall( |
| 746 | NewFn, {CI->getArgOperand(0), CI->getArgOperand(1)}, Name)); |
Matt Arsenault | fbcbce4 | 2013-10-07 18:06:48 +0000 | [diff] [blame] | 747 | CI->eraseFromParent(); |
| 748 | return; |
| 749 | |
Joel Jones | b84f7be | 2012-07-18 00:02:16 +0000 | [diff] [blame] | 750 | case Intrinsic::ctpop: { |
David Blaikie | ff6409d | 2015-05-18 22:13:54 +0000 | [diff] [blame] | 751 | CI->replaceAllUsesWith(Builder.CreateCall(NewFn, {CI->getArgOperand(0)})); |
Joel Jones | b84f7be | 2012-07-18 00:02:16 +0000 | [diff] [blame] | 752 | CI->eraseFromParent(); |
| 753 | return; |
| 754 | } |
Joel Jones | 43cb878 | 2012-07-13 23:25:25 +0000 | [diff] [blame] | 755 | |
Craig Topper | 71dc02d | 2012-06-13 07:18:53 +0000 | [diff] [blame] | 756 | case Intrinsic::x86_xop_vfrcz_ss: |
| 757 | case Intrinsic::x86_xop_vfrcz_sd: |
David Blaikie | ff6409d | 2015-05-18 22:13:54 +0000 | [diff] [blame] | 758 | CI->replaceAllUsesWith( |
| 759 | Builder.CreateCall(NewFn, {CI->getArgOperand(1)}, Name)); |
Craig Topper | 71dc02d | 2012-06-13 07:18:53 +0000 | [diff] [blame] | 760 | CI->eraseFromParent(); |
| 761 | return; |
| 762 | |
Nadav Rotem | 17ee58a | 2012-06-10 18:42:51 +0000 | [diff] [blame] | 763 | case Intrinsic::x86_sse41_ptestc: |
| 764 | case Intrinsic::x86_sse41_ptestz: |
Craig Topper | 71dc02d | 2012-06-13 07:18:53 +0000 | [diff] [blame] | 765 | case Intrinsic::x86_sse41_ptestnzc: { |
Nadav Rotem | 17ee58a | 2012-06-10 18:42:51 +0000 | [diff] [blame] | 766 | // The arguments for these intrinsics used to be v4f32, and changed |
| 767 | // to v2i64. This is purely a nop, since those are bitwise intrinsics. |
| 768 | // So, the only thing required is a bitcast for both arguments. |
| 769 | // First, check the arguments have the old type. |
| 770 | Value *Arg0 = CI->getArgOperand(0); |
| 771 | if (Arg0->getType() != VectorType::get(Type::getFloatTy(C), 4)) |
| 772 | return; |
| 773 | |
| 774 | // Old intrinsic, add bitcasts |
| 775 | Value *Arg1 = CI->getArgOperand(1); |
| 776 | |
David Blaikie | 5bacf37 | 2015-04-24 21:16:07 +0000 | [diff] [blame] | 777 | Type *NewVecTy = VectorType::get(Type::getInt64Ty(C), 2); |
Nadav Rotem | 17ee58a | 2012-06-10 18:42:51 +0000 | [diff] [blame] | 778 | |
David Blaikie | 5bacf37 | 2015-04-24 21:16:07 +0000 | [diff] [blame] | 779 | Value *BC0 = Builder.CreateBitCast(Arg0, NewVecTy, "cast"); |
| 780 | Value *BC1 = Builder.CreateBitCast(Arg1, NewVecTy, "cast"); |
| 781 | |
David Blaikie | ff6409d | 2015-05-18 22:13:54 +0000 | [diff] [blame] | 782 | CallInst *NewCall = Builder.CreateCall(NewFn, {BC0, BC1}, Name); |
Nadav Rotem | 17ee58a | 2012-06-10 18:42:51 +0000 | [diff] [blame] | 783 | CI->replaceAllUsesWith(NewCall); |
| 784 | CI->eraseFromParent(); |
| 785 | return; |
Evan Cheng | 0e179d0 | 2007-12-17 22:33:23 +0000 | [diff] [blame] | 786 | } |
Chandler Carruth | 373b2b1 | 2014-09-06 10:00:01 +0000 | [diff] [blame] | 787 | |
Chandler Carruth | 373b2b1 | 2014-09-06 10:00:01 +0000 | [diff] [blame] | 788 | case Intrinsic::x86_sse41_insertps: |
| 789 | case Intrinsic::x86_sse41_dppd: |
| 790 | case Intrinsic::x86_sse41_dpps: |
| 791 | case Intrinsic::x86_sse41_mpsadbw: |
Chandler Carruth | 373b2b1 | 2014-09-06 10:00:01 +0000 | [diff] [blame] | 792 | case Intrinsic::x86_avx_dp_ps_256: |
Chandler Carruth | 373b2b1 | 2014-09-06 10:00:01 +0000 | [diff] [blame] | 793 | case Intrinsic::x86_avx2_mpsadbw: { |
| 794 | // Need to truncate the last argument from i32 to i8 -- this argument models |
| 795 | // an inherently 8-bit immediate operand to these x86 instructions. |
| 796 | SmallVector<Value *, 4> Args(CI->arg_operands().begin(), |
| 797 | CI->arg_operands().end()); |
| 798 | |
| 799 | // Replace the last argument with a trunc. |
| 800 | Args.back() = Builder.CreateTrunc(Args.back(), Type::getInt8Ty(C), "trunc"); |
| 801 | |
| 802 | CallInst *NewCall = Builder.CreateCall(NewFn, Args); |
| 803 | CI->replaceAllUsesWith(NewCall); |
| 804 | CI->eraseFromParent(); |
| 805 | return; |
| 806 | } |
Marcin Koscielnicki | 3fdc257 | 2016-04-19 20:51:05 +0000 | [diff] [blame] | 807 | |
| 808 | case Intrinsic::thread_pointer: { |
| 809 | CI->replaceAllUsesWith(Builder.CreateCall(NewFn, {})); |
| 810 | CI->eraseFromParent(); |
| 811 | return; |
| 812 | } |
Craig Topper | 71dc02d | 2012-06-13 07:18:53 +0000 | [diff] [blame] | 813 | } |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 814 | } |
| 815 | |
Sanjay Patel | fdf0d5f | 2016-04-18 19:11:57 +0000 | [diff] [blame] | 816 | void llvm::UpgradeCallsToIntrinsic(Function *F) { |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 817 | assert(F && "Illegal attempt to upgrade a non-existent intrinsic."); |
| 818 | |
Sanjay Patel | fdf0d5f | 2016-04-18 19:11:57 +0000 | [diff] [blame] | 819 | // Check if this function should be upgraded and get the replacement function |
| 820 | // if there is one. |
Chris Lattner | 80ed9dc | 2011-06-18 06:05:24 +0000 | [diff] [blame] | 821 | Function *NewFn; |
Evan Cheng | 0e179d0 | 2007-12-17 22:33:23 +0000 | [diff] [blame] | 822 | if (UpgradeIntrinsicFunction(F, NewFn)) { |
Sanjay Patel | fdf0d5f | 2016-04-18 19:11:57 +0000 | [diff] [blame] | 823 | // Replace all users of the old function with the new function or new |
| 824 | // instructions. This is not a range loop because the call is deleted. |
| 825 | 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] | 826 | if (CallInst *CI = dyn_cast<CallInst>(*UI++)) |
Filipe Cabecinhas | 0011c58 | 2015-07-03 20:12:01 +0000 | [diff] [blame] | 827 | UpgradeIntrinsicCall(CI, NewFn); |
Sanjay Patel | fdf0d5f | 2016-04-18 19:11:57 +0000 | [diff] [blame] | 828 | |
Filipe Cabecinhas | 0011c58 | 2015-07-03 20:12:01 +0000 | [diff] [blame] | 829 | // Remove old function, no longer used, from the module. |
| 830 | F->eraseFromParent(); |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 831 | } |
| 832 | } |
Devang Patel | 80ae349 | 2009-08-28 23:24:31 +0000 | [diff] [blame] | 833 | |
Manman Ren | 209b17c | 2013-09-28 00:22:27 +0000 | [diff] [blame] | 834 | void llvm::UpgradeInstWithTBAATag(Instruction *I) { |
Duncan P. N. Exon Smith | de36e80 | 2014-11-11 21:30:22 +0000 | [diff] [blame] | 835 | MDNode *MD = I->getMetadata(LLVMContext::MD_tbaa); |
Manman Ren | 209b17c | 2013-09-28 00:22:27 +0000 | [diff] [blame] | 836 | assert(MD && "UpgradeInstWithTBAATag should have a TBAA tag"); |
| 837 | // Check if the tag uses struct-path aware TBAA format. |
| 838 | if (isa<MDNode>(MD->getOperand(0)) && MD->getNumOperands() >= 3) |
| 839 | return; |
| 840 | |
| 841 | if (MD->getNumOperands() == 3) { |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 842 | Metadata *Elts[] = {MD->getOperand(0), MD->getOperand(1)}; |
Manman Ren | 209b17c | 2013-09-28 00:22:27 +0000 | [diff] [blame] | 843 | MDNode *ScalarType = MDNode::get(I->getContext(), Elts); |
| 844 | // Create a MDNode <ScalarType, ScalarType, offset 0, const> |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 845 | Metadata *Elts2[] = {ScalarType, ScalarType, |
| 846 | ConstantAsMetadata::get(Constant::getNullValue( |
| 847 | Type::getInt64Ty(I->getContext()))), |
| 848 | MD->getOperand(2)}; |
Manman Ren | 209b17c | 2013-09-28 00:22:27 +0000 | [diff] [blame] | 849 | I->setMetadata(LLVMContext::MD_tbaa, MDNode::get(I->getContext(), Elts2)); |
| 850 | } else { |
| 851 | // Create a MDNode <MD, MD, offset 0> |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 852 | Metadata *Elts[] = {MD, MD, ConstantAsMetadata::get(Constant::getNullValue( |
| 853 | Type::getInt64Ty(I->getContext())))}; |
Manman Ren | 209b17c | 2013-09-28 00:22:27 +0000 | [diff] [blame] | 854 | I->setMetadata(LLVMContext::MD_tbaa, MDNode::get(I->getContext(), Elts)); |
| 855 | } |
| 856 | } |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 857 | |
| 858 | Instruction *llvm::UpgradeBitCastInst(unsigned Opc, Value *V, Type *DestTy, |
| 859 | Instruction *&Temp) { |
| 860 | if (Opc != Instruction::BitCast) |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 861 | return nullptr; |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 862 | |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 863 | Temp = nullptr; |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 864 | Type *SrcTy = V->getType(); |
| 865 | if (SrcTy->isPtrOrPtrVectorTy() && DestTy->isPtrOrPtrVectorTy() && |
| 866 | SrcTy->getPointerAddressSpace() != DestTy->getPointerAddressSpace()) { |
| 867 | LLVMContext &Context = V->getContext(); |
| 868 | |
| 869 | // We have no information about target data layout, so we assume that |
| 870 | // the maximum pointer size is 64bit. |
| 871 | Type *MidTy = Type::getInt64Ty(Context); |
| 872 | Temp = CastInst::Create(Instruction::PtrToInt, V, MidTy); |
| 873 | |
| 874 | return CastInst::Create(Instruction::IntToPtr, Temp, DestTy); |
| 875 | } |
| 876 | |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 877 | return nullptr; |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 878 | } |
| 879 | |
| 880 | Value *llvm::UpgradeBitCastExpr(unsigned Opc, Constant *C, Type *DestTy) { |
| 881 | if (Opc != Instruction::BitCast) |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 882 | return nullptr; |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 883 | |
| 884 | Type *SrcTy = C->getType(); |
| 885 | if (SrcTy->isPtrOrPtrVectorTy() && DestTy->isPtrOrPtrVectorTy() && |
| 886 | SrcTy->getPointerAddressSpace() != DestTy->getPointerAddressSpace()) { |
| 887 | LLVMContext &Context = C->getContext(); |
| 888 | |
| 889 | // We have no information about target data layout, so we assume that |
| 890 | // the maximum pointer size is 64bit. |
| 891 | Type *MidTy = Type::getInt64Ty(Context); |
| 892 | |
| 893 | return ConstantExpr::getIntToPtr(ConstantExpr::getPtrToInt(C, MidTy), |
| 894 | DestTy); |
| 895 | } |
| 896 | |
Craig Topper | c620761 | 2014-04-09 06:08:46 +0000 | [diff] [blame] | 897 | return nullptr; |
Matt Arsenault | b03bd4d | 2013-11-15 01:34:59 +0000 | [diff] [blame] | 898 | } |
Manman Ren | 8b4306c | 2013-12-02 21:29:56 +0000 | [diff] [blame] | 899 | |
| 900 | /// Check the debug info version number, if it is out-dated, drop the debug |
| 901 | /// info. Return true if module is modified. |
| 902 | bool llvm::UpgradeDebugInfo(Module &M) { |
Manman Ren | 2ebfb42 | 2014-01-16 01:51:12 +0000 | [diff] [blame] | 903 | unsigned Version = getDebugMetadataVersionFromModule(M); |
| 904 | if (Version == DEBUG_METADATA_VERSION) |
Manman Ren | 8b4306c | 2013-12-02 21:29:56 +0000 | [diff] [blame] | 905 | return false; |
| 906 | |
Manman Ren | 2ebfb42 | 2014-01-16 01:51:12 +0000 | [diff] [blame] | 907 | bool RetCode = StripDebugInfo(M); |
| 908 | if (RetCode) { |
| 909 | DiagnosticInfoDebugMetadataVersion DiagVersion(M, Version); |
| 910 | M.getContext().diagnose(DiagVersion); |
| 911 | } |
| 912 | return RetCode; |
Manman Ren | 8b4306c | 2013-12-02 21:29:56 +0000 | [diff] [blame] | 913 | } |
Eli Bendersky | 5d5e18d | 2014-06-25 15:41:00 +0000 | [diff] [blame] | 914 | |
Manman Ren | b5d7ff4 | 2016-05-25 23:14:48 +0000 | [diff] [blame] | 915 | bool llvm::UpgradeModuleFlags(Module &M) { |
| 916 | const NamedMDNode *ModFlags = M.getModuleFlagsMetadata(); |
| 917 | if (!ModFlags) |
| 918 | return false; |
| 919 | |
| 920 | bool HasObjCFlag = false, HasClassProperties = false; |
| 921 | for (unsigned I = 0, E = ModFlags->getNumOperands(); I != E; ++I) { |
| 922 | MDNode *Op = ModFlags->getOperand(I); |
| 923 | if (Op->getNumOperands() < 2) |
| 924 | continue; |
| 925 | MDString *ID = dyn_cast_or_null<MDString>(Op->getOperand(1)); |
| 926 | if (!ID) |
| 927 | continue; |
| 928 | if (ID->getString() == "Objective-C Image Info Version") |
| 929 | HasObjCFlag = true; |
| 930 | if (ID->getString() == "Objective-C Class Properties") |
| 931 | HasClassProperties = true; |
| 932 | } |
| 933 | // "Objective-C Class Properties" is recently added for Objective-C. We |
| 934 | // upgrade ObjC bitcodes to contain a "Objective-C Class Properties" module |
| 935 | // flag of value 0, so we can correclty report error when trying to link |
| 936 | // an ObjC bitcode without this module flag with an ObjC bitcode with this |
| 937 | // module flag. |
| 938 | if (HasObjCFlag && !HasClassProperties) { |
| 939 | M.addModuleFlag(llvm::Module::Error, "Objective-C Class Properties", |
| 940 | (uint32_t)0); |
| 941 | return true; |
| 942 | } |
| 943 | return false; |
| 944 | } |
| 945 | |
Duncan P. N. Exon Smith | efe16c8 | 2016-03-25 00:56:13 +0000 | [diff] [blame] | 946 | static bool isOldLoopArgument(Metadata *MD) { |
| 947 | auto *T = dyn_cast_or_null<MDTuple>(MD); |
| 948 | if (!T) |
| 949 | return false; |
| 950 | if (T->getNumOperands() < 1) |
| 951 | return false; |
| 952 | auto *S = dyn_cast_or_null<MDString>(T->getOperand(0)); |
| 953 | if (!S) |
| 954 | return false; |
| 955 | return S->getString().startswith("llvm.vectorizer."); |
| 956 | } |
| 957 | |
| 958 | static MDString *upgradeLoopTag(LLVMContext &C, StringRef OldTag) { |
| 959 | StringRef OldPrefix = "llvm.vectorizer."; |
| 960 | assert(OldTag.startswith(OldPrefix) && "Expected old prefix"); |
| 961 | |
| 962 | if (OldTag == "llvm.vectorizer.unroll") |
| 963 | return MDString::get(C, "llvm.loop.interleave.count"); |
| 964 | |
| 965 | return MDString::get( |
| 966 | C, (Twine("llvm.loop.vectorize.") + OldTag.drop_front(OldPrefix.size())) |
| 967 | .str()); |
| 968 | } |
| 969 | |
| 970 | static Metadata *upgradeLoopArgument(Metadata *MD) { |
| 971 | auto *T = dyn_cast_or_null<MDTuple>(MD); |
| 972 | if (!T) |
| 973 | return MD; |
| 974 | if (T->getNumOperands() < 1) |
| 975 | return MD; |
| 976 | auto *OldTag = dyn_cast_or_null<MDString>(T->getOperand(0)); |
| 977 | if (!OldTag) |
| 978 | return MD; |
| 979 | if (!OldTag->getString().startswith("llvm.vectorizer.")) |
| 980 | return MD; |
| 981 | |
| 982 | // This has an old tag. Upgrade it. |
| 983 | SmallVector<Metadata *, 8> Ops; |
| 984 | Ops.reserve(T->getNumOperands()); |
| 985 | Ops.push_back(upgradeLoopTag(T->getContext(), OldTag->getString())); |
| 986 | for (unsigned I = 1, E = T->getNumOperands(); I != E; ++I) |
| 987 | Ops.push_back(T->getOperand(I)); |
| 988 | |
| 989 | return MDTuple::get(T->getContext(), Ops); |
| 990 | } |
| 991 | |
| 992 | MDNode *llvm::upgradeInstructionLoopAttachment(MDNode &N) { |
| 993 | auto *T = dyn_cast<MDTuple>(&N); |
| 994 | if (!T) |
| 995 | return &N; |
| 996 | |
| 997 | if (!llvm::any_of(T->operands(), isOldLoopArgument)) |
| 998 | return &N; |
| 999 | |
| 1000 | SmallVector<Metadata *, 8> Ops; |
| 1001 | Ops.reserve(T->getNumOperands()); |
| 1002 | for (Metadata *MD : T->operands()) |
| 1003 | Ops.push_back(upgradeLoopArgument(MD)); |
| 1004 | |
| 1005 | return MDTuple::get(T->getContext(), Ops); |
Eli Bendersky | 5d5e18d | 2014-06-25 15:41:00 +0000 | [diff] [blame] | 1006 | } |