Various crash reporting tools have a problem with the dwarf generated for
[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   switch (Name[0]) {
42   default: break;
43   case 'c': {
44     if (Name.startswith("ctlz.") && F->arg_size() == 1) {
45       F->setName(Name + ".old");
46       NewFn = Intrinsic::getDeclaration(F->getParent(), Intrinsic::ctlz,
47                                         F->arg_begin()->getType());
48       return true;
49     }
50     if (Name.startswith("cttz.") && F->arg_size() == 1) {
51       F->setName(Name + ".old");
52       NewFn = Intrinsic::getDeclaration(F->getParent(), Intrinsic::cttz,
53                                         F->arg_begin()->getType());
54       return true;
55     }
56     break;
57   }
58   }
59
60   //  This may not belong here. This function is effectively being overloaded 
61   //  to both detect an intrinsic which needs upgrading, and to provide the 
62   //  upgraded form of the intrinsic. We should perhaps have two separate 
63   //  functions for this.
64   return false;
65 }
66
67 bool llvm::UpgradeIntrinsicFunction(Function *F, Function *&NewFn) {
68   NewFn = 0;
69   bool Upgraded = UpgradeIntrinsicFunction1(F, NewFn);
70
71   // Upgrade intrinsic attributes.  This does not change the function.
72   if (NewFn)
73     F = NewFn;
74   if (unsigned id = F->getIntrinsicID())
75     F->setAttributes(Intrinsic::getAttributes((Intrinsic::ID)id));
76   return Upgraded;
77 }
78
79 bool llvm::UpgradeGlobalVariable(GlobalVariable *GV) {
80   // Nothing to do yet.
81   return false;
82 }
83
84 // UpgradeIntrinsicCall - Upgrade a call to an old intrinsic to be a call the 
85 // upgraded intrinsic. All argument and return casting must be provided in 
86 // order to seamlessly integrate with existing context.
87 void llvm::UpgradeIntrinsicCall(CallInst *CI, Function *NewFn) {
88   assert(CI->getCalledFunction() && "Intrinsic call is not direct?");
89   if (!NewFn) return;
90
91   LLVMContext &C = CI->getContext();
92   IRBuilder<> Builder(C);
93   Builder.SetInsertPoint(CI->getParent(), CI);
94
95   switch (NewFn->getIntrinsicID()) {
96   default:
97     llvm_unreachable("Unknown function for CallInst upgrade.");
98
99   case Intrinsic::ctlz:
100   case Intrinsic::cttz:
101     assert(CI->getNumArgOperands() == 1 &&
102            "Mismatch between function args and call args");
103     StringRef Name = CI->getName();
104     CI->setName(Name + ".old");
105     CI->replaceAllUsesWith(Builder.CreateCall2(NewFn, CI->getArgOperand(0),
106                                                Builder.getFalse(), Name));
107     CI->eraseFromParent();
108     return;
109   }
110 }
111
112 // This tests each Function to determine if it needs upgrading. When we find 
113 // one we are interested in, we then upgrade all calls to reflect the new 
114 // function.
115 void llvm::UpgradeCallsToIntrinsic(Function* F) {
116   assert(F && "Illegal attempt to upgrade a non-existent intrinsic.");
117
118   // Upgrade the function and check if it is a totaly new function.
119   Function *NewFn;
120   if (UpgradeIntrinsicFunction(F, NewFn)) {
121     if (NewFn != F) {
122       // Replace all uses to the old function with the new one if necessary.
123       for (Value::use_iterator UI = F->use_begin(), UE = F->use_end();
124            UI != UE; ) {
125         if (CallInst *CI = dyn_cast<CallInst>(*UI++))
126           UpgradeIntrinsicCall(CI, NewFn);
127       }
128       // Remove old function, no longer used, from the module.
129       F->eraseFromParent();
130     }
131   }
132 }
133