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(); |
Stephen Hines | ebe69fe | 2015-03-23 12:10:34 -0700 | [diff] [blame^] | 136 | const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo(); |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 137 | std::vector<TargetLowering::AsmOperandInfo> Ops = |
Stephen Hines | ebe69fe | 2015-03-23 12:10:34 -0700 | [diff] [blame^] | 138 | TLI->ParseConstraints(TRI, CS); |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 139 | for (size_t I = 0, E = Ops.size(); I != E; ++I) { |
| 140 | TargetLowering::AsmOperandInfo &Op = Ops[I]; |
| 141 | if (Op.Type == InlineAsm::isClobber) { |
| 142 | // Clobbers don't have SDValue operands, hence SDValue(). |
| 143 | TLI->ComputeConstraintToUse(Op, SDValue(), DAG); |
Stephen Hines | 37ed9c1 | 2014-12-01 14:51:49 -0800 | [diff] [blame] | 144 | std::pair<unsigned, const TargetRegisterClass *> PhysReg = |
Stephen Hines | ebe69fe | 2015-03-23 12:10:34 -0700 | [diff] [blame^] | 145 | TLI->getRegForInlineAsmConstraint(TRI, Op.ConstraintCode, |
| 146 | Op.ConstraintVT); |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 147 | if (PhysReg.first == SP) |
| 148 | MF->getFrameInfo()->setHasInlineAsmWithSPAdjust(true); |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | |
Stephen Hines | 37ed9c1 | 2014-12-01 14:51:49 -0800 | [diff] [blame] | 154 | // Look for calls to the @llvm.va_start intrinsic. We can omit some |
| 155 | // prologue boilerplate for variadic functions that don't examine their |
| 156 | // arguments. |
| 157 | if (const auto *II = dyn_cast<IntrinsicInst>(I)) { |
| 158 | if (II->getIntrinsicID() == Intrinsic::vastart) |
| 159 | MF->getFrameInfo()->setHasVAStart(true); |
| 160 | } |
| 161 | |
| 162 | // If we have a musttail call in a variadic funciton, we need to ensure we |
| 163 | // forward implicit register parameters. |
| 164 | if (const auto *CI = dyn_cast<CallInst>(I)) { |
| 165 | if (CI->isMustTailCall() && Fn->isVarArg()) |
| 166 | MF->getFrameInfo()->setHasMustTailInVarArgFunc(true); |
| 167 | } |
| 168 | |
Dan Gohman | 9c3d5e4 | 2010-07-16 17:54:27 +0000 | [diff] [blame] | 169 | // Mark values used outside their block as exported, by allocating |
| 170 | // a virtual register for them. |
Cameron Zwarich | 4ecc82e | 2011-02-22 03:24:52 +0000 | [diff] [blame] | 171 | if (isUsedOutsideOfDefiningBlock(I)) |
Dan Gohman | 6277eb2 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 172 | if (!isa<AllocaInst>(I) || |
| 173 | !StaticAllocaMap.count(cast<AllocaInst>(I))) |
| 174 | InitializeRegForValue(I); |
| 175 | |
Dan Gohman | 9c3d5e4 | 2010-07-16 17:54:27 +0000 | [diff] [blame] | 176 | // Collect llvm.dbg.declare information. This is done now instead of |
| 177 | // during the initial isel pass through the IR so that it is done |
| 178 | // in a predictable order. |
| 179 | if (const DbgDeclareInst *DI = dyn_cast<DbgDeclareInst>(I)) { |
| 180 | MachineModuleInfo &MMI = MF->getMMI(); |
Manman Ren | cbafae6 | 2013-06-28 05:43:10 +0000 | [diff] [blame] | 181 | DIVariable DIVar(DI->getVariable()); |
| 182 | assert((!DIVar || DIVar.isVariable()) && |
| 183 | "Variable in DbgDeclareInst should be either null or a DIVariable."); |
Dan Gohman | 9c3d5e4 | 2010-07-16 17:54:27 +0000 | [diff] [blame] | 184 | if (MMI.hasDebugInfo() && |
Manman Ren | cbafae6 | 2013-06-28 05:43:10 +0000 | [diff] [blame] | 185 | DIVar && |
Dan Gohman | 9c3d5e4 | 2010-07-16 17:54:27 +0000 | [diff] [blame] | 186 | !DI->getDebugLoc().isUnknown()) { |
| 187 | // Don't handle byval struct arguments or VLAs, for example. |
| 188 | // Non-byval arguments are handled here (they refer to the stack |
| 189 | // temporary alloca at this point). |
| 190 | const Value *Address = DI->getAddress(); |
| 191 | if (Address) { |
| 192 | if (const BitCastInst *BCI = dyn_cast<BitCastInst>(Address)) |
| 193 | Address = BCI->getOperand(0); |
| 194 | if (const AllocaInst *AI = dyn_cast<AllocaInst>(Address)) { |
| 195 | DenseMap<const AllocaInst *, int>::iterator SI = |
| 196 | StaticAllocaMap.find(AI); |
| 197 | if (SI != StaticAllocaMap.end()) { // Check for VLAs. |
| 198 | int FI = SI->second; |
Stephen Hines | 37ed9c1 | 2014-12-01 14:51:49 -0800 | [diff] [blame] | 199 | MMI.setVariableDbgInfo(DI->getVariable(), DI->getExpression(), |
Dan Gohman | 9c3d5e4 | 2010-07-16 17:54:27 +0000 | [diff] [blame] | 200 | FI, DI->getDebugLoc()); |
| 201 | } |
| 202 | } |
| 203 | } |
| 204 | } |
| 205 | } |
Stephen Hines | 37ed9c1 | 2014-12-01 14:51:49 -0800 | [diff] [blame] | 206 | |
| 207 | // Decide the preferred extend type for a value. |
| 208 | PreferredExtendType[I] = getPreferredExtendForValue(I); |
Dan Gohman | 9c3d5e4 | 2010-07-16 17:54:27 +0000 | [diff] [blame] | 209 | } |
| 210 | |
Dan Gohman | 6277eb2 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 211 | // Create an initial MachineBasicBlock for each LLVM BasicBlock in F. This |
| 212 | // also creates the initial PHI MachineInstrs, though none of the input |
| 213 | // operands are populated. |
Dan Gohman | d0d8275 | 2010-04-14 16:30:40 +0000 | [diff] [blame] | 214 | for (BB = Fn->begin(); BB != EB; ++BB) { |
Dan Gohman | 6277eb2 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 215 | MachineBasicBlock *MBB = mf.CreateMachineBasicBlock(BB); |
| 216 | MBBMap[BB] = MBB; |
| 217 | MF->push_back(MBB); |
| 218 | |
| 219 | // Transfer the address-taken flag. This is necessary because there could |
| 220 | // be multiple MachineBasicBlocks corresponding to one BasicBlock, and only |
| 221 | // the first one should be marked. |
| 222 | if (BB->hasAddressTaken()) |
| 223 | MBB->setHasAddressTaken(); |
| 224 | |
| 225 | // Create Machine PHI nodes for LLVM PHI nodes, lowering them as |
| 226 | // appropriate. |
Dan Gohman | 3f1403f | 2010-04-20 14:46:25 +0000 | [diff] [blame] | 227 | for (BasicBlock::const_iterator I = BB->begin(); |
| 228 | const PHINode *PN = dyn_cast<PHINode>(I); ++I) { |
| 229 | if (PN->use_empty()) continue; |
Dan Gohman | 6277eb2 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 230 | |
Rafael Espindola | 3fa8283 | 2011-05-13 15:18:06 +0000 | [diff] [blame] | 231 | // Skip empty types |
| 232 | if (PN->getType()->isEmptyTy()) |
| 233 | continue; |
| 234 | |
Dan Gohman | c025c85 | 2010-04-20 14:48:02 +0000 | [diff] [blame] | 235 | DebugLoc DL = PN->getDebugLoc(); |
Dan Gohman | 6277eb2 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 236 | unsigned PHIReg = ValueMap[PN]; |
| 237 | assert(PHIReg && "PHI node does not have an assigned virtual register!"); |
| 238 | |
| 239 | SmallVector<EVT, 4> ValueVTs; |
Bill Wendling | 384ceb8 | 2013-06-06 00:11:39 +0000 | [diff] [blame] | 240 | ComputeValueVTs(*TLI, PN->getType(), ValueVTs); |
Dan Gohman | 6277eb2 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 241 | for (unsigned vti = 0, vte = ValueVTs.size(); vti != vte; ++vti) { |
| 242 | EVT VT = ValueVTs[vti]; |
Bill Wendling | 384ceb8 | 2013-06-06 00:11:39 +0000 | [diff] [blame] | 243 | unsigned NumRegisters = TLI->getNumRegisters(Fn->getContext(), VT); |
Stephen Hines | 37ed9c1 | 2014-12-01 14:51:49 -0800 | [diff] [blame] | 244 | const TargetInstrInfo *TII = MF->getSubtarget().getInstrInfo(); |
Dan Gohman | 6277eb2 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 245 | for (unsigned i = 0; i != NumRegisters; ++i) |
Chris Lattner | 518bb53 | 2010-02-09 19:54:29 +0000 | [diff] [blame] | 246 | BuildMI(MBB, DL, TII->get(TargetOpcode::PHI), PHIReg + i); |
Dan Gohman | 6277eb2 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 247 | PHIReg += NumRegisters; |
| 248 | } |
| 249 | } |
| 250 | } |
Dan Gohman | de4c0a7 | 2010-04-14 16:32:56 +0000 | [diff] [blame] | 251 | |
| 252 | // Mark landing pad blocks. |
| 253 | for (BB = Fn->begin(); BB != EB; ++BB) |
Dan Gohman | ae541aa | 2010-04-15 04:33:49 +0000 | [diff] [blame] | 254 | if (const InvokeInst *Invoke = dyn_cast<InvokeInst>(BB->getTerminator())) |
Dan Gohman | de4c0a7 | 2010-04-14 16:32:56 +0000 | [diff] [blame] | 255 | MBBMap[Invoke->getSuccessor(1)]->setIsLandingPad(); |
Dan Gohman | 6277eb2 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | /// clear - Clear out all the function-specific state. This returns this |
| 259 | /// FunctionLoweringInfo to an empty state, ready to be used for a |
| 260 | /// different function. |
| 261 | void FunctionLoweringInfo::clear() { |
Dan Gohman | 0e02672 | 2010-04-14 17:11:23 +0000 | [diff] [blame] | 262 | assert(CatchInfoFound.size() == CatchInfoLost.size() && |
| 263 | "Not all catch info was assigned to a landing pad!"); |
| 264 | |
Dan Gohman | 6277eb2 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 265 | MBBMap.clear(); |
| 266 | ValueMap.clear(); |
| 267 | StaticAllocaMap.clear(); |
| 268 | #ifndef NDEBUG |
| 269 | CatchInfoLost.clear(); |
| 270 | CatchInfoFound.clear(); |
| 271 | #endif |
| 272 | LiveOutRegInfo.clear(); |
Cameron Zwarich | a46cd97 | 2011-02-24 10:00:13 +0000 | [diff] [blame] | 273 | VisitedBBs.clear(); |
Evan Cheng | 2ad0fcf | 2010-04-28 23:08:54 +0000 | [diff] [blame] | 274 | ArgDbgValues.clear(); |
Devang Patel | 0b48ead | 2010-08-31 22:22:42 +0000 | [diff] [blame] | 275 | ByValArgFrameIndexMap.clear(); |
Dan Gohman | 84023e0 | 2010-07-10 09:00:22 +0000 | [diff] [blame] | 276 | RegFixups.clear(); |
Stephen Hines | ebe69fe | 2015-03-23 12:10:34 -0700 | [diff] [blame^] | 277 | StatepointStackSlots.clear(); |
Stephen Hines | 37ed9c1 | 2014-12-01 14:51:49 -0800 | [diff] [blame] | 278 | PreferredExtendType.clear(); |
Dan Gohman | 6277eb2 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 279 | } |
| 280 | |
Dan Gohman | 89496d0 | 2010-07-02 00:10:16 +0000 | [diff] [blame] | 281 | /// CreateReg - Allocate a single virtual register for the given type. |
Patrik Hagglund | a61b17c | 2012-12-13 06:34:11 +0000 | [diff] [blame] | 282 | unsigned FunctionLoweringInfo::CreateReg(MVT VT) { |
Stephen Hines | 37ed9c1 | 2014-12-01 14:51:49 -0800 | [diff] [blame] | 283 | return RegInfo->createVirtualRegister( |
| 284 | MF->getSubtarget().getTargetLowering()->getRegClassFor(VT)); |
Dan Gohman | 6277eb2 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 285 | } |
| 286 | |
Dan Gohman | 89496d0 | 2010-07-02 00:10:16 +0000 | [diff] [blame] | 287 | /// CreateRegs - Allocate the appropriate number of virtual registers of |
Dan Gohman | 6277eb2 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 288 | /// the correctly promoted or expanded types. Assign these registers |
| 289 | /// consecutive vreg numbers and return the first assigned number. |
| 290 | /// |
| 291 | /// In the case that the given value has struct or array type, this function |
| 292 | /// will assign registers for each member or element. |
| 293 | /// |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 294 | unsigned FunctionLoweringInfo::CreateRegs(Type *Ty) { |
Stephen Hines | 37ed9c1 | 2014-12-01 14:51:49 -0800 | [diff] [blame] | 295 | const TargetLowering *TLI = MF->getSubtarget().getTargetLowering(); |
Bill Wendling | d626d33 | 2013-06-19 20:32:16 +0000 | [diff] [blame] | 296 | |
Dan Gohman | 6277eb2 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 297 | SmallVector<EVT, 4> ValueVTs; |
Bill Wendling | 384ceb8 | 2013-06-06 00:11:39 +0000 | [diff] [blame] | 298 | ComputeValueVTs(*TLI, Ty, ValueVTs); |
Dan Gohman | 6277eb2 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 299 | |
| 300 | unsigned FirstReg = 0; |
| 301 | for (unsigned Value = 0, e = ValueVTs.size(); Value != e; ++Value) { |
| 302 | EVT ValueVT = ValueVTs[Value]; |
Bill Wendling | 384ceb8 | 2013-06-06 00:11:39 +0000 | [diff] [blame] | 303 | MVT RegisterVT = TLI->getRegisterType(Ty->getContext(), ValueVT); |
Dan Gohman | 6277eb2 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 304 | |
Bill Wendling | 384ceb8 | 2013-06-06 00:11:39 +0000 | [diff] [blame] | 305 | unsigned NumRegs = TLI->getNumRegisters(Ty->getContext(), ValueVT); |
Dan Gohman | 6277eb2 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 306 | for (unsigned i = 0; i != NumRegs; ++i) { |
Dan Gohman | 89496d0 | 2010-07-02 00:10:16 +0000 | [diff] [blame] | 307 | unsigned R = CreateReg(RegisterVT); |
Dan Gohman | 6277eb2 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 308 | if (!FirstReg) FirstReg = R; |
| 309 | } |
| 310 | } |
| 311 | return FirstReg; |
| 312 | } |
Dan Gohman | 66336ed | 2009-11-23 17:42:46 +0000 | [diff] [blame] | 313 | |
Cameron Zwarich | 8ca814c | 2011-02-24 10:00:25 +0000 | [diff] [blame] | 314 | /// GetLiveOutRegInfo - Gets LiveOutInfo for a register, returning NULL if the |
| 315 | /// register is a PHI destination and the PHI's LiveOutInfo is not valid. If |
| 316 | /// the register's LiveOutInfo is for a smaller bit width, it is extended to |
| 317 | /// the larger bit width by zero extension. The bit width must be no smaller |
| 318 | /// than the LiveOutInfo's existing bit width. |
| 319 | const FunctionLoweringInfo::LiveOutInfo * |
| 320 | FunctionLoweringInfo::GetLiveOutRegInfo(unsigned Reg, unsigned BitWidth) { |
| 321 | if (!LiveOutRegInfo.inBounds(Reg)) |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame] | 322 | return nullptr; |
Cameron Zwarich | 8ca814c | 2011-02-24 10:00:25 +0000 | [diff] [blame] | 323 | |
| 324 | LiveOutInfo *LOI = &LiveOutRegInfo[Reg]; |
| 325 | if (!LOI->IsValid) |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame] | 326 | return nullptr; |
Cameron Zwarich | 8ca814c | 2011-02-24 10:00:25 +0000 | [diff] [blame] | 327 | |
Cameron Zwarich | 33b5547 | 2011-02-25 01:10:55 +0000 | [diff] [blame] | 328 | if (BitWidth > LOI->KnownZero.getBitWidth()) { |
Cameron Zwarich | 8fbbdca | 2011-02-25 01:11:01 +0000 | [diff] [blame] | 329 | LOI->NumSignBits = 1; |
Cameron Zwarich | 8ca814c | 2011-02-24 10:00:25 +0000 | [diff] [blame] | 330 | LOI->KnownZero = LOI->KnownZero.zextOrTrunc(BitWidth); |
| 331 | LOI->KnownOne = LOI->KnownOne.zextOrTrunc(BitWidth); |
| 332 | } |
| 333 | |
| 334 | return LOI; |
| 335 | } |
| 336 | |
| 337 | /// ComputePHILiveOutRegInfo - Compute LiveOutInfo for a PHI's destination |
| 338 | /// register based on the LiveOutInfo of its operands. |
| 339 | void FunctionLoweringInfo::ComputePHILiveOutRegInfo(const PHINode *PN) { |
Chris Lattner | db125cf | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 340 | Type *Ty = PN->getType(); |
Cameron Zwarich | 8ca814c | 2011-02-24 10:00:25 +0000 | [diff] [blame] | 341 | if (!Ty->isIntegerTy() || Ty->isVectorTy()) |
| 342 | return; |
| 343 | |
| 344 | SmallVector<EVT, 1> ValueVTs; |
Bill Wendling | 384ceb8 | 2013-06-06 00:11:39 +0000 | [diff] [blame] | 345 | ComputeValueVTs(*TLI, Ty, ValueVTs); |
Cameron Zwarich | 8ca814c | 2011-02-24 10:00:25 +0000 | [diff] [blame] | 346 | assert(ValueVTs.size() == 1 && |
| 347 | "PHIs with non-vector integer types should have a single VT."); |
| 348 | EVT IntVT = ValueVTs[0]; |
| 349 | |
Bill Wendling | 384ceb8 | 2013-06-06 00:11:39 +0000 | [diff] [blame] | 350 | if (TLI->getNumRegisters(PN->getContext(), IntVT) != 1) |
Cameron Zwarich | 8ca814c | 2011-02-24 10:00:25 +0000 | [diff] [blame] | 351 | return; |
Bill Wendling | 384ceb8 | 2013-06-06 00:11:39 +0000 | [diff] [blame] | 352 | IntVT = TLI->getTypeToTransformTo(PN->getContext(), IntVT); |
Cameron Zwarich | 8ca814c | 2011-02-24 10:00:25 +0000 | [diff] [blame] | 353 | unsigned BitWidth = IntVT.getSizeInBits(); |
| 354 | |
| 355 | unsigned DestReg = ValueMap[PN]; |
| 356 | if (!TargetRegisterInfo::isVirtualRegister(DestReg)) |
| 357 | return; |
| 358 | LiveOutRegInfo.grow(DestReg); |
| 359 | LiveOutInfo &DestLOI = LiveOutRegInfo[DestReg]; |
| 360 | |
| 361 | Value *V = PN->getIncomingValue(0); |
| 362 | if (isa<UndefValue>(V) || isa<ConstantExpr>(V)) { |
| 363 | DestLOI.NumSignBits = 1; |
| 364 | APInt Zero(BitWidth, 0); |
| 365 | DestLOI.KnownZero = Zero; |
| 366 | DestLOI.KnownOne = Zero; |
| 367 | return; |
| 368 | } |
| 369 | |
| 370 | if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) { |
| 371 | APInt Val = CI->getValue().zextOrTrunc(BitWidth); |
| 372 | DestLOI.NumSignBits = Val.getNumSignBits(); |
| 373 | DestLOI.KnownZero = ~Val; |
| 374 | DestLOI.KnownOne = Val; |
| 375 | } else { |
| 376 | assert(ValueMap.count(V) && "V should have been placed in ValueMap when its" |
| 377 | "CopyToReg node was created."); |
| 378 | unsigned SrcReg = ValueMap[V]; |
| 379 | if (!TargetRegisterInfo::isVirtualRegister(SrcReg)) { |
| 380 | DestLOI.IsValid = false; |
| 381 | return; |
| 382 | } |
| 383 | const LiveOutInfo *SrcLOI = GetLiveOutRegInfo(SrcReg, BitWidth); |
| 384 | if (!SrcLOI) { |
| 385 | DestLOI.IsValid = false; |
| 386 | return; |
| 387 | } |
| 388 | DestLOI = *SrcLOI; |
| 389 | } |
| 390 | |
| 391 | assert(DestLOI.KnownZero.getBitWidth() == BitWidth && |
| 392 | DestLOI.KnownOne.getBitWidth() == BitWidth && |
| 393 | "Masks should have the same bit width as the type."); |
| 394 | |
| 395 | for (unsigned i = 1, e = PN->getNumIncomingValues(); i != e; ++i) { |
| 396 | Value *V = PN->getIncomingValue(i); |
| 397 | if (isa<UndefValue>(V) || isa<ConstantExpr>(V)) { |
| 398 | DestLOI.NumSignBits = 1; |
| 399 | APInt Zero(BitWidth, 0); |
| 400 | DestLOI.KnownZero = Zero; |
| 401 | DestLOI.KnownOne = Zero; |
Eric Christopher | 471e422 | 2011-06-08 23:55:35 +0000 | [diff] [blame] | 402 | return; |
Cameron Zwarich | 8ca814c | 2011-02-24 10:00:25 +0000 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) { |
| 406 | APInt Val = CI->getValue().zextOrTrunc(BitWidth); |
| 407 | DestLOI.NumSignBits = std::min(DestLOI.NumSignBits, Val.getNumSignBits()); |
| 408 | DestLOI.KnownZero &= ~Val; |
| 409 | DestLOI.KnownOne &= Val; |
| 410 | continue; |
| 411 | } |
| 412 | |
| 413 | assert(ValueMap.count(V) && "V should have been placed in ValueMap when " |
| 414 | "its CopyToReg node was created."); |
| 415 | unsigned SrcReg = ValueMap[V]; |
| 416 | if (!TargetRegisterInfo::isVirtualRegister(SrcReg)) { |
| 417 | DestLOI.IsValid = false; |
| 418 | return; |
| 419 | } |
| 420 | const LiveOutInfo *SrcLOI = GetLiveOutRegInfo(SrcReg, BitWidth); |
| 421 | if (!SrcLOI) { |
| 422 | DestLOI.IsValid = false; |
| 423 | return; |
| 424 | } |
| 425 | DestLOI.NumSignBits = std::min(DestLOI.NumSignBits, SrcLOI->NumSignBits); |
| 426 | DestLOI.KnownZero &= SrcLOI->KnownZero; |
| 427 | DestLOI.KnownOne &= SrcLOI->KnownOne; |
| 428 | } |
| 429 | } |
| 430 | |
Devang Patel | 9aee335 | 2011-09-08 22:59:09 +0000 | [diff] [blame] | 431 | /// setArgumentFrameIndex - Record frame index for the byval |
Devang Patel | 0b48ead | 2010-08-31 22:22:42 +0000 | [diff] [blame] | 432 | /// argument. This overrides previous frame index entry for this argument, |
| 433 | /// if any. |
Devang Patel | 9aee335 | 2011-09-08 22:59:09 +0000 | [diff] [blame] | 434 | void FunctionLoweringInfo::setArgumentFrameIndex(const Argument *A, |
Eric Christopher | 5b13ed1 | 2012-02-24 01:59:01 +0000 | [diff] [blame] | 435 | int FI) { |
Devang Patel | 0b48ead | 2010-08-31 22:22:42 +0000 | [diff] [blame] | 436 | ByValArgFrameIndexMap[A] = FI; |
| 437 | } |
Eric Christopher | 471e422 | 2011-06-08 23:55:35 +0000 | [diff] [blame] | 438 | |
Devang Patel | 9aee335 | 2011-09-08 22:59:09 +0000 | [diff] [blame] | 439 | /// getArgumentFrameIndex - Get frame index for the byval argument. |
Devang Patel | 0b48ead | 2010-08-31 22:22:42 +0000 | [diff] [blame] | 440 | /// If the argument does not have any assigned frame index then 0 is |
| 441 | /// returned. |
Devang Patel | 9aee335 | 2011-09-08 22:59:09 +0000 | [diff] [blame] | 442 | int FunctionLoweringInfo::getArgumentFrameIndex(const Argument *A) { |
Eric Christopher | 471e422 | 2011-06-08 23:55:35 +0000 | [diff] [blame] | 443 | DenseMap<const Argument *, int>::iterator I = |
Devang Patel | 0b48ead | 2010-08-31 22:22:42 +0000 | [diff] [blame] | 444 | ByValArgFrameIndexMap.find(A); |
| 445 | if (I != ByValArgFrameIndexMap.end()) |
| 446 | return I->second; |
Eric Christopher | 0822e01 | 2012-02-23 03:39:43 +0000 | [diff] [blame] | 447 | DEBUG(dbgs() << "Argument does not have assigned frame index!\n"); |
Devang Patel | 0b48ead | 2010-08-31 22:22:42 +0000 | [diff] [blame] | 448 | return 0; |
| 449 | } |
| 450 | |
Michael J. Spencer | c9c137b | 2012-02-22 19:06:13 +0000 | [diff] [blame] | 451 | /// ComputeUsesVAFloatArgument - Determine if any floating-point values are |
| 452 | /// being passed to this variadic function, and set the MachineModuleInfo's |
| 453 | /// usesVAFloatArgument flag if so. This flag is used to emit an undefined |
| 454 | /// reference to _fltused on Windows, which will link in MSVCRT's |
| 455 | /// floating-point support. |
| 456 | void llvm::ComputeUsesVAFloatArgument(const CallInst &I, |
| 457 | MachineModuleInfo *MMI) |
| 458 | { |
| 459 | FunctionType *FT = cast<FunctionType>( |
| 460 | I.getCalledValue()->getType()->getContainedType(0)); |
| 461 | if (FT->isVarArg() && !MMI->usesVAFloatArgument()) { |
| 462 | for (unsigned i = 0, e = I.getNumArgOperands(); i != e; ++i) { |
| 463 | Type* T = I.getArgOperand(i)->getType(); |
| 464 | for (po_iterator<Type*> i = po_begin(T), e = po_end(T); |
| 465 | i != e; ++i) { |
| 466 | if (i->isFloatingPointTy()) { |
| 467 | MMI->setUsesVAFloatArgument(true); |
| 468 | return; |
| 469 | } |
| 470 | } |
| 471 | } |
| 472 | } |
| 473 | } |
| 474 | |
Bill Wendling | 2ac0e6b | 2011-08-17 21:56:44 +0000 | [diff] [blame] | 475 | /// AddLandingPadInfo - Extract the exception handling information from the |
| 476 | /// landingpad instruction and add them to the specified machine module info. |
| 477 | void llvm::AddLandingPadInfo(const LandingPadInst &I, MachineModuleInfo &MMI, |
| 478 | MachineBasicBlock *MBB) { |
| 479 | MMI.addPersonality(MBB, |
| 480 | cast<Function>(I.getPersonalityFn()->stripPointerCasts())); |
| 481 | |
| 482 | if (I.isCleanup()) |
| 483 | MMI.addCleanup(MBB); |
| 484 | |
| 485 | // FIXME: New EH - Add the clauses in reverse order. This isn't 100% correct, |
| 486 | // but we need to do it this way because of how the DWARF EH emitter |
| 487 | // processes the clauses. |
| 488 | for (unsigned i = I.getNumClauses(); i != 0; --i) { |
| 489 | Value *Val = I.getClause(i - 1); |
| 490 | if (I.isCatch(i - 1)) { |
| 491 | MMI.addCatchTypeInfo(MBB, |
Stephen Hines | 37ed9c1 | 2014-12-01 14:51:49 -0800 | [diff] [blame] | 492 | dyn_cast<GlobalValue>(Val->stripPointerCasts())); |
Bill Wendling | 2ac0e6b | 2011-08-17 21:56:44 +0000 | [diff] [blame] | 493 | } else { |
| 494 | // Add filters in a list. |
| 495 | Constant *CVal = cast<Constant>(Val); |
Stephen Hines | 37ed9c1 | 2014-12-01 14:51:49 -0800 | [diff] [blame] | 496 | SmallVector<const GlobalValue*, 4> FilterList; |
Bill Wendling | 2ac0e6b | 2011-08-17 21:56:44 +0000 | [diff] [blame] | 497 | for (User::op_iterator |
| 498 | II = CVal->op_begin(), IE = CVal->op_end(); II != IE; ++II) |
Stephen Hines | 37ed9c1 | 2014-12-01 14:51:49 -0800 | [diff] [blame] | 499 | FilterList.push_back(cast<GlobalValue>((*II)->stripPointerCasts())); |
Bill Wendling | 2ac0e6b | 2011-08-17 21:56:44 +0000 | [diff] [blame] | 500 | |
| 501 | MMI.addFilterTypeInfo(MBB, FilterList); |
| 502 | } |
| 503 | } |
| 504 | } |