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 | |
Hans Wennborg | acb842d | 2014-03-05 02:43:26 +0000 | [diff] [blame] | 83 | void FunctionLoweringInfo::set(const Function &fn, MachineFunction &mf, |
| 84 | SelectionDAG *DAG) { |
Dan Gohman | a3624b6 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 85 | Fn = &fn; |
| 86 | MF = &mf; |
Eric Christopher | 2ae2de7 | 2014-10-09 00:57:31 +0000 | [diff] [blame] | 87 | TLI = MF->getSubtarget().getTargetLowering(); |
Dan Gohman | a3624b6 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 88 | RegInfo = &MF->getRegInfo(); |
David Majnemer | cde3303 | 2015-03-30 22:58:10 +0000 | [diff] [blame] | 89 | MachineModuleInfo &MMI = MF->getMMI(); |
Dan Gohman | a3624b6 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 90 | |
Dan Gohman | d7b5ce3 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 91 | // Check whether the function can return without sret-demotion. |
| 92 | SmallVector<ISD::OutputArg, 4> Outs; |
Mehdi Amini | 56228da | 2015-07-09 01:57:34 +0000 | [diff] [blame] | 93 | GetReturnInfo(Fn->getReturnType(), Fn->getAttributes(), Outs, *TLI, |
| 94 | mf.getDataLayout()); |
Bill Wendling | 8db01cb | 2013-06-06 00:11:39 +0000 | [diff] [blame] | 95 | CanLowerReturn = TLI->CanLowerReturn(Fn->getCallingConv(), *MF, |
Eric Christopher | 2ae2de7 | 2014-10-09 00:57:31 +0000 | [diff] [blame] | 96 | Fn->isVarArg(), Outs, Fn->getContext()); |
Dan Gohman | d7b5ce3 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 97 | |
Dan Gohman | a3624b6 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 98 | // Initialize the mapping of values to registers. This is only set up for |
| 99 | // instruction values that are used outside of the block that defines |
| 100 | // them. |
Dan Gohman | 913c998 | 2010-04-15 04:33:49 +0000 | [diff] [blame] | 101 | Function::const_iterator BB = Fn->begin(), EB = Fn->end(); |
Dan Gohman | a3624b6 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 102 | for (; BB != EB; ++BB) |
Eric Christopher | 219d51d | 2012-02-24 01:59:01 +0000 | [diff] [blame] | 103 | for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); |
| 104 | I != E; ++I) { |
Hans Wennborg | acb842d | 2014-03-05 02:43:26 +0000 | [diff] [blame] | 105 | if (const AllocaInst *AI = dyn_cast<AllocaInst>(I)) { |
Reid Kleckner | 0b2bccc | 2014-09-02 18:42:44 +0000 | [diff] [blame] | 106 | // Static allocas can be folded into the initial stack frame adjustment. |
| 107 | if (AI->isStaticAlloca()) { |
| 108 | const ConstantInt *CUI = cast<ConstantInt>(AI->getArraySize()); |
| 109 | Type *Ty = AI->getAllocatedType(); |
Mehdi Amini | 8ac7a9d | 2015-07-07 19:07:19 +0000 | [diff] [blame] | 110 | uint64_t TySize = MF->getDataLayout().getTypeAllocSize(Ty); |
Reid Kleckner | 0b2bccc | 2014-09-02 18:42:44 +0000 | [diff] [blame] | 111 | unsigned Align = |
Mehdi Amini | 8ac7a9d | 2015-07-07 19:07:19 +0000 | [diff] [blame] | 112 | std::max((unsigned)MF->getDataLayout().getPrefTypeAlignment(Ty), |
Eric Christopher | 2ae2de7 | 2014-10-09 00:57:31 +0000 | [diff] [blame] | 113 | AI->getAlignment()); |
Reid Kleckner | 0b2bccc | 2014-09-02 18:42:44 +0000 | [diff] [blame] | 114 | |
| 115 | TySize *= CUI->getZExtValue(); // Get total allocated size. |
| 116 | if (TySize == 0) TySize = 1; // Don't create zero-sized stack objects. |
| 117 | |
| 118 | StaticAllocaMap[AI] = |
| 119 | MF->getFrameInfo()->CreateStackObject(TySize, Align, false, AI); |
| 120 | |
| 121 | } else { |
Mehdi Amini | 8ac7a9d | 2015-07-07 19:07:19 +0000 | [diff] [blame] | 122 | unsigned Align = |
| 123 | std::max((unsigned)MF->getDataLayout().getPrefTypeAlignment( |
| 124 | AI->getAllocatedType()), |
| 125 | AI->getAlignment()); |
Eric Christopher | d913448 | 2014-08-04 21:25:23 +0000 | [diff] [blame] | 126 | unsigned StackAlign = |
Eric Christopher | 2ae2de7 | 2014-10-09 00:57:31 +0000 | [diff] [blame] | 127 | MF->getSubtarget().getFrameLowering()->getStackAlignment(); |
Hans Wennborg | acb842d | 2014-03-05 02:43:26 +0000 | [diff] [blame] | 128 | if (Align <= StackAlign) |
| 129 | Align = 0; |
| 130 | // Inform the Frame Information that we have variable-sized objects. |
| 131 | MF->getFrameInfo()->CreateVariableSizedObject(Align ? Align : 1, AI); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | // Look for inline asm that clobbers the SP register. |
| 136 | if (isa<CallInst>(I) || isa<InvokeInst>(I)) { |
| 137 | ImmutableCallSite CS(I); |
Hans Wennborg | 0c72fd2 | 2014-03-05 03:21:23 +0000 | [diff] [blame] | 138 | if (isa<InlineAsm>(CS.getCalledValue())) { |
Hans Wennborg | acb842d | 2014-03-05 02:43:26 +0000 | [diff] [blame] | 139 | unsigned SP = TLI->getStackPointerRegisterToSaveRestore(); |
Eric Christopher | 11e4df7 | 2015-02-26 22:38:43 +0000 | [diff] [blame] | 140 | const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo(); |
Hans Wennborg | acb842d | 2014-03-05 02:43:26 +0000 | [diff] [blame] | 141 | std::vector<TargetLowering::AsmOperandInfo> Ops = |
Mehdi Amini | 8ac7a9d | 2015-07-07 19:07:19 +0000 | [diff] [blame] | 142 | TLI->ParseConstraints(Fn->getParent()->getDataLayout(), TRI, CS); |
Hans Wennborg | acb842d | 2014-03-05 02:43:26 +0000 | [diff] [blame] | 143 | for (size_t I = 0, E = Ops.size(); I != E; ++I) { |
| 144 | TargetLowering::AsmOperandInfo &Op = Ops[I]; |
| 145 | if (Op.Type == InlineAsm::isClobber) { |
| 146 | // Clobbers don't have SDValue operands, hence SDValue(). |
| 147 | TLI->ComputeConstraintToUse(Op, SDValue(), DAG); |
Eric Christopher | 2ae2de7 | 2014-10-09 00:57:31 +0000 | [diff] [blame] | 148 | std::pair<unsigned, const TargetRegisterClass *> PhysReg = |
Eric Christopher | 11e4df7 | 2015-02-26 22:38:43 +0000 | [diff] [blame] | 149 | TLI->getRegForInlineAsmConstraint(TRI, Op.ConstraintCode, |
| 150 | Op.ConstraintVT); |
Hans Wennborg | acb842d | 2014-03-05 02:43:26 +0000 | [diff] [blame] | 151 | if (PhysReg.first == SP) |
Reid Kleckner | e69bdb8 | 2015-07-07 23:45:58 +0000 | [diff] [blame] | 152 | MF->getFrameInfo()->setHasOpaqueSPAdjustment(true); |
Hans Wennborg | acb842d | 2014-03-05 02:43:26 +0000 | [diff] [blame] | 153 | } |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 | |
Reid Kleckner | 2d9bb65 | 2014-08-22 21:59:26 +0000 | [diff] [blame] | 158 | // Look for calls to the @llvm.va_start intrinsic. We can omit some |
| 159 | // prologue boilerplate for variadic functions that don't examine their |
| 160 | // arguments. |
| 161 | if (const auto *II = dyn_cast<IntrinsicInst>(I)) { |
| 162 | if (II->getIntrinsicID() == Intrinsic::vastart) |
| 163 | MF->getFrameInfo()->setHasVAStart(true); |
| 164 | } |
| 165 | |
Reid Kleckner | 16e5541 | 2014-08-29 21:42:08 +0000 | [diff] [blame] | 166 | // If we have a musttail call in a variadic funciton, we need to ensure we |
| 167 | // forward implicit register parameters. |
Reid Kleckner | dccd0cb | 2014-08-29 21:42:21 +0000 | [diff] [blame] | 168 | if (const auto *CI = dyn_cast<CallInst>(I)) { |
Reid Kleckner | 16e5541 | 2014-08-29 21:42:08 +0000 | [diff] [blame] | 169 | if (CI->isMustTailCall() && Fn->isVarArg()) |
| 170 | MF->getFrameInfo()->setHasMustTailInVarArgFunc(true); |
| 171 | } |
| 172 | |
Dan Gohman | 1e936277 | 2010-07-16 17:54:27 +0000 | [diff] [blame] | 173 | // Mark values used outside their block as exported, by allocating |
| 174 | // a virtual register for them. |
Cameron Zwarich | f8b22b3 | 2011-02-22 03:24:52 +0000 | [diff] [blame] | 175 | if (isUsedOutsideOfDefiningBlock(I)) |
Dan Gohman | a3624b6 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 176 | if (!isa<AllocaInst>(I) || |
| 177 | !StaticAllocaMap.count(cast<AllocaInst>(I))) |
| 178 | InitializeRegForValue(I); |
| 179 | |
Dan Gohman | 1e936277 | 2010-07-16 17:54:27 +0000 | [diff] [blame] | 180 | // Collect llvm.dbg.declare information. This is done now instead of |
| 181 | // during the initial isel pass through the IR so that it is done |
| 182 | // in a predictable order. |
| 183 | if (const DbgDeclareInst *DI = dyn_cast<DbgDeclareInst>(I)) { |
Duncan P. N. Exon Smith | d4a19a3 | 2015-04-21 18:24:23 +0000 | [diff] [blame] | 184 | assert(DI->getVariable() && "Missing variable"); |
| 185 | assert(DI->getDebugLoc() && "Missing location"); |
| 186 | if (MMI.hasDebugInfo()) { |
Dan Gohman | 1e936277 | 2010-07-16 17:54:27 +0000 | [diff] [blame] | 187 | // Don't handle byval struct arguments or VLAs, for example. |
| 188 | // Non-byval arguments are handled here (they refer to the stack |
| 189 | // temporary alloca at this point). |
| 190 | const Value *Address = DI->getAddress(); |
| 191 | if (Address) { |
| 192 | if (const BitCastInst *BCI = dyn_cast<BitCastInst>(Address)) |
| 193 | Address = BCI->getOperand(0); |
| 194 | if (const AllocaInst *AI = dyn_cast<AllocaInst>(Address)) { |
| 195 | DenseMap<const AllocaInst *, int>::iterator SI = |
| 196 | StaticAllocaMap.find(AI); |
| 197 | if (SI != StaticAllocaMap.end()) { // Check for VLAs. |
| 198 | int FI = SI->second; |
Adrian Prantl | 87b7eb9 | 2014-10-01 18:55:02 +0000 | [diff] [blame] | 199 | MMI.setVariableDbgInfo(DI->getVariable(), DI->getExpression(), |
Dan Gohman | 1e936277 | 2010-07-16 17:54:27 +0000 | [diff] [blame] | 200 | FI, DI->getDebugLoc()); |
| 201 | } |
| 202 | } |
| 203 | } |
| 204 | } |
| 205 | } |
Jiangning Liu | ffbc690 | 2014-09-19 05:30:35 +0000 | [diff] [blame] | 206 | |
| 207 | // Decide the preferred extend type for a value. |
| 208 | PreferredExtendType[I] = getPreferredExtendForValue(I); |
Dan Gohman | 1e936277 | 2010-07-16 17:54:27 +0000 | [diff] [blame] | 209 | } |
| 210 | |
Dan Gohman | a3624b6 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 211 | // Create an initial MachineBasicBlock for each LLVM BasicBlock in F. This |
| 212 | // also creates the initial PHI MachineInstrs, though none of the input |
| 213 | // operands are populated. |
Dan Gohman | f57117d | 2010-04-14 16:30:40 +0000 | [diff] [blame] | 214 | for (BB = Fn->begin(); BB != EB; ++BB) { |
Reid Kleckner | 51189f0a | 2015-09-08 23:28:38 +0000 | [diff] [blame] | 215 | // Don't create MachineBasicBlocks for imaginary EH pad blocks. These blocks |
| 216 | // are really data, and no instructions can live here. |
| 217 | if (BB->isEHPad()) { |
| 218 | const Instruction *I = BB->getFirstNonPHI(); |
| 219 | if (!isa<LandingPadInst>(I)) |
| 220 | MMI.setHasEHFunclets(true); |
| 221 | if (isa<CatchPadInst>(I) || isa<CatchEndPadInst>(I) || |
| 222 | isa<CleanupEndPadInst>(I)) { |
| 223 | assert(&*BB->begin() == I && |
| 224 | "WinEHPrepare failed to remove PHIs from imaginary BBs"); |
| 225 | continue; |
Reid Kleckner | 51189f0a | 2015-09-08 23:28:38 +0000 | [diff] [blame] | 226 | } |
| 227 | } |
| 228 | |
Dan Gohman | a3624b6 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 229 | MachineBasicBlock *MBB = mf.CreateMachineBasicBlock(BB); |
| 230 | MBBMap[BB] = MBB; |
| 231 | MF->push_back(MBB); |
| 232 | |
| 233 | // Transfer the address-taken flag. This is necessary because there could |
| 234 | // be multiple MachineBasicBlocks corresponding to one BasicBlock, and only |
| 235 | // the first one should be marked. |
| 236 | if (BB->hasAddressTaken()) |
| 237 | MBB->setHasAddressTaken(); |
| 238 | |
| 239 | // Create Machine PHI nodes for LLVM PHI nodes, lowering them as |
| 240 | // appropriate. |
Dan Gohman | 0f055d3 | 2010-04-20 14:46:25 +0000 | [diff] [blame] | 241 | for (BasicBlock::const_iterator I = BB->begin(); |
| 242 | const PHINode *PN = dyn_cast<PHINode>(I); ++I) { |
| 243 | if (PN->use_empty()) continue; |
Dan Gohman | a3624b6 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 244 | |
Rafael Espindola | e53b7d1 | 2011-05-13 15:18:06 +0000 | [diff] [blame] | 245 | // Skip empty types |
| 246 | if (PN->getType()->isEmptyTy()) |
| 247 | continue; |
| 248 | |
Dan Gohman | 7b7f088 | 2010-04-20 14:48:02 +0000 | [diff] [blame] | 249 | DebugLoc DL = PN->getDebugLoc(); |
Dan Gohman | a3624b6 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 250 | unsigned PHIReg = ValueMap[PN]; |
| 251 | assert(PHIReg && "PHI node does not have an assigned virtual register!"); |
| 252 | |
| 253 | SmallVector<EVT, 4> ValueVTs; |
Mehdi Amini | 56228da | 2015-07-09 01:57:34 +0000 | [diff] [blame] | 254 | ComputeValueVTs(*TLI, MF->getDataLayout(), PN->getType(), ValueVTs); |
Dan Gohman | a3624b6 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 255 | for (unsigned vti = 0, vte = ValueVTs.size(); vti != vte; ++vti) { |
| 256 | EVT VT = ValueVTs[vti]; |
Bill Wendling | 8db01cb | 2013-06-06 00:11:39 +0000 | [diff] [blame] | 257 | unsigned NumRegisters = TLI->getNumRegisters(Fn->getContext(), VT); |
Eric Christopher | fc6de42 | 2014-08-05 02:39:49 +0000 | [diff] [blame] | 258 | const TargetInstrInfo *TII = MF->getSubtarget().getInstrInfo(); |
Dan Gohman | a3624b6 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 259 | for (unsigned i = 0; i != NumRegisters; ++i) |
Chris Lattner | b06015a | 2010-02-09 19:54:29 +0000 | [diff] [blame] | 260 | BuildMI(MBB, DL, TII->get(TargetOpcode::PHI), PHIReg + i); |
Dan Gohman | a3624b6 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 261 | PHIReg += NumRegisters; |
| 262 | } |
| 263 | } |
| 264 | } |
Dan Gohman | 69e8e32 | 2010-04-14 16:32:56 +0000 | [diff] [blame] | 265 | |
| 266 | // Mark landing pad blocks. |
Reid Kleckner | cfbfe6f | 2015-04-24 20:25:05 +0000 | [diff] [blame] | 267 | SmallVector<const LandingPadInst *, 4> LPads; |
Reid Kleckner | d2a1a51 | 2015-04-21 18:23:57 +0000 | [diff] [blame] | 268 | for (BB = Fn->begin(); BB != EB; ++BB) { |
Reid Kleckner | 0e28823 | 2015-08-27 23:27:47 +0000 | [diff] [blame] | 269 | const Instruction *FNP = BB->getFirstNonPHI(); |
Reid Kleckner | 7878391 | 2015-09-10 00:25:23 +0000 | [diff] [blame] | 270 | if (BB->isEHPad() && MBBMap.count(BB)) |
Reid Kleckner | 51189f0a | 2015-09-08 23:28:38 +0000 | [diff] [blame] | 271 | MBBMap[BB]->setIsEHPad(); |
Reid Kleckner | 0e28823 | 2015-08-27 23:27:47 +0000 | [diff] [blame] | 272 | if (const auto *LPI = dyn_cast<LandingPadInst>(FNP)) |
| 273 | LPads.push_back(LPI); |
Reid Kleckner | d2a1a51 | 2015-04-21 18:23:57 +0000 | [diff] [blame] | 274 | } |
David Majnemer | cde3303 | 2015-03-30 22:58:10 +0000 | [diff] [blame] | 275 | |
Joseph Tremoulet | 2afea54 | 2015-10-06 20:28:16 +0000 | [diff] [blame] | 276 | // If this personality uses funclets, we need to do a bit more work. |
Reid Kleckner | 0e28823 | 2015-08-27 23:27:47 +0000 | [diff] [blame] | 277 | if (!Fn->hasPersonalityFn()) |
| 278 | return; |
| 279 | EHPersonality Personality = classifyEHPersonality(Fn->getPersonalityFn()); |
Joseph Tremoulet | 2afea54 | 2015-10-06 20:28:16 +0000 | [diff] [blame] | 280 | if (!isFuncletEHPersonality(Personality)) |
Reid Kleckner | cfbfe6f | 2015-04-24 20:25:05 +0000 | [diff] [blame] | 281 | return; |
| 282 | |
Reid Kleckner | f12c030 | 2015-06-09 21:42:19 +0000 | [diff] [blame] | 283 | if (Personality == EHPersonality::MSVC_Win64SEH || |
| 284 | Personality == EHPersonality::MSVC_X86SEH) { |
Reid Kleckner | cfbfe6f | 2015-04-24 20:25:05 +0000 | [diff] [blame] | 285 | addSEHHandlersForLPads(LPads); |
Reid Kleckner | f12c030 | 2015-06-09 21:42:19 +0000 | [diff] [blame] | 286 | } |
Reid Kleckner | cfbfe6f | 2015-04-24 20:25:05 +0000 | [diff] [blame] | 287 | |
Reid Kleckner | 94b704c | 2015-09-09 21:10:03 +0000 | [diff] [blame] | 288 | // Calculate state numbers if we haven't already. |
Reid Kleckner | f12c030 | 2015-06-09 21:42:19 +0000 | [diff] [blame] | 289 | WinEHFuncInfo &EHInfo = MMI.getWinEHFuncInfo(&fn); |
Reid Kleckner | 7878391 | 2015-09-10 00:25:23 +0000 | [diff] [blame] | 290 | const Function *WinEHParentFn = MMI.getWinEHParent(&fn); |
| 291 | if (Personality == EHPersonality::MSVC_CXX) |
Reid Kleckner | f12c030 | 2015-06-09 21:42:19 +0000 | [diff] [blame] | 292 | calculateWinCXXEHStateNumbers(WinEHParentFn, EHInfo); |
David Majnemer | f828a0c | 2015-10-01 18:44:59 +0000 | [diff] [blame] | 293 | else if (isAsynchronousEHPersonality(Personality)) |
Reid Kleckner | 94b704c | 2015-09-09 21:10:03 +0000 | [diff] [blame] | 294 | calculateSEHStateNumbers(WinEHParentFn, EHInfo); |
Joseph Tremoulet | 7f8c116 | 2015-10-06 20:30:33 +0000 | [diff] [blame^] | 295 | else if (Personality == EHPersonality::CoreCLR) |
| 296 | calculateClrEHStateNumbers(WinEHParentFn, EHInfo); |
Reid Kleckner | 7878391 | 2015-09-10 00:25:23 +0000 | [diff] [blame] | 297 | |
David Majnemer | f828a0c | 2015-10-01 18:44:59 +0000 | [diff] [blame] | 298 | calculateCatchReturnSuccessorColors(WinEHParentFn, EHInfo); |
| 299 | |
Reid Kleckner | 7878391 | 2015-09-10 00:25:23 +0000 | [diff] [blame] | 300 | // Map all BB references in the WinEH data to MBBs. |
Reid Kleckner | b005d28 | 2015-09-16 20:16:27 +0000 | [diff] [blame] | 301 | for (WinEHTryBlockMapEntry &TBME : EHInfo.TryBlockMap) { |
| 302 | for (WinEHHandlerType &H : TBME.HandlerArray) { |
| 303 | if (H.CatchObjRecoverIdx == -2 && H.CatchObj.Alloca) { |
| 304 | assert(StaticAllocaMap.count(H.CatchObj.Alloca)); |
| 305 | H.CatchObj.FrameIndex = StaticAllocaMap[H.CatchObj.Alloca]; |
| 306 | } else { |
| 307 | H.CatchObj.FrameIndex = INT_MAX; |
| 308 | } |
| 309 | if (const auto *BB = dyn_cast<BasicBlock>(H.Handler.get<const Value *>())) |
Reid Kleckner | 7878391 | 2015-09-10 00:25:23 +0000 | [diff] [blame] | 310 | H.Handler = MBBMap[BB]; |
Reid Kleckner | b005d28 | 2015-09-16 20:16:27 +0000 | [diff] [blame] | 311 | } |
| 312 | } |
Reid Kleckner | 7878391 | 2015-09-10 00:25:23 +0000 | [diff] [blame] | 313 | for (WinEHUnwindMapEntry &UME : EHInfo.UnwindMap) |
| 314 | if (UME.Cleanup) |
| 315 | if (const auto *BB = dyn_cast<BasicBlock>(UME.Cleanup.get<const Value *>())) |
| 316 | UME.Cleanup = MBBMap[BB]; |
| 317 | for (SEHUnwindMapEntry &UME : EHInfo.SEHUnwindMap) { |
| 318 | const BasicBlock *BB = UME.Handler.get<const BasicBlock *>(); |
| 319 | UME.Handler = MBBMap[BB]; |
Reid Kleckner | 94b704c | 2015-09-09 21:10:03 +0000 | [diff] [blame] | 320 | } |
Joseph Tremoulet | 7f8c116 | 2015-10-06 20:30:33 +0000 | [diff] [blame^] | 321 | for (ClrEHUnwindMapEntry &CME : EHInfo.ClrEHUnwindMap) { |
| 322 | const BasicBlock *BB = CME.Handler.get<const BasicBlock *>(); |
| 323 | CME.Handler = MBBMap[BB]; |
| 324 | } |
Reid Kleckner | 94b704c | 2015-09-09 21:10:03 +0000 | [diff] [blame] | 325 | |
Reid Kleckner | 94b704c | 2015-09-09 21:10:03 +0000 | [diff] [blame] | 326 | // If there's an explicit EH registration node on the stack, record its |
| 327 | // frame index. |
| 328 | if (EHInfo.EHRegNode && EHInfo.EHRegNode->getParent()->getParent() == Fn) { |
| 329 | assert(StaticAllocaMap.count(EHInfo.EHRegNode)); |
| 330 | EHInfo.EHRegNodeFrameIndex = StaticAllocaMap[EHInfo.EHRegNode]; |
Reid Kleckner | f12c030 | 2015-06-09 21:42:19 +0000 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | // Copy the state numbers to LandingPadInfo for the current function, which |
| 334 | // could be a handler or the parent. This should happen for 32-bit SEH and |
| 335 | // C++ EH. |
| 336 | if (Personality == EHPersonality::MSVC_CXX || |
| 337 | Personality == EHPersonality::MSVC_X86SEH) { |
Reid Kleckner | cfbfe6f | 2015-04-24 20:25:05 +0000 | [diff] [blame] | 338 | for (const LandingPadInst *LP : LPads) { |
| 339 | MachineBasicBlock *LPadMBB = MBBMap[LP->getParent()]; |
David Majnemer | 0ad363e | 2015-08-18 19:07:12 +0000 | [diff] [blame] | 340 | MMI.addWinEHState(LPadMBB, EHInfo.EHPadStateMap[LP]); |
Reid Kleckner | cfbfe6f | 2015-04-24 20:25:05 +0000 | [diff] [blame] | 341 | } |
David Majnemer | a225a19 | 2015-03-31 22:35:44 +0000 | [diff] [blame] | 342 | } |
David Majnemer | cde3303 | 2015-03-30 22:58:10 +0000 | [diff] [blame] | 343 | } |
| 344 | |
Reid Kleckner | cfbfe6f | 2015-04-24 20:25:05 +0000 | [diff] [blame] | 345 | void FunctionLoweringInfo::addSEHHandlersForLPads( |
| 346 | ArrayRef<const LandingPadInst *> LPads) { |
Reid Kleckner | d2a1a51 | 2015-04-21 18:23:57 +0000 | [diff] [blame] | 347 | MachineModuleInfo &MMI = MF->getMMI(); |
| 348 | |
| 349 | // Iterate over all landing pads with llvm.eh.actions calls. |
Reid Kleckner | cfbfe6f | 2015-04-24 20:25:05 +0000 | [diff] [blame] | 350 | for (const LandingPadInst *LP : LPads) { |
Reid Kleckner | d2a1a51 | 2015-04-21 18:23:57 +0000 | [diff] [blame] | 351 | const IntrinsicInst *ActionsCall = |
| 352 | dyn_cast<IntrinsicInst>(LP->getNextNode()); |
| 353 | if (!ActionsCall || |
| 354 | ActionsCall->getIntrinsicID() != Intrinsic::eh_actions) |
| 355 | continue; |
| 356 | |
| 357 | // Parse the llvm.eh.actions call we found. |
| 358 | MachineBasicBlock *LPadMBB = MBBMap[LP->getParent()]; |
Benjamin Kramer | a48e065 | 2015-05-16 15:40:03 +0000 | [diff] [blame] | 359 | SmallVector<std::unique_ptr<ActionHandler>, 4> Actions; |
Reid Kleckner | d2a1a51 | 2015-04-21 18:23:57 +0000 | [diff] [blame] | 360 | parseEHActions(ActionsCall, Actions); |
| 361 | |
| 362 | // Iterate EH actions from most to least precedence, which means |
| 363 | // iterating in reverse. |
| 364 | for (auto I = Actions.rbegin(), E = Actions.rend(); I != E; ++I) { |
Benjamin Kramer | a48e065 | 2015-05-16 15:40:03 +0000 | [diff] [blame] | 365 | ActionHandler *Action = I->get(); |
Reid Kleckner | d2a1a51 | 2015-04-21 18:23:57 +0000 | [diff] [blame] | 366 | if (auto *CH = dyn_cast<CatchHandler>(Action)) { |
| 367 | const auto *Filter = |
| 368 | dyn_cast<Function>(CH->getSelector()->stripPointerCasts()); |
| 369 | assert((Filter || CH->getSelector()->isNullValue()) && |
| 370 | "expected function or catch-all"); |
| 371 | const auto *RecoverBA = |
| 372 | cast<BlockAddress>(CH->getHandlerBlockOrFunc()); |
| 373 | MMI.addSEHCatchHandler(LPadMBB, Filter, RecoverBA); |
| 374 | } else { |
| 375 | assert(isa<CleanupHandler>(Action)); |
| 376 | const auto *Fini = cast<Function>(Action->getHandlerBlockOrFunc()); |
| 377 | MMI.addSEHCleanupHandler(LPadMBB, Fini); |
| 378 | } |
| 379 | } |
Reid Kleckner | d2a1a51 | 2015-04-21 18:23:57 +0000 | [diff] [blame] | 380 | } |
| 381 | } |
| 382 | |
Dan Gohman | a3624b6 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 383 | /// clear - Clear out all the function-specific state. This returns this |
| 384 | /// FunctionLoweringInfo to an empty state, ready to be used for a |
| 385 | /// different function. |
| 386 | void FunctionLoweringInfo::clear() { |
Dan Gohman | ad0b3ea | 2010-04-14 17:11:23 +0000 | [diff] [blame] | 387 | assert(CatchInfoFound.size() == CatchInfoLost.size() && |
| 388 | "Not all catch info was assigned to a landing pad!"); |
| 389 | |
Dan Gohman | a3624b6 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 390 | MBBMap.clear(); |
| 391 | ValueMap.clear(); |
| 392 | StaticAllocaMap.clear(); |
| 393 | #ifndef NDEBUG |
| 394 | CatchInfoLost.clear(); |
| 395 | CatchInfoFound.clear(); |
| 396 | #endif |
| 397 | LiveOutRegInfo.clear(); |
Cameron Zwarich | 988faf9 | 2011-02-24 10:00:13 +0000 | [diff] [blame] | 398 | VisitedBBs.clear(); |
Evan Cheng | 6e82245 | 2010-04-28 23:08:54 +0000 | [diff] [blame] | 399 | ArgDbgValues.clear(); |
Devang Patel | 86ec8b3 | 2010-08-31 22:22:42 +0000 | [diff] [blame] | 400 | ByValArgFrameIndexMap.clear(); |
Dan Gohman | d7b5ce3 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 401 | RegFixups.clear(); |
Philip Reames | 1a1bdb2 | 2014-12-02 18:50:36 +0000 | [diff] [blame] | 402 | StatepointStackSlots.clear(); |
Igor Laevsky | 423bc9ec | 2015-05-20 11:37:25 +0000 | [diff] [blame] | 403 | StatepointRelocatedValues.clear(); |
Jiangning Liu | 3b09617 | 2014-09-24 03:22:56 +0000 | [diff] [blame] | 404 | PreferredExtendType.clear(); |
Dan Gohman | a3624b6 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 405 | } |
| 406 | |
Dan Gohman | 93f5920 | 2010-07-02 00:10:16 +0000 | [diff] [blame] | 407 | /// CreateReg - Allocate a single virtual register for the given type. |
Patrik Hagglund | 5e6c361 | 2012-12-13 06:34:11 +0000 | [diff] [blame] | 408 | unsigned FunctionLoweringInfo::CreateReg(MVT VT) { |
Eric Christopher | d913448 | 2014-08-04 21:25:23 +0000 | [diff] [blame] | 409 | return RegInfo->createVirtualRegister( |
Eric Christopher | 2ae2de7 | 2014-10-09 00:57:31 +0000 | [diff] [blame] | 410 | MF->getSubtarget().getTargetLowering()->getRegClassFor(VT)); |
Dan Gohman | a3624b6 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 411 | } |
| 412 | |
Dan Gohman | 93f5920 | 2010-07-02 00:10:16 +0000 | [diff] [blame] | 413 | /// CreateRegs - Allocate the appropriate number of virtual registers of |
Dan Gohman | a3624b6 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 414 | /// the correctly promoted or expanded types. Assign these registers |
| 415 | /// consecutive vreg numbers and return the first assigned number. |
| 416 | /// |
| 417 | /// In the case that the given value has struct or array type, this function |
| 418 | /// will assign registers for each member or element. |
| 419 | /// |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 420 | unsigned FunctionLoweringInfo::CreateRegs(Type *Ty) { |
Eric Christopher | 2ae2de7 | 2014-10-09 00:57:31 +0000 | [diff] [blame] | 421 | const TargetLowering *TLI = MF->getSubtarget().getTargetLowering(); |
Bill Wendling | 0ccf310 | 2013-06-19 20:32:16 +0000 | [diff] [blame] | 422 | |
Dan Gohman | a3624b6 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 423 | SmallVector<EVT, 4> ValueVTs; |
Mehdi Amini | 56228da | 2015-07-09 01:57:34 +0000 | [diff] [blame] | 424 | ComputeValueVTs(*TLI, MF->getDataLayout(), Ty, ValueVTs); |
Dan Gohman | a3624b6 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 425 | |
| 426 | unsigned FirstReg = 0; |
| 427 | for (unsigned Value = 0, e = ValueVTs.size(); Value != e; ++Value) { |
| 428 | EVT ValueVT = ValueVTs[Value]; |
Bill Wendling | 8db01cb | 2013-06-06 00:11:39 +0000 | [diff] [blame] | 429 | MVT RegisterVT = TLI->getRegisterType(Ty->getContext(), ValueVT); |
Dan Gohman | a3624b6 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 430 | |
Bill Wendling | 8db01cb | 2013-06-06 00:11:39 +0000 | [diff] [blame] | 431 | unsigned NumRegs = TLI->getNumRegisters(Ty->getContext(), ValueVT); |
Dan Gohman | a3624b6 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 432 | for (unsigned i = 0; i != NumRegs; ++i) { |
Dan Gohman | 93f5920 | 2010-07-02 00:10:16 +0000 | [diff] [blame] | 433 | unsigned R = CreateReg(RegisterVT); |
Dan Gohman | a3624b6 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 434 | if (!FirstReg) FirstReg = R; |
| 435 | } |
| 436 | } |
| 437 | return FirstReg; |
| 438 | } |
Dan Gohman | ad97b3d | 2009-11-23 17:42:46 +0000 | [diff] [blame] | 439 | |
Cameron Zwarich | a62fc89 | 2011-02-24 10:00:25 +0000 | [diff] [blame] | 440 | /// GetLiveOutRegInfo - Gets LiveOutInfo for a register, returning NULL if the |
| 441 | /// register is a PHI destination and the PHI's LiveOutInfo is not valid. If |
| 442 | /// the register's LiveOutInfo is for a smaller bit width, it is extended to |
| 443 | /// the larger bit width by zero extension. The bit width must be no smaller |
| 444 | /// than the LiveOutInfo's existing bit width. |
| 445 | const FunctionLoweringInfo::LiveOutInfo * |
| 446 | FunctionLoweringInfo::GetLiveOutRegInfo(unsigned Reg, unsigned BitWidth) { |
| 447 | if (!LiveOutRegInfo.inBounds(Reg)) |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 448 | return nullptr; |
Cameron Zwarich | a62fc89 | 2011-02-24 10:00:25 +0000 | [diff] [blame] | 449 | |
| 450 | LiveOutInfo *LOI = &LiveOutRegInfo[Reg]; |
| 451 | if (!LOI->IsValid) |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 452 | return nullptr; |
Cameron Zwarich | a62fc89 | 2011-02-24 10:00:25 +0000 | [diff] [blame] | 453 | |
Cameron Zwarich | d2f3041 | 2011-02-25 01:10:55 +0000 | [diff] [blame] | 454 | if (BitWidth > LOI->KnownZero.getBitWidth()) { |
Cameron Zwarich | 4c82cd2 | 2011-02-25 01:11:01 +0000 | [diff] [blame] | 455 | LOI->NumSignBits = 1; |
Cameron Zwarich | a62fc89 | 2011-02-24 10:00:25 +0000 | [diff] [blame] | 456 | LOI->KnownZero = LOI->KnownZero.zextOrTrunc(BitWidth); |
| 457 | LOI->KnownOne = LOI->KnownOne.zextOrTrunc(BitWidth); |
| 458 | } |
| 459 | |
| 460 | return LOI; |
| 461 | } |
| 462 | |
| 463 | /// ComputePHILiveOutRegInfo - Compute LiveOutInfo for a PHI's destination |
| 464 | /// register based on the LiveOutInfo of its operands. |
| 465 | void FunctionLoweringInfo::ComputePHILiveOutRegInfo(const PHINode *PN) { |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 466 | Type *Ty = PN->getType(); |
Cameron Zwarich | a62fc89 | 2011-02-24 10:00:25 +0000 | [diff] [blame] | 467 | if (!Ty->isIntegerTy() || Ty->isVectorTy()) |
| 468 | return; |
| 469 | |
| 470 | SmallVector<EVT, 1> ValueVTs; |
Mehdi Amini | 56228da | 2015-07-09 01:57:34 +0000 | [diff] [blame] | 471 | ComputeValueVTs(*TLI, MF->getDataLayout(), Ty, ValueVTs); |
Cameron Zwarich | a62fc89 | 2011-02-24 10:00:25 +0000 | [diff] [blame] | 472 | assert(ValueVTs.size() == 1 && |
| 473 | "PHIs with non-vector integer types should have a single VT."); |
| 474 | EVT IntVT = ValueVTs[0]; |
| 475 | |
Bill Wendling | 8db01cb | 2013-06-06 00:11:39 +0000 | [diff] [blame] | 476 | if (TLI->getNumRegisters(PN->getContext(), IntVT) != 1) |
Cameron Zwarich | a62fc89 | 2011-02-24 10:00:25 +0000 | [diff] [blame] | 477 | return; |
Bill Wendling | 8db01cb | 2013-06-06 00:11:39 +0000 | [diff] [blame] | 478 | IntVT = TLI->getTypeToTransformTo(PN->getContext(), IntVT); |
Cameron Zwarich | a62fc89 | 2011-02-24 10:00:25 +0000 | [diff] [blame] | 479 | unsigned BitWidth = IntVT.getSizeInBits(); |
| 480 | |
| 481 | unsigned DestReg = ValueMap[PN]; |
| 482 | if (!TargetRegisterInfo::isVirtualRegister(DestReg)) |
| 483 | return; |
| 484 | LiveOutRegInfo.grow(DestReg); |
| 485 | LiveOutInfo &DestLOI = LiveOutRegInfo[DestReg]; |
| 486 | |
| 487 | Value *V = PN->getIncomingValue(0); |
| 488 | if (isa<UndefValue>(V) || isa<ConstantExpr>(V)) { |
| 489 | DestLOI.NumSignBits = 1; |
| 490 | APInt Zero(BitWidth, 0); |
| 491 | DestLOI.KnownZero = Zero; |
| 492 | DestLOI.KnownOne = Zero; |
| 493 | return; |
| 494 | } |
| 495 | |
| 496 | if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) { |
| 497 | APInt Val = CI->getValue().zextOrTrunc(BitWidth); |
| 498 | DestLOI.NumSignBits = Val.getNumSignBits(); |
| 499 | DestLOI.KnownZero = ~Val; |
| 500 | DestLOI.KnownOne = Val; |
| 501 | } else { |
| 502 | assert(ValueMap.count(V) && "V should have been placed in ValueMap when its" |
| 503 | "CopyToReg node was created."); |
| 504 | unsigned SrcReg = ValueMap[V]; |
| 505 | if (!TargetRegisterInfo::isVirtualRegister(SrcReg)) { |
| 506 | DestLOI.IsValid = false; |
| 507 | return; |
| 508 | } |
| 509 | const LiveOutInfo *SrcLOI = GetLiveOutRegInfo(SrcReg, BitWidth); |
| 510 | if (!SrcLOI) { |
| 511 | DestLOI.IsValid = false; |
| 512 | return; |
| 513 | } |
| 514 | DestLOI = *SrcLOI; |
| 515 | } |
| 516 | |
| 517 | assert(DestLOI.KnownZero.getBitWidth() == BitWidth && |
| 518 | DestLOI.KnownOne.getBitWidth() == BitWidth && |
| 519 | "Masks should have the same bit width as the type."); |
| 520 | |
| 521 | for (unsigned i = 1, e = PN->getNumIncomingValues(); i != e; ++i) { |
| 522 | Value *V = PN->getIncomingValue(i); |
| 523 | if (isa<UndefValue>(V) || isa<ConstantExpr>(V)) { |
| 524 | DestLOI.NumSignBits = 1; |
| 525 | APInt Zero(BitWidth, 0); |
| 526 | DestLOI.KnownZero = Zero; |
| 527 | DestLOI.KnownOne = Zero; |
Eric Christopher | 0713a9d | 2011-06-08 23:55:35 +0000 | [diff] [blame] | 528 | return; |
Cameron Zwarich | a62fc89 | 2011-02-24 10:00:25 +0000 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) { |
| 532 | APInt Val = CI->getValue().zextOrTrunc(BitWidth); |
| 533 | DestLOI.NumSignBits = std::min(DestLOI.NumSignBits, Val.getNumSignBits()); |
| 534 | DestLOI.KnownZero &= ~Val; |
| 535 | DestLOI.KnownOne &= Val; |
| 536 | continue; |
| 537 | } |
| 538 | |
| 539 | assert(ValueMap.count(V) && "V should have been placed in ValueMap when " |
| 540 | "its CopyToReg node was created."); |
| 541 | unsigned SrcReg = ValueMap[V]; |
| 542 | if (!TargetRegisterInfo::isVirtualRegister(SrcReg)) { |
| 543 | DestLOI.IsValid = false; |
| 544 | return; |
| 545 | } |
| 546 | const LiveOutInfo *SrcLOI = GetLiveOutRegInfo(SrcReg, BitWidth); |
| 547 | if (!SrcLOI) { |
| 548 | DestLOI.IsValid = false; |
| 549 | return; |
| 550 | } |
| 551 | DestLOI.NumSignBits = std::min(DestLOI.NumSignBits, SrcLOI->NumSignBits); |
| 552 | DestLOI.KnownZero &= SrcLOI->KnownZero; |
| 553 | DestLOI.KnownOne &= SrcLOI->KnownOne; |
| 554 | } |
| 555 | } |
| 556 | |
Devang Patel | 9d904e1 | 2011-09-08 22:59:09 +0000 | [diff] [blame] | 557 | /// setArgumentFrameIndex - Record frame index for the byval |
Devang Patel | 86ec8b3 | 2010-08-31 22:22:42 +0000 | [diff] [blame] | 558 | /// argument. This overrides previous frame index entry for this argument, |
| 559 | /// if any. |
Devang Patel | 9d904e1 | 2011-09-08 22:59:09 +0000 | [diff] [blame] | 560 | void FunctionLoweringInfo::setArgumentFrameIndex(const Argument *A, |
Eric Christopher | 219d51d | 2012-02-24 01:59:01 +0000 | [diff] [blame] | 561 | int FI) { |
Devang Patel | 86ec8b3 | 2010-08-31 22:22:42 +0000 | [diff] [blame] | 562 | ByValArgFrameIndexMap[A] = FI; |
| 563 | } |
Eric Christopher | 0713a9d | 2011-06-08 23:55:35 +0000 | [diff] [blame] | 564 | |
Devang Patel | 9d904e1 | 2011-09-08 22:59:09 +0000 | [diff] [blame] | 565 | /// getArgumentFrameIndex - Get frame index for the byval argument. |
Devang Patel | 86ec8b3 | 2010-08-31 22:22:42 +0000 | [diff] [blame] | 566 | /// If the argument does not have any assigned frame index then 0 is |
| 567 | /// returned. |
Devang Patel | 9d904e1 | 2011-09-08 22:59:09 +0000 | [diff] [blame] | 568 | int FunctionLoweringInfo::getArgumentFrameIndex(const Argument *A) { |
Eric Christopher | 0713a9d | 2011-06-08 23:55:35 +0000 | [diff] [blame] | 569 | DenseMap<const Argument *, int>::iterator I = |
Devang Patel | 86ec8b3 | 2010-08-31 22:22:42 +0000 | [diff] [blame] | 570 | ByValArgFrameIndexMap.find(A); |
| 571 | if (I != ByValArgFrameIndexMap.end()) |
| 572 | return I->second; |
Eric Christopher | 18c6be7 | 2012-02-23 03:39:43 +0000 | [diff] [blame] | 573 | DEBUG(dbgs() << "Argument does not have assigned frame index!\n"); |
Devang Patel | 86ec8b3 | 2010-08-31 22:22:42 +0000 | [diff] [blame] | 574 | return 0; |
| 575 | } |
| 576 | |
Michael J. Spencer | 8b98bf2 | 2012-02-22 19:06:13 +0000 | [diff] [blame] | 577 | /// ComputeUsesVAFloatArgument - Determine if any floating-point values are |
| 578 | /// being passed to this variadic function, and set the MachineModuleInfo's |
| 579 | /// usesVAFloatArgument flag if so. This flag is used to emit an undefined |
| 580 | /// reference to _fltused on Windows, which will link in MSVCRT's |
| 581 | /// floating-point support. |
| 582 | void llvm::ComputeUsesVAFloatArgument(const CallInst &I, |
| 583 | MachineModuleInfo *MMI) |
| 584 | { |
| 585 | FunctionType *FT = cast<FunctionType>( |
| 586 | I.getCalledValue()->getType()->getContainedType(0)); |
| 587 | if (FT->isVarArg() && !MMI->usesVAFloatArgument()) { |
| 588 | for (unsigned i = 0, e = I.getNumArgOperands(); i != e; ++i) { |
| 589 | Type* T = I.getArgOperand(i)->getType(); |
Daniel Berlin | 25db4f4 | 2015-04-15 17:41:42 +0000 | [diff] [blame] | 590 | for (auto i : post_order(T)) { |
Michael J. Spencer | 8b98bf2 | 2012-02-22 19:06:13 +0000 | [diff] [blame] | 591 | if (i->isFloatingPointTy()) { |
| 592 | MMI->setUsesVAFloatArgument(true); |
| 593 | return; |
| 594 | } |
| 595 | } |
| 596 | } |
| 597 | } |
| 598 | } |
| 599 | |
Bill Wendling | 247fd3b | 2011-08-17 21:56:44 +0000 | [diff] [blame] | 600 | /// AddLandingPadInfo - Extract the exception handling information from the |
| 601 | /// landingpad instruction and add them to the specified machine module info. |
| 602 | void llvm::AddLandingPadInfo(const LandingPadInst &I, MachineModuleInfo &MMI, |
| 603 | MachineBasicBlock *MBB) { |
Reid Kleckner | e00faf8 | 2015-08-31 20:02:16 +0000 | [diff] [blame] | 604 | if (const auto *PF = dyn_cast<Function>( |
| 605 | I.getParent()->getParent()->getPersonalityFn()->stripPointerCasts())) |
| 606 | MMI.addPersonality(PF); |
Bill Wendling | 247fd3b | 2011-08-17 21:56:44 +0000 | [diff] [blame] | 607 | |
| 608 | if (I.isCleanup()) |
| 609 | MMI.addCleanup(MBB); |
| 610 | |
| 611 | // FIXME: New EH - Add the clauses in reverse order. This isn't 100% correct, |
| 612 | // but we need to do it this way because of how the DWARF EH emitter |
| 613 | // processes the clauses. |
| 614 | for (unsigned i = I.getNumClauses(); i != 0; --i) { |
| 615 | Value *Val = I.getClause(i - 1); |
| 616 | if (I.isCatch(i - 1)) { |
| 617 | MMI.addCatchTypeInfo(MBB, |
Reid Kleckner | 283bc2e | 2014-11-14 00:35:50 +0000 | [diff] [blame] | 618 | dyn_cast<GlobalValue>(Val->stripPointerCasts())); |
Bill Wendling | 247fd3b | 2011-08-17 21:56:44 +0000 | [diff] [blame] | 619 | } else { |
| 620 | // Add filters in a list. |
| 621 | Constant *CVal = cast<Constant>(Val); |
Reid Kleckner | 283bc2e | 2014-11-14 00:35:50 +0000 | [diff] [blame] | 622 | SmallVector<const GlobalValue*, 4> FilterList; |
Bill Wendling | 247fd3b | 2011-08-17 21:56:44 +0000 | [diff] [blame] | 623 | for (User::op_iterator |
| 624 | II = CVal->op_begin(), IE = CVal->op_end(); II != IE; ++II) |
Reid Kleckner | 283bc2e | 2014-11-14 00:35:50 +0000 | [diff] [blame] | 625 | FilterList.push_back(cast<GlobalValue>((*II)->stripPointerCasts())); |
Bill Wendling | 247fd3b | 2011-08-17 21:56:44 +0000 | [diff] [blame] | 626 | |
| 627 | MMI.addFilterTypeInfo(MBB, FilterList); |
| 628 | } |
| 629 | } |
| 630 | } |