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