Don't upgrade global constructors when reading bitcode
[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 == "x86.avx.vbroadcast.ss" ||
118         Name == "x86.avx.vbroadcast.ss.256" ||
119         Name == "x86.avx.vbroadcast.sd.256" ||
120         (Name.startswith("x86.xop.vpcom") && F->arg_size() == 2)) {
121       NewFn = nullptr;
122       return true;
123     }
124     // SSE4.1 ptest functions may have an old signature.
125     if (Name.startswith("x86.sse41.ptest")) {
126       if (Name == "x86.sse41.ptestc")
127         return UpgradeSSE41Function(F, Intrinsic::x86_sse41_ptestc, NewFn);
128       if (Name == "x86.sse41.ptestz")
129         return UpgradeSSE41Function(F, Intrinsic::x86_sse41_ptestz, NewFn);
130       if (Name == "x86.sse41.ptestnzc")
131         return UpgradeSSE41Function(F, Intrinsic::x86_sse41_ptestnzc, NewFn);
132     }
133     // frcz.ss/sd may need to have an argument dropped
134     if (Name.startswith("x86.xop.vfrcz.ss") && F->arg_size() == 2) {
135       F->setName(Name + ".old");
136       NewFn = Intrinsic::getDeclaration(F->getParent(),
137                                         Intrinsic::x86_xop_vfrcz_ss);
138       return true;
139     }
140     if (Name.startswith("x86.xop.vfrcz.sd") && F->arg_size() == 2) {
141       F->setName(Name + ".old");
142       NewFn = Intrinsic::getDeclaration(F->getParent(),
143                                         Intrinsic::x86_xop_vfrcz_sd);
144       return true;
145     }
146     // Fix the FMA4 intrinsics to remove the 4
147     if (Name.startswith("x86.fma4.")) {
148       F->setName("llvm.x86.fma" + Name.substr(8));
149       NewFn = F;
150       return true;
151     }
152     break;
153   }
154   }
155
156   //  This may not belong here. This function is effectively being overloaded
157   //  to both detect an intrinsic which needs upgrading, and to provide the
158   //  upgraded form of the intrinsic. We should perhaps have two separate
159   //  functions for this.
160   return false;
161 }
162
163 bool llvm::UpgradeIntrinsicFunction(Function *F, Function *&NewFn) {
164   NewFn = nullptr;
165   bool Upgraded = UpgradeIntrinsicFunction1(F, NewFn);
166
167   // Upgrade intrinsic attributes.  This does not change the function.
168   if (NewFn)
169     F = NewFn;
170   if (unsigned id = F->getIntrinsicID())
171     F->setAttributes(Intrinsic::getAttributes(F->getContext(),
172                                               (Intrinsic::ID)id));
173   return Upgraded;
174 }
175
176 bool llvm::UpgradeGlobalVariable(GlobalVariable *GV) {
177   // Nothing to do yet.
178   return false;
179 }
180
181 // UpgradeIntrinsicCall - Upgrade a call to an old intrinsic to be a call the
182 // upgraded intrinsic. All argument and return casting must be provided in
183 // order to seamlessly integrate with existing context.
184 void llvm::UpgradeIntrinsicCall(CallInst *CI, Function *NewFn) {
185   Function *F = CI->getCalledFunction();
186   LLVMContext &C = CI->getContext();
187   IRBuilder<> Builder(C);
188   Builder.SetInsertPoint(CI->getParent(), CI);
189
190   assert(F && "Intrinsic call is not direct?");
191
192   if (!NewFn) {
193     // Get the Function's name.
194     StringRef Name = F->getName();
195
196     Value *Rep;
197     // Upgrade packed integer vector compares intrinsics to compare instructions
198     if (Name.startswith("llvm.x86.sse2.pcmpeq.") ||
199         Name.startswith("llvm.x86.avx2.pcmpeq.")) {
200       Rep = Builder.CreateICmpEQ(CI->getArgOperand(0), CI->getArgOperand(1),
201                                  "pcmpeq");
202       // need to sign extend since icmp returns vector of i1
203       Rep = Builder.CreateSExt(Rep, CI->getType(), "");
204     } else if (Name.startswith("llvm.x86.sse2.pcmpgt.") ||
205                Name.startswith("llvm.x86.avx2.pcmpgt.")) {
206       Rep = Builder.CreateICmpSGT(CI->getArgOperand(0), CI->getArgOperand(1),
207                                   "pcmpgt");
208       // need to sign extend since icmp returns vector of i1
209       Rep = Builder.CreateSExt(Rep, CI->getType(), "");
210     } else if (Name == "llvm.x86.avx.movnt.dq.256" ||
211                Name == "llvm.x86.avx.movnt.ps.256" ||
212                Name == "llvm.x86.avx.movnt.pd.256") {
213       IRBuilder<> Builder(C);
214       Builder.SetInsertPoint(CI->getParent(), CI);
215
216       Module *M = F->getParent();
217       SmallVector<Value *, 1> Elts;
218       Elts.push_back(ConstantInt::get(Type::getInt32Ty(C), 1));
219       MDNode *Node = MDNode::get(C, Elts);
220
221       Value *Arg0 = CI->getArgOperand(0);
222       Value *Arg1 = CI->getArgOperand(1);
223
224       // Convert the type of the pointer to a pointer to the stored type.
225       Value *BC = Builder.CreateBitCast(Arg0,
226                                         PointerType::getUnqual(Arg1->getType()),
227                                         "cast");
228       StoreInst *SI = Builder.CreateStore(Arg1, BC);
229       SI->setMetadata(M->getMDKindID("nontemporal"), Node);
230       SI->setAlignment(16);
231
232       // Remove intrinsic.
233       CI->eraseFromParent();
234       return;
235     } else if (Name.startswith("llvm.x86.xop.vpcom")) {
236       Intrinsic::ID intID;
237       if (Name.endswith("ub"))
238         intID = Intrinsic::x86_xop_vpcomub;
239       else if (Name.endswith("uw"))
240         intID = Intrinsic::x86_xop_vpcomuw;
241       else if (Name.endswith("ud"))
242         intID = Intrinsic::x86_xop_vpcomud;
243       else if (Name.endswith("uq"))
244         intID = Intrinsic::x86_xop_vpcomuq;
245       else if (Name.endswith("b"))
246         intID = Intrinsic::x86_xop_vpcomb;
247       else if (Name.endswith("w"))
248         intID = Intrinsic::x86_xop_vpcomw;
249       else if (Name.endswith("d"))
250         intID = Intrinsic::x86_xop_vpcomd;
251       else if (Name.endswith("q"))
252         intID = Intrinsic::x86_xop_vpcomq;
253       else
254         llvm_unreachable("Unknown suffix");
255
256       Name = Name.substr(18); // strip off "llvm.x86.xop.vpcom"
257       unsigned Imm;
258       if (Name.startswith("lt"))
259         Imm = 0;
260       else if (Name.startswith("le"))
261         Imm = 1;
262       else if (Name.startswith("gt"))
263         Imm = 2;
264       else if (Name.startswith("ge"))
265         Imm = 3;
266       else if (Name.startswith("eq"))
267         Imm = 4;
268       else if (Name.startswith("ne"))
269         Imm = 5;
270       else if (Name.startswith("true"))
271         Imm = 6;
272       else if (Name.startswith("false"))
273         Imm = 7;
274       else
275         llvm_unreachable("Unknown condition");
276
277       Function *VPCOM = Intrinsic::getDeclaration(F->getParent(), intID);
278       Rep = Builder.CreateCall3(VPCOM, CI->getArgOperand(0),
279                                 CI->getArgOperand(1), Builder.getInt8(Imm));
280     } else if (Name == "llvm.x86.sse42.crc32.64.8") {
281       Function *CRC32 = Intrinsic::getDeclaration(F->getParent(),
282                                                Intrinsic::x86_sse42_crc32_32_8);
283       Value *Trunc0 = Builder.CreateTrunc(CI->getArgOperand(0), Type::getInt32Ty(C));
284       Rep = Builder.CreateCall2(CRC32, Trunc0, CI->getArgOperand(1));
285       Rep = Builder.CreateZExt(Rep, CI->getType(), "");
286     } else if (Name.startswith("llvm.x86.avx.vbroadcast")) {
287       // Replace broadcasts with a series of insertelements.
288       Type *VecTy = CI->getType();
289       Type *EltTy = VecTy->getVectorElementType();
290       unsigned EltNum = VecTy->getVectorNumElements();
291       Value *Cast = Builder.CreateBitCast(CI->getArgOperand(0),
292                                           EltTy->getPointerTo());
293       Value *Load = Builder.CreateLoad(Cast);
294       Type *I32Ty = Type::getInt32Ty(C);
295       Rep = UndefValue::get(VecTy);
296       for (unsigned I = 0; I < EltNum; ++I)
297         Rep = Builder.CreateInsertElement(Rep, Load,
298                                           ConstantInt::get(I32Ty, I));
299     } else {
300       bool PD128 = false, PD256 = false, PS128 = false, PS256 = false;
301       if (Name == "llvm.x86.avx.vpermil.pd.256")
302         PD256 = true;
303       else if (Name == "llvm.x86.avx.vpermil.pd")
304         PD128 = true;
305       else if (Name == "llvm.x86.avx.vpermil.ps.256")
306         PS256 = true;
307       else if (Name == "llvm.x86.avx.vpermil.ps")
308         PS128 = true;
309
310       if (PD256 || PD128 || PS256 || PS128) {
311         Value *Op0 = CI->getArgOperand(0);
312         unsigned Imm = cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue();
313         SmallVector<Constant*, 8> Idxs;
314
315         if (PD128)
316           for (unsigned i = 0; i != 2; ++i)
317             Idxs.push_back(Builder.getInt32((Imm >> i) & 0x1));
318         else if (PD256)
319           for (unsigned l = 0; l != 4; l+=2)
320             for (unsigned i = 0; i != 2; ++i)
321               Idxs.push_back(Builder.getInt32(((Imm >> (l+i)) & 0x1) + l));
322         else if (PS128)
323           for (unsigned i = 0; i != 4; ++i)
324             Idxs.push_back(Builder.getInt32((Imm >> (2 * i)) & 0x3));
325         else if (PS256)
326           for (unsigned l = 0; l != 8; l+=4)
327             for (unsigned i = 0; i != 4; ++i)
328               Idxs.push_back(Builder.getInt32(((Imm >> (2 * i)) & 0x3) + l));
329         else
330           llvm_unreachable("Unexpected function");
331
332         Rep = Builder.CreateShuffleVector(Op0, Op0, ConstantVector::get(Idxs));
333       } else {
334         llvm_unreachable("Unknown function for CallInst upgrade.");
335       }
336     }
337
338     CI->replaceAllUsesWith(Rep);
339     CI->eraseFromParent();
340     return;
341   }
342
343   std::string Name = CI->getName().str();
344   CI->setName(Name + ".old");
345
346   switch (NewFn->getIntrinsicID()) {
347   default:
348     llvm_unreachable("Unknown function for CallInst upgrade.");
349
350   case Intrinsic::ctlz:
351   case Intrinsic::cttz:
352     assert(CI->getNumArgOperands() == 1 &&
353            "Mismatch between function args and call args");
354     CI->replaceAllUsesWith(Builder.CreateCall2(NewFn, CI->getArgOperand(0),
355                                                Builder.getFalse(), Name));
356     CI->eraseFromParent();
357     return;
358
359   case Intrinsic::objectsize:
360     CI->replaceAllUsesWith(Builder.CreateCall2(NewFn,
361                                                CI->getArgOperand(0),
362                                                CI->getArgOperand(1),
363                                                Name));
364     CI->eraseFromParent();
365     return;
366
367   case Intrinsic::arm_neon_vclz: {
368     // Change name from llvm.arm.neon.vclz.* to llvm.ctlz.*
369     CI->replaceAllUsesWith(Builder.CreateCall2(NewFn, CI->getArgOperand(0),
370                                                Builder.getFalse(),
371                                                "llvm.ctlz." + Name.substr(14)));
372     CI->eraseFromParent();
373     return;
374   }
375   case Intrinsic::ctpop: {
376     CI->replaceAllUsesWith(Builder.CreateCall(NewFn, CI->getArgOperand(0)));
377     CI->eraseFromParent();
378     return;
379   }
380
381   case Intrinsic::x86_xop_vfrcz_ss:
382   case Intrinsic::x86_xop_vfrcz_sd:
383     CI->replaceAllUsesWith(Builder.CreateCall(NewFn, CI->getArgOperand(1),
384                                               Name));
385     CI->eraseFromParent();
386     return;
387
388   case Intrinsic::x86_sse41_ptestc:
389   case Intrinsic::x86_sse41_ptestz:
390   case Intrinsic::x86_sse41_ptestnzc: {
391     // The arguments for these intrinsics used to be v4f32, and changed
392     // to v2i64. This is purely a nop, since those are bitwise intrinsics.
393     // So, the only thing required is a bitcast for both arguments.
394     // First, check the arguments have the old type.
395     Value *Arg0 = CI->getArgOperand(0);
396     if (Arg0->getType() != VectorType::get(Type::getFloatTy(C), 4))
397       return;
398
399     // Old intrinsic, add bitcasts
400     Value *Arg1 = CI->getArgOperand(1);
401
402     Value *BC0 =
403       Builder.CreateBitCast(Arg0,
404                             VectorType::get(Type::getInt64Ty(C), 2),
405                             "cast");
406     Value *BC1 =
407       Builder.CreateBitCast(Arg1,
408                             VectorType::get(Type::getInt64Ty(C), 2),
409                             "cast");
410
411     CallInst* NewCall = Builder.CreateCall2(NewFn, BC0, BC1, Name);
412     CI->replaceAllUsesWith(NewCall);
413     CI->eraseFromParent();
414     return;
415   }
416   }
417 }
418
419 // This tests each Function to determine if it needs upgrading. When we find
420 // one we are interested in, we then upgrade all calls to reflect the new
421 // function.
422 void llvm::UpgradeCallsToIntrinsic(Function* F) {
423   assert(F && "Illegal attempt to upgrade a non-existent intrinsic.");
424
425   // Upgrade the function and check if it is a totaly new function.
426   Function *NewFn;
427   if (UpgradeIntrinsicFunction(F, NewFn)) {
428     if (NewFn != F) {
429       // Replace all uses to the old function with the new one if necessary.
430       for (Value::user_iterator UI = F->user_begin(), UE = F->user_end();
431            UI != UE; ) {
432         if (CallInst *CI = dyn_cast<CallInst>(*UI++))
433           UpgradeIntrinsicCall(CI, NewFn);
434       }
435       // Remove old function, no longer used, from the module.
436       F->eraseFromParent();
437     }
438   }
439 }
440
441 void llvm::UpgradeInstWithTBAATag(Instruction *I) {
442   MDNode *MD = I->getMetadata(LLVMContext::MD_tbaa);
443   assert(MD && "UpgradeInstWithTBAATag should have a TBAA tag");
444   // Check if the tag uses struct-path aware TBAA format.
445   if (isa<MDNode>(MD->getOperand(0)) && MD->getNumOperands() >= 3)
446     return;
447
448   if (MD->getNumOperands() == 3) {
449     Value *Elts[] = {
450       MD->getOperand(0),
451       MD->getOperand(1)
452     };
453     MDNode *ScalarType = MDNode::get(I->getContext(), Elts);
454     // Create a MDNode <ScalarType, ScalarType, offset 0, const>
455     Value *Elts2[] = {
456       ScalarType, ScalarType,
457       Constant::getNullValue(Type::getInt64Ty(I->getContext())),
458       MD->getOperand(2)
459     };
460     I->setMetadata(LLVMContext::MD_tbaa, MDNode::get(I->getContext(), Elts2));
461   } else {
462     // Create a MDNode <MD, MD, offset 0>
463     Value *Elts[] = {MD, MD,
464       Constant::getNullValue(Type::getInt64Ty(I->getContext()))};
465     I->setMetadata(LLVMContext::MD_tbaa, MDNode::get(I->getContext(), Elts));
466   }
467 }
468
469 Instruction *llvm::UpgradeBitCastInst(unsigned Opc, Value *V, Type *DestTy,
470                                       Instruction *&Temp) {
471   if (Opc != Instruction::BitCast)
472     return nullptr;
473
474   Temp = nullptr;
475   Type *SrcTy = V->getType();
476   if (SrcTy->isPtrOrPtrVectorTy() && DestTy->isPtrOrPtrVectorTy() &&
477       SrcTy->getPointerAddressSpace() != DestTy->getPointerAddressSpace()) {
478     LLVMContext &Context = V->getContext();
479
480     // We have no information about target data layout, so we assume that
481     // the maximum pointer size is 64bit.
482     Type *MidTy = Type::getInt64Ty(Context);
483     Temp = CastInst::Create(Instruction::PtrToInt, V, MidTy);
484
485     return CastInst::Create(Instruction::IntToPtr, Temp, DestTy);
486   }
487
488   return nullptr;
489 }
490
491 Value *llvm::UpgradeBitCastExpr(unsigned Opc, Constant *C, Type *DestTy) {
492   if (Opc != Instruction::BitCast)
493     return nullptr;
494
495   Type *SrcTy = C->getType();
496   if (SrcTy->isPtrOrPtrVectorTy() && DestTy->isPtrOrPtrVectorTy() &&
497       SrcTy->getPointerAddressSpace() != DestTy->getPointerAddressSpace()) {
498     LLVMContext &Context = C->getContext();
499
500     // We have no information about target data layout, so we assume that
501     // the maximum pointer size is 64bit.
502     Type *MidTy = Type::getInt64Ty(Context);
503
504     return ConstantExpr::getIntToPtr(ConstantExpr::getPtrToInt(C, MidTy),
505                                      DestTy);
506   }
507
508   return nullptr;
509 }
510
511 /// Check the debug info version number, if it is out-dated, drop the debug
512 /// info. Return true if module is modified.
513 bool llvm::UpgradeDebugInfo(Module &M) {
514   unsigned Version = getDebugMetadataVersionFromModule(M);
515   if (Version == DEBUG_METADATA_VERSION)
516     return false;
517
518   bool RetCode = StripDebugInfo(M);
519   if (RetCode) {
520     DiagnosticInfoDebugMetadataVersion DiagVersion(M, Version);
521     M.getContext().diagnose(DiagVersion);
522   }
523   return RetCode;
524 }
525
526 void llvm::UpgradeMDStringConstant(std::string &String) {
527   const std::string OldPrefix = "llvm.vectorizer.";
528   if (String == "llvm.vectorizer.unroll") {
529     String = "llvm.loop.interleave.count";
530   } else if (String.find(OldPrefix) == 0) {
531     String.replace(0, OldPrefix.size(), "llvm.loop.vectorize.");
532   }
533 }