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