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