Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/Target/X86/X86WinAllocaExpander.cpp
Line
Count
Source (jump to first uncovered line)
1
//===----- X86WinAllocaExpander.cpp - Expand WinAlloca pseudo instruction -===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
//
9
// This file defines a pass that expands WinAlloca pseudo-instructions.
10
//
11
// It performs a conservative analysis to determine whether each allocation
12
// falls within a region of the stack that is safe to use, or whether stack
13
// probes must be emitted.
14
//
15
//===----------------------------------------------------------------------===//
16
17
#include "X86.h"
18
#include "X86InstrBuilder.h"
19
#include "X86InstrInfo.h"
20
#include "X86MachineFunctionInfo.h"
21
#include "X86Subtarget.h"
22
#include "llvm/ADT/PostOrderIterator.h"
23
#include "llvm/CodeGen/MachineFunctionPass.h"
24
#include "llvm/CodeGen/MachineInstrBuilder.h"
25
#include "llvm/CodeGen/MachineRegisterInfo.h"
26
#include "llvm/CodeGen/Passes.h"
27
#include "llvm/CodeGen/TargetInstrInfo.h"
28
#include "llvm/IR/Function.h"
29
#include "llvm/Support/raw_ostream.h"
30
31
using namespace llvm;
32
33
namespace {
34
35
class X86WinAllocaExpander : public MachineFunctionPass {
36
public:
37
12.2k
  X86WinAllocaExpander() : MachineFunctionPass(ID) {}
38
39
  bool runOnMachineFunction(MachineFunction &MF) override;
40
41
private:
42
  /// Strategies for lowering a WinAlloca.
43
  enum Lowering { TouchAndSub, Sub, Probe };
44
45
  /// Deterministic-order map from WinAlloca instruction to desired lowering.
46
  typedef MapVector<MachineInstr*, Lowering> LoweringMap;
47
48
  /// Compute which lowering to use for each WinAlloca instruction.
49
  void computeLowerings(MachineFunction &MF, LoweringMap& Lowerings);
50
51
  /// Get the appropriate lowering based on current offset and amount.
52
  Lowering getLowering(int64_t CurrentOffset, int64_t AllocaAmount);
53
54
  /// Lower a WinAlloca instruction.
55
  void lower(MachineInstr* MI, Lowering L);
56
57
  MachineRegisterInfo *MRI;
58
  const X86Subtarget *STI;
59
  const TargetInstrInfo *TII;
60
  const X86RegisterInfo *TRI;
61
  unsigned StackPtr;
62
  unsigned SlotSize;
63
  int64_t StackProbeSize;
64
  bool NoStackArgProbe;
65
66
150k
  StringRef getPassName() const override { return "X86 WinAlloca Expander"; }
67
  static char ID;
68
};
69
70
char X86WinAllocaExpander::ID = 0;
71
72
} // end anonymous namespace
73
74
12.2k
FunctionPass *llvm::createX86WinAllocaExpander() {
75
12.2k
  return new X86WinAllocaExpander();
76
12.2k
}
77
78
/// Return the allocation amount for a WinAlloca instruction, or -1 if unknown.
79
214
static int64_t getWinAllocaAmount(MachineInstr *MI, MachineRegisterInfo *MRI) {
80
214
  assert(MI->getOpcode() == X86::WIN_ALLOCA_32 ||
81
214
         MI->getOpcode() == X86::WIN_ALLOCA_64);
82
214
  assert(MI->getOperand(0).isReg());
83
214
84
214
  unsigned AmountReg = MI->getOperand(0).getReg();
85
214
  MachineInstr *Def = MRI->getUniqueVRegDef(AmountReg);
86
214
87
214
  if (!Def ||
88
214
      (Def->getOpcode() != X86::MOV32ri && 
Def->getOpcode() != X86::MOV64ri136
) ||
89
214
      
!Def->getOperand(1).isImm()78
)
90
136
    return -1;
91
78
92
78
  return Def->getOperand(1).getImm();
93
78
}
94
95
X86WinAllocaExpander::Lowering
96
X86WinAllocaExpander::getLowering(int64_t CurrentOffset,
97
107
                                  int64_t AllocaAmount) {
98
107
  // For a non-constant amount or a large amount, we have to probe.
99
107
  if (AllocaAmount < 0 || 
AllocaAmount > StackProbeSize39
)
100
73
    return Probe;
101
34
102
34
  // If it fits within the safe region of the stack, just subtract.
103
34
  if (CurrentOffset + AllocaAmount <= StackProbeSize)
104
7
    return Sub;
105
27
106
27
  // Otherwise, touch the current tip of the stack, then subtract.
107
27
  return TouchAndSub;
108
27
}
109
110
1.49k
static bool isPushPop(const MachineInstr &MI) {
111
1.49k
  switch (MI.getOpcode()) {
112
1.49k
  case X86::PUSH32i8:
113
106
  case X86::PUSH32r:
114
106
  case X86::PUSH32rmm:
115
106
  case X86::PUSH32rmr:
116
106
  case X86::PUSHi32:
117
106
  case X86::PUSH64i8:
118
106
  case X86::PUSH64r:
119
106
  case X86::PUSH64rmm:
120
106
  case X86::PUSH64rmr:
121
106
  case X86::PUSH64i32:
122
106
  case X86::POP32r:
123
106
  case X86::POP64r:
124
106
    return true;
125
1.39k
  default:
126
1.39k
    return false;
127
1.49k
  }
128
1.49k
}
129
130
void X86WinAllocaExpander::computeLowerings(MachineFunction &MF,
131
90
                                            LoweringMap &Lowerings) {
132
90
  // Do a one-pass reverse post-order walk of the CFG to conservatively estimate
133
90
  // the offset between the stack pointer and the lowest touched part of the
134
90
  // stack, and use that to decide how to lower each WinAlloca instruction.
135
90
136
90
  // Initialize OutOffset[B], the stack offset at exit from B, to something big.
137
90
  DenseMap<MachineBasicBlock *, int64_t> OutOffset;
138
90
  for (MachineBasicBlock &MBB : MF)
139
142
    OutOffset[&MBB] = INT32_MAX;
140
90
141
90
  // Note: we don't know the offset at the start of the entry block since the
142
90
  // prologue hasn't been inserted yet, and how much that will adjust the stack
143
90
  // pointer depends on register spills, which have not been computed yet.
144
90
145
90
  // Compute the reverse post-order.
146
90
  ReversePostOrderTraversal<MachineFunction*> RPO(&MF);
147
90
148
142
  for (MachineBasicBlock *MBB : RPO) {
149
142
    int64_t Offset = -1;
150
142
    for (MachineBasicBlock *Pred : MBB->predecessors())
151
62
      Offset = std::max(Offset, OutOffset[Pred]);
152
142
    if (Offset == -1) 
Offset = INT32_MAX103
;
153
142
154
1.73k
    for (MachineInstr &MI : *MBB) {
155
1.73k
      if (MI.getOpcode() == X86::WIN_ALLOCA_32 ||
156
1.73k
          
MI.getOpcode() == X86::WIN_ALLOCA_641.64k
) {
157
107
        // A WinAlloca moves StackPtr, and potentially touches it.
158
107
        int64_t Amount = getWinAllocaAmount(&MI, MRI);
159
107
        Lowering L = getLowering(Offset, Amount);
160
107
        Lowerings[&MI] = L;
161
107
        switch (L) {
162
107
        case Sub:
163
7
          Offset += Amount;
164
7
          break;
165
107
        case TouchAndSub:
166
27
          Offset = Amount;
167
27
          break;
168
107
        case Probe:
169
73
          Offset = 0;
170
73
          break;
171
1.62k
        }
172
1.62k
      } else if (MI.isCall() || 
isPushPop(MI)1.49k
) {
173
230
        // Calls, pushes and pops touch the tip of the stack.
174
230
        Offset = 0;
175
1.39k
      } else if (MI.getOpcode() == X86::ADJCALLSTACKUP32 ||
176
1.39k
                 
MI.getOpcode() == X86::ADJCALLSTACKUP641.21k
) {
177
231
        Offset -= MI.getOperand(0).getImm();
178
1.16k
      } else if (MI.getOpcode() == X86::ADJCALLSTACKDOWN32 ||
179
1.16k
                 
MI.getOpcode() == X86::ADJCALLSTACKDOWN64987
) {
180
231
        Offset += MI.getOperand(0).getImm();
181
931
      } else if (MI.modifiesRegister(StackPtr, TRI)) {
182
12
        // Any other modification of SP means we've lost track of it.
183
12
        Offset = INT32_MAX;
184
12
      }
185
1.73k
    }
186
142
187
142
    OutOffset[MBB] = Offset;
188
142
  }
189
90
}
190
191
25
static unsigned getSubOpcode(bool Is64Bit, int64_t Amount) {
192
25
  if (Is64Bit)
193
0
    return isInt<8>(Amount) ? X86::SUB64ri8 : X86::SUB64ri32;
194
25
  return isInt<8>(Amount) ? 
X86::SUB32ri83
:
X86::SUB32ri22
;
195
25
}
196
197
107
void X86WinAllocaExpander::lower(MachineInstr* MI, Lowering L) {
198
107
  DebugLoc DL = MI->getDebugLoc();
199
107
  MachineBasicBlock *MBB = MI->getParent();
200
107
  MachineBasicBlock::iterator I = *MI;
201
107
202
107
  int64_t Amount = getWinAllocaAmount(MI, MRI);
203
107
  if (Amount == 0) {
204
0
    MI->eraseFromParent();
205
0
    return;
206
0
  }
207
107
208
107
  // These two variables differ on x32, which is a 64-bit target with a
209
107
  // 32-bit alloca.
210
107
  bool Is64Bit = STI->is64Bit();
211
107
  bool Is64BitAlloca = MI->getOpcode() == X86::WIN_ALLOCA_64;
212
107
  assert(SlotSize == 4 || SlotSize == 8);
213
107
214
107
  switch (L) {
215
107
  case TouchAndSub: {
216
27
    assert(Amount >= SlotSize);
217
27
218
27
    // Use a push to touch the top of the stack.
219
27
    unsigned RegA = Is64Bit ? 
X86::RAX1
:
X86::EAX26
;
220
27
    BuildMI(*MBB, I, DL, TII->get(Is64Bit ? 
X86::PUSH64r1
:
X86::PUSH32r26
))
221
27
        .addReg(RegA, RegState::Undef);
222
27
    Amount -= SlotSize;
223
27
    if (!Amount)
224
4
      break;
225
23
226
23
    // Fall through to make any remaining adjustment.
227
23
    LLVM_FALLTHROUGH;
228
23
  }
229
30
  case Sub:
230
30
    assert(Amount > 0);
231
30
    if (Amount == SlotSize) {
232
5
      // Use push to save size.
233
5
      unsigned RegA = Is64Bit ? 
X86::RAX0
: X86::EAX;
234
5
      BuildMI(*MBB, I, DL, TII->get(Is64Bit ? 
X86::PUSH64r0
: X86::PUSH32r))
235
5
          .addReg(RegA, RegState::Undef);
236
25
    } else {
237
25
      // Sub.
238
25
      BuildMI(*MBB, I, DL,
239
25
              TII->get(getSubOpcode(Is64BitAlloca, Amount)), StackPtr)
240
25
          .addReg(StackPtr)
241
25
          .addImm(Amount);
242
25
    }
243
30
    break;
244
73
  case Probe:
245
73
    if (!NoStackArgProbe) {
246
71
      // The probe lowering expects the amount in RAX/EAX.
247
71
      unsigned RegA = Is64BitAlloca ? 
X86::RAX24
:
X86::EAX47
;
248
71
      BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::COPY), RegA)
249
71
          .addReg(MI->getOperand(0).getReg());
250
71
251
71
      // Do the probe.
252
71
      STI->getFrameLowering()->emitStackProbe(*MBB->getParent(), *MBB, MI, DL,
253
71
                                              /*InProlog=*/false);
254
71
    } else {
255
2
      // Sub
256
2
      BuildMI(*MBB, I, DL,
257
2
              TII->get(Is64BitAlloca ? 
X86::SUB64rr0
: X86::SUB32rr), StackPtr)
258
2
          .addReg(StackPtr)
259
2
          .addReg(MI->getOperand(0).getReg());
260
2
    }
