1 //===-- AutoUpgrade.cpp - Implement auto-upgrade helper functions ---------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the auto-upgrade helper functions
12 //===----------------------------------------------------------------------===//
14 #include "llvm/AutoUpgrade.h"
15 #include "llvm/DebugInfo.h"
16 #include "llvm/IR/Constants.h"
17 #include "llvm/IR/Function.h"
18 #include "llvm/IR/IRBuilder.h"
19 #include "llvm/IR/Instruction.h"
20 #include "llvm/IR/IntrinsicInst.h"
21 #include "llvm/IR/LLVMContext.h"
22 #include "llvm/IR/Module.h"
23 #include "llvm/Support/CFG.h"
24 #include "llvm/Support/CallSite.h"
25 #include "llvm/Support/ErrorHandling.h"
29 // Upgrade the declarations of the SSE4.1 functions whose arguments have
30 // changed their type from v4f32 to v2i64.
31 static bool UpgradeSSE41Function(Function* F, Intrinsic::ID IID,
33 // Check whether this is an old version of the function, which received
35 Type *Arg0Type = F->getFunctionType()->getParamType(0);
36 if (Arg0Type != VectorType::get(Type::getFloatTy(F->getContext()), 4))
39 // Yes, it's old, replace it with new version.
40 F->setName(F->getName() + ".old");
41 NewFn = Intrinsic::getDeclaration(F->getParent(), IID);
45 static bool UpgradeIntrinsicFunction1(Function *F, Function *&NewFn) {
46 assert(F && "Illegal to upgrade a non-existent Function.");
48 // Quickly eliminate it, if it's not a candidate.
49 StringRef Name = F->getName();
50 if (Name.size() <= 8 || !Name.startswith("llvm."))
52 Name = Name.substr(5); // Strip off "llvm."
57 if (Name.startswith("arm.neon.vclz")) {
59 F->arg_begin()->getType(),
60 Type::getInt1Ty(F->getContext())
62 // Can't use Intrinsic::getDeclaration here as it adds a ".i1" to
63 // the end of the name. Change name from llvm.arm.neon.vclz.* to
65 FunctionType* fType = FunctionType::get(F->getReturnType(), args, false);
66 NewFn = Function::Create(fType, F->getLinkage(),
67 "llvm.ctlz." + Name.substr(14), F->getParent());
70 if (Name.startswith("arm.neon.vcnt")) {
71 NewFn = Intrinsic::getDeclaration(F->getParent(), Intrinsic::ctpop,
72 F->arg_begin()->getType());
78 if (Name.startswith("ctlz.") && F->arg_size() == 1) {
79 F->setName(Name + ".old");
80 NewFn = Intrinsic::getDeclaration(F->getParent(), Intrinsic::ctlz,
81 F->arg_begin()->getType());
84 if (Name.startswith("cttz.") && F->arg_size() == 1) {
85 F->setName(Name + ".old");
86 NewFn = Intrinsic::getDeclaration(F->getParent(), Intrinsic::cttz,
87 F->arg_begin()->getType());
93 // We only need to change the name to match the mangling including the
95 if (F->arg_size() == 2 && Name.startswith("objectsize.")) {
96 Type *Tys[2] = { F->getReturnType(), F->arg_begin()->getType() };
97 if (F->getName() != Intrinsic::getName(Intrinsic::objectsize, Tys)) {
98 F->setName(Name + ".old");
99 NewFn = Intrinsic::getDeclaration(F->getParent(),
100 Intrinsic::objectsize, Tys);
107 if (Name.startswith("x86.sse2.pcmpeq.") ||
108 Name.startswith("x86.sse2.pcmpgt.") ||
109 Name.startswith("x86.avx2.pcmpeq.") ||
110 Name.startswith("x86.avx2.pcmpgt.") ||
111 Name.startswith("x86.avx.vpermil.") ||
112 Name == "x86.avx.movnt.dq.256" ||
113 Name == "x86.avx.movnt.pd.256" ||
114 Name == "x86.avx.movnt.ps.256" ||
115 Name == "x86.sse42.crc32.64.8" ||
116 (Name.startswith("x86.xop.vpcom") && F->arg_size() == 2)) {
120 // SSE4.1 ptest functions may have an old signature.
121 if (Name.startswith("x86.sse41.ptest")) {
122 if (Name == "x86.sse41.ptestc")
123 return UpgradeSSE41Function(F, Intrinsic::x86_sse41_ptestc, NewFn);
124 if (Name == "x86.sse41.ptestz")
125 return UpgradeSSE41Function(F, Intrinsic::x86_sse41_ptestz, NewFn);
126 if (Name == "x86.sse41.ptestnzc")
127 return UpgradeSSE41Function(F, Intrinsic::x86_sse41_ptestnzc, NewFn);
129 // frcz.ss/sd may need to have an argument dropped
130 if (Name.startswith("x86.xop.vfrcz.ss") && F->arg_size() == 2) {
131 F->setName(Name + ".old");
132 NewFn = Intrinsic::getDeclaration(F->getParent(),
133 Intrinsic::x86_xop_vfrcz_ss);
136 if (Name.startswith("x86.xop.vfrcz.sd") && F->arg_size() == 2) {
137 F->setName(Name + ".old");
138 NewFn = Intrinsic::getDeclaration(F->getParent(),
139 Intrinsic::x86_xop_vfrcz_sd);
142 // Fix the FMA4 intrinsics to remove the 4
143 if (Name.startswith("x86.fma4.")) {
144 F->setName("llvm.x86.fma" + Name.substr(8));
152 // This may not belong here. This function is effectively being overloaded
153 // to both detect an intrinsic which needs upgrading, and to provide the
154 // upgraded form of the intrinsic. We should perhaps have two separate
155 // functions for this.
159 bool llvm::UpgradeIntrinsicFunction(Function *F, Function *&NewFn) {
161 bool Upgraded = UpgradeIntrinsicFunction1(F, NewFn);
163 // Upgrade intrinsic attributes. This does not change the function.
166 if (unsigned id = F->getIntrinsicID())
167 F->setAttributes(Intrinsic::getAttributes(F->getContext(),
172 bool llvm::UpgradeGlobalVariable(GlobalVariable *GV) {
173 // Nothing to do yet.
177 // UpgradeIntrinsicCall - Upgrade a call to an old intrinsic to be a call the
178 // upgraded intrinsic. All argument and return casting must be provided in
179 // order to seamlessly integrate with existing context.
180 void llvm::UpgradeIntrinsicCall(CallInst *CI, Function *NewFn) {
181 Function *F = CI->getCalledFunction();
182 LLVMContext &C = CI->getContext();
183 IRBuilder<> Builder(C);
184 Builder.SetInsertPoint(CI->getParent(), CI);
186 assert(F && "Intrinsic call is not direct?");
189 // Get the Function's name.
190 StringRef Name = F->getName();
193 // Upgrade packed integer vector compares intrinsics to compare instructions
194 if (Name.startswith("llvm.x86.sse2.pcmpeq.") ||
195 Name.startswith("llvm.x86.avx2.pcmpeq.")) {
196 Rep = Builder.CreateICmpEQ(CI->getArgOperand(0), CI->getArgOperand(1),
198 // need to sign extend since icmp returns vector of i1
199 Rep = Builder.CreateSExt(Rep, CI->getType(), "");
200 } else if (Name.startswith("llvm.x86.sse2.pcmpgt.") ||
201 Name.startswith("llvm.x86.avx2.pcmpgt.")) {
202 Rep = Builder.CreateICmpSGT(CI->getArgOperand(0), CI->getArgOperand(1),
204 // need to sign extend since icmp returns vector of i1
205 Rep = Builder.CreateSExt(Rep, CI->getType(), "");
206 } else if (Name == "llvm.x86.avx.movnt.dq.256" ||
207 Name == "llvm.x86.avx.movnt.ps.256" ||
208 Name == "llvm.x86.avx.movnt.pd.256") {
209 IRBuilder<> Builder(C);
210 Builder.SetInsertPoint(CI->getParent(), CI);
212 Module *M = F->getParent();
213 SmallVector<Value *, 1> Elts;
214 Elts.push_back(ConstantInt::get(Type::getInt32Ty(C), 1));
215 MDNode *Node = MDNode::get(C, Elts);
217 Value *Arg0 = CI->getArgOperand(0);
218 Value *Arg1 = CI->getArgOperand(1);
220 // Convert the type of the pointer to a pointer to the stored type.
221 Value *BC = Builder.CreateBitCast(Arg0,
222 PointerType::getUnqual(Arg1->getType()),
224 StoreInst *SI = Builder.CreateStore(Arg1, BC);
225 SI->setMetadata(M->getMDKindID("nontemporal"), Node);
226 SI->setAlignment(16);
229 CI->eraseFromParent();
231 } else if (Name.startswith("llvm.x86.xop.vpcom")) {
233 if (Name.endswith("ub"))
234 intID = Intrinsic::x86_xop_vpcomub;
235 else if (Name.endswith("uw"))
236 intID = Intrinsic::x86_xop_vpcomuw;
237 else if (Name.endswith("ud"))
238 intID = Intrinsic::x86_xop_vpcomud;
239 else if (Name.endswith("uq"))
240 intID = Intrinsic::x86_xop_vpcomuq;
241 else if (Name.endswith("b"))
242 intID = Intrinsic::x86_xop_vpcomb;
243 else if (Name.endswith("w"))
244 intID = Intrinsic::x86_xop_vpcomw;
245 else if (Name.endswith("d"))
246 intID = Intrinsic::x86_xop_vpcomd;
247 else if (Name.endswith("q"))
248 intID = Intrinsic::x86_xop_vpcomq;
250 llvm_unreachable("Unknown suffix");
252 Name = Name.substr(18); // strip off "llvm.x86.xop.vpcom"
254 if (Name.startswith("lt"))
256 else if (Name.startswith("le"))
258 else if (Name.startswith("gt"))
260 else if (Name.startswith("ge"))
262 else if (Name.startswith("eq"))
264 else if (Name.startswith("ne"))
266 else if (Name.startswith("true"))
268 else if (Name.startswith("false"))
271 llvm_unreachable("Unknown condition");
273 Function *VPCOM = Intrinsic::getDeclaration(F->getParent(), intID);
274 Rep = Builder.CreateCall3(VPCOM, CI->getArgOperand(0),
275 CI->getArgOperand(1), Builder.getInt8(Imm));
276 } else if (Name == "llvm.x86.sse42.crc32.64.8") {
277 Function *CRC32 = Intrinsic::getDeclaration(F->getParent(),
278 Intrinsic::x86_sse42_crc32_32_8);
279 Value *Trunc0 = Builder.CreateTrunc(CI->getArgOperand(0), Type::getInt32Ty(C));
280 Rep = Builder.CreateCall2(CRC32, Trunc0, CI->getArgOperand(1));
281 Rep = Builder.CreateZExt(Rep, CI->getType(), "");
283 bool PD128 = false, PD256 = false, PS128 = false, PS256 = false;
284 if (Name == "llvm.x86.avx.vpermil.pd.256")
286 else if (Name == "llvm.x86.avx.vpermil.pd")
288 else if (Name == "llvm.x86.avx.vpermil.ps.256")
290 else if (Name == "llvm.x86.avx.vpermil.ps")
293 if (PD256 || PD128 || PS256 || PS128) {
294 Value *Op0 = CI->getArgOperand(0);
295 unsigned Imm = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue();
296 SmallVector<Constant*, 8> Idxs;
299 for (unsigned i = 0; i != 2; ++i)
300 Idxs.push_back(Builder.getInt32((Imm >> i) & 0x1));
302 for (unsigned l = 0; l != 4; l+=2)
303 for (unsigned i = 0; i != 2; ++i)
304 Idxs.push_back(Builder.getInt32(((Imm >> (l+i)) & 0x1) + l));
306 for (unsigned i = 0; i != 4; ++i)
307 Idxs.push_back(Builder.getInt32((Imm >> (2 * i)) & 0x3));
309 for (unsigned l = 0; l != 8; l+=4)
310 for (unsigned i = 0; i != 4; ++i)
311 Idxs.push_back(Builder.getInt32(((Imm >> (2 * i)) & 0x3) + l));
313 llvm_unreachable("Unexpected function");
315 Rep = Builder.CreateShuffleVector(Op0, Op0, ConstantVector::get(Idxs));
317 llvm_unreachable("Unknown function for CallInst upgrade.");
321 CI->replaceAllUsesWith(Rep);
322 CI->eraseFromParent();
326 std::string Name = CI->getName().str();
327 CI->setName(Name + ".old");
329 switch (NewFn->getIntrinsicID()) {
331 llvm_unreachable("Unknown function for CallInst upgrade.");
333 case Intrinsic::ctlz:
334 case Intrinsic::cttz:
335 assert(CI->getNumArgOperands() == 1 &&
336 "Mismatch between function args and call args");
337 CI->replaceAllUsesWith(Builder.CreateCall2(NewFn, CI->getArgOperand(0),
338 Builder.getFalse(), Name));
339 CI->eraseFromParent();
342 case Intrinsic::objectsize:
343 CI->replaceAllUsesWith(Builder.CreateCall2(NewFn,
344 CI->getArgOperand(0),
345 CI->getArgOperand(1),
347 CI->eraseFromParent();
350 case Intrinsic::arm_neon_vclz: {
351 // Change name from llvm.arm.neon.vclz.* to llvm.ctlz.*
352 CI->replaceAllUsesWith(Builder.CreateCall2(NewFn, CI->getArgOperand(0),
354 "llvm.ctlz." + Name.substr(14)));
355 CI->eraseFromParent();
358 case Intrinsic::ctpop: {
359 CI->replaceAllUsesWith(Builder.CreateCall(NewFn, CI->getArgOperand(0)));
360 CI->eraseFromParent();
364 case Intrinsic::x86_xop_vfrcz_ss:
365 case Intrinsic::x86_xop_vfrcz_sd:
366 CI->replaceAllUsesWith(Builder.CreateCall(NewFn, CI->getArgOperand(1),
368 CI->eraseFromParent();
371 case Intrinsic::x86_sse41_ptestc:
372 case Intrinsic::x86_sse41_ptestz:
373 case Intrinsic::x86_sse41_ptestnzc: {
374 // The arguments for these intrinsics used to be v4f32, and changed
375 // to v2i64. This is purely a nop, since those are bitwise intrinsics.
376 // So, the only thing required is a bitcast for both arguments.
377 // First, check the arguments have the old type.
378 Value *Arg0 = CI->getArgOperand(0);
379 if (Arg0->getType() != VectorType::get(Type::getFloatTy(C), 4))
382 // Old intrinsic, add bitcasts
383 Value *Arg1 = CI->getArgOperand(1);
386 Builder.CreateBitCast(Arg0,
387 VectorType::get(Type::getInt64Ty(C), 2),
390 Builder.CreateBitCast(Arg1,
391 VectorType::get(Type::getInt64Ty(C), 2),
394 CallInst* NewCall = Builder.CreateCall2(NewFn, BC0, BC1, Name);
395 CI->replaceAllUsesWith(NewCall);
396 CI->eraseFromParent();
402 // This tests each Function to determine if it needs upgrading. When we find
403 // one we are interested in, we then upgrade all calls to reflect the new
405 void llvm::UpgradeCallsToIntrinsic(Function* F) {
406 assert(F && "Illegal attempt to upgrade a non-existent intrinsic.");
408 // Upgrade the function and check if it is a totaly new function.
410 if (UpgradeIntrinsicFunction(F, NewFn)) {
412 // Replace all uses to the old function with the new one if necessary.
413 for (Value::use_iterator UI = F->use_begin(), UE = F->use_end();
415 if (CallInst *CI = dyn_cast<CallInst>(*UI++))
416 UpgradeIntrinsicCall(CI, NewFn);
418 // Remove old function, no longer used, from the module.
419 F->eraseFromParent();
424 void llvm::UpgradeInstWithTBAATag(Instruction *I) {
425 MDNode *MD = I->getMetadata(LLVMContext::MD_tbaa);
426 assert(MD && "UpgradeInstWithTBAATag should have a TBAA tag");
427 // Check if the tag uses struct-path aware TBAA format.
428 if (isa<MDNode>(MD->getOperand(0)) && MD->getNumOperands() >= 3)
431 if (MD->getNumOperands() == 3) {
436 MDNode *ScalarType = MDNode::get(I->getContext(), Elts);
437 // Create a MDNode <ScalarType, ScalarType, offset 0, const>
439 ScalarType, ScalarType,
440 Constant::getNullValue(Type::getInt64Ty(I->getContext())),
443 I->setMetadata(LLVMContext::MD_tbaa, MDNode::get(I->getContext(), Elts2));
445 // Create a MDNode <MD, MD, offset 0>
446 Value *Elts[] = {MD, MD,
447 Constant::getNullValue(Type::getInt64Ty(I->getContext()))};
448 I->setMetadata(LLVMContext::MD_tbaa, MDNode::get(I->getContext(), Elts));
452 Instruction *llvm::UpgradeBitCastInst(unsigned Opc, Value *V, Type *DestTy,
453 Instruction *&Temp) {
454 if (Opc != Instruction::BitCast)
458 Type *SrcTy = V->getType();
459 if (SrcTy->isPtrOrPtrVectorTy() && DestTy->isPtrOrPtrVectorTy() &&
460 SrcTy->getPointerAddressSpace() != DestTy->getPointerAddressSpace()) {
461 LLVMContext &Context = V->getContext();
463 // We have no information about target data layout, so we assume that
464 // the maximum pointer size is 64bit.
465 Type *MidTy = Type::getInt64Ty(Context);
466 Temp = CastInst::Create(Instruction::PtrToInt, V, MidTy);
468 return CastInst::Create(Instruction::IntToPtr, Temp, DestTy);
474 Value *llvm::UpgradeBitCastExpr(unsigned Opc, Constant *C, Type *DestTy) {
475 if (Opc != Instruction::BitCast)
478 Type *SrcTy = C->getType();
479 if (SrcTy->isPtrOrPtrVectorTy() && DestTy->isPtrOrPtrVectorTy() &&
480 SrcTy->getPointerAddressSpace() != DestTy->getPointerAddressSpace()) {
481 LLVMContext &Context = C->getContext();
483 // We have no information about target data layout, so we assume that
484 // the maximum pointer size is 64bit.
485 Type *MidTy = Type::getInt64Ty(Context);
487 return ConstantExpr::getIntToPtr(ConstantExpr::getPtrToInt(C, MidTy),
494 /// Check the debug info version number, if it is out-dated, drop the debug
495 /// info. Return true if module is modified.
496 bool llvm::UpgradeDebugInfo(Module &M) {
497 if (getDebugMetadataVersionFromModule(M) == DEBUG_METADATA_VERSION)
500 return StripDebugInfo(M);