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