7b0b34ed89f1e2698e9e21a46cfd9c6ce26962b6
[oota-llvm.git] / lib / VMCore / 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/AutoUpgrade.h"
15 #include "llvm/Constants.h"
16 #include "llvm/Function.h"
17 #include "llvm/Instruction.h"
18 #include "llvm/LLVMContext.h"
19 #include "llvm/Module.h"
20 #include "llvm/IntrinsicInst.h"
21 #include "llvm/ADT/DenseMap.h"
22 #include "llvm/ADT/SmallPtrSet.h"
23 #include "llvm/ADT/SmallVector.h"
24 #include "llvm/Support/CallSite.h"
25 #include "llvm/Support/CFG.h"
26 #include "llvm/Support/ErrorHandling.h"
27 #include "llvm/Support/IRBuilder.h"
28 #include <cstring>
29 using namespace llvm;
30
31
32 static bool UpgradeIntrinsicFunction1(Function *F, Function *&NewFn) {
33   assert(F && "Illegal to upgrade a non-existent Function.");
34
35   // Quickly eliminate it, if it's not a candidate.
36   StringRef Name = F->getName();
37   if (Name.size() <= 8 || !Name.startswith("llvm."))
38     return false;
39   Name = Name.substr(5); // Strip off "llvm."
40
41   FunctionType *FTy = F->getFunctionType();
42   Module *M = F->getParent();
43   
44   switch (Name[0]) {
45   default: break;
46   case 'p':
47     //  This upgrades the llvm.prefetch intrinsic to accept one more parameter,
48     //  which is a instruction / data cache identifier. The old version only
49     //  implicitly accepted the data version.
50     if (Name == "prefetch") {
51       // Don't do anything if it has the correct number of arguments already
52       if (FTy->getNumParams() == 4)
53         break;
54
55       assert(FTy->getNumParams() == 3 && "old prefetch takes 3 args!");
56       //  We first need to change the name of the old (bad) intrinsic, because
57       //  its type is incorrect, but we cannot overload that name. We
58       //  arbitrarily unique it here allowing us to construct a correctly named
59       //  and typed function below.
60       std::string NameTmp = F->getName();
61       F->setName("");
62       NewFn = cast<Function>(M->getOrInsertFunction(NameTmp,
63                                                     FTy->getReturnType(),
64                                                     FTy->getParamType(0),
65                                                     FTy->getParamType(1),
66                                                     FTy->getParamType(2),
67                                                     FTy->getParamType(2),
68                                                     (Type*)0));
69       return true;
70     }
71
72     break;
73   case 'x': {
74     const char *NewFnName = NULL;
75     // This fixes the poorly named crc32 intrinsics.
76     if (Name == "x86.sse42.crc32.8")
77       NewFnName = "llvm.x86.sse42.crc32.32.8";
78     else if (Name == "x86.sse42.crc32.16")
79       NewFnName = "llvm.x86.sse42.crc32.32.16";
80     else if (Name == "x86.sse42.crc32.32")
81       NewFnName = "llvm.x86.sse42.crc32.32.32";
82     else if (Name == "x86.sse42.crc64.8")
83       NewFnName = "llvm.x86.sse42.crc32.64.8";
84     else if (Name == "x86.sse42.crc64.64")
85       NewFnName = "llvm.x86.sse42.crc32.64.64";
86     
87     if (NewFnName) {
88       F->setName(NewFnName);
89       NewFn = F;
90       return true;
91     }
92
93     // Calls to these instructions are transformed into unaligned loads.
94     if (Name == "x86.sse.loadu.ps" || Name == "x86.sse2.loadu.dq" ||
95         Name == "x86.sse2.loadu.pd")
96       return true;
97       
98     // Calls to these instructions are transformed into nontemporal stores.
99     if (Name == "x86.sse.movnt.ps"  || Name == "x86.sse2.movnt.dq" ||
100         Name == "x86.sse2.movnt.pd" || Name == "x86.sse2.movnt.i")
101       return true;
102
103     break;
104   }
105   }
106
107   //  This may not belong here. This function is effectively being overloaded 
108   //  to both detect an intrinsic which needs upgrading, and to provide the 
109   //  upgraded form of the intrinsic. We should perhaps have two separate 
110   //  functions for this.
111   return false;
112 }
113
114 bool llvm::UpgradeIntrinsicFunction(Function *F, Function *&NewFn) {
115   NewFn = 0;
116   bool Upgraded = UpgradeIntrinsicFunction1(F, NewFn);
117
118   // Upgrade intrinsic attributes.  This does not change the function.
119   if (NewFn)
120     F = NewFn;
121   if (unsigned id = F->getIntrinsicID())
122     F->setAttributes(Intrinsic::getAttributes((Intrinsic::ID)id));
123   return Upgraded;
124 }
125
126 bool llvm::UpgradeGlobalVariable(GlobalVariable *GV) {
127   // Nothing to do yet.
128   return false;
129 }
130
131 // UpgradeIntrinsicCall - Upgrade a call to an old intrinsic to be a call the 
132 // upgraded intrinsic. All argument and return casting must be provided in 
133 // order to seamlessly integrate with existing context.
134 void llvm::UpgradeIntrinsicCall(CallInst *CI, Function *NewFn) {
135   Function *F = CI->getCalledFunction();
136   LLVMContext &C = CI->getContext();
137   ImmutableCallSite CS(CI);
138
139   assert(F && "CallInst has no function associated with it.");
140
141   if (!NewFn) {
142     if (F->getName() == "llvm.x86.sse.loadu.ps" ||
143         F->getName() == "llvm.x86.sse2.loadu.dq" ||
144         F->getName() == "llvm.x86.sse2.loadu.pd") {
145       // Convert to a native, unaligned load.
146       Type *VecTy = CI->getType();
147       Type *IntTy = IntegerType::get(C, 128);
148       IRBuilder<> Builder(C);
149       Builder.SetInsertPoint(CI->getParent(), CI);
150
151       Value *BC = Builder.CreateBitCast(CI->getArgOperand(0),
152                                         PointerType::getUnqual(IntTy),
153                                         "cast");
154       LoadInst *LI = Builder.CreateLoad(BC, CI->getName());
155       LI->setAlignment(1);      // Unaligned load.
156       BC = Builder.CreateBitCast(LI, VecTy, "new.cast");
157
158       // Fix up all the uses with our new load.
159       if (!CI->use_empty())
160         CI->replaceAllUsesWith(BC);
161
162       // Remove intrinsic.
163       CI->eraseFromParent();
164     } else if (F->getName() == "llvm.x86.sse.movnt.ps" ||
165                F->getName() == "llvm.x86.sse2.movnt.dq" ||
166                F->getName() == "llvm.x86.sse2.movnt.pd" ||
167                F->getName() == "llvm.x86.sse2.movnt.i") {
168       IRBuilder<> Builder(C);
169       Builder.SetInsertPoint(CI->getParent(), CI);
170
171       Module *M = F->getParent();
172       SmallVector<Value *, 1> Elts;
173       Elts.push_back(ConstantInt::get(Type::getInt32Ty(C), 1));
174       MDNode *Node = MDNode::get(C, Elts);
175
176       Value *Arg0 = CI->getArgOperand(0);
177       Value *Arg1 = CI->getArgOperand(1);
178
179       // Convert the type of the pointer to a pointer to the stored type.
180       Value *BC = Builder.CreateBitCast(Arg0,
181                                         PointerType::getUnqual(Arg1->getType()),
182                                         "cast");
183       StoreInst *SI = Builder.CreateStore(Arg1, BC);
184       SI->setMetadata(M->getMDKindID("nontemporal"), Node);
185       SI->setAlignment(16);
186
187       // Remove intrinsic.
188       CI->eraseFromParent();
189     } else {
190       llvm_unreachable("Unknown function for CallInst upgrade.");
191     }
192     return;
193   }
194
195   switch (NewFn->getIntrinsicID()) {
196   case Intrinsic::prefetch: {
197     IRBuilder<> Builder(C);
198     Builder.SetInsertPoint(CI->getParent(), CI);
199     llvm::Type *I32Ty = llvm::Type::getInt32Ty(CI->getContext());
200
201     // Add the extra "data cache" argument
202     Value *Operands[4] = { CI->getArgOperand(0), CI->getArgOperand(1),
203                            CI->getArgOperand(2),
204                            llvm::ConstantInt::get(I32Ty, 1) };
205     CallInst *NewCI = CallInst::Create(NewFn, Operands,
206                                        CI->getName(), CI);
207     NewCI->setTailCall(CI->isTailCall());
208     NewCI->setCallingConv(CI->getCallingConv());
209     //  Handle any uses of the old CallInst.
210     if (!CI->use_empty())
211       //  Replace all uses of the old call with the new cast which has the
212       //  correct type.
213       CI->replaceAllUsesWith(NewCI);
214
215     //  Clean up the old call now that it has been completely upgraded.
216     CI->eraseFromParent();
217     break;
218   }
219   }
220 }
221
222 // This tests each Function to determine if it needs upgrading. When we find 
223 // one we are interested in, we then upgrade all calls to reflect the new 
224 // function.
225 void llvm::UpgradeCallsToIntrinsic(Function* F) {
226   assert(F && "Illegal attempt to upgrade a non-existent intrinsic.");
227
228   // Upgrade the function and check if it is a totaly new function.
229   Function *NewFn;
230   if (UpgradeIntrinsicFunction(F, NewFn)) {
231     if (NewFn != F) {
232       // Replace all uses to the old function with the new one if necessary.
233       for (Value::use_iterator UI = F->use_begin(), UE = F->use_end();
234            UI != UE; ) {
235         if (CallInst *CI = dyn_cast<CallInst>(*UI++))
236           UpgradeIntrinsicCall(CI, NewFn);
237       }
238       // Remove old function, no longer used, from the module.
239       F->eraseFromParent();
240     }
241   }
242 }
243
244 /// This function strips all debug info intrinsics, except for llvm.dbg.declare.
245 /// If an llvm.dbg.declare intrinsic is invalid, then this function simply
246 /// strips that use.
247 void llvm::CheckDebugInfoIntrinsics(Module *M) {
248   if (Function *FuncStart = M->getFunction("llvm.dbg.func.start")) {
249     while (!FuncStart->use_empty())
250       cast<CallInst>(FuncStart->use_back())->eraseFromParent();
251     FuncStart->eraseFromParent();
252   }
253   
254   if (Function *StopPoint = M->getFunction("llvm.dbg.stoppoint")) {
255     while (!StopPoint->use_empty())
256       cast<CallInst>(StopPoint->use_back())->eraseFromParent();
257     StopPoint->eraseFromParent();
258   }
259
260   if (Function *RegionStart = M->getFunction("llvm.dbg.region.start")) {
261     while (!RegionStart->use_empty())
262       cast<CallInst>(RegionStart->use_back())->eraseFromParent();
263     RegionStart->eraseFromParent();
264   }
265
266   if (Function *RegionEnd = M->getFunction("llvm.dbg.region.end")) {
267     while (!RegionEnd->use_empty())
268       cast<CallInst>(RegionEnd->use_back())->eraseFromParent();
269     RegionEnd->eraseFromParent();
270   }
271   
272   if (Function *Declare = M->getFunction("llvm.dbg.declare")) {
273     if (!Declare->use_empty()) {
274       DbgDeclareInst *DDI = cast<DbgDeclareInst>(Declare->use_back());
275       if (!isa<MDNode>(DDI->getArgOperand(0)) ||
276           !isa<MDNode>(DDI->getArgOperand(1))) {
277         while (!Declare->use_empty()) {
278           CallInst *CI = cast<CallInst>(Declare->use_back());
279           CI->eraseFromParent();
280         }
281         Declare->eraseFromParent();
282       }
283     }
284   }
285 }
286
287 /// FindExnAndSelIntrinsics - Find the eh_exception and eh_selector intrinsic
288 /// calls reachable from the unwind basic block.
289 static void FindExnAndSelIntrinsics(BasicBlock *BB, CallInst *&Exn,
290                                     CallInst *&Sel,
291                                     SmallPtrSet<BasicBlock*, 8> &Visited) {
292   if (!Visited.insert(BB)) return;
293
294   for (BasicBlock::iterator
295          I = BB->begin(), E = BB->end(); I != E; ++I) {
296     if (CallInst *CI = dyn_cast<CallInst>(I)) {
297       switch (CI->getCalledFunction()->getIntrinsicID()) {
298       default: break;
299       case Intrinsic::eh_exception:
300         assert(!Exn && "Found more than one eh.exception call!");
301         Exn = CI;
302         break;
303       case Intrinsic::eh_selector:
304         assert(!Sel && "Found more than one eh.selector call!");
305         Sel = CI;
306         break;
307       }
308
309       if (Exn && Sel) return;
310     }
311   }
312
313   if (Exn && Sel) return;
314
315   for (succ_iterator I = succ_begin(BB), E = succ_end(BB); I != E; ++I) {
316     FindExnAndSelIntrinsics(*I, Exn, Sel, Visited);
317     if (Exn && Sel) return;
318   }
319 }
320
321 /// TransferClausesToLandingPadInst - Transfer the exception handling clauses
322 /// from the eh_selector call to the new landingpad instruction.
323 static void TransferClausesToLandingPadInst(LandingPadInst *LPI,
324                                             CallInst *EHSel) {
325   LLVMContext &Context = LPI->getContext();
326   unsigned N = EHSel->getNumArgOperands();
327
328   for (unsigned i = N - 1; i > 1; --i) {
329     if (const ConstantInt *CI = dyn_cast<ConstantInt>(EHSel->getArgOperand(i))){
330       unsigned FilterLength = CI->getZExtValue();
331       unsigned FirstCatch = i + FilterLength + !FilterLength;
332       assert(FirstCatch <= N && "Invalid filter length");
333
334       if (FirstCatch < N)
335         for (unsigned j = FirstCatch; j < N; ++j) {
336           Value *Val = EHSel->getArgOperand(j);
337           if (!Val->hasName() || Val->getName() != "llvm.eh.catch.all.value") {
338             LPI->addClause(EHSel->getArgOperand(j));
339           } else {
340             GlobalVariable *GV = cast<GlobalVariable>(Val);
341             LPI->addClause(GV->getInitializer());
342           }
343         }
344
345       if (!FilterLength) {
346         // Cleanup.
347         LPI->setCleanup(true);
348       } else {
349         // Filter.
350         SmallVector<Constant *, 4> TyInfo;
351         TyInfo.reserve(FilterLength - 1);
352         for (unsigned j = i + 1; j < FirstCatch; ++j)
353           TyInfo.push_back(cast<Constant>(EHSel->getArgOperand(j)));
354         ArrayType *AType =
355           ArrayType::get(!TyInfo.empty() ? TyInfo[0]->getType() :
356                          PointerType::getUnqual(Type::getInt8Ty(Context)),
357                          TyInfo.size());
358         LPI->addClause(ConstantArray::get(AType, TyInfo));
359       }
360
361       N = i;
362     }
363   }
364
365   if (N > 2)
366     for (unsigned j = 2; j < N; ++j) {
367       Value *Val = EHSel->getArgOperand(j);
368       if (!Val->hasName() || Val->getName() != "llvm.eh.catch.all.value") {
369         LPI->addClause(EHSel->getArgOperand(j));
370       } else {
371         GlobalVariable *GV = cast<GlobalVariable>(Val);
372         LPI->addClause(GV->getInitializer());
373       }
374     }
375 }
376
377 /// This function upgrades the old pre-3.0 exception handling system to the new
378 /// one. N.B. This will be removed in 3.1.
379 void llvm::UpgradeExceptionHandling(Module *M) {
380   Function *EHException = M->getFunction("llvm.eh.exception");
381   Function *EHSelector = M->getFunction("llvm.eh.selector");
382   if (!EHException || !EHSelector)
383     return;
384
385   LLVMContext &Context = M->getContext();
386   Type *ExnTy = PointerType::getUnqual(Type::getInt8Ty(Context));
387   Type *SelTy = Type::getInt32Ty(Context);
388   Type *LPadSlotTy = StructType::get(ExnTy, SelTy, NULL);
389
390   // This map stores the slots where the exception object and selector value are
391   // stored within a function.
392   SmallPtrSet<Instruction*, 32> DeadInsts;
393   DenseMap<Function*, std::pair<Value*, Value*> > FnToLPadSlotMap;
394   for (Module::iterator
395          I = M->begin(), E = M->end(); I != E; ++I) {
396     Function &F = *I;
397
398     for (Function::iterator
399            II = F.begin(), IE = F.end(); II != IE; ++II) {
400       BasicBlock *BB = &*II;
401       InvokeInst *Inst = dyn_cast<InvokeInst>(BB->getTerminator());
402       if (!Inst) continue;
403       BasicBlock *UnwindDest = Inst->getUnwindDest();
404       if (UnwindDest->isLandingPad()) continue; // All ready converted.
405
406       // Store the exception object and selector value in the entry block.
407       Value *ExnSlot = 0;
408       Value *SelSlot = 0;
409       if (!FnToLPadSlotMap[&F].first) {
410         BasicBlock *Entry = &F.front();
411         ExnSlot = new AllocaInst(ExnTy, "exn", Entry->getTerminator());
412         SelSlot = new AllocaInst(SelTy, "sel", Entry->getTerminator());
413         FnToLPadSlotMap[&F] = std::make_pair(ExnSlot, SelSlot);
414       } else {
415         ExnSlot = FnToLPadSlotMap[&F].first;
416         SelSlot = FnToLPadSlotMap[&F].second;
417       }
418
419       // We're in an unwind block. Try to find the eh.exception and eh.selector
420       // calls.
421       IRBuilder<> Builder(Context);
422       Builder.SetInsertPoint(UnwindDest, UnwindDest->getFirstNonPHI());
423
424       SmallPtrSet<BasicBlock*, 8> Visited;
425       CallInst *Exn = 0;
426       CallInst *Sel = 0;
427       FindExnAndSelIntrinsics(UnwindDest, Exn, Sel, Visited);
428       assert(Exn && Sel && "Cannot find eh.exception and eh.selector calls!");
429
430       Value *PersFn = Sel->getArgOperand(1);
431       LandingPadInst *LPI = Builder.CreateLandingPad(LPadSlotTy, PersFn, 0);
432       Value *LPExn = Builder.CreateExtractValue(LPI, 0);
433       Value *LPSel = Builder.CreateExtractValue(LPI, 1);
434       Builder.CreateStore(LPExn, ExnSlot);
435       Builder.CreateStore(LPSel, SelSlot);
436
437       TransferClausesToLandingPadInst(LPI, Sel);
438
439       Exn->replaceAllUsesWith(LPExn);
440       Sel->replaceAllUsesWith(LPSel);
441
442       DeadInsts.insert(Exn);
443       DeadInsts.insert(Sel);
444     }
445   }
446
447   // Remove the dead instructions.
448   for (SmallPtrSet<Instruction*, 32>::iterator
449          I = DeadInsts.begin(), E = DeadInsts.end(); I != E; ++I) {
450     Instruction *Inst = *I;
451     Inst->eraseFromParent();
452   }
453
454   // Replace calls to "llvm.eh.resume" with the 'resume' instruction. Load the
455   // exception and selector values from the stored place.
456   Function *EHResume = M->getFunction("llvm.eh.resume");
457   if (!EHResume) return;
458
459   while (!EHResume->use_empty()) {
460     CallInst *Resume = cast<CallInst>(EHResume->use_back());
461     BasicBlock *BB = Resume->getParent();
462     Function *Fn = BB->getParent();
463     std::pair<Value*, Value*> &ExnSel = FnToLPadSlotMap[Fn];
464     IRBuilder<> Builder(Context);
465     Builder.SetInsertPoint(BB, Resume);
466
467     Value *Exn = Builder.CreateLoad(ExnSel.first, "exn");
468     Value *Sel = Builder.CreateLoad(ExnSel.second, "sel");
469
470     Value *LPadVal =
471       Builder.CreateInsertValue(UndefValue::get(LPadSlotTy),
472                                 Exn, 0, "lpad.val");
473     LPadVal = Builder.CreateInsertValue(LPadVal, Sel, 1, "lpad.val");
474     Builder.CreateResume(LPadVal);
475
476     // Remove all instructions after the 'resume.'
477     BasicBlock::iterator I = Resume;
478     while (I != BB->end()) {
479       Instruction *Inst = &*I++;
480       Inst->eraseFromParent();
481     }
482   }
483 }