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" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 23 | #include "llvm/IR/DataLayout.h" |
Chandler Carruth | 9a4c9e5 | 2014-03-06 00:46:21 +0000 | [diff] [blame] | 24 | #include "llvm/IR/DebugInfo.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 25 | #include "llvm/IR/DerivedTypes.h" |
| 26 | #include "llvm/IR/Function.h" |
| 27 | #include "llvm/IR/Instructions.h" |
| 28 | #include "llvm/IR/IntrinsicInst.h" |
| 29 | #include "llvm/IR/LLVMContext.h" |
| 30 | #include "llvm/IR/Module.h" |
Dan Gohman | a3624b6 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 31 | #include "llvm/Support/Debug.h" |
| 32 | #include "llvm/Support/ErrorHandling.h" |
| 33 | #include "llvm/Support/MathExtras.h" |
Hans Wennborg | acb842d | 2014-03-05 02:43:26 +0000 | [diff] [blame] | 34 | #include "llvm/Target/TargetFrameLowering.h" |
Chandler Carruth | 9205140 | 2014-03-05 10:30:38 +0000 | [diff] [blame] | 35 | #include "llvm/Target/TargetInstrInfo.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 36 | #include "llvm/Target/TargetLowering.h" |
| 37 | #include "llvm/Target/TargetOptions.h" |
| 38 | #include "llvm/Target/TargetRegisterInfo.h" |
Eric Christopher | d913448 | 2014-08-04 21:25:23 +0000 | [diff] [blame] | 39 | #include "llvm/Target/TargetSubtargetInfo.h" |
Dan Gohman | a3624b6 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 40 | #include <algorithm> |
| 41 | using namespace llvm; |
| 42 | |
Chandler Carruth | 1b9dde0 | 2014-04-22 02:02:50 +0000 | [diff] [blame] | 43 | #define DEBUG_TYPE "function-lowering-info" |
| 44 | |
Dan Gohman | a3624b6 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 45 | /// isUsedOutsideOfDefiningBlock - Return true if this instruction is used by |
| 46 | /// PHI nodes or outside of the basic block that defines it, or used by a |
| 47 | /// switch or atomic instruction, which may expand to multiple basic blocks. |
Dan Gohman | 913c998 | 2010-04-15 04:33:49 +0000 | [diff] [blame] | 48 | static bool isUsedOutsideOfDefiningBlock(const Instruction *I) { |
Dan Gohman | 7c845e4 | 2010-04-20 14:50:13 +0000 | [diff] [blame] | 49 | if (I->use_empty()) return false; |
Dan Gohman | a3624b6 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 50 | if (isa<PHINode>(I)) return true; |
Dan Gohman | 913c998 | 2010-04-15 04:33:49 +0000 | [diff] [blame] | 51 | const BasicBlock *BB = I->getParent(); |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 52 | for (const User *U : I->users()) |
Gabor Greif | 52617fc | 2010-07-09 16:08:33 +0000 | [diff] [blame] | 53 | if (cast<Instruction>(U)->getParent() != BB || isa<PHINode>(U)) |
Dan Gohman | a3624b6 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 54 | return true; |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 55 | |
Dan Gohman | a3624b6 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 56 | return false; |
| 57 | } |
| 58 | |
Hans Wennborg | acb842d | 2014-03-05 02:43:26 +0000 | [diff] [blame] | 59 | void FunctionLoweringInfo::set(const Function &fn, MachineFunction &mf, |
| 60 | SelectionDAG *DAG) { |
Eric Christopher | d913448 | 2014-08-04 21:25:23 +0000 | [diff] [blame] | 61 | const TargetLowering *TLI = TM.getSubtargetImpl()->getTargetLowering(); |
Bill Wendling | 0ccf310 | 2013-06-19 20:32:16 +0000 | [diff] [blame] | 62 | |
Dan Gohman | a3624b6 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 63 | Fn = &fn; |
| 64 | MF = &mf; |
| 65 | RegInfo = &MF->getRegInfo(); |
| 66 | |
Dan Gohman | d7b5ce3 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 67 | // Check whether the function can return without sret-demotion. |
| 68 | SmallVector<ISD::OutputArg, 4> Outs; |
Bill Wendling | 8db01cb | 2013-06-06 00:11:39 +0000 | [diff] [blame] | 69 | GetReturnInfo(Fn->getReturnType(), Fn->getAttributes(), Outs, *TLI); |
| 70 | CanLowerReturn = TLI->CanLowerReturn(Fn->getCallingConv(), *MF, |
| 71 | Fn->isVarArg(), |
| 72 | Outs, Fn->getContext()); |
Dan Gohman | d7b5ce3 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 73 | |
Dan Gohman | a3624b6 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 74 | // Initialize the mapping of values to registers. This is only set up for |
| 75 | // instruction values that are used outside of the block that defines |
| 76 | // them. |
Dan Gohman | 913c998 | 2010-04-15 04:33:49 +0000 | [diff] [blame] | 77 | Function::const_iterator BB = Fn->begin(), EB = Fn->end(); |
| 78 | for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I) |
Reid Kleckner | dfbed59 | 2014-01-31 23:45:12 +0000 | [diff] [blame] | 79 | if (const AllocaInst *AI = dyn_cast<AllocaInst>(I)) { |
| 80 | // Don't fold inalloca allocas or other dynamic allocas into the initial |
| 81 | // stack frame allocation, even if they are in the entry block. |
| 82 | if (!AI->isStaticAlloca()) |
| 83 | continue; |
| 84 | |
Dan Gohman | 913c998 | 2010-04-15 04:33:49 +0000 | [diff] [blame] | 85 | if (const ConstantInt *CUI = dyn_cast<ConstantInt>(AI->getArraySize())) { |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 86 | Type *Ty = AI->getAllocatedType(); |
Bill Wendling | 8db01cb | 2013-06-06 00:11:39 +0000 | [diff] [blame] | 87 | uint64_t TySize = TLI->getDataLayout()->getTypeAllocSize(Ty); |
Dan Gohman | a3624b6 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 88 | unsigned Align = |
Bill Wendling | 8db01cb | 2013-06-06 00:11:39 +0000 | [diff] [blame] | 89 | std::max((unsigned)TLI->getDataLayout()->getPrefTypeAlignment(Ty), |
Dan Gohman | a3624b6 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 90 | AI->getAlignment()); |
| 91 | |
| 92 | TySize *= CUI->getZExtValue(); // Get total allocated size. |
| 93 | if (TySize == 0) TySize = 1; // Don't create zero-sized stack objects. |
Bill Wendling | 0ff1ef6 | 2010-07-27 01:55:19 +0000 | [diff] [blame] | 94 | |
Dan Gohman | a3624b6 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 95 | StaticAllocaMap[AI] = |
Josh Magee | 22b8ba2 | 2013-12-19 03:17:11 +0000 | [diff] [blame] | 96 | MF->getFrameInfo()->CreateStackObject(TySize, Align, false, AI); |
Dan Gohman | a3624b6 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 97 | } |
Reid Kleckner | dfbed59 | 2014-01-31 23:45:12 +0000 | [diff] [blame] | 98 | } |
Dan Gohman | a3624b6 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 99 | |
| 100 | for (; BB != EB; ++BB) |
Eric Christopher | 219d51d | 2012-02-24 01:59:01 +0000 | [diff] [blame] | 101 | for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); |
| 102 | I != E; ++I) { |
Hans Wennborg | acb842d | 2014-03-05 02:43:26 +0000 | [diff] [blame] | 103 | // Look for dynamic allocas. |
| 104 | if (const AllocaInst *AI = dyn_cast<AllocaInst>(I)) { |
| 105 | if (!AI->isStaticAlloca()) { |
| 106 | unsigned Align = std::max( |
| 107 | (unsigned)TLI->getDataLayout()->getPrefTypeAlignment( |
| 108 | AI->getAllocatedType()), |
| 109 | AI->getAlignment()); |
Eric Christopher | d913448 | 2014-08-04 21:25:23 +0000 | [diff] [blame] | 110 | unsigned StackAlign = |
| 111 | TM.getSubtargetImpl()->getFrameLowering()->getStackAlignment(); |
Hans Wennborg | acb842d | 2014-03-05 02:43:26 +0000 | [diff] [blame] | 112 | if (Align <= StackAlign) |
| 113 | Align = 0; |
| 114 | // Inform the Frame Information that we have variable-sized objects. |
| 115 | MF->getFrameInfo()->CreateVariableSizedObject(Align ? Align : 1, AI); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | // Look for inline asm that clobbers the SP register. |
| 120 | if (isa<CallInst>(I) || isa<InvokeInst>(I)) { |
| 121 | ImmutableCallSite CS(I); |
Hans Wennborg | 0c72fd2 | 2014-03-05 03:21:23 +0000 | [diff] [blame] | 122 | if (isa<InlineAsm>(CS.getCalledValue())) { |
Hans Wennborg | acb842d | 2014-03-05 02:43:26 +0000 | [diff] [blame] | 123 | unsigned SP = TLI->getStackPointerRegisterToSaveRestore(); |
| 124 | std::vector<TargetLowering::AsmOperandInfo> Ops = |
| 125 | TLI->ParseConstraints(CS); |
| 126 | for (size_t I = 0, E = Ops.size(); I != E; ++I) { |
| 127 | TargetLowering::AsmOperandInfo &Op = Ops[I]; |
| 128 | if (Op.Type == InlineAsm::isClobber) { |
| 129 | // Clobbers don't have SDValue operands, hence SDValue(). |
| 130 | TLI->ComputeConstraintToUse(Op, SDValue(), DAG); |
| 131 | std::pair<unsigned, const TargetRegisterClass*> PhysReg = |
| 132 | TLI->getRegForInlineAsmConstraint(Op.ConstraintCode, |
| 133 | Op.ConstraintVT); |
| 134 | if (PhysReg.first == SP) |
| 135 | MF->getFrameInfo()->setHasInlineAsmWithSPAdjust(true); |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | |
Reid Kleckner | 2d9bb65 | 2014-08-22 21:59:26 +0000 | [diff] [blame] | 141 | // Look for calls to the @llvm.va_start intrinsic. We can omit some |
| 142 | // prologue boilerplate for variadic functions that don't examine their |
| 143 | // arguments. |
| 144 | if (const auto *II = dyn_cast<IntrinsicInst>(I)) { |
| 145 | if (II->getIntrinsicID() == Intrinsic::vastart) |
| 146 | MF->getFrameInfo()->setHasVAStart(true); |
| 147 | } |
| 148 | |
Reid Kleckner | 16e5541 | 2014-08-29 21:42:08 +0000 | [diff] [blame^] | 149 | // If we have a musttail call in a variadic funciton, we need to ensure we |
| 150 | // forward implicit register parameters. |
| 151 | if (auto *CI = dyn_cast<CallInst>(I)) { |
| 152 | if (CI->isMustTailCall() && Fn->isVarArg()) |
| 153 | MF->getFrameInfo()->setHasMustTailInVarArgFunc(true); |
| 154 | } |
| 155 | |
Dan Gohman | 1e936277 | 2010-07-16 17:54:27 +0000 | [diff] [blame] | 156 | // Mark values used outside their block as exported, by allocating |
| 157 | // a virtual register for them. |
Cameron Zwarich | f8b22b3 | 2011-02-22 03:24:52 +0000 | [diff] [blame] | 158 | if (isUsedOutsideOfDefiningBlock(I)) |
Dan Gohman | a3624b6 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 159 | if (!isa<AllocaInst>(I) || |
| 160 | !StaticAllocaMap.count(cast<AllocaInst>(I))) |
| 161 | InitializeRegForValue(I); |
| 162 | |
Dan Gohman | 1e936277 | 2010-07-16 17:54:27 +0000 | [diff] [blame] | 163 | // Collect llvm.dbg.declare information. This is done now instead of |
| 164 | // during the initial isel pass through the IR so that it is done |
| 165 | // in a predictable order. |
| 166 | if (const DbgDeclareInst *DI = dyn_cast<DbgDeclareInst>(I)) { |
| 167 | MachineModuleInfo &MMI = MF->getMMI(); |
Manman Ren | 983a16c | 2013-06-28 05:43:10 +0000 | [diff] [blame] | 168 | DIVariable DIVar(DI->getVariable()); |
| 169 | assert((!DIVar || DIVar.isVariable()) && |
| 170 | "Variable in DbgDeclareInst should be either null or a DIVariable."); |
Dan Gohman | 1e936277 | 2010-07-16 17:54:27 +0000 | [diff] [blame] | 171 | if (MMI.hasDebugInfo() && |
Manman Ren | 983a16c | 2013-06-28 05:43:10 +0000 | [diff] [blame] | 172 | DIVar && |
Dan Gohman | 1e936277 | 2010-07-16 17:54:27 +0000 | [diff] [blame] | 173 | !DI->getDebugLoc().isUnknown()) { |
| 174 | // Don't handle byval struct arguments or VLAs, for example. |
| 175 | // Non-byval arguments are handled here (they refer to the stack |
| 176 | // temporary alloca at this point). |
| 177 | const Value *Address = DI->getAddress(); |
| 178 | if (Address) { |
| 179 | if (const BitCastInst *BCI = dyn_cast<BitCastInst>(Address)) |
| 180 | Address = BCI->getOperand(0); |
| 181 | if (const AllocaInst *AI = dyn_cast<AllocaInst>(Address)) { |
| 182 | DenseMap<const AllocaInst *, int>::iterator SI = |
| 183 | StaticAllocaMap.find(AI); |
| 184 | if (SI != StaticAllocaMap.end()) { // Check for VLAs. |
| 185 | int FI = SI->second; |
| 186 | MMI.setVariableDbgInfo(DI->getVariable(), |
| 187 | FI, DI->getDebugLoc()); |
| 188 | } |
| 189 | } |
| 190 | } |
| 191 | } |
| 192 | } |
| 193 | } |
| 194 | |
Dan Gohman | a3624b6 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 195 | // Create an initial MachineBasicBlock for each LLVM BasicBlock in F. This |
| 196 | // also creates the initial PHI MachineInstrs, though none of the input |
| 197 | // operands are populated. |
Dan Gohman | f57117d | 2010-04-14 16:30:40 +0000 | [diff] [blame] | 198 | for (BB = Fn->begin(); BB != EB; ++BB) { |
Dan Gohman | a3624b6 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 199 | MachineBasicBlock *MBB = mf.CreateMachineBasicBlock(BB); |
| 200 | MBBMap[BB] = MBB; |
| 201 | MF->push_back(MBB); |
| 202 | |
| 203 | // Transfer the address-taken flag. This is necessary because there could |
| 204 | // be multiple MachineBasicBlocks corresponding to one BasicBlock, and only |
| 205 | // the first one should be marked. |
| 206 | if (BB->hasAddressTaken()) |
| 207 | MBB->setHasAddressTaken(); |
| 208 | |
| 209 | // Create Machine PHI nodes for LLVM PHI nodes, lowering them as |
| 210 | // appropriate. |
Dan Gohman | 0f055d3 | 2010-04-20 14:46:25 +0000 | [diff] [blame] | 211 | for (BasicBlock::const_iterator I = BB->begin(); |
| 212 | const PHINode *PN = dyn_cast<PHINode>(I); ++I) { |
| 213 | if (PN->use_empty()) continue; |
Dan Gohman | a3624b6 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 214 | |
Rafael Espindola | e53b7d1 | 2011-05-13 15:18:06 +0000 | [diff] [blame] | 215 | // Skip empty types |
| 216 | if (PN->getType()->isEmptyTy()) |
| 217 | continue; |
| 218 | |
Dan Gohman | 7b7f088 | 2010-04-20 14:48:02 +0000 | [diff] [blame] | 219 | DebugLoc DL = PN->getDebugLoc(); |
Dan Gohman | a3624b6 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 220 | unsigned PHIReg = ValueMap[PN]; |
| 221 | assert(PHIReg && "PHI node does not have an assigned virtual register!"); |
| 222 | |
| 223 | SmallVector<EVT, 4> ValueVTs; |
Bill Wendling | 8db01cb | 2013-06-06 00:11:39 +0000 | [diff] [blame] | 224 | ComputeValueVTs(*TLI, PN->getType(), ValueVTs); |
Dan Gohman | a3624b6 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 225 | for (unsigned vti = 0, vte = ValueVTs.size(); vti != vte; ++vti) { |
| 226 | EVT VT = ValueVTs[vti]; |
Bill Wendling | 8db01cb | 2013-06-06 00:11:39 +0000 | [diff] [blame] | 227 | unsigned NumRegisters = TLI->getNumRegisters(Fn->getContext(), VT); |
Eric Christopher | fc6de42 | 2014-08-05 02:39:49 +0000 | [diff] [blame] | 228 | const TargetInstrInfo *TII = MF->getSubtarget().getInstrInfo(); |
Dan Gohman | a3624b6 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 229 | for (unsigned i = 0; i != NumRegisters; ++i) |
Chris Lattner | b06015a | 2010-02-09 19:54:29 +0000 | [diff] [blame] | 230 | BuildMI(MBB, DL, TII->get(TargetOpcode::PHI), PHIReg + i); |
Dan Gohman | a3624b6 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 231 | PHIReg += NumRegisters; |
| 232 | } |
| 233 | } |
| 234 | } |
Dan Gohman | 69e8e32 | 2010-04-14 16:32:56 +0000 | [diff] [blame] | 235 | |
| 236 | // Mark landing pad blocks. |
| 237 | for (BB = Fn->begin(); BB != EB; ++BB) |
Dan Gohman | 913c998 | 2010-04-15 04:33:49 +0000 | [diff] [blame] | 238 | if (const InvokeInst *Invoke = dyn_cast<InvokeInst>(BB->getTerminator())) |
Dan Gohman | 69e8e32 | 2010-04-14 16:32:56 +0000 | [diff] [blame] | 239 | MBBMap[Invoke->getSuccessor(1)]->setIsLandingPad(); |
Dan Gohman | a3624b6 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | /// clear - Clear out all the function-specific state. This returns this |
| 243 | /// FunctionLoweringInfo to an empty state, ready to be used for a |
| 244 | /// different function. |
| 245 | void FunctionLoweringInfo::clear() { |
Dan Gohman | ad0b3ea | 2010-04-14 17:11:23 +0000 | [diff] [blame] | 246 | assert(CatchInfoFound.size() == CatchInfoLost.size() && |
| 247 | "Not all catch info was assigned to a landing pad!"); |
| 248 | |
Dan Gohman | a3624b6 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 249 | MBBMap.clear(); |
| 250 | ValueMap.clear(); |
| 251 | StaticAllocaMap.clear(); |
| 252 | #ifndef NDEBUG |
| 253 | CatchInfoLost.clear(); |
| 254 | CatchInfoFound.clear(); |
| 255 | #endif |
| 256 | LiveOutRegInfo.clear(); |
Cameron Zwarich | 988faf9 | 2011-02-24 10:00:13 +0000 | [diff] [blame] | 257 | VisitedBBs.clear(); |
Evan Cheng | 6e82245 | 2010-04-28 23:08:54 +0000 | [diff] [blame] | 258 | ArgDbgValues.clear(); |
Devang Patel | 86ec8b3 | 2010-08-31 22:22:42 +0000 | [diff] [blame] | 259 | ByValArgFrameIndexMap.clear(); |
Dan Gohman | d7b5ce3 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 260 | RegFixups.clear(); |
Dan Gohman | a3624b6 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 261 | } |
| 262 | |
Dan Gohman | 93f5920 | 2010-07-02 00:10:16 +0000 | [diff] [blame] | 263 | /// CreateReg - Allocate a single virtual register for the given type. |
Patrik Hagglund | 5e6c361 | 2012-12-13 06:34:11 +0000 | [diff] [blame] | 264 | unsigned FunctionLoweringInfo::CreateReg(MVT VT) { |
Eric Christopher | d913448 | 2014-08-04 21:25:23 +0000 | [diff] [blame] | 265 | return RegInfo->createVirtualRegister( |
| 266 | TM.getSubtargetImpl()->getTargetLowering()->getRegClassFor(VT)); |
Dan Gohman | a3624b6 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 267 | } |
| 268 | |
Dan Gohman | 93f5920 | 2010-07-02 00:10:16 +0000 | [diff] [blame] | 269 | /// CreateRegs - Allocate the appropriate number of virtual registers of |
Dan Gohman | a3624b6 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 270 | /// the correctly promoted or expanded types. Assign these registers |
| 271 | /// consecutive vreg numbers and return the first assigned number. |
| 272 | /// |
| 273 | /// In the case that the given value has struct or array type, this function |
| 274 | /// will assign registers for each member or element. |
| 275 | /// |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 276 | unsigned FunctionLoweringInfo::CreateRegs(Type *Ty) { |
Eric Christopher | d913448 | 2014-08-04 21:25:23 +0000 | [diff] [blame] | 277 | const TargetLowering *TLI = TM.getSubtargetImpl()->getTargetLowering(); |
Bill Wendling | 0ccf310 | 2013-06-19 20:32:16 +0000 | [diff] [blame] | 278 | |
Dan Gohman | a3624b6 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 279 | SmallVector<EVT, 4> ValueVTs; |
Bill Wendling | 8db01cb | 2013-06-06 00:11:39 +0000 | [diff] [blame] | 280 | ComputeValueVTs(*TLI, Ty, ValueVTs); |
Dan Gohman | a3624b6 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 281 | |
| 282 | unsigned FirstReg = 0; |
| 283 | for (unsigned Value = 0, e = ValueVTs.size(); Value != e; ++Value) { |
| 284 | EVT ValueVT = ValueVTs[Value]; |
Bill Wendling | 8db01cb | 2013-06-06 00:11:39 +0000 | [diff] [blame] | 285 | MVT RegisterVT = TLI->getRegisterType(Ty->getContext(), ValueVT); |
Dan Gohman | a3624b6 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 286 | |
Bill Wendling | 8db01cb | 2013-06-06 00:11:39 +0000 | [diff] [blame] | 287 | unsigned NumRegs = TLI->getNumRegisters(Ty->getContext(), ValueVT); |
Dan Gohman | a3624b6 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 288 | for (unsigned i = 0; i != NumRegs; ++i) { |
Dan Gohman | 93f5920 | 2010-07-02 00:10:16 +0000 | [diff] [blame] | 289 | unsigned R = CreateReg(RegisterVT); |
Dan Gohman | a3624b6 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 290 | if (!FirstReg) FirstReg = R; |
| 291 | } |
| 292 | } |
| 293 | return FirstReg; |
| 294 | } |
Dan Gohman | ad97b3d | 2009-11-23 17:42:46 +0000 | [diff] [blame] | 295 | |
Cameron Zwarich | a62fc89 | 2011-02-24 10:00:25 +0000 | [diff] [blame] | 296 | /// GetLiveOutRegInfo - Gets LiveOutInfo for a register, returning NULL if the |
| 297 | /// register is a PHI destination and the PHI's LiveOutInfo is not valid. If |
| 298 | /// the register's LiveOutInfo is for a smaller bit width, it is extended to |
| 299 | /// the larger bit width by zero extension. The bit width must be no smaller |
| 300 | /// than the LiveOutInfo's existing bit width. |
| 301 | const FunctionLoweringInfo::LiveOutInfo * |
| 302 | FunctionLoweringInfo::GetLiveOutRegInfo(unsigned Reg, unsigned BitWidth) { |
| 303 | if (!LiveOutRegInfo.inBounds(Reg)) |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 304 | return nullptr; |
Cameron Zwarich | a62fc89 | 2011-02-24 10:00:25 +0000 | [diff] [blame] | 305 | |
| 306 | LiveOutInfo *LOI = &LiveOutRegInfo[Reg]; |
| 307 | if (!LOI->IsValid) |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 308 | return nullptr; |
Cameron Zwarich | a62fc89 | 2011-02-24 10:00:25 +0000 | [diff] [blame] | 309 | |
Cameron Zwarich | d2f3041 | 2011-02-25 01:10:55 +0000 | [diff] [blame] | 310 | if (BitWidth > LOI->KnownZero.getBitWidth()) { |
Cameron Zwarich | 4c82cd2 | 2011-02-25 01:11:01 +0000 | [diff] [blame] | 311 | LOI->NumSignBits = 1; |
Cameron Zwarich | a62fc89 | 2011-02-24 10:00:25 +0000 | [diff] [blame] | 312 | LOI->KnownZero = LOI->KnownZero.zextOrTrunc(BitWidth); |
| 313 | LOI->KnownOne = LOI->KnownOne.zextOrTrunc(BitWidth); |
| 314 | } |
| 315 | |
| 316 | return LOI; |
| 317 | } |
| 318 | |
| 319 | /// ComputePHILiveOutRegInfo - Compute LiveOutInfo for a PHI's destination |
| 320 | /// register based on the LiveOutInfo of its operands. |
| 321 | void FunctionLoweringInfo::ComputePHILiveOutRegInfo(const PHINode *PN) { |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 322 | Type *Ty = PN->getType(); |
Cameron Zwarich | a62fc89 | 2011-02-24 10:00:25 +0000 | [diff] [blame] | 323 | if (!Ty->isIntegerTy() || Ty->isVectorTy()) |
| 324 | return; |
| 325 | |
Eric Christopher | d913448 | 2014-08-04 21:25:23 +0000 | [diff] [blame] | 326 | const TargetLowering *TLI = TM.getSubtargetImpl()->getTargetLowering(); |
Bill Wendling | 0ccf310 | 2013-06-19 20:32:16 +0000 | [diff] [blame] | 327 | |
Cameron Zwarich | a62fc89 | 2011-02-24 10:00:25 +0000 | [diff] [blame] | 328 | SmallVector<EVT, 1> ValueVTs; |
Bill Wendling | 8db01cb | 2013-06-06 00:11:39 +0000 | [diff] [blame] | 329 | ComputeValueVTs(*TLI, Ty, ValueVTs); |
Cameron Zwarich | a62fc89 | 2011-02-24 10:00:25 +0000 | [diff] [blame] | 330 | assert(ValueVTs.size() == 1 && |
| 331 | "PHIs with non-vector integer types should have a single VT."); |
| 332 | EVT IntVT = ValueVTs[0]; |
| 333 | |
Bill Wendling | 8db01cb | 2013-06-06 00:11:39 +0000 | [diff] [blame] | 334 | if (TLI->getNumRegisters(PN->getContext(), IntVT) != 1) |
Cameron Zwarich | a62fc89 | 2011-02-24 10:00:25 +0000 | [diff] [blame] | 335 | return; |
Bill Wendling | 8db01cb | 2013-06-06 00:11:39 +0000 | [diff] [blame] | 336 | IntVT = TLI->getTypeToTransformTo(PN->getContext(), IntVT); |
Cameron Zwarich | a62fc89 | 2011-02-24 10:00:25 +0000 | [diff] [blame] | 337 | unsigned BitWidth = IntVT.getSizeInBits(); |
| 338 | |
| 339 | unsigned DestReg = ValueMap[PN]; |
| 340 | if (!TargetRegisterInfo::isVirtualRegister(DestReg)) |
| 341 | return; |
| 342 | LiveOutRegInfo.grow(DestReg); |
| 343 | LiveOutInfo &DestLOI = LiveOutRegInfo[DestReg]; |
| 344 | |
| 345 | Value *V = PN->getIncomingValue(0); |
| 346 | if (isa<UndefValue>(V) || isa<ConstantExpr>(V)) { |
| 347 | DestLOI.NumSignBits = 1; |
| 348 | APInt Zero(BitWidth, 0); |
| 349 | DestLOI.KnownZero = Zero; |
| 350 | DestLOI.KnownOne = Zero; |
| 351 | return; |
| 352 | } |
| 353 | |
| 354 | if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) { |
| 355 | APInt Val = CI->getValue().zextOrTrunc(BitWidth); |
| 356 | DestLOI.NumSignBits = Val.getNumSignBits(); |
| 357 | DestLOI.KnownZero = ~Val; |
| 358 | DestLOI.KnownOne = Val; |
| 359 | } else { |
| 360 | assert(ValueMap.count(V) && "V should have been placed in ValueMap when its" |
| 361 | "CopyToReg node was created."); |
| 362 | unsigned SrcReg = ValueMap[V]; |
| 363 | if (!TargetRegisterInfo::isVirtualRegister(SrcReg)) { |
| 364 | DestLOI.IsValid = false; |
| 365 | return; |
| 366 | } |
| 367 | const LiveOutInfo *SrcLOI = GetLiveOutRegInfo(SrcReg, BitWidth); |
| 368 | if (!SrcLOI) { |
| 369 | DestLOI.IsValid = false; |
| 370 | return; |
| 371 | } |
| 372 | DestLOI = *SrcLOI; |
| 373 | } |
| 374 | |
| 375 | assert(DestLOI.KnownZero.getBitWidth() == BitWidth && |
| 376 | DestLOI.KnownOne.getBitWidth() == BitWidth && |
| 377 | "Masks should have the same bit width as the type."); |
| 378 | |
| 379 | for (unsigned i = 1, e = PN->getNumIncomingValues(); i != e; ++i) { |
| 380 | Value *V = PN->getIncomingValue(i); |
| 381 | if (isa<UndefValue>(V) || isa<ConstantExpr>(V)) { |
| 382 | DestLOI.NumSignBits = 1; |
| 383 | APInt Zero(BitWidth, 0); |
| 384 | DestLOI.KnownZero = Zero; |
| 385 | DestLOI.KnownOne = Zero; |
Eric Christopher | 0713a9d | 2011-06-08 23:55:35 +0000 | [diff] [blame] | 386 | return; |
Cameron Zwarich | a62fc89 | 2011-02-24 10:00:25 +0000 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) { |
| 390 | APInt Val = CI->getValue().zextOrTrunc(BitWidth); |
| 391 | DestLOI.NumSignBits = std::min(DestLOI.NumSignBits, Val.getNumSignBits()); |
| 392 | DestLOI.KnownZero &= ~Val; |
| 393 | DestLOI.KnownOne &= Val; |
| 394 | continue; |
| 395 | } |
| 396 | |
| 397 | assert(ValueMap.count(V) && "V should have been placed in ValueMap when " |
| 398 | "its CopyToReg node was created."); |
| 399 | unsigned SrcReg = ValueMap[V]; |
| 400 | if (!TargetRegisterInfo::isVirtualRegister(SrcReg)) { |
| 401 | DestLOI.IsValid = false; |
| 402 | return; |
| 403 | } |
| 404 | const LiveOutInfo *SrcLOI = GetLiveOutRegInfo(SrcReg, BitWidth); |
| 405 | if (!SrcLOI) { |
| 406 | DestLOI.IsValid = false; |
| 407 | return; |
| 408 | } |
| 409 | DestLOI.NumSignBits = std::min(DestLOI.NumSignBits, SrcLOI->NumSignBits); |
| 410 | DestLOI.KnownZero &= SrcLOI->KnownZero; |
| 411 | DestLOI.KnownOne &= SrcLOI->KnownOne; |
| 412 | } |
| 413 | } |
| 414 | |
Devang Patel | 9d904e1 | 2011-09-08 22:59:09 +0000 | [diff] [blame] | 415 | /// setArgumentFrameIndex - Record frame index for the byval |
Devang Patel | 86ec8b3 | 2010-08-31 22:22:42 +0000 | [diff] [blame] | 416 | /// argument. This overrides previous frame index entry for this argument, |
| 417 | /// if any. |
Devang Patel | 9d904e1 | 2011-09-08 22:59:09 +0000 | [diff] [blame] | 418 | void FunctionLoweringInfo::setArgumentFrameIndex(const Argument *A, |
Eric Christopher | 219d51d | 2012-02-24 01:59:01 +0000 | [diff] [blame] | 419 | int FI) { |
Devang Patel | 86ec8b3 | 2010-08-31 22:22:42 +0000 | [diff] [blame] | 420 | ByValArgFrameIndexMap[A] = FI; |
| 421 | } |
Eric Christopher | 0713a9d | 2011-06-08 23:55:35 +0000 | [diff] [blame] | 422 | |
Devang Patel | 9d904e1 | 2011-09-08 22:59:09 +0000 | [diff] [blame] | 423 | /// getArgumentFrameIndex - Get frame index for the byval argument. |
Devang Patel | 86ec8b3 | 2010-08-31 22:22:42 +0000 | [diff] [blame] | 424 | /// If the argument does not have any assigned frame index then 0 is |
| 425 | /// returned. |
Devang Patel | 9d904e1 | 2011-09-08 22:59:09 +0000 | [diff] [blame] | 426 | int FunctionLoweringInfo::getArgumentFrameIndex(const Argument *A) { |
Eric Christopher | 0713a9d | 2011-06-08 23:55:35 +0000 | [diff] [blame] | 427 | DenseMap<const Argument *, int>::iterator I = |
Devang Patel | 86ec8b3 | 2010-08-31 22:22:42 +0000 | [diff] [blame] | 428 | ByValArgFrameIndexMap.find(A); |
| 429 | if (I != ByValArgFrameIndexMap.end()) |
| 430 | return I->second; |
Eric Christopher | 18c6be7 | 2012-02-23 03:39:43 +0000 | [diff] [blame] | 431 | DEBUG(dbgs() << "Argument does not have assigned frame index!\n"); |
Devang Patel | 86ec8b3 | 2010-08-31 22:22:42 +0000 | [diff] [blame] | 432 | return 0; |
| 433 | } |
| 434 | |
Michael J. Spencer | 8b98bf2 | 2012-02-22 19:06:13 +0000 | [diff] [blame] | 435 | /// ComputeUsesVAFloatArgument - Determine if any floating-point values are |
| 436 | /// being passed to this variadic function, and set the MachineModuleInfo's |
| 437 | /// usesVAFloatArgument flag if so. This flag is used to emit an undefined |
| 438 | /// reference to _fltused on Windows, which will link in MSVCRT's |
| 439 | /// floating-point support. |
| 440 | void llvm::ComputeUsesVAFloatArgument(const CallInst &I, |
| 441 | MachineModuleInfo *MMI) |
| 442 | { |
| 443 | FunctionType *FT = cast<FunctionType>( |
| 444 | I.getCalledValue()->getType()->getContainedType(0)); |
| 445 | if (FT->isVarArg() && !MMI->usesVAFloatArgument()) { |
| 446 | for (unsigned i = 0, e = I.getNumArgOperands(); i != e; ++i) { |
| 447 | Type* T = I.getArgOperand(i)->getType(); |
| 448 | for (po_iterator<Type*> i = po_begin(T), e = po_end(T); |
| 449 | i != e; ++i) { |
| 450 | if (i->isFloatingPointTy()) { |
| 451 | MMI->setUsesVAFloatArgument(true); |
| 452 | return; |
| 453 | } |
| 454 | } |
| 455 | } |
| 456 | } |
| 457 | } |
| 458 | |
Dan Gohman | ad97b3d | 2009-11-23 17:42:46 +0000 | [diff] [blame] | 459 | /// AddCatchInfo - Extract the personality and type infos from an eh.selector |
| 460 | /// call, and add them to the specified machine basic block. |
Dan Gohman | 7deb447 | 2010-04-14 19:53:31 +0000 | [diff] [blame] | 461 | void llvm::AddCatchInfo(const CallInst &I, MachineModuleInfo *MMI, |
Dan Gohman | ad97b3d | 2009-11-23 17:42:46 +0000 | [diff] [blame] | 462 | MachineBasicBlock *MBB) { |
| 463 | // Inform the MachineModuleInfo of the personality for this landing pad. |
Gabor Greif | e4eed70 | 2010-06-25 08:24:59 +0000 | [diff] [blame] | 464 | const ConstantExpr *CE = cast<ConstantExpr>(I.getArgOperand(1)); |
Dan Gohman | ad97b3d | 2009-11-23 17:42:46 +0000 | [diff] [blame] | 465 | assert(CE->getOpcode() == Instruction::BitCast && |
| 466 | isa<Function>(CE->getOperand(0)) && |
| 467 | "Personality should be a function"); |
| 468 | MMI->addPersonality(MBB, cast<Function>(CE->getOperand(0))); |
| 469 | |
| 470 | // Gather all the type infos for this landing pad and pass them along to |
| 471 | // MachineModuleInfo. |
Dan Gohman | bcaf681 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 472 | std::vector<const GlobalVariable *> TyInfo; |
Gabor Greif | 647d9c9 | 2010-06-30 13:45:50 +0000 | [diff] [blame] | 473 | unsigned N = I.getNumArgOperands(); |
Dan Gohman | ad97b3d | 2009-11-23 17:42:46 +0000 | [diff] [blame] | 474 | |
Gabor Greif | 647d9c9 | 2010-06-30 13:45:50 +0000 | [diff] [blame] | 475 | for (unsigned i = N - 1; i > 1; --i) { |
| 476 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(I.getArgOperand(i))) { |
Dan Gohman | ad97b3d | 2009-11-23 17:42:46 +0000 | [diff] [blame] | 477 | unsigned FilterLength = CI->getZExtValue(); |
| 478 | unsigned FirstCatch = i + FilterLength + !FilterLength; |
Gabor Greif | 647d9c9 | 2010-06-30 13:45:50 +0000 | [diff] [blame] | 479 | assert(FirstCatch <= N && "Invalid filter length"); |
Dan Gohman | ad97b3d | 2009-11-23 17:42:46 +0000 | [diff] [blame] | 480 | |
| 481 | if (FirstCatch < N) { |
| 482 | TyInfo.reserve(N - FirstCatch); |
| 483 | for (unsigned j = FirstCatch; j < N; ++j) |
Gabor Greif | 647d9c9 | 2010-06-30 13:45:50 +0000 | [diff] [blame] | 484 | TyInfo.push_back(ExtractTypeInfo(I.getArgOperand(j))); |
Dan Gohman | ad97b3d | 2009-11-23 17:42:46 +0000 | [diff] [blame] | 485 | MMI->addCatchTypeInfo(MBB, TyInfo); |
| 486 | TyInfo.clear(); |
| 487 | } |
| 488 | |
| 489 | if (!FilterLength) { |
| 490 | // Cleanup. |
| 491 | MMI->addCleanup(MBB); |
| 492 | } else { |
| 493 | // Filter. |
| 494 | TyInfo.reserve(FilterLength - 1); |
| 495 | for (unsigned j = i + 1; j < FirstCatch; ++j) |
Gabor Greif | 647d9c9 | 2010-06-30 13:45:50 +0000 | [diff] [blame] | 496 | TyInfo.push_back(ExtractTypeInfo(I.getArgOperand(j))); |
Dan Gohman | ad97b3d | 2009-11-23 17:42:46 +0000 | [diff] [blame] | 497 | MMI->addFilterTypeInfo(MBB, TyInfo); |
| 498 | TyInfo.clear(); |
| 499 | } |
| 500 | |
| 501 | N = i; |
| 502 | } |
| 503 | } |
| 504 | |
Gabor Greif | 647d9c9 | 2010-06-30 13:45:50 +0000 | [diff] [blame] | 505 | if (N > 2) { |
| 506 | TyInfo.reserve(N - 2); |
| 507 | for (unsigned j = 2; j < N; ++j) |
| 508 | TyInfo.push_back(ExtractTypeInfo(I.getArgOperand(j))); |
Dan Gohman | ad97b3d | 2009-11-23 17:42:46 +0000 | [diff] [blame] | 509 | MMI->addCatchTypeInfo(MBB, TyInfo); |
| 510 | } |
| 511 | } |
| 512 | |
Bill Wendling | 247fd3b | 2011-08-17 21:56:44 +0000 | [diff] [blame] | 513 | /// AddLandingPadInfo - Extract the exception handling information from the |
| 514 | /// landingpad instruction and add them to the specified machine module info. |
| 515 | void llvm::AddLandingPadInfo(const LandingPadInst &I, MachineModuleInfo &MMI, |
| 516 | MachineBasicBlock *MBB) { |
| 517 | MMI.addPersonality(MBB, |
| 518 | cast<Function>(I.getPersonalityFn()->stripPointerCasts())); |
| 519 | |
| 520 | if (I.isCleanup()) |
| 521 | MMI.addCleanup(MBB); |
| 522 | |
| 523 | // FIXME: New EH - Add the clauses in reverse order. This isn't 100% correct, |
| 524 | // but we need to do it this way because of how the DWARF EH emitter |
| 525 | // processes the clauses. |
| 526 | for (unsigned i = I.getNumClauses(); i != 0; --i) { |
| 527 | Value *Val = I.getClause(i - 1); |
| 528 | if (I.isCatch(i - 1)) { |
| 529 | MMI.addCatchTypeInfo(MBB, |
| 530 | dyn_cast<GlobalVariable>(Val->stripPointerCasts())); |
| 531 | } else { |
| 532 | // Add filters in a list. |
| 533 | Constant *CVal = cast<Constant>(Val); |
| 534 | SmallVector<const GlobalVariable*, 4> FilterList; |
| 535 | for (User::op_iterator |
| 536 | II = CVal->op_begin(), IE = CVal->op_end(); II != IE; ++II) |
| 537 | FilterList.push_back(cast<GlobalVariable>((*II)->stripPointerCasts())); |
| 538 | |
| 539 | MMI.addFilterTypeInfo(MBB, FilterList); |
| 540 | } |
| 541 | } |
| 542 | } |