1 //===-- AutoUpgrade.cpp - Implement auto-upgrade helper functions ---------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the auto-upgrade helper functions
12 //===----------------------------------------------------------------------===//
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"
32 static bool UpgradeIntrinsicFunction1(Function *F, Function *&NewFn) {
33 assert(F && "Illegal to upgrade a non-existent Function.");
35 // Quickly eliminate it, if it's not a candidate.
36 StringRef Name = F->getName();
37 if (Name.size() <= 8 || !Name.startswith("llvm."))
39 Name = Name.substr(5); // Strip off "llvm."
44 if (Name.startswith("atomic.cmp.swap") ||
45 Name.startswith("atomic.swap") ||
46 Name.startswith("atomic.load.add") ||
47 Name.startswith("atomic.load.sub") ||
48 Name.startswith("atomic.load.and") ||
49 Name.startswith("atomic.load.nand") ||
50 Name.startswith("atomic.load.or") ||
51 Name.startswith("atomic.load.xor") ||
52 Name.startswith("atomic.load.max") ||
53 Name.startswith("atomic.load.min") ||
54 Name.startswith("atomic.load.umax") ||
55 Name.startswith("atomic.load.umin"))
59 if (Name == "memory.barrier")
64 // This may not belong here. This function is effectively being overloaded
65 // to both detect an intrinsic which needs upgrading, and to provide the
66 // upgraded form of the intrinsic. We should perhaps have two separate
67 // functions for this.
71 bool llvm::UpgradeIntrinsicFunction(Function *F, Function *&NewFn) {
73 bool Upgraded = UpgradeIntrinsicFunction1(F, NewFn);
75 // Upgrade intrinsic attributes. This does not change the function.
78 if (unsigned id = F->getIntrinsicID())
79 F->setAttributes(Intrinsic::getAttributes((Intrinsic::ID)id));
83 bool llvm::UpgradeGlobalVariable(GlobalVariable *GV) {
88 // UpgradeIntrinsicCall - Upgrade a call to an old intrinsic to be a call the
89 // upgraded intrinsic. All argument and return casting must be provided in
90 // order to seamlessly integrate with existing context.
91 void llvm::UpgradeIntrinsicCall(CallInst *CI, Function *NewFn) {
92 Function *F = CI->getCalledFunction();
93 LLVMContext &C = CI->getContext();
94 ImmutableCallSite CS(CI);
96 assert(F && "CallInst has no function associated with it.");
99 if (F->getName().startswith("llvm.atomic.cmp.swap")) {
100 IRBuilder<> Builder(C);
101 Builder.SetInsertPoint(CI->getParent(), CI);
102 Value *Val = Builder.CreateAtomicCmpXchg(CI->getArgOperand(0),
103 CI->getArgOperand(1),
104 CI->getArgOperand(2),
107 // Replace intrinsic.
109 if (!CI->use_empty())
110 CI->replaceAllUsesWith(Val);
111 CI->eraseFromParent();
112 } else if (F->getName().startswith("llvm.atomic")) {
113 IRBuilder<> Builder(C);
114 Builder.SetInsertPoint(CI->getParent(), CI);
116 AtomicRMWInst::BinOp Op;
117 if (F->getName().startswith("llvm.atomic.swap"))
118 Op = AtomicRMWInst::Xchg;
119 else if (F->getName().startswith("llvm.atomic.load.add"))
120 Op = AtomicRMWInst::Add;
121 else if (F->getName().startswith("llvm.atomic.load.sub"))
122 Op = AtomicRMWInst::Sub;
123 else if (F->getName().startswith("llvm.atomic.load.and"))
124 Op = AtomicRMWInst::And;
125 else if (F->getName().startswith("llvm.atomic.load.nand"))
126 Op = AtomicRMWInst::Nand;
127 else if (F->getName().startswith("llvm.atomic.load.or"))
128 Op = AtomicRMWInst::Or;
129 else if (F->getName().startswith("llvm.atomic.load.xor"))
130 Op = AtomicRMWInst::Xor;
131 else if (F->getName().startswith("llvm.atomic.load.max"))
132 Op = AtomicRMWInst::Max;
133 else if (F->getName().startswith("llvm.atomic.load.min"))
134 Op = AtomicRMWInst::Min;
135 else if (F->getName().startswith("llvm.atomic.load.umax"))
136 Op = AtomicRMWInst::UMax;
137 else if (F->getName().startswith("llvm.atomic.load.umin"))
138 Op = AtomicRMWInst::UMin;
140 llvm_unreachable("Unknown atomic");
142 Value *Val = Builder.CreateAtomicRMW(Op, CI->getArgOperand(0),
143 CI->getArgOperand(1),
146 // Replace intrinsic.
148 if (!CI->use_empty())
149 CI->replaceAllUsesWith(Val);
150 CI->eraseFromParent();
151 } else if (F->getName() == "llvm.memory.barrier") {
152 IRBuilder<> Builder(C);
153 Builder.SetInsertPoint(CI->getParent(), CI);
155 // Note that this conversion ignores the "device" bit; it was not really
156 // well-defined, and got abused because nobody paid enough attention to
157 // get it right. In practice, this probably doesn't matter; application
158 // code generally doesn't need anything stronger than
159 // SequentiallyConsistent (and realistically, SequentiallyConsistent
160 // is lowered to a strong enough barrier for almost anything).
162 if (cast<ConstantInt>(CI->getArgOperand(1))->getZExtValue())
163 Builder.CreateFence(SequentiallyConsistent);
164 else if (!cast<ConstantInt>(CI->getArgOperand(0))->getZExtValue())
165 Builder.CreateFence(Release);
166 else if (!cast<ConstantInt>(CI->getArgOperand(3))->getZExtValue())
167 Builder.CreateFence(Acquire);
169 Builder.CreateFence(AcquireRelease);
172 CI->eraseFromParent();
174 llvm_unreachable("Unknown function for CallInst upgrade.");
180 // This tests each Function to determine if it needs upgrading. When we find
181 // one we are interested in, we then upgrade all calls to reflect the new
183 void llvm::UpgradeCallsToIntrinsic(Function* F) {
184 assert(F && "Illegal attempt to upgrade a non-existent intrinsic.");
186 // Upgrade the function and check if it is a totaly new function.
188 if (UpgradeIntrinsicFunction(F, NewFn)) {
190 // Replace all uses to the old function with the new one if necessary.
191 for (Value::use_iterator UI = F->use_begin(), UE = F->use_end();
193 if (CallInst *CI = dyn_cast<CallInst>(*UI++))
194 UpgradeIntrinsicCall(CI, NewFn);
196 // Remove old function, no longer used, from the module.
197 F->eraseFromParent();