]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - contrib/llvm/patches/patch-r263312-llvm-r202930-fix-alloca-esi-clobber.diff
- Copy stable/10@285827 to releng/10.2 in preparation for 10.2-RC1
[FreeBSD/releng/10.2.git] / contrib / llvm / patches / patch-r263312-llvm-r202930-fix-alloca-esi-clobber.diff
1 Pull in r202930 from upstream llvm trunk (by Hans Wennborg):
2
3   Check for dynamic allocas and inline asm that clobbers sp before building
4   selection dag (PR19012)
5
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).
9
10   The problem is that MachineFrameInfo wouldn't know about dynamic allocas or
11   inline asm that clobbers the stack pointer until SelectionDAGBuilder has
12   encountered them.
13
14   This patch fixes the problem by checking for such things when building the
15   FunctionLoweringInfo.
16
17   Differential Revision: http://llvm-reviews.chandlerc.com/D2954
18
19 Introduced here: http://svnweb.freebsd.org/changeset/base/263312
20
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
27  /// actually used.
28  ///
29 -int MachineFrameInfo::CreateVariableSizedObject(unsigned Alignment) {
30 +int MachineFrameInfo::CreateVariableSizedObject(unsigned Alignment,
31 +                                                const AllocaInst *Alloca) {
32    HasVarSizedObjects = true;
33    Alignment =
34      clampStackAlignment(!getFrameLowering()->isStackRealignable() ||
35                            !RealignOption,
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;
41  }
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);
48  
49    CurDAG->init(*MF, TTI, TLI);
50 -  FuncInfo->set(Fn, *MF);
51 +  FuncInfo->set(Fn, *MF, CurDAG);
52  
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);
57  
58    MF->setHasInlineAsm(false);
59 -  MF->getFrameInfo()->setHasInlineAsmWithSPAdjust(false);
60  
61    SelectAllBasicBlocks(Fn);
62  
63 Index: lib/CodeGen/SelectionDAG/FunctionLoweringInfo.cpp
64 ===================================================================
65 --- lib/CodeGen/SelectionDAG/FunctionLoweringInfo.cpp
66 +++ lib/CodeGen/SelectionDAG/FunctionLoweringInfo.cpp
67 @@ -33,6 +33,7 @@
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
76    return false;
77  }
78  
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();
83  
84    Fn = &fn;
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();
88           I != E; ++I) {
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)
98 +            Align = 0;
99 +          // Inform the Frame Information that we have variable-sized objects.
100 +          MF->getFrameInfo()->CreateVariableSizedObject(Align ? Align : 1, AI);
101 +        }
102 +      }
103 +
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,
118 +                                                  Op.ConstraintVT);
119 +              if (PhysReg.first == SP)
120 +                MF->getFrameInfo()->setHasInlineAsmWithSPAdjust(true);
121 +            }
122 +          }
123 +        }
124 +      }
125 +
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));
136  
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());
144        }
145      }
146    }
147 @@ -3378,9 +3378,7 @@ void SelectionDAGBuilder::visitAlloca(const Alloca
148    setValue(&I, DSA);
149    DAG.setRoot(DSA.getValue(1));
150  
151 -  // Inform the Frame Information that we have just allocated a variable-sized
152 -  // object.
153 -  FuncInfo.MF->getFrameInfo()->CreateVariableSizedObject(Align ? Align : 1);
154 +  assert(FuncInfo.MF->getFrameInfo()->hasVarSizedObjects());
155  }
156  
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
162 @@ -2,6 +2,9 @@
163  
164  %struct.foo = type { [88 x i8] }
165  
166 +declare void @bar(i8* nocapture, %struct.foo* align 4 byval) nounwind
167 +declare void @baz(i8*) nounwind
168 +
169  ; PR15249
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
174  }
175  
176 -declare void @bar(i8* nocapture, %struct.foo* align 4 byval) nounwind
177 +; PR19012
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)
183 +  ret void
184 +
185 +; CHECK-LABEL: test2:
186 +; CHECK: movl %esp, %esi
187 +; CHECK-NOT: rep;movsl
188 +}
189 +
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)
195 +  ret void
196 +
197 +; CHECK-LABEL: test3:
198 +; CHECK: rep;movsl
199 +}
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
204 @@ -0,0 +1,17 @@
205 +; RUN: llc < %s -force-align-stack -mtriple i386-apple-darwin -mcpu=i486 | FileCheck %s
206 +
207 +%struct.foo = type { [88 x i8] }
208 +
209 +declare void @bar(i8* nocapture, %struct.foo* align 4 byval) nounwind
210 +
211 +; PR19012
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)
216 +  ret void
217 +
218 +; CHECK-LABEL: test1:
219 +; CHECK: movl %esp, %esi
220 +; CHECK-NOT: rep;movsl
221 +}
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;
230 +class SelectionDAG;
231  class TargetLowering;
232  class Value;
233  
234 @@ -125,7 +126,7 @@ class FunctionLoweringInfo {
235    /// set - Initialize this FunctionLoweringInfo with the given Function
236    /// and its associated MachineFunction.
237    ///
238 -  void set(const Function &Fn, MachineFunction &MF);
239 +  void set(const Function &Fn, MachineFunction &MF, SelectionDAG *DAG);
240  
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 {
248      LocalFrameSize = 0;
249      LocalFrameMaxAlign = 0;
250      UseLocalStackAllocationBlock = false;
251 +    HasInlineAsmWithSPAdjust = false;
252    }
253  
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
257    /// actually used.
258    ///
259 -  int CreateVariableSizedObject(unsigned Alignment);
260 +  int CreateVariableSizedObject(unsigned Alignment, const AllocaInst *Alloca);
261  
262    /// getCalleeSavedInfo - Returns a reference to call saved info vector for the
263    /// current function.