Dan Gohman | a3624b6 | 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 | |
Dan Gohman | e784616 | 2010-07-07 16:01:37 +0000 | [diff] [blame] | 15 | #include "llvm/CodeGen/FunctionLoweringInfo.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/PostOrderIterator.h" |
| 17 | #include "llvm/CodeGen/Analysis.h" |
| 18 | #include "llvm/CodeGen/MachineFrameInfo.h" |
| 19 | #include "llvm/CodeGen/MachineFunction.h" |
| 20 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| 21 | #include "llvm/CodeGen/MachineModuleInfo.h" |
| 22 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
David Majnemer | cde3303 | 2015-03-30 22:58:10 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/WinEHFuncInfo.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 24 | #include "llvm/IR/DataLayout.h" |
Chandler Carruth | 9a4c9e5 | 2014-03-06 00:46:21 +0000 | [diff] [blame] | 25 | #include "llvm/IR/DebugInfo.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 26 | #include "llvm/IR/DerivedTypes.h" |
| 27 | #include "llvm/IR/Function.h" |
| 28 | #include "llvm/IR/Instructions.h" |
| 29 | #include "llvm/IR/IntrinsicInst.h" |
| 30 | #include "llvm/IR/LLVMContext.h" |
| 31 | #include "llvm/IR/Module.h" |
Dan Gohman | a3624b6 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 32 | #include "llvm/Support/Debug.h" |
| 33 | #include "llvm/Support/ErrorHandling.h" |
| 34 | #include "llvm/Support/MathExtras.h" |
Benjamin Kramer | 799003b | 2015-03-23 19:32:43 +0000 | [diff] [blame] | 35 | #include "llvm/Support/raw_ostream.h" |
Hans Wennborg | acb842d | 2014-03-05 02:43:26 +0000 | [diff] [blame] | 36 | #include "llvm/Target/TargetFrameLowering.h" |
Chandler Carruth | 9205140 | 2014-03-05 10:30:38 +0000 | [diff] [blame] | 37 | #include "llvm/Target/TargetInstrInfo.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 38 | #include "llvm/Target/TargetLowering.h" |
| 39 | #include "llvm/Target/TargetOptions.h" |
| 40 | #include "llvm/Target/TargetRegisterInfo.h" |
Eric Christopher | d913448 | 2014-08-04 21:25:23 +0000 | [diff] [blame] | 41 | #include "llvm/Target/TargetSubtargetInfo.h" |
Dan Gohman | a3624b6 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 42 | #include <algorithm> |
| 43 | using namespace llvm; |
| 44 | |
Chandler Carruth | 1b9dde0 | 2014-04-22 02:02:50 +0000 | [diff] [blame] | 45 | #define DEBUG_TYPE "function-lowering-info" |
| 46 | |
Dan Gohman | a3624b6 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 47 | /// isUsedOutsideOfDefiningBlock - Return true if this instruction is used by |
| 48 | /// PHI nodes or outside of the basic block that defines it, or used by a |
| 49 | /// switch or atomic instruction, which may expand to multiple basic blocks. |
Dan Gohman | 913c998 | 2010-04-15 04:33:49 +0000 | [diff] [blame] | 50 | static bool isUsedOutsideOfDefiningBlock(const Instruction *I) { |
Dan Gohman | 7c845e4 | 2010-04-20 14:50:13 +0000 | [diff] [blame] | 51 | if (I->use_empty()) return false; |
Dan Gohman | a3624b6 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 52 | if (isa<PHINode>(I)) return true; |
Dan Gohman | 913c998 | 2010-04-15 04:33:49 +0000 | [diff] [blame] | 53 | const BasicBlock *BB = I->getParent(); |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 54 | for (const User *U : I->users()) |
Gabor Greif | 52617fc | 2010-07-09 16:08:33 +0000 | [diff] [blame] | 55 | if (cast<Instruction>(U)->getParent() != BB || isa<PHINode>(U)) |
Dan Gohman | a3624b6 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 56 | return true; |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 57 | |
Dan Gohman | a3624b6 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 58 | return false; |
| 59 | } |
| 60 | |
Jiangning Liu | ffbc690 | 2014-09-19 05:30:35 +0000 | [diff] [blame] | 61 | static ISD::NodeType getPreferredExtendForValue(const Value *V) { |
| 62 | // For the users of the source value being used for compare instruction, if |
| 63 | // the number of signed predicate is greater than unsigned predicate, we |
| 64 | // prefer to use SIGN_EXTEND. |
| 65 | // |
| 66 | // With this optimization, we would be able to reduce some redundant sign or |
| 67 | // zero extension instruction, and eventually more machine CSE opportunities |
| 68 | // can be exposed. |
| 69 | ISD::NodeType ExtendKind = ISD::ANY_EXTEND; |
| 70 | unsigned NumOfSigned = 0, NumOfUnsigned = 0; |
| 71 | for (const User *U : V->users()) { |
| 72 | if (const auto *CI = dyn_cast<CmpInst>(U)) { |
| 73 | NumOfSigned += CI->isSigned(); |
| 74 | NumOfUnsigned += CI->isUnsigned(); |
| 75 | } |
| 76 | } |
| 77 | if (NumOfSigned > NumOfUnsigned) |
| 78 | ExtendKind = ISD::SIGN_EXTEND; |
| 79 | |
| 80 | return ExtendKind; |
| 81 | } |
| 82 | |
David Majnemer | cde3303 | 2015-03-30 22:58:10 +0000 | [diff] [blame] | 83 | namespace { |
| 84 | struct WinEHNumbering { |
| 85 | WinEHNumbering(WinEHFuncInfo &FuncInfo) : FuncInfo(FuncInfo), NextState(0) {} |
| 86 | |
| 87 | WinEHFuncInfo &FuncInfo; |
| 88 | int NextState; |
| 89 | |
| 90 | SmallVector<ActionHandler *, 4> HandlerStack; |
David Majnemer | a225a19 | 2015-03-31 22:35:44 +0000 | [diff] [blame] | 91 | SmallPtrSet<const Function *, 4> VisitedHandlers; |
David Majnemer | cde3303 | 2015-03-30 22:58:10 +0000 | [diff] [blame] | 92 | |
| 93 | int currentEHNumber() const { |
| 94 | return HandlerStack.empty() ? -1 : HandlerStack.back()->getEHState(); |
| 95 | } |
| 96 | |
David Majnemer | cde3303 | 2015-03-30 22:58:10 +0000 | [diff] [blame] | 97 | void createUnwindMapEntry(int ToState, ActionHandler *AH); |
David Majnemer | a225a19 | 2015-03-31 22:35:44 +0000 | [diff] [blame] | 98 | void createTryBlockMapEntry(int TryLow, int TryHigh, |
| 99 | ArrayRef<CatchHandler *> Handlers); |
| 100 | void processCallSite(ArrayRef<ActionHandler *> Actions, ImmutableCallSite CS); |
David Majnemer | cde3303 | 2015-03-30 22:58:10 +0000 | [diff] [blame] | 101 | void calculateStateNumbers(const Function &F); |
| 102 | }; |
| 103 | } |
| 104 | |
Hans Wennborg | acb842d | 2014-03-05 02:43:26 +0000 | [diff] [blame] | 105 | void FunctionLoweringInfo::set(const Function &fn, MachineFunction &mf, |
| 106 | SelectionDAG *DAG) { |
Dan Gohman | a3624b6 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 107 | Fn = &fn; |
| 108 | MF = &mf; |
Eric Christopher | 2ae2de7 | 2014-10-09 00:57:31 +0000 | [diff] [blame] | 109 | TLI = MF->getSubtarget().getTargetLowering(); |
Dan Gohman | a3624b6 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 110 | RegInfo = &MF->getRegInfo(); |
David Majnemer | cde3303 | 2015-03-30 22:58:10 +0000 | [diff] [blame] | 111 | MachineModuleInfo &MMI = MF->getMMI(); |
Dan Gohman | a3624b6 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 112 | |
Dan Gohman | d7b5ce3 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 113 | // Check whether the function can return without sret-demotion. |
| 114 | SmallVector<ISD::OutputArg, 4> Outs; |
Bill Wendling | 8db01cb | 2013-06-06 00:11:39 +0000 | [diff] [blame] | 115 | GetReturnInfo(Fn->getReturnType(), Fn->getAttributes(), Outs, *TLI); |
| 116 | CanLowerReturn = TLI->CanLowerReturn(Fn->getCallingConv(), *MF, |
Eric Christopher | 2ae2de7 | 2014-10-09 00:57:31 +0000 | [diff] [blame] | 117 | Fn->isVarArg(), Outs, Fn->getContext()); |
Dan Gohman | d7b5ce3 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 118 | |
Dan Gohman | a3624b6 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 119 | // Initialize the mapping of values to registers. This is only set up for |
| 120 | // instruction values that are used outside of the block that defines |
| 121 | // them. |
Dan Gohman | 913c998 | 2010-04-15 04:33:49 +0000 | [diff] [blame] | 122 | Function::const_iterator BB = Fn->begin(), EB = Fn->end(); |
Dan Gohman | a3624b6 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 123 | for (; BB != EB; ++BB) |
Eric Christopher | 219d51d | 2012-02-24 01:59:01 +0000 | [diff] [blame] | 124 | for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); |
| 125 | I != E; ++I) { |
Hans Wennborg | acb842d | 2014-03-05 02:43:26 +0000 | [diff] [blame] | 126 | if (const AllocaInst *AI = dyn_cast<AllocaInst>(I)) { |
Reid Kleckner | 0b2bccc | 2014-09-02 18:42:44 +0000 | [diff] [blame] | 127 | // Static allocas can be folded into the initial stack frame adjustment. |
| 128 | if (AI->isStaticAlloca()) { |
| 129 | const ConstantInt *CUI = cast<ConstantInt>(AI->getArraySize()); |
| 130 | Type *Ty = AI->getAllocatedType(); |
| 131 | uint64_t TySize = TLI->getDataLayout()->getTypeAllocSize(Ty); |
| 132 | unsigned Align = |
Eric Christopher | 2ae2de7 | 2014-10-09 00:57:31 +0000 | [diff] [blame] | 133 | std::max((unsigned)TLI->getDataLayout()->getPrefTypeAlignment(Ty), |
| 134 | AI->getAlignment()); |
Reid Kleckner | 0b2bccc | 2014-09-02 18:42:44 +0000 | [diff] [blame] | 135 | |
| 136 | TySize *= CUI->getZExtValue(); // Get total allocated size. |
| 137 | if (TySize == 0) TySize = 1; // Don't create zero-sized stack objects. |
| 138 | |
| 139 | StaticAllocaMap[AI] = |
| 140 | MF->getFrameInfo()->CreateStackObject(TySize, Align, false, AI); |
| 141 | |
| 142 | } else { |
Hans Wennborg | acb842d | 2014-03-05 02:43:26 +0000 | [diff] [blame] | 143 | unsigned Align = std::max( |
| 144 | (unsigned)TLI->getDataLayout()->getPrefTypeAlignment( |
| 145 | AI->getAllocatedType()), |
| 146 | AI->getAlignment()); |
Eric Christopher | d913448 | 2014-08-04 21:25:23 +0000 | [diff] [blame] | 147 | unsigned StackAlign = |
Eric Christopher | 2ae2de7 | 2014-10-09 00:57:31 +0000 | [diff] [blame] | 148 | MF->getSubtarget().getFrameLowering()->getStackAlignment(); |
Hans Wennborg | acb842d | 2014-03-05 02:43:26 +0000 | [diff] [blame] | 149 | if (Align <= StackAlign) |
| 150 | Align = 0; |
| 151 | // Inform the Frame Information that we have variable-sized objects. |
| 152 | MF->getFrameInfo()->CreateVariableSizedObject(Align ? Align : 1, AI); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | // Look for inline asm that clobbers the SP register. |
| 157 | if (isa<CallInst>(I) || isa<InvokeInst>(I)) { |
| 158 | ImmutableCallSite CS(I); |
Hans Wennborg | 0c72fd2 | 2014-03-05 03:21:23 +0000 | [diff] [blame] | 159 | if (isa<InlineAsm>(CS.getCalledValue())) { |
Hans Wennborg | acb842d | 2014-03-05 02:43:26 +0000 | [diff] [blame] | 160 | unsigned SP = TLI->getStackPointerRegisterToSaveRestore(); |
Eric Christopher | 11e4df7 | 2015-02-26 22:38:43 +0000 | [diff] [blame] | 161 | const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo(); |
Hans Wennborg | acb842d | 2014-03-05 02:43:26 +0000 | [diff] [blame] | 162 | std::vector<TargetLowering::AsmOperandInfo> Ops = |
Eric Christopher | 11e4df7 | 2015-02-26 22:38:43 +0000 | [diff] [blame] | 163 | TLI->ParseConstraints(TRI, CS); |
Hans Wennborg | acb842d | 2014-03-05 02:43:26 +0000 | [diff] [blame] | 164 | for (size_t I = 0, E = Ops.size(); I != E; ++I) { |
| 165 | TargetLowering::AsmOperandInfo &Op = Ops[I]; |
| 166 | if (Op.Type == InlineAsm::isClobber) { |
| 167 | // Clobbers don't have SDValue operands, hence SDValue(). |
| 168 | TLI->ComputeConstraintToUse(Op, SDValue(), DAG); |
Eric Christopher | 2ae2de7 | 2014-10-09 00:57:31 +0000 | [diff] [blame] | 169 | std::pair<unsigned, const TargetRegisterClass *> PhysReg = |
Eric Christopher | 11e4df7 | 2015-02-26 22:38:43 +0000 | [diff] [blame] | 170 | TLI->getRegForInlineAsmConstraint(TRI, Op.ConstraintCode, |
| 171 | Op.ConstraintVT); |
Hans Wennborg | acb842d | 2014-03-05 02:43:26 +0000 | [diff] [blame] | 172 | if (PhysReg.first == SP) |
| 173 | MF->getFrameInfo()->setHasInlineAsmWithSPAdjust(true); |
| 174 | } |
| 175 | } |
| 176 | } |
| 177 | } |
| 178 | |
Reid Kleckner | 2d9bb65 | 2014-08-22 21:59:26 +0000 | [diff] [blame] | 179 | // Look for calls to the @llvm.va_start intrinsic. We can omit some |
| 180 | // prologue boilerplate for variadic functions that don't examine their |
| 181 | // arguments. |
| 182 | if (const auto *II = dyn_cast<IntrinsicInst>(I)) { |
| 183 | if (II->getIntrinsicID() == Intrinsic::vastart) |
| 184 | MF->getFrameInfo()->setHasVAStart(true); |
| 185 | } |
| 186 | |
Reid Kleckner | 16e5541 | 2014-08-29 21:42:08 +0000 | [diff] [blame] | 187 | // If we have a musttail call in a variadic funciton, we need to ensure we |
| 188 | // forward implicit register parameters. |
Reid Kleckner | dccd0cb | 2014-08-29 21:42:21 +0000 | [diff] [blame] | 189 | if (const auto *CI = dyn_cast<CallInst>(I)) { |
Reid Kleckner | 16e5541 | 2014-08-29 21:42:08 +0000 | [diff] [blame] | 190 | if (CI->isMustTailCall() && Fn->isVarArg()) |
| 191 | MF->getFrameInfo()->setHasMustTailInVarArgFunc(true); |
| 192 | } |
| 193 | |
Dan Gohman | 1e936277 | 2010-07-16 17:54:27 +0000 | [diff] [blame] | 194 | // Mark values used outside their block as exported, by allocating |
| 195 | // a virtual register for them. |
Cameron Zwarich | f8b22b3 | 2011-02-22 03:24:52 +0000 | [diff] [blame] | 196 | if (isUsedOutsideOfDefiningBlock(I)) |
Dan Gohman | a3624b6 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 197 | if (!isa<AllocaInst>(I) || |
| 198 | !StaticAllocaMap.count(cast<AllocaInst>(I))) |
| 199 | InitializeRegForValue(I); |
| 200 | |
Dan Gohman | 1e936277 | 2010-07-16 17:54:27 +0000 | [diff] [blame] | 201 | // Collect llvm.dbg.declare information. This is done now instead of |
| 202 | // during the initial isel pass through the IR so that it is done |
| 203 | // in a predictable order. |
| 204 | if (const DbgDeclareInst *DI = dyn_cast<DbgDeclareInst>(I)) { |
Duncan P. N. Exon Smith | e686f15 | 2015-04-06 23:27:40 +0000 | [diff] [blame] | 205 | DIVariable DIVar = DI->getVariable(); |
Duncan P. N. Exon Smith | 9dffcd0 | 2015-03-30 19:14:47 +0000 | [diff] [blame] | 206 | if (MMI.hasDebugInfo() && DIVar && DI->getDebugLoc()) { |
Dan Gohman | 1e936277 | 2010-07-16 17:54:27 +0000 | [diff] [blame] | 207 | // Don't handle byval struct arguments or VLAs, for example. |
| 208 | // Non-byval arguments are handled here (they refer to the stack |
| 209 | // temporary alloca at this point). |
| 210 | const Value *Address = DI->getAddress(); |
| 211 | if (Address) { |
| 212 | if (const BitCastInst *BCI = dyn_cast<BitCastInst>(Address)) |
| 213 | Address = BCI->getOperand(0); |
| 214 | if (const AllocaInst *AI = dyn_cast<AllocaInst>(Address)) { |
| 215 | DenseMap<const AllocaInst *, int>::iterator SI = |
| 216 | StaticAllocaMap.find(AI); |
| 217 | if (SI != StaticAllocaMap.end()) { // Check for VLAs. |
| 218 | int FI = SI->second; |
Adrian Prantl | 87b7eb9 | 2014-10-01 18:55:02 +0000 | [diff] [blame] | 219 | MMI.setVariableDbgInfo(DI->getVariable(), DI->getExpression(), |
Dan Gohman | 1e936277 | 2010-07-16 17:54:27 +0000 | [diff] [blame] | 220 | FI, DI->getDebugLoc()); |
| 221 | } |
| 222 | } |
| 223 | } |
| 224 | } |
| 225 | } |
Jiangning Liu | ffbc690 | 2014-09-19 05:30:35 +0000 | [diff] [blame] | 226 | |
| 227 | // Decide the preferred extend type for a value. |
| 228 | PreferredExtendType[I] = getPreferredExtendForValue(I); |
Dan Gohman | 1e936277 | 2010-07-16 17:54:27 +0000 | [diff] [blame] | 229 | } |
| 230 | |
Dan Gohman | a3624b6 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 231 | // Create an initial MachineBasicBlock for each LLVM BasicBlock in F. This |
| 232 | // also creates the initial PHI MachineInstrs, though none of the input |
| 233 | // operands are populated. |
Dan Gohman | f57117d | 2010-04-14 16:30:40 +0000 | [diff] [blame] | 234 | for (BB = Fn->begin(); BB != EB; ++BB) { |
Dan Gohman | a3624b6 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 235 | MachineBasicBlock *MBB = mf.CreateMachineBasicBlock(BB); |
| 236 | MBBMap[BB] = MBB; |
| 237 | MF->push_back(MBB); |
| 238 | |
| 239 | // Transfer the address-taken flag. This is necessary because there could |
| 240 | // be multiple MachineBasicBlocks corresponding to one BasicBlock, and only |
| 241 | // the first one should be marked. |
| 242 | if (BB->hasAddressTaken()) |
| 243 | MBB->setHasAddressTaken(); |
| 244 | |
| 245 | // Create Machine PHI nodes for LLVM PHI nodes, lowering them as |
| 246 | // appropriate. |
Dan Gohman | 0f055d3 | 2010-04-20 14:46:25 +0000 | [diff] [blame] | 247 | for (BasicBlock::const_iterator I = BB->begin(); |
| 248 | const PHINode *PN = dyn_cast<PHINode>(I); ++I) { |
| 249 | if (PN->use_empty()) continue; |
Dan Gohman | a3624b6 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 250 | |
Rafael Espindola | e53b7d1 | 2011-05-13 15:18:06 +0000 | [diff] [blame] | 251 | // Skip empty types |
| 252 | if (PN->getType()->isEmptyTy()) |
| 253 | continue; |
| 254 | |
Dan Gohman | 7b7f088 | 2010-04-20 14:48:02 +0000 | [diff] [blame] | 255 | DebugLoc DL = PN->getDebugLoc(); |
Dan Gohman | a3624b6 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 256 | unsigned PHIReg = ValueMap[PN]; |
| 257 | assert(PHIReg && "PHI node does not have an assigned virtual register!"); |
| 258 | |
| 259 | SmallVector<EVT, 4> ValueVTs; |
Bill Wendling | 8db01cb | 2013-06-06 00:11:39 +0000 | [diff] [blame] | 260 | ComputeValueVTs(*TLI, PN->getType(), ValueVTs); |
Dan Gohman | a3624b6 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 261 | for (unsigned vti = 0, vte = ValueVTs.size(); vti != vte; ++vti) { |
| 262 | EVT VT = ValueVTs[vti]; |
Bill Wendling | 8db01cb | 2013-06-06 00:11:39 +0000 | [diff] [blame] | 263 | unsigned NumRegisters = TLI->getNumRegisters(Fn->getContext(), VT); |
Eric Christopher | fc6de42 | 2014-08-05 02:39:49 +0000 | [diff] [blame] | 264 | const TargetInstrInfo *TII = MF->getSubtarget().getInstrInfo(); |
Dan Gohman | a3624b6 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 265 | for (unsigned i = 0; i != NumRegisters; ++i) |
Chris Lattner | b06015a | 2010-02-09 19:54:29 +0000 | [diff] [blame] | 266 | BuildMI(MBB, DL, TII->get(TargetOpcode::PHI), PHIReg + i); |
Dan Gohman | a3624b6 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 267 | PHIReg += NumRegisters; |
| 268 | } |
| 269 | } |
| 270 | } |
Dan Gohman | 69e8e32 | 2010-04-14 16:32:56 +0000 | [diff] [blame] | 271 | |
| 272 | // Mark landing pad blocks. |
Reid Kleckner | d2a1a51 | 2015-04-21 18:23:57 +0000 | [diff] [blame^] | 273 | const LandingPadInst *LP = nullptr; |
| 274 | for (BB = Fn->begin(); BB != EB; ++BB) { |
David Majnemer | cde3303 | 2015-03-30 22:58:10 +0000 | [diff] [blame] | 275 | if (const auto *Invoke = dyn_cast<InvokeInst>(BB->getTerminator())) |
Dan Gohman | 69e8e32 | 2010-04-14 16:32:56 +0000 | [diff] [blame] | 276 | MBBMap[Invoke->getSuccessor(1)]->setIsLandingPad(); |
Reid Kleckner | d2a1a51 | 2015-04-21 18:23:57 +0000 | [diff] [blame^] | 277 | if (BB->isLandingPad()) |
| 278 | LP = BB->getLandingPadInst(); |
| 279 | } |
David Majnemer | cde3303 | 2015-03-30 22:58:10 +0000 | [diff] [blame] | 280 | |
Reid Kleckner | d2a1a51 | 2015-04-21 18:23:57 +0000 | [diff] [blame^] | 281 | // Calculate EH numbers for MSVC C++ EH and save SEH handlers if necessary. |
| 282 | EHPersonality Personality = EHPersonality::Unknown; |
| 283 | if (LP) |
| 284 | Personality = classifyEHPersonality(LP->getPersonalityFn()); |
| 285 | if (Personality == EHPersonality::MSVC_Win64SEH) { |
| 286 | addSEHHandlersForLPads(); |
| 287 | } else if (Personality == EHPersonality::MSVC_CXX) { |
David Majnemer | 5c65f58 | 2015-04-10 04:56:17 +0000 | [diff] [blame] | 288 | const Function *WinEHParentFn = MMI.getWinEHParent(&fn); |
| 289 | WinEHFuncInfo &FI = MMI.getWinEHFuncInfo(WinEHParentFn); |
| 290 | if (FI.LandingPadStateMap.empty()) { |
| 291 | WinEHNumbering Num(FI); |
| 292 | Num.calculateStateNumbers(*WinEHParentFn); |
| 293 | // Pop everything on the handler stack. |
| 294 | Num.processCallSite(None, ImmutableCallSite()); |
| 295 | } |
David Majnemer | a225a19 | 2015-03-31 22:35:44 +0000 | [diff] [blame] | 296 | } |
David Majnemer | cde3303 | 2015-03-30 22:58:10 +0000 | [diff] [blame] | 297 | } |
| 298 | |
Reid Kleckner | d2a1a51 | 2015-04-21 18:23:57 +0000 | [diff] [blame^] | 299 | void FunctionLoweringInfo::addSEHHandlersForLPads() { |
| 300 | MachineModuleInfo &MMI = MF->getMMI(); |
| 301 | |
| 302 | // Iterate over all landing pads with llvm.eh.actions calls. |
| 303 | for (const BasicBlock &BB : *Fn) { |
| 304 | const LandingPadInst *LP = BB.getLandingPadInst(); |
| 305 | if (!LP) |
| 306 | continue; |
| 307 | const IntrinsicInst *ActionsCall = |
| 308 | dyn_cast<IntrinsicInst>(LP->getNextNode()); |
| 309 | if (!ActionsCall || |
| 310 | ActionsCall->getIntrinsicID() != Intrinsic::eh_actions) |
| 311 | continue; |
| 312 | |
| 313 | // Parse the llvm.eh.actions call we found. |
| 314 | MachineBasicBlock *LPadMBB = MBBMap[LP->getParent()]; |
| 315 | SmallVector<ActionHandler *, 4> Actions; |
| 316 | parseEHActions(ActionsCall, Actions); |
| 317 | |
| 318 | // Iterate EH actions from most to least precedence, which means |
| 319 | // iterating in reverse. |
| 320 | for (auto I = Actions.rbegin(), E = Actions.rend(); I != E; ++I) { |
| 321 | ActionHandler *Action = *I; |
| 322 | if (auto *CH = dyn_cast<CatchHandler>(Action)) { |
| 323 | const auto *Filter = |
| 324 | dyn_cast<Function>(CH->getSelector()->stripPointerCasts()); |
| 325 | assert((Filter || CH->getSelector()->isNullValue()) && |
| 326 | "expected function or catch-all"); |
| 327 | const auto *RecoverBA = |
| 328 | cast<BlockAddress>(CH->getHandlerBlockOrFunc()); |
| 329 | MMI.addSEHCatchHandler(LPadMBB, Filter, RecoverBA); |
| 330 | } else { |
| 331 | assert(isa<CleanupHandler>(Action)); |
| 332 | const auto *Fini = cast<Function>(Action->getHandlerBlockOrFunc()); |
| 333 | MMI.addSEHCleanupHandler(LPadMBB, Fini); |
| 334 | } |
| 335 | } |
| 336 | DeleteContainerPointers(Actions); |
| 337 | } |
| 338 | } |
| 339 | |
David Majnemer | cde3303 | 2015-03-30 22:58:10 +0000 | [diff] [blame] | 340 | void WinEHNumbering::createUnwindMapEntry(int ToState, ActionHandler *AH) { |
| 341 | WinEHUnwindMapEntry UME; |
| 342 | UME.ToState = ToState; |
David Majnemer | a225a19 | 2015-03-31 22:35:44 +0000 | [diff] [blame] | 343 | if (auto *CH = dyn_cast_or_null<CleanupHandler>(AH)) |
David Majnemer | cde3303 | 2015-03-30 22:58:10 +0000 | [diff] [blame] | 344 | UME.Cleanup = cast<Function>(CH->getHandlerBlockOrFunc()); |
| 345 | else |
| 346 | UME.Cleanup = nullptr; |
| 347 | FuncInfo.UnwindMap.push_back(UME); |
| 348 | } |
| 349 | |
David Majnemer | a225a19 | 2015-03-31 22:35:44 +0000 | [diff] [blame] | 350 | void WinEHNumbering::createTryBlockMapEntry(int TryLow, int TryHigh, |
| 351 | ArrayRef<CatchHandler *> Handlers) { |
| 352 | WinEHTryBlockMapEntry TBME; |
| 353 | TBME.TryLow = TryLow; |
| 354 | TBME.TryHigh = TryHigh; |
David Majnemer | a225a19 | 2015-03-31 22:35:44 +0000 | [diff] [blame] | 355 | assert(TBME.TryLow <= TBME.TryHigh); |
David Majnemer | a225a19 | 2015-03-31 22:35:44 +0000 | [diff] [blame] | 356 | for (CatchHandler *CH : Handlers) { |
| 357 | WinEHHandlerType HT; |
David Majnemer | e8eb9e6 | 2015-04-01 05:20:42 +0000 | [diff] [blame] | 358 | if (CH->getSelector()->isNullValue()) { |
| 359 | HT.Adjectives = 0x40; |
| 360 | HT.TypeDescriptor = nullptr; |
| 361 | } else { |
| 362 | auto *GV = cast<GlobalVariable>(CH->getSelector()->stripPointerCasts()); |
| 363 | // Selectors are always pointers to GlobalVariables with 'struct' type. |
| 364 | // The struct has two fields, adjectives and a type descriptor. |
| 365 | auto *CS = cast<ConstantStruct>(GV->getInitializer()); |
| 366 | HT.Adjectives = |
| 367 | cast<ConstantInt>(CS->getAggregateElement(0U))->getZExtValue(); |
| 368 | HT.TypeDescriptor = |
| 369 | cast<GlobalVariable>(CS->getAggregateElement(1)->stripPointerCasts()); |
| 370 | } |
David Majnemer | a225a19 | 2015-03-31 22:35:44 +0000 | [diff] [blame] | 371 | HT.Handler = cast<Function>(CH->getHandlerBlockOrFunc()); |
David Majnemer | 69132a7 | 2015-04-03 22:49:05 +0000 | [diff] [blame] | 372 | HT.CatchObjRecoverIdx = CH->getExceptionVarIndex(); |
David Majnemer | a225a19 | 2015-03-31 22:35:44 +0000 | [diff] [blame] | 373 | TBME.HandlerArray.push_back(HT); |
| 374 | } |
| 375 | FuncInfo.TryBlockMap.push_back(TBME); |
| 376 | } |
| 377 | |
David Majnemer | cde3303 | 2015-03-30 22:58:10 +0000 | [diff] [blame] | 378 | static void print_name(const Value *V) { |
David Majnemer | 9a55539 | 2015-03-30 23:14:45 +0000 | [diff] [blame] | 379 | #ifndef NDEBUG |
David Majnemer | cde3303 | 2015-03-30 22:58:10 +0000 | [diff] [blame] | 380 | if (!V) { |
| 381 | DEBUG(dbgs() << "null"); |
| 382 | return; |
| 383 | } |
| 384 | |
| 385 | if (const auto *F = dyn_cast<Function>(V)) |
| 386 | DEBUG(dbgs() << F->getName()); |
| 387 | else |
| 388 | DEBUG(V->dump()); |
David Majnemer | 9a55539 | 2015-03-30 23:14:45 +0000 | [diff] [blame] | 389 | #endif |
David Majnemer | cde3303 | 2015-03-30 22:58:10 +0000 | [diff] [blame] | 390 | } |
| 391 | |
David Majnemer | a225a19 | 2015-03-31 22:35:44 +0000 | [diff] [blame] | 392 | void WinEHNumbering::processCallSite(ArrayRef<ActionHandler *> Actions, |
| 393 | ImmutableCallSite CS) { |
David Majnemer | cde3303 | 2015-03-30 22:58:10 +0000 | [diff] [blame] | 394 | int FirstMismatch = 0; |
| 395 | for (int E = std::min(HandlerStack.size(), Actions.size()); FirstMismatch < E; |
| 396 | ++FirstMismatch) { |
| 397 | if (HandlerStack[FirstMismatch]->getHandlerBlockOrFunc() != |
| 398 | Actions[FirstMismatch]->getHandlerBlockOrFunc()) |
| 399 | break; |
| 400 | delete Actions[FirstMismatch]; |
| 401 | } |
| 402 | |
David Majnemer | a225a19 | 2015-03-31 22:35:44 +0000 | [diff] [blame] | 403 | bool EnteringScope = (int)Actions.size() > FirstMismatch; |
David Majnemer | a225a19 | 2015-03-31 22:35:44 +0000 | [diff] [blame] | 404 | |
David Majnemer | cde3303 | 2015-03-30 22:58:10 +0000 | [diff] [blame] | 405 | // Don't recurse while we are looping over the handler stack. Instead, defer |
| 406 | // the numbering of the catch handlers until we are done popping. |
David Majnemer | a225a19 | 2015-03-31 22:35:44 +0000 | [diff] [blame] | 407 | SmallVector<CatchHandler *, 4> PoppedCatches; |
David Majnemer | cde3303 | 2015-03-30 22:58:10 +0000 | [diff] [blame] | 408 | for (int I = HandlerStack.size() - 1; I >= FirstMismatch; --I) { |
David Majnemer | a225a19 | 2015-03-31 22:35:44 +0000 | [diff] [blame] | 409 | if (auto *CH = dyn_cast<CatchHandler>(HandlerStack.back())) { |
| 410 | PoppedCatches.push_back(CH); |
| 411 | } else { |
| 412 | // Delete cleanup handlers |
| 413 | delete HandlerStack.back(); |
| 414 | } |
David Majnemer | cde3303 | 2015-03-30 22:58:10 +0000 | [diff] [blame] | 415 | HandlerStack.pop_back(); |
| 416 | } |
| 417 | |
David Majnemer | a225a19 | 2015-03-31 22:35:44 +0000 | [diff] [blame] | 418 | // We need to create a new state number if we are exiting a try scope and we |
| 419 | // will not push any more actions. |
| 420 | int TryHigh = NextState - 1; |
David Majnemer | d1079bf | 2015-03-31 22:43:56 +0000 | [diff] [blame] | 421 | if (!EnteringScope && !PoppedCatches.empty()) { |
David Majnemer | a225a19 | 2015-03-31 22:35:44 +0000 | [diff] [blame] | 422 | createUnwindMapEntry(currentEHNumber(), nullptr); |
| 423 | ++NextState; |
| 424 | } |
David Majnemer | cde3303 | 2015-03-30 22:58:10 +0000 | [diff] [blame] | 425 | |
David Majnemer | a225a19 | 2015-03-31 22:35:44 +0000 | [diff] [blame] | 426 | int LastTryLowIdx = 0; |
| 427 | for (int I = 0, E = PoppedCatches.size(); I != E; ++I) { |
| 428 | CatchHandler *CH = PoppedCatches[I]; |
| 429 | if (I + 1 == E || CH->getEHState() != PoppedCatches[I + 1]->getEHState()) { |
| 430 | int TryLow = CH->getEHState(); |
| 431 | auto Handlers = |
| 432 | makeArrayRef(&PoppedCatches[LastTryLowIdx], I - LastTryLowIdx + 1); |
| 433 | createTryBlockMapEntry(TryLow, TryHigh, Handlers); |
| 434 | LastTryLowIdx = I + 1; |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | for (CatchHandler *CH : PoppedCatches) { |
| 439 | if (auto *F = dyn_cast<Function>(CH->getHandlerBlockOrFunc())) |
| 440 | calculateStateNumbers(*F); |
| 441 | delete CH; |
| 442 | } |
| 443 | |
| 444 | bool LastActionWasCatch = false; |
David Majnemer | cde3303 | 2015-03-30 22:58:10 +0000 | [diff] [blame] | 445 | for (size_t I = FirstMismatch; I != Actions.size(); ++I) { |
David Majnemer | a225a19 | 2015-03-31 22:35:44 +0000 | [diff] [blame] | 446 | // We can reuse eh states when pushing two catches for the same invoke. |
| 447 | bool CurrActionIsCatch = isa<CatchHandler>(Actions[I]); |
| 448 | // FIXME: Reenable this optimization! |
| 449 | if (CurrActionIsCatch && LastActionWasCatch && false) { |
| 450 | Actions[I]->setEHState(currentEHNumber()); |
| 451 | } else { |
| 452 | createUnwindMapEntry(currentEHNumber(), Actions[I]); |
| 453 | Actions[I]->setEHState(NextState); |
| 454 | NextState++; |
| 455 | DEBUG(dbgs() << "Creating unwind map entry for: ("); |
| 456 | print_name(Actions[I]->getHandlerBlockOrFunc()); |
| 457 | DEBUG(dbgs() << ", " << currentEHNumber() << ")\n"); |
| 458 | } |
David Majnemer | cde3303 | 2015-03-30 22:58:10 +0000 | [diff] [blame] | 459 | HandlerStack.push_back(Actions[I]); |
David Majnemer | a225a19 | 2015-03-31 22:35:44 +0000 | [diff] [blame] | 460 | LastActionWasCatch = CurrActionIsCatch; |
David Majnemer | cde3303 | 2015-03-30 22:58:10 +0000 | [diff] [blame] | 461 | } |
| 462 | |
| 463 | DEBUG(dbgs() << "In EHState " << currentEHNumber() << " for CallSite: "); |
| 464 | print_name(CS ? CS.getCalledValue() : nullptr); |
| 465 | DEBUG(dbgs() << '\n'); |
| 466 | } |
| 467 | |
| 468 | void WinEHNumbering::calculateStateNumbers(const Function &F) { |
David Majnemer | a225a19 | 2015-03-31 22:35:44 +0000 | [diff] [blame] | 469 | auto I = VisitedHandlers.insert(&F); |
| 470 | if (!I.second) |
| 471 | return; // We've already visited this handler, don't renumber it. |
| 472 | |
David Majnemer | cde3303 | 2015-03-30 22:58:10 +0000 | [diff] [blame] | 473 | DEBUG(dbgs() << "Calculating state numbers for: " << F.getName() << '\n'); |
| 474 | SmallVector<ActionHandler *, 4> ActionList; |
| 475 | for (const BasicBlock &BB : F) { |
| 476 | for (const Instruction &I : BB) { |
| 477 | const auto *CI = dyn_cast<CallInst>(&I); |
| 478 | if (!CI || CI->doesNotThrow()) |
| 479 | continue; |
David Majnemer | a225a19 | 2015-03-31 22:35:44 +0000 | [diff] [blame] | 480 | processCallSite(None, CI); |
David Majnemer | cde3303 | 2015-03-30 22:58:10 +0000 | [diff] [blame] | 481 | } |
| 482 | const auto *II = dyn_cast<InvokeInst>(BB.getTerminator()); |
| 483 | if (!II) |
| 484 | continue; |
| 485 | const LandingPadInst *LPI = II->getLandingPadInst(); |
David Majnemer | a225a19 | 2015-03-31 22:35:44 +0000 | [diff] [blame] | 486 | auto *ActionsCall = dyn_cast<IntrinsicInst>(LPI->getNextNode()); |
| 487 | if (!ActionsCall) |
| 488 | continue; |
| 489 | assert(ActionsCall->getIntrinsicID() == Intrinsic::eh_actions); |
| 490 | parseEHActions(ActionsCall, ActionList); |
| 491 | processCallSite(ActionList, II); |
| 492 | ActionList.clear(); |
| 493 | FuncInfo.LandingPadStateMap[LPI] = currentEHNumber(); |
David Majnemer | cde3303 | 2015-03-30 22:58:10 +0000 | [diff] [blame] | 494 | } |
David Majnemer | 7f5e714 | 2015-04-03 23:37:34 +0000 | [diff] [blame] | 495 | |
| 496 | FuncInfo.CatchHandlerMaxState[&F] = NextState - 1; |
Dan Gohman | a3624b6 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 497 | } |
| 498 | |
| 499 | /// clear - Clear out all the function-specific state. This returns this |
| 500 | /// FunctionLoweringInfo to an empty state, ready to be used for a |
| 501 | /// different function. |
| 502 | void FunctionLoweringInfo::clear() { |
Dan Gohman | ad0b3ea | 2010-04-14 17:11:23 +0000 | [diff] [blame] | 503 | assert(CatchInfoFound.size() == CatchInfoLost.size() && |
| 504 | "Not all catch info was assigned to a landing pad!"); |
| 505 | |
Dan Gohman | a3624b6 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 506 | MBBMap.clear(); |
| 507 | ValueMap.clear(); |
| 508 | StaticAllocaMap.clear(); |
| 509 | #ifndef NDEBUG |
| 510 | CatchInfoLost.clear(); |
| 511 | CatchInfoFound.clear(); |
| 512 | #endif |
| 513 | LiveOutRegInfo.clear(); |
Cameron Zwarich | 988faf9 | 2011-02-24 10:00:13 +0000 | [diff] [blame] | 514 | VisitedBBs.clear(); |
Evan Cheng | 6e82245 | 2010-04-28 23:08:54 +0000 | [diff] [blame] | 515 | ArgDbgValues.clear(); |
Devang Patel | 86ec8b3 | 2010-08-31 22:22:42 +0000 | [diff] [blame] | 516 | ByValArgFrameIndexMap.clear(); |
Dan Gohman | d7b5ce3 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 517 | RegFixups.clear(); |
Philip Reames | 1a1bdb2 | 2014-12-02 18:50:36 +0000 | [diff] [blame] | 518 | StatepointStackSlots.clear(); |
Jiangning Liu | 3b09617 | 2014-09-24 03:22:56 +0000 | [diff] [blame] | 519 | PreferredExtendType.clear(); |
Dan Gohman | a3624b6 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 520 | } |
| 521 | |
Dan Gohman | 93f5920 | 2010-07-02 00:10:16 +0000 | [diff] [blame] | 522 | /// CreateReg - Allocate a single virtual register for the given type. |
Patrik Hagglund | 5e6c361 | 2012-12-13 06:34:11 +0000 | [diff] [blame] | 523 | unsigned FunctionLoweringInfo::CreateReg(MVT VT) { |
Eric Christopher | d913448 | 2014-08-04 21:25:23 +0000 | [diff] [blame] | 524 | return RegInfo->createVirtualRegister( |
Eric Christopher | 2ae2de7 | 2014-10-09 00:57:31 +0000 | [diff] [blame] | 525 | MF->getSubtarget().getTargetLowering()->getRegClassFor(VT)); |
Dan Gohman | a3624b6 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 526 | } |
| 527 | |
Dan Gohman | 93f5920 | 2010-07-02 00:10:16 +0000 | [diff] [blame] | 528 | /// CreateRegs - Allocate the appropriate number of virtual registers of |
Dan Gohman | a3624b6 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 529 | /// the correctly promoted or expanded types. Assign these registers |
| 530 | /// consecutive vreg numbers and return the first assigned number. |
| 531 | /// |
| 532 | /// In the case that the given value has struct or array type, this function |
| 533 | /// will assign registers for each member or element. |
| 534 | /// |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 535 | unsigned FunctionLoweringInfo::CreateRegs(Type *Ty) { |
Eric Christopher | 2ae2de7 | 2014-10-09 00:57:31 +0000 | [diff] [blame] | 536 | const TargetLowering *TLI = MF->getSubtarget().getTargetLowering(); |
Bill Wendling | 0ccf310 | 2013-06-19 20:32:16 +0000 | [diff] [blame] | 537 | |
Dan Gohman | a3624b6 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 538 | SmallVector<EVT, 4> ValueVTs; |
Bill Wendling | 8db01cb | 2013-06-06 00:11:39 +0000 | [diff] [blame] | 539 | ComputeValueVTs(*TLI, Ty, ValueVTs); |
Dan Gohman | a3624b6 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 540 | |
| 541 | unsigned FirstReg = 0; |
| 542 | for (unsigned Value = 0, e = ValueVTs.size(); Value != e; ++Value) { |
| 543 | EVT ValueVT = ValueVTs[Value]; |
Bill Wendling | 8db01cb | 2013-06-06 00:11:39 +0000 | [diff] [blame] | 544 | MVT RegisterVT = TLI->getRegisterType(Ty->getContext(), ValueVT); |
Dan Gohman | a3624b6 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 545 | |
Bill Wendling | 8db01cb | 2013-06-06 00:11:39 +0000 | [diff] [blame] | 546 | unsigned NumRegs = TLI->getNumRegisters(Ty->getContext(), ValueVT); |
Dan Gohman | a3624b6 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 547 | for (unsigned i = 0; i != NumRegs; ++i) { |
Dan Gohman | 93f5920 | 2010-07-02 00:10:16 +0000 | [diff] [blame] | 548 | unsigned R = CreateReg(RegisterVT); |
Dan Gohman | a3624b6 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 549 | if (!FirstReg) FirstReg = R; |
| 550 | } |
| 551 | } |
| 552 | return FirstReg; |
| 553 | } |
Dan Gohman | ad97b3d | 2009-11-23 17:42:46 +0000 | [diff] [blame] | 554 | |
Cameron Zwarich | a62fc89 | 2011-02-24 10:00:25 +0000 | [diff] [blame] | 555 | /// GetLiveOutRegInfo - Gets LiveOutInfo for a register, returning NULL if the |
| 556 | /// register is a PHI destination and the PHI's LiveOutInfo is not valid. If |
| 557 | /// the register's LiveOutInfo is for a smaller bit width, it is extended to |
| 558 | /// the larger bit width by zero extension. The bit width must be no smaller |
| 559 | /// than the LiveOutInfo's existing bit width. |
| 560 | const FunctionLoweringInfo::LiveOutInfo * |
| 561 | FunctionLoweringInfo::GetLiveOutRegInfo(unsigned Reg, unsigned BitWidth) { |
| 562 | if (!LiveOutRegInfo.inBounds(Reg)) |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 563 | return nullptr; |
Cameron Zwarich | a62fc89 | 2011-02-24 10:00:25 +0000 | [diff] [blame] | 564 | |
| 565 | LiveOutInfo *LOI = &LiveOutRegInfo[Reg]; |
| 566 | if (!LOI->IsValid) |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 567 | return nullptr; |
Cameron Zwarich | a62fc89 | 2011-02-24 10:00:25 +0000 | [diff] [blame] | 568 | |
Cameron Zwarich | d2f3041 | 2011-02-25 01:10:55 +0000 | [diff] [blame] | 569 | if (BitWidth > LOI->KnownZero.getBitWidth()) { |
Cameron Zwarich | 4c82cd2 | 2011-02-25 01:11:01 +0000 | [diff] [blame] | 570 | LOI->NumSignBits = 1; |
Cameron Zwarich | a62fc89 | 2011-02-24 10:00:25 +0000 | [diff] [blame] | 571 | LOI->KnownZero = LOI->KnownZero.zextOrTrunc(BitWidth); |
| 572 | LOI->KnownOne = LOI->KnownOne.zextOrTrunc(BitWidth); |
| 573 | } |
| 574 | |
| 575 | return LOI; |
| 576 | } |
| 577 | |
| 578 | /// ComputePHILiveOutRegInfo - Compute LiveOutInfo for a PHI's destination |
| 579 | /// register based on the LiveOutInfo of its operands. |
| 580 | void FunctionLoweringInfo::ComputePHILiveOutRegInfo(const PHINode *PN) { |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 581 | Type *Ty = PN->getType(); |
Cameron Zwarich | a62fc89 | 2011-02-24 10:00:25 +0000 | [diff] [blame] | 582 | if (!Ty->isIntegerTy() || Ty->isVectorTy()) |
| 583 | return; |
| 584 | |
| 585 | SmallVector<EVT, 1> ValueVTs; |
Bill Wendling | 8db01cb | 2013-06-06 00:11:39 +0000 | [diff] [blame] | 586 | ComputeValueVTs(*TLI, Ty, ValueVTs); |
Cameron Zwarich | a62fc89 | 2011-02-24 10:00:25 +0000 | [diff] [blame] | 587 | assert(ValueVTs.size() == 1 && |
| 588 | "PHIs with non-vector integer types should have a single VT."); |
| 589 | EVT IntVT = ValueVTs[0]; |
| 590 | |
Bill Wendling | 8db01cb | 2013-06-06 00:11:39 +0000 | [diff] [blame] | 591 | if (TLI->getNumRegisters(PN->getContext(), IntVT) != 1) |
Cameron Zwarich | a62fc89 | 2011-02-24 10:00:25 +0000 | [diff] [blame] | 592 | return; |
Bill Wendling | 8db01cb | 2013-06-06 00:11:39 +0000 | [diff] [blame] | 593 | IntVT = TLI->getTypeToTransformTo(PN->getContext(), IntVT); |
Cameron Zwarich | a62fc89 | 2011-02-24 10:00:25 +0000 | [diff] [blame] | 594 | unsigned BitWidth = IntVT.getSizeInBits(); |
| 595 | |
| 596 | unsigned DestReg = ValueMap[PN]; |
| 597 | if (!TargetRegisterInfo::isVirtualRegister(DestReg)) |
| 598 | return; |
| 599 | LiveOutRegInfo.grow(DestReg); |
| 600 | LiveOutInfo &DestLOI = LiveOutRegInfo[DestReg]; |
| 601 | |
| 602 | Value *V = PN->getIncomingValue(0); |
| 603 | if (isa<UndefValue>(V) || isa<ConstantExpr>(V)) { |
| 604 | DestLOI.NumSignBits = 1; |
| 605 | APInt Zero(BitWidth, 0); |
| 606 | DestLOI.KnownZero = Zero; |
| 607 | DestLOI.KnownOne = Zero; |
| 608 | return; |
| 609 | } |
| 610 | |
| 611 | if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) { |
| 612 | APInt Val = CI->getValue().zextOrTrunc(BitWidth); |
| 613 | DestLOI.NumSignBits = Val.getNumSignBits(); |
| 614 | DestLOI.KnownZero = ~Val; |
| 615 | DestLOI.KnownOne = Val; |
| 616 | } else { |
| 617 | assert(ValueMap.count(V) && "V should have been placed in ValueMap when its" |
| 618 | "CopyToReg node was created."); |
| 619 | unsigned SrcReg = ValueMap[V]; |
| 620 | if (!TargetRegisterInfo::isVirtualRegister(SrcReg)) { |
| 621 | DestLOI.IsValid = false; |
| 622 | return; |
| 623 | } |
| 624 | const LiveOutInfo *SrcLOI = GetLiveOutRegInfo(SrcReg, BitWidth); |
| 625 | if (!SrcLOI) { |
| 626 | DestLOI.IsValid = false; |
| 627 | return; |
| 628 | } |
| 629 | DestLOI = *SrcLOI; |
| 630 | } |
| 631 | |
| 632 | assert(DestLOI.KnownZero.getBitWidth() == BitWidth && |
| 633 | DestLOI.KnownOne.getBitWidth() == BitWidth && |
| 634 | "Masks should have the same bit width as the type."); |
| 635 | |
| 636 | for (unsigned i = 1, e = PN->getNumIncomingValues(); i != e; ++i) { |
| 637 | Value *V = PN->getIncomingValue(i); |
| 638 | if (isa<UndefValue>(V) || isa<ConstantExpr>(V)) { |
| 639 | DestLOI.NumSignBits = 1; |
| 640 | APInt Zero(BitWidth, 0); |
| 641 | DestLOI.KnownZero = Zero; |
| 642 | DestLOI.KnownOne = Zero; |
Eric Christopher | 0713a9d | 2011-06-08 23:55:35 +0000 | [diff] [blame] | 643 | return; |
Cameron Zwarich | a62fc89 | 2011-02-24 10:00:25 +0000 | [diff] [blame] | 644 | } |
| 645 | |
| 646 | if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) { |
| 647 | APInt Val = CI->getValue().zextOrTrunc(BitWidth); |
| 648 | DestLOI.NumSignBits = std::min(DestLOI.NumSignBits, Val.getNumSignBits()); |
| 649 | DestLOI.KnownZero &= ~Val; |
| 650 | DestLOI.KnownOne &= Val; |
| 651 | continue; |
| 652 | } |
| 653 | |
| 654 | assert(ValueMap.count(V) && "V should have been placed in ValueMap when " |
| 655 | "its CopyToReg node was created."); |
| 656 | unsigned SrcReg = ValueMap[V]; |
| 657 | if (!TargetRegisterInfo::isVirtualRegister(SrcReg)) { |
| 658 | DestLOI.IsValid = false; |
| 659 | return; |
| 660 | } |
| 661 | const LiveOutInfo *SrcLOI = GetLiveOutRegInfo(SrcReg, BitWidth); |
| 662 | if (!SrcLOI) { |
| 663 | DestLOI.IsValid = false; |
| 664 | return; |
| 665 | } |
| 666 | DestLOI.NumSignBits = std::min(DestLOI.NumSignBits, SrcLOI->NumSignBits); |
| 667 | DestLOI.KnownZero &= SrcLOI->KnownZero; |
| 668 | DestLOI.KnownOne &= SrcLOI->KnownOne; |
| 669 | } |
| 670 | } |
| 671 | |
Devang Patel | 9d904e1 | 2011-09-08 22:59:09 +0000 | [diff] [blame] | 672 | /// setArgumentFrameIndex - Record frame index for the byval |
Devang Patel | 86ec8b3 | 2010-08-31 22:22:42 +0000 | [diff] [blame] | 673 | /// argument. This overrides previous frame index entry for this argument, |
| 674 | /// if any. |
Devang Patel | 9d904e1 | 2011-09-08 22:59:09 +0000 | [diff] [blame] | 675 | void FunctionLoweringInfo::setArgumentFrameIndex(const Argument *A, |
Eric Christopher | 219d51d | 2012-02-24 01:59:01 +0000 | [diff] [blame] | 676 | int FI) { |
Devang Patel | 86ec8b3 | 2010-08-31 22:22:42 +0000 | [diff] [blame] | 677 | ByValArgFrameIndexMap[A] = FI; |
| 678 | } |
Eric Christopher | 0713a9d | 2011-06-08 23:55:35 +0000 | [diff] [blame] | 679 | |
Devang Patel | 9d904e1 | 2011-09-08 22:59:09 +0000 | [diff] [blame] | 680 | /// getArgumentFrameIndex - Get frame index for the byval argument. |
Devang Patel | 86ec8b3 | 2010-08-31 22:22:42 +0000 | [diff] [blame] | 681 | /// If the argument does not have any assigned frame index then 0 is |
| 682 | /// returned. |
Devang Patel | 9d904e1 | 2011-09-08 22:59:09 +0000 | [diff] [blame] | 683 | int FunctionLoweringInfo::getArgumentFrameIndex(const Argument *A) { |
Eric Christopher | 0713a9d | 2011-06-08 23:55:35 +0000 | [diff] [blame] | 684 | DenseMap<const Argument *, int>::iterator I = |
Devang Patel | 86ec8b3 | 2010-08-31 22:22:42 +0000 | [diff] [blame] | 685 | ByValArgFrameIndexMap.find(A); |
| 686 | if (I != ByValArgFrameIndexMap.end()) |
| 687 | return I->second; |
Eric Christopher | 18c6be7 | 2012-02-23 03:39:43 +0000 | [diff] [blame] | 688 | DEBUG(dbgs() << "Argument does not have assigned frame index!\n"); |
Devang Patel | 86ec8b3 | 2010-08-31 22:22:42 +0000 | [diff] [blame] | 689 | return 0; |
| 690 | } |
| 691 | |
Michael J. Spencer | 8b98bf2 | 2012-02-22 19:06:13 +0000 | [diff] [blame] | 692 | /// ComputeUsesVAFloatArgument - Determine if any floating-point values are |
| 693 | /// being passed to this variadic function, and set the MachineModuleInfo's |
| 694 | /// usesVAFloatArgument flag if so. This flag is used to emit an undefined |
| 695 | /// reference to _fltused on Windows, which will link in MSVCRT's |
| 696 | /// floating-point support. |
| 697 | void llvm::ComputeUsesVAFloatArgument(const CallInst &I, |
| 698 | MachineModuleInfo *MMI) |
| 699 | { |
| 700 | FunctionType *FT = cast<FunctionType>( |
| 701 | I.getCalledValue()->getType()->getContainedType(0)); |
| 702 | if (FT->isVarArg() && !MMI->usesVAFloatArgument()) { |
| 703 | for (unsigned i = 0, e = I.getNumArgOperands(); i != e; ++i) { |
| 704 | Type* T = I.getArgOperand(i)->getType(); |
Daniel Berlin | 25db4f4 | 2015-04-15 17:41:42 +0000 | [diff] [blame] | 705 | for (auto i : post_order(T)) { |
Michael J. Spencer | 8b98bf2 | 2012-02-22 19:06:13 +0000 | [diff] [blame] | 706 | if (i->isFloatingPointTy()) { |
| 707 | MMI->setUsesVAFloatArgument(true); |
| 708 | return; |
| 709 | } |
| 710 | } |
| 711 | } |
| 712 | } |
| 713 | } |
| 714 | |
Bill Wendling | 247fd3b | 2011-08-17 21:56:44 +0000 | [diff] [blame] | 715 | /// AddLandingPadInfo - Extract the exception handling information from the |
| 716 | /// landingpad instruction and add them to the specified machine module info. |
| 717 | void llvm::AddLandingPadInfo(const LandingPadInst &I, MachineModuleInfo &MMI, |
| 718 | MachineBasicBlock *MBB) { |
| 719 | MMI.addPersonality(MBB, |
| 720 | cast<Function>(I.getPersonalityFn()->stripPointerCasts())); |
| 721 | |
| 722 | if (I.isCleanup()) |
| 723 | MMI.addCleanup(MBB); |
| 724 | |
| 725 | // FIXME: New EH - Add the clauses in reverse order. This isn't 100% correct, |
| 726 | // but we need to do it this way because of how the DWARF EH emitter |
| 727 | // processes the clauses. |
| 728 | for (unsigned i = I.getNumClauses(); i != 0; --i) { |
| 729 | Value *Val = I.getClause(i - 1); |
| 730 | if (I.isCatch(i - 1)) { |
| 731 | MMI.addCatchTypeInfo(MBB, |
Reid Kleckner | 283bc2e | 2014-11-14 00:35:50 +0000 | [diff] [blame] | 732 | dyn_cast<GlobalValue>(Val->stripPointerCasts())); |
Bill Wendling | 247fd3b | 2011-08-17 21:56:44 +0000 | [diff] [blame] | 733 | } else { |
| 734 | // Add filters in a list. |
| 735 | Constant *CVal = cast<Constant>(Val); |
Reid Kleckner | 283bc2e | 2014-11-14 00:35:50 +0000 | [diff] [blame] | 736 | SmallVector<const GlobalValue*, 4> FilterList; |
Bill Wendling | 247fd3b | 2011-08-17 21:56:44 +0000 | [diff] [blame] | 737 | for (User::op_iterator |
| 738 | II = CVal->op_begin(), IE = CVal->op_end(); II != IE; ++II) |
Reid Kleckner | 283bc2e | 2014-11-14 00:35:50 +0000 | [diff] [blame] | 739 | FilterList.push_back(cast<GlobalValue>((*II)->stripPointerCasts())); |
Bill Wendling | 247fd3b | 2011-08-17 21:56:44 +0000 | [diff] [blame] | 740 | |
| 741 | MMI.addFilterTypeInfo(MBB, FilterList); |
| 742 | } |
| 743 | } |
| 744 | } |