annotate lib/Target/X86/X86WinAllocaExpander.cpp @ 128:c347d3398279 default tip

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