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/LLVMContext.h"
18 #include "llvm/Module.h"
19 #include "llvm/IntrinsicInst.h"
20 #include "llvm/ADT/SmallVector.h"
21 #include "llvm/Support/CallSite.h"
22 #include "llvm/Support/ErrorHandling.h"
23 #include "llvm/Support/IRBuilder.h"
28 static bool UpgradeIntrinsicFunction1(Function
*F
, Function
*&NewFn
) {
29 assert(F
&& "Illegal to upgrade a non-existent Function.");
31 // Quickly eliminate it, if it's not a candidate.
32 StringRef Name
= F
->getName();
33 if (Name
.size() <= 8 || !Name
.startswith("llvm."))
35 Name
= Name
.substr(5); // Strip off "llvm."
37 const FunctionType
*FTy
= F
->getFunctionType();
38 Module
*M
= F
->getParent();
43 // This upgrades the llvm.prefetch intrinsic to accept one more parameter,
44 // which is a instruction / data cache identifier. The old version only
45 // implicitly accepted the data version.
46 if (Name
== "prefetch") {
47 // Don't do anything if it has the correct number of arguments already
48 if (FTy
->getNumParams() == 4)
51 assert(FTy
->getNumParams() == 3 && "old prefetch takes 3 args!");
52 // We first need to change the name of the old (bad) intrinsic, because
53 // its type is incorrect, but we cannot overload that name. We
54 // arbitrarily unique it here allowing us to construct a correctly named
55 // and typed function below.
56 std::string NameTmp
= F
->getName();
58 NewFn
= cast
<Function
>(M
->getOrInsertFunction(NameTmp
,
70 const char *NewFnName
= NULL
;
71 // This fixes the poorly named crc32 intrinsics.
72 if (Name
== "x86.sse42.crc32.8")
73 NewFnName
= "llvm.x86.sse42.crc32.32.8";
74 else if (Name
== "x86.sse42.crc32.16")
75 NewFnName
= "llvm.x86.sse42.crc32.32.16";
76 else if (Name
== "x86.sse42.crc32.32")
77 NewFnName
= "llvm.x86.sse42.crc32.32.32";
78 else if (Name
== "x86.sse42.crc64.8")
79 NewFnName
= "llvm.x86.sse42.crc32.64.8";
80 else if (Name
== "x86.sse42.crc64.64")
81 NewFnName
= "llvm.x86.sse42.crc32.64.64";
84 F
->setName(NewFnName
);
89 // Calls to these instructions are transformed into unaligned loads.
90 if (Name
== "x86.sse.loadu.ps" || Name
== "x86.sse2.loadu.dq" ||
91 Name
== "x86.sse2.loadu.pd")
94 // Calls to these instructions are transformed into nontemporal stores.
95 if (Name
== "x86.sse.movnt.ps" || Name
== "x86.sse2.movnt.dq" ||
96 Name
== "x86.sse2.movnt.pd" || Name
== "x86.sse2.movnt.i")
103 // This may not belong here. This function is effectively being overloaded
104 // to both detect an intrinsic which needs upgrading, and to provide the
105 // upgraded form of the intrinsic. We should perhaps have two separate
106 // functions for this.
110 bool llvm::UpgradeIntrinsicFunction(Function
*F
, Function
*&NewFn
) {
112 bool Upgraded
= UpgradeIntrinsicFunction1(F
, NewFn
);
114 // Upgrade intrinsic attributes. This does not change the function.
117 if (unsigned id
= F
->getIntrinsicID())
118 F
->setAttributes(Intrinsic::getAttributes((Intrinsic::ID
)id
));
122 bool llvm::UpgradeGlobalVariable(GlobalVariable
*GV
) {
123 // Nothing to do yet.
127 // UpgradeIntrinsicCall - Upgrade a call to an old intrinsic to be a call the
128 // upgraded intrinsic. All argument and return casting must be provided in
129 // order to seamlessly integrate with existing context.
130 void llvm::UpgradeIntrinsicCall(CallInst
*CI
, Function
*NewFn
) {
131 Function
*F
= CI
->getCalledFunction();
132 LLVMContext
&C
= CI
->getContext();
133 ImmutableCallSite
CS(CI
);
135 assert(F
&& "CallInst has no function associated with it.");
138 if (F
->getName() == "llvm.x86.sse.loadu.ps" ||
139 F
->getName() == "llvm.x86.sse2.loadu.dq" ||
140 F
->getName() == "llvm.x86.sse2.loadu.pd") {
141 // Convert to a native, unaligned load.
142 const Type
*VecTy
= CI
->getType();
143 const Type
*IntTy
= IntegerType::get(C
, 128);
144 IRBuilder
<> Builder(C
);
145 Builder
.SetInsertPoint(CI
->getParent(), CI
);
147 Value
*BC
= Builder
.CreateBitCast(CI
->getArgOperand(0),
148 PointerType::getUnqual(IntTy
),
150 LoadInst
*LI
= Builder
.CreateLoad(BC
, CI
->getName());
151 LI
->setAlignment(1); // Unaligned load.
152 BC
= Builder
.CreateBitCast(LI
, VecTy
, "new.cast");
154 // Fix up all the uses with our new load.
155 if (!CI
->use_empty())
156 CI
->replaceAllUsesWith(BC
);
159 CI
->eraseFromParent();
160 } else if (F
->getName() == "llvm.x86.sse.movnt.ps" ||
161 F
->getName() == "llvm.x86.sse2.movnt.dq" ||
162 F
->getName() == "llvm.x86.sse2.movnt.pd" ||
163 F
->getName() == "llvm.x86.sse2.movnt.i") {
164 IRBuilder
<> Builder(C
);
165 Builder
.SetInsertPoint(CI
->getParent(), CI
);
167 Module
*M
= F
->getParent();
168 SmallVector
<Value
*, 1> Elts
;
169 Elts
.push_back(ConstantInt::get(Type::getInt32Ty(C
), 1));
170 MDNode
*Node
= MDNode::get(C
, Elts
);
172 Value
*Arg0
= CI
->getArgOperand(0);
173 Value
*Arg1
= CI
->getArgOperand(1);
175 // Convert the type of the pointer to a pointer to the stored type.
176 Value
*BC
= Builder
.CreateBitCast(Arg0
,
177 PointerType::getUnqual(Arg1
->getType()),
179 StoreInst
*SI
= Builder
.CreateStore(Arg1
, BC
);
180 SI
->setMetadata(M
->getMDKindID("nontemporal"), Node
);
181 SI
->setAlignment(16);
184 CI
->eraseFromParent();
186 llvm_unreachable("Unknown function for CallInst upgrade.");
191 switch (NewFn
->getIntrinsicID()) {
192 case Intrinsic::prefetch
: {
193 IRBuilder
<> Builder(C
);
194 Builder
.SetInsertPoint(CI
->getParent(), CI
);
195 const llvm::Type
*I32Ty
= llvm::Type::getInt32Ty(CI
->getContext());
197 // Add the extra "data cache" argument
198 Value
*Operands
[4] = { CI
->getArgOperand(0), CI
->getArgOperand(1),
199 CI
->getArgOperand(2),
200 llvm::ConstantInt::get(I32Ty
, 1) };
201 CallInst
*NewCI
= CallInst::Create(NewFn
, Operands
, Operands
+4,
203 NewCI
->setTailCall(CI
->isTailCall());
204 NewCI
->setCallingConv(CI
->getCallingConv());
205 // Handle any uses of the old CallInst.
206 if (!CI
->use_empty())
207 // Replace all uses of the old call with the new cast which has the
209 CI
->replaceAllUsesWith(NewCI
);
211 // Clean up the old call now that it has been completely upgraded.
212 CI
->eraseFromParent();
218 // This tests each Function to determine if it needs upgrading. When we find
219 // one we are interested in, we then upgrade all calls to reflect the new
221 void llvm::UpgradeCallsToIntrinsic(Function
* F
) {
222 assert(F
&& "Illegal attempt to upgrade a non-existent intrinsic.");
224 // Upgrade the function and check if it is a totaly new function.
226 if (UpgradeIntrinsicFunction(F
, NewFn
)) {
228 // Replace all uses to the old function with the new one if necessary.
229 for (Value::use_iterator UI
= F
->use_begin(), UE
= F
->use_end();
231 if (CallInst
*CI
= dyn_cast
<CallInst
>(*UI
++))
232 UpgradeIntrinsicCall(CI
, NewFn
);
234 // Remove old function, no longer used, from the module.
235 F
->eraseFromParent();
240 /// This function strips all debug info intrinsics, except for llvm.dbg.declare.
241 /// If an llvm.dbg.declare intrinsic is invalid, then this function simply
243 void llvm::CheckDebugInfoIntrinsics(Module
*M
) {
244 if (Function
*FuncStart
= M
->getFunction("llvm.dbg.func.start")) {
245 while (!FuncStart
->use_empty())
246 cast
<CallInst
>(FuncStart
->use_back())->eraseFromParent();
247 FuncStart
->eraseFromParent();
250 if (Function
*StopPoint
= M
->getFunction("llvm.dbg.stoppoint")) {
251 while (!StopPoint
->use_empty())
252 cast
<CallInst
>(StopPoint
->use_back())->eraseFromParent();
253 StopPoint
->eraseFromParent();
256 if (Function
*RegionStart
= M
->getFunction("llvm.dbg.region.start")) {
257 while (!RegionStart
->use_empty())
258 cast
<CallInst
>(RegionStart
->use_back())->eraseFromParent();
259 RegionStart
->eraseFromParent();
262 if (Function
*RegionEnd
= M
->getFunction("llvm.dbg.region.end")) {
263 while (!RegionEnd
->use_empty())
264 cast
<CallInst
>(RegionEnd
->use_back())->eraseFromParent();
265 RegionEnd
->eraseFromParent();
268 if (Function
*Declare
= M
->getFunction("llvm.dbg.declare")) {
269 if (!Declare
->use_empty()) {
270 DbgDeclareInst
*DDI
= cast
<DbgDeclareInst
>(Declare
->use_back());
271 if (!isa
<MDNode
>(DDI
->getArgOperand(0)) ||
272 !isa
<MDNode
>(DDI
->getArgOperand(1))) {
273 while (!Declare
->use_empty()) {
274 CallInst
*CI
= cast
<CallInst
>(Declare
->use_back());
275 CI
->eraseFromParent();
277 Declare
->eraseFromParent();