261
73
    break;
262
107
  }
263
107
264
107
  unsigned AmountReg = MI->getOperand(0).getReg();
265
107
  MI->eraseFromParent();
266
107
267
107
  // Delete the definition of AmountReg.
268
107
  if (MRI->use_empty(AmountReg))
269
26
    if (MachineInstr *AmountDef = MRI->getUniqueVRegDef(AmountReg))
270
26
      AmountDef->eraseFromParent();
271
107
}
272
273
137k
bool X86WinAllocaExpander::runOnMachineFunction(MachineFunction &MF) {
274
137k
  if (!MF.getInfo<X86MachineFunctionInfo>()->hasWinAlloca())
275
137k
    return false;
276
93
277
93
  MRI = &MF.getRegInfo();
278
93
  STI = &MF.getSubtarget<X86Subtarget>();
279
93
  TII = STI->getInstrInfo();
280
93
  TRI = STI->getRegisterInfo();
281
93
  StackPtr = TRI->getStackRegister();
282
93
  SlotSize = TRI->getSlotSize();
283
93
284
93
  StackProbeSize = 4096;
285
93
  if (MF.getFunction().hasFnAttribute("stack-probe-size")) {
286
2
    MF.getFunction()
287
2
        .getFnAttribute("stack-probe-size")
288
2
        .getValueAsString()
289
2
        .getAsInteger(0, StackProbeSize);
290
2
  }
291
93
  NoStackArgProbe = MF.getFunction().hasFnAttribute("no-stack-arg-probe");
292
93
  if (NoStackArgProbe)
293
2
    StackProbeSize = INT64_MAX;
294
93
295
93
  LoweringMap Lowerings;
296
93
  computeLowerings(MF, Lowerings);
297
93
  for (auto &P : Lowerings)
298
107
    lower(P.first, P.second);
299
93
300
93
  return true;
301
93
}