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