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 | |
| 15 | #define DEBUG_TYPE "function-lowering-info" |
| 16 | #include "FunctionLoweringInfo.h" |
Dan Gohman | 6277eb2 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 17 | #include "llvm/DerivedTypes.h" |
| 18 | #include "llvm/Function.h" |
| 19 | #include "llvm/Instructions.h" |
Dan Gohman | 5fca8b1 | 2009-11-23 18:12:11 +0000 | [diff] [blame] | 20 | #include "llvm/IntrinsicInst.h" |
Dan Gohman | 6277eb2 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 21 | #include "llvm/LLVMContext.h" |
| 22 | #include "llvm/Module.h" |
Dan Gohman | 5eb6d65 | 2010-04-21 01:22:34 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/Analysis.h" |
Dan Gohman | 6277eb2 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 24 | #include "llvm/CodeGen/MachineFunction.h" |
| 25 | #include "llvm/CodeGen/MachineFrameInfo.h" |
| 26 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| 27 | #include "llvm/CodeGen/MachineModuleInfo.h" |
| 28 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Dan Gohman | 6277eb2 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 29 | #include "llvm/Target/TargetRegisterInfo.h" |
| 30 | #include "llvm/Target/TargetData.h" |
| 31 | #include "llvm/Target/TargetFrameInfo.h" |
| 32 | #include "llvm/Target/TargetInstrInfo.h" |
Dan Gohman | 6277eb2 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 33 | #include "llvm/Target/TargetLowering.h" |
| 34 | #include "llvm/Target/TargetOptions.h" |
Dan Gohman | 6277eb2 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 35 | #include "llvm/Support/Debug.h" |
| 36 | #include "llvm/Support/ErrorHandling.h" |
| 37 | #include "llvm/Support/MathExtras.h" |
Dan Gohman | 6277eb2 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 38 | #include <algorithm> |
| 39 | using namespace llvm; |
| 40 | |
Dan Gohman | 6277eb2 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 41 | /// isUsedOutsideOfDefiningBlock - Return true if this instruction is used by |
| 42 | /// PHI nodes or outside of the basic block that defines it, or used by a |
| 43 | /// switch or atomic instruction, which may expand to multiple basic blocks. |
Dan Gohman | ae541aa | 2010-04-15 04:33:49 +0000 | [diff] [blame] | 44 | static bool isUsedOutsideOfDefiningBlock(const Instruction *I) { |
Dan Gohman | d84e806 | 2010-04-20 14:50:13 +0000 | [diff] [blame] | 45 | if (I->use_empty()) return false; |
Dan Gohman | 6277eb2 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 46 | if (isa<PHINode>(I)) return true; |
Dan Gohman | ae541aa | 2010-04-15 04:33:49 +0000 | [diff] [blame] | 47 | const BasicBlock *BB = I->getParent(); |
| 48 | for (Value::const_use_iterator UI = I->use_begin(), E = I->use_end(); |
| 49 | UI != E; ++UI) |
Dan Gohman | 6277eb2 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 50 | if (cast<Instruction>(*UI)->getParent() != BB || isa<PHINode>(*UI)) |
| 51 | return true; |
| 52 | return false; |
| 53 | } |
| 54 | |
| 55 | /// isOnlyUsedInEntryBlock - If the specified argument is only used in the |
| 56 | /// entry block, return true. This includes arguments used by switches, since |
| 57 | /// the switch may expand into multiple basic blocks. |
Dan Gohman | ae541aa | 2010-04-15 04:33:49 +0000 | [diff] [blame] | 58 | static bool isOnlyUsedInEntryBlock(const Argument *A, bool EnableFastISel) { |
Dan Gohman | 6277eb2 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 59 | // With FastISel active, we may be splitting blocks, so force creation |
| 60 | // of virtual registers for all non-dead arguments. |
Dan Gohman | d725f04 | 2010-05-01 02:44:23 +0000 | [diff] [blame] | 61 | if (EnableFastISel) |
Dan Gohman | 6277eb2 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 62 | return A->use_empty(); |
| 63 | |
Dan Gohman | ae541aa | 2010-04-15 04:33:49 +0000 | [diff] [blame] | 64 | const BasicBlock *Entry = A->getParent()->begin(); |
| 65 | for (Value::const_use_iterator UI = A->use_begin(), E = A->use_end(); |
| 66 | UI != E; ++UI) |
Dan Gohman | 6277eb2 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 67 | if (cast<Instruction>(*UI)->getParent() != Entry || isa<SwitchInst>(*UI)) |
| 68 | return false; // Use not in entry block. |
| 69 | return true; |
| 70 | } |
| 71 | |
Dan Gohman | d858e90 | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 72 | FunctionLoweringInfo::FunctionLoweringInfo(const TargetLowering &tli) |
Dan Gohman | 6277eb2 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 73 | : TLI(tli) { |
| 74 | } |
| 75 | |
Dan Gohman | 7451d3e | 2010-05-29 17:03:36 +0000 | [diff] [blame] | 76 | void FunctionLoweringInfo::set(const Function &fn, MachineFunction &mf) { |
Dan Gohman | 6277eb2 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 77 | Fn = &fn; |
| 78 | MF = &mf; |
| 79 | RegInfo = &MF->getRegInfo(); |
| 80 | |
| 81 | // Create a vreg for each argument register that is not dead and is used |
| 82 | // outside of the entry block for the function. |
Dan Gohman | ae541aa | 2010-04-15 04:33:49 +0000 | [diff] [blame] | 83 | for (Function::const_arg_iterator AI = Fn->arg_begin(), E = Fn->arg_end(); |
Dan Gohman | 6277eb2 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 84 | AI != E; ++AI) |
| 85 | if (!isOnlyUsedInEntryBlock(AI, EnableFastISel)) |
| 86 | InitializeRegForValue(AI); |
| 87 | |
| 88 | // Initialize the mapping of values to registers. This is only set up for |
| 89 | // instruction values that are used outside of the block that defines |
| 90 | // them. |
Dan Gohman | ae541aa | 2010-04-15 04:33:49 +0000 | [diff] [blame] | 91 | Function::const_iterator BB = Fn->begin(), EB = Fn->end(); |
| 92 | for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I) |
| 93 | if (const AllocaInst *AI = dyn_cast<AllocaInst>(I)) |
| 94 | if (const ConstantInt *CUI = dyn_cast<ConstantInt>(AI->getArraySize())) { |
Dan Gohman | 6277eb2 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 95 | const Type *Ty = AI->getAllocatedType(); |
| 96 | uint64_t TySize = TLI.getTargetData()->getTypeAllocSize(Ty); |
| 97 | unsigned Align = |
| 98 | std::max((unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty), |
| 99 | AI->getAlignment()); |
| 100 | |
| 101 | TySize *= CUI->getZExtValue(); // Get total allocated size. |
| 102 | if (TySize == 0) TySize = 1; // Don't create zero-sized stack objects. |
| 103 | StaticAllocaMap[AI] = |
| 104 | MF->getFrameInfo()->CreateStackObject(TySize, Align, false); |
| 105 | } |
| 106 | |
| 107 | for (; BB != EB; ++BB) |
Dan Gohman | ae541aa | 2010-04-15 04:33:49 +0000 | [diff] [blame] | 108 | for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I) |
Dan Gohman | d84e806 | 2010-04-20 14:50:13 +0000 | [diff] [blame] | 109 | if (isUsedOutsideOfDefiningBlock(I)) |
Dan Gohman | 6277eb2 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 110 | if (!isa<AllocaInst>(I) || |
| 111 | !StaticAllocaMap.count(cast<AllocaInst>(I))) |
| 112 | InitializeRegForValue(I); |
| 113 | |
| 114 | // Create an initial MachineBasicBlock for each LLVM BasicBlock in F. This |
| 115 | // also creates the initial PHI MachineInstrs, though none of the input |
| 116 | // operands are populated. |
Dan Gohman | d0d8275 | 2010-04-14 16:30:40 +0000 | [diff] [blame] | 117 | for (BB = Fn->begin(); BB != EB; ++BB) { |
Dan Gohman | 6277eb2 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 118 | MachineBasicBlock *MBB = mf.CreateMachineBasicBlock(BB); |
| 119 | MBBMap[BB] = MBB; |
| 120 | MF->push_back(MBB); |
| 121 | |
| 122 | // Transfer the address-taken flag. This is necessary because there could |
| 123 | // be multiple MachineBasicBlocks corresponding to one BasicBlock, and only |
| 124 | // the first one should be marked. |
| 125 | if (BB->hasAddressTaken()) |
| 126 | MBB->setHasAddressTaken(); |
| 127 | |
| 128 | // Create Machine PHI nodes for LLVM PHI nodes, lowering them as |
| 129 | // appropriate. |
Dan Gohman | 3f1403f | 2010-04-20 14:46:25 +0000 | [diff] [blame] | 130 | for (BasicBlock::const_iterator I = BB->begin(); |
| 131 | const PHINode *PN = dyn_cast<PHINode>(I); ++I) { |
| 132 | if (PN->use_empty()) continue; |
Dan Gohman | 6277eb2 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 133 | |
Dan Gohman | c025c85 | 2010-04-20 14:48:02 +0000 | [diff] [blame] | 134 | DebugLoc DL = PN->getDebugLoc(); |
Dan Gohman | 6277eb2 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 135 | unsigned PHIReg = ValueMap[PN]; |
| 136 | assert(PHIReg && "PHI node does not have an assigned virtual register!"); |
| 137 | |
| 138 | SmallVector<EVT, 4> ValueVTs; |
| 139 | ComputeValueVTs(TLI, PN->getType(), ValueVTs); |
| 140 | for (unsigned vti = 0, vte = ValueVTs.size(); vti != vte; ++vti) { |
| 141 | EVT VT = ValueVTs[vti]; |
| 142 | unsigned NumRegisters = TLI.getNumRegisters(Fn->getContext(), VT); |
| 143 | const TargetInstrInfo *TII = MF->getTarget().getInstrInfo(); |
| 144 | for (unsigned i = 0; i != NumRegisters; ++i) |
Chris Lattner | 518bb53 | 2010-02-09 19:54:29 +0000 | [diff] [blame] | 145 | BuildMI(MBB, DL, TII->get(TargetOpcode::PHI), PHIReg + i); |
Dan Gohman | 6277eb2 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 146 | PHIReg += NumRegisters; |
| 147 | } |
| 148 | } |
| 149 | } |
Dan Gohman | de4c0a7 | 2010-04-14 16:32:56 +0000 | [diff] [blame] | 150 | |
| 151 | // Mark landing pad blocks. |
| 152 | for (BB = Fn->begin(); BB != EB; ++BB) |
Dan Gohman | ae541aa | 2010-04-15 04:33:49 +0000 | [diff] [blame] | 153 | if (const InvokeInst *Invoke = dyn_cast<InvokeInst>(BB->getTerminator())) |
Dan Gohman | de4c0a7 | 2010-04-14 16:32:56 +0000 | [diff] [blame] | 154 | MBBMap[Invoke->getSuccessor(1)]->setIsLandingPad(); |
Dan Gohman | 6277eb2 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | /// clear - Clear out all the function-specific state. This returns this |
| 158 | /// FunctionLoweringInfo to an empty state, ready to be used for a |
| 159 | /// different function. |
| 160 | void FunctionLoweringInfo::clear() { |
Dan Gohman | 0e02672 | 2010-04-14 17:11:23 +0000 | [diff] [blame] | 161 | assert(CatchInfoFound.size() == CatchInfoLost.size() && |
| 162 | "Not all catch info was assigned to a landing pad!"); |
| 163 | |
Dan Gohman | 6277eb2 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 164 | MBBMap.clear(); |
| 165 | ValueMap.clear(); |
| 166 | StaticAllocaMap.clear(); |
| 167 | #ifndef NDEBUG |
| 168 | CatchInfoLost.clear(); |
| 169 | CatchInfoFound.clear(); |
| 170 | #endif |
| 171 | LiveOutRegInfo.clear(); |
Evan Cheng | 2ad0fcf | 2010-04-28 23:08:54 +0000 | [diff] [blame] | 172 | ArgDbgValues.clear(); |
Dan Gohman | 6277eb2 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | unsigned FunctionLoweringInfo::MakeReg(EVT VT) { |
| 176 | return RegInfo->createVirtualRegister(TLI.getRegClassFor(VT)); |
| 177 | } |
| 178 | |
Dan Gohman | ffda6ba | 2010-07-01 03:55:39 +0000 | [diff] [blame^] | 179 | /// CreateReg - Allocate the appropriate number of virtual registers of |
Dan Gohman | 6277eb2 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 180 | /// the correctly promoted or expanded types. Assign these registers |
| 181 | /// consecutive vreg numbers and return the first assigned number. |
| 182 | /// |
| 183 | /// In the case that the given value has struct or array type, this function |
| 184 | /// will assign registers for each member or element. |
| 185 | /// |
Dan Gohman | ffda6ba | 2010-07-01 03:55:39 +0000 | [diff] [blame^] | 186 | unsigned FunctionLoweringInfo::CreateReg(const Type *Ty) { |
Dan Gohman | 6277eb2 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 187 | SmallVector<EVT, 4> ValueVTs; |
Dan Gohman | ffda6ba | 2010-07-01 03:55:39 +0000 | [diff] [blame^] | 188 | ComputeValueVTs(TLI, Ty, ValueVTs); |
Dan Gohman | 6277eb2 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 189 | |
| 190 | unsigned FirstReg = 0; |
| 191 | for (unsigned Value = 0, e = ValueVTs.size(); Value != e; ++Value) { |
| 192 | EVT ValueVT = ValueVTs[Value]; |
Dan Gohman | ffda6ba | 2010-07-01 03:55:39 +0000 | [diff] [blame^] | 193 | EVT RegisterVT = TLI.getRegisterType(Ty->getContext(), ValueVT); |
Dan Gohman | 6277eb2 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 194 | |
Dan Gohman | ffda6ba | 2010-07-01 03:55:39 +0000 | [diff] [blame^] | 195 | unsigned NumRegs = TLI.getNumRegisters(Ty->getContext(), ValueVT); |
Dan Gohman | 6277eb2 | 2009-11-23 17:16:22 +0000 | [diff] [blame] | 196 | for (unsigned i = 0; i != NumRegs; ++i) { |
| 197 | unsigned R = MakeReg(RegisterVT); |
| 198 | if (!FirstReg) FirstReg = R; |
| 199 | } |
| 200 | } |
| 201 | return FirstReg; |
| 202 | } |
Dan Gohman | 66336ed | 2009-11-23 17:42:46 +0000 | [diff] [blame] | 203 | |
Dan Gohman | 66336ed | 2009-11-23 17:42:46 +0000 | [diff] [blame] | 204 | /// AddCatchInfo - Extract the personality and type infos from an eh.selector |
| 205 | /// call, and add them to the specified machine basic block. |
Dan Gohman | 2520864 | 2010-04-14 19:53:31 +0000 | [diff] [blame] | 206 | void llvm::AddCatchInfo(const CallInst &I, MachineModuleInfo *MMI, |
Dan Gohman | 66336ed | 2009-11-23 17:42:46 +0000 | [diff] [blame] | 207 | MachineBasicBlock *MBB) { |
| 208 | // Inform the MachineModuleInfo of the personality for this landing pad. |
Gabor Greif | 1518444 | 2010-06-25 08:24:59 +0000 | [diff] [blame] | 209 | const ConstantExpr *CE = cast<ConstantExpr>(I.getArgOperand(1)); |
Dan Gohman | 66336ed | 2009-11-23 17:42:46 +0000 | [diff] [blame] | 210 | assert(CE->getOpcode() == Instruction::BitCast && |
| 211 | isa<Function>(CE->getOperand(0)) && |
| 212 | "Personality should be a function"); |
| 213 | MMI->addPersonality(MBB, cast<Function>(CE->getOperand(0))); |
| 214 | |
| 215 | // Gather all the type infos for this landing pad and pass them along to |
| 216 | // MachineModuleInfo. |
Dan Gohman | 46510a7 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 217 | std::vector<const GlobalVariable *> TyInfo; |
Gabor Greif | e767e6b | 2010-06-30 13:45:50 +0000 | [diff] [blame] | 218 | unsigned N = I.getNumArgOperands(); |
Dan Gohman | 66336ed | 2009-11-23 17:42:46 +0000 | [diff] [blame] | 219 | |
Gabor Greif | e767e6b | 2010-06-30 13:45:50 +0000 | [diff] [blame] | 220 | for (unsigned i = N - 1; i > 1; --i) { |
| 221 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(I.getArgOperand(i))) { |
Dan Gohman | 66336ed | 2009-11-23 17:42:46 +0000 | [diff] [blame] | 222 | unsigned FilterLength = CI->getZExtValue(); |
| 223 | unsigned FirstCatch = i + FilterLength + !FilterLength; |
Gabor Greif | e767e6b | 2010-06-30 13:45:50 +0000 | [diff] [blame] | 224 | assert(FirstCatch <= N && "Invalid filter length"); |
Dan Gohman | 66336ed | 2009-11-23 17:42:46 +0000 | [diff] [blame] | 225 | |
| 226 | if (FirstCatch < N) { |
| 227 | TyInfo.reserve(N - FirstCatch); |
| 228 | for (unsigned j = FirstCatch; j < N; ++j) |
Gabor Greif | e767e6b | 2010-06-30 13:45:50 +0000 | [diff] [blame] | 229 | TyInfo.push_back(ExtractTypeInfo(I.getArgOperand(j))); |
Dan Gohman | 66336ed | 2009-11-23 17:42:46 +0000 | [diff] [blame] | 230 | MMI->addCatchTypeInfo(MBB, TyInfo); |
| 231 | TyInfo.clear(); |
| 232 | } |
| 233 | |
| 234 | if (!FilterLength) { |
| 235 | // Cleanup. |
| 236 | MMI->addCleanup(MBB); |
| 237 | } else { |
| 238 | // Filter. |
| 239 | TyInfo.reserve(FilterLength - 1); |
| 240 | for (unsigned j = i + 1; j < FirstCatch; ++j) |
Gabor Greif | e767e6b | 2010-06-30 13:45:50 +0000 | [diff] [blame] | 241 | TyInfo.push_back(ExtractTypeInfo(I.getArgOperand(j))); |
Dan Gohman | 66336ed | 2009-11-23 17:42:46 +0000 | [diff] [blame] | 242 | MMI->addFilterTypeInfo(MBB, TyInfo); |
| 243 | TyInfo.clear(); |
| 244 | } |
| 245 | |
| 246 | N = i; |
| 247 | } |
| 248 | } |
| 249 | |
Gabor Greif | e767e6b | 2010-06-30 13:45:50 +0000 | [diff] [blame] | 250 | if (N > 2) { |
| 251 | TyInfo.reserve(N - 2); |
| 252 | for (unsigned j = 2; j < N; ++j) |
| 253 | TyInfo.push_back(ExtractTypeInfo(I.getArgOperand(j))); |
Dan Gohman | 66336ed | 2009-11-23 17:42:46 +0000 | [diff] [blame] | 254 | MMI->addCatchTypeInfo(MBB, TyInfo); |
| 255 | } |
| 256 | } |
| 257 | |
Dan Gohman | 2520864 | 2010-04-14 19:53:31 +0000 | [diff] [blame] | 258 | void llvm::CopyCatchInfo(const BasicBlock *SrcBB, const BasicBlock *DestBB, |
Dan Gohman | 5fca8b1 | 2009-11-23 18:12:11 +0000 | [diff] [blame] | 259 | MachineModuleInfo *MMI, FunctionLoweringInfo &FLI) { |
Dan Gohman | 2520864 | 2010-04-14 19:53:31 +0000 | [diff] [blame] | 260 | for (BasicBlock::const_iterator I = SrcBB->begin(), E = --SrcBB->end(); |
| 261 | I != E; ++I) |
| 262 | if (const EHSelectorInst *EHSel = dyn_cast<EHSelectorInst>(I)) { |
Dan Gohman | 5fca8b1 | 2009-11-23 18:12:11 +0000 | [diff] [blame] | 263 | // Apply the catch info to DestBB. |
| 264 | AddCatchInfo(*EHSel, MMI, FLI.MBBMap[DestBB]); |
| 265 | #ifndef NDEBUG |
| 266 | if (!FLI.MBBMap[SrcBB]->isLandingPad()) |
| 267 | FLI.CatchInfoFound.insert(EHSel); |
| 268 | #endif |
| 269 | } |
| 270 | } |