comparison lib/CodeGen/SelectionDAG/FunctionLoweringInfo.cpp @ 100:7d135dc70f03 LLVM 3.9

LLVM 3.9
author Miyagi Mitsuki <e135756@ie.u-ryukyu.ac.jp>
date Tue, 26 Jan 2016 22:53:40 +0900
parents afa8332a0e37
children 1172e4bd9c6f
comparison
equal deleted inserted replaced
96:6418606d0ead 100:7d135dc70f03
85 Fn = &fn; 85 Fn = &fn;
86 MF = &mf; 86 MF = &mf;
87 TLI = MF->getSubtarget().getTargetLowering(); 87 TLI = MF->getSubtarget().getTargetLowering();
88 RegInfo = &MF->getRegInfo(); 88 RegInfo = &MF->getRegInfo();
89 MachineModuleInfo &MMI = MF->getMMI(); 89 MachineModuleInfo &MMI = MF->getMMI();
90 const TargetFrameLowering *TFI = MF->getSubtarget().getFrameLowering();
90 91
91 // Check whether the function can return without sret-demotion. 92 // Check whether the function can return without sret-demotion.
92 SmallVector<ISD::OutputArg, 4> Outs; 93 SmallVector<ISD::OutputArg, 4> Outs;
93 GetReturnInfo(Fn->getReturnType(), Fn->getAttributes(), Outs, *TLI, 94 GetReturnInfo(Fn->getReturnType(), Fn->getAttributes(), Outs, *TLI,
94 mf.getDataLayout()); 95 mf.getDataLayout());
101 Function::const_iterator BB = Fn->begin(), EB = Fn->end(); 102 Function::const_iterator BB = Fn->begin(), EB = Fn->end();
102 for (; BB != EB; ++BB) 103 for (; BB != EB; ++BB)
103 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); 104 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end();
104 I != E; ++I) { 105 I != E; ++I) {
105 if (const AllocaInst *AI = dyn_cast<AllocaInst>(I)) { 106 if (const AllocaInst *AI = dyn_cast<AllocaInst>(I)) {
106 // Static allocas can be folded into the initial stack frame adjustment. 107 Type *Ty = AI->getAllocatedType();
107 if (AI->isStaticAlloca()) { 108 unsigned Align =
109 std::max((unsigned)MF->getDataLayout().getPrefTypeAlignment(Ty),
110 AI->getAlignment());
111 unsigned StackAlign = TFI->getStackAlignment();
112
113 // Static allocas can be folded into the initial stack frame
114 // adjustment. For targets that don't realign the stack, don't
115 // do this if there is an extra alignment requirement.
116 if (AI->isStaticAlloca() &&
117 (TFI->isStackRealignable() || (Align <= StackAlign))) {
108 const ConstantInt *CUI = cast<ConstantInt>(AI->getArraySize()); 118 const ConstantInt *CUI = cast<ConstantInt>(AI->getArraySize());
109 Type *Ty = AI->getAllocatedType();
110 uint64_t TySize = MF->getDataLayout().getTypeAllocSize(Ty); 119 uint64_t TySize = MF->getDataLayout().getTypeAllocSize(Ty);
111 unsigned Align =
112 std::max((unsigned)MF->getDataLayout().getPrefTypeAlignment(Ty),
113 AI->getAlignment());
114 120
115 TySize *= CUI->getZExtValue(); // Get total allocated size. 121 TySize *= CUI->getZExtValue(); // Get total allocated size.
116 if (TySize == 0) TySize = 1; // Don't create zero-sized stack objects. 122 if (TySize == 0) TySize = 1; // Don't create zero-sized stack objects.
117 123
118 StaticAllocaMap[AI] = 124 StaticAllocaMap[AI] =
119 MF->getFrameInfo()->CreateStackObject(TySize, Align, false, AI); 125 MF->getFrameInfo()->CreateStackObject(TySize, Align, false, AI);
120
121 } else { 126 } else {
122 unsigned Align = 127 // FIXME: Overaligned static allocas should be grouped into
123 std::max((unsigned)MF->getDataLayout().getPrefTypeAlignment( 128 // a single dynamic allocation instead of using a separate
124 AI->getAllocatedType()), 129 // stack allocation for each one.
125 AI->getAlignment());
126 unsigned StackAlign =
127 MF->getSubtarget().getFrameLowering()->getStackAlignment();
128 if (Align <= StackAlign) 130 if (Align <= StackAlign)
129 Align = 0; 131 Align = 0;
130 // Inform the Frame Information that we have variable-sized objects. 132 // Inform the Frame Information that we have variable-sized objects.
131 MF->getFrameInfo()->CreateVariableSizedObject(Align ? Align : 1, AI); 133 MF->getFrameInfo()->CreateVariableSizedObject(Align ? Align : 1, AI);
132 } 134 }
133 } 135 }
134 136
135 // Look for inline asm that clobbers the SP register. 137 // Look for inline asm that clobbers the SP register.
136 if (isa<CallInst>(I) || isa<InvokeInst>(I)) { 138 if (isa<CallInst>(I) || isa<InvokeInst>(I)) {
137 ImmutableCallSite CS(I); 139 ImmutableCallSite CS(&*I);
138 if (isa<InlineAsm>(CS.getCalledValue())) { 140 if (isa<InlineAsm>(CS.getCalledValue())) {
139 unsigned SP = TLI->getStackPointerRegisterToSaveRestore(); 141 unsigned SP = TLI->getStackPointerRegisterToSaveRestore();
140 const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo(); 142 const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo();
141 std::vector<TargetLowering::AsmOperandInfo> Ops = 143 std::vector<TargetLowering::AsmOperandInfo> Ops =
142 TLI->ParseConstraints(Fn->getParent()->getDataLayout(), TRI, CS); 144 TLI->ParseConstraints(Fn->getParent()->getDataLayout(), TRI, CS);
161 if (const auto *II = dyn_cast<IntrinsicInst>(I)) { 163 if (const auto *II = dyn_cast<IntrinsicInst>(I)) {
162 if (II->getIntrinsicID() == Intrinsic::vastart) 164 if (II->getIntrinsicID() == Intrinsic::vastart)
163 MF->getFrameInfo()->setHasVAStart(true); 165 MF->getFrameInfo()->setHasVAStart(true);
164 } 166 }
165 167
166 // If we have a musttail call in a variadic funciton, we need to ensure we 168 // If we have a musttail call in a variadic function, we need to ensure we
167 // forward implicit register parameters. 169 // forward implicit register parameters.
168 if (const auto *CI = dyn_cast<CallInst>(I)) { 170 if (const auto *CI = dyn_cast<CallInst>(I)) {
169 if (CI->isMustTailCall() && Fn->isVarArg()) 171 if (CI->isMustTailCall() && Fn->isVarArg())
170 MF->getFrameInfo()->setHasMustTailInVarArgFunc(true); 172 MF->getFrameInfo()->setHasMustTailInVarArgFunc(true);
171 } 173 }
172 174
173 // Mark values used outside their block as exported, by allocating 175 // Mark values used outside their block as exported, by allocating
174 // a virtual register for them. 176 // a virtual register for them.
175 if (isUsedOutsideOfDefiningBlock(I)) 177 if (isUsedOutsideOfDefiningBlock(&*I))
176 if (!isa<AllocaInst>(I) || 178 if (!isa<AllocaInst>(I) || !StaticAllocaMap.count(cast<AllocaInst>(I)))
177 !StaticAllocaMap.count(cast<AllocaInst>(I))) 179 InitializeRegForValue(&*I);
178 InitializeRegForValue(I);
179 180
180 // Collect llvm.dbg.declare information. This is done now instead of 181 // Collect llvm.dbg.declare information. This is done now instead of
181 // during the initial isel pass through the IR so that it is done 182 // during the initial isel pass through the IR so that it is done
182 // in a predictable order. 183 // in a predictable order.
183 if (const DbgDeclareInst *DI = dyn_cast<DbgDeclareInst>(I)) { 184 if (const DbgDeclareInst *DI = dyn_cast<DbgDeclareInst>(I)) {
203 } 204 }
204 } 205 }
205 } 206 }
206 207
207 // Decide the preferred extend type for a value. 208 // Decide the preferred extend type for a value.
208 PreferredExtendType[I] = getPreferredExtendForValue(I); 209 PreferredExtendType[&*I] = getPreferredExtendForValue(&*I);
209 } 210 }
210 211
211 // Create an initial MachineBasicBlock for each LLVM BasicBlock in F. This 212 // Create an initial MachineBasicBlock for each LLVM BasicBlock in F. This
212 // also creates the initial PHI MachineInstrs, though none of the input 213 // also creates the initial PHI MachineInstrs, though none of the input
213 // operands are populated. 214 // operands are populated.
214 for (BB = Fn->begin(); BB != EB; ++BB) { 215 for (BB = Fn->begin(); BB != EB; ++BB) {
215 // Don't create MachineBasicBlocks for imaginary EH pad blocks. These blocks 216 // Don't create MachineBasicBlocks for imaginary EH pad blocks. These blocks
216 // are really data, and no instructions can live here. 217 // are really data, and no instructions can live here.
217 if (BB->isEHPad()) { 218 if (BB->isEHPad()) {
218 const Instruction *I = BB->getFirstNonPHI(); 219 const Instruction *I = BB->getFirstNonPHI();
219 // FIXME: Don't mark SEH functions without __finally blocks as having 220 // If this is a non-landingpad EH pad, mark this function as using
220 // funclets. 221 // funclets.
221 if (!isa<LandingPadInst>(I)) 222 // FIXME: SEH catchpads do not create funclets, so we could avoid setting
223 // this in such cases in order to improve frame layout.
224 if (!isa<LandingPadInst>(I)) {
222 MMI.setHasEHFunclets(true); 225 MMI.setHasEHFunclets(true);
223 if (isa<CatchEndPadInst>(I) || isa<CleanupEndPadInst>(I)) { 226 MF->getFrameInfo()->setHasOpaqueSPAdjustment(true);
227 }
228 if (isa<CatchSwitchInst>(I)) {
224 assert(&*BB->begin() == I && 229 assert(&*BB->begin() == I &&
225 "WinEHPrepare failed to remove PHIs from imaginary BBs"); 230 "WinEHPrepare failed to remove PHIs from imaginary BBs");
226 continue; 231 continue;
227 } 232 }
228 if (isa<CatchPadInst>(I) || isa<CleanupPadInst>(I)) 233 if (isa<FuncletPadInst>(I))
229 assert(&*BB->begin() == I && "WinEHPrepare failed to demote PHIs"); 234 assert(&*BB->begin() == I && "WinEHPrepare failed to demote PHIs");
230 } 235 }
231 236
232 MachineBasicBlock *MBB = mf.CreateMachineBasicBlock(BB); 237 MachineBasicBlock *MBB = mf.CreateMachineBasicBlock(&*BB);
233 MBBMap[BB] = MBB; 238 MBBMap[&*BB] = MBB;
234 MF->push_back(MBB); 239 MF->push_back(MBB);
235 240
236 // Transfer the address-taken flag. This is necessary because there could 241 // Transfer the address-taken flag. This is necessary because there could
237 // be multiple MachineBasicBlocks corresponding to one BasicBlock, and only 242 // be multiple MachineBasicBlocks corresponding to one BasicBlock, and only
238 // the first one should be marked. 243 // the first one should be marked.
268 273
269 // Mark landing pad blocks. 274 // Mark landing pad blocks.
270 SmallVector<const LandingPadInst *, 4> LPads; 275 SmallVector<const LandingPadInst *, 4> LPads;
271 for (BB = Fn->begin(); BB != EB; ++BB) { 276 for (BB = Fn->begin(); BB != EB; ++BB) {
272 const Instruction *FNP = BB->getFirstNonPHI(); 277 const Instruction *FNP = BB->getFirstNonPHI();
273 if (BB->isEHPad() && MBBMap.count(BB)) 278 if (BB->isEHPad() && MBBMap.count(&*BB))
274 MBBMap[BB]->setIsEHPad(); 279 MBBMap[&*BB]->setIsEHPad();
275 if (const auto *LPI = dyn_cast<LandingPadInst>(FNP)) 280 if (const auto *LPI = dyn_cast<LandingPadInst>(FNP))
276 LPads.push_back(LPI); 281 LPads.push_back(LPI);
277 } 282 }
278 283
279 // If this personality uses funclets, we need to do a bit more work. 284 // If this personality uses funclets, we need to do a bit more work.
282 EHPersonality Personality = classifyEHPersonality(Fn->getPersonalityFn()); 287 EHPersonality Personality = classifyEHPersonality(Fn->getPersonalityFn());
283 if (!isFuncletEHPersonality(Personality)) 288 if (!isFuncletEHPersonality(Personality))
284 return; 289 return;
285 290
286 // Calculate state numbers if we haven't already. 291 // Calculate state numbers if we haven't already.
287 WinEHFuncInfo &EHInfo = MMI.getWinEHFuncInfo(&fn); 292 WinEHFuncInfo &EHInfo = *MF->getWinEHFuncInfo();
288 if (Personality == EHPersonality::MSVC_CXX) 293 if (Personality == EHPersonality::MSVC_CXX)
289 calculateWinCXXEHStateNumbers(&fn, EHInfo); 294 calculateWinCXXEHStateNumbers(&fn, EHInfo);
290 else if (isAsynchronousEHPersonality(Personality)) 295 else if (isAsynchronousEHPersonality(Personality))
291 calculateSEHStateNumbers(&fn, EHInfo); 296 calculateSEHStateNumbers(&fn, EHInfo);
292 else if (Personality == EHPersonality::CoreCLR) 297 else if (Personality == EHPersonality::CoreCLR)
293 calculateClrEHStateNumbers(&fn, EHInfo); 298 calculateClrEHStateNumbers(&fn, EHInfo);
294
295 calculateCatchReturnSuccessorColors(&fn, EHInfo);
296 299
297 // Map all BB references in the WinEH data to MBBs. 300 // Map all BB references in the WinEH data to MBBs.
298 for (WinEHTryBlockMapEntry &TBME : EHInfo.TryBlockMap) { 301 for (WinEHTryBlockMapEntry &TBME : EHInfo.TryBlockMap) {
299 for (WinEHHandlerType &H : TBME.HandlerArray) { 302 for (WinEHHandlerType &H : TBME.HandlerArray) {
300 if (H.CatchObj.Alloca) { 303 if (H.CatchObj.Alloca) {
316 } 319 }
317 for (ClrEHUnwindMapEntry &CME : EHInfo.ClrEHUnwindMap) { 320 for (ClrEHUnwindMapEntry &CME : EHInfo.ClrEHUnwindMap) {
318 const BasicBlock *BB = CME.Handler.get<const BasicBlock *>(); 321 const BasicBlock *BB = CME.Handler.get<const BasicBlock *>();
319 CME.Handler = MBBMap[BB]; 322 CME.Handler = MBBMap[BB];
320 } 323 }
321
322 // If there's an explicit EH registration node on the stack, record its
323 // frame index.
324 if (EHInfo.EHRegNode && EHInfo.EHRegNode->getParent()->getParent() == Fn) {
325 assert(StaticAllocaMap.count(EHInfo.EHRegNode));
326 EHInfo.EHRegNodeFrameIndex = StaticAllocaMap[EHInfo.EHRegNode];
327 }
328
329 // Copy the state numbers to LandingPadInfo for the current function, which
330 // could be a handler or the parent. This should happen for 32-bit SEH and
331 // C++ EH.
332 if (Personality == EHPersonality::MSVC_CXX ||
333 Personality == EHPersonality::MSVC_X86SEH) {
334 for (const LandingPadInst *LP : LPads) {
335 MachineBasicBlock *LPadMBB = MBBMap[LP->getParent()];
336 MMI.addWinEHState(LPadMBB, EHInfo.EHPadStateMap[LP]);
337 }
338 }
339 } 324 }
340 325
341 /// clear - Clear out all the function-specific state. This returns this 326 /// clear - Clear out all the function-specific state. This returns this
342 /// FunctionLoweringInfo to an empty state, ready to be used for a 327 /// FunctionLoweringInfo to an empty state, ready to be used for a
343 /// different function. 328 /// different function.
344 void FunctionLoweringInfo::clear() { 329 void FunctionLoweringInfo::clear() {
345 assert(CatchInfoFound.size() == CatchInfoLost.size() &&
346 "Not all catch info was assigned to a landing pad!");
347
348 MBBMap.clear(); 330 MBBMap.clear();
349 ValueMap.clear(); 331 ValueMap.clear();
350 StaticAllocaMap.clear(); 332 StaticAllocaMap.clear();
351 #ifndef NDEBUG
352 CatchInfoLost.clear();
353 CatchInfoFound.clear();
354 #endif
355 LiveOutRegInfo.clear(); 333 LiveOutRegInfo.clear();
356 VisitedBBs.clear(); 334 VisitedBBs.clear();
357 ArgDbgValues.clear(); 335 ArgDbgValues.clear();
358 ByValArgFrameIndexMap.clear(); 336 ByValArgFrameIndexMap.clear();
359 RegFixups.clear(); 337 RegFixups.clear();