Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 1 | //===-- AutoUpgrade.cpp - Implement auto-upgrade helper functions ---------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | f3ebc3f | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the auto-upgrade helper functions |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "llvm/AutoUpgrade.h" |
Anders Carlsson | f924f34 | 2007-12-14 06:38:54 +0000 | [diff] [blame] | 15 | #include "llvm/Constants.h" |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 16 | #include "llvm/Function.h" |
Bill Wendling | 45449b1 | 2011-08-25 23:22:40 +0000 | [diff] [blame^] | 17 | #include "llvm/Instruction.h" |
Owen Anderson | 155dccd8 | 2009-07-07 23:43:39 +0000 | [diff] [blame] | 18 | #include "llvm/LLVMContext.h" |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 19 | #include "llvm/Module.h" |
Devang Patel | 80ae349 | 2009-08-28 23:24:31 +0000 | [diff] [blame] | 20 | #include "llvm/IntrinsicInst.h" |
Bill Wendling | 45449b1 | 2011-08-25 23:22:40 +0000 | [diff] [blame^] | 21 | #include "llvm/ADT/DenseMap.h" |
| 22 | #include "llvm/ADT/SmallPtrSet.h" |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/SmallVector.h" |
Gabor Greif | e540653 | 2010-06-23 08:45:32 +0000 | [diff] [blame] | 24 | #include "llvm/Support/CallSite.h" |
Bill Wendling | 45449b1 | 2011-08-25 23:22:40 +0000 | [diff] [blame^] | 25 | #include "llvm/Support/CFG.h" |
Torok Edwin | 56d0659 | 2009-07-11 20:10:48 +0000 | [diff] [blame] | 26 | #include "llvm/Support/ErrorHandling.h" |
Eric Christopher | 64831c6 | 2010-04-20 00:59:54 +0000 | [diff] [blame] | 27 | #include "llvm/Support/IRBuilder.h" |
Anton Korobeynikov | 579f071 | 2008-02-20 11:08:44 +0000 | [diff] [blame] | 28 | #include <cstring> |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 29 | using namespace llvm; |
| 30 | |
| 31 | |
Evan Cheng | 0e179d0 | 2007-12-17 22:33:23 +0000 | [diff] [blame] | 32 | static bool UpgradeIntrinsicFunction1(Function *F, Function *&NewFn) { |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 33 | assert(F && "Illegal to upgrade a non-existent Function."); |
| 34 | |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 35 | // Quickly eliminate it, if it's not a candidate. |
Chris Lattner | b372f66 | 2011-06-18 18:56:39 +0000 | [diff] [blame] | 36 | StringRef Name = F->getName(); |
| 37 | if (Name.size() <= 8 || !Name.startswith("llvm.")) |
Evan Cheng | 0e179d0 | 2007-12-17 22:33:23 +0000 | [diff] [blame] | 38 | return false; |
Chris Lattner | b372f66 | 2011-06-18 18:56:39 +0000 | [diff] [blame] | 39 | Name = Name.substr(5); // Strip off "llvm." |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 40 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 41 | FunctionType *FTy = F->getFunctionType(); |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 42 | Module *M = F->getParent(); |
Chris Lattner | b372f66 | 2011-06-18 18:56:39 +0000 | [diff] [blame] | 43 | |
| 44 | switch (Name[0]) { |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 45 | default: break; |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 46 | case 'p': |
Bruno Cardoso Lopes | dc9ff3a | 2011-06-14 04:58:37 +0000 | [diff] [blame] | 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. |
Chris Lattner | b372f66 | 2011-06-18 18:56:39 +0000 | [diff] [blame] | 50 | if (Name == "prefetch") { |
Bruno Cardoso Lopes | dc9ff3a | 2011-06-14 04:58:37 +0000 | [diff] [blame] | 51 | // Don't do anything if it has the correct number of arguments already |
| 52 | if (FTy->getNumParams() == 4) |
| 53 | break; |
| 54 | |
| 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. |
Chris Lattner | b372f66 | 2011-06-18 18:56:39 +0000 | [diff] [blame] | 60 | std::string NameTmp = F->getName(); |
Bruno Cardoso Lopes | dc9ff3a | 2011-06-14 04:58:37 +0000 | [diff] [blame] | 61 | F->setName(""); |
Chris Lattner | b372f66 | 2011-06-18 18:56:39 +0000 | [diff] [blame] | 62 | NewFn = cast<Function>(M->getOrInsertFunction(NameTmp, |
Bruno Cardoso Lopes | dc9ff3a | 2011-06-14 04:58:37 +0000 | [diff] [blame] | 63 | FTy->getReturnType(), |
| 64 | FTy->getParamType(0), |
| 65 | FTy->getParamType(1), |
| 66 | FTy->getParamType(2), |
| 67 | FTy->getParamType(2), |
| 68 | (Type*)0)); |
| 69 | return true; |
| 70 | } |
| 71 | |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 72 | break; |
Chris Lattner | b372f66 | 2011-06-18 18:56:39 +0000 | [diff] [blame] | 73 | case 'x': { |
| 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"; |
| 86 | |
| 87 | if (NewFnName) { |
| 88 | F->setName(NewFnName); |
| 89 | NewFn = F; |
| 90 | return true; |
Chad Rosier | b362884 | 2011-05-26 23:13:19 +0000 | [diff] [blame] | 91 | } |
| 92 | |
Chris Lattner | b372f66 | 2011-06-18 18:56:39 +0000 | [diff] [blame] | 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") |
Bill Wendling | b902f1d | 2011-04-13 00:36:11 +0000 | [diff] [blame] | 96 | return true; |
Chris Lattner | 80ed9dc | 2011-06-18 06:05:24 +0000 | [diff] [blame] | 97 | |
Chris Lattner | b372f66 | 2011-06-18 18:56:39 +0000 | [diff] [blame] | 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") |
Bill Wendling | db0996c | 2011-05-03 21:11:17 +0000 | [diff] [blame] | 101 | return true; |
Evan Cheng | 0e179d0 | 2007-12-17 22:33:23 +0000 | [diff] [blame] | 102 | |
Anders Carlsson | f924f34 | 2007-12-14 06:38:54 +0000 | [diff] [blame] | 103 | break; |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 104 | } |
Chris Lattner | b372f66 | 2011-06-18 18:56:39 +0000 | [diff] [blame] | 105 | } |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 106 | |
| 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. |
Evan Cheng | 0e179d0 | 2007-12-17 22:33:23 +0000 | [diff] [blame] | 111 | return false; |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 112 | } |
| 113 | |
Evan Cheng | 0e179d0 | 2007-12-17 22:33:23 +0000 | [diff] [blame] | 114 | bool llvm::UpgradeIntrinsicFunction(Function *F, Function *&NewFn) { |
| 115 | NewFn = 0; |
| 116 | bool Upgraded = UpgradeIntrinsicFunction1(F, NewFn); |
Duncan Sands | 38ef3a8 | 2007-12-03 20:06:50 +0000 | [diff] [blame] | 117 | |
| 118 | // Upgrade intrinsic attributes. This does not change the function. |
Evan Cheng | 0e179d0 | 2007-12-17 22:33:23 +0000 | [diff] [blame] | 119 | if (NewFn) |
| 120 | F = NewFn; |
Dale Johannesen | b842d52 | 2009-02-05 01:49:45 +0000 | [diff] [blame] | 121 | if (unsigned id = F->getIntrinsicID()) |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 122 | F->setAttributes(Intrinsic::getAttributes((Intrinsic::ID)id)); |
Duncan Sands | 38ef3a8 | 2007-12-03 20:06:50 +0000 | [diff] [blame] | 123 | return Upgraded; |
| 124 | } |
| 125 | |
Bill Wendling | e26fffc | 2010-09-10 18:51:56 +0000 | [diff] [blame] | 126 | bool llvm::UpgradeGlobalVariable(GlobalVariable *GV) { |
Chris Lattner | 80ed9dc | 2011-06-18 06:05:24 +0000 | [diff] [blame] | 127 | // Nothing to do yet. |
Bill Wendling | e26fffc | 2010-09-10 18:51:56 +0000 | [diff] [blame] | 128 | return false; |
| 129 | } |
| 130 | |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 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) { |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 135 | Function *F = CI->getCalledFunction(); |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 136 | LLVMContext &C = CI->getContext(); |
Gabor Greif | e540653 | 2010-06-23 08:45:32 +0000 | [diff] [blame] | 137 | ImmutableCallSite CS(CI); |
| 138 | |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 139 | assert(F && "CallInst has no function associated with it."); |
Evan Cheng | 0e179d0 | 2007-12-17 22:33:23 +0000 | [diff] [blame] | 140 | |
| 141 | if (!NewFn) { |
Chris Lattner | 80ed9dc | 2011-06-18 06:05:24 +0000 | [diff] [blame] | 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") { |
Bill Wendling | b902f1d | 2011-04-13 00:36:11 +0000 | [diff] [blame] | 145 | // Convert to a native, unaligned load. |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 146 | Type *VecTy = CI->getType(); |
| 147 | Type *IntTy = IntegerType::get(C, 128); |
Bill Wendling | b902f1d | 2011-04-13 00:36:11 +0000 | [diff] [blame] | 148 | IRBuilder<> Builder(C); |
| 149 | Builder.SetInsertPoint(CI->getParent(), CI); |
| 150 | |
| 151 | Value *BC = Builder.CreateBitCast(CI->getArgOperand(0), |
| 152 | PointerType::getUnqual(IntTy), |
| 153 | "cast"); |
| 154 | LoadInst *LI = Builder.CreateLoad(BC, CI->getName()); |
| 155 | LI->setAlignment(1); // Unaligned load. |
| 156 | BC = Builder.CreateBitCast(LI, VecTy, "new.cast"); |
| 157 | |
| 158 | // Fix up all the uses with our new load. |
| 159 | if (!CI->use_empty()) |
| 160 | CI->replaceAllUsesWith(BC); |
| 161 | |
| 162 | // Remove intrinsic. |
| 163 | CI->eraseFromParent(); |
Bill Wendling | db0996c | 2011-05-03 21:11:17 +0000 | [diff] [blame] | 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); |
| 170 | |
| 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); |
| 175 | |
| 176 | Value *Arg0 = CI->getArgOperand(0); |
| 177 | Value *Arg1 = CI->getArgOperand(1); |
| 178 | |
| 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()), |
| 182 | "cast"); |
| 183 | StoreInst *SI = Builder.CreateStore(Arg1, BC); |
| 184 | SI->setMetadata(M->getMDKindID("nontemporal"), Node); |
| 185 | SI->setAlignment(16); |
| 186 | |
| 187 | // Remove intrinsic. |
| 188 | CI->eraseFromParent(); |
Evan Cheng | a8288f4 | 2007-12-18 01:04:25 +0000 | [diff] [blame] | 189 | } else { |
Torok Edwin | fbcc663 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 190 | llvm_unreachable("Unknown function for CallInst upgrade."); |
Evan Cheng | 0e179d0 | 2007-12-17 22:33:23 +0000 | [diff] [blame] | 191 | } |
| 192 | return; |
| 193 | } |
| 194 | |
Gabor Greif | e9ecc68 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 195 | switch (NewFn->getIntrinsicID()) { |
Bruno Cardoso Lopes | dc9ff3a | 2011-06-14 04:58:37 +0000 | [diff] [blame] | 196 | case Intrinsic::prefetch: { |
| 197 | IRBuilder<> Builder(C); |
| 198 | Builder.SetInsertPoint(CI->getParent(), CI); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 199 | llvm::Type *I32Ty = llvm::Type::getInt32Ty(CI->getContext()); |
Bruno Cardoso Lopes | dc9ff3a | 2011-06-14 04:58:37 +0000 | [diff] [blame] | 200 | |
| 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) }; |
Jay Foad | 5bd375a | 2011-07-15 08:37:34 +0000 | [diff] [blame] | 205 | CallInst *NewCI = CallInst::Create(NewFn, Operands, |
Bruno Cardoso Lopes | dc9ff3a | 2011-06-14 04:58:37 +0000 | [diff] [blame] | 206 | CI->getName(), CI); |
| 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 |
| 212 | // correct type. |
| 213 | CI->replaceAllUsesWith(NewCI); |
| 214 | |
| 215 | // Clean up the old call now that it has been completely upgraded. |
| 216 | CI->eraseFromParent(); |
| 217 | break; |
| 218 | } |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 219 | } |
| 220 | } |
| 221 | |
| 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 |
| 224 | // function. |
| 225 | void llvm::UpgradeCallsToIntrinsic(Function* F) { |
| 226 | assert(F && "Illegal attempt to upgrade a non-existent intrinsic."); |
| 227 | |
| 228 | // Upgrade the function and check if it is a totaly new function. |
Chris Lattner | 80ed9dc | 2011-06-18 06:05:24 +0000 | [diff] [blame] | 229 | Function *NewFn; |
Evan Cheng | 0e179d0 | 2007-12-17 22:33:23 +0000 | [diff] [blame] | 230 | if (UpgradeIntrinsicFunction(F, NewFn)) { |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 231 | if (NewFn != F) { |
| 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(); |
| 234 | UI != UE; ) { |
Chris Lattner | 80ed9dc | 2011-06-18 06:05:24 +0000 | [diff] [blame] | 235 | if (CallInst *CI = dyn_cast<CallInst>(*UI++)) |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 236 | UpgradeIntrinsicCall(CI, NewFn); |
| 237 | } |
| 238 | // Remove old function, no longer used, from the module. |
| 239 | F->eraseFromParent(); |
| 240 | } |
| 241 | } |
| 242 | } |
Devang Patel | 80ae349 | 2009-08-28 23:24:31 +0000 | [diff] [blame] | 243 | |
Victor Hernandez | c2044a1 | 2010-01-05 21:13:46 +0000 | [diff] [blame] | 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 |
| 246 | /// strips that use. |
Devang Patel | 80ae349 | 2009-08-28 23:24:31 +0000 | [diff] [blame] | 247 | void llvm::CheckDebugInfoIntrinsics(Module *M) { |
Devang Patel | 80ae349 | 2009-08-28 23:24:31 +0000 | [diff] [blame] | 248 | if (Function *FuncStart = M->getFunction("llvm.dbg.func.start")) { |
Chris Lattner | 80ed9dc | 2011-06-18 06:05:24 +0000 | [diff] [blame] | 249 | while (!FuncStart->use_empty()) |
| 250 | cast<CallInst>(FuncStart->use_back())->eraseFromParent(); |
Devang Patel | be94f23 | 2010-01-05 01:10:40 +0000 | [diff] [blame] | 251 | FuncStart->eraseFromParent(); |
Devang Patel | 80ae349 | 2009-08-28 23:24:31 +0000 | [diff] [blame] | 252 | } |
Devang Patel | be94f23 | 2010-01-05 01:10:40 +0000 | [diff] [blame] | 253 | |
Devang Patel | 80ae349 | 2009-08-28 23:24:31 +0000 | [diff] [blame] | 254 | if (Function *StopPoint = M->getFunction("llvm.dbg.stoppoint")) { |
Chris Lattner | 80ed9dc | 2011-06-18 06:05:24 +0000 | [diff] [blame] | 255 | while (!StopPoint->use_empty()) |
| 256 | cast<CallInst>(StopPoint->use_back())->eraseFromParent(); |
Devang Patel | be94f23 | 2010-01-05 01:10:40 +0000 | [diff] [blame] | 257 | StopPoint->eraseFromParent(); |
Devang Patel | 80ae349 | 2009-08-28 23:24:31 +0000 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | if (Function *RegionStart = M->getFunction("llvm.dbg.region.start")) { |
Chris Lattner | 80ed9dc | 2011-06-18 06:05:24 +0000 | [diff] [blame] | 261 | while (!RegionStart->use_empty()) |
| 262 | cast<CallInst>(RegionStart->use_back())->eraseFromParent(); |
Devang Patel | be94f23 | 2010-01-05 01:10:40 +0000 | [diff] [blame] | 263 | RegionStart->eraseFromParent(); |
Devang Patel | 80ae349 | 2009-08-28 23:24:31 +0000 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | if (Function *RegionEnd = M->getFunction("llvm.dbg.region.end")) { |
Chris Lattner | 80ed9dc | 2011-06-18 06:05:24 +0000 | [diff] [blame] | 267 | while (!RegionEnd->use_empty()) |
| 268 | cast<CallInst>(RegionEnd->use_back())->eraseFromParent(); |
Devang Patel | be94f23 | 2010-01-05 01:10:40 +0000 | [diff] [blame] | 269 | RegionEnd->eraseFromParent(); |
Devang Patel | 80ae349 | 2009-08-28 23:24:31 +0000 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | if (Function *Declare = M->getFunction("llvm.dbg.declare")) { |
| 273 | if (!Declare->use_empty()) { |
| 274 | DbgDeclareInst *DDI = cast<DbgDeclareInst>(Declare->use_back()); |
Gabor Greif | c89d2aa | 2010-06-22 20:40:38 +0000 | [diff] [blame] | 275 | if (!isa<MDNode>(DDI->getArgOperand(0)) || |
| 276 | !isa<MDNode>(DDI->getArgOperand(1))) { |
Devang Patel | 80ae349 | 2009-08-28 23:24:31 +0000 | [diff] [blame] | 277 | while (!Declare->use_empty()) { |
| 278 | CallInst *CI = cast<CallInst>(Declare->use_back()); |
| 279 | CI->eraseFromParent(); |
| 280 | } |
| 281 | Declare->eraseFromParent(); |
| 282 | } |
| 283 | } |
| 284 | } |
| 285 | } |
Bill Wendling | 45449b1 | 2011-08-25 23:22:40 +0000 | [diff] [blame^] | 286 | |
| 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, |
| 290 | CallInst *&Sel, |
| 291 | SmallPtrSet<BasicBlock*, 8> &Visited) { |
| 292 | if (!Visited.insert(BB)) return; |
| 293 | |
| 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()) { |
| 298 | default: break; |
| 299 | case Intrinsic::eh_exception: |
| 300 | assert(!Exn && "Found more than one eh.exception call!"); |
| 301 | Exn = CI; |
| 302 | break; |
| 303 | case Intrinsic::eh_selector: |
| 304 | assert(!Sel && "Found more than one eh.selector call!"); |
| 305 | Sel = CI; |
| 306 | break; |
| 307 | } |
| 308 | |
| 309 | if (Exn && Sel) return; |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | if (Exn && Sel) return; |
| 314 | |
| 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; |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | /// TransferClausesToLandingPadInst - Transfer the exception handling clauses |
| 322 | /// from the eh_selector call to the new landingpad instruction. |
| 323 | static void TransferClausesToLandingPadInst(LandingPadInst *LPI, |
| 324 | CallInst *EHSel) { |
| 325 | LLVMContext &Context = LPI->getContext(); |
| 326 | unsigned N = EHSel->getNumArgOperands(); |
| 327 | |
| 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"); |
| 333 | |
| 334 | if (FirstCatch < N) |
| 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)); |
| 339 | } else { |
| 340 | GlobalVariable *GV = cast<GlobalVariable>(Val); |
| 341 | LPI->addClause(GV->getInitializer()); |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | if (!FilterLength) { |
| 346 | // Cleanup. |
| 347 | LPI->setCleanup(true); |
| 348 | } else { |
| 349 | // Filter. |
| 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))); |
| 354 | ArrayType *AType = |
| 355 | ArrayType::get(!TyInfo.empty() ? TyInfo[0]->getType() : |
| 356 | PointerType::getUnqual(Type::getInt8Ty(Context)), |
| 357 | TyInfo.size()); |
| 358 | LPI->addClause(ConstantArray::get(AType, TyInfo)); |
| 359 | } |
| 360 | |
| 361 | N = i; |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | if (N > 2) |
| 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)); |
| 370 | } else { |
| 371 | GlobalVariable *GV = cast<GlobalVariable>(Val); |
| 372 | LPI->addClause(GV->getInitializer()); |
| 373 | } |
| 374 | } |
| 375 | } |
| 376 | |
| 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) |
| 383 | return; |
| 384 | |
| 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); |
| 389 | |
| 390 | // This map stores the slots where the exception object and selector value are |
| 391 | // stored within a function. |
| 392 | SmallVector<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) { |
| 396 | Function &F = *I; |
| 397 | |
| 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()); |
| 402 | if (!Inst) continue; |
| 403 | BasicBlock *UnwindDest = Inst->getUnwindDest(); |
| 404 | if (UnwindDest->isLandingPad()) continue; // All ready converted. |
| 405 | |
| 406 | // Store the exception object and selector value in the entry block. |
| 407 | Value *ExnSlot = 0; |
| 408 | Value *SelSlot = 0; |
| 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); |
| 414 | } else { |
| 415 | ExnSlot = FnToLPadSlotMap[&F].first; |
| 416 | SelSlot = FnToLPadSlotMap[&F].second; |
| 417 | } |
| 418 | |
| 419 | // We're in an unwind block. Try to find the eh.exception and eh.selector |
| 420 | // calls. |
| 421 | IRBuilder<> Builder(Context); |
| 422 | Builder.SetInsertPoint(UnwindDest, UnwindDest->getFirstNonPHI()); |
| 423 | |
| 424 | SmallPtrSet<BasicBlock*, 8> Visited; |
| 425 | CallInst *Exn = 0; |
| 426 | CallInst *Sel = 0; |
| 427 | FindExnAndSelIntrinsics(UnwindDest, Exn, Sel, Visited); |
| 428 | assert(Exn && Sel && "Cannot find eh.exception and eh.selector calls!"); |
| 429 | |
| 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); |
| 436 | |
| 437 | TransferClausesToLandingPadInst(LPI, Sel); |
| 438 | |
| 439 | Exn->replaceAllUsesWith(LPExn); |
| 440 | Sel->replaceAllUsesWith(LPSel); |
| 441 | |
| 442 | DeadInsts.push_back(Exn); |
| 443 | DeadInsts.push_back(Sel); |
| 444 | } |
| 445 | } |
| 446 | |
| 447 | // Remove the dead instructions. |
| 448 | while (!DeadInsts.empty()) { |
| 449 | Instruction *Inst = DeadInsts.pop_back_val(); |
| 450 | Inst->eraseFromParent(); |
| 451 | } |
| 452 | |
| 453 | // Replace calls to "llvm.eh.resume" with the 'resume' instruction. Load the |
| 454 | // exception and selector values from the stored place. |
| 455 | Function *EHResume = M->getFunction("llvm.eh.resume"); |
| 456 | if (!EHResume) return; |
| 457 | |
| 458 | while (!EHResume->use_empty()) { |
| 459 | CallInst *Resume = cast<CallInst>(EHResume->use_back()); |
| 460 | BasicBlock *BB = Resume->getParent(); |
| 461 | Function *Fn = BB->getParent(); |
| 462 | std::pair<Value*, Value*> &ExnSel = FnToLPadSlotMap[Fn]; |
| 463 | IRBuilder<> Builder(Context); |
| 464 | Builder.SetInsertPoint(BB, Resume); |
| 465 | |
| 466 | Value *Exn = Builder.CreateLoad(ExnSel.first, "exn"); |
| 467 | Value *Sel = Builder.CreateLoad(ExnSel.second, "sel"); |
| 468 | |
| 469 | Value *LPadVal = |
| 470 | Builder.CreateInsertValue(UndefValue::get(LPadSlotTy), |
| 471 | Exn, 0, "lpad.val"); |
| 472 | LPadVal = Builder.CreateInsertValue(LPadVal, Sel, 1, "lpad.val"); |
| 473 | Builder.CreateResume(LPadVal); |
| 474 | |
| 475 | // Remove all instructions after the 'resume.' |
| 476 | BasicBlock::iterator I = Resume; |
| 477 | while (I != BB->end()) { |
| 478 | Instruction *Inst = &*I++; |
| 479 | Inst->eraseFromParent(); |
| 480 | } |
| 481 | } |
| 482 | } |