2007-08-04 09:51:18 +08:00
|
|
|
//===-- AutoUpgrade.cpp - Implement auto-upgrade helper functions ---------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 04:36:04 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2007-08-04 09:51:18 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the auto-upgrade helper functions
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/AutoUpgrade.h"
|
2007-12-14 14:38:54 +08:00
|
|
|
#include "llvm/Constants.h"
|
2007-08-04 09:51:18 +08:00
|
|
|
#include "llvm/Function.h"
|
2011-08-26 07:22:40 +08:00
|
|
|
#include "llvm/Instruction.h"
|
2009-07-08 07:43:39 +08:00
|
|
|
#include "llvm/LLVMContext.h"
|
2007-08-04 09:51:18 +08:00
|
|
|
#include "llvm/Module.h"
|
2009-08-29 07:24:31 +08:00
|
|
|
#include "llvm/IntrinsicInst.h"
|
2011-08-26 07:22:40 +08:00
|
|
|
#include "llvm/ADT/DenseMap.h"
|
|
|
|
#include "llvm/ADT/SmallPtrSet.h"
|
2008-03-13 01:45:29 +08:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2010-06-23 16:45:32 +08:00
|
|
|
#include "llvm/Support/CallSite.h"
|
2011-08-26 07:22:40 +08:00
|
|
|
#include "llvm/Support/CFG.h"
|
2009-07-12 04:10:48 +08:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2010-04-20 08:59:54 +08:00
|
|
|
#include "llvm/Support/IRBuilder.h"
|
2008-02-20 19:08:44 +08:00
|
|
|
#include <cstring>
|
2007-08-04 09:51:18 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
|
2007-12-18 06:33:23 +08:00
|
|
|
static bool UpgradeIntrinsicFunction1(Function *F, Function *&NewFn) {
|
2007-08-04 09:51:18 +08:00
|
|
|
assert(F && "Illegal to upgrade a non-existent Function.");
|
|
|
|
|
|
|
|
// Quickly eliminate it, if it's not a candidate.
|
2011-06-19 02:56:39 +08:00
|
|
|
StringRef Name = F->getName();
|
|
|
|
if (Name.size() <= 8 || !Name.startswith("llvm."))
|
2007-12-18 06:33:23 +08:00
|
|
|
return false;
|
2011-06-19 02:56:39 +08:00
|
|
|
Name = Name.substr(5); // Strip off "llvm."
|
2011-11-27 16:42:07 +08:00
|
|
|
|
2011-06-19 02:56:39 +08:00
|
|
|
switch (Name[0]) {
|
2007-08-04 09:51:18 +08:00
|
|
|
default: break;
|
2011-12-12 12:26:04 +08:00
|
|
|
case 'c': {
|
|
|
|
Type *Tys[] = { F->arg_begin()->getType() };
|
|
|
|
if (Name.startswith("ctlz.") && F->arg_size() == 1) {
|
|
|
|
F->setName(Name + ".old");
|
|
|
|
NewFn = Intrinsic::getDeclaration(F->getParent(), Intrinsic::ctlz, Tys);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (Name.startswith("cttz.") && F->arg_size() == 1) {
|
|
|
|
F->setName(Name + ".old");
|
|
|
|
NewFn = Intrinsic::getDeclaration(F->getParent(), Intrinsic::cttz, Tys);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2007-08-04 09:51:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// This may not belong here. This function is effectively being overloaded
|
|
|
|
// to both detect an intrinsic which needs upgrading, and to provide the
|
|
|
|
// upgraded form of the intrinsic. We should perhaps have two separate
|
|
|
|
// functions for this.
|
2007-12-18 06:33:23 +08:00
|
|
|
return false;
|
2007-08-04 09:51:18 +08:00
|
|
|
}
|
|
|
|
|
2007-12-18 06:33:23 +08:00
|
|
|
bool llvm::UpgradeIntrinsicFunction(Function *F, Function *&NewFn) {
|
|
|
|
NewFn = 0;
|
|
|
|
bool Upgraded = UpgradeIntrinsicFunction1(F, NewFn);
|
2007-12-04 04:06:50 +08:00
|
|
|
|
|
|
|
// Upgrade intrinsic attributes. This does not change the function.
|
2007-12-18 06:33:23 +08:00
|
|
|
if (NewFn)
|
|
|
|
F = NewFn;
|
2009-02-05 09:49:45 +08:00
|
|
|
if (unsigned id = F->getIntrinsicID())
|
2008-09-26 05:00:45 +08:00
|
|
|
F->setAttributes(Intrinsic::getAttributes((Intrinsic::ID)id));
|
2007-12-04 04:06:50 +08:00
|
|
|
return Upgraded;
|
|
|
|
}
|
|
|
|
|
2010-09-11 02:51:56 +08:00
|
|
|
bool llvm::UpgradeGlobalVariable(GlobalVariable *GV) {
|
2011-06-18 14:05:24 +08:00
|
|
|
// Nothing to do yet.
|
2010-09-11 02:51:56 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2007-08-04 09:51:18 +08:00
|
|
|
// UpgradeIntrinsicCall - Upgrade a call to an old intrinsic to be a call the
|
|
|
|
// upgraded intrinsic. All argument and return casting must be provided in
|
|
|
|
// order to seamlessly integrate with existing context.
|
|
|
|
void llvm::UpgradeIntrinsicCall(CallInst *CI, Function *NewFn) {
|
|
|
|
Function *F = CI->getCalledFunction();
|
2011-12-12 12:26:04 +08:00
|
|
|
LLVMContext &C = CI->getContext();
|
2010-06-23 16:45:32 +08:00
|
|
|
|
2007-08-04 09:51:18 +08:00
|
|
|
assert(F && "CallInst has no function associated with it.");
|
2007-12-18 06:33:23 +08:00
|
|
|
|
2011-12-12 12:26:04 +08:00
|
|
|
if (!NewFn) return;
|
|
|
|
|
|
|
|
IRBuilder<> Builder(C);
|
|
|
|
Builder.SetInsertPoint(CI->getParent(), CI);
|
|
|
|
|
|
|
|
switch (NewFn->getIntrinsicID()) {
|
|
|
|
default:
|
2011-11-27 16:42:07 +08:00
|
|
|
llvm_unreachable("Unknown function for CallInst upgrade.");
|
2011-12-12 12:26:04 +08:00
|
|
|
|
|
|
|
case Intrinsic::ctlz:
|
|
|
|
case Intrinsic::cttz:
|
|
|
|
assert(CI->getNumArgOperands() == 1 &&
|
|
|
|
"Mismatch between function args and call args");
|
|
|
|
StringRef Name = CI->getName();
|
|
|
|
CI->setName(Name + ".old");
|
|
|
|
CI->replaceAllUsesWith(Builder.CreateCall2(NewFn, CI->getArgOperand(0),
|
|
|
|
Builder.getFalse(), Name));
|
|
|
|
CI->eraseFromParent();
|
|
|
|
return;
|
2007-12-18 06:33:23 +08:00
|
|
|
}
|
2007-08-04 09:51:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// This tests each Function to determine if it needs upgrading. When we find
|
|
|
|
// one we are interested in, we then upgrade all calls to reflect the new
|
|
|
|
// function.
|
|
|
|
void llvm::UpgradeCallsToIntrinsic(Function* F) {
|
|
|
|
assert(F && "Illegal attempt to upgrade a non-existent intrinsic.");
|
|
|
|
|
|
|
|
// Upgrade the function and check if it is a totaly new function.
|
2011-06-18 14:05:24 +08:00
|
|
|
Function *NewFn;
|
2007-12-18 06:33:23 +08:00
|
|
|
if (UpgradeIntrinsicFunction(F, NewFn)) {
|
2007-08-04 09:51:18 +08:00
|
|
|
if (NewFn != F) {
|
|
|
|
// Replace all uses to the old function with the new one if necessary.
|
|
|
|
for (Value::use_iterator UI = F->use_begin(), UE = F->use_end();
|
|
|
|
UI != UE; ) {
|
2011-06-18 14:05:24 +08:00
|
|
|
if (CallInst *CI = dyn_cast<CallInst>(*UI++))
|
2007-08-04 09:51:18 +08:00
|
|
|
UpgradeIntrinsicCall(CI, NewFn);
|
|
|
|
}
|
|
|
|
// Remove old function, no longer used, from the module.
|
|
|
|
F->eraseFromParent();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-08-29 07:24:31 +08:00
|
|
|
|