Eli managed to kill off llvm.membarrier in llvm 3.0 also, this means
[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   // SOMEDAY: Add some.
44   }
45
46   //  This may not belong here. This function is effectively being overloaded 
47   //  to both detect an intrinsic which needs upgrading, and to provide the 
48   //  upgraded form of the intrinsic. We should perhaps have two separate 
49   //  functions for this.
50   return false;
51 }
52
53 bool llvm::UpgradeIntrinsicFunction(Function *F, Function *&NewFn) {
54   NewFn = 0;
55   bool Upgraded = UpgradeIntrinsicFunction1(F, NewFn);
56
57   // Upgrade intrinsic attributes.  This does not change the function.
58   if (NewFn)
59     F = NewFn;
60   if (unsigned id = F->getIntrinsicID())
61     F->setAttributes(Intrinsic::getAttributes((Intrinsic::ID)id));
62   return Upgraded;
63 }
64
65 bool llvm::UpgradeGlobalVariable(GlobalVariable *GV) {
66   // Nothing to do yet.
67   return false;
68 }
69
70 // UpgradeIntrinsicCall - Upgrade a call to an old intrinsic to be a call the 
71 // upgraded intrinsic. All argument and return casting must be provided in 
72 // order to seamlessly integrate with existing context.
73 void llvm::UpgradeIntrinsicCall(CallInst *CI, Function *NewFn) {
74   Function *F = CI->getCalledFunction();
75
76   assert(F && "CallInst has no function associated with it.");
77
78   if (NewFn) return;
79   
80   if (F->getName() == "llvm.something eventually") {
81     // UPGRADE HERE.
82   } else {
83     llvm_unreachable("Unknown function for CallInst upgrade.");
84   }
85 }
86
87 // This tests each Function to determine if it needs upgrading. When we find 
88 // one we are interested in, we then upgrade all calls to reflect the new 
89 // function.
90 void llvm::UpgradeCallsToIntrinsic(Function* F) {
91   assert(F && "Illegal attempt to upgrade a non-existent intrinsic.");
92
93   // Upgrade the function and check if it is a totaly new function.
94   Function *NewFn;
95   if (UpgradeIntrinsicFunction(F, NewFn)) {
96     if (NewFn != F) {
97       // Replace all uses to the old function with the new one if necessary.
98       for (Value::use_iterator UI = F->use_begin(), UE = F->use_end();
99            UI != UE; ) {
100         if (CallInst *CI = dyn_cast<CallInst>(*UI++))
101           UpgradeIntrinsicCall(CI, NewFn);
102       }
103       // Remove old function, no longer used, from the module.
104       F->eraseFromParent();
105     }
106   }
107 }
108