1 Pull in r202930 from upstream llvm trunk (by Hans Wennborg):
3 Check for dynamic allocas and inline asm that clobbers sp before building
4 selection dag (PR19012)
6 In X86SelectionDagInfo::EmitTargetCodeForMemcpy we check with MachineFrameInfo
7 to make sure that ESI isn't used as a base pointer register before we choose to
8 emit rep movs (which clobbers esi).
10 The problem is that MachineFrameInfo wouldn't know about dynamic allocas or
11 inline asm that clobbers the stack pointer until SelectionDAGBuilder has
14 This patch fixes the problem by checking for such things when building the
17 Differential Revision: http://llvm-reviews.chandlerc.com/D2954
19 Introduced here: http://svnweb.freebsd.org/changeset/base/263312
21 Index: lib/CodeGen/MachineFunction.cpp
22 ===================================================================
23 --- lib/CodeGen/MachineFunction.cpp
24 +++ lib/CodeGen/MachineFunction.cpp
25 @@ -525,13 +525,14 @@ int MachineFrameInfo::CreateSpillStackObject(uint6
26 /// variable sized object is created, whether or not the index returned is
29 -int MachineFrameInfo::CreateVariableSizedObject(unsigned Alignment) {
30 +int MachineFrameInfo::CreateVariableSizedObject(unsigned Alignment,
31 + const AllocaInst *Alloca) {
32 HasVarSizedObjects = true;
34 clampStackAlignment(!getFrameLowering()->isStackRealignable() ||
36 Alignment, getFrameLowering()->getStackAlignment());
37 - Objects.push_back(StackObject(0, Alignment, 0, false, false, true, 0));
38 + Objects.push_back(StackObject(0, Alignment, 0, false, false, true, Alloca));
39 ensureMaxAlignment(Alignment);
40 return (int)Objects.size()-NumFixedObjects-1;
42 Index: lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp
43 ===================================================================
44 --- lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp
45 +++ lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp
46 @@ -419,7 +419,7 @@ bool SelectionDAGISel::runOnMachineFunction(Machin
47 SplitCriticalSideEffectEdges(const_cast<Function&>(Fn), this);
49 CurDAG->init(*MF, TTI, TLI);
50 - FuncInfo->set(Fn, *MF);
51 + FuncInfo->set(Fn, *MF, CurDAG);
53 if (UseMBPI && OptLevel != CodeGenOpt::None)
54 FuncInfo->BPI = &getAnalysis<BranchProbabilityInfo>();
55 @@ -429,7 +429,6 @@ bool SelectionDAGISel::runOnMachineFunction(Machin
56 SDB->init(GFI, *AA, LibInfo);
58 MF->setHasInlineAsm(false);
59 - MF->getFrameInfo()->setHasInlineAsmWithSPAdjust(false);
61 SelectAllBasicBlocks(Fn);
63 Index: lib/CodeGen/SelectionDAG/FunctionLoweringInfo.cpp
64 ===================================================================
65 --- lib/CodeGen/SelectionDAG/FunctionLoweringInfo.cpp
66 +++ lib/CodeGen/SelectionDAG/FunctionLoweringInfo.cpp
68 #include "llvm/Support/ErrorHandling.h"
69 #include "llvm/Support/MathExtras.h"
70 #include "llvm/Target/TargetInstrInfo.h"
71 +#include "llvm/Target/TargetFrameLowering.h"
72 #include "llvm/Target/TargetLowering.h"
73 #include "llvm/Target/TargetOptions.h"
74 #include "llvm/Target/TargetRegisterInfo.h"
75 @@ -55,7 +56,8 @@ static bool isUsedOutsideOfDefiningBlock(const Ins
79 -void FunctionLoweringInfo::set(const Function &fn, MachineFunction &mf) {
80 +void FunctionLoweringInfo::set(const Function &fn, MachineFunction &mf,
81 + SelectionDAG *DAG) {
82 const TargetLowering *TLI = TM.getTargetLowering();
85 @@ -100,6 +102,43 @@ static bool isUsedOutsideOfDefiningBlock(const Ins
86 for (; BB != EB; ++BB)
87 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end();
89 + // Look for dynamic allocas.
90 + if (const AllocaInst *AI = dyn_cast<AllocaInst>(I)) {
91 + if (!AI->isStaticAlloca()) {
92 + unsigned Align = std::max(
93 + (unsigned)TLI->getDataLayout()->getPrefTypeAlignment(
94 + AI->getAllocatedType()),
95 + AI->getAlignment());
96 + unsigned StackAlign = TM.getFrameLowering()->getStackAlignment();
97 + if (Align <= StackAlign)
99 + // Inform the Frame Information that we have variable-sized objects.
100 + MF->getFrameInfo()->CreateVariableSizedObject(Align ? Align : 1, AI);
104 + // Look for inline asm that clobbers the SP register.
105 + if (isa<CallInst>(I) || isa<InvokeInst>(I)) {
106 + ImmutableCallSite CS(I);
107 + if (const InlineAsm *IA = dyn_cast<InlineAsm>(CS.getCalledValue())) {
108 + unsigned SP = TLI->getStackPointerRegisterToSaveRestore();
109 + std::vector<TargetLowering::AsmOperandInfo> Ops =
110 + TLI->ParseConstraints(CS);
111 + for (size_t I = 0, E = Ops.size(); I != E; ++I) {
112 + TargetLowering::AsmOperandInfo &Op = Ops[I];
113 + if (Op.Type == InlineAsm::isClobber) {
114 + // Clobbers don't have SDValue operands, hence SDValue().
115 + TLI->ComputeConstraintToUse(Op, SDValue(), DAG);
116 + std::pair<unsigned, const TargetRegisterClass*> PhysReg =
117 + TLI->getRegForInlineAsmConstraint(Op.ConstraintCode,
119 + if (PhysReg.first == SP)
120 + MF->getFrameInfo()->setHasInlineAsmWithSPAdjust(true);
126 // Mark values used outside their block as exported, by allocating
127 // a virtual register for them.
128 if (isUsedOutsideOfDefiningBlock(I))
129 Index: lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp
130 ===================================================================
131 --- lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp
132 +++ lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp
133 @@ -860,10 +860,10 @@ void RegsForValue::AddInlineAsmOperands(unsigned C
134 unsigned TheReg = Regs[Reg++];
135 Ops.push_back(DAG.getRegister(TheReg, RegisterVT));
137 - // Notice if we clobbered the stack pointer. Yes, inline asm can do this.
138 if (TheReg == SP && Code == InlineAsm::Kind_Clobber) {
139 - MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
140 - MFI->setHasInlineAsmWithSPAdjust(true);
141 + // If we clobbered the stack pointer, MFI should know about it.
142 + assert(DAG.getMachineFunction().getFrameInfo()->
143 + hasInlineAsmWithSPAdjust());
147 @@ -3378,9 +3378,7 @@ void SelectionDAGBuilder::visitAlloca(const Alloca
149 DAG.setRoot(DSA.getValue(1));
151 - // Inform the Frame Information that we have just allocated a variable-sized
153 - FuncInfo.MF->getFrameInfo()->CreateVariableSizedObject(Align ? Align : 1);
154 + assert(FuncInfo.MF->getFrameInfo()->hasVarSizedObjects());
157 void SelectionDAGBuilder::visitLoad(const LoadInst &I) {
158 Index: test/CodeGen/X86/stack-align-memcpy.ll
159 ===================================================================
160 --- test/CodeGen/X86/stack-align-memcpy.ll
161 +++ test/CodeGen/X86/stack-align-memcpy.ll
164 %struct.foo = type { [88 x i8] }
166 +declare void @bar(i8* nocapture, %struct.foo* align 4 byval) nounwind
167 +declare void @baz(i8*) nounwind
170 ; We can't use rep;movsl here because it clobbers the base pointer in %esi.
171 define void @test1(%struct.foo* nocapture %x, i32 %y) nounwind {
172 @@ -15,4 +18,26 @@ define void @test1(%struct.foo* nocapture %x, i32
173 ; CHECK-NOT: rep;movsl
176 -declare void @bar(i8* nocapture, %struct.foo* align 4 byval) nounwind
178 +; Also don't clobber %esi if the dynamic alloca comes after the memcpy.
179 +define void @test2(%struct.foo* nocapture %x, i32 %y, i8* %z) nounwind {
180 + call void @bar(i8* %z, %struct.foo* align 4 byval %x)
181 + %dynalloc = alloca i8, i32 %y, align 1
182 + call void @baz(i8* %dynalloc)
185 +; CHECK-LABEL: test2:
186 +; CHECK: movl %esp, %esi
187 +; CHECK-NOT: rep;movsl
190 +; Check that we do use rep movs if we make the alloca static.
191 +define void @test3(%struct.foo* nocapture %x, i32 %y, i8* %z) nounwind {
192 + call void @bar(i8* %z, %struct.foo* align 4 byval %x)
193 + %statalloc = alloca i8, i32 8, align 1
194 + call void @baz(i8* %statalloc)
197 +; CHECK-LABEL: test3:
200 Index: test/CodeGen/X86/inline-asm-sp-clobber-memcpy.ll
201 ===================================================================
202 --- test/CodeGen/X86/inline-asm-sp-clobber-memcpy.ll
203 +++ test/CodeGen/X86/inline-asm-sp-clobber-memcpy.ll
205 +; RUN: llc < %s -force-align-stack -mtriple i386-apple-darwin -mcpu=i486 | FileCheck %s
207 +%struct.foo = type { [88 x i8] }
209 +declare void @bar(i8* nocapture, %struct.foo* align 4 byval) nounwind
212 +; Don't clobber %esi if we have inline asm that clobbers %esp.
213 +define void @test1(%struct.foo* nocapture %x, i32 %y, i8* %z) nounwind {
214 + call void @bar(i8* %z, %struct.foo* align 4 byval %x)
215 + call void asm sideeffect inteldialect "xor esp, esp", "=*m,~{flags},~{esp},~{esp},~{dirflag},~{fpsr},~{flags}"(i8* %z)
218 +; CHECK-LABEL: test1:
219 +; CHECK: movl %esp, %esi
220 +; CHECK-NOT: rep;movsl
222 Index: include/llvm/CodeGen/FunctionLoweringInfo.h
223 ===================================================================
224 --- include/llvm/CodeGen/FunctionLoweringInfo.h
225 +++ include/llvm/CodeGen/FunctionLoweringInfo.h
226 @@ -41,6 +41,7 @@ class MachineBasicBlock;
227 class MachineFunction;
228 class MachineModuleInfo;
229 class MachineRegisterInfo;
231 class TargetLowering;
234 @@ -125,7 +126,7 @@ class FunctionLoweringInfo {
235 /// set - Initialize this FunctionLoweringInfo with the given Function
236 /// and its associated MachineFunction.
238 - void set(const Function &Fn, MachineFunction &MF);
239 + void set(const Function &Fn, MachineFunction &MF, SelectionDAG *DAG);
241 /// clear - Clear out all the function-specific state. This returns this
242 /// FunctionLoweringInfo to an empty state, ready to be used for a
243 Index: include/llvm/CodeGen/MachineFrameInfo.h
244 ===================================================================
245 --- include/llvm/CodeGen/MachineFrameInfo.h
246 +++ include/llvm/CodeGen/MachineFrameInfo.h
247 @@ -244,6 +244,7 @@ class MachineFrameInfo {
249 LocalFrameMaxAlign = 0;
250 UseLocalStackAllocationBlock = false;
251 + HasInlineAsmWithSPAdjust = false;
254 /// hasStackObjects - Return true if there are any stack objects in this
255 @@ -529,7 +530,7 @@ class MachineFrameInfo {
256 /// variable sized object is created, whether or not the index returned is
259 - int CreateVariableSizedObject(unsigned Alignment);
260 + int CreateVariableSizedObject(unsigned Alignment, const AllocaInst *Alloca);
262 /// getCalleeSavedInfo - Returns a reference to call saved info vector for the
263 /// current function.