//
// The LLVM Compiler Infrastructure
//
-// This file was developed by Reid Spencer and is distributed under the
-// University of Illinois Open Source License. See LICENSE.TXT for details.
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
//
//===----------------------------------------------------------------------===//
-#include "llvm/Assembly/AutoUpgrade.h"
+#include "llvm/AutoUpgrade.h"
#include "llvm/Constants.h"
-#include "llvm/DerivedTypes.h"
#include "llvm/Function.h"
+#include "llvm/LLVMContext.h"
#include "llvm/Module.h"
-#include "llvm/Instructions.h"
-#include "llvm/Intrinsics.h"
-#include "llvm/SymbolTable.h"
-#include <iostream>
+#include "llvm/IntrinsicInst.h"
+#include "llvm/ADT/SmallVector.h"
+#include "llvm/Support/CallSite.h"
+#include "llvm/Support/ErrorHandling.h"
+#include "llvm/Support/IRBuilder.h"
+#include <cstring>
using namespace llvm;
-static Function *getUpgradedUnaryFn(Function *F) {
- const std::string &Name = F->getName();
- Module *M = F->getParent();
- switch (F->getReturnType()->getTypeID()) {
- default: return 0;
- case Type::UByteTyID:
- case Type::SByteTyID:
- return M->getOrInsertFunction(Name+".i8",
- Type::UByteTy, Type::UByteTy, NULL);
- case Type::UShortTyID:
- case Type::ShortTyID:
- return M->getOrInsertFunction(Name+".i16",
- Type::UShortTy, Type::UShortTy, NULL);
- case Type::UIntTyID:
- case Type::IntTyID:
- return M->getOrInsertFunction(Name+".i32",
- Type::UIntTy, Type::UIntTy, NULL);
- case Type::ULongTyID:
- case Type::LongTyID:
- return M->getOrInsertFunction(Name+".i64",
- Type::ULongTy, Type::ULongTy, NULL);
- case Type::FloatTyID:
- return M->getOrInsertFunction(Name+".f32",
- Type::FloatTy, Type::FloatTy, NULL);
- case Type::DoubleTyID:
- return M->getOrInsertFunction(Name+".f64",
- Type::DoubleTy, Type::DoubleTy, NULL);
- }
-}
-static Function *getUpgradedIntrinsic(Function *F) {
- // If there's no function, we can't get the argument type.
- if (!F) return 0;
-
- // Get the Function's name.
- const std::string& Name = F->getName();
+static bool UpgradeIntrinsicFunction1(Function *F, Function *&NewFn) {
+ assert(F && "Illegal to upgrade a non-existent Function.");
// Quickly eliminate it, if it's not a candidate.
- if (Name.length() <= 8 || Name[0] != 'l' || Name[1] != 'l' ||
- Name[2] != 'v' || Name[3] != 'm' || Name[4] != '.')
- return 0;
+ StringRef Name = F->getName();
+ if (Name.size() <= 8 || !Name.startswith("llvm."))
+ return false;
+ Name = Name.substr(5); // Strip off "llvm."
+ FunctionType *FTy = F->getFunctionType();
Module *M = F->getParent();
- switch (Name[5]) {
+
+ switch (Name[0]) {
default: break;
- case 'b':
- if (Name == "llvm.bswap") return getUpgradedUnaryFn(F);
- break;
- case 'c':
- if (Name == "llvm.ctpop" || Name == "llvm.ctlz" || Name == "llvm.cttz")
- return getUpgradedUnaryFn(F);
- break;
- case 'd':
- if (Name == "llvm.dbg.stoppoint") {
- PointerType *ESP =
- PointerType::get(StructType::get(std::vector<const Type*>()));
- if (F->getReturnType() != Type::VoidTy ||
- F->getFunctionType()->getParamType(2) != ESP) {
- return M->getOrInsertFunction(Name, Type::VoidTy,
- Type::UIntTy, Type::UIntTy, ESP, NULL);
- }
- } else if (Name == "llvm.dbg.func.start") {
- PointerType *ESP =
- PointerType::get(StructType::get(std::vector<const Type*>()));
- if (F->getReturnType() != Type::VoidTy ||
- F->getFunctionType()->getParamType(0) != ESP) {
- return M->getOrInsertFunction(Name, Type::VoidTy, ESP, NULL);
- }
- } else if (Name == "llvm.dbg.region.start") {
- PointerType *ESP =
- PointerType::get(StructType::get(std::vector<const Type*>()));
- if (F->getReturnType() != Type::VoidTy ||
- F->getFunctionType()->getParamType(0) != ESP) {
- return M->getOrInsertFunction(Name, Type::VoidTy, ESP, NULL);
- }
- } else if (Name == "llvm.dbg.region.end") {
- PointerType *ESP =
- PointerType::get(StructType::get(std::vector<const Type*>()));
- if (F->getReturnType() != Type::VoidTy ||
- F->getFunctionType()->getParamType(0) != ESP) {
- return M->getOrInsertFunction(Name, Type::VoidTy, ESP, NULL);
- }
- } else if (Name == "llvm.dbg.declare") {
- PointerType *ESP =
- PointerType::get(StructType::get(std::vector<const Type*>()));
- if (F->getReturnType() != Type::VoidTy ||
- F->getFunctionType()->getParamType(0) != ESP ||
- F->getFunctionType()->getParamType(1) != ESP) {
- return M->getOrInsertFunction(Name, Type::VoidTy, ESP, ESP, NULL);
- }
- }
- break;
- case 'i':
- if (Name == "llvm.isunordered" && F->arg_begin() != F->arg_end()) {
- if (F->arg_begin()->getType() == Type::FloatTy)
- return M->getOrInsertFunction(Name+".f32", F->getFunctionType());
- if (F->arg_begin()->getType() == Type::DoubleTy)
- return M->getOrInsertFunction(Name+".f64", F->getFunctionType());
+ case 'p':
+ // This upgrades the llvm.prefetch intrinsic to accept one more parameter,
+ // which is a instruction / data cache identifier. The old version only
+ // implicitly accepted the data version.
+ if (Name == "prefetch") {
+ // Don't do anything if it has the correct number of arguments already
+ if (FTy->getNumParams() == 4)
+ break;
+
+ assert(FTy->getNumParams() == 3 && "old prefetch takes 3 args!");
+ // We first need to change the name of the old (bad) intrinsic, because
+ // its type is incorrect, but we cannot overload that name. We
+ // arbitrarily unique it here allowing us to construct a correctly named
+ // and typed function below.
+ std::string NameTmp = F->getName();
+ F->setName("");
+ NewFn = cast<Function>(M->getOrInsertFunction(NameTmp,
+ FTy->getReturnType(),
+ FTy->getParamType(0),
+ FTy->getParamType(1),
+ FTy->getParamType(2),
+ FTy->getParamType(2),
+ (Type*)0));
+ return true;
}
+
break;
- case 'm':
- if (Name == "llvm.memcpy" || Name == "llvm.memset" ||
- Name == "llvm.memmove") {
- if (F->getFunctionType()->getParamType(2) == Type::UIntTy ||
- F->getFunctionType()->getParamType(2) == Type::IntTy)
- return M->getOrInsertFunction(Name+".i32", Type::VoidTy,
- PointerType::get(Type::SByteTy),
- F->getFunctionType()->getParamType(1),
- Type::UIntTy, Type::UIntTy, NULL);
- if (F->getFunctionType()->getParamType(2) == Type::ULongTy ||
- F->getFunctionType()->getParamType(2) == Type::LongTy)
- return M->getOrInsertFunction(Name+".i64", Type::VoidTy,
- PointerType::get(Type::SByteTy),
- F->getFunctionType()->getParamType(1),
- Type::ULongTy, Type::UIntTy, NULL);
+ case 'x': {
+ const char *NewFnName = NULL;
+ // This fixes the poorly named crc32 intrinsics.
+ if (Name == "x86.sse42.crc32.8")
+ NewFnName = "llvm.x86.sse42.crc32.32.8";
+ else if (Name == "x86.sse42.crc32.16")
+ NewFnName = "llvm.x86.sse42.crc32.32.16";
+ else if (Name == "x86.sse42.crc32.32")
+ NewFnName = "llvm.x86.sse42.crc32.32.32";
+ else if (Name == "x86.sse42.crc64.8")
+ NewFnName = "llvm.x86.sse42.crc32.64.8";
+ else if (Name == "x86.sse42.crc64.64")
+ NewFnName = "llvm.x86.sse42.crc32.64.64";
+
+ if (NewFnName) {
+ F->setName(NewFnName);
+ NewFn = F;
+ return true;
}
- break;
- case 's':
- if (Name == "llvm.sqrt")
- return getUpgradedUnaryFn(F);
- break;
- }
- return 0;
-}
-// Occasionally upgraded function call site arguments need to be permutated to
-// some new order. The result of getArgumentPermutation is an array of size
-// F->getFunctionType()getNumParams() indicating the new operand order. A value
-// of zero in the array indicates replacing with UndefValue for the arg type.
-// NULL is returned if there is no permutation. It's assumed that the function
-// name is in the form "llvm.?????"
-static unsigned *getArgumentPermutation(Function* Fn, Function* NewFn) {
- const std::string& Name = Fn->getName();
- unsigned N = Fn->getFunctionType()->getNumParams();
- unsigned M = NewFn->getFunctionType()->getNumParams();
-
- switch (Name[5]) {
- case 'd':
- if (Name == "llvm.dbg.stoppoint") {
- static unsigned Permutation[] = { 2, 3, 4 };
- assert(M == (sizeof(Permutation) / sizeof(unsigned)) &&
- "Permutation is wrong length");
- if (N == 4) return Permutation;
- } else if (Name == "llvm.dbg.region.start") {
- static unsigned Permutation[] = { 0 };
- assert(M == (sizeof(Permutation) / sizeof(unsigned)) &&
- "Permutation is wrong length");
- if (N == 0) return Permutation;
- } else if (Name == "llvm.dbg.region.end") {
- static unsigned Permutation[] = { 0 };
- assert(M == (sizeof(Permutation) / sizeof(unsigned)) &&
- "Permutation is wrong length");
- if (N == 0) return Permutation;
- } else if (Name == "llvm.dbg.declare") {
- static unsigned Permutation[] = { 0, 0 };
- assert(M == (sizeof(Permutation) / sizeof(unsigned)) &&
- "Permutation is wrong length");
- if (N == 0) return Permutation;
- }
+ // Calls to these instructions are transformed into unaligned loads.
+ if (Name == "x86.sse.loadu.ps" || Name == "x86.sse2.loadu.dq" ||
+ Name == "x86.sse2.loadu.pd")
+ return true;
+
+ // Calls to these instructions are transformed into nontemporal stores.
+ if (Name == "x86.sse.movnt.ps" || Name == "x86.sse2.movnt.dq" ||
+ Name == "x86.sse2.movnt.pd" || Name == "x86.sse2.movnt.i")
+ return true;
+
break;
}
- return NULL;
+ }
+
+ // 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.
+ return false;
}
-// UpgradeIntrinsicFunction - Convert overloaded intrinsic function names to
-// their non-overloaded variants by appending the appropriate suffix based on
-// the argument types.
-Function *llvm::UpgradeIntrinsicFunction(Function* F) {
- // See if its one of the name's we're interested in.
- if (Function *R = getUpgradedIntrinsic(F)) {
- if (R->getName() != F->getName())
- std::cerr << "WARNING: change " << F->getName() << " to "
- << R->getName() << "\n";
- return R;
- }
- return 0;
+bool llvm::UpgradeIntrinsicFunction(Function *F, Function *&NewFn) {
+ NewFn = 0;
+ bool Upgraded = UpgradeIntrinsicFunction1(F, NewFn);
+
+ // Upgrade intrinsic attributes. This does not change the function.
+ if (NewFn)
+ F = NewFn;
+ if (unsigned id = F->getIntrinsicID())
+ F->setAttributes(Intrinsic::getAttributes((Intrinsic::ID)id));
+ return Upgraded;
}
-// CastArg - Perform the appropriate cast of an upgraded argument.
-//
-static Value *CastArg(Value *Arg, const Type *Ty, Instruction *InsertBefore) {
- if (Constant *C = dyn_cast<Constant>(Arg)) {
- return ConstantExpr::getCast(C, Ty);
- } else {
- Value *Cast = new CastInst(Arg, Ty, "autoupgrade_cast", InsertBefore);
- return Cast;
- }
+bool llvm::UpgradeGlobalVariable(GlobalVariable *GV) {
+ // Nothing to do yet.
+ return false;
}
-// UpgradeIntrinsicCall - In the BC reader, change a call to an intrinsic to be
-// a call to an upgraded intrinsic. We may have to permute the order or promote
-// some arguments with a cast.
+// 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();
+ LLVMContext &C = CI->getContext();
+ ImmutableCallSite CS(CI);
- const FunctionType *NewFnTy = NewFn->getFunctionType();
- std::vector<Value*> Oprnds;
-
- unsigned *Permutation = getArgumentPermutation(F, NewFn);
- unsigned N = NewFnTy->getNumParams();
+ assert(F && "CallInst has no function associated with it.");
- if (Permutation) {
- for (unsigned i = 0; i != N; ++i) {
- unsigned p = Permutation[i];
-
- if (p) {
- Value *V = CI->getOperand(p);
- if (V->getType() != NewFnTy->getParamType(i))
- V = CastArg(V, NewFnTy->getParamType(i), CI);
- Oprnds.push_back(V);
- } else
- Oprnds.push_back(UndefValue::get(NewFnTy->getParamType(i)));
- }
- } else if (N) {
- assert(N == (CI->getNumOperands() - 1) &&
- "Upgraded function needs permutation");
- for (unsigned i = 0; i != N; ++i) {
- Value *V = CI->getOperand(i + 1);
- if (V->getType() != NewFnTy->getParamType(i))
- V = CastArg(V, NewFnTy->getParamType(i), CI);
- Oprnds.push_back(V);
+ if (!NewFn) {
+ if (F->getName() == "llvm.x86.sse.loadu.ps" ||
+ F->getName() == "llvm.x86.sse2.loadu.dq" ||
+ F->getName() == "llvm.x86.sse2.loadu.pd") {
+ // Convert to a native, unaligned load.
+ Type *VecTy = CI->getType();
+ Type *IntTy = IntegerType::get(C, 128);
+ IRBuilder<> Builder(C);
+ Builder.SetInsertPoint(CI->getParent(), CI);
+
+ Value *BC = Builder.CreateBitCast(CI->getArgOperand(0),
+ PointerType::getUnqual(IntTy),
+ "cast");
+ LoadInst *LI = Builder.CreateLoad(BC, CI->getName());
+ LI->setAlignment(1); // Unaligned load.
+ BC = Builder.CreateBitCast(LI, VecTy, "new.cast");
+
+ // Fix up all the uses with our new load.
+ if (!CI->use_empty())
+ CI->replaceAllUsesWith(BC);
+
+ // Remove intrinsic.
+ CI->eraseFromParent();
+ } else if (F->getName() == "llvm.x86.sse.movnt.ps" ||
+ F->getName() == "llvm.x86.sse2.movnt.dq" ||
+ F->getName() == "llvm.x86.sse2.movnt.pd" ||
+ F->getName() == "llvm.x86.sse2.movnt.i") {
+ IRBuilder<> Builder(C);
+ Builder.SetInsertPoint(CI->getParent(), CI);
+
+ Module *M = F->getParent();
+ SmallVector<Value *, 1> Elts;
+ Elts.push_back(ConstantInt::get(Type::getInt32Ty(C), 1));
+ MDNode *Node = MDNode::get(C, Elts);
+
+ Value *Arg0 = CI->getArgOperand(0);
+ Value *Arg1 = CI->getArgOperand(1);
+
+ // Convert the type of the pointer to a pointer to the stored type.
+ Value *BC = Builder.CreateBitCast(Arg0,
+ PointerType::getUnqual(Arg1->getType()),
+ "cast");
+ StoreInst *SI = Builder.CreateStore(Arg1, BC);
+ SI->setMetadata(M->getMDKindID("nontemporal"), Node);
+ SI->setAlignment(16);
+
+ // Remove intrinsic.
+ CI->eraseFromParent();
+ } else {
+ llvm_unreachable("Unknown function for CallInst upgrade.");
}
+ return;
}
-
- bool NewIsVoid = NewFn->getReturnType() == Type::VoidTy;
-
- CallInst *NewCI = new CallInst(NewFn, Oprnds,
- NewIsVoid ? "" : CI->getName(),
- CI);
- NewCI->setTailCall(CI->isTailCall());
- NewCI->setCallingConv(CI->getCallingConv());
-
- if (!CI->use_empty()) {
- if (NewIsVoid) {
- CI->replaceAllUsesWith(UndefValue::get(CI->getType()));
- } else {
- Instruction *RetVal = NewCI;
-
- if (F->getReturnType() != NewFn->getReturnType()) {
- RetVal = new CastInst(NewCI, F->getReturnType(),
- NewCI->getName(), CI);
- NewCI->moveBefore(RetVal);
+
+ switch (NewFn->getIntrinsicID()) {
+ case Intrinsic::prefetch: {
+ IRBuilder<> Builder(C);
+ Builder.SetInsertPoint(CI->getParent(), CI);
+ llvm::Type *I32Ty = llvm::Type::getInt32Ty(CI->getContext());
+
+ // Add the extra "data cache" argument
+ Value *Operands[4] = { CI->getArgOperand(0), CI->getArgOperand(1),
+ CI->getArgOperand(2),
+ llvm::ConstantInt::get(I32Ty, 1) };
+ CallInst *NewCI = CallInst::Create(NewFn, Operands,
+ CI->getName(), CI);
+ NewCI->setTailCall(CI->isTailCall());
+ NewCI->setCallingConv(CI->getCallingConv());
+ // Handle any uses of the old CallInst.
+ if (!CI->use_empty())
+ // Replace all uses of the old call with the new cast which has the
+ // correct type.
+ CI->replaceAllUsesWith(NewCI);
+
+ // Clean up the old call now that it has been completely upgraded.
+ CI->eraseFromParent();
+ break;
+ }
+ }
+}
+
+// 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.
+ Function *NewFn;
+ if (UpgradeIntrinsicFunction(F, NewFn)) {
+ 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; ) {
+ if (CallInst *CI = dyn_cast<CallInst>(*UI++))
+ UpgradeIntrinsicCall(CI, NewFn);
}
-
- CI->replaceAllUsesWith(RetVal);
+ // Remove old function, no longer used, from the module.
+ F->eraseFromParent();
}
}
- CI->eraseFromParent();
}
-bool llvm::UpgradeCallsToIntrinsic(Function* F) {
- if (Function* NewFn = UpgradeIntrinsicFunction(F)) {
- for (Value::use_iterator UI = F->use_begin(), UE = F->use_end();
- UI != UE; ) {
- if (CallInst* CI = dyn_cast<CallInst>(*UI++))
- UpgradeIntrinsicCall(CI, NewFn);
+/// This function strips all debug info intrinsics, except for llvm.dbg.declare.
+/// If an llvm.dbg.declare intrinsic is invalid, then this function simply
+/// strips that use.
+void llvm::CheckDebugInfoIntrinsics(Module *M) {
+ if (Function *FuncStart = M->getFunction("llvm.dbg.func.start")) {
+ while (!FuncStart->use_empty())
+ cast<CallInst>(FuncStart->use_back())->eraseFromParent();
+ FuncStart->eraseFromParent();
+ }
+
+ if (Function *StopPoint = M->getFunction("llvm.dbg.stoppoint")) {
+ while (!StopPoint->use_empty())
+ cast<CallInst>(StopPoint->use_back())->eraseFromParent();
+ StopPoint->eraseFromParent();
+ }
+
+ if (Function *RegionStart = M->getFunction("llvm.dbg.region.start")) {
+ while (!RegionStart->use_empty())
+ cast<CallInst>(RegionStart->use_back())->eraseFromParent();
+ RegionStart->eraseFromParent();
+ }
+
+ if (Function *RegionEnd = M->getFunction("llvm.dbg.region.end")) {
+ while (!RegionEnd->use_empty())
+ cast<CallInst>(RegionEnd->use_back())->eraseFromParent();
+ RegionEnd->eraseFromParent();
+ }
+
+ if (Function *Declare = M->getFunction("llvm.dbg.declare")) {
+ if (!Declare->use_empty()) {
+ DbgDeclareInst *DDI = cast<DbgDeclareInst>(Declare->use_back());
+ if (!isa<MDNode>(DDI->getArgOperand(0)) ||
+ !isa<MDNode>(DDI->getArgOperand(1))) {
+ while (!Declare->use_empty()) {
+ CallInst *CI = cast<CallInst>(Declare->use_back());
+ CI->eraseFromParent();
+ }
+ Declare->eraseFromParent();
+ }
}
- if (NewFn != F)
- F->eraseFromParent();
- return true;
}
- return false;
}