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