Dan Gohman | 6277eb2 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 1 | //===-- FunctionLoweringInfo.cpp ------------------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This implements routines for translating functions from LLVM IR into |
| 11 | // Machine IR. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #define DEBUG_TYPE "function-lowering-info" |
Dan Gohman | 4c3fd9f | 2010-07-07 16:01:37 +0000 | [diff] [blame] | 16 | #include "llvm/CodeGen/FunctionLoweringInfo.h" |
Dan Gohman | 6277eb2 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 17 | #include "llvm/DerivedTypes.h" |
| 18 | #include "llvm/Function.h" |
| 19 | #include "llvm/Instructions.h" |
Dan Gohman | 5fca8b1 | 2009-11-23 18:12:11 +0000 | [diff] [blame] | 20 | #include "llvm/IntrinsicInst.h" |
Dan Gohman | 6277eb2 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 21 | #include "llvm/LLVMContext.h" |
| 22 | #include "llvm/Module.h" |
Dan Gohman | 9c3d5e4 | 2010-07-16 17:54:27 +0000 | [diff] [blame] | 23 | #include "llvm/Analysis/DebugInfo.h" |
Dan Gohman | 5eb6d65 | 2010-04-21 01:22:34 +0000 | [diff] [blame] | 24 | #include "llvm/CodeGen/Analysis.h" |
Dan Gohman | 6277eb2 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 25 | #include "llvm/CodeGen/MachineFunction.h" |
| 26 | #include "llvm/CodeGen/MachineFrameInfo.h" |
| 27 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| 28 | #include "llvm/CodeGen/MachineModuleInfo.h" |
| 29 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Dan Gohman | 6277eb2 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 30 | #include "llvm/Target/TargetRegisterInfo.h" |
| 31 | #include "llvm/Target/TargetData.h" |
Dan Gohman | 6277eb2 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 32 | #include "llvm/Target/TargetInstrInfo.h" |
Dan Gohman | 6277eb2 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 33 | #include "llvm/Target/TargetLowering.h" |
| 34 | #include "llvm/Target/TargetOptions.h" |
Dan Gohman | 6277eb2 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 35 | #include "llvm/Support/Debug.h" |
| 36 | #include "llvm/Support/ErrorHandling.h" |
| 37 | #include "llvm/Support/MathExtras.h" |
Dan Gohman | 6277eb2 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 38 | #include <algorithm> |
| 39 | using namespace llvm; |
| 40 | |
Dan Gohman | 6277eb2 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 41 | /// isUsedOutsideOfDefiningBlock - Return true if this instruction is used by |
| 42 | /// PHI nodes or outside of the basic block that defines it, or used by a |
| 43 | /// switch or atomic instruction, which may expand to multiple basic blocks. |
Dan Gohman | ae541aa | 2010-04-15 04:33:49 +0000 | [diff] [blame] | 44 | static bool isUsedOutsideOfDefiningBlock(const Instruction *I) { |
Dan Gohman | d84e806 | 2010-04-20 14:50:13 +0000 | [diff] [blame] | 45 | if (I->use_empty()) return false; |
Dan Gohman | 6277eb2 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 46 | if (isa<PHINode>(I)) return true; |
Dan Gohman | ae541aa | 2010-04-15 04:33:49 +0000 | [diff] [blame] | 47 | const BasicBlock *BB = I->getParent(); |
| 48 | for (Value::const_use_iterator UI = I->use_begin(), E = I->use_end(); |
Gabor Greif | 03f09a3 | 2010-07-09 16:08:33 +0000 | [diff] [blame] | 49 | UI != E; ++UI) { |
| 50 | const User *U = *UI; |
| 51 | if (cast<Instruction>(U)->getParent() != BB || isa<PHINode>(U)) |
Dan Gohman | 6277eb2 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 52 | return true; |
Gabor Greif | 03f09a3 | 2010-07-09 16:08:33 +0000 | [diff] [blame] | 53 | } |
Dan Gohman | 6277eb2 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 54 | return false; |
| 55 | } |
| 56 | |
| 57 | /// isOnlyUsedInEntryBlock - If the specified argument is only used in the |
| 58 | /// entry block, return true. This includes arguments used by switches, since |
| 59 | /// the switch may expand into multiple basic blocks. |
Dan Gohman | ae541aa | 2010-04-15 04:33:49 +0000 | [diff] [blame] | 60 | static bool isOnlyUsedInEntryBlock(const Argument *A, bool EnableFastISel) { |
Dan Gohman | 6277eb2 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 61 | // With FastISel active, we may be splitting blocks, so force creation |
| 62 | // of virtual registers for all non-dead arguments. |
Dan Gohman | d725f04 | 2010-05-01 02:44:23 +0000 | [diff] [blame] | 63 | if (EnableFastISel) |
Dan Gohman | 6277eb2 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 64 | return A->use_empty(); |
| 65 | |
Dan Gohman | ae541aa | 2010-04-15 04:33:49 +0000 | [diff] [blame] | 66 | const BasicBlock *Entry = A->getParent()->begin(); |
| 67 | for (Value::const_use_iterator UI = A->use_begin(), E = A->use_end(); |
Gabor Greif | 03f09a3 | 2010-07-09 16:08:33 +0000 | [diff] [blame] | 68 | UI != E; ++UI) { |
| 69 | const User *U = *UI; |
| 70 | if (cast<Instruction>(U)->getParent() != Entry || isa<SwitchInst>(U)) |
Dan Gohman | 6277eb2 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 71 | return false; // Use not in entry block. |
Gabor Greif | 03f09a3 | 2010-07-09 16:08:33 +0000 | [diff] [blame] | 72 | } |
Dan Gohman | 6277eb2 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 73 | return true; |
| 74 | } |
| 75 | |
Dan Gohman | d858e90 | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 76 | FunctionLoweringInfo::FunctionLoweringInfo(const TargetLowering &tli) |
Dan Gohman | 6277eb2 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 77 | : TLI(tli) { |
| 78 | } |
| 79 | |
Dan Gohman | 7451d3e | 2010-05-29 17:03:36 +0000 | [diff] [blame] | 80 | void FunctionLoweringInfo::set(const Function &fn, MachineFunction &mf) { |
Dan Gohman | 6277eb2 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 81 | Fn = &fn; |
| 82 | MF = &mf; |
| 83 | RegInfo = &MF->getRegInfo(); |
| 84 | |
Dan Gohman | 84023e0 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 85 | // Check whether the function can return without sret-demotion. |
| 86 | SmallVector<ISD::OutputArg, 4> Outs; |
| 87 | GetReturnInfo(Fn->getReturnType(), |
| 88 | Fn->getAttributes().getRetAttributes(), Outs, TLI); |
| 89 | CanLowerReturn = TLI.CanLowerReturn(Fn->getCallingConv(), Fn->isVarArg(), |
| 90 | Outs, Fn->getContext()); |
| 91 | |
Dan Gohman | 6277eb2 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 92 | // Create a vreg for each argument register that is not dead and is used |
| 93 | // outside of the entry block for the function. |
Dan Gohman | ae541aa | 2010-04-15 04:33:49 +0000 | [diff] [blame] | 94 | for (Function::const_arg_iterator AI = Fn->arg_begin(), E = Fn->arg_end(); |
Dan Gohman | 6277eb2 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 95 | AI != E; ++AI) |
| 96 | if (!isOnlyUsedInEntryBlock(AI, EnableFastISel)) |
| 97 | InitializeRegForValue(AI); |
| 98 | |
| 99 | // Initialize the mapping of values to registers. This is only set up for |
| 100 | // instruction values that are used outside of the block that defines |
| 101 | // them. |
Dan Gohman | ae541aa | 2010-04-15 04:33:49 +0000 | [diff] [blame] | 102 | Function::const_iterator BB = Fn->begin(), EB = Fn->end(); |
| 103 | for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I) |
| 104 | if (const AllocaInst *AI = dyn_cast<AllocaInst>(I)) |
| 105 | if (const ConstantInt *CUI = dyn_cast<ConstantInt>(AI->getArraySize())) { |
Dan Gohman | 6277eb2 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 106 | const Type *Ty = AI->getAllocatedType(); |
| 107 | uint64_t TySize = TLI.getTargetData()->getTypeAllocSize(Ty); |
| 108 | unsigned Align = |
| 109 | std::max((unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty), |
| 110 | AI->getAlignment()); |
| 111 | |
| 112 | TySize *= CUI->getZExtValue(); // Get total allocated size. |
| 113 | if (TySize == 0) TySize = 1; // Don't create zero-sized stack objects. |
Bill Wendling | dfc2c51 | 2010-07-27 01:55:19 +0000 | [diff] [blame] | 114 | |
| 115 | // The object may need to be placed onto the stack near the stack |
| 116 | // protector if one exists. Determine here if this object is a suitable |
| 117 | // candidate. I.e., it would trigger the creation of a stack protector. |
| 118 | bool MayNeedSP = |
| 119 | (AI->isArrayAllocation() || |
| 120 | (TySize > 8 && isa<ArrayType>(Ty) && |
| 121 | cast<ArrayType>(Ty)->getElementType()->isIntegerTy(8))); |
Dan Gohman | 6277eb2 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 122 | StaticAllocaMap[AI] = |
Bill Wendling | dfc2c51 | 2010-07-27 01:55:19 +0000 | [diff] [blame] | 123 | MF->getFrameInfo()->CreateStackObject(TySize, Align, false, MayNeedSP); |
Dan Gohman | 6277eb2 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | for (; BB != EB; ++BB) |
Dan Gohman | 9c3d5e4 | 2010-07-16 17:54:27 +0000 | [diff] [blame] | 127 | for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I) { |
| 128 | // Mark values used outside their block as exported, by allocating |
| 129 | // a virtual register for them. |
Cameron Zwarich | 92efda7 | 2011-02-22 00:46:27 +0000 | [diff] [blame] | 130 | if (!EnableFastISel && isa<PHINode>(I)) { |
| 131 | PHIDestRegs.insert(InitializeRegForValue(I)); |
| 132 | } else if (isUsedOutsideOfDefiningBlock(I)) { |
Dan Gohman | 6277eb2 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 133 | if (!isa<AllocaInst>(I) || |
| 134 | !StaticAllocaMap.count(cast<AllocaInst>(I))) |
| 135 | InitializeRegForValue(I); |
Cameron Zwarich | 92efda7 | 2011-02-22 00:46:27 +0000 | [diff] [blame] | 136 | } |
Dan Gohman | 6277eb2 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 137 | |
Dan Gohman | 9c3d5e4 | 2010-07-16 17:54:27 +0000 | [diff] [blame] | 138 | // Collect llvm.dbg.declare information. This is done now instead of |
| 139 | // during the initial isel pass through the IR so that it is done |
| 140 | // in a predictable order. |
| 141 | if (const DbgDeclareInst *DI = dyn_cast<DbgDeclareInst>(I)) { |
| 142 | MachineModuleInfo &MMI = MF->getMMI(); |
| 143 | if (MMI.hasDebugInfo() && |
| 144 | DIVariable(DI->getVariable()).Verify() && |
| 145 | !DI->getDebugLoc().isUnknown()) { |
| 146 | // Don't handle byval struct arguments or VLAs, for example. |
| 147 | // Non-byval arguments are handled here (they refer to the stack |
| 148 | // temporary alloca at this point). |
| 149 | const Value *Address = DI->getAddress(); |
| 150 | if (Address) { |
| 151 | if (const BitCastInst *BCI = dyn_cast<BitCastInst>(Address)) |
| 152 | Address = BCI->getOperand(0); |
| 153 | if (const AllocaInst *AI = dyn_cast<AllocaInst>(Address)) { |
| 154 | DenseMap<const AllocaInst *, int>::iterator SI = |
| 155 | StaticAllocaMap.find(AI); |
| 156 | if (SI != StaticAllocaMap.end()) { // Check for VLAs. |
| 157 | int FI = SI->second; |
| 158 | MMI.setVariableDbgInfo(DI->getVariable(), |
| 159 | FI, DI->getDebugLoc()); |
| 160 | } |
| 161 | } |
| 162 | } |
| 163 | } |
| 164 | } |
| 165 | } |
| 166 | |
Dan Gohman | 6277eb2 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 167 | // Create an initial MachineBasicBlock for each LLVM BasicBlock in F. This |
| 168 | // also creates the initial PHI MachineInstrs, though none of the input |
| 169 | // operands are populated. |
Dan Gohman | d0d8275 | 2010-04-14 16:30:40 +0000 | [diff] [blame] | 170 | for (BB = Fn->begin(); BB != EB; ++BB) { |
Dan Gohman | 6277eb2 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 171 | MachineBasicBlock *MBB = mf.CreateMachineBasicBlock(BB); |
| 172 | MBBMap[BB] = MBB; |
| 173 | MF->push_back(MBB); |
| 174 | |
| 175 | // Transfer the address-taken flag. This is necessary because there could |
| 176 | // be multiple MachineBasicBlocks corresponding to one BasicBlock, and only |
| 177 | // the first one should be marked. |
| 178 | if (BB->hasAddressTaken()) |
| 179 | MBB->setHasAddressTaken(); |
| 180 | |
| 181 | // Create Machine PHI nodes for LLVM PHI nodes, lowering them as |
| 182 | // appropriate. |
Dan Gohman | 3f1403f | 2010-04-20 14:46:25 +0000 | [diff] [blame] | 183 | for (BasicBlock::const_iterator I = BB->begin(); |
| 184 | const PHINode *PN = dyn_cast<PHINode>(I); ++I) { |
| 185 | if (PN->use_empty()) continue; |
Dan Gohman | 6277eb2 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 186 | |
Dan Gohman | c025c85 | 2010-04-20 14:48:02 +0000 | [diff] [blame] | 187 | DebugLoc DL = PN->getDebugLoc(); |
Dan Gohman | 6277eb2 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 188 | unsigned PHIReg = ValueMap[PN]; |
| 189 | assert(PHIReg && "PHI node does not have an assigned virtual register!"); |
| 190 | |
| 191 | SmallVector<EVT, 4> ValueVTs; |
| 192 | ComputeValueVTs(TLI, PN->getType(), ValueVTs); |
| 193 | for (unsigned vti = 0, vte = ValueVTs.size(); vti != vte; ++vti) { |
| 194 | EVT VT = ValueVTs[vti]; |
| 195 | unsigned NumRegisters = TLI.getNumRegisters(Fn->getContext(), VT); |
| 196 | const TargetInstrInfo *TII = MF->getTarget().getInstrInfo(); |
| 197 | for (unsigned i = 0; i != NumRegisters; ++i) |
Chris Lattner | 518bb53 | 2010-02-09 19:54:29 +0000 | [diff] [blame] | 198 | BuildMI(MBB, DL, TII->get(TargetOpcode::PHI), PHIReg + i); |
Dan Gohman | 6277eb2 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 199 | PHIReg += NumRegisters; |
| 200 | } |
| 201 | } |
| 202 | } |
Dan Gohman | de4c0a7 | 2010-04-14 16:32:56 +0000 | [diff] [blame] | 203 | |
| 204 | // Mark landing pad blocks. |
| 205 | for (BB = Fn->begin(); BB != EB; ++BB) |
Dan Gohman | ae541aa | 2010-04-15 04:33:49 +0000 | [diff] [blame] | 206 | if (const InvokeInst *Invoke = dyn_cast<InvokeInst>(BB->getTerminator())) |
Dan Gohman | de4c0a7 | 2010-04-14 16:32:56 +0000 | [diff] [blame] | 207 | MBBMap[Invoke->getSuccessor(1)]->setIsLandingPad(); |
Dan Gohman | 6277eb2 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | /// clear - Clear out all the function-specific state. This returns this |
| 211 | /// FunctionLoweringInfo to an empty state, ready to be used for a |
| 212 | /// different function. |
| 213 | void FunctionLoweringInfo::clear() { |
Dan Gohman | 0e02672 | 2010-04-14 17:11:23 +0000 | [diff] [blame] | 214 | assert(CatchInfoFound.size() == CatchInfoLost.size() && |
| 215 | "Not all catch info was assigned to a landing pad!"); |
| 216 | |
Dan Gohman | 6277eb2 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 217 | MBBMap.clear(); |
| 218 | ValueMap.clear(); |
| 219 | StaticAllocaMap.clear(); |
| 220 | #ifndef NDEBUG |
| 221 | CatchInfoLost.clear(); |
| 222 | CatchInfoFound.clear(); |
| 223 | #endif |
| 224 | LiveOutRegInfo.clear(); |
Cameron Zwarich | 92efda7 | 2011-02-22 00:46:27 +0000 | [diff] [blame] | 225 | VisitedBBs.clear(); |
| 226 | PHIDestRegs.clear(); |
| 227 | PHISrcToDestMap.clear(); |
Evan Cheng | 2ad0fcf | 2010-04-28 23:08:54 +0000 | [diff] [blame] | 228 | ArgDbgValues.clear(); |
Devang Patel | 0b48ead | 2010-08-31 22:22:42 +0000 | [diff] [blame] | 229 | ByValArgFrameIndexMap.clear(); |
Dan Gohman | 84023e0 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 230 | RegFixups.clear(); |
Dan Gohman | 6277eb2 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 231 | } |
| 232 | |
Dan Gohman | 89496d0 | 2010-07-02 00:10:16 +0000 | [diff] [blame] | 233 | /// CreateReg - Allocate a single virtual register for the given type. |
| 234 | unsigned FunctionLoweringInfo::CreateReg(EVT VT) { |
Dan Gohman | 6277eb2 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 235 | return RegInfo->createVirtualRegister(TLI.getRegClassFor(VT)); |
| 236 | } |
| 237 | |
Dan Gohman | 89496d0 | 2010-07-02 00:10:16 +0000 | [diff] [blame] | 238 | /// CreateRegs - Allocate the appropriate number of virtual registers of |
Dan Gohman | 6277eb2 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 239 | /// the correctly promoted or expanded types. Assign these registers |
| 240 | /// consecutive vreg numbers and return the first assigned number. |
| 241 | /// |
| 242 | /// In the case that the given value has struct or array type, this function |
| 243 | /// will assign registers for each member or element. |
| 244 | /// |
Dan Gohman | 89496d0 | 2010-07-02 00:10:16 +0000 | [diff] [blame] | 245 | unsigned FunctionLoweringInfo::CreateRegs(const Type *Ty) { |
Dan Gohman | 6277eb2 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 246 | SmallVector<EVT, 4> ValueVTs; |
Dan Gohman | ffda6ba | 2010-07-01 03:55:39 +0000 | [diff] [blame] | 247 | ComputeValueVTs(TLI, Ty, ValueVTs); |
Dan Gohman | 6277eb2 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 248 | |
| 249 | unsigned FirstReg = 0; |
| 250 | for (unsigned Value = 0, e = ValueVTs.size(); Value != e; ++Value) { |
| 251 | EVT ValueVT = ValueVTs[Value]; |
Dan Gohman | ffda6ba | 2010-07-01 03:55:39 +0000 | [diff] [blame] | 252 | EVT RegisterVT = TLI.getRegisterType(Ty->getContext(), ValueVT); |
Dan Gohman | 6277eb2 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 253 | |
Dan Gohman | ffda6ba | 2010-07-01 03:55:39 +0000 | [diff] [blame] | 254 | unsigned NumRegs = TLI.getNumRegisters(Ty->getContext(), ValueVT); |
Dan Gohman | 6277eb2 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 255 | for (unsigned i = 0; i != NumRegs; ++i) { |
Dan Gohman | 89496d0 | 2010-07-02 00:10:16 +0000 | [diff] [blame] | 256 | unsigned R = CreateReg(RegisterVT); |
Dan Gohman | 6277eb2 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 257 | if (!FirstReg) FirstReg = R; |
| 258 | } |
| 259 | } |
| 260 | return FirstReg; |
| 261 | } |
Dan Gohman | 66336ed | 2009-11-23 17:42:46 +0000 | [diff] [blame] | 262 | |
Devang Patel | 0b48ead | 2010-08-31 22:22:42 +0000 | [diff] [blame] | 263 | /// setByValArgumentFrameIndex - Record frame index for the byval |
| 264 | /// argument. This overrides previous frame index entry for this argument, |
| 265 | /// if any. |
| 266 | void FunctionLoweringInfo::setByValArgumentFrameIndex(const Argument *A, |
| 267 | int FI) { |
| 268 | assert (A->hasByValAttr() && "Argument does not have byval attribute!"); |
| 269 | ByValArgFrameIndexMap[A] = FI; |
| 270 | } |
| 271 | |
| 272 | /// getByValArgumentFrameIndex - Get frame index for the byval argument. |
| 273 | /// If the argument does not have any assigned frame index then 0 is |
| 274 | /// returned. |
| 275 | int FunctionLoweringInfo::getByValArgumentFrameIndex(const Argument *A) { |
| 276 | assert (A->hasByValAttr() && "Argument does not have byval attribute!"); |
| 277 | DenseMap<const Argument *, int>::iterator I = |
| 278 | ByValArgFrameIndexMap.find(A); |
| 279 | if (I != ByValArgFrameIndexMap.end()) |
| 280 | return I->second; |
| 281 | DEBUG(dbgs() << "Argument does not have assigned frame index!"); |
| 282 | return 0; |
| 283 | } |
| 284 | |
Dan Gohman | 66336ed | 2009-11-23 17:42:46 +0000 | [diff] [blame] | 285 | /// AddCatchInfo - Extract the personality and type infos from an eh.selector |
| 286 | /// call, and add them to the specified machine basic block. |
Dan Gohman | 2520864 | 2010-04-14 19:53:31 +0000 | [diff] [blame] | 287 | void llvm::AddCatchInfo(const CallInst &I, MachineModuleInfo *MMI, |
Dan Gohman | 66336ed | 2009-11-23 17:42:46 +0000 | [diff] [blame] | 288 | MachineBasicBlock *MBB) { |
| 289 | // Inform the MachineModuleInfo of the personality for this landing pad. |
Gabor Greif | 1518444 | 2010-06-25 08:24:59 +0000 | [diff] [blame] | 290 | const ConstantExpr *CE = cast<ConstantExpr>(I.getArgOperand(1)); |
Dan Gohman | 66336ed | 2009-11-23 17:42:46 +0000 | [diff] [blame] | 291 | assert(CE->getOpcode() == Instruction::BitCast && |
| 292 | isa<Function>(CE->getOperand(0)) && |
| 293 | "Personality should be a function"); |
| 294 | MMI->addPersonality(MBB, cast<Function>(CE->getOperand(0))); |
| 295 | |
| 296 | // Gather all the type infos for this landing pad and pass them along to |
| 297 | // MachineModuleInfo. |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 298 | std::vector<const GlobalVariable *> TyInfo; |
Gabor Greif | e767e6b | 2010-06-30 13:45:50 +0000 | [diff] [blame] | 299 | unsigned N = I.getNumArgOperands(); |
Dan Gohman | 66336ed | 2009-11-23 17:42:46 +0000 | [diff] [blame] | 300 | |
Gabor Greif | e767e6b | 2010-06-30 13:45:50 +0000 | [diff] [blame] | 301 | for (unsigned i = N - 1; i > 1; --i) { |
| 302 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(I.getArgOperand(i))) { |
Dan Gohman | 66336ed | 2009-11-23 17:42:46 +0000 | [diff] [blame] | 303 | unsigned FilterLength = CI->getZExtValue(); |
| 304 | unsigned FirstCatch = i + FilterLength + !FilterLength; |
Gabor Greif | e767e6b | 2010-06-30 13:45:50 +0000 | [diff] [blame] | 305 | assert(FirstCatch <= N && "Invalid filter length"); |
Dan Gohman | 66336ed | 2009-11-23 17:42:46 +0000 | [diff] [blame] | 306 | |
| 307 | if (FirstCatch < N) { |
| 308 | TyInfo.reserve(N - FirstCatch); |
| 309 | for (unsigned j = FirstCatch; j < N; ++j) |
Gabor Greif | e767e6b | 2010-06-30 13:45:50 +0000 | [diff] [blame] | 310 | TyInfo.push_back(ExtractTypeInfo(I.getArgOperand(j))); |
Dan Gohman | 66336ed | 2009-11-23 17:42:46 +0000 | [diff] [blame] | 311 | MMI->addCatchTypeInfo(MBB, TyInfo); |
| 312 | TyInfo.clear(); |
| 313 | } |
| 314 | |
| 315 | if (!FilterLength) { |
| 316 | // Cleanup. |
| 317 | MMI->addCleanup(MBB); |
| 318 | } else { |
| 319 | // Filter. |
| 320 | TyInfo.reserve(FilterLength - 1); |
| 321 | for (unsigned j = i + 1; j < FirstCatch; ++j) |
Gabor Greif | e767e6b | 2010-06-30 13:45:50 +0000 | [diff] [blame] | 322 | TyInfo.push_back(ExtractTypeInfo(I.getArgOperand(j))); |
Dan Gohman | 66336ed | 2009-11-23 17:42:46 +0000 | [diff] [blame] | 323 | MMI->addFilterTypeInfo(MBB, TyInfo); |
| 324 | TyInfo.clear(); |
| 325 | } |
| 326 | |
| 327 | N = i; |
| 328 | } |
| 329 | } |
| 330 | |
Gabor Greif | e767e6b | 2010-06-30 13:45:50 +0000 | [diff] [blame] | 331 | if (N > 2) { |
| 332 | TyInfo.reserve(N - 2); |
| 333 | for (unsigned j = 2; j < N; ++j) |
| 334 | TyInfo.push_back(ExtractTypeInfo(I.getArgOperand(j))); |
Dan Gohman | 66336ed | 2009-11-23 17:42:46 +0000 | [diff] [blame] | 335 | MMI->addCatchTypeInfo(MBB, TyInfo); |
| 336 | } |
| 337 | } |
| 338 | |
Dan Gohman | 2520864 | 2010-04-14 19:53:31 +0000 | [diff] [blame] | 339 | void llvm::CopyCatchInfo(const BasicBlock *SrcBB, const BasicBlock *DestBB, |
Dan Gohman | 5fca8b1 | 2009-11-23 18:12:11 +0000 | [diff] [blame] | 340 | MachineModuleInfo *MMI, FunctionLoweringInfo &FLI) { |
Dan Gohman | 2520864 | 2010-04-14 19:53:31 +0000 | [diff] [blame] | 341 | for (BasicBlock::const_iterator I = SrcBB->begin(), E = --SrcBB->end(); |
| 342 | I != E; ++I) |
| 343 | if (const EHSelectorInst *EHSel = dyn_cast<EHSelectorInst>(I)) { |
Dan Gohman | 5fca8b1 | 2009-11-23 18:12:11 +0000 | [diff] [blame] | 344 | // Apply the catch info to DestBB. |
| 345 | AddCatchInfo(*EHSel, MMI, FLI.MBBMap[DestBB]); |
| 346 | #ifndef NDEBUG |
| 347 | if (!FLI.MBBMap[SrcBB]->isLandingPad()) |
| 348 | FLI.CatchInfoFound.insert(EHSel); |
| 349 | #endif |
| 350 | } |
| 351 | } |