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 Type *Tys[] = { F->arg_begin()->getType() };
45 if (Name.startswith("ctlz.") && F->arg_size() == 1) {
46 F->setName(Name + ".old");
47 NewFn = Intrinsic::getDeclaration(F->getParent(), Intrinsic::ctlz, Tys);
50 if (Name.startswith("cttz.") && F->arg_size() == 1) {
51 F->setName(Name + ".old");
52 NewFn = Intrinsic::getDeclaration(F->getParent(), Intrinsic::cttz, Tys);
59 // This may not belong here. This function is effectively being overloaded
60 // to both detect an intrinsic which needs upgrading, and to provide the
61 // upgraded form of the intrinsic. We should perhaps have two separate
62 // functions for this.
66 bool llvm::UpgradeIntrinsicFunction(Function *F, Function *&NewFn) {
68 bool Upgraded = UpgradeIntrinsicFunction1(F, NewFn);
70 // Upgrade intrinsic attributes. This does not change the function.
73 if (unsigned id = F->getIntrinsicID())
74 F->setAttributes(Intrinsic::getAttributes((Intrinsic::ID)id));
78 bool llvm::UpgradeGlobalVariable(GlobalVariable *GV) {
83 // UpgradeIntrinsicCall - Upgrade a call to an old intrinsic to be a call the
84 // upgraded intrinsic. All argument and return casting must be provided in
85 // order to seamlessly integrate with existing context.
86 void llvm::UpgradeIntrinsicCall(CallInst *CI, Function *NewFn) {
87 Function *F = CI->getCalledFunction();
88 LLVMContext &C = CI->getContext();
90 assert(F && "CallInst has no function associated with it.");
94 IRBuilder<> Builder(C);
95 Builder.SetInsertPoint(CI->getParent(), CI);
97 switch (NewFn->getIntrinsicID()) {
99 llvm_unreachable("Unknown function for CallInst upgrade.");
101 case Intrinsic::ctlz:
102 case Intrinsic::cttz:
103 assert(CI->getNumArgOperands() == 1 &&
104 "Mismatch between function args and call args");
105 StringRef Name = CI->getName();
106 CI->setName(Name + ".old");
107 CI->replaceAllUsesWith(Builder.CreateCall2(NewFn, CI->getArgOperand(0),
108 Builder.getFalse(), Name));
109 CI->eraseFromParent();
114 // This tests each Function to determine if it needs upgrading. When we find
115 // one we are interested in, we then upgrade all calls to reflect the new
117 void llvm::UpgradeCallsToIntrinsic(Function* F) {
118 assert(F && "Illegal attempt to upgrade a non-existent intrinsic.");
120 // Upgrade the function and check if it is a totaly new function.
122 if (UpgradeIntrinsicFunction(F, NewFn)) {
124 // Replace all uses to the old function with the new one if necessary.
125 for (Value::use_iterator UI = F->use_begin(), UE = F->use_end();
127 if (CallInst *CI = dyn_cast<CallInst>(*UI++))
128 UpgradeIntrinsicCall(CI, NewFn);
130 // Remove old function, no longer used, from the module.
131 F->eraseFromParent();