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/CodeGen/Analysis.h" |
| 17 | #include "llvm/CodeGen/MachineFrameInfo.h" |
| 18 | #include "llvm/CodeGen/MachineFunction.h" |
| 19 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
David Blaikie | 3f833ed | 2017-11-08 01:01:31 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/TargetFrameLowering.h" |
| 22 | #include "llvm/CodeGen/TargetInstrInfo.h" |
David Blaikie | b3bde2e | 2017-11-17 01:07:10 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/TargetLowering.h" |
| 24 | #include "llvm/CodeGen/TargetRegisterInfo.h" |
| 25 | #include "llvm/CodeGen/TargetSubtargetInfo.h" |
David Majnemer | cde3303 | 2015-03-30 22:58:10 +0000 | [diff] [blame] | 26 | #include "llvm/CodeGen/WinEHFuncInfo.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 27 | #include "llvm/IR/DataLayout.h" |
| 28 | #include "llvm/IR/DerivedTypes.h" |
| 29 | #include "llvm/IR/Function.h" |
| 30 | #include "llvm/IR/Instructions.h" |
| 31 | #include "llvm/IR/IntrinsicInst.h" |
| 32 | #include "llvm/IR/LLVMContext.h" |
| 33 | #include "llvm/IR/Module.h" |
Dan Gohman | a3624b6 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 34 | #include "llvm/Support/Debug.h" |
| 35 | #include "llvm/Support/ErrorHandling.h" |
| 36 | #include "llvm/Support/MathExtras.h" |
Benjamin Kramer | 799003b | 2015-03-23 19:32:43 +0000 | [diff] [blame] | 37 | #include "llvm/Support/raw_ostream.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 38 | #include "llvm/Target/TargetOptions.h" |
Dan Gohman | a3624b6 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 39 | #include <algorithm> |
| 40 | using namespace llvm; |
| 41 | |
Chandler Carruth | 1b9dde0 | 2014-04-22 02:02:50 +0000 | [diff] [blame] | 42 | #define DEBUG_TYPE "function-lowering-info" |
| 43 | |
Dan Gohman | a3624b6 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 44 | /// isUsedOutsideOfDefiningBlock - Return true if this instruction is used by |
| 45 | /// PHI nodes or outside of the basic block that defines it, or used by a |
| 46 | /// switch or atomic instruction, which may expand to multiple basic blocks. |
Dan Gohman | 913c998 | 2010-04-15 04:33:49 +0000 | [diff] [blame] | 47 | static bool isUsedOutsideOfDefiningBlock(const Instruction *I) { |
Dan Gohman | 7c845e4 | 2010-04-20 14:50:13 +0000 | [diff] [blame] | 48 | if (I->use_empty()) return false; |
Dan Gohman | a3624b6 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 49 | if (isa<PHINode>(I)) return true; |
Dan Gohman | 913c998 | 2010-04-15 04:33:49 +0000 | [diff] [blame] | 50 | const BasicBlock *BB = I->getParent(); |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 51 | for (const User *U : I->users()) |
Gabor Greif | 52617fc | 2010-07-09 16:08:33 +0000 | [diff] [blame] | 52 | if (cast<Instruction>(U)->getParent() != BB || isa<PHINode>(U)) |
Dan Gohman | a3624b6 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 53 | return true; |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 54 | |
Dan Gohman | a3624b6 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 55 | return false; |
| 56 | } |
| 57 | |
Jiangning Liu | ffbc690 | 2014-09-19 05:30:35 +0000 | [diff] [blame] | 58 | static ISD::NodeType getPreferredExtendForValue(const Value *V) { |
| 59 | // For the users of the source value being used for compare instruction, if |
| 60 | // the number of signed predicate is greater than unsigned predicate, we |
| 61 | // prefer to use SIGN_EXTEND. |
| 62 | // |
| 63 | // With this optimization, we would be able to reduce some redundant sign or |
| 64 | // zero extension instruction, and eventually more machine CSE opportunities |
| 65 | // can be exposed. |
| 66 | ISD::NodeType ExtendKind = ISD::ANY_EXTEND; |
| 67 | unsigned NumOfSigned = 0, NumOfUnsigned = 0; |
| 68 | for (const User *U : V->users()) { |
| 69 | if (const auto *CI = dyn_cast<CmpInst>(U)) { |
| 70 | NumOfSigned += CI->isSigned(); |
| 71 | NumOfUnsigned += CI->isUnsigned(); |
| 72 | } |
| 73 | } |
| 74 | if (NumOfSigned > NumOfUnsigned) |
| 75 | ExtendKind = ISD::SIGN_EXTEND; |
| 76 | |
| 77 | return ExtendKind; |
| 78 | } |
| 79 | |
Hans Wennborg | acb842d | 2014-03-05 02:43:26 +0000 | [diff] [blame] | 80 | void FunctionLoweringInfo::set(const Function &fn, MachineFunction &mf, |
| 81 | SelectionDAG *DAG) { |
Dan Gohman | a3624b6 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 82 | Fn = &fn; |
| 83 | MF = &mf; |
Eric Christopher | 2ae2de7 | 2014-10-09 00:57:31 +0000 | [diff] [blame] | 84 | TLI = MF->getSubtarget().getTargetLowering(); |
Dan Gohman | a3624b6 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 85 | RegInfo = &MF->getRegInfo(); |
Jonas Paulsson | f12b925 | 2015-11-28 11:02:32 +0000 | [diff] [blame] | 86 | const TargetFrameLowering *TFI = MF->getSubtarget().getFrameLowering(); |
David Majnemer | 1ef6540 | 2016-03-03 00:01:25 +0000 | [diff] [blame] | 87 | unsigned StackAlign = TFI->getStackAlignment(); |
Dan Gohman | a3624b6 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 88 | |
Dan Gohman | d7b5ce3 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 89 | // Check whether the function can return without sret-demotion. |
| 90 | SmallVector<ISD::OutputArg, 4> Outs; |
Mehdi Amini | 56228da | 2015-07-09 01:57:34 +0000 | [diff] [blame] | 91 | GetReturnInfo(Fn->getReturnType(), Fn->getAttributes(), Outs, *TLI, |
| 92 | mf.getDataLayout()); |
Bill Wendling | 8db01cb | 2013-06-06 00:11:39 +0000 | [diff] [blame] | 93 | CanLowerReturn = TLI->CanLowerReturn(Fn->getCallingConv(), *MF, |
Eric Christopher | 2ae2de7 | 2014-10-09 00:57:31 +0000 | [diff] [blame] | 94 | Fn->isVarArg(), Outs, Fn->getContext()); |
Dan Gohman | d7b5ce3 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 95 | |
David Majnemer | 1ef6540 | 2016-03-03 00:01:25 +0000 | [diff] [blame] | 96 | // If this personality uses funclets, we need to do a bit more work. |
Reid Kleckner | f7ad534 | 2016-10-19 17:08:23 +0000 | [diff] [blame] | 97 | DenseMap<const AllocaInst *, TinyPtrVector<int *>> CatchObjects; |
David Majnemer | 1ef6540 | 2016-03-03 00:01:25 +0000 | [diff] [blame] | 98 | EHPersonality Personality = classifyEHPersonality( |
| 99 | Fn->hasPersonalityFn() ? Fn->getPersonalityFn() : nullptr); |
| 100 | if (isFuncletEHPersonality(Personality)) { |
| 101 | // Calculate state numbers if we haven't already. |
| 102 | WinEHFuncInfo &EHInfo = *MF->getWinEHFuncInfo(); |
| 103 | if (Personality == EHPersonality::MSVC_CXX) |
| 104 | calculateWinCXXEHStateNumbers(&fn, EHInfo); |
| 105 | else if (isAsynchronousEHPersonality(Personality)) |
| 106 | calculateSEHStateNumbers(&fn, EHInfo); |
| 107 | else if (Personality == EHPersonality::CoreCLR) |
| 108 | calculateClrEHStateNumbers(&fn, EHInfo); |
| 109 | |
| 110 | // Map all BB references in the WinEH data to MBBs. |
| 111 | for (WinEHTryBlockMapEntry &TBME : EHInfo.TryBlockMap) { |
| 112 | for (WinEHHandlerType &H : TBME.HandlerArray) { |
| 113 | if (const AllocaInst *AI = H.CatchObj.Alloca) |
Reid Kleckner | f7ad534 | 2016-10-19 17:08:23 +0000 | [diff] [blame] | 114 | CatchObjects.insert({AI, {}}).first->second.push_back( |
| 115 | &H.CatchObj.FrameIndex); |
David Majnemer | 1ef6540 | 2016-03-03 00:01:25 +0000 | [diff] [blame] | 116 | else |
| 117 | H.CatchObj.FrameIndex = INT_MAX; |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | |
Dan Gohman | a3624b6 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 122 | // Initialize the mapping of values to registers. This is only set up for |
| 123 | // instruction values that are used outside of the block that defines |
| 124 | // them. |
Reid Kleckner | 0e7c84c | 2016-12-30 00:21:38 +0000 | [diff] [blame] | 125 | for (const BasicBlock &BB : *Fn) { |
| 126 | for (const Instruction &I : BB) { |
| 127 | if (const AllocaInst *AI = dyn_cast<AllocaInst>(&I)) { |
Jonas Paulsson | f12b925 | 2015-11-28 11:02:32 +0000 | [diff] [blame] | 128 | Type *Ty = AI->getAllocatedType(); |
| 129 | unsigned Align = |
| 130 | std::max((unsigned)MF->getDataLayout().getPrefTypeAlignment(Ty), |
| 131 | AI->getAlignment()); |
Jonas Paulsson | f12b925 | 2015-11-28 11:02:32 +0000 | [diff] [blame] | 132 | |
| 133 | // Static allocas can be folded into the initial stack frame |
| 134 | // adjustment. For targets that don't realign the stack, don't |
| 135 | // do this if there is an extra alignment requirement. |
Reid Kleckner | 0e7c84c | 2016-12-30 00:21:38 +0000 | [diff] [blame] | 136 | if (AI->isStaticAlloca() && |
Jonas Paulsson | f12b925 | 2015-11-28 11:02:32 +0000 | [diff] [blame] | 137 | (TFI->isStackRealignable() || (Align <= StackAlign))) { |
Reid Kleckner | 0b2bccc | 2014-09-02 18:42:44 +0000 | [diff] [blame] | 138 | const ConstantInt *CUI = cast<ConstantInt>(AI->getArraySize()); |
Mehdi Amini | 8ac7a9d | 2015-07-07 19:07:19 +0000 | [diff] [blame] | 139 | uint64_t TySize = MF->getDataLayout().getTypeAllocSize(Ty); |
Reid Kleckner | 0b2bccc | 2014-09-02 18:42:44 +0000 | [diff] [blame] | 140 | |
| 141 | TySize *= CUI->getZExtValue(); // Get total allocated size. |
| 142 | if (TySize == 0) TySize = 1; // Don't create zero-sized stack objects. |
David Majnemer | 1ef6540 | 2016-03-03 00:01:25 +0000 | [diff] [blame] | 143 | int FrameIndex = INT_MAX; |
| 144 | auto Iter = CatchObjects.find(AI); |
| 145 | if (Iter != CatchObjects.end() && TLI->needsFixedCatchObjects()) { |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 146 | FrameIndex = MF->getFrameInfo().CreateFixedObject( |
David Majnemer | 1ef6540 | 2016-03-03 00:01:25 +0000 | [diff] [blame] | 147 | TySize, 0, /*Immutable=*/false, /*isAliased=*/true); |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 148 | MF->getFrameInfo().setObjectAlignment(FrameIndex, Align); |
David Majnemer | 1ef6540 | 2016-03-03 00:01:25 +0000 | [diff] [blame] | 149 | } else { |
| 150 | FrameIndex = |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 151 | MF->getFrameInfo().CreateStackObject(TySize, Align, false, AI); |
David Majnemer | 1ef6540 | 2016-03-03 00:01:25 +0000 | [diff] [blame] | 152 | } |
Reid Kleckner | 0b2bccc | 2014-09-02 18:42:44 +0000 | [diff] [blame] | 153 | |
David Majnemer | 1ef6540 | 2016-03-03 00:01:25 +0000 | [diff] [blame] | 154 | StaticAllocaMap[AI] = FrameIndex; |
| 155 | // Update the catch handler information. |
Reid Kleckner | f7ad534 | 2016-10-19 17:08:23 +0000 | [diff] [blame] | 156 | if (Iter != CatchObjects.end()) { |
| 157 | for (int *CatchObjPtr : Iter->second) |
| 158 | *CatchObjPtr = FrameIndex; |
| 159 | } |
Reid Kleckner | 0b2bccc | 2014-09-02 18:42:44 +0000 | [diff] [blame] | 160 | } else { |
Jonas Paulsson | f12b925 | 2015-11-28 11:02:32 +0000 | [diff] [blame] | 161 | // FIXME: Overaligned static allocas should be grouped into |
| 162 | // a single dynamic allocation instead of using a separate |
| 163 | // stack allocation for each one. |
Hans Wennborg | acb842d | 2014-03-05 02:43:26 +0000 | [diff] [blame] | 164 | if (Align <= StackAlign) |
| 165 | Align = 0; |
| 166 | // Inform the Frame Information that we have variable-sized objects. |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 167 | MF->getFrameInfo().CreateVariableSizedObject(Align ? Align : 1, AI); |
Hans Wennborg | acb842d | 2014-03-05 02:43:26 +0000 | [diff] [blame] | 168 | } |
| 169 | } |
| 170 | |
| 171 | // Look for inline asm that clobbers the SP register. |
| 172 | if (isa<CallInst>(I) || isa<InvokeInst>(I)) { |
Reid Kleckner | 0e7c84c | 2016-12-30 00:21:38 +0000 | [diff] [blame] | 173 | ImmutableCallSite CS(&I); |
Hans Wennborg | 0c72fd2 | 2014-03-05 03:21:23 +0000 | [diff] [blame] | 174 | if (isa<InlineAsm>(CS.getCalledValue())) { |
Hans Wennborg | acb842d | 2014-03-05 02:43:26 +0000 | [diff] [blame] | 175 | unsigned SP = TLI->getStackPointerRegisterToSaveRestore(); |
Eric Christopher | 11e4df7 | 2015-02-26 22:38:43 +0000 | [diff] [blame] | 176 | const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo(); |
Hans Wennborg | acb842d | 2014-03-05 02:43:26 +0000 | [diff] [blame] | 177 | std::vector<TargetLowering::AsmOperandInfo> Ops = |
Mehdi Amini | 8ac7a9d | 2015-07-07 19:07:19 +0000 | [diff] [blame] | 178 | TLI->ParseConstraints(Fn->getParent()->getDataLayout(), TRI, CS); |
Reid Kleckner | 0e7c84c | 2016-12-30 00:21:38 +0000 | [diff] [blame] | 179 | for (TargetLowering::AsmOperandInfo &Op : Ops) { |
Hans Wennborg | acb842d | 2014-03-05 02:43:26 +0000 | [diff] [blame] | 180 | if (Op.Type == InlineAsm::isClobber) { |
| 181 | // Clobbers don't have SDValue operands, hence SDValue(). |
| 182 | TLI->ComputeConstraintToUse(Op, SDValue(), DAG); |
Eric Christopher | 2ae2de7 | 2014-10-09 00:57:31 +0000 | [diff] [blame] | 183 | std::pair<unsigned, const TargetRegisterClass *> PhysReg = |
Eric Christopher | 11e4df7 | 2015-02-26 22:38:43 +0000 | [diff] [blame] | 184 | TLI->getRegForInlineAsmConstraint(TRI, Op.ConstraintCode, |
| 185 | Op.ConstraintVT); |
Hans Wennborg | acb842d | 2014-03-05 02:43:26 +0000 | [diff] [blame] | 186 | if (PhysReg.first == SP) |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 187 | MF->getFrameInfo().setHasOpaqueSPAdjustment(true); |
Hans Wennborg | acb842d | 2014-03-05 02:43:26 +0000 | [diff] [blame] | 188 | } |
| 189 | } |
| 190 | } |
| 191 | } |
| 192 | |
Reid Kleckner | 2d9bb65 | 2014-08-22 21:59:26 +0000 | [diff] [blame] | 193 | // Look for calls to the @llvm.va_start intrinsic. We can omit some |
| 194 | // prologue boilerplate for variadic functions that don't examine their |
| 195 | // arguments. |
Reid Kleckner | 0e7c84c | 2016-12-30 00:21:38 +0000 | [diff] [blame] | 196 | if (const auto *II = dyn_cast<IntrinsicInst>(&I)) { |
Reid Kleckner | 2d9bb65 | 2014-08-22 21:59:26 +0000 | [diff] [blame] | 197 | if (II->getIntrinsicID() == Intrinsic::vastart) |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 198 | MF->getFrameInfo().setHasVAStart(true); |
Reid Kleckner | 2d9bb65 | 2014-08-22 21:59:26 +0000 | [diff] [blame] | 199 | } |
| 200 | |
Eric Christopher | bfba572 | 2015-12-16 23:10:53 +0000 | [diff] [blame] | 201 | // 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] | 202 | // forward implicit register parameters. |
Reid Kleckner | 0e7c84c | 2016-12-30 00:21:38 +0000 | [diff] [blame] | 203 | if (const auto *CI = dyn_cast<CallInst>(&I)) { |
Reid Kleckner | 16e5541 | 2014-08-29 21:42:08 +0000 | [diff] [blame] | 204 | if (CI->isMustTailCall() && Fn->isVarArg()) |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 205 | MF->getFrameInfo().setHasMustTailInVarArgFunc(true); |
Reid Kleckner | 16e5541 | 2014-08-29 21:42:08 +0000 | [diff] [blame] | 206 | } |
| 207 | |
Dan Gohman | 1e936277 | 2010-07-16 17:54:27 +0000 | [diff] [blame] | 208 | // Mark values used outside their block as exported, by allocating |
| 209 | // a virtual register for them. |
Reid Kleckner | 0e7c84c | 2016-12-30 00:21:38 +0000 | [diff] [blame] | 210 | if (isUsedOutsideOfDefiningBlock(&I)) |
| 211 | if (!isa<AllocaInst>(I) || !StaticAllocaMap.count(cast<AllocaInst>(&I))) |
| 212 | InitializeRegForValue(&I); |
Dan Gohman | a3624b6 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 213 | |
Jiangning Liu | ffbc690 | 2014-09-19 05:30:35 +0000 | [diff] [blame] | 214 | // Decide the preferred extend type for a value. |
Reid Kleckner | 0e7c84c | 2016-12-30 00:21:38 +0000 | [diff] [blame] | 215 | PreferredExtendType[&I] = getPreferredExtendForValue(&I); |
Dan Gohman | 1e936277 | 2010-07-16 17:54:27 +0000 | [diff] [blame] | 216 | } |
Reid Kleckner | 0e7c84c | 2016-12-30 00:21:38 +0000 | [diff] [blame] | 217 | } |
Dan Gohman | 1e936277 | 2010-07-16 17:54:27 +0000 | [diff] [blame] | 218 | |
Dan Gohman | a3624b6 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 219 | // Create an initial MachineBasicBlock for each LLVM BasicBlock in F. This |
| 220 | // also creates the initial PHI MachineInstrs, though none of the input |
| 221 | // operands are populated. |
Reid Kleckner | 0e7c84c | 2016-12-30 00:21:38 +0000 | [diff] [blame] | 222 | for (const BasicBlock &BB : *Fn) { |
Reid Kleckner | 51189f0a | 2015-09-08 23:28:38 +0000 | [diff] [blame] | 223 | // Don't create MachineBasicBlocks for imaginary EH pad blocks. These blocks |
| 224 | // are really data, and no instructions can live here. |
Reid Kleckner | 0e7c84c | 2016-12-30 00:21:38 +0000 | [diff] [blame] | 225 | if (BB.isEHPad()) { |
| 226 | const Instruction *PadInst = BB.getFirstNonPHI(); |
Reid Kleckner | 6ddae31 | 2015-11-05 21:09:49 +0000 | [diff] [blame] | 227 | // 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] | 228 | // funclets. |
Reid Kleckner | 6ddae31 | 2015-11-05 21:09:49 +0000 | [diff] [blame] | 229 | // FIXME: SEH catchpads do not create funclets, so we could avoid setting |
| 230 | // this in such cases in order to improve frame layout. |
Reid Kleckner | 0e7c84c | 2016-12-30 00:21:38 +0000 | [diff] [blame] | 231 | if (!isa<LandingPadInst>(PadInst)) { |
Matthias Braun | d0ee66c | 2016-12-01 19:32:15 +0000 | [diff] [blame] | 232 | MF->setHasEHFunclets(true); |
Matthias Braun | 941a705 | 2016-07-28 18:40:00 +0000 | [diff] [blame] | 233 | MF->getFrameInfo().setHasOpaqueSPAdjustment(true); |
Reid Kleckner | 6ddae31 | 2015-11-05 21:09:49 +0000 | [diff] [blame] | 234 | } |
Reid Kleckner | 0e7c84c | 2016-12-30 00:21:38 +0000 | [diff] [blame] | 235 | if (isa<CatchSwitchInst>(PadInst)) { |
| 236 | assert(&*BB.begin() == PadInst && |
Reid Kleckner | 51189f0a | 2015-09-08 23:28:38 +0000 | [diff] [blame] | 237 | "WinEHPrepare failed to remove PHIs from imaginary BBs"); |
| 238 | continue; |
Reid Kleckner | 51189f0a | 2015-09-08 23:28:38 +0000 | [diff] [blame] | 239 | } |
Reid Kleckner | 0e7c84c | 2016-12-30 00:21:38 +0000 | [diff] [blame] | 240 | if (isa<FuncletPadInst>(PadInst)) |
| 241 | assert(&*BB.begin() == PadInst && "WinEHPrepare failed to demote PHIs"); |
Reid Kleckner | 51189f0a | 2015-09-08 23:28:38 +0000 | [diff] [blame] | 242 | } |
| 243 | |
Reid Kleckner | 0e7c84c | 2016-12-30 00:21:38 +0000 | [diff] [blame] | 244 | MachineBasicBlock *MBB = mf.CreateMachineBasicBlock(&BB); |
| 245 | MBBMap[&BB] = MBB; |
Dan Gohman | a3624b6 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 246 | MF->push_back(MBB); |
| 247 | |
| 248 | // Transfer the address-taken flag. This is necessary because there could |
| 249 | // be multiple MachineBasicBlocks corresponding to one BasicBlock, and only |
| 250 | // the first one should be marked. |
Reid Kleckner | 0e7c84c | 2016-12-30 00:21:38 +0000 | [diff] [blame] | 251 | if (BB.hasAddressTaken()) |
Dan Gohman | a3624b6 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 252 | MBB->setHasAddressTaken(); |
| 253 | |
Reid Kleckner | 0e7c84c | 2016-12-30 00:21:38 +0000 | [diff] [blame] | 254 | // Mark landing pad blocks. |
| 255 | if (BB.isEHPad()) |
| 256 | MBB->setIsEHPad(); |
| 257 | |
Dan Gohman | a3624b6 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 258 | // Create Machine PHI nodes for LLVM PHI nodes, lowering them as |
| 259 | // appropriate. |
Benjamin Kramer | c7fc81e | 2017-12-30 15:27:33 +0000 | [diff] [blame] | 260 | for (const PHINode &PN : BB.phis()) { |
| 261 | if (PN.use_empty()) |
Rafael Espindola | e53b7d1 | 2011-05-13 15:18:06 +0000 | [diff] [blame] | 262 | continue; |
| 263 | |
Benjamin Kramer | c7fc81e | 2017-12-30 15:27:33 +0000 | [diff] [blame] | 264 | // Skip empty types |
| 265 | if (PN.getType()->isEmptyTy()) |
| 266 | continue; |
| 267 | |
| 268 | DebugLoc DL = PN.getDebugLoc(); |
| 269 | unsigned PHIReg = ValueMap[&PN]; |
Dan Gohman | a3624b6 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 270 | assert(PHIReg && "PHI node does not have an assigned virtual register!"); |
| 271 | |
| 272 | SmallVector<EVT, 4> ValueVTs; |
Benjamin Kramer | c7fc81e | 2017-12-30 15:27:33 +0000 | [diff] [blame] | 273 | ComputeValueVTs(*TLI, MF->getDataLayout(), PN.getType(), ValueVTs); |
Reid Kleckner | 0e7c84c | 2016-12-30 00:21:38 +0000 | [diff] [blame] | 274 | for (EVT VT : ValueVTs) { |
Bill Wendling | 8db01cb | 2013-06-06 00:11:39 +0000 | [diff] [blame] | 275 | unsigned NumRegisters = TLI->getNumRegisters(Fn->getContext(), VT); |
Eric Christopher | fc6de42 | 2014-08-05 02:39:49 +0000 | [diff] [blame] | 276 | const TargetInstrInfo *TII = MF->getSubtarget().getInstrInfo(); |
Dan Gohman | a3624b6 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 277 | for (unsigned i = 0; i != NumRegisters; ++i) |
Chris Lattner | b06015a | 2010-02-09 19:54:29 +0000 | [diff] [blame] | 278 | BuildMI(MBB, DL, TII->get(TargetOpcode::PHI), PHIReg + i); |
Dan Gohman | a3624b6 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 279 | PHIReg += NumRegisters; |
| 280 | } |
| 281 | } |
| 282 | } |
Dan Gohman | 69e8e32 | 2010-04-14 16:32:56 +0000 | [diff] [blame] | 283 | |
Joseph Tremoulet | 2afea54 | 2015-10-06 20:28:16 +0000 | [diff] [blame] | 284 | if (!isFuncletEHPersonality(Personality)) |
Reid Kleckner | cfbfe6f | 2015-04-24 20:25:05 +0000 | [diff] [blame] | 285 | return; |
| 286 | |
David Majnemer | 0e90f46 | 2016-01-07 04:31:35 +0000 | [diff] [blame] | 287 | WinEHFuncInfo &EHInfo = *MF->getWinEHFuncInfo(); |
Reid Kleckner | 7878391 | 2015-09-10 00:25:23 +0000 | [diff] [blame] | 288 | |
| 289 | // Map all BB references in the WinEH data to MBBs. |
Reid Kleckner | b005d28 | 2015-09-16 20:16:27 +0000 | [diff] [blame] | 290 | for (WinEHTryBlockMapEntry &TBME : EHInfo.TryBlockMap) { |
| 291 | for (WinEHHandlerType &H : TBME.HandlerArray) { |
David Majnemer | bfa5b98 | 2015-10-10 00:04:29 +0000 | [diff] [blame] | 292 | if (H.Handler) |
| 293 | H.Handler = MBBMap[H.Handler.get<const BasicBlock *>()]; |
Reid Kleckner | b005d28 | 2015-09-16 20:16:27 +0000 | [diff] [blame] | 294 | } |
| 295 | } |
Reid Kleckner | 14e7735 | 2015-10-09 23:34:53 +0000 | [diff] [blame] | 296 | for (CxxUnwindMapEntry &UME : EHInfo.CxxUnwindMap) |
Reid Kleckner | 7878391 | 2015-09-10 00:25:23 +0000 | [diff] [blame] | 297 | if (UME.Cleanup) |
David Majnemer | bfa5b98 | 2015-10-10 00:04:29 +0000 | [diff] [blame] | 298 | UME.Cleanup = MBBMap[UME.Cleanup.get<const BasicBlock *>()]; |
Reid Kleckner | 7878391 | 2015-09-10 00:25:23 +0000 | [diff] [blame] | 299 | for (SEHUnwindMapEntry &UME : EHInfo.SEHUnwindMap) { |
| 300 | const BasicBlock *BB = UME.Handler.get<const BasicBlock *>(); |
| 301 | UME.Handler = MBBMap[BB]; |
Reid Kleckner | 94b704c | 2015-09-09 21:10:03 +0000 | [diff] [blame] | 302 | } |
Joseph Tremoulet | 7f8c116 | 2015-10-06 20:30:33 +0000 | [diff] [blame] | 303 | for (ClrEHUnwindMapEntry &CME : EHInfo.ClrEHUnwindMap) { |
| 304 | const BasicBlock *BB = CME.Handler.get<const BasicBlock *>(); |
| 305 | CME.Handler = MBBMap[BB]; |
| 306 | } |
David Majnemer | cde3303 | 2015-03-30 22:58:10 +0000 | [diff] [blame] | 307 | } |
| 308 | |
Dan Gohman | a3624b6 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 309 | /// clear - Clear out all the function-specific state. This returns this |
| 310 | /// FunctionLoweringInfo to an empty state, ready to be used for a |
| 311 | /// different function. |
| 312 | void FunctionLoweringInfo::clear() { |
| 313 | MBBMap.clear(); |
| 314 | ValueMap.clear(); |
| 315 | StaticAllocaMap.clear(); |
Dan Gohman | a3624b6 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 316 | LiveOutRegInfo.clear(); |
Cameron Zwarich | 988faf9 | 2011-02-24 10:00:13 +0000 | [diff] [blame] | 317 | VisitedBBs.clear(); |
Evan Cheng | 6e82245 | 2010-04-28 23:08:54 +0000 | [diff] [blame] | 318 | ArgDbgValues.clear(); |
Devang Patel | 86ec8b3 | 2010-08-31 22:22:42 +0000 | [diff] [blame] | 319 | ByValArgFrameIndexMap.clear(); |
Dan Gohman | d7b5ce3 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 320 | RegFixups.clear(); |
Reid Kleckner | 3a7a2e4 | 2018-03-14 21:54:21 +0000 | [diff] [blame] | 321 | RegsWithFixups.clear(); |
Philip Reames | 1a1bdb2 | 2014-12-02 18:50:36 +0000 | [diff] [blame] | 322 | StatepointStackSlots.clear(); |
Sanjoy Das | c0c59fe | 2016-03-24 18:57:39 +0000 | [diff] [blame] | 323 | StatepointSpillMaps.clear(); |
Jiangning Liu | 3b09617 | 2014-09-24 03:22:56 +0000 | [diff] [blame] | 324 | PreferredExtendType.clear(); |
Dan Gohman | a3624b6 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 325 | } |
| 326 | |
Dan Gohman | 93f5920 | 2010-07-02 00:10:16 +0000 | [diff] [blame] | 327 | /// CreateReg - Allocate a single virtual register for the given type. |
Patrik Hagglund | 5e6c361 | 2012-12-13 06:34:11 +0000 | [diff] [blame] | 328 | unsigned FunctionLoweringInfo::CreateReg(MVT VT) { |
Eric Christopher | d913448 | 2014-08-04 21:25:23 +0000 | [diff] [blame] | 329 | return RegInfo->createVirtualRegister( |
Eric Christopher | 2ae2de7 | 2014-10-09 00:57:31 +0000 | [diff] [blame] | 330 | MF->getSubtarget().getTargetLowering()->getRegClassFor(VT)); |
Dan Gohman | a3624b6 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 331 | } |
| 332 | |
Dan Gohman | 93f5920 | 2010-07-02 00:10:16 +0000 | [diff] [blame] | 333 | /// CreateRegs - Allocate the appropriate number of virtual registers of |
Dan Gohman | a3624b6 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 334 | /// the correctly promoted or expanded types. Assign these registers |
| 335 | /// consecutive vreg numbers and return the first assigned number. |
| 336 | /// |
| 337 | /// In the case that the given value has struct or array type, this function |
| 338 | /// will assign registers for each member or element. |
| 339 | /// |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 340 | unsigned FunctionLoweringInfo::CreateRegs(Type *Ty) { |
Eric Christopher | 2ae2de7 | 2014-10-09 00:57:31 +0000 | [diff] [blame] | 341 | const TargetLowering *TLI = MF->getSubtarget().getTargetLowering(); |
Bill Wendling | 0ccf310 | 2013-06-19 20:32:16 +0000 | [diff] [blame] | 342 | |
Dan Gohman | a3624b6 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 343 | SmallVector<EVT, 4> ValueVTs; |
Mehdi Amini | 56228da | 2015-07-09 01:57:34 +0000 | [diff] [blame] | 344 | ComputeValueVTs(*TLI, MF->getDataLayout(), Ty, ValueVTs); |
Dan Gohman | a3624b6 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 345 | |
| 346 | unsigned FirstReg = 0; |
| 347 | for (unsigned Value = 0, e = ValueVTs.size(); Value != e; ++Value) { |
| 348 | EVT ValueVT = ValueVTs[Value]; |
Bill Wendling | 8db01cb | 2013-06-06 00:11:39 +0000 | [diff] [blame] | 349 | MVT RegisterVT = TLI->getRegisterType(Ty->getContext(), ValueVT); |
Dan Gohman | a3624b6 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 350 | |
Bill Wendling | 8db01cb | 2013-06-06 00:11:39 +0000 | [diff] [blame] | 351 | unsigned NumRegs = TLI->getNumRegisters(Ty->getContext(), ValueVT); |
Dan Gohman | a3624b6 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 352 | for (unsigned i = 0; i != NumRegs; ++i) { |
Dan Gohman | 93f5920 | 2010-07-02 00:10:16 +0000 | [diff] [blame] | 353 | unsigned R = CreateReg(RegisterVT); |
Dan Gohman | a3624b6 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 354 | if (!FirstReg) FirstReg = R; |
| 355 | } |
| 356 | } |
| 357 | return FirstReg; |
| 358 | } |
Dan Gohman | ad97b3d | 2009-11-23 17:42:46 +0000 | [diff] [blame] | 359 | |
Cameron Zwarich | a62fc89 | 2011-02-24 10:00:25 +0000 | [diff] [blame] | 360 | /// GetLiveOutRegInfo - Gets LiveOutInfo for a register, returning NULL if the |
| 361 | /// register is a PHI destination and the PHI's LiveOutInfo is not valid. If |
| 362 | /// the register's LiveOutInfo is for a smaller bit width, it is extended to |
| 363 | /// the larger bit width by zero extension. The bit width must be no smaller |
| 364 | /// than the LiveOutInfo's existing bit width. |
| 365 | const FunctionLoweringInfo::LiveOutInfo * |
| 366 | FunctionLoweringInfo::GetLiveOutRegInfo(unsigned Reg, unsigned BitWidth) { |
| 367 | if (!LiveOutRegInfo.inBounds(Reg)) |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 368 | return nullptr; |
Cameron Zwarich | a62fc89 | 2011-02-24 10:00:25 +0000 | [diff] [blame] | 369 | |
| 370 | LiveOutInfo *LOI = &LiveOutRegInfo[Reg]; |
| 371 | if (!LOI->IsValid) |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 372 | return nullptr; |
Cameron Zwarich | a62fc89 | 2011-02-24 10:00:25 +0000 | [diff] [blame] | 373 | |
Craig Topper | d0af7e8 | 2017-04-28 05:31:46 +0000 | [diff] [blame] | 374 | if (BitWidth > LOI->Known.getBitWidth()) { |
Cameron Zwarich | 4c82cd2 | 2011-02-25 01:11:01 +0000 | [diff] [blame] | 375 | LOI->NumSignBits = 1; |
Craig Topper | d938fd1 | 2017-05-03 22:07:25 +0000 | [diff] [blame] | 376 | LOI->Known = LOI->Known.zextOrTrunc(BitWidth); |
Cameron Zwarich | a62fc89 | 2011-02-24 10:00:25 +0000 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | return LOI; |
| 380 | } |
| 381 | |
| 382 | /// ComputePHILiveOutRegInfo - Compute LiveOutInfo for a PHI's destination |
| 383 | /// register based on the LiveOutInfo of its operands. |
| 384 | void FunctionLoweringInfo::ComputePHILiveOutRegInfo(const PHINode *PN) { |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 385 | Type *Ty = PN->getType(); |
Cameron Zwarich | a62fc89 | 2011-02-24 10:00:25 +0000 | [diff] [blame] | 386 | if (!Ty->isIntegerTy() || Ty->isVectorTy()) |
| 387 | return; |
| 388 | |
| 389 | SmallVector<EVT, 1> ValueVTs; |
Mehdi Amini | 56228da | 2015-07-09 01:57:34 +0000 | [diff] [blame] | 390 | ComputeValueVTs(*TLI, MF->getDataLayout(), Ty, ValueVTs); |
Cameron Zwarich | a62fc89 | 2011-02-24 10:00:25 +0000 | [diff] [blame] | 391 | assert(ValueVTs.size() == 1 && |
| 392 | "PHIs with non-vector integer types should have a single VT."); |
| 393 | EVT IntVT = ValueVTs[0]; |
| 394 | |
Bill Wendling | 8db01cb | 2013-06-06 00:11:39 +0000 | [diff] [blame] | 395 | if (TLI->getNumRegisters(PN->getContext(), IntVT) != 1) |
Cameron Zwarich | a62fc89 | 2011-02-24 10:00:25 +0000 | [diff] [blame] | 396 | return; |
Bill Wendling | 8db01cb | 2013-06-06 00:11:39 +0000 | [diff] [blame] | 397 | IntVT = TLI->getTypeToTransformTo(PN->getContext(), IntVT); |
Cameron Zwarich | a62fc89 | 2011-02-24 10:00:25 +0000 | [diff] [blame] | 398 | unsigned BitWidth = IntVT.getSizeInBits(); |
| 399 | |
| 400 | unsigned DestReg = ValueMap[PN]; |
| 401 | if (!TargetRegisterInfo::isVirtualRegister(DestReg)) |
| 402 | return; |
| 403 | LiveOutRegInfo.grow(DestReg); |
| 404 | LiveOutInfo &DestLOI = LiveOutRegInfo[DestReg]; |
| 405 | |
| 406 | Value *V = PN->getIncomingValue(0); |
| 407 | if (isa<UndefValue>(V) || isa<ConstantExpr>(V)) { |
| 408 | DestLOI.NumSignBits = 1; |
Craig Topper | d0af7e8 | 2017-04-28 05:31:46 +0000 | [diff] [blame] | 409 | DestLOI.Known = KnownBits(BitWidth); |
Cameron Zwarich | a62fc89 | 2011-02-24 10:00:25 +0000 | [diff] [blame] | 410 | return; |
| 411 | } |
| 412 | |
| 413 | if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) { |
| 414 | APInt Val = CI->getValue().zextOrTrunc(BitWidth); |
| 415 | DestLOI.NumSignBits = Val.getNumSignBits(); |
Craig Topper | d0af7e8 | 2017-04-28 05:31:46 +0000 | [diff] [blame] | 416 | DestLOI.Known.Zero = ~Val; |
| 417 | DestLOI.Known.One = Val; |
Cameron Zwarich | a62fc89 | 2011-02-24 10:00:25 +0000 | [diff] [blame] | 418 | } else { |
| 419 | assert(ValueMap.count(V) && "V should have been placed in ValueMap when its" |
| 420 | "CopyToReg node was created."); |
| 421 | unsigned SrcReg = ValueMap[V]; |
| 422 | if (!TargetRegisterInfo::isVirtualRegister(SrcReg)) { |
| 423 | DestLOI.IsValid = false; |
| 424 | return; |
| 425 | } |
| 426 | const LiveOutInfo *SrcLOI = GetLiveOutRegInfo(SrcReg, BitWidth); |
| 427 | if (!SrcLOI) { |
| 428 | DestLOI.IsValid = false; |
| 429 | return; |
| 430 | } |
| 431 | DestLOI = *SrcLOI; |
| 432 | } |
| 433 | |
Craig Topper | d0af7e8 | 2017-04-28 05:31:46 +0000 | [diff] [blame] | 434 | assert(DestLOI.Known.Zero.getBitWidth() == BitWidth && |
| 435 | DestLOI.Known.One.getBitWidth() == BitWidth && |
Cameron Zwarich | a62fc89 | 2011-02-24 10:00:25 +0000 | [diff] [blame] | 436 | "Masks should have the same bit width as the type."); |
| 437 | |
| 438 | for (unsigned i = 1, e = PN->getNumIncomingValues(); i != e; ++i) { |
| 439 | Value *V = PN->getIncomingValue(i); |
| 440 | if (isa<UndefValue>(V) || isa<ConstantExpr>(V)) { |
| 441 | DestLOI.NumSignBits = 1; |
Craig Topper | d0af7e8 | 2017-04-28 05:31:46 +0000 | [diff] [blame] | 442 | DestLOI.Known = KnownBits(BitWidth); |
Eric Christopher | 0713a9d | 2011-06-08 23:55:35 +0000 | [diff] [blame] | 443 | return; |
Cameron Zwarich | a62fc89 | 2011-02-24 10:00:25 +0000 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) { |
| 447 | APInt Val = CI->getValue().zextOrTrunc(BitWidth); |
| 448 | DestLOI.NumSignBits = std::min(DestLOI.NumSignBits, Val.getNumSignBits()); |
Craig Topper | d0af7e8 | 2017-04-28 05:31:46 +0000 | [diff] [blame] | 449 | DestLOI.Known.Zero &= ~Val; |
| 450 | DestLOI.Known.One &= Val; |
Cameron Zwarich | a62fc89 | 2011-02-24 10:00:25 +0000 | [diff] [blame] | 451 | continue; |
| 452 | } |
| 453 | |
| 454 | assert(ValueMap.count(V) && "V should have been placed in ValueMap when " |
| 455 | "its CopyToReg node was created."); |
| 456 | unsigned SrcReg = ValueMap[V]; |
| 457 | if (!TargetRegisterInfo::isVirtualRegister(SrcReg)) { |
| 458 | DestLOI.IsValid = false; |
| 459 | return; |
| 460 | } |
| 461 | const LiveOutInfo *SrcLOI = GetLiveOutRegInfo(SrcReg, BitWidth); |
| 462 | if (!SrcLOI) { |
| 463 | DestLOI.IsValid = false; |
| 464 | return; |
| 465 | } |
| 466 | DestLOI.NumSignBits = std::min(DestLOI.NumSignBits, SrcLOI->NumSignBits); |
Craig Topper | d0af7e8 | 2017-04-28 05:31:46 +0000 | [diff] [blame] | 467 | DestLOI.Known.Zero &= SrcLOI->Known.Zero; |
| 468 | DestLOI.Known.One &= SrcLOI->Known.One; |
Cameron Zwarich | a62fc89 | 2011-02-24 10:00:25 +0000 | [diff] [blame] | 469 | } |
| 470 | } |
| 471 | |
Devang Patel | 9d904e1 | 2011-09-08 22:59:09 +0000 | [diff] [blame] | 472 | /// setArgumentFrameIndex - Record frame index for the byval |
Devang Patel | 86ec8b3 | 2010-08-31 22:22:42 +0000 | [diff] [blame] | 473 | /// argument. This overrides previous frame index entry for this argument, |
| 474 | /// if any. |
Devang Patel | 9d904e1 | 2011-09-08 22:59:09 +0000 | [diff] [blame] | 475 | void FunctionLoweringInfo::setArgumentFrameIndex(const Argument *A, |
Eric Christopher | 219d51d | 2012-02-24 01:59:01 +0000 | [diff] [blame] | 476 | int FI) { |
Devang Patel | 86ec8b3 | 2010-08-31 22:22:42 +0000 | [diff] [blame] | 477 | ByValArgFrameIndexMap[A] = FI; |
| 478 | } |
Eric Christopher | 0713a9d | 2011-06-08 23:55:35 +0000 | [diff] [blame] | 479 | |
Devang Patel | 9d904e1 | 2011-09-08 22:59:09 +0000 | [diff] [blame] | 480 | /// getArgumentFrameIndex - Get frame index for the byval argument. |
Devang Patel | 86ec8b3 | 2010-08-31 22:22:42 +0000 | [diff] [blame] | 481 | /// If the argument does not have any assigned frame index then 0 is |
| 482 | /// returned. |
Devang Patel | 9d904e1 | 2011-09-08 22:59:09 +0000 | [diff] [blame] | 483 | int FunctionLoweringInfo::getArgumentFrameIndex(const Argument *A) { |
Reid Kleckner | 3a363ff | 2017-05-09 16:02:20 +0000 | [diff] [blame] | 484 | auto I = ByValArgFrameIndexMap.find(A); |
Devang Patel | 86ec8b3 | 2010-08-31 22:22:42 +0000 | [diff] [blame] | 485 | if (I != ByValArgFrameIndexMap.end()) |
| 486 | return I->second; |
Eric Christopher | 18c6be7 | 2012-02-23 03:39:43 +0000 | [diff] [blame] | 487 | DEBUG(dbgs() << "Argument does not have assigned frame index!\n"); |
Reid Kleckner | 3a363ff | 2017-05-09 16:02:20 +0000 | [diff] [blame] | 488 | return INT_MAX; |
Devang Patel | 86ec8b3 | 2010-08-31 22:22:42 +0000 | [diff] [blame] | 489 | } |
| 490 | |
Reid Kleckner | 72ba704 | 2015-10-07 00:27:33 +0000 | [diff] [blame] | 491 | unsigned FunctionLoweringInfo::getCatchPadExceptionPointerVReg( |
| 492 | const Value *CPI, const TargetRegisterClass *RC) { |
| 493 | MachineRegisterInfo &MRI = MF->getRegInfo(); |
| 494 | auto I = CatchPadExceptionPointers.insert({CPI, 0}); |
| 495 | unsigned &VReg = I.first->second; |
| 496 | if (I.second) |
| 497 | VReg = MRI.createVirtualRegister(RC); |
| 498 | assert(VReg && "null vreg in exception pointer table!"); |
| 499 | return VReg; |
| 500 | } |
| 501 | |
Arnold Schwaighofer | 3f25658 | 2016-10-07 22:06:55 +0000 | [diff] [blame] | 502 | unsigned |
| 503 | FunctionLoweringInfo::getOrCreateSwiftErrorVReg(const MachineBasicBlock *MBB, |
| 504 | const Value *Val) { |
| 505 | auto Key = std::make_pair(MBB, Val); |
| 506 | auto It = SwiftErrorVRegDefMap.find(Key); |
| 507 | // If this is the first use of this swifterror value in this basic block, |
| 508 | // create a new virtual register. |
| 509 | // After we processed all basic blocks we will satisfy this "upwards exposed |
| 510 | // use" by inserting a copy or phi at the beginning of this block. |
| 511 | if (It == SwiftErrorVRegDefMap.end()) { |
| 512 | auto &DL = MF->getDataLayout(); |
| 513 | const TargetRegisterClass *RC = TLI->getRegClassFor(TLI->getPointerTy(DL)); |
| 514 | auto VReg = MF->getRegInfo().createVirtualRegister(RC); |
| 515 | SwiftErrorVRegDefMap[Key] = VReg; |
| 516 | SwiftErrorVRegUpwardsUse[Key] = VReg; |
| 517 | return VReg; |
| 518 | } else return It->second; |
Manman Ren | e221a87 | 2016-04-05 18:13:16 +0000 | [diff] [blame] | 519 | } |
| 520 | |
Arnold Schwaighofer | 3f25658 | 2016-10-07 22:06:55 +0000 | [diff] [blame] | 521 | void FunctionLoweringInfo::setCurrentSwiftErrorVReg( |
| 522 | const MachineBasicBlock *MBB, const Value *Val, unsigned VReg) { |
| 523 | SwiftErrorVRegDefMap[std::make_pair(MBB, Val)] = VReg; |
Manman Ren | e221a87 | 2016-04-05 18:13:16 +0000 | [diff] [blame] | 524 | } |
Arnold Schwaighofer | ae9312c | 2017-06-15 17:34:42 +0000 | [diff] [blame] | 525 | |
| 526 | std::pair<unsigned, bool> |
| 527 | FunctionLoweringInfo::getOrCreateSwiftErrorVRegDefAt(const Instruction *I) { |
| 528 | auto Key = PointerIntPair<const Instruction *, 1, bool>(I, true); |
| 529 | auto It = SwiftErrorVRegDefUses.find(Key); |
| 530 | if (It == SwiftErrorVRegDefUses.end()) { |
| 531 | auto &DL = MF->getDataLayout(); |
| 532 | const TargetRegisterClass *RC = TLI->getRegClassFor(TLI->getPointerTy(DL)); |
| 533 | unsigned VReg = MF->getRegInfo().createVirtualRegister(RC); |
| 534 | SwiftErrorVRegDefUses[Key] = VReg; |
| 535 | return std::make_pair(VReg, true); |
| 536 | } |
| 537 | return std::make_pair(It->second, false); |
| 538 | } |
| 539 | |
| 540 | std::pair<unsigned, bool> |
| 541 | FunctionLoweringInfo::getOrCreateSwiftErrorVRegUseAt(const Instruction *I, const MachineBasicBlock *MBB, const Value *Val) { |
| 542 | auto Key = PointerIntPair<const Instruction *, 1, bool>(I, false); |
| 543 | auto It = SwiftErrorVRegDefUses.find(Key); |
| 544 | if (It == SwiftErrorVRegDefUses.end()) { |
| 545 | unsigned VReg = getOrCreateSwiftErrorVReg(MBB, Val); |
| 546 | SwiftErrorVRegDefUses[Key] = VReg; |
| 547 | return std::make_pair(VReg, true); |
| 548 | } |
| 549 | return std::make_pair(It->second, false); |
| 550 | } |
Alexander Timofeev | 2e5eece | 2018-03-05 15:12:21 +0000 | [diff] [blame] | 551 | |
| 552 | const Value * |
| 553 | FunctionLoweringInfo::getValueFromVirtualReg(unsigned Vreg) { |
| 554 | if (VirtReg2Value.empty()) { |
| 555 | for (auto &P : ValueMap) { |
| 556 | VirtReg2Value[P.second] = P.first; |
| 557 | } |
| 558 | } |
| 559 | return VirtReg2Value[Vreg]; |
| 560 | } |