The llvm.atomic intrinsics *were* removed in LLVM 3.0 (in r141333), remove the
[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 'm':
44     if (Name == "memory.barrier")
45       return true;
46     break;
47   }
48
49   //  This may not belong here. This function is effectively being overloaded 
50   //  to both detect an intrinsic which needs upgrading, and to provide the 
51   //  upgraded form of the intrinsic. We should perhaps have two separate 
52   //  functions for this.
53   return false;
54 }
55
56 bool llvm::UpgradeIntrinsicFunction(Function *F, Function *&NewFn) {
57   NewFn = 0;
58   bool Upgraded = UpgradeIntrinsicFunction1(F, NewFn);
59
60   // Upgrade intrinsic attributes.  This does not change the function.
61   if (NewFn)
62     F = NewFn;
63   if (unsigned id = F->getIntrinsicID())
64     F->setAttributes(Intrinsic::getAttributes((Intrinsic::ID)id));
65   return Upgraded;
66 }
67
68 bool llvm::UpgradeGlobalVariable(GlobalVariable *GV) {
69   // Nothing to do yet.
70   return false;
71 }
72
73 // UpgradeIntrinsicCall - Upgrade a call to an old intrinsic to be a call the 
74 // upgraded intrinsic. All argument and return casting must be provided in 
75 // order to seamlessly integrate with existing context.
76 void llvm::UpgradeIntrinsicCall(CallInst *CI, Function *NewFn) {
77   Function *F = CI->getCalledFunction();
78   LLVMContext &C = CI->getContext();
79   ImmutableCallSite CS(CI);
80
81   assert(F && "CallInst has no function associated with it.");
82
83   if (!NewFn) {
84     if (F->getName() == "llvm.memory.barrier") {
85       IRBuilder<> Builder(C);
86       Builder.SetInsertPoint(CI->getParent(), CI);
87
88       // Note that this conversion ignores the "device" bit; it was not really
89       // well-defined, and got abused because nobody paid enough attention to
90       // get it right. In practice, this probably doesn't matter; application
91       // code generally doesn't need anything stronger than
92       // SequentiallyConsistent (and realistically, SequentiallyConsistent
93       // is lowered to a strong enough barrier for almost anything).
94
95       if (cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue())
96         Builder.CreateFence(SequentiallyConsistent);
97       else if (!cast<ConstantInt>(CI->getArgOperand(0))->getZExtValue())
98         Builder.CreateFence(Release);
99       else if (!cast<ConstantInt>(CI->getArgOperand(3))->getZExtValue())
100         Builder.CreateFence(Acquire);
101       else
102         Builder.CreateFence(AcquireRelease);
103
104       // Remove intrinsic.
105       CI->eraseFromParent();
106     } else {
107       llvm_unreachable("Unknown function for CallInst upgrade.");
108     }
109     return;
110   }
111 }
112
113 // This tests each Function to determine if it needs upgrading. When we find 
114 // one we are interested in, we then upgrade all calls to reflect the new 
115 // function.
116 void llvm::UpgradeCallsToIntrinsic(Function* F) {
117   assert(F && "Illegal attempt to upgrade a non-existent intrinsic.");
118
119   // Upgrade the function and check if it is a totaly new function.
120   Function *NewFn;
121   if (UpgradeIntrinsicFunction(F, NewFn)) {
122     if (NewFn != F) {
123       // Replace all uses to the old function with the new one if necessary.
124       for (Value::use_iterator UI = F->use_begin(), UE = F->use_end();
125            UI != UE; ) {
126         if (CallInst *CI = dyn_cast<CallInst>(*UI++))
127           UpgradeIntrinsicCall(CI, NewFn);
128       }
129       // Remove old function, no longer used, from the module.
130       F->eraseFromParent();
131     }
132   }
133 }
134