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."
41 FunctionType *FTy = F->getFunctionType();
42 Module *M = F->getParent();
47 // This upgrades the llvm.prefetch intrinsic to accept one more parameter,
48 // which is a instruction / data cache identifier. The old version only
49 // implicitly accepted the data version.
50 if (Name == "prefetch") {
51 // Don't do anything if it has the correct number of arguments already
52 if (FTy->getNumParams() == 4)
55 assert(FTy->getNumParams() == 3 && "old prefetch takes 3 args!");
56 // We first need to change the name of the old (bad) intrinsic, because
57 // its type is incorrect, but we cannot overload that name. We
58 // arbitrarily unique it here allowing us to construct a correctly named
59 // and typed function below.
60 std::string NameTmp = F->getName();
62 NewFn = cast<Function>(M->getOrInsertFunction(NameTmp,
74 const char *NewFnName = NULL;
75 // This fixes the poorly named crc32 intrinsics.
76 if (Name == "x86.sse42.crc32.8")
77 NewFnName = "llvm.x86.sse42.crc32.32.8";
78 else if (Name == "x86.sse42.crc32.16")
79 NewFnName = "llvm.x86.sse42.crc32.32.16";
80 else if (Name == "x86.sse42.crc32.32")
81 NewFnName = "llvm.x86.sse42.crc32.32.32";
82 else if (Name == "x86.sse42.crc64.8")
83 NewFnName = "llvm.x86.sse42.crc32.64.8";
84 else if (Name == "x86.sse42.crc64.64")
85 NewFnName = "llvm.x86.sse42.crc32.64.64";
88 F->setName(NewFnName);
93 // Calls to these instructions are transformed into unaligned loads.
94 if (Name == "x86.sse.loadu.ps" || Name == "x86.sse2.loadu.dq" ||
95 Name == "x86.sse2.loadu.pd")
98 // Calls to these instructions are transformed into nontemporal stores.
99 if (Name == "x86.sse.movnt.ps" || Name == "x86.sse2.movnt.dq" ||
100 Name == "x86.sse2.movnt.pd" || Name == "x86.sse2.movnt.i")
107 // This may not belong here. This function is effectively being overloaded
108 // to both detect an intrinsic which needs upgrading, and to provide the
109 // upgraded form of the intrinsic. We should perhaps have two separate
110 // functions for this.
114 bool llvm::UpgradeIntrinsicFunction(Function *F, Function *&NewFn) {
116 bool Upgraded = UpgradeIntrinsicFunction1(F, NewFn);
118 // Upgrade intrinsic attributes. This does not change the function.
121 if (unsigned id = F->getIntrinsicID())
122 F->setAttributes(Intrinsic::getAttributes((Intrinsic::ID)id));
126 bool llvm::UpgradeGlobalVariable(GlobalVariable *GV) {
127 // Nothing to do yet.
131 // UpgradeIntrinsicCall - Upgrade a call to an old intrinsic to be a call the
132 // upgraded intrinsic. All argument and return casting must be provided in
133 // order to seamlessly integrate with existing context.
134 void llvm::UpgradeIntrinsicCall(CallInst *CI, Function *NewFn) {
135 Function *F = CI->getCalledFunction();
136 LLVMContext &C = CI->getContext();
137 ImmutableCallSite CS(CI);
139 assert(F && "CallInst has no function associated with it.");
142 if (F->getName() == "llvm.x86.sse.loadu.ps" ||
143 F->getName() == "llvm.x86.sse2.loadu.dq" ||
144 F->getName() == "llvm.x86.sse2.loadu.pd") {
145 // Convert to a native, unaligned load.
146 Type *VecTy = CI->getType();
147 Type *IntTy = IntegerType::get(C, 128);
148 IRBuilder<> Builder(C);
149 Builder.SetInsertPoint(CI->getParent(), CI);
151 Value *BC = Builder.CreateBitCast(CI->getArgOperand(0),
152 PointerType::getUnqual(IntTy),
154 LoadInst *LI = Builder.CreateLoad(BC, CI->getName());
155 LI->setAlignment(1); // Unaligned load.
156 BC = Builder.CreateBitCast(LI, VecTy, "new.cast");
158 // Fix up all the uses with our new load.
159 if (!CI->use_empty())
160 CI->replaceAllUsesWith(BC);
163 CI->eraseFromParent();
164 } else if (F->getName() == "llvm.x86.sse.movnt.ps" ||
165 F->getName() == "llvm.x86.sse2.movnt.dq" ||
166 F->getName() == "llvm.x86.sse2.movnt.pd" ||
167 F->getName() == "llvm.x86.sse2.movnt.i") {
168 IRBuilder<> Builder(C);
169 Builder.SetInsertPoint(CI->getParent(), CI);
171 Module *M = F->getParent();
172 SmallVector<Value *, 1> Elts;
173 Elts.push_back(ConstantInt::get(Type::getInt32Ty(C), 1));
174 MDNode *Node = MDNode::get(C, Elts);
176 Value *Arg0 = CI->getArgOperand(0);
177 Value *Arg1 = CI->getArgOperand(1);
179 // Convert the type of the pointer to a pointer to the stored type.
180 Value *BC = Builder.CreateBitCast(Arg0,
181 PointerType::getUnqual(Arg1->getType()),
183 StoreInst *SI = Builder.CreateStore(Arg1, BC);
184 SI->setMetadata(M->getMDKindID("nontemporal"), Node);
185 SI->setAlignment(16);
188 CI->eraseFromParent();
190 llvm_unreachable("Unknown function for CallInst upgrade.");
195 switch (NewFn->getIntrinsicID()) {
196 case Intrinsic::prefetch: {
197 IRBuilder<> Builder(C);
198 Builder.SetInsertPoint(CI->getParent(), CI);
199 llvm::Type *I32Ty = llvm::Type::getInt32Ty(CI->getContext());
201 // Add the extra "data cache" argument
202 Value *Operands[4] = { CI->getArgOperand(0), CI->getArgOperand(1),
203 CI->getArgOperand(2),
204 llvm::ConstantInt::get(I32Ty, 1) };
205 CallInst *NewCI = CallInst::Create(NewFn, Operands,
207 NewCI->setTailCall(CI->isTailCall());
208 NewCI->setCallingConv(CI->getCallingConv());
209 // Handle any uses of the old CallInst.
210 if (!CI->use_empty())
211 // Replace all uses of the old call with the new cast which has the
213 CI->replaceAllUsesWith(NewCI);
215 // Clean up the old call now that it has been completely upgraded.
216 CI->eraseFromParent();
222 // This tests each Function to determine if it needs upgrading. When we find
223 // one we are interested in, we then upgrade all calls to reflect the new
225 void llvm::UpgradeCallsToIntrinsic(Function* F) {
226 assert(F && "Illegal attempt to upgrade a non-existent intrinsic.");
228 // Upgrade the function and check if it is a totaly new function.
230 if (UpgradeIntrinsicFunction(F, NewFn)) {
232 // Replace all uses to the old function with the new one if necessary.
233 for (Value::use_iterator UI = F->use_begin(), UE = F->use_end();
235 if (CallInst *CI = dyn_cast<CallInst>(*UI++))
236 UpgradeIntrinsicCall(CI, NewFn);
238 // Remove old function, no longer used, from the module.
239 F->eraseFromParent();
244 /// This function strips all debug info intrinsics, except for llvm.dbg.declare.
245 /// If an llvm.dbg.declare intrinsic is invalid, then this function simply
247 void llvm::CheckDebugInfoIntrinsics(Module *M) {
248 if (Function *FuncStart = M->getFunction("llvm.dbg.func.start")) {
249 while (!FuncStart->use_empty())
250 cast<CallInst>(FuncStart->use_back())->eraseFromParent();
251 FuncStart->eraseFromParent();
254 if (Function *StopPoint = M->getFunction("llvm.dbg.stoppoint")) {
255 while (!StopPoint->use_empty())
256 cast<CallInst>(StopPoint->use_back())->eraseFromParent();
257 StopPoint->eraseFromParent();
260 if (Function *RegionStart = M->getFunction("llvm.dbg.region.start")) {
261 while (!RegionStart->use_empty())
262 cast<CallInst>(RegionStart->use_back())->eraseFromParent();
263 RegionStart->eraseFromParent();
266 if (Function *RegionEnd = M->getFunction("llvm.dbg.region.end")) {
267 while (!RegionEnd->use_empty())
268 cast<CallInst>(RegionEnd->use_back())->eraseFromParent();
269 RegionEnd->eraseFromParent();
272 if (Function *Declare = M->getFunction("llvm.dbg.declare")) {
273 if (!Declare->use_empty()) {
274 DbgDeclareInst *DDI = cast<DbgDeclareInst>(Declare->use_back());
275 if (!isa<MDNode>(DDI->getArgOperand(0)) ||
276 !isa<MDNode>(DDI->getArgOperand(1))) {
277 while (!Declare->use_empty()) {
278 CallInst *CI = cast<CallInst>(Declare->use_back());
279 CI->eraseFromParent();
281 Declare->eraseFromParent();
287 /// FindExnAndSelIntrinsics - Find the eh_exception and eh_selector intrinsic
288 /// calls reachable from the unwind basic block.
289 static void FindExnAndSelIntrinsics(BasicBlock *BB, CallInst *&Exn,
291 SmallPtrSet<BasicBlock*, 8> &Visited) {
292 if (!Visited.insert(BB)) return;
294 for (BasicBlock::iterator
295 I = BB->begin(), E = BB->end(); I != E; ++I) {
296 if (CallInst *CI = dyn_cast<CallInst>(I)) {
297 switch (CI->getCalledFunction()->getIntrinsicID()) {
299 case Intrinsic::eh_exception:
300 assert(!Exn && "Found more than one eh.exception call!");
303 case Intrinsic::eh_selector:
304 assert(!Sel && "Found more than one eh.selector call!");
309 if (Exn && Sel) return;
313 if (Exn && Sel) return;
315 for (succ_iterator I = succ_begin(BB), E = succ_end(BB); I != E; ++I) {
316 FindExnAndSelIntrinsics(*I, Exn, Sel, Visited);
317 if (Exn && Sel) return;
321 /// TransferClausesToLandingPadInst - Transfer the exception handling clauses
322 /// from the eh_selector call to the new landingpad instruction.
323 static void TransferClausesToLandingPadInst(LandingPadInst *LPI,
325 LLVMContext &Context = LPI->getContext();
326 unsigned N = EHSel->getNumArgOperands();
328 for (unsigned i = N - 1; i > 1; --i) {
329 if (const ConstantInt *CI = dyn_cast<ConstantInt>(EHSel->getArgOperand(i))){
330 unsigned FilterLength = CI->getZExtValue();
331 unsigned FirstCatch = i + FilterLength + !FilterLength;
332 assert(FirstCatch <= N && "Invalid filter length");
335 for (unsigned j = FirstCatch; j < N; ++j) {
336 Value *Val = EHSel->getArgOperand(j);
337 if (!Val->hasName() || Val->getName() != "llvm.eh.catch.all.value") {
338 LPI->addClause(EHSel->getArgOperand(j));
340 GlobalVariable *GV = cast<GlobalVariable>(Val);
341 LPI->addClause(GV->getInitializer());
347 LPI->setCleanup(true);
350 SmallVector<Constant *, 4> TyInfo;
351 TyInfo.reserve(FilterLength - 1);
352 for (unsigned j = i + 1; j < FirstCatch; ++j)
353 TyInfo.push_back(cast<Constant>(EHSel->getArgOperand(j)));
355 ArrayType::get(!TyInfo.empty() ? TyInfo[0]->getType() :
356 PointerType::getUnqual(Type::getInt8Ty(Context)),
358 LPI->addClause(ConstantArray::get(AType, TyInfo));
366 for (unsigned j = 2; j < N; ++j) {
367 Value *Val = EHSel->getArgOperand(j);
368 if (!Val->hasName() || Val->getName() != "llvm.eh.catch.all.value") {
369 LPI->addClause(EHSel->getArgOperand(j));
371 GlobalVariable *GV = cast<GlobalVariable>(Val);
372 LPI->addClause(GV->getInitializer());
377 /// This function upgrades the old pre-3.0 exception handling system to the new
378 /// one. N.B. This will be removed in 3.1.
379 void llvm::UpgradeExceptionHandling(Module *M) {
380 Function *EHException = M->getFunction("llvm.eh.exception");
381 Function *EHSelector = M->getFunction("llvm.eh.selector");
382 if (!EHException || !EHSelector)
385 LLVMContext &Context = M->getContext();
386 Type *ExnTy = PointerType::getUnqual(Type::getInt8Ty(Context));
387 Type *SelTy = Type::getInt32Ty(Context);
388 Type *LPadSlotTy = StructType::get(ExnTy, SelTy, NULL);
390 // This map stores the slots where the exception object and selector value are
391 // stored within a function.
392 SmallPtrSet<Instruction*, 32> DeadInsts;
393 DenseMap<Function*, std::pair<Value*, Value*> > FnToLPadSlotMap;
394 for (Module::iterator
395 I = M->begin(), E = M->end(); I != E; ++I) {
398 for (Function::iterator
399 II = F.begin(), IE = F.end(); II != IE; ++II) {
400 BasicBlock *BB = &*II;
401 InvokeInst *Inst = dyn_cast<InvokeInst>(BB->getTerminator());
403 BasicBlock *UnwindDest = Inst->getUnwindDest();
404 if (UnwindDest->isLandingPad()) continue; // All ready converted.
406 // Store the exception object and selector value in the entry block.
409 if (!FnToLPadSlotMap[&F].first) {
410 BasicBlock *Entry = &F.front();
411 ExnSlot = new AllocaInst(ExnTy, "exn", Entry->getTerminator());
412 SelSlot = new AllocaInst(SelTy, "sel", Entry->getTerminator());
413 FnToLPadSlotMap[&F] = std::make_pair(ExnSlot, SelSlot);
415 ExnSlot = FnToLPadSlotMap[&F].first;
416 SelSlot = FnToLPadSlotMap[&F].second;
419 // We're in an unwind block. Try to find the eh.exception and eh.selector
421 IRBuilder<> Builder(Context);
422 Builder.SetInsertPoint(UnwindDest, UnwindDest->getFirstNonPHI());
424 SmallPtrSet<BasicBlock*, 8> Visited;
427 FindExnAndSelIntrinsics(UnwindDest, Exn, Sel, Visited);
428 assert(Exn && Sel && "Cannot find eh.exception and eh.selector calls!");
430 Value *PersFn = Sel->getArgOperand(1);
431 LandingPadInst *LPI = Builder.CreateLandingPad(LPadSlotTy, PersFn, 0);
432 Value *LPExn = Builder.CreateExtractValue(LPI, 0);
433 Value *LPSel = Builder.CreateExtractValue(LPI, 1);
434 Builder.CreateStore(LPExn, ExnSlot);
435 Builder.CreateStore(LPSel, SelSlot);
437 TransferClausesToLandingPadInst(LPI, Sel);
439 Exn->replaceAllUsesWith(LPExn);
440 Sel->replaceAllUsesWith(LPSel);
442 DeadInsts.insert(Exn);
443 DeadInsts.insert(Sel);
447 // Remove the dead instructions.
448 for (SmallPtrSet<Instruction*, 32>::iterator
449 I = DeadInsts.begin(), E = DeadInsts.end(); I != E; ++I) {
450 Instruction *Inst = *I;
451 Inst->eraseFromParent();
454 // Replace calls to "llvm.eh.resume" with the 'resume' instruction. Load the
455 // exception and selector values from the stored place.
456 Function *EHResume = M->getFunction("llvm.eh.resume");
457 if (!EHResume) return;
459 while (!EHResume->use_empty()) {
460 CallInst *Resume = cast<CallInst>(EHResume->use_back());
461 BasicBlock *BB = Resume->getParent();
462 Function *Fn = BB->getParent();
463 std::pair<Value*, Value*> &ExnSel = FnToLPadSlotMap[Fn];
464 IRBuilder<> Builder(Context);
465 Builder.SetInsertPoint(BB, Resume);
467 Value *Exn = Builder.CreateLoad(ExnSel.first, "exn");
468 Value *Sel = Builder.CreateLoad(ExnSel.second, "sel");
471 Builder.CreateInsertValue(UndefValue::get(LPadSlotTy),
473 LPadVal = Builder.CreateInsertValue(LPadVal, Sel, 1, "lpad.val");
474 Builder.CreateResume(LPadVal);
476 // Remove all instructions after the 'resume.'
477 BasicBlock::iterator I = Resume;
478 while (I != BB->end()) {
479 Instruction *Inst = &*I++;
480 Inst->eraseFromParent();