Add comdat key field to llvm.global_ctors and llvm.global_dtors
[oota-llvm.git] / lib / IR / AutoUpgrade.cpp
1 //===-- AutoUpgrade.cpp - Implement auto-upgrade helper functions ---------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the auto-upgrade helper functions
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/IR/AutoUpgrade.h"
15 #include "llvm/IR/CFG.h"
16 #include "llvm/IR/CallSite.h"
17 #include "llvm/IR/Constants.h"
18 #include "llvm/IR/DebugInfo.h"
19 #include "llvm/IR/DiagnosticInfo.h"
20 #include "llvm/IR/Function.h"
21 #include "llvm/IR/IRBuilder.h"
22 #include "llvm/IR/Instruction.h"
23 #include "llvm/IR/IntrinsicInst.h"
24 #include "llvm/IR/LLVMContext.h"
25 #include "llvm/IR/Module.h"
26 #include "llvm/Support/ErrorHandling.h"
27 #include <cstring>
28 using namespace llvm;
29
30 // Upgrade the declarations of the SSE4.1 functions whose arguments have
31 // changed their type from v4f32 to v2i64.
32 static bool UpgradeSSE41Function(Function* F, Intrinsic::ID IID,
33                                  Function *&NewFn) {
34   // Check whether this is an old version of the function, which received
35   // v4f32 arguments.
36   Type *Arg0Type = F->getFunctionType()->getParamType(0);
37   if (Arg0Type != VectorType::get(Type::getFloatTy(F->getContext()), 4))
38     return false;
39
40   // Yes, it's old, replace it with new version.
41   F->setName(F->getName() + ".old");
42   NewFn = Intrinsic::getDeclaration(F->getParent(), IID);
43   return true;
44 }
45
46 static bool UpgradeIntrinsicFunction1(Function *F, Function *&NewFn) {
47   assert(F && "Illegal to upgrade a non-existent Function.");
48
49   // Quickly eliminate it, if it's not a candidate.
50   StringRef Name = F->getName();
51   if (Name.size() <= 8 || !Name.startswith("llvm."))
52     return false;
53   Name = Name.substr(5); // Strip off "llvm."
54
55   switch (Name[0]) {
56   default: break;
57   case 'a': {
58     if (Name.startswith("arm.neon.vclz")) {
59       Type* args[2] = {
60         F->arg_begin()->getType(),
61         Type::getInt1Ty(F->getContext())
62       };
63       // Can't use Intrinsic::getDeclaration here as it adds a ".i1" to
64       // the end of the name. Change name from llvm.arm.neon.vclz.* to
65       //  llvm.ctlz.*
66       FunctionType* fType = FunctionType::get(F->getReturnType(), args, false);
67       NewFn = Function::Create(fType, F->getLinkage(),
68                                "llvm.ctlz." + Name.substr(14), F->getParent());
69       return true;
70     }
71     if (Name.startswith("arm.neon.vcnt")) {
72       NewFn = Intrinsic::getDeclaration(F->getParent(), Intrinsic::ctpop,
73                                         F->arg_begin()->getType());
74       return true;
75     }
76     break;
77   }
78   case 'c': {
79     if (Name.startswith("ctlz.") && F->arg_size() == 1) {
80       F->setName(Name + ".old");
81       NewFn = Intrinsic::getDeclaration(F->getParent(), Intrinsic::ctlz,
82                                         F->arg_begin()->getType());
83       return true;
84     }
85     if (Name.startswith("cttz.") && F->arg_size() == 1) {
86       F->setName(Name + ".old");
87       NewFn = Intrinsic::getDeclaration(F->getParent(), Intrinsic::cttz,
88                                         F->arg_begin()->getType());
89       return true;
90     }
91     break;
92   }
93   case 'o':
94     // We only need to change the name to match the mangling including the
95     // address space.
96     if (F->arg_size() == 2 && Name.startswith("objectsize.")) {
97       Type *Tys[2] = { F->getReturnType(), F->arg_begin()->getType() };
98       if (F->getName() != Intrinsic::getName(Intrinsic::objectsize, Tys)) {
99         F->setName(Name + ".old");
100         NewFn = Intrinsic::getDeclaration(F->getParent(),
101                                           Intrinsic::objectsize, Tys);
102         return true;
103       }
104     }
105     break;
106
107   case 'x': {
108     if (Name.startswith("x86.sse2.pcmpeq.") ||
109         Name.startswith("x86.sse2.pcmpgt.") ||
110         Name.startswith("x86.avx2.pcmpeq.") ||
111         Name.startswith("x86.avx2.pcmpgt.") ||
112         Name.startswith("x86.avx.vpermil.") ||
113         Name == "x86.avx.movnt.dq.256" ||
114         Name == "x86.avx.movnt.pd.256" ||
115         Name == "x86.avx.movnt.ps.256" ||
116         Name == "x86.sse42.crc32.64.8" ||
117         (Name.startswith("x86.xop.vpcom") && F->arg_size() == 2)) {
118       NewFn = nullptr;
119       return true;
120     }
121     // SSE4.1 ptest functions may have an old signature.
122     if (Name.startswith("x86.sse41.ptest")) {
123       if (Name == "x86.sse41.ptestc")
124         return UpgradeSSE41Function(F, Intrinsic::x86_sse41_ptestc, NewFn);
125       if (Name == "x86.sse41.ptestz")
126         return UpgradeSSE41Function(F, Intrinsic::x86_sse41_ptestz, NewFn);
127       if (Name == "x86.sse41.ptestnzc")
128         return UpgradeSSE41Function(F, Intrinsic::x86_sse41_ptestnzc, NewFn);
129     }
130     // frcz.ss/sd may need to have an argument dropped
131     if (Name.startswith("x86.xop.vfrcz.ss") && F->arg_size() == 2) {
132       F->setName(Name + ".old");
133       NewFn = Intrinsic::getDeclaration(F->getParent(),
134                                         Intrinsic::x86_xop_vfrcz_ss);
135       return true;
136     }
137     if (Name.startswith("x86.xop.vfrcz.sd") && F->arg_size() == 2) {
138       F->setName(Name + ".old");
139       NewFn = Intrinsic::getDeclaration(F->getParent(),
140                                         Intrinsic::x86_xop_vfrcz_sd);
141       return true;
142     }
143     // Fix the FMA4 intrinsics to remove the 4
144     if (Name.startswith("x86.fma4.")) {
145       F->setName("llvm.x86.fma" + Name.substr(8));
146       NewFn = F;
147       return true;
148     }
149     break;
150   }
151   }
152
153   //  This may not belong here. This function is effectively being overloaded
154   //  to both detect an intrinsic which needs upgrading, and to provide the
155   //  upgraded form of the intrinsic. We should perhaps have two separate
156   //  functions for this.
157   return false;
158 }
159
160 bool llvm::UpgradeIntrinsicFunction(Function *F, Function *&NewFn) {
161   NewFn = nullptr;
162   bool Upgraded = UpgradeIntrinsicFunction1(F, NewFn);
163
164   // Upgrade intrinsic attributes.  This does not change the function.
165   if (NewFn)
166     F = NewFn;
167   if (unsigned id = F->getIntrinsicID())
168     F->setAttributes(Intrinsic::getAttributes(F->getContext(),
169                                               (Intrinsic::ID)id));
170   return Upgraded;
171 }
172
173 static bool UpgradeGlobalStructors(GlobalVariable *GV) {
174   ArrayType *ATy = dyn_cast<ArrayType>(GV->getType()->getElementType());
175   StructType *OldTy =
176       ATy ? dyn_cast<StructType>(ATy->getElementType()) : nullptr;
177
178   // Only upgrade an array of a two field struct with the appropriate field
179   // types.
180   if (!OldTy || OldTy->getNumElements() != 2)
181     return false;
182
183   // Get the upgraded 3 element type.
184   PointerType *VoidPtrTy = Type::getInt8Ty(GV->getContext())->getPointerTo();
185   Type *Tys[3] = {
186     OldTy->getElementType(0),
187     OldTy->getElementType(1),
188     VoidPtrTy
189   };
190   StructType *NewTy =
191       StructType::get(GV->getContext(), Tys, /*isPacked=*/false);
192
193   // Build new constants with a null third field filled in.
194   ConstantArray *OldInit = dyn_cast<ConstantArray>(GV->getInitializer());
195   if (!OldInit)
196     return false;
197   std::vector<Constant *> Initializers;
198   for (Use &U : OldInit->operands()) {
199     ConstantStruct *Init = cast<ConstantStruct>(&U);
200     Constant *NewInit =
201         ConstantStruct::get(NewTy, Init->getOperand(0), Init->getOperand(1),
202                             Constant::getNullValue(VoidPtrTy), nullptr);
203     Initializers.push_back(NewInit);
204   }
205   assert(Initializers.size() == ATy->getNumElements());
206
207   // Replace the old GV with a new one.
208   ATy = ArrayType::get(NewTy, Initializers.size());
209   Constant *NewInit = ConstantArray::get(ATy, Initializers);
210   GlobalVariable *NewGV = new GlobalVariable(
211       *GV->getParent(), ATy, GV->isConstant(), GV->getLinkage(), NewInit, "",
212       GV, GV->getThreadLocalMode(), GV->getType()->getAddressSpace(),
213       GV->isExternallyInitialized());
214   NewGV->copyAttributesFrom(GV);
215   NewGV->takeName(GV);
216   assert(GV->use_empty() && "program cannot use initializer list");
217   GV->eraseFromParent();
218   return true;
219 }
220
221 bool llvm::UpgradeGlobalVariable(GlobalVariable *GV) {
222   if (GV->getName() == "llvm.global_ctors" ||
223       GV->getName() == "llvm.global_dtors")
224     return UpgradeGlobalStructors(GV);
225
226   // Nothing to do yet.
227   return false;
228 }
229
230 // UpgradeIntrinsicCall - Upgrade a call to an old intrinsic to be a call the
231 // upgraded intrinsic. All argument and return casting must be provided in
232 // order to seamlessly integrate with existing context.
233 void llvm::UpgradeIntrinsicCall(CallInst *CI, Function *NewFn) {
234   Function *F = CI->getCalledFunction();
235   LLVMContext &C = CI->getContext();
236   IRBuilder<> Builder(C);
237   Builder.SetInsertPoint(CI->getParent(), CI);
238
239   assert(F && "Intrinsic call is not direct?");
240
241   if (!NewFn) {
242     // Get the Function's name.
243     StringRef Name = F->getName();
244
245     Value *Rep;
246     // Upgrade packed integer vector compares intrinsics to compare instructions
247     if (Name.startswith("llvm.x86.sse2.pcmpeq.") ||
248         Name.startswith("llvm.x86.avx2.pcmpeq.")) {
249       Rep = Builder.CreateICmpEQ(CI->getArgOperand(0), CI->getArgOperand(1),
250                                  "pcmpeq");
251       // need to sign extend since icmp returns vector of i1
252       Rep = Builder.CreateSExt(Rep, CI->getType(), "");
253     } else if (Name.startswith("llvm.x86.sse2.pcmpgt.") ||
254                Name.startswith("llvm.x86.avx2.pcmpgt.")) {
255       Rep = Builder.CreateICmpSGT(CI->getArgOperand(0), CI->getArgOperand(1),
256                                   "pcmpgt");
257       // need to sign extend since icmp returns vector of i1
258       Rep = Builder.CreateSExt(Rep, CI->getType(), "");
259     } else if (Name == "llvm.x86.avx.movnt.dq.256" ||
260                Name == "llvm.x86.avx.movnt.ps.256" ||
261                Name == "llvm.x86.avx.movnt.pd.256") {
262       IRBuilder<> Builder(C);
263       Builder.SetInsertPoint(CI->getParent(), CI);
264
265       Module *M = F->getParent();
266       SmallVector<Value *, 1> Elts;
267       Elts.push_back(ConstantInt::get(Type::getInt32Ty(C), 1));
268       MDNode *Node = MDNode::get(C, Elts);
269
270       Value *Arg0 = CI->getArgOperand(0);
271       Value *Arg1 = CI->getArgOperand(1);
272
273       // Convert the type of the pointer to a pointer to the stored type.
274       Value *BC = Builder.CreateBitCast(Arg0,
275                                         PointerType::getUnqual(Arg1->getType()),
276                                         "cast");
277       StoreInst *SI = Builder.CreateStore(Arg1, BC);
278       SI->setMetadata(M->getMDKindID("nontemporal"), Node);
279       SI->setAlignment(16);
280
281       // Remove intrinsic.
282       CI->eraseFromParent();
283       return;
284     } else if (Name.startswith("llvm.x86.xop.vpcom")) {
285       Intrinsic::ID intID;
286       if (Name.endswith("ub"))
287         intID = Intrinsic::x86_xop_vpcomub;
288       else if (Name.endswith("uw"))
289         intID = Intrinsic::x86_xop_vpcomuw;
290       else if (Name.endswith("ud"))
291         intID = Intrinsic::x86_xop_vpcomud;
292       else if (Name.endswith("uq"))
293         intID = Intrinsic::x86_xop_vpcomuq;
294       else if (Name.endswith("b"))
295         intID = Intrinsic::x86_xop_vpcomb;
296       else if (Name.endswith("w"))
297         intID = Intrinsic::x86_xop_vpcomw;
298       else if (Name.endswith("d"))
299         intID = Intrinsic::x86_xop_vpcomd;
300       else if (Name.endswith("q"))
301         intID = Intrinsic::x86_xop_vpcomq;
302       else
303         llvm_unreachable("Unknown suffix");
304
305       Name = Name.substr(18); // strip off "llvm.x86.xop.vpcom"
306       unsigned Imm;
307       if (Name.startswith("lt"))
308         Imm = 0;
309       else if (Name.startswith("le"))
310         Imm = 1;
311       else if (Name.startswith("gt"))
312         Imm = 2;
313       else if (Name.startswith("ge"))
314         Imm = 3;
315       else if (Name.startswith("eq"))
316         Imm = 4;
317       else if (Name.startswith("ne"))
318         Imm = 5;
319       else if (Name.startswith("true"))
320         Imm = 6;
321       else if (Name.startswith("false"))
322         Imm = 7;
323       else
324         llvm_unreachable("Unknown condition");
325
326       Function *VPCOM = Intrinsic::getDeclaration(F->getParent(), intID);
327       Rep = Builder.CreateCall3(VPCOM, CI->getArgOperand(0),
328                                 CI->getArgOperand(1), Builder.getInt8(Imm));
329     } else if (Name == "llvm.x86.sse42.crc32.64.8") {
330       Function *CRC32 = Intrinsic::getDeclaration(F->getParent(),
331                                                Intrinsic::x86_sse42_crc32_32_8);
332       Value *Trunc0 = Builder.CreateTrunc(CI->getArgOperand(0), Type::getInt32Ty(C));
333       Rep = Builder.CreateCall2(CRC32, Trunc0, CI->getArgOperand(1));
334       Rep = Builder.CreateZExt(Rep, CI->getType(), "");
335     } else {
336       bool PD128 = false, PD256 = false, PS128 = false, PS256 = false;
337       if (Name == "llvm.x86.avx.vpermil.pd.256")
338         PD256 = true;
339       else if (Name == "llvm.x86.avx.vpermil.pd")
340         PD128 = true;
341       else if (Name == "llvm.x86.avx.vpermil.ps.256")
342         PS256 = true;
343       else if (Name == "llvm.x86.avx.vpermil.ps")
344         PS128 = true;
345
346       if (PD256 || PD128 || PS256 || PS128) {
347         Value *Op0 = CI->getArgOperand(0);
348         unsigned Imm = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue();
349         SmallVector<Constant*, 8> Idxs;
350
351         if (PD128)
352           for (unsigned i = 0; i != 2; ++i)
353             Idxs.push_back(Builder.getInt32((Imm >> i) & 0x1));
354         else if (PD256)
355           for (unsigned l = 0; l != 4; l+=2)
356             for (unsigned i = 0; i != 2; ++i)
357               Idxs.push_back(Builder.getInt32(((Imm >> (l+i)) & 0x1) + l));
358         else if (PS128)
359           for (unsigned i = 0; i != 4; ++i)
360             Idxs.push_back(Builder.getInt32((Imm >> (2 * i)) & 0x3));
361         else if (PS256)
362           for (unsigned l = 0; l != 8; l+=4)
363             for (unsigned i = 0; i != 4; ++i)
364               Idxs.push_back(Builder.getInt32(((Imm >> (2 * i)) & 0x3) + l));
365         else
366           llvm_unreachable("Unexpected function");
367
368         Rep = Builder.CreateShuffleVector(Op0, Op0, ConstantVector::get(Idxs));
369       } else {
370         llvm_unreachable("Unknown function for CallInst upgrade.");
371       }
372     }
373
374     CI->replaceAllUsesWith(Rep);
375     CI->eraseFromParent();
376     return;
377   }
378
379   std::string Name = CI->getName().str();
380   CI->setName(Name + ".old");
381
382   switch (NewFn->getIntrinsicID()) {
383   default:
384     llvm_unreachable("Unknown function for CallInst upgrade.");
385
386   case Intrinsic::ctlz:
387   case Intrinsic::cttz:
388     assert(CI->getNumArgOperands() == 1 &&
389            "Mismatch between function args and call args");
390     CI->replaceAllUsesWith(Builder.CreateCall2(NewFn, CI->getArgOperand(0),
391                                                Builder.getFalse(), Name));
392     CI->eraseFromParent();
393     return;
394
395   case Intrinsic::objectsize:
396     CI->replaceAllUsesWith(Builder.CreateCall2(NewFn,
397                                                CI->getArgOperand(0),
398                                                CI->getArgOperand(1),
399                                                Name));
400     CI->eraseFromParent();
401     return;
402
403   case Intrinsic::arm_neon_vclz: {
404     // Change name from llvm.arm.neon.vclz.* to llvm.ctlz.*
405     CI->replaceAllUsesWith(Builder.CreateCall2(NewFn, CI->getArgOperand(0),
406                                                Builder.getFalse(),
407                                                "llvm.ctlz." + Name.substr(14)));
408     CI->eraseFromParent();
409     return;
410   }
411   case Intrinsic::ctpop: {
412     CI->replaceAllUsesWith(Builder.CreateCall(NewFn, CI->getArgOperand(0)));
413     CI->eraseFromParent();
414     return;
415   }
416
417   case Intrinsic::x86_xop_vfrcz_ss:
418   case Intrinsic::x86_xop_vfrcz_sd:
419     CI->replaceAllUsesWith(Builder.CreateCall(NewFn, CI->getArgOperand(1),
420                                               Name));
421     CI->eraseFromParent();
422     return;
423
424   case Intrinsic::x86_sse41_ptestc:
425   case Intrinsic::x86_sse41_ptestz:
426   case Intrinsic::x86_sse41_ptestnzc: {
427     // The arguments for these intrinsics used to be v4f32, and changed
428     // to v2i64. This is purely a nop, since those are bitwise intrinsics.
429     // So, the only thing required is a bitcast for both arguments.
430     // First, check the arguments have the old type.
431     Value *Arg0 = CI->getArgOperand(0);
432     if (Arg0->getType() != VectorType::get(Type::getFloatTy(C), 4))
433       return;
434
435     // Old intrinsic, add bitcasts
436     Value *Arg1 = CI->getArgOperand(1);
437
438     Value *BC0 =
439       Builder.CreateBitCast(Arg0,
440                             VectorType::get(Type::getInt64Ty(C), 2),
441                             "cast");
442     Value *BC1 =
443       Builder.CreateBitCast(Arg1,
444                             VectorType::get(Type::getInt64Ty(C), 2),
445                             "cast");
446
447     CallInst* NewCall = Builder.CreateCall2(NewFn, BC0, BC1, Name);
448     CI->replaceAllUsesWith(NewCall);
449     CI->eraseFromParent();
450     return;
451   }
452   }
453 }
454
455 // This tests each Function to determine if it needs upgrading. When we find
456 // one we are interested in, we then upgrade all calls to reflect the new
457 // function.
458 void llvm::UpgradeCallsToIntrinsic(Function* F) {
459   assert(F && "Illegal attempt to upgrade a non-existent intrinsic.");
460
461   // Upgrade the function and check if it is a totaly new function.
462   Function *NewFn;
463   if (UpgradeIntrinsicFunction(F, NewFn)) {
464     if (NewFn != F) {
465       // Replace all uses to the old function with the new one if necessary.
466       for (Value::user_iterator UI = F->user_begin(), UE = F->user_end();
467            UI != UE; ) {
468         if (CallInst *CI = dyn_cast<CallInst>(*UI++))
469           UpgradeIntrinsicCall(CI, NewFn);
470       }
471       // Remove old function, no longer used, from the module.
472       F->eraseFromParent();
473     }
474   }
475 }
476
477 void llvm::UpgradeInstWithTBAATag(Instruction *I) {
478   MDNode *MD = I->getMetadata(LLVMContext::MD_tbaa);
479   assert(MD && "UpgradeInstWithTBAATag should have a TBAA tag");
480   // Check if the tag uses struct-path aware TBAA format.
481   if (isa<MDNode>(MD->getOperand(0)) && MD->getNumOperands() >= 3)
482     return;
483
484   if (MD->getNumOperands() == 3) {
485     Value *Elts[] = {
486       MD->getOperand(0),
487       MD->getOperand(1)
488     };
489     MDNode *ScalarType = MDNode::get(I->getContext(), Elts);
490     // Create a MDNode <ScalarType, ScalarType, offset 0, const>
491     Value *Elts2[] = {
492       ScalarType, ScalarType,
493       Constant::getNullValue(Type::getInt64Ty(I->getContext())),
494       MD->getOperand(2)
495     };
496     I->setMetadata(LLVMContext::MD_tbaa, MDNode::get(I->getContext(), Elts2));
497   } else {
498     // Create a MDNode <MD, MD, offset 0>
499     Value *Elts[] = {MD, MD,
500       Constant::getNullValue(Type::getInt64Ty(I->getContext()))};
501     I->setMetadata(LLVMContext::MD_tbaa, MDNode::get(I->getContext(), Elts));
502   }
503 }
504
505 Instruction *llvm::UpgradeBitCastInst(unsigned Opc, Value *V, Type *DestTy,
506                                       Instruction *&Temp) {
507   if (Opc != Instruction::BitCast)
508     return nullptr;
509
510   Temp = nullptr;
511   Type *SrcTy = V->getType();
512   if (SrcTy->isPtrOrPtrVectorTy() && DestTy->isPtrOrPtrVectorTy() &&
513       SrcTy->getPointerAddressSpace() != DestTy->getPointerAddressSpace()) {
514     LLVMContext &Context = V->getContext();
515
516     // We have no information about target data layout, so we assume that
517     // the maximum pointer size is 64bit.
518     Type *MidTy = Type::getInt64Ty(Context);
519     Temp = CastInst::Create(Instruction::PtrToInt, V, MidTy);
520
521     return CastInst::Create(Instruction::IntToPtr, Temp, DestTy);
522   }
523
524   return nullptr;
525 }
526
527 Value *llvm::UpgradeBitCastExpr(unsigned Opc, Constant *C, Type *DestTy) {
528   if (Opc != Instruction::BitCast)
529     return nullptr;
530
531   Type *SrcTy = C->getType();
532   if (SrcTy->isPtrOrPtrVectorTy() && DestTy->isPtrOrPtrVectorTy() &&
533       SrcTy->getPointerAddressSpace() != DestTy->getPointerAddressSpace()) {
534     LLVMContext &Context = C->getContext();
535
536     // We have no information about target data layout, so we assume that
537     // the maximum pointer size is 64bit.
538     Type *MidTy = Type::getInt64Ty(Context);
539
540     return ConstantExpr::getIntToPtr(ConstantExpr::getPtrToInt(C, MidTy),
541                                      DestTy);
542   }
543
544   return nullptr;
545 }
546
547 /// Check the debug info version number, if it is out-dated, drop the debug
548 /// info. Return true if module is modified.
549 bool llvm::UpgradeDebugInfo(Module &M) {
550   unsigned Version = getDebugMetadataVersionFromModule(M);
551   if (Version == DEBUG_METADATA_VERSION)
552     return false;
553
554   bool RetCode = StripDebugInfo(M);
555   if (RetCode) {
556     DiagnosticInfoDebugMetadataVersion DiagVersion(M, Version);
557     M.getContext().diagnose(DiagVersion);
558   }
559   return RetCode;
560 }