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