Index: include/llvm/CodeGen/TargetFrameLowering.h =================================================================== --- include/llvm/CodeGen/TargetFrameLowering.h +++ include/llvm/CodeGen/TargetFrameLowering.h @@ -99,7 +99,9 @@ } /// isStackRealignable - This method returns whether the stack can be - /// realigned. + /// realigned on this target. Note that even for targets which allow stack + /// realignment, not all functions are required to. Most potential callers + /// probably want TargetRegisterInfo::canRealignStack(MF) instead. bool isStackRealignable() const { return StackRealignable; } Index: lib/CodeGen/MachineFunction.cpp =================================================================== --- lib/CodeGen/MachineFunction.cpp +++ lib/CodeGen/MachineFunction.cpp @@ -159,10 +159,8 @@ RegInfo = nullptr; MFInfo = nullptr; - // We can realign the stack if the target supports it and the user hasn't - // explicitly asked us not to. - bool CanRealignSP = STI->getFrameLowering()->isStackRealignable() && - !F.hasFnAttribute("no-realign-stack"); + + bool CanRealignSP = STI->getRegisterInfo()->canRealignStack(*this); FrameInfo = new (Allocator) MachineFrameInfo( getFnStackAlignment(STI, F), /*StackRealignable=*/CanRealignSP, /*ForceRealign=*/CanRealignSP && Index: lib/CodeGen/SelectionDAG/FunctionLoweringInfo.cpp =================================================================== --- lib/CodeGen/SelectionDAG/FunctionLoweringInfo.cpp +++ lib/CodeGen/SelectionDAG/FunctionLoweringInfo.cpp @@ -84,6 +84,7 @@ TLI = MF->getSubtarget().getTargetLowering(); RegInfo = &MF->getRegInfo(); const TargetFrameLowering *TFI = MF->getSubtarget().getFrameLowering(); + const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo(); unsigned StackAlign = TFI->getStackAlignment(); DA = DAG->getDivergenceAnalysis(); @@ -141,7 +142,7 @@ // adjustment. For targets that don't realign the stack, don't // do this if there is an extra alignment requirement. if (AI->isStaticAlloca() && - (TFI->isStackRealignable() || (Align <= StackAlign))) { + (TRI->canRealignStack(mf) || (Align <= StackAlign))) { const ConstantInt *CUI = cast(AI->getArraySize()); uint64_t TySize = MF->getDataLayout().getTypeAllocSize(Ty); @@ -180,7 +181,6 @@ ImmutableCallSite CS(&I); if (isa(CS.getCalledValue())) { unsigned SP = TLI->getStackPointerRegisterToSaveRestore(); - const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo(); std::vector Ops = TLI->ParseConstraints(Fn->getParent()->getDataLayout(), TRI, CS); for (TargetLowering::AsmOperandInfo &Op : Ops) { Index: lib/CodeGen/TargetRegisterInfo.cpp =================================================================== --- lib/CodeGen/TargetRegisterInfo.cpp +++ lib/CodeGen/TargetRegisterInfo.cpp @@ -434,7 +434,8 @@ } bool TargetRegisterInfo::canRealignStack(const MachineFunction &MF) const { - return !MF.getFunction().hasFnAttribute("no-realign-stack"); + return MF.getSubtarget().getFrameLowering()->isStackRealignable() && + !MF.getFunction().hasFnAttribute("no-realign-stack"); } bool TargetRegisterInfo::needsStackRealignment( Index: test/CodeGen/X86/alloca-overaligned.ll =================================================================== --- test/CodeGen/X86/alloca-overaligned.ll +++ test/CodeGen/X86/alloca-overaligned.ll @@ -42,12 +42,19 @@ define void @test_norealign() "no-realign-stack" { ; CHECK-LABEL: test_norealign: ; CHECK: # %bb.0: -; CHECK-NEXT: pushq %rax +; CHECK-NEXT: pushq %rbp ; CHECK-NEXT: .cfi_def_cfa_offset 16 +; CHECK-NEXT: .cfi_offset %rbp, -16 +; CHECK-NEXT: movq %rsp, %rbp +; CHECK-NEXT: .cfi_def_cfa_register %rbp ; CHECK-NEXT: movq %rsp, %rdi +; CHECK-NEXT: addq $-16, %rdi +; CHECK-NEXT: andq $-64, %rdi +; CHECK-NEXT: movq %rdi, %rsp ; CHECK-NEXT: callq capture -; CHECK-NEXT: popq %rax -; CHECK-NEXT: .cfi_def_cfa_offset 8 +; CHECK-NEXT: movq %rbp, %rsp +; CHECK-NEXT: popq %rbp +; CHECK-NEXT: .cfi_def_cfa %rsp, 8 ; CHECK-NEXT: retq %a = alloca i64, align 64 call void @capture(i64* %a)