2007-08-04 01:51:18 +00:00
|
|
|
//===-- AutoUpgrade.cpp - Implement auto-upgrade helper functions ---------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 20:36:04 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2007-08-04 01:51:18 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the auto-upgrade helper functions
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/AutoUpgrade.h"
|
2007-12-14 06:38:54 +00:00
|
|
|
#include "llvm/Constants.h"
|
2007-08-04 01:51:18 +00:00
|
|
|
#include "llvm/Function.h"
|
2011-08-25 23:22:40 +00:00
|
|
|
#include "llvm/Instruction.h"
|
2009-07-07 23:43:39 +00:00
|
|
|
#include "llvm/LLVMContext.h"
|
2007-08-04 01:51:18 +00:00
|
|
|
#include "llvm/Module.h"
|
2009-08-28 23:24:31 +00:00
|
|
|
#include "llvm/IntrinsicInst.h"
|
2011-08-25 23:22:40 +00:00
|
|
|
#include "llvm/ADT/DenseMap.h"
|
|
|
|
#include "llvm/ADT/SmallPtrSet.h"
|
2008-03-12 17:45:29 +00:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2010-06-23 08:45:32 +00:00
|
|
|
#include "llvm/Support/CallSite.h"
|
2011-08-25 23:22:40 +00:00
|
|
|
#include "llvm/Support/CFG.h"
|
2009-07-11 20:10:48 +00:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2010-04-20 00:59:54 +00:00
|
|
|
#include "llvm/Support/IRBuilder.h"
|
2008-02-20 11:08:44 +00:00
|
|
|
#include <cstring>
|
2007-08-04 01:51:18 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
|
2007-12-17 22:33:23 +00:00
|
|
|
static bool UpgradeIntrinsicFunction1(Function *F, Function *&NewFn) {
|
2007-08-04 01:51:18 +00:00
|
|
|
assert(F && "Illegal to upgrade a non-existent Function.");
|
|
|
|
|
|
|
|
// Quickly eliminate it, if it's not a candidate.
|
2011-06-18 18:56:39 +00:00
|
|
|
StringRef Name = F->getName();
|
|
|
|
if (Name.size() <= 8 || !Name.startswith("llvm."))
|
2007-12-17 22:33:23 +00:00
|
|
|
return false;
|
2011-06-18 18:56:39 +00:00
|
|
|
Name = Name.substr(5); // Strip off "llvm."
|
2011-11-27 08:42:07 +00:00
|
|
|
|
2011-06-18 18:56:39 +00:00
|
|
|
switch (Name[0]) {
|
2007-08-04 01:51:18 +00:00
|
|
|
default: break;
|
2011-12-12 04:26:04 +00:00
|
|
|
case 'c': {
|
|
|
|
if (Name.startswith("ctlz.") && F->arg_size() == 1) {
|
|
|
|
F->setName(Name + ".old");
|
2011-12-12 10:57:20 +00:00
|
|
|
NewFn = Intrinsic::getDeclaration(F->getParent(), Intrinsic::ctlz,
|
|
|
|
F->arg_begin()->getType());
|
2011-12-12 04:26:04 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (Name.startswith("cttz.") && F->arg_size() == 1) {
|
|
|
|
F->setName(Name + ".old");
|
2011-12-12 10:57:20 +00:00
|
|
|
NewFn = Intrinsic::getDeclaration(F->getParent(), Intrinsic::cttz,
|
|
|
|
F->arg_begin()->getType());
|
2011-12-12 04:26:04 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2007-08-04 01:51:18 +00: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-17 22:33:23 +00:00
|
|
|
return false;
|
2007-08-04 01:51:18 +00:00
|
|
|
}
|
|
|
|
|
2007-12-17 22:33:23 +00:00
|
|
|
bool llvm::UpgradeIntrinsicFunction(Function *F, Function *&NewFn) {
|
|
|
|
NewFn = 0;
|
|
|
|
bool Upgraded = UpgradeIntrinsicFunction1(F, NewFn);
|
2007-12-03 20:06:50 +00:00
|
|
|
|
|
|
|
// Upgrade intrinsic attributes. This does not change the function.
|
2007-12-17 22:33:23 +00:00
|
|
|
if (NewFn)
|
|
|
|
F = NewFn;
|
2009-02-05 01:49:45 +00:00
|
|
|
if (unsigned id = F->getIntrinsicID())
|
2008-09-25 21:00:45 +00:00
|
|
|
F->setAttributes(Intrinsic::getAttributes((Intrinsic::ID)id));
|
2007-12-03 20:06:50 +00:00
|
|
|
return Upgraded;
|
|
|
|
}
|
|
|
|
|
2010-09-10 18:51:56 +00:00
|
|
|
bool llvm::UpgradeGlobalVariable(GlobalVariable *GV) {
|
2011-06-18 06:05:24 +00:00
|
|
|
// Nothing to do yet.
|
2010-09-10 18:51:56 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2007-08-04 01:51:18 +00: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) {
|
2011-12-12 22:59:34 +00:00
|
|
|
assert(CI->getCalledFunction() && "Intrinsic call is not direct?");
|
2011-12-12 04:26:04 +00:00
|
|
|
if (!NewFn) return;
|
|
|
|
|
2011-12-12 22:59:34 +00:00
|
|
|
LLVMContext &C = CI->getContext();
|
2011-12-12 04:26:04 +00:00
|
|
|
IRBuilder<> Builder(C);
|
|
|
|
Builder.SetInsertPoint(CI->getParent(), CI);
|
|
|
|
|
|
|
|
switch (NewFn->getIntrinsicID()) {
|
|
|
|
default:
|
2011-11-27 08:42:07 +00:00
|
|
|
llvm_unreachable("Unknown function for CallInst upgrade.");
|
2011-12-12 04:26:04 +00: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-17 22:33:23 +00:00
|
|
|
}
|
2007-08-04 01:51:18 +00: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 06:05:24 +00:00
|
|
|
Function *NewFn;
|
2007-12-17 22:33:23 +00:00
|
|
|
if (UpgradeIntrinsicFunction(F, NewFn)) {
|
2007-08-04 01:51:18 +00: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 06:05:24 +00:00
|
|
|
if (CallInst *CI = dyn_cast<CallInst>(*UI++))
|
2007-08-04 01:51:18 +00:00
|
|
|
UpgradeIntrinsicCall(CI, NewFn);
|
|
|
|
}
|
|
|
|
// Remove old function, no longer used, from the module.
|
|
|
|
F->eraseFromParent();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-08-28 23:24:31 +00:00
|
|
|
|