blob: 55adc263dd5d1c01592a8329071a9bb26e450cf2 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===- X86InstrInfo.cpp - X86 Instruction Information -----------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file contains the X86 implementation of the TargetInstrInfo class.
11//
12//===----------------------------------------------------------------------===//
13
14#include "X86InstrInfo.h"
15#include "X86.h"
16#include "X86GenInstrInfo.inc"
17#include "X86InstrBuilder.h"
Owen Anderson6690c7f2008-01-04 23:57:37 +000018#include "X86MachineFunctionInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000019#include "X86Subtarget.h"
20#include "X86TargetMachine.h"
Dan Gohmanc24a3f82009-01-05 17:59:02 +000021#include "llvm/DerivedTypes.h"
Owen Anderson15b39322009-07-13 04:09:18 +000022#include "llvm/LLVMContext.h"
Owen Anderson1636de92007-09-07 04:06:50 +000023#include "llvm/ADT/STLExtras.h"
Dan Gohman37eb6c82008-12-03 05:21:24 +000024#include "llvm/CodeGen/MachineConstantPool.h"
Owen Anderson6690c7f2008-01-04 23:57:37 +000025#include "llvm/CodeGen/MachineFrameInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000026#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner1b989192007-12-31 04:13:23 +000027#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000028#include "llvm/CodeGen/LiveVariables.h"
David Greene138ae532009-11-12 20:55:29 +000029#include "llvm/CodeGen/PseudoSourceValue.h"
Chris Lattnera4083332010-04-26 23:37:21 +000030#include "llvm/MC/MCInst.h"
Owen Anderson9a184ef2008-01-07 01:35:02 +000031#include "llvm/Support/CommandLine.h"
David Greene5fd1b6e2010-01-05 01:29:29 +000032#include "llvm/Support/Debug.h"
Edwin Török3cb88482009-07-08 18:01:40 +000033#include "llvm/Support/ErrorHandling.h"
34#include "llvm/Support/raw_ostream.h"
Evan Cheng950aac02007-09-25 01:57:46 +000035#include "llvm/Target/TargetOptions.h"
Chris Lattner621c44d2009-08-22 20:48:53 +000036#include "llvm/MC/MCAsmInfo.h"
David Greene138ae532009-11-12 20:55:29 +000037
38#include <limits>
39
Dan Gohmanf17a25c2007-07-18 16:29:46 +000040using namespace llvm;
41
Chris Lattnerd71b0b02009-08-23 03:41:05 +000042static cl::opt<bool>
43NoFusing("disable-spill-fusing",
44 cl::desc("Disable fusing of spill code into instructions"));
45static cl::opt<bool>
46PrintFailedFusing("print-failed-fuse-candidates",
47 cl::desc("Print instructions that the allocator wants to"
48 " fuse, but the X86 backend currently can't"),
49 cl::Hidden);
50static cl::opt<bool>
51ReMatPICStubLoad("remat-pic-stub-load",
52 cl::desc("Re-materialize load from stub in PIC mode"),
53 cl::init(false), cl::Hidden);
Owen Anderson9a184ef2008-01-07 01:35:02 +000054
Dan Gohmanf17a25c2007-07-18 16:29:46 +000055X86InstrInfo::X86InstrInfo(X86TargetMachine &tm)
Chris Lattnerd2fd6db2008-01-01 01:03:04 +000056 : TargetInstrInfoImpl(X86Insts, array_lengthof(X86Insts)),
Dan Gohmanf17a25c2007-07-18 16:29:46 +000057 TM(tm), RI(tm, *this) {
Owen Anderson9a184ef2008-01-07 01:35:02 +000058 SmallVector<unsigned,16> AmbEntries;
59 static const unsigned OpTbl2Addr[][2] = {
60 { X86::ADC32ri, X86::ADC32mi },
61 { X86::ADC32ri8, X86::ADC32mi8 },
62 { X86::ADC32rr, X86::ADC32mr },
63 { X86::ADC64ri32, X86::ADC64mi32 },
64 { X86::ADC64ri8, X86::ADC64mi8 },
65 { X86::ADC64rr, X86::ADC64mr },
66 { X86::ADD16ri, X86::ADD16mi },
67 { X86::ADD16ri8, X86::ADD16mi8 },
68 { X86::ADD16rr, X86::ADD16mr },
69 { X86::ADD32ri, X86::ADD32mi },
70 { X86::ADD32ri8, X86::ADD32mi8 },
71 { X86::ADD32rr, X86::ADD32mr },
72 { X86::ADD64ri32, X86::ADD64mi32 },
73 { X86::ADD64ri8, X86::ADD64mi8 },
74 { X86::ADD64rr, X86::ADD64mr },
75 { X86::ADD8ri, X86::ADD8mi },
76 { X86::ADD8rr, X86::ADD8mr },
77 { X86::AND16ri, X86::AND16mi },
78 { X86::AND16ri8, X86::AND16mi8 },
79 { X86::AND16rr, X86::AND16mr },
80 { X86::AND32ri, X86::AND32mi },
81 { X86::AND32ri8, X86::AND32mi8 },
82 { X86::AND32rr, X86::AND32mr },
83 { X86::AND64ri32, X86::AND64mi32 },
84 { X86::AND64ri8, X86::AND64mi8 },
85 { X86::AND64rr, X86::AND64mr },
86 { X86::AND8ri, X86::AND8mi },
87 { X86::AND8rr, X86::AND8mr },
88 { X86::DEC16r, X86::DEC16m },
89 { X86::DEC32r, X86::DEC32m },
90 { X86::DEC64_16r, X86::DEC64_16m },
91 { X86::DEC64_32r, X86::DEC64_32m },
92 { X86::DEC64r, X86::DEC64m },
93 { X86::DEC8r, X86::DEC8m },
94 { X86::INC16r, X86::INC16m },
95 { X86::INC32r, X86::INC32m },
96 { X86::INC64_16r, X86::INC64_16m },
97 { X86::INC64_32r, X86::INC64_32m },
98 { X86::INC64r, X86::INC64m },
99 { X86::INC8r, X86::INC8m },
100 { X86::NEG16r, X86::NEG16m },
101 { X86::NEG32r, X86::NEG32m },
102 { X86::NEG64r, X86::NEG64m },
103 { X86::NEG8r, X86::NEG8m },
104 { X86::NOT16r, X86::NOT16m },
105 { X86::NOT32r, X86::NOT32m },
106 { X86::NOT64r, X86::NOT64m },
107 { X86::NOT8r, X86::NOT8m },
108 { X86::OR16ri, X86::OR16mi },
109 { X86::OR16ri8, X86::OR16mi8 },
110 { X86::OR16rr, X86::OR16mr },
111 { X86::OR32ri, X86::OR32mi },
112 { X86::OR32ri8, X86::OR32mi8 },
113 { X86::OR32rr, X86::OR32mr },
114 { X86::OR64ri32, X86::OR64mi32 },
115 { X86::OR64ri8, X86::OR64mi8 },
116 { X86::OR64rr, X86::OR64mr },
117 { X86::OR8ri, X86::OR8mi },
118 { X86::OR8rr, X86::OR8mr },
119 { X86::ROL16r1, X86::ROL16m1 },
120 { X86::ROL16rCL, X86::ROL16mCL },
121 { X86::ROL16ri, X86::ROL16mi },
122 { X86::ROL32r1, X86::ROL32m1 },
123 { X86::ROL32rCL, X86::ROL32mCL },
124 { X86::ROL32ri, X86::ROL32mi },
125 { X86::ROL64r1, X86::ROL64m1 },
126 { X86::ROL64rCL, X86::ROL64mCL },
127 { X86::ROL64ri, X86::ROL64mi },
128 { X86::ROL8r1, X86::ROL8m1 },
129 { X86::ROL8rCL, X86::ROL8mCL },
130 { X86::ROL8ri, X86::ROL8mi },
131 { X86::ROR16r1, X86::ROR16m1 },
132 { X86::ROR16rCL, X86::ROR16mCL },
133 { X86::ROR16ri, X86::ROR16mi },
134 { X86::ROR32r1, X86::ROR32m1 },
135 { X86::ROR32rCL, X86::ROR32mCL },
136 { X86::ROR32ri, X86::ROR32mi },
137 { X86::ROR64r1, X86::ROR64m1 },
138 { X86::ROR64rCL, X86::ROR64mCL },
139 { X86::ROR64ri, X86::ROR64mi },
140 { X86::ROR8r1, X86::ROR8m1 },
141 { X86::ROR8rCL, X86::ROR8mCL },
142 { X86::ROR8ri, X86::ROR8mi },
143 { X86::SAR16r1, X86::SAR16m1 },
144 { X86::SAR16rCL, X86::SAR16mCL },
145 { X86::SAR16ri, X86::SAR16mi },
146 { X86::SAR32r1, X86::SAR32m1 },
147 { X86::SAR32rCL, X86::SAR32mCL },
148 { X86::SAR32ri, X86::SAR32mi },
149 { X86::SAR64r1, X86::SAR64m1 },
150 { X86::SAR64rCL, X86::SAR64mCL },
151 { X86::SAR64ri, X86::SAR64mi },
152 { X86::SAR8r1, X86::SAR8m1 },
153 { X86::SAR8rCL, X86::SAR8mCL },
154 { X86::SAR8ri, X86::SAR8mi },
155 { X86::SBB32ri, X86::SBB32mi },
156 { X86::SBB32ri8, X86::SBB32mi8 },
157 { X86::SBB32rr, X86::SBB32mr },
158 { X86::SBB64ri32, X86::SBB64mi32 },
159 { X86::SBB64ri8, X86::SBB64mi8 },
160 { X86::SBB64rr, X86::SBB64mr },
Owen Anderson9a184ef2008-01-07 01:35:02 +0000161 { X86::SHL16rCL, X86::SHL16mCL },
162 { X86::SHL16ri, X86::SHL16mi },
Owen Anderson9a184ef2008-01-07 01:35:02 +0000163 { X86::SHL32rCL, X86::SHL32mCL },
164 { X86::SHL32ri, X86::SHL32mi },
Owen Anderson9a184ef2008-01-07 01:35:02 +0000165 { X86::SHL64rCL, X86::SHL64mCL },
166 { X86::SHL64ri, X86::SHL64mi },
Owen Anderson9a184ef2008-01-07 01:35:02 +0000167 { X86::SHL8rCL, X86::SHL8mCL },
168 { X86::SHL8ri, X86::SHL8mi },
169 { X86::SHLD16rrCL, X86::SHLD16mrCL },
170 { X86::SHLD16rri8, X86::SHLD16mri8 },
171 { X86::SHLD32rrCL, X86::SHLD32mrCL },
172 { X86::SHLD32rri8, X86::SHLD32mri8 },
173 { X86::SHLD64rrCL, X86::SHLD64mrCL },
174 { X86::SHLD64rri8, X86::SHLD64mri8 },
175 { X86::SHR16r1, X86::SHR16m1 },
176 { X86::SHR16rCL, X86::SHR16mCL },
177 { X86::SHR16ri, X86::SHR16mi },
178 { X86::SHR32r1, X86::SHR32m1 },
179 { X86::SHR32rCL, X86::SHR32mCL },
180 { X86::SHR32ri, X86::SHR32mi },
181 { X86::SHR64r1, X86::SHR64m1 },
182 { X86::SHR64rCL, X86::SHR64mCL },
183 { X86::SHR64ri, X86::SHR64mi },
184 { X86::SHR8r1, X86::SHR8m1 },
185 { X86::SHR8rCL, X86::SHR8mCL },
186 { X86::SHR8ri, X86::SHR8mi },
187 { X86::SHRD16rrCL, X86::SHRD16mrCL },
188 { X86::SHRD16rri8, X86::SHRD16mri8 },
189 { X86::SHRD32rrCL, X86::SHRD32mrCL },
190 { X86::SHRD32rri8, X86::SHRD32mri8 },
191 { X86::SHRD64rrCL, X86::SHRD64mrCL },
192 { X86::SHRD64rri8, X86::SHRD64mri8 },
193 { X86::SUB16ri, X86::SUB16mi },
194 { X86::SUB16ri8, X86::SUB16mi8 },
195 { X86::SUB16rr, X86::SUB16mr },
196 { X86::SUB32ri, X86::SUB32mi },
197 { X86::SUB32ri8, X86::SUB32mi8 },
198 { X86::SUB32rr, X86::SUB32mr },
199 { X86::SUB64ri32, X86::SUB64mi32 },
200 { X86::SUB64ri8, X86::SUB64mi8 },
201 { X86::SUB64rr, X86::SUB64mr },
202 { X86::SUB8ri, X86::SUB8mi },
203 { X86::SUB8rr, X86::SUB8mr },
204 { X86::XOR16ri, X86::XOR16mi },
205 { X86::XOR16ri8, X86::XOR16mi8 },
206 { X86::XOR16rr, X86::XOR16mr },
207 { X86::XOR32ri, X86::XOR32mi },
208 { X86::XOR32ri8, X86::XOR32mi8 },
209 { X86::XOR32rr, X86::XOR32mr },
210 { X86::XOR64ri32, X86::XOR64mi32 },
211 { X86::XOR64ri8, X86::XOR64mi8 },
212 { X86::XOR64rr, X86::XOR64mr },
213 { X86::XOR8ri, X86::XOR8mi },
214 { X86::XOR8rr, X86::XOR8mr }
215 };
216
217 for (unsigned i = 0, e = array_lengthof(OpTbl2Addr); i != e; ++i) {
218 unsigned RegOp = OpTbl2Addr[i][0];
219 unsigned MemOp = OpTbl2Addr[i][1];
Dan Gohman55d19662008-07-07 17:46:23 +0000220 if (!RegOp2MemOpTable2Addr.insert(std::make_pair((unsigned*)RegOp,
Evan Chenga5853792009-07-15 06:10:07 +0000221 std::make_pair(MemOp,0))).second)
Owen Anderson9a184ef2008-01-07 01:35:02 +0000222 assert(false && "Duplicated entries?");
Evan Chenga5853792009-07-15 06:10:07 +0000223 // Index 0, folded load and store, no alignment requirement.
224 unsigned AuxInfo = 0 | (1 << 4) | (1 << 5);
Owen Anderson9a184ef2008-01-07 01:35:02 +0000225 if (!MemOp2RegOpTable.insert(std::make_pair((unsigned*)MemOp,
Dan Gohman55d19662008-07-07 17:46:23 +0000226 std::make_pair(RegOp,
227 AuxInfo))).second)
Owen Anderson9a184ef2008-01-07 01:35:02 +0000228 AmbEntries.push_back(MemOp);
229 }
230
231 // If the third value is 1, then it's folding either a load or a store.
Evan Chenga5853792009-07-15 06:10:07 +0000232 static const unsigned OpTbl0[][4] = {
233 { X86::BT16ri8, X86::BT16mi8, 1, 0 },
234 { X86::BT32ri8, X86::BT32mi8, 1, 0 },
235 { X86::BT64ri8, X86::BT64mi8, 1, 0 },
236 { X86::CALL32r, X86::CALL32m, 1, 0 },
237 { X86::CALL64r, X86::CALL64m, 1, 0 },
238 { X86::CMP16ri, X86::CMP16mi, 1, 0 },
239 { X86::CMP16ri8, X86::CMP16mi8, 1, 0 },
240 { X86::CMP16rr, X86::CMP16mr, 1, 0 },
241 { X86::CMP32ri, X86::CMP32mi, 1, 0 },
242 { X86::CMP32ri8, X86::CMP32mi8, 1, 0 },
243 { X86::CMP32rr, X86::CMP32mr, 1, 0 },
244 { X86::CMP64ri32, X86::CMP64mi32, 1, 0 },
245 { X86::CMP64ri8, X86::CMP64mi8, 1, 0 },
246 { X86::CMP64rr, X86::CMP64mr, 1, 0 },
247 { X86::CMP8ri, X86::CMP8mi, 1, 0 },
248 { X86::CMP8rr, X86::CMP8mr, 1, 0 },
249 { X86::DIV16r, X86::DIV16m, 1, 0 },
250 { X86::DIV32r, X86::DIV32m, 1, 0 },
251 { X86::DIV64r, X86::DIV64m, 1, 0 },
252 { X86::DIV8r, X86::DIV8m, 1, 0 },
253 { X86::EXTRACTPSrr, X86::EXTRACTPSmr, 0, 16 },
254 { X86::FsMOVAPDrr, X86::MOVSDmr, 0, 0 },
255 { X86::FsMOVAPSrr, X86::MOVSSmr, 0, 0 },
256 { X86::IDIV16r, X86::IDIV16m, 1, 0 },
257 { X86::IDIV32r, X86::IDIV32m, 1, 0 },
258 { X86::IDIV64r, X86::IDIV64m, 1, 0 },
259 { X86::IDIV8r, X86::IDIV8m, 1, 0 },
260 { X86::IMUL16r, X86::IMUL16m, 1, 0 },
261 { X86::IMUL32r, X86::IMUL32m, 1, 0 },
262 { X86::IMUL64r, X86::IMUL64m, 1, 0 },
263 { X86::IMUL8r, X86::IMUL8m, 1, 0 },
264 { X86::JMP32r, X86::JMP32m, 1, 0 },
265 { X86::JMP64r, X86::JMP64m, 1, 0 },
266 { X86::MOV16ri, X86::MOV16mi, 0, 0 },
267 { X86::MOV16rr, X86::MOV16mr, 0, 0 },
268 { X86::MOV32ri, X86::MOV32mi, 0, 0 },
269 { X86::MOV32rr, X86::MOV32mr, 0, 0 },
Evan Cheng9ac24d12010-03-14 03:48:46 +0000270 { X86::MOV32rr_TC, X86::MOV32mr_TC, 0, 0 },
Evan Chenga5853792009-07-15 06:10:07 +0000271 { X86::MOV64ri32, X86::MOV64mi32, 0, 0 },
272 { X86::MOV64rr, X86::MOV64mr, 0, 0 },
273 { X86::MOV8ri, X86::MOV8mi, 0, 0 },
274 { X86::MOV8rr, X86::MOV8mr, 0, 0 },
275 { X86::MOV8rr_NOREX, X86::MOV8mr_NOREX, 0, 0 },
276 { X86::MOVAPDrr, X86::MOVAPDmr, 0, 16 },
277 { X86::MOVAPSrr, X86::MOVAPSmr, 0, 16 },
278 { X86::MOVDQArr, X86::MOVDQAmr, 0, 16 },
279 { X86::MOVPDI2DIrr, X86::MOVPDI2DImr, 0, 0 },
280 { X86::MOVPQIto64rr,X86::MOVPQI2QImr, 0, 0 },
Evan Chenga5853792009-07-15 06:10:07 +0000281 { X86::MOVSDto64rr, X86::MOVSDto64mr, 0, 0 },
282 { X86::MOVSS2DIrr, X86::MOVSS2DImr, 0, 0 },
Evan Chenga5853792009-07-15 06:10:07 +0000283 { X86::MOVUPDrr, X86::MOVUPDmr, 0, 0 },
284 { X86::MOVUPSrr, X86::MOVUPSmr, 0, 0 },
285 { X86::MUL16r, X86::MUL16m, 1, 0 },
286 { X86::MUL32r, X86::MUL32m, 1, 0 },
287 { X86::MUL64r, X86::MUL64m, 1, 0 },
288 { X86::MUL8r, X86::MUL8m, 1, 0 },
289 { X86::SETAEr, X86::SETAEm, 0, 0 },
290 { X86::SETAr, X86::SETAm, 0, 0 },
291 { X86::SETBEr, X86::SETBEm, 0, 0 },
292 { X86::SETBr, X86::SETBm, 0, 0 },
293 { X86::SETEr, X86::SETEm, 0, 0 },
294 { X86::SETGEr, X86::SETGEm, 0, 0 },
295 { X86::SETGr, X86::SETGm, 0, 0 },
296 { X86::SETLEr, X86::SETLEm, 0, 0 },
297 { X86::SETLr, X86::SETLm, 0, 0 },
298 { X86::SETNEr, X86::SETNEm, 0, 0 },
299 { X86::SETNOr, X86::SETNOm, 0, 0 },
300 { X86::SETNPr, X86::SETNPm, 0, 0 },
301 { X86::SETNSr, X86::SETNSm, 0, 0 },
302 { X86::SETOr, X86::SETOm, 0, 0 },
303 { X86::SETPr, X86::SETPm, 0, 0 },
304 { X86::SETSr, X86::SETSm, 0, 0 },
305 { X86::TAILJMPr, X86::TAILJMPm, 1, 0 },
Evan Cheng9ac24d12010-03-14 03:48:46 +0000306 { X86::TAILJMPr64, X86::TAILJMPm64, 1, 0 },
Evan Chenga5853792009-07-15 06:10:07 +0000307 { X86::TEST16ri, X86::TEST16mi, 1, 0 },
308 { X86::TEST32ri, X86::TEST32mi, 1, 0 },
309 { X86::TEST64ri32, X86::TEST64mi32, 1, 0 },
310 { X86::TEST8ri, X86::TEST8mi, 1, 0 }
Owen Anderson9a184ef2008-01-07 01:35:02 +0000311 };
312
313 for (unsigned i = 0, e = array_lengthof(OpTbl0); i != e; ++i) {
314 unsigned RegOp = OpTbl0[i][0];
315 unsigned MemOp = OpTbl0[i][1];
Evan Chenga5853792009-07-15 06:10:07 +0000316 unsigned Align = OpTbl0[i][3];
Dan Gohman55d19662008-07-07 17:46:23 +0000317 if (!RegOp2MemOpTable0.insert(std::make_pair((unsigned*)RegOp,
Evan Chenga5853792009-07-15 06:10:07 +0000318 std::make_pair(MemOp,Align))).second)
Owen Anderson9a184ef2008-01-07 01:35:02 +0000319 assert(false && "Duplicated entries?");
320 unsigned FoldedLoad = OpTbl0[i][2];
321 // Index 0, folded load or store.
322 unsigned AuxInfo = 0 | (FoldedLoad << 4) | ((FoldedLoad^1) << 5);
323 if (RegOp != X86::FsMOVAPDrr && RegOp != X86::FsMOVAPSrr)
324 if (!MemOp2RegOpTable.insert(std::make_pair((unsigned*)MemOp,
Dan Gohman55d19662008-07-07 17:46:23 +0000325 std::make_pair(RegOp, AuxInfo))).second)
Owen Anderson9a184ef2008-01-07 01:35:02 +0000326 AmbEntries.push_back(MemOp);
327 }
328
Evan Chenga5853792009-07-15 06:10:07 +0000329 static const unsigned OpTbl1[][3] = {
330 { X86::CMP16rr, X86::CMP16rm, 0 },
331 { X86::CMP32rr, X86::CMP32rm, 0 },
332 { X86::CMP64rr, X86::CMP64rm, 0 },
333 { X86::CMP8rr, X86::CMP8rm, 0 },
334 { X86::CVTSD2SSrr, X86::CVTSD2SSrm, 0 },
335 { X86::CVTSI2SD64rr, X86::CVTSI2SD64rm, 0 },
336 { X86::CVTSI2SDrr, X86::CVTSI2SDrm, 0 },
337 { X86::CVTSI2SS64rr, X86::CVTSI2SS64rm, 0 },
338 { X86::CVTSI2SSrr, X86::CVTSI2SSrm, 0 },
339 { X86::CVTSS2SDrr, X86::CVTSS2SDrm, 0 },
340 { X86::CVTTSD2SI64rr, X86::CVTTSD2SI64rm, 0 },
341 { X86::CVTTSD2SIrr, X86::CVTTSD2SIrm, 0 },
342 { X86::CVTTSS2SI64rr, X86::CVTTSS2SI64rm, 0 },
343 { X86::CVTTSS2SIrr, X86::CVTTSS2SIrm, 0 },
344 { X86::FsMOVAPDrr, X86::MOVSDrm, 0 },
345 { X86::FsMOVAPSrr, X86::MOVSSrm, 0 },
346 { X86::IMUL16rri, X86::IMUL16rmi, 0 },
347 { X86::IMUL16rri8, X86::IMUL16rmi8, 0 },
348 { X86::IMUL32rri, X86::IMUL32rmi, 0 },
349 { X86::IMUL32rri8, X86::IMUL32rmi8, 0 },
350 { X86::IMUL64rri32, X86::IMUL64rmi32, 0 },
351 { X86::IMUL64rri8, X86::IMUL64rmi8, 0 },
352 { X86::Int_CMPSDrr, X86::Int_CMPSDrm, 0 },
353 { X86::Int_CMPSSrr, X86::Int_CMPSSrm, 0 },
354 { X86::Int_COMISDrr, X86::Int_COMISDrm, 0 },
355 { X86::Int_COMISSrr, X86::Int_COMISSrm, 0 },
356 { X86::Int_CVTDQ2PDrr, X86::Int_CVTDQ2PDrm, 16 },
357 { X86::Int_CVTDQ2PSrr, X86::Int_CVTDQ2PSrm, 16 },
358 { X86::Int_CVTPD2DQrr, X86::Int_CVTPD2DQrm, 16 },
359 { X86::Int_CVTPD2PSrr, X86::Int_CVTPD2PSrm, 16 },
360 { X86::Int_CVTPS2DQrr, X86::Int_CVTPS2DQrm, 16 },
361 { X86::Int_CVTPS2PDrr, X86::Int_CVTPS2PDrm, 0 },
362 { X86::Int_CVTSD2SI64rr,X86::Int_CVTSD2SI64rm, 0 },
363 { X86::Int_CVTSD2SIrr, X86::Int_CVTSD2SIrm, 0 },
364 { X86::Int_CVTSD2SSrr, X86::Int_CVTSD2SSrm, 0 },
365 { X86::Int_CVTSI2SD64rr,X86::Int_CVTSI2SD64rm, 0 },
366 { X86::Int_CVTSI2SDrr, X86::Int_CVTSI2SDrm, 0 },
367 { X86::Int_CVTSI2SS64rr,X86::Int_CVTSI2SS64rm, 0 },
368 { X86::Int_CVTSI2SSrr, X86::Int_CVTSI2SSrm, 0 },
369 { X86::Int_CVTSS2SDrr, X86::Int_CVTSS2SDrm, 0 },
370 { X86::Int_CVTSS2SI64rr,X86::Int_CVTSS2SI64rm, 0 },
371 { X86::Int_CVTSS2SIrr, X86::Int_CVTSS2SIrm, 0 },
372 { X86::Int_CVTTPD2DQrr, X86::Int_CVTTPD2DQrm, 16 },
373 { X86::Int_CVTTPS2DQrr, X86::Int_CVTTPS2DQrm, 16 },
374 { X86::Int_CVTTSD2SI64rr,X86::Int_CVTTSD2SI64rm, 0 },
375 { X86::Int_CVTTSD2SIrr, X86::Int_CVTTSD2SIrm, 0 },
376 { X86::Int_CVTTSS2SI64rr,X86::Int_CVTTSS2SI64rm, 0 },
377 { X86::Int_CVTTSS2SIrr, X86::Int_CVTTSS2SIrm, 0 },
378 { X86::Int_UCOMISDrr, X86::Int_UCOMISDrm, 0 },
379 { X86::Int_UCOMISSrr, X86::Int_UCOMISSrm, 0 },
380 { X86::MOV16rr, X86::MOV16rm, 0 },
381 { X86::MOV32rr, X86::MOV32rm, 0 },
Evan Cheng9ac24d12010-03-14 03:48:46 +0000382 { X86::MOV32rr_TC, X86::MOV32rm_TC, 0 },
Evan Chenga5853792009-07-15 06:10:07 +0000383 { X86::MOV64rr, X86::MOV64rm, 0 },
384 { X86::MOV64toPQIrr, X86::MOVQI2PQIrm, 0 },
385 { X86::MOV64toSDrr, X86::MOV64toSDrm, 0 },
386 { X86::MOV8rr, X86::MOV8rm, 0 },
387 { X86::MOVAPDrr, X86::MOVAPDrm, 16 },
388 { X86::MOVAPSrr, X86::MOVAPSrm, 16 },
389 { X86::MOVDDUPrr, X86::MOVDDUPrm, 0 },
390 { X86::MOVDI2PDIrr, X86::MOVDI2PDIrm, 0 },
391 { X86::MOVDI2SSrr, X86::MOVDI2SSrm, 0 },
392 { X86::MOVDQArr, X86::MOVDQArm, 16 },
Evan Chenga5853792009-07-15 06:10:07 +0000393 { X86::MOVSHDUPrr, X86::MOVSHDUPrm, 16 },
394 { X86::MOVSLDUPrr, X86::MOVSLDUPrm, 16 },
Evan Chenga5853792009-07-15 06:10:07 +0000395 { X86::MOVSX16rr8, X86::MOVSX16rm8, 0 },
396 { X86::MOVSX32rr16, X86::MOVSX32rm16, 0 },
397 { X86::MOVSX32rr8, X86::MOVSX32rm8, 0 },
398 { X86::MOVSX64rr16, X86::MOVSX64rm16, 0 },
399 { X86::MOVSX64rr32, X86::MOVSX64rm32, 0 },
400 { X86::MOVSX64rr8, X86::MOVSX64rm8, 0 },
401 { X86::MOVUPDrr, X86::MOVUPDrm, 16 },
Evan Cheng8bbd0912010-01-21 00:55:14 +0000402 { X86::MOVUPSrr, X86::MOVUPSrm, 0 },
Evan Chenga5853792009-07-15 06:10:07 +0000403 { X86::MOVZDI2PDIrr, X86::MOVZDI2PDIrm, 0 },
404 { X86::MOVZQI2PQIrr, X86::MOVZQI2PQIrm, 0 },
405 { X86::MOVZPQILo2PQIrr, X86::MOVZPQILo2PQIrm, 16 },
406 { X86::MOVZX16rr8, X86::MOVZX16rm8, 0 },
407 { X86::MOVZX32rr16, X86::MOVZX32rm16, 0 },
408 { X86::MOVZX32_NOREXrr8, X86::MOVZX32_NOREXrm8, 0 },
409 { X86::MOVZX32rr8, X86::MOVZX32rm8, 0 },
410 { X86::MOVZX64rr16, X86::MOVZX64rm16, 0 },
411 { X86::MOVZX64rr32, X86::MOVZX64rm32, 0 },
412 { X86::MOVZX64rr8, X86::MOVZX64rm8, 0 },
413 { X86::PSHUFDri, X86::PSHUFDmi, 16 },
414 { X86::PSHUFHWri, X86::PSHUFHWmi, 16 },
415 { X86::PSHUFLWri, X86::PSHUFLWmi, 16 },
416 { X86::RCPPSr, X86::RCPPSm, 16 },
417 { X86::RCPPSr_Int, X86::RCPPSm_Int, 16 },
418 { X86::RSQRTPSr, X86::RSQRTPSm, 16 },
419 { X86::RSQRTPSr_Int, X86::RSQRTPSm_Int, 16 },
420 { X86::RSQRTSSr, X86::RSQRTSSm, 0 },
421 { X86::RSQRTSSr_Int, X86::RSQRTSSm_Int, 0 },
422 { X86::SQRTPDr, X86::SQRTPDm, 16 },
423 { X86::SQRTPDr_Int, X86::SQRTPDm_Int, 16 },
424 { X86::SQRTPSr, X86::SQRTPSm, 16 },
425 { X86::SQRTPSr_Int, X86::SQRTPSm_Int, 16 },
426 { X86::SQRTSDr, X86::SQRTSDm, 0 },
427 { X86::SQRTSDr_Int, X86::SQRTSDm_Int, 0 },
428 { X86::SQRTSSr, X86::SQRTSSm, 0 },
429 { X86::SQRTSSr_Int, X86::SQRTSSm_Int, 0 },
430 { X86::TEST16rr, X86::TEST16rm, 0 },
431 { X86::TEST32rr, X86::TEST32rm, 0 },
432 { X86::TEST64rr, X86::TEST64rm, 0 },
433 { X86::TEST8rr, X86::TEST8rm, 0 },
Owen Anderson9a184ef2008-01-07 01:35:02 +0000434 // FIXME: TEST*rr EAX,EAX ---> CMP [mem], 0
Evan Chenga5853792009-07-15 06:10:07 +0000435 { X86::UCOMISDrr, X86::UCOMISDrm, 0 },
436 { X86::UCOMISSrr, X86::UCOMISSrm, 0 }
Owen Anderson9a184ef2008-01-07 01:35:02 +0000437 };
438
439 for (unsigned i = 0, e = array_lengthof(OpTbl1); i != e; ++i) {
440 unsigned RegOp = OpTbl1[i][0];
441 unsigned MemOp = OpTbl1[i][1];
Evan Chenga5853792009-07-15 06:10:07 +0000442 unsigned Align = OpTbl1[i][2];
Dan Gohman55d19662008-07-07 17:46:23 +0000443 if (!RegOp2MemOpTable1.insert(std::make_pair((unsigned*)RegOp,
Evan Chenga5853792009-07-15 06:10:07 +0000444 std::make_pair(MemOp,Align))).second)
Owen Anderson9a184ef2008-01-07 01:35:02 +0000445 assert(false && "Duplicated entries?");
Evan Chenga5853792009-07-15 06:10:07 +0000446 // Index 1, folded load
447 unsigned AuxInfo = 1 | (1 << 4);
Owen Anderson9a184ef2008-01-07 01:35:02 +0000448 if (RegOp != X86::FsMOVAPDrr && RegOp != X86::FsMOVAPSrr)
449 if (!MemOp2RegOpTable.insert(std::make_pair((unsigned*)MemOp,
Dan Gohman55d19662008-07-07 17:46:23 +0000450 std::make_pair(RegOp, AuxInfo))).second)
Owen Anderson9a184ef2008-01-07 01:35:02 +0000451 AmbEntries.push_back(MemOp);
452 }
453
Evan Chenga5853792009-07-15 06:10:07 +0000454 static const unsigned OpTbl2[][3] = {
455 { X86::ADC32rr, X86::ADC32rm, 0 },
456 { X86::ADC64rr, X86::ADC64rm, 0 },
457 { X86::ADD16rr, X86::ADD16rm, 0 },
458 { X86::ADD32rr, X86::ADD32rm, 0 },
459 { X86::ADD64rr, X86::ADD64rm, 0 },
460 { X86::ADD8rr, X86::ADD8rm, 0 },
461 { X86::ADDPDrr, X86::ADDPDrm, 16 },
462 { X86::ADDPSrr, X86::ADDPSrm, 16 },
463 { X86::ADDSDrr, X86::ADDSDrm, 0 },
464 { X86::ADDSSrr, X86::ADDSSrm, 0 },
465 { X86::ADDSUBPDrr, X86::ADDSUBPDrm, 16 },
466 { X86::ADDSUBPSrr, X86::ADDSUBPSrm, 16 },
467 { X86::AND16rr, X86::AND16rm, 0 },
468 { X86::AND32rr, X86::AND32rm, 0 },
469 { X86::AND64rr, X86::AND64rm, 0 },
470 { X86::AND8rr, X86::AND8rm, 0 },
471 { X86::ANDNPDrr, X86::ANDNPDrm, 16 },
472 { X86::ANDNPSrr, X86::ANDNPSrm, 16 },
473 { X86::ANDPDrr, X86::ANDPDrm, 16 },
474 { X86::ANDPSrr, X86::ANDPSrm, 16 },
475 { X86::CMOVA16rr, X86::CMOVA16rm, 0 },
476 { X86::CMOVA32rr, X86::CMOVA32rm, 0 },
477 { X86::CMOVA64rr, X86::CMOVA64rm, 0 },
478 { X86::CMOVAE16rr, X86::CMOVAE16rm, 0 },
479 { X86::CMOVAE32rr, X86::CMOVAE32rm, 0 },
480 { X86::CMOVAE64rr, X86::CMOVAE64rm, 0 },
481 { X86::CMOVB16rr, X86::CMOVB16rm, 0 },
482 { X86::CMOVB32rr, X86::CMOVB32rm, 0 },
483 { X86::CMOVB64rr, X86::CMOVB64rm, 0 },
484 { X86::CMOVBE16rr, X86::CMOVBE16rm, 0 },
485 { X86::CMOVBE32rr, X86::CMOVBE32rm, 0 },
486 { X86::CMOVBE64rr, X86::CMOVBE64rm, 0 },
487 { X86::CMOVE16rr, X86::CMOVE16rm, 0 },
488 { X86::CMOVE32rr, X86::CMOVE32rm, 0 },
489 { X86::CMOVE64rr, X86::CMOVE64rm, 0 },
490 { X86::CMOVG16rr, X86::CMOVG16rm, 0 },
491 { X86::CMOVG32rr, X86::CMOVG32rm, 0 },
492 { X86::CMOVG64rr, X86::CMOVG64rm, 0 },
493 { X86::CMOVGE16rr, X86::CMOVGE16rm, 0 },
494 { X86::CMOVGE32rr, X86::CMOVGE32rm, 0 },
495 { X86::CMOVGE64rr, X86::CMOVGE64rm, 0 },
496 { X86::CMOVL16rr, X86::CMOVL16rm, 0 },
497 { X86::CMOVL32rr, X86::CMOVL32rm, 0 },
498 { X86::CMOVL64rr, X86::CMOVL64rm, 0 },
499 { X86::CMOVLE16rr, X86::CMOVLE16rm, 0 },
500 { X86::CMOVLE32rr, X86::CMOVLE32rm, 0 },
501 { X86::CMOVLE64rr, X86::CMOVLE64rm, 0 },
502 { X86::CMOVNE16rr, X86::CMOVNE16rm, 0 },
503 { X86::CMOVNE32rr, X86::CMOVNE32rm, 0 },
504 { X86::CMOVNE64rr, X86::CMOVNE64rm, 0 },
505 { X86::CMOVNO16rr, X86::CMOVNO16rm, 0 },
506 { X86::CMOVNO32rr, X86::CMOVNO32rm, 0 },
507 { X86::CMOVNO64rr, X86::CMOVNO64rm, 0 },
508 { X86::CMOVNP16rr, X86::CMOVNP16rm, 0 },
509 { X86::CMOVNP32rr, X86::CMOVNP32rm, 0 },
510 { X86::CMOVNP64rr, X86::CMOVNP64rm, 0 },
511 { X86::CMOVNS16rr, X86::CMOVNS16rm, 0 },
512 { X86::CMOVNS32rr, X86::CMOVNS32rm, 0 },
513 { X86::CMOVNS64rr, X86::CMOVNS64rm, 0 },
514 { X86::CMOVO16rr, X86::CMOVO16rm, 0 },
515 { X86::CMOVO32rr, X86::CMOVO32rm, 0 },
516 { X86::CMOVO64rr, X86::CMOVO64rm, 0 },
517 { X86::CMOVP16rr, X86::CMOVP16rm, 0 },
518 { X86::CMOVP32rr, X86::CMOVP32rm, 0 },
519 { X86::CMOVP64rr, X86::CMOVP64rm, 0 },
520 { X86::CMOVS16rr, X86::CMOVS16rm, 0 },
521 { X86::CMOVS32rr, X86::CMOVS32rm, 0 },
522 { X86::CMOVS64rr, X86::CMOVS64rm, 0 },
523 { X86::CMPPDrri, X86::CMPPDrmi, 16 },
524 { X86::CMPPSrri, X86::CMPPSrmi, 16 },
525 { X86::CMPSDrr, X86::CMPSDrm, 0 },
526 { X86::CMPSSrr, X86::CMPSSrm, 0 },
527 { X86::DIVPDrr, X86::DIVPDrm, 16 },
528 { X86::DIVPSrr, X86::DIVPSrm, 16 },
529 { X86::DIVSDrr, X86::DIVSDrm, 0 },
530 { X86::DIVSSrr, X86::DIVSSrm, 0 },
531 { X86::FsANDNPDrr, X86::FsANDNPDrm, 16 },
532 { X86::FsANDNPSrr, X86::FsANDNPSrm, 16 },
533 { X86::FsANDPDrr, X86::FsANDPDrm, 16 },
534 { X86::FsANDPSrr, X86::FsANDPSrm, 16 },
535 { X86::FsORPDrr, X86::FsORPDrm, 16 },
536 { X86::FsORPSrr, X86::FsORPSrm, 16 },
537 { X86::FsXORPDrr, X86::FsXORPDrm, 16 },
538 { X86::FsXORPSrr, X86::FsXORPSrm, 16 },
539 { X86::HADDPDrr, X86::HADDPDrm, 16 },
540 { X86::HADDPSrr, X86::HADDPSrm, 16 },
541 { X86::HSUBPDrr, X86::HSUBPDrm, 16 },
542 { X86::HSUBPSrr, X86::HSUBPSrm, 16 },
543 { X86::IMUL16rr, X86::IMUL16rm, 0 },
544 { X86::IMUL32rr, X86::IMUL32rm, 0 },
545 { X86::IMUL64rr, X86::IMUL64rm, 0 },
546 { X86::MAXPDrr, X86::MAXPDrm, 16 },
547 { X86::MAXPDrr_Int, X86::MAXPDrm_Int, 16 },
548 { X86::MAXPSrr, X86::MAXPSrm, 16 },
549 { X86::MAXPSrr_Int, X86::MAXPSrm_Int, 16 },
550 { X86::MAXSDrr, X86::MAXSDrm, 0 },
551 { X86::MAXSDrr_Int, X86::MAXSDrm_Int, 0 },
552 { X86::MAXSSrr, X86::MAXSSrm, 0 },
553 { X86::MAXSSrr_Int, X86::MAXSSrm_Int, 0 },
554 { X86::MINPDrr, X86::MINPDrm, 16 },
555 { X86::MINPDrr_Int, X86::MINPDrm_Int, 16 },
556 { X86::MINPSrr, X86::MINPSrm, 16 },
557 { X86::MINPSrr_Int, X86::MINPSrm_Int, 16 },
558 { X86::MINSDrr, X86::MINSDrm, 0 },
559 { X86::MINSDrr_Int, X86::MINSDrm_Int, 0 },
560 { X86::MINSSrr, X86::MINSSrm, 0 },
561 { X86::MINSSrr_Int, X86::MINSSrm_Int, 0 },
562 { X86::MULPDrr, X86::MULPDrm, 16 },
563 { X86::MULPSrr, X86::MULPSrm, 16 },
564 { X86::MULSDrr, X86::MULSDrm, 0 },
565 { X86::MULSSrr, X86::MULSSrm, 0 },
566 { X86::OR16rr, X86::OR16rm, 0 },
567 { X86::OR32rr, X86::OR32rm, 0 },
568 { X86::OR64rr, X86::OR64rm, 0 },
569 { X86::OR8rr, X86::OR8rm, 0 },
570 { X86::ORPDrr, X86::ORPDrm, 16 },
571 { X86::ORPSrr, X86::ORPSrm, 16 },
572 { X86::PACKSSDWrr, X86::PACKSSDWrm, 16 },
573 { X86::PACKSSWBrr, X86::PACKSSWBrm, 16 },
574 { X86::PACKUSWBrr, X86::PACKUSWBrm, 16 },
575 { X86::PADDBrr, X86::PADDBrm, 16 },
576 { X86::PADDDrr, X86::PADDDrm, 16 },
577 { X86::PADDQrr, X86::PADDQrm, 16 },
578 { X86::PADDSBrr, X86::PADDSBrm, 16 },
579 { X86::PADDSWrr, X86::PADDSWrm, 16 },
580 { X86::PADDWrr, X86::PADDWrm, 16 },
581 { X86::PANDNrr, X86::PANDNrm, 16 },
582 { X86::PANDrr, X86::PANDrm, 16 },
583 { X86::PAVGBrr, X86::PAVGBrm, 16 },
584 { X86::PAVGWrr, X86::PAVGWrm, 16 },
585 { X86::PCMPEQBrr, X86::PCMPEQBrm, 16 },
586 { X86::PCMPEQDrr, X86::PCMPEQDrm, 16 },
587 { X86::PCMPEQWrr, X86::PCMPEQWrm, 16 },
588 { X86::PCMPGTBrr, X86::PCMPGTBrm, 16 },
589 { X86::PCMPGTDrr, X86::PCMPGTDrm, 16 },
590 { X86::PCMPGTWrr, X86::PCMPGTWrm, 16 },
591 { X86::PINSRWrri, X86::PINSRWrmi, 16 },
592 { X86::PMADDWDrr, X86::PMADDWDrm, 16 },
593 { X86::PMAXSWrr, X86::PMAXSWrm, 16 },
594 { X86::PMAXUBrr, X86::PMAXUBrm, 16 },
595 { X86::PMINSWrr, X86::PMINSWrm, 16 },
596 { X86::PMINUBrr, X86::PMINUBrm, 16 },
597 { X86::PMULDQrr, X86::PMULDQrm, 16 },
598 { X86::PMULHUWrr, X86::PMULHUWrm, 16 },
599 { X86::PMULHWrr, X86::PMULHWrm, 16 },
600 { X86::PMULLDrr, X86::PMULLDrm, 16 },
Evan Chenga5853792009-07-15 06:10:07 +0000601 { X86::PMULLWrr, X86::PMULLWrm, 16 },
602 { X86::PMULUDQrr, X86::PMULUDQrm, 16 },
603 { X86::PORrr, X86::PORrm, 16 },
604 { X86::PSADBWrr, X86::PSADBWrm, 16 },
605 { X86::PSLLDrr, X86::PSLLDrm, 16 },
606 { X86::PSLLQrr, X86::PSLLQrm, 16 },
607 { X86::PSLLWrr, X86::PSLLWrm, 16 },
608 { X86::PSRADrr, X86::PSRADrm, 16 },
609 { X86::PSRAWrr, X86::PSRAWrm, 16 },
610 { X86::PSRLDrr, X86::PSRLDrm, 16 },
611 { X86::PSRLQrr, X86::PSRLQrm, 16 },
612 { X86::PSRLWrr, X86::PSRLWrm, 16 },
613 { X86::PSUBBrr, X86::PSUBBrm, 16 },
614 { X86::PSUBDrr, X86::PSUBDrm, 16 },
615 { X86::PSUBSBrr, X86::PSUBSBrm, 16 },
616 { X86::PSUBSWrr, X86::PSUBSWrm, 16 },
617 { X86::PSUBWrr, X86::PSUBWrm, 16 },
618 { X86::PUNPCKHBWrr, X86::PUNPCKHBWrm, 16 },
619 { X86::PUNPCKHDQrr, X86::PUNPCKHDQrm, 16 },
620 { X86::PUNPCKHQDQrr, X86::PUNPCKHQDQrm, 16 },
621 { X86::PUNPCKHWDrr, X86::PUNPCKHWDrm, 16 },
622 { X86::PUNPCKLBWrr, X86::PUNPCKLBWrm, 16 },
623 { X86::PUNPCKLDQrr, X86::PUNPCKLDQrm, 16 },
624 { X86::PUNPCKLQDQrr, X86::PUNPCKLQDQrm, 16 },
625 { X86::PUNPCKLWDrr, X86::PUNPCKLWDrm, 16 },
626 { X86::PXORrr, X86::PXORrm, 16 },
627 { X86::SBB32rr, X86::SBB32rm, 0 },
628 { X86::SBB64rr, X86::SBB64rm, 0 },
629 { X86::SHUFPDrri, X86::SHUFPDrmi, 16 },
630 { X86::SHUFPSrri, X86::SHUFPSrmi, 16 },
631 { X86::SUB16rr, X86::SUB16rm, 0 },
632 { X86::SUB32rr, X86::SUB32rm, 0 },
633 { X86::SUB64rr, X86::SUB64rm, 0 },
634 { X86::SUB8rr, X86::SUB8rm, 0 },
635 { X86::SUBPDrr, X86::SUBPDrm, 16 },
636 { X86::SUBPSrr, X86::SUBPSrm, 16 },
637 { X86::SUBSDrr, X86::SUBSDrm, 0 },
638 { X86::SUBSSrr, X86::SUBSSrm, 0 },
Owen Anderson9a184ef2008-01-07 01:35:02 +0000639 // FIXME: TEST*rr -> swapped operand of TEST*mr.
Evan Chenga5853792009-07-15 06:10:07 +0000640 { X86::UNPCKHPDrr, X86::UNPCKHPDrm, 16 },
641 { X86::UNPCKHPSrr, X86::UNPCKHPSrm, 16 },
642 { X86::UNPCKLPDrr, X86::UNPCKLPDrm, 16 },
643 { X86::UNPCKLPSrr, X86::UNPCKLPSrm, 16 },
644 { X86::XOR16rr, X86::XOR16rm, 0 },
645 { X86::XOR32rr, X86::XOR32rm, 0 },
646 { X86::XOR64rr, X86::XOR64rm, 0 },
647 { X86::XOR8rr, X86::XOR8rm, 0 },
648 { X86::XORPDrr, X86::XORPDrm, 16 },
649 { X86::XORPSrr, X86::XORPSrm, 16 }
Owen Anderson9a184ef2008-01-07 01:35:02 +0000650 };
651
652 for (unsigned i = 0, e = array_lengthof(OpTbl2); i != e; ++i) {
653 unsigned RegOp = OpTbl2[i][0];
654 unsigned MemOp = OpTbl2[i][1];
Evan Chenga5853792009-07-15 06:10:07 +0000655 unsigned Align = OpTbl2[i][2];
Dan Gohman55d19662008-07-07 17:46:23 +0000656 if (!RegOp2MemOpTable2.insert(std::make_pair((unsigned*)RegOp,
Evan Chenga5853792009-07-15 06:10:07 +0000657 std::make_pair(MemOp,Align))).second)
Owen Anderson9a184ef2008-01-07 01:35:02 +0000658 assert(false && "Duplicated entries?");
Evan Chenga5853792009-07-15 06:10:07 +0000659 // Index 2, folded load
660 unsigned AuxInfo = 2 | (1 << 4);
Owen Anderson9a184ef2008-01-07 01:35:02 +0000661 if (!MemOp2RegOpTable.insert(std::make_pair((unsigned*)MemOp,
Dan Gohman55d19662008-07-07 17:46:23 +0000662 std::make_pair(RegOp, AuxInfo))).second)
Owen Anderson9a184ef2008-01-07 01:35:02 +0000663 AmbEntries.push_back(MemOp);
664 }
665
666 // Remove ambiguous entries.
667 assert(AmbEntries.empty() && "Duplicated entries in unfolding maps?");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000668}
669
670bool X86InstrInfo::isMoveInstr(const MachineInstr& MI,
Evan Chengf97496a2009-01-20 19:12:24 +0000671 unsigned &SrcReg, unsigned &DstReg,
672 unsigned &SrcSubIdx, unsigned &DstSubIdx) const {
Chris Lattnerff195282008-03-11 19:28:17 +0000673 switch (MI.getOpcode()) {
674 default:
675 return false;
676 case X86::MOV8rr:
Bill Wendling2d1c8222009-04-17 22:40:38 +0000677 case X86::MOV8rr_NOREX:
Chris Lattnerff195282008-03-11 19:28:17 +0000678 case X86::MOV16rr:
679 case X86::MOV32rr:
680 case X86::MOV64rr:
Evan Cheng9ac24d12010-03-14 03:48:46 +0000681 case X86::MOV32rr_TC:
682 case X86::MOV64rr_TC:
Chris Lattnerc81df282008-03-11 19:30:09 +0000683
684 // FP Stack register class copies
685 case X86::MOV_Fp3232: case X86::MOV_Fp6464: case X86::MOV_Fp8080:
686 case X86::MOV_Fp3264: case X86::MOV_Fp3280:
687 case X86::MOV_Fp6432: case X86::MOV_Fp8032:
Dan Gohman66e432b2010-02-28 00:17:42 +0000688
689 // Note that MOVSSrr and MOVSDrr are not considered copies. FR32 and FR64
690 // copies are done with FsMOVAPSrr and FsMOVAPDrr.
691
Chris Lattnerff195282008-03-11 19:28:17 +0000692 case X86::FsMOVAPSrr:
693 case X86::FsMOVAPDrr:
694 case X86::MOVAPSrr:
695 case X86::MOVAPDrr:
Dan Gohmana645d1a2009-01-09 02:40:34 +0000696 case X86::MOVDQArr:
Chris Lattnerff195282008-03-11 19:28:17 +0000697 case X86::MMX_MOVQ64rr:
698 assert(MI.getNumOperands() >= 2 &&
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000699 MI.getOperand(0).isReg() &&
700 MI.getOperand(1).isReg() &&
Chris Lattnerff195282008-03-11 19:28:17 +0000701 "invalid register-register move instruction");
Evan Chengf97496a2009-01-20 19:12:24 +0000702 SrcReg = MI.getOperand(1).getReg();
703 DstReg = MI.getOperand(0).getReg();
704 SrcSubIdx = MI.getOperand(1).getSubReg();
705 DstSubIdx = MI.getOperand(0).getSubReg();
Chris Lattnerff195282008-03-11 19:28:17 +0000706 return true;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000707 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000708}
709
Evan Cheng756aef32010-01-12 00:09:37 +0000710bool
Evan Chengeb485c92010-01-13 00:30:23 +0000711X86InstrInfo::isCoalescableExtInstr(const MachineInstr &MI,
712 unsigned &SrcReg, unsigned &DstReg,
713 unsigned &SubIdx) const {
Evan Cheng756aef32010-01-12 00:09:37 +0000714 switch (MI.getOpcode()) {
715 default: break;
716 case X86::MOVSX16rr8:
717 case X86::MOVZX16rr8:
718 case X86::MOVSX32rr8:
719 case X86::MOVZX32rr8:
720 case X86::MOVSX64rr8:
721 case X86::MOVZX64rr8:
Evan Cheng64b06562010-01-13 08:01:32 +0000722 if (!TM.getSubtarget<X86Subtarget>().is64Bit())
723 // It's not always legal to reference the low 8-bit of the larger
724 // register in 32-bit mode.
725 return false;
Evan Cheng756aef32010-01-12 00:09:37 +0000726 case X86::MOVSX32rr16:
727 case X86::MOVZX32rr16:
728 case X86::MOVSX64rr16:
729 case X86::MOVZX64rr16:
730 case X86::MOVSX64rr32:
731 case X86::MOVZX64rr32: {
732 if (MI.getOperand(0).getSubReg() || MI.getOperand(1).getSubReg())
733 // Be conservative.
734 return false;
Evan Cheng756aef32010-01-12 00:09:37 +0000735 SrcReg = MI.getOperand(1).getReg();
736 DstReg = MI.getOperand(0).getReg();
Evan Cheng756aef32010-01-12 00:09:37 +0000737 switch (MI.getOpcode()) {
738 default:
739 llvm_unreachable(0);
740 break;
741 case X86::MOVSX16rr8:
742 case X86::MOVZX16rr8:
743 case X86::MOVSX32rr8:
744 case X86::MOVZX32rr8:
745 case X86::MOVSX64rr8:
746 case X86::MOVZX64rr8:
Jakob Stoklund Olesenbe0da7c2010-05-25 17:04:16 +0000747 SubIdx = X86::sub_8bit;
Evan Cheng756aef32010-01-12 00:09:37 +0000748 break;
749 case X86::MOVSX32rr16:
750 case X86::MOVZX32rr16:
751 case X86::MOVSX64rr16:
752 case X86::MOVZX64rr16:
Jakob Stoklund Olesenbe0da7c2010-05-25 17:04:16 +0000753 SubIdx = X86::sub_16bit;
Evan Cheng756aef32010-01-12 00:09:37 +0000754 break;
755 case X86::MOVSX64rr32:
756 case X86::MOVZX64rr32:
Jakob Stoklund Olesenbe0da7c2010-05-25 17:04:16 +0000757 SubIdx = X86::sub_32bit;
Evan Cheng756aef32010-01-12 00:09:37 +0000758 break;
759 }
Evan Chengeb485c92010-01-13 00:30:23 +0000760 return true;
Evan Cheng756aef32010-01-12 00:09:37 +0000761 }
762 }
Evan Chengeb485c92010-01-13 00:30:23 +0000763 return false;
Evan Cheng756aef32010-01-12 00:09:37 +0000764}
765
David Greene138ae532009-11-12 20:55:29 +0000766/// isFrameOperand - Return true and the FrameIndex if the specified
767/// operand and follow operands form a reference to the stack frame.
768bool X86InstrInfo::isFrameOperand(const MachineInstr *MI, unsigned int Op,
769 int &FrameIndex) const {
770 if (MI->getOperand(Op).isFI() && MI->getOperand(Op+1).isImm() &&
771 MI->getOperand(Op+2).isReg() && MI->getOperand(Op+3).isImm() &&
772 MI->getOperand(Op+1).getImm() == 1 &&
773 MI->getOperand(Op+2).getReg() == 0 &&
774 MI->getOperand(Op+3).getImm() == 0) {
775 FrameIndex = MI->getOperand(Op).getIndex();
776 return true;
777 }
778 return false;
779}
780
David Greene98c70f72009-11-13 00:29:53 +0000781static bool isFrameLoadOpcode(int Opcode) {
782 switch (Opcode) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000783 default: break;
784 case X86::MOV8rm:
785 case X86::MOV16rm:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000786 case X86::MOV32rm:
Jakob Stoklund Olesen4a72bf52010-07-09 21:27:55 +0000787 case X86::MOV32rm_TC:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000788 case X86::MOV64rm:
Jakob Stoklund Olesen4a72bf52010-07-09 21:27:55 +0000789 case X86::MOV64rm_TC:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000790 case X86::LD_Fp64m:
791 case X86::MOVSSrm:
792 case X86::MOVSDrm:
793 case X86::MOVAPSrm:
794 case X86::MOVAPDrm:
Dan Gohmana645d1a2009-01-09 02:40:34 +0000795 case X86::MOVDQArm:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000796 case X86::MMX_MOVD64rm:
797 case X86::MMX_MOVQ64rm:
David Greene98c70f72009-11-13 00:29:53 +0000798 return true;
799 break;
800 }
801 return false;
802}
803
804static bool isFrameStoreOpcode(int Opcode) {
805 switch (Opcode) {
806 default: break;
807 case X86::MOV8mr:
808 case X86::MOV16mr:
809 case X86::MOV32mr:
Jakob Stoklund Olesen4a72bf52010-07-09 21:27:55 +0000810 case X86::MOV32mr_TC:
David Greene98c70f72009-11-13 00:29:53 +0000811 case X86::MOV64mr:
Jakob Stoklund Olesen4a72bf52010-07-09 21:27:55 +0000812 case X86::MOV64mr_TC:
David Greene98c70f72009-11-13 00:29:53 +0000813 case X86::ST_FpP64m:
814 case X86::MOVSSmr:
815 case X86::MOVSDmr:
816 case X86::MOVAPSmr:
817 case X86::MOVAPDmr:
818 case X86::MOVDQAmr:
819 case X86::MMX_MOVD64mr:
820 case X86::MMX_MOVQ64mr:
821 case X86::MMX_MOVNTQmr:
822 return true;
823 }
824 return false;
825}
826
827unsigned X86InstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
828 int &FrameIndex) const {
829 if (isFrameLoadOpcode(MI->getOpcode()))
830 if (isFrameOperand(MI, 1, FrameIndex))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000831 return MI->getOperand(0).getReg();
David Greene98c70f72009-11-13 00:29:53 +0000832 return 0;
833}
834
835unsigned X86InstrInfo::isLoadFromStackSlotPostFE(const MachineInstr *MI,
836 int &FrameIndex) const {
837 if (isFrameLoadOpcode(MI->getOpcode())) {
838 unsigned Reg;
839 if ((Reg = isLoadFromStackSlot(MI, FrameIndex)))
840 return Reg;
David Greene138ae532009-11-12 20:55:29 +0000841 // Check for post-frame index elimination operations
David Greene647636f2009-12-04 22:38:46 +0000842 const MachineMemOperand *Dummy;
843 return hasLoadFromStackSlot(MI, Dummy, FrameIndex);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000844 }
845 return 0;
846}
847
David Greene138ae532009-11-12 20:55:29 +0000848bool X86InstrInfo::hasLoadFromStackSlot(const MachineInstr *MI,
David Greene647636f2009-12-04 22:38:46 +0000849 const MachineMemOperand *&MMO,
David Greene138ae532009-11-12 20:55:29 +0000850 int &FrameIndex) const {
851 for (MachineInstr::mmo_iterator o = MI->memoperands_begin(),
852 oe = MI->memoperands_end();
853 o != oe;
854 ++o) {
855 if ((*o)->isLoad() && (*o)->getValue())
856 if (const FixedStackPseudoSourceValue *Value =
857 dyn_cast<const FixedStackPseudoSourceValue>((*o)->getValue())) {
858 FrameIndex = Value->getFrameIndex();
David Greene647636f2009-12-04 22:38:46 +0000859 MMO = *o;
David Greene138ae532009-11-12 20:55:29 +0000860 return true;
861 }
862 }
863 return false;
864}
865
Dan Gohman90feee22008-11-18 19:49:32 +0000866unsigned X86InstrInfo::isStoreToStackSlot(const MachineInstr *MI,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000867 int &FrameIndex) const {
David Greene98c70f72009-11-13 00:29:53 +0000868 if (isFrameStoreOpcode(MI->getOpcode()))
869 if (isFrameOperand(MI, 0, FrameIndex))
Chris Lattner65d58572010-07-08 22:41:28 +0000870 return MI->getOperand(X86::AddrNumOperands).getReg();
David Greene98c70f72009-11-13 00:29:53 +0000871 return 0;
872}
873
874unsigned X86InstrInfo::isStoreToStackSlotPostFE(const MachineInstr *MI,
875 int &FrameIndex) const {
876 if (isFrameStoreOpcode(MI->getOpcode())) {
877 unsigned Reg;
878 if ((Reg = isStoreToStackSlot(MI, FrameIndex)))
879 return Reg;
David Greene138ae532009-11-12 20:55:29 +0000880 // Check for post-frame index elimination operations
David Greene647636f2009-12-04 22:38:46 +0000881 const MachineMemOperand *Dummy;
882 return hasStoreToStackSlot(MI, Dummy, FrameIndex);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000883 }
884 return 0;
885}
886
David Greene138ae532009-11-12 20:55:29 +0000887bool X86InstrInfo::hasStoreToStackSlot(const MachineInstr *MI,
David Greene647636f2009-12-04 22:38:46 +0000888 const MachineMemOperand *&MMO,
David Greene138ae532009-11-12 20:55:29 +0000889 int &FrameIndex) const {
890 for (MachineInstr::mmo_iterator o = MI->memoperands_begin(),
891 oe = MI->memoperands_end();
892 o != oe;
893 ++o) {
894 if ((*o)->isStore() && (*o)->getValue())
895 if (const FixedStackPseudoSourceValue *Value =
896 dyn_cast<const FixedStackPseudoSourceValue>((*o)->getValue())) {
897 FrameIndex = Value->getFrameIndex();
David Greene647636f2009-12-04 22:38:46 +0000898 MMO = *o;
David Greene138ae532009-11-12 20:55:29 +0000899 return true;
900 }
901 }
902 return false;
903}
904
Evan Chengb819a512008-03-27 01:45:11 +0000905/// regIsPICBase - Return true if register is PIC base (i.e.g defined by
906/// X86::MOVPC32r.
Dan Gohman221a4372008-07-07 23:14:23 +0000907static bool regIsPICBase(unsigned BaseReg, const MachineRegisterInfo &MRI) {
Evan Chengb819a512008-03-27 01:45:11 +0000908 bool isPICBase = false;
909 for (MachineRegisterInfo::def_iterator I = MRI.def_begin(BaseReg),
910 E = MRI.def_end(); I != E; ++I) {
911 MachineInstr *DefMI = I.getOperand().getParent();
912 if (DefMI->getOpcode() != X86::MOVPC32r)
913 return false;
914 assert(!isPICBase && "More than one PIC base?");
915 isPICBase = true;
916 }
917 return isPICBase;
918}
Evan Chenge9caab52008-03-31 07:54:19 +0000919
Bill Wendlingb1cc1302008-05-12 20:54:26 +0000920bool
Dan Gohman1ef18852009-10-10 00:34:18 +0000921X86InstrInfo::isReallyTriviallyReMaterializable(const MachineInstr *MI,
922 AliasAnalysis *AA) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000923 switch (MI->getOpcode()) {
924 default: break;
Evan Cheng1ea8e6b2008-03-27 01:41:09 +0000925 case X86::MOV8rm:
926 case X86::MOV16rm:
Evan Cheng1ea8e6b2008-03-27 01:41:09 +0000927 case X86::MOV32rm:
Evan Cheng1ea8e6b2008-03-27 01:41:09 +0000928 case X86::MOV64rm:
929 case X86::LD_Fp64m:
930 case X86::MOVSSrm:
931 case X86::MOVSDrm:
932 case X86::MOVAPSrm:
Evan Cheng9d7cd4e2009-11-16 21:56:03 +0000933 case X86::MOVUPSrm:
Evan Cheng8e664712009-11-17 09:51:18 +0000934 case X86::MOVUPSrm_Int:
Evan Cheng1ea8e6b2008-03-27 01:41:09 +0000935 case X86::MOVAPDrm:
Dan Gohmana645d1a2009-01-09 02:40:34 +0000936 case X86::MOVDQArm:
Evan Cheng1ea8e6b2008-03-27 01:41:09 +0000937 case X86::MMX_MOVD64rm:
Evan Cheng8e664712009-11-17 09:51:18 +0000938 case X86::MMX_MOVQ64rm:
939 case X86::FsMOVAPSrm:
940 case X86::FsMOVAPDrm: {
Evan Cheng1ea8e6b2008-03-27 01:41:09 +0000941 // Loads from constant pools are trivially rematerializable.
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000942 if (MI->getOperand(1).isReg() &&
943 MI->getOperand(2).isImm() &&
944 MI->getOperand(3).isReg() && MI->getOperand(3).getReg() == 0 &&
Dan Gohman1ef18852009-10-10 00:34:18 +0000945 MI->isInvariantLoad(AA)) {
Evan Cheng1ea8e6b2008-03-27 01:41:09 +0000946 unsigned BaseReg = MI->getOperand(1).getReg();
Chris Lattnerdc6fc472009-06-27 04:16:01 +0000947 if (BaseReg == 0 || BaseReg == X86::RIP)
Evan Cheng1ea8e6b2008-03-27 01:41:09 +0000948 return true;
949 // Allow re-materialization of PIC load.
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000950 if (!ReMatPICStubLoad && MI->getOperand(4).isGlobal())
Evan Chengc87df652008-04-01 23:26:12 +0000951 return false;
Dan Gohman221a4372008-07-07 23:14:23 +0000952 const MachineFunction &MF = *MI->getParent()->getParent();
953 const MachineRegisterInfo &MRI = MF.getRegInfo();
Evan Cheng1ea8e6b2008-03-27 01:41:09 +0000954 bool isPICBase = false;
955 for (MachineRegisterInfo::def_iterator I = MRI.def_begin(BaseReg),
956 E = MRI.def_end(); I != E; ++I) {
957 MachineInstr *DefMI = I.getOperand().getParent();
958 if (DefMI->getOpcode() != X86::MOVPC32r)
959 return false;
960 assert(!isPICBase && "More than one PIC base?");
961 isPICBase = true;
962 }
963 return isPICBase;
964 }
965 return false;
Evan Cheng60490e62008-02-22 09:25:47 +0000966 }
Evan Cheng1ea8e6b2008-03-27 01:41:09 +0000967
968 case X86::LEA32r:
969 case X86::LEA64r: {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000970 if (MI->getOperand(2).isImm() &&
971 MI->getOperand(3).isReg() && MI->getOperand(3).getReg() == 0 &&
972 !MI->getOperand(4).isReg()) {
Evan Cheng1ea8e6b2008-03-27 01:41:09 +0000973 // lea fi#, lea GV, etc. are all rematerializable.
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000974 if (!MI->getOperand(1).isReg())
Dan Gohmanbee19a42008-09-26 21:30:20 +0000975 return true;
Evan Cheng1ea8e6b2008-03-27 01:41:09 +0000976 unsigned BaseReg = MI->getOperand(1).getReg();
977 if (BaseReg == 0)
978 return true;
979 // Allow re-materialization of lea PICBase + x.
Dan Gohman221a4372008-07-07 23:14:23 +0000980 const MachineFunction &MF = *MI->getParent()->getParent();
981 const MachineRegisterInfo &MRI = MF.getRegInfo();
Evan Chengb819a512008-03-27 01:45:11 +0000982 return regIsPICBase(BaseReg, MRI);
Evan Cheng1ea8e6b2008-03-27 01:41:09 +0000983 }
984 return false;
985 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000986 }
Evan Cheng1ea8e6b2008-03-27 01:41:09 +0000987
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000988 // All other instructions marked M_REMATERIALIZABLE are always trivially
989 // rematerializable.
990 return true;
991}
992
Evan Chengc564ded2008-06-24 07:10:51 +0000993/// isSafeToClobberEFLAGS - Return true if it's safe insert an instruction that
994/// would clobber the EFLAGS condition register. Note the result may be
995/// conservative. If it cannot definitely determine the safety after visiting
Dan Gohmanf20cb162009-10-14 00:08:59 +0000996/// a few instructions in each direction it assumes it's not safe.
Evan Chengc564ded2008-06-24 07:10:51 +0000997static bool isSafeToClobberEFLAGS(MachineBasicBlock &MBB,
998 MachineBasicBlock::iterator I) {
Evan Cheng9464b212010-03-23 20:35:45 +0000999 MachineBasicBlock::iterator E = MBB.end();
1000
Dan Gohman3588f9d2008-10-21 03:24:31 +00001001 // It's always safe to clobber EFLAGS at the end of a block.
Evan Cheng9464b212010-03-23 20:35:45 +00001002 if (I == E)
Dan Gohman3588f9d2008-10-21 03:24:31 +00001003 return true;
1004
Evan Chengc564ded2008-06-24 07:10:51 +00001005 // For compile time consideration, if we are not able to determine the
Dan Gohmanf20cb162009-10-14 00:08:59 +00001006 // safety after visiting 4 instructions in each direction, we will assume
1007 // it's not safe.
1008 MachineBasicBlock::iterator Iter = I;
1009 for (unsigned i = 0; i < 4; ++i) {
Evan Chengc564ded2008-06-24 07:10:51 +00001010 bool SeenDef = false;
Dan Gohmanf20cb162009-10-14 00:08:59 +00001011 for (unsigned j = 0, e = Iter->getNumOperands(); j != e; ++j) {
1012 MachineOperand &MO = Iter->getOperand(j);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00001013 if (!MO.isReg())
Evan Chengc564ded2008-06-24 07:10:51 +00001014 continue;
1015 if (MO.getReg() == X86::EFLAGS) {
1016 if (MO.isUse())
1017 return false;
1018 SeenDef = true;
1019 }
1020 }
1021
1022 if (SeenDef)
1023 // This instruction defines EFLAGS, no need to look any further.
1024 return true;
Dan Gohmanf20cb162009-10-14 00:08:59 +00001025 ++Iter;
Evan Cheng9464b212010-03-23 20:35:45 +00001026 // Skip over DBG_VALUE.
1027 while (Iter != E && Iter->isDebugValue())
1028 ++Iter;
Dan Gohman3588f9d2008-10-21 03:24:31 +00001029
1030 // If we make it to the end of the block, it's safe to clobber EFLAGS.
Evan Cheng9464b212010-03-23 20:35:45 +00001031 if (Iter == E)
Dan Gohmanf20cb162009-10-14 00:08:59 +00001032 return true;
1033 }
1034
Evan Cheng9464b212010-03-23 20:35:45 +00001035 MachineBasicBlock::iterator B = MBB.begin();
Dan Gohmanf20cb162009-10-14 00:08:59 +00001036 Iter = I;
1037 for (unsigned i = 0; i < 4; ++i) {
1038 // If we make it to the beginning of the block, it's safe to clobber
1039 // EFLAGS iff EFLAGS is not live-in.
Evan Cheng9464b212010-03-23 20:35:45 +00001040 if (Iter == B)
Dan Gohmanf20cb162009-10-14 00:08:59 +00001041 return !MBB.isLiveIn(X86::EFLAGS);
1042
1043 --Iter;
Evan Cheng9464b212010-03-23 20:35:45 +00001044 // Skip over DBG_VALUE.
1045 while (Iter != B && Iter->isDebugValue())
1046 --Iter;
1047
Dan Gohmanf20cb162009-10-14 00:08:59 +00001048 bool SawKill = false;
1049 for (unsigned j = 0, e = Iter->getNumOperands(); j != e; ++j) {
1050 MachineOperand &MO = Iter->getOperand(j);
1051 if (MO.isReg() && MO.getReg() == X86::EFLAGS) {
1052 if (MO.isDef()) return MO.isDead();
1053 if (MO.isKill()) SawKill = true;
1054 }
1055 }
1056
1057 if (SawKill)
1058 // This instruction kills EFLAGS and doesn't redefine it, so
1059 // there's no need to look further.
Dan Gohman3588f9d2008-10-21 03:24:31 +00001060 return true;
Evan Chengc564ded2008-06-24 07:10:51 +00001061 }
1062
1063 // Conservative answer.
1064 return false;
1065}
1066
Evan Cheng7d73efc2008-03-31 20:40:39 +00001067void X86InstrInfo::reMaterialize(MachineBasicBlock &MBB,
1068 MachineBasicBlock::iterator I,
Evan Cheng463a3e42009-07-16 09:20:10 +00001069 unsigned DestReg, unsigned SubIdx,
Evan Chenga88d1ac2009-11-14 02:55:43 +00001070 const MachineInstr *Orig,
Jakob Stoklund Olesend2776e02010-06-02 22:47:25 +00001071 const TargetRegisterInfo &TRI) const {
Dan Gohman6bf788c2010-05-07 01:28:10 +00001072 DebugLoc DL = Orig->getDebugLoc();
Bill Wendling13ee2e42009-02-11 21:51:19 +00001073
Evan Cheng7d73efc2008-03-31 20:40:39 +00001074 // MOV32r0 etc. are implemented with xor which clobbers condition code.
1075 // Re-materialize them as movri instructions to avoid side effects.
Evan Cheng463a3e42009-07-16 09:20:10 +00001076 bool Clone = true;
1077 unsigned Opc = Orig->getOpcode();
1078 switch (Opc) {
Evan Chengc564ded2008-06-24 07:10:51 +00001079 default: break;
Evan Cheng7d73efc2008-03-31 20:40:39 +00001080 case X86::MOV8r0:
Dan Gohmanb9e1c8d2010-01-12 04:42:54 +00001081 case X86::MOV16r0:
1082 case X86::MOV32r0:
1083 case X86::MOV64r0: {
Evan Chengc564ded2008-06-24 07:10:51 +00001084 if (!isSafeToClobberEFLAGS(MBB, I)) {
Evan Cheng463a3e42009-07-16 09:20:10 +00001085 switch (Opc) {
Evan Chengc564ded2008-06-24 07:10:51 +00001086 default: break;
1087 case X86::MOV8r0: Opc = X86::MOV8ri; break;
Dan Gohmanb9e1c8d2010-01-12 04:42:54 +00001088 case X86::MOV16r0: Opc = X86::MOV16ri; break;
Evan Chengc564ded2008-06-24 07:10:51 +00001089 case X86::MOV32r0: Opc = X86::MOV32ri; break;
Dan Gohmanfb5c85f2010-02-26 16:49:27 +00001090 case X86::MOV64r0: Opc = X86::MOV64ri64i32; break;
Evan Chengc564ded2008-06-24 07:10:51 +00001091 }
Evan Cheng463a3e42009-07-16 09:20:10 +00001092 Clone = false;
Evan Chengc564ded2008-06-24 07:10:51 +00001093 }
Evan Cheng7d73efc2008-03-31 20:40:39 +00001094 break;
Evan Chengc564ded2008-06-24 07:10:51 +00001095 }
1096 }
1097
Evan Cheng463a3e42009-07-16 09:20:10 +00001098 if (Clone) {
Dan Gohman221a4372008-07-07 23:14:23 +00001099 MachineInstr *MI = MBB.getParent()->CloneMachineInstr(Orig);
Evan Cheng7d73efc2008-03-31 20:40:39 +00001100 MBB.insert(I, MI);
Evan Cheng463a3e42009-07-16 09:20:10 +00001101 } else {
Jakob Stoklund Olesend2776e02010-06-02 22:47:25 +00001102 BuildMI(MBB, I, DL, get(Opc)).addOperand(Orig->getOperand(0)).addImm(0);
Evan Cheng7d73efc2008-03-31 20:40:39 +00001103 }
Evan Cheng1c32d2d2008-04-16 23:44:44 +00001104
Evan Cheng463a3e42009-07-16 09:20:10 +00001105 MachineInstr *NewMI = prior(I);
Jakob Stoklund Olesend2776e02010-06-02 22:47:25 +00001106 NewMI->substituteRegister(Orig->getOperand(0).getReg(), DestReg, SubIdx, TRI);
Evan Cheng7d73efc2008-03-31 20:40:39 +00001107}
1108
Evan Chengfa1a4952007-10-05 08:04:01 +00001109/// hasLiveCondCodeDef - True if MI has a condition code def, e.g. EFLAGS, that
1110/// is not marked dead.
1111static bool hasLiveCondCodeDef(MachineInstr *MI) {
Evan Chengfa1a4952007-10-05 08:04:01 +00001112 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1113 MachineOperand &MO = MI->getOperand(i);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00001114 if (MO.isReg() && MO.isDef() &&
Evan Chengfa1a4952007-10-05 08:04:01 +00001115 MO.getReg() == X86::EFLAGS && !MO.isDead()) {
1116 return true;
1117 }
1118 }
1119 return false;
1120}
1121
Evan Cheng85979012009-12-12 20:03:14 +00001122/// convertToThreeAddressWithLEA - Helper for convertToThreeAddress when
Evan Chengf031da82009-12-11 06:01:48 +00001123/// 16-bit LEA is disabled, use 32-bit LEA to form 3-address code by promoting
1124/// to a 32-bit superregister and then truncating back down to a 16-bit
1125/// subregister.
1126MachineInstr *
1127X86InstrInfo::convertToThreeAddressWithLEA(unsigned MIOpc,
1128 MachineFunction::iterator &MFI,
1129 MachineBasicBlock::iterator &MBBI,
1130 LiveVariables *LV) const {
1131 MachineInstr *MI = MBBI;
1132 unsigned Dest = MI->getOperand(0).getReg();
1133 unsigned Src = MI->getOperand(1).getReg();
1134 bool isDead = MI->getOperand(0).isDead();
1135 bool isKill = MI->getOperand(1).isKill();
1136
1137 unsigned Opc = TM.getSubtarget<X86Subtarget>().is64Bit()
1138 ? X86::LEA64_32r : X86::LEA32r;
1139 MachineRegisterInfo &RegInfo = MFI->getParent()->getRegInfo();
1140 unsigned leaInReg = RegInfo.createVirtualRegister(&X86::GR32RegClass);
1141 unsigned leaOutReg = RegInfo.createVirtualRegister(&X86::GR32RegClass);
1142
1143 // Build and insert into an implicit UNDEF value. This is OK because
1144 // well be shifting and then extracting the lower 16-bits.
Evan Cheng85979012009-12-12 20:03:14 +00001145 // This has the potential to cause partial register stall. e.g.
Evan Cheng9357ab42009-12-12 18:55:26 +00001146 // movw (%rbp,%rcx,2), %dx
1147 // leal -65(%rdx), %esi
Evan Cheng85979012009-12-12 20:03:14 +00001148 // But testing has shown this *does* help performance in 64-bit mode (at
1149 // least on modern x86 machines).
Evan Chengf031da82009-12-11 06:01:48 +00001150 BuildMI(*MFI, MBBI, MI->getDebugLoc(), get(X86::IMPLICIT_DEF), leaInReg);
1151 MachineInstr *InsMI =
Jakob Stoklund Olesen68abc432010-07-08 16:40:15 +00001152 BuildMI(*MFI, MBBI, MI->getDebugLoc(), get(TargetOpcode::COPY))
1153 .addReg(leaInReg, RegState::Define, X86::sub_16bit)
1154 .addReg(Src, getKillRegState(isKill));
Evan Chengf031da82009-12-11 06:01:48 +00001155
1156 MachineInstrBuilder MIB = BuildMI(*MFI, MBBI, MI->getDebugLoc(),
1157 get(Opc), leaOutReg);
1158 switch (MIOpc) {
1159 default:
1160 llvm_unreachable(0);
1161 break;
1162 case X86::SHL16ri: {
1163 unsigned ShAmt = MI->getOperand(2).getImm();
1164 MIB.addReg(0).addImm(1 << ShAmt)
Chris Lattnereed3f012010-07-08 23:46:44 +00001165 .addReg(leaInReg, RegState::Kill).addImm(0).addReg(0);
Evan Chengf031da82009-12-11 06:01:48 +00001166 break;
1167 }
1168 case X86::INC16r:
1169 case X86::INC64_16r:
Chris Lattnereed3f012010-07-08 23:46:44 +00001170 addRegOffset(MIB, leaInReg, true, 1);
Evan Chengf031da82009-12-11 06:01:48 +00001171 break;
1172 case X86::DEC16r:
1173 case X86::DEC64_16r:
Chris Lattnereed3f012010-07-08 23:46:44 +00001174 addRegOffset(MIB, leaInReg, true, -1);
Evan Chengf031da82009-12-11 06:01:48 +00001175 break;
1176 case X86::ADD16ri:
1177 case X86::ADD16ri8:
Chris Lattnereed3f012010-07-08 23:46:44 +00001178 addRegOffset(MIB, leaInReg, true, MI->getOperand(2).getImm());
Evan Chengf031da82009-12-11 06:01:48 +00001179 break;
1180 case X86::ADD16rr: {
1181 unsigned Src2 = MI->getOperand(2).getReg();
1182 bool isKill2 = MI->getOperand(2).isKill();
1183 unsigned leaInReg2 = 0;
1184 MachineInstr *InsMI2 = 0;
1185 if (Src == Src2) {
1186 // ADD16rr %reg1028<kill>, %reg1028
1187 // just a single insert_subreg.
1188 addRegReg(MIB, leaInReg, true, leaInReg, false);
1189 } else {
1190 leaInReg2 = RegInfo.createVirtualRegister(&X86::GR32RegClass);
1191 // Build and insert into an implicit UNDEF value. This is OK because
1192 // well be shifting and then extracting the lower 16-bits.
1193 BuildMI(*MFI, MIB, MI->getDebugLoc(), get(X86::IMPLICIT_DEF), leaInReg2);
1194 InsMI2 =
Jakob Stoklund Olesen68abc432010-07-08 16:40:15 +00001195 BuildMI(*MFI, MIB, MI->getDebugLoc(), get(TargetOpcode::COPY))
1196 .addReg(leaInReg2, RegState::Define, X86::sub_16bit)
1197 .addReg(Src2, getKillRegState(isKill2));
Evan Chengf031da82009-12-11 06:01:48 +00001198 addRegReg(MIB, leaInReg, true, leaInReg2, true);
1199 }
1200 if (LV && isKill2 && InsMI2)
1201 LV->replaceKillInstruction(Src2, MI, InsMI2);
1202 break;
1203 }
1204 }
1205
1206 MachineInstr *NewMI = MIB;
1207 MachineInstr *ExtMI =
Jakob Stoklund Olesen4dc8a1e2010-07-08 16:40:22 +00001208 BuildMI(*MFI, MBBI, MI->getDebugLoc(), get(TargetOpcode::COPY))
Evan Chengf031da82009-12-11 06:01:48 +00001209 .addReg(Dest, RegState::Define | getDeadRegState(isDead))
Jakob Stoklund Olesen4dc8a1e2010-07-08 16:40:22 +00001210 .addReg(leaOutReg, RegState::Kill, X86::sub_16bit);
Evan Chengf031da82009-12-11 06:01:48 +00001211
1212 if (LV) {
1213 // Update live variables
1214 LV->getVarInfo(leaInReg).Kills.push_back(NewMI);
1215 LV->getVarInfo(leaOutReg).Kills.push_back(ExtMI);
1216 if (isKill)
1217 LV->replaceKillInstruction(Src, MI, InsMI);
1218 if (isDead)
1219 LV->replaceKillInstruction(Dest, MI, ExtMI);
1220 }
1221
1222 return ExtMI;
1223}
1224
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001225/// convertToThreeAddress - This method must be implemented by targets that
1226/// set the M_CONVERTIBLE_TO_3_ADDR flag. When this flag is set, the target
1227/// may be able to convert a two-address instruction into a true
1228/// three-address instruction on demand. This allows the X86 target (for
1229/// example) to convert ADD and SHL instructions into LEA instructions if they
1230/// would require register copies due to two-addressness.
1231///
1232/// This method returns a null pointer if the transformation cannot be
1233/// performed, otherwise it returns the new instruction.
1234///
1235MachineInstr *
1236X86InstrInfo::convertToThreeAddress(MachineFunction::iterator &MFI,
1237 MachineBasicBlock::iterator &MBBI,
Owen Andersonc6959722008-07-02 23:41:07 +00001238 LiveVariables *LV) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001239 MachineInstr *MI = MBBI;
Dan Gohman221a4372008-07-07 23:14:23 +00001240 MachineFunction &MF = *MI->getParent()->getParent();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001241 // All instructions input are two-addr instructions. Get the known operands.
1242 unsigned Dest = MI->getOperand(0).getReg();
1243 unsigned Src = MI->getOperand(1).getReg();
Evan Chenge52c1912008-07-03 09:09:37 +00001244 bool isDead = MI->getOperand(0).isDead();
1245 bool isKill = MI->getOperand(1).isKill();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001246
1247 MachineInstr *NewMI = NULL;
1248 // FIXME: 16-bit LEA's are really slow on Athlons, but not bad on P4's. When
1249 // we have better subtarget support, enable the 16-bit LEA generation here.
Evan Cheng85979012009-12-12 20:03:14 +00001250 // 16-bit LEA is also slow on Core2.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001251 bool DisableLEA16 = true;
Evan Cheng85979012009-12-12 20:03:14 +00001252 bool is64Bit = TM.getSubtarget<X86Subtarget>().is64Bit();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001253
Evan Cheng6b96ed32007-10-05 20:34:26 +00001254 unsigned MIOpc = MI->getOpcode();
1255 switch (MIOpc) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001256 case X86::SHUFPSrri: {
1257 assert(MI->getNumOperands() == 4 && "Unknown shufps instruction!");
1258 if (!TM.getSubtarget<X86Subtarget>().hasSSE2()) return 0;
1259
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001260 unsigned B = MI->getOperand(1).getReg();
1261 unsigned C = MI->getOperand(2).getReg();
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001262 if (B != C) return 0;
Evan Chenge52c1912008-07-03 09:09:37 +00001263 unsigned A = MI->getOperand(0).getReg();
1264 unsigned M = MI->getOperand(3).getImm();
Bill Wendling13ee2e42009-02-11 21:51:19 +00001265 NewMI = BuildMI(MF, MI->getDebugLoc(), get(X86::PSHUFDri))
Bill Wendling2b739762009-05-13 21:33:08 +00001266 .addReg(A, RegState::Define | getDeadRegState(isDead))
1267 .addReg(B, getKillRegState(isKill)).addImm(M);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001268 break;
1269 }
1270 case X86::SHL64ri: {
Evan Cheng55687072007-09-14 21:48:26 +00001271 assert(MI->getNumOperands() >= 3 && "Unknown shift instruction!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001272 // NOTE: LEA doesn't produce flags like shift does, but LLVM never uses
1273 // the flags produced by a shift yet, so this is safe.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001274 unsigned ShAmt = MI->getOperand(2).getImm();
1275 if (ShAmt == 0 || ShAmt >= 4) return 0;
Evan Chenge52c1912008-07-03 09:09:37 +00001276
Bill Wendling13ee2e42009-02-11 21:51:19 +00001277 NewMI = BuildMI(MF, MI->getDebugLoc(), get(X86::LEA64r))
Bill Wendling2b739762009-05-13 21:33:08 +00001278 .addReg(Dest, RegState::Define | getDeadRegState(isDead))
1279 .addReg(0).addImm(1 << ShAmt)
1280 .addReg(Src, getKillRegState(isKill))
Chris Lattnereed3f012010-07-08 23:46:44 +00001281 .addImm(0).addReg(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001282 break;
1283 }
1284 case X86::SHL32ri: {
Evan Cheng55687072007-09-14 21:48:26 +00001285 assert(MI->getNumOperands() >= 3 && "Unknown shift instruction!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001286 // NOTE: LEA doesn't produce flags like shift does, but LLVM never uses
1287 // the flags produced by a shift yet, so this is safe.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001288 unsigned ShAmt = MI->getOperand(2).getImm();
1289 if (ShAmt == 0 || ShAmt >= 4) return 0;
Evan Chenge52c1912008-07-03 09:09:37 +00001290
Evan Cheng85979012009-12-12 20:03:14 +00001291 unsigned Opc = is64Bit ? X86::LEA64_32r : X86::LEA32r;
Bill Wendling13ee2e42009-02-11 21:51:19 +00001292 NewMI = BuildMI(MF, MI->getDebugLoc(), get(Opc))
Bill Wendling2b739762009-05-13 21:33:08 +00001293 .addReg(Dest, RegState::Define | getDeadRegState(isDead))
Evan Chenge52c1912008-07-03 09:09:37 +00001294 .addReg(0).addImm(1 << ShAmt)
Chris Lattnereed3f012010-07-08 23:46:44 +00001295 .addReg(Src, getKillRegState(isKill)).addImm(0).addReg(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001296 break;
1297 }
1298 case X86::SHL16ri: {
Evan Cheng55687072007-09-14 21:48:26 +00001299 assert(MI->getNumOperands() >= 3 && "Unknown shift instruction!");
Evan Cheng0b1e8712007-09-06 00:14:41 +00001300 // NOTE: LEA doesn't produce flags like shift does, but LLVM never uses
1301 // the flags produced by a shift yet, so this is safe.
Evan Cheng0b1e8712007-09-06 00:14:41 +00001302 unsigned ShAmt = MI->getOperand(2).getImm();
1303 if (ShAmt == 0 || ShAmt >= 4) return 0;
Evan Chenge52c1912008-07-03 09:09:37 +00001304
Evan Chengf031da82009-12-11 06:01:48 +00001305 if (DisableLEA16)
Evan Cheng85979012009-12-12 20:03:14 +00001306 return is64Bit ? convertToThreeAddressWithLEA(MIOpc, MFI, MBBI, LV) : 0;
Evan Chengf031da82009-12-11 06:01:48 +00001307 NewMI = BuildMI(MF, MI->getDebugLoc(), get(X86::LEA16r))
1308 .addReg(Dest, RegState::Define | getDeadRegState(isDead))
1309 .addReg(0).addImm(1 << ShAmt)
1310 .addReg(Src, getKillRegState(isKill))
Chris Lattnereed3f012010-07-08 23:46:44 +00001311 .addImm(0).addReg(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001312 break;
1313 }
Evan Cheng6b96ed32007-10-05 20:34:26 +00001314 default: {
1315 // The following opcodes also sets the condition code register(s). Only
1316 // convert them to equivalent lea if the condition code register def's
1317 // are dead!
1318 if (hasLiveCondCodeDef(MI))
1319 return 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001320
Evan Cheng6b96ed32007-10-05 20:34:26 +00001321 switch (MIOpc) {
1322 default: return 0;
1323 case X86::INC64r:
Dan Gohman69782502009-01-06 23:34:46 +00001324 case X86::INC32r:
1325 case X86::INC64_32r: {
Evan Cheng6b96ed32007-10-05 20:34:26 +00001326 assert(MI->getNumOperands() >= 2 && "Unknown inc instruction!");
Evan Chenga28a9562007-10-09 07:14:53 +00001327 unsigned Opc = MIOpc == X86::INC64r ? X86::LEA64r
1328 : (is64Bit ? X86::LEA64_32r : X86::LEA32r);
Chris Lattnereed3f012010-07-08 23:46:44 +00001329 NewMI = addRegOffset(BuildMI(MF, MI->getDebugLoc(), get(Opc))
Bill Wendling2b739762009-05-13 21:33:08 +00001330 .addReg(Dest, RegState::Define |
1331 getDeadRegState(isDead)),
Rafael Espindolabca99f72009-04-08 21:14:34 +00001332 Src, isKill, 1);
Evan Cheng6b96ed32007-10-05 20:34:26 +00001333 break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001334 }
Evan Cheng6b96ed32007-10-05 20:34:26 +00001335 case X86::INC16r:
1336 case X86::INC64_16r:
Evan Chengf031da82009-12-11 06:01:48 +00001337 if (DisableLEA16)
Evan Cheng85979012009-12-12 20:03:14 +00001338 return is64Bit ? convertToThreeAddressWithLEA(MIOpc, MFI, MBBI, LV) : 0;
Evan Cheng6b96ed32007-10-05 20:34:26 +00001339 assert(MI->getNumOperands() >= 2 && "Unknown inc instruction!");
Bill Wendling13ee2e42009-02-11 21:51:19 +00001340 NewMI = addRegOffset(BuildMI(MF, MI->getDebugLoc(), get(X86::LEA16r))
Bill Wendling2b739762009-05-13 21:33:08 +00001341 .addReg(Dest, RegState::Define |
1342 getDeadRegState(isDead)),
Evan Chenge52c1912008-07-03 09:09:37 +00001343 Src, isKill, 1);
Evan Cheng6b96ed32007-10-05 20:34:26 +00001344 break;
1345 case X86::DEC64r:
Dan Gohman69782502009-01-06 23:34:46 +00001346 case X86::DEC32r:
1347 case X86::DEC64_32r: {
Evan Cheng6b96ed32007-10-05 20:34:26 +00001348 assert(MI->getNumOperands() >= 2 && "Unknown dec instruction!");
Evan Chenga28a9562007-10-09 07:14:53 +00001349 unsigned Opc = MIOpc == X86::DEC64r ? X86::LEA64r
1350 : (is64Bit ? X86::LEA64_32r : X86::LEA32r);
Chris Lattnereed3f012010-07-08 23:46:44 +00001351 NewMI = addRegOffset(BuildMI(MF, MI->getDebugLoc(), get(Opc))
Bill Wendling2b739762009-05-13 21:33:08 +00001352 .addReg(Dest, RegState::Define |
1353 getDeadRegState(isDead)),
Rafael Espindolabca99f72009-04-08 21:14:34 +00001354 Src, isKill, -1);
Evan Cheng6b96ed32007-10-05 20:34:26 +00001355 break;
1356 }
1357 case X86::DEC16r:
1358 case X86::DEC64_16r:
Evan Chengf031da82009-12-11 06:01:48 +00001359 if (DisableLEA16)
Evan Cheng85979012009-12-12 20:03:14 +00001360 return is64Bit ? convertToThreeAddressWithLEA(MIOpc, MFI, MBBI, LV) : 0;
Evan Cheng6b96ed32007-10-05 20:34:26 +00001361 assert(MI->getNumOperands() >= 2 && "Unknown dec instruction!");
Bill Wendling13ee2e42009-02-11 21:51:19 +00001362 NewMI = addRegOffset(BuildMI(MF, MI->getDebugLoc(), get(X86::LEA16r))
Bill Wendling2b739762009-05-13 21:33:08 +00001363 .addReg(Dest, RegState::Define |
1364 getDeadRegState(isDead)),
Evan Chenge52c1912008-07-03 09:09:37 +00001365 Src, isKill, -1);
Evan Cheng6b96ed32007-10-05 20:34:26 +00001366 break;
1367 case X86::ADD64rr:
1368 case X86::ADD32rr: {
1369 assert(MI->getNumOperands() >= 3 && "Unknown add instruction!");
Evan Chenga28a9562007-10-09 07:14:53 +00001370 unsigned Opc = MIOpc == X86::ADD64rr ? X86::LEA64r
1371 : (is64Bit ? X86::LEA64_32r : X86::LEA32r);
Evan Chenge52c1912008-07-03 09:09:37 +00001372 unsigned Src2 = MI->getOperand(2).getReg();
1373 bool isKill2 = MI->getOperand(2).isKill();
Bill Wendling13ee2e42009-02-11 21:51:19 +00001374 NewMI = addRegReg(BuildMI(MF, MI->getDebugLoc(), get(Opc))
Bill Wendling2b739762009-05-13 21:33:08 +00001375 .addReg(Dest, RegState::Define |
1376 getDeadRegState(isDead)),
Evan Chenge52c1912008-07-03 09:09:37 +00001377 Src, isKill, Src2, isKill2);
1378 if (LV && isKill2)
1379 LV->replaceKillInstruction(Src2, MI, NewMI);
Evan Cheng6b96ed32007-10-05 20:34:26 +00001380 break;
1381 }
Evan Chenge52c1912008-07-03 09:09:37 +00001382 case X86::ADD16rr: {
Evan Chengf031da82009-12-11 06:01:48 +00001383 if (DisableLEA16)
Evan Cheng85979012009-12-12 20:03:14 +00001384 return is64Bit ? convertToThreeAddressWithLEA(MIOpc, MFI, MBBI, LV) : 0;
Evan Cheng6b96ed32007-10-05 20:34:26 +00001385 assert(MI->getNumOperands() >= 3 && "Unknown add instruction!");
Evan Chenge52c1912008-07-03 09:09:37 +00001386 unsigned Src2 = MI->getOperand(2).getReg();
1387 bool isKill2 = MI->getOperand(2).isKill();
Bill Wendling13ee2e42009-02-11 21:51:19 +00001388 NewMI = addRegReg(BuildMI(MF, MI->getDebugLoc(), get(X86::LEA16r))
Bill Wendling2b739762009-05-13 21:33:08 +00001389 .addReg(Dest, RegState::Define |
1390 getDeadRegState(isDead)),
Evan Chenge52c1912008-07-03 09:09:37 +00001391 Src, isKill, Src2, isKill2);
1392 if (LV && isKill2)
1393 LV->replaceKillInstruction(Src2, MI, NewMI);
Evan Cheng6b96ed32007-10-05 20:34:26 +00001394 break;
Evan Chenge52c1912008-07-03 09:09:37 +00001395 }
Evan Cheng6b96ed32007-10-05 20:34:26 +00001396 case X86::ADD64ri32:
1397 case X86::ADD64ri8:
1398 assert(MI->getNumOperands() >= 3 && "Unknown add instruction!");
Chris Lattnereed3f012010-07-08 23:46:44 +00001399 NewMI = addRegOffset(BuildMI(MF, MI->getDebugLoc(), get(X86::LEA64r))
Evan Chengf031da82009-12-11 06:01:48 +00001400 .addReg(Dest, RegState::Define |
1401 getDeadRegState(isDead)),
1402 Src, isKill, MI->getOperand(2).getImm());
Evan Cheng6b96ed32007-10-05 20:34:26 +00001403 break;
1404 case X86::ADD32ri:
Evan Chengf031da82009-12-11 06:01:48 +00001405 case X86::ADD32ri8: {
Evan Cheng6b96ed32007-10-05 20:34:26 +00001406 assert(MI->getNumOperands() >= 3 && "Unknown add instruction!");
Evan Chengf031da82009-12-11 06:01:48 +00001407 unsigned Opc = is64Bit ? X86::LEA64_32r : X86::LEA32r;
Chris Lattnereed3f012010-07-08 23:46:44 +00001408 NewMI = addRegOffset(BuildMI(MF, MI->getDebugLoc(), get(Opc))
Evan Chengf031da82009-12-11 06:01:48 +00001409 .addReg(Dest, RegState::Define |
1410 getDeadRegState(isDead)),
Rafael Espindolabca99f72009-04-08 21:14:34 +00001411 Src, isKill, MI->getOperand(2).getImm());
Evan Cheng6b96ed32007-10-05 20:34:26 +00001412 break;
1413 }
Evan Chengf031da82009-12-11 06:01:48 +00001414 case X86::ADD16ri:
1415 case X86::ADD16ri8:
1416 if (DisableLEA16)
Evan Cheng85979012009-12-12 20:03:14 +00001417 return is64Bit ? convertToThreeAddressWithLEA(MIOpc, MFI, MBBI, LV) : 0;
Evan Chengf031da82009-12-11 06:01:48 +00001418 assert(MI->getNumOperands() >= 3 && "Unknown add instruction!");
Chris Lattnereed3f012010-07-08 23:46:44 +00001419 NewMI = addRegOffset(BuildMI(MF, MI->getDebugLoc(), get(X86::LEA16r))
Evan Chengf031da82009-12-11 06:01:48 +00001420 .addReg(Dest, RegState::Define |
1421 getDeadRegState(isDead)),
1422 Src, isKill, MI->getOperand(2).getImm());
1423 break;
Evan Cheng6b96ed32007-10-05 20:34:26 +00001424 }
1425 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001426 }
1427
Evan Chengc3cb24d2008-02-07 08:29:53 +00001428 if (!NewMI) return 0;
1429
Evan Chenge52c1912008-07-03 09:09:37 +00001430 if (LV) { // Update live variables
1431 if (isKill)
1432 LV->replaceKillInstruction(Src, MI, NewMI);
1433 if (isDead)
1434 LV->replaceKillInstruction(Dest, MI, NewMI);
1435 }
1436
Evan Cheng6b96ed32007-10-05 20:34:26 +00001437 MFI->insert(MBBI, NewMI); // Insert the new inst
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001438 return NewMI;
1439}
1440
1441/// commuteInstruction - We have a few instructions that must be hacked on to
1442/// commute them.
1443///
Evan Cheng5de1aaf2008-06-16 07:33:11 +00001444MachineInstr *
1445X86InstrInfo::commuteInstruction(MachineInstr *MI, bool NewMI) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001446 switch (MI->getOpcode()) {
1447 case X86::SHRD16rri8: // A = SHRD16rri8 B, C, I -> A = SHLD16rri8 C, B, (16-I)
1448 case X86::SHLD16rri8: // A = SHLD16rri8 B, C, I -> A = SHRD16rri8 C, B, (16-I)
1449 case X86::SHRD32rri8: // A = SHRD32rri8 B, C, I -> A = SHLD32rri8 C, B, (32-I)
Dan Gohman4d9fc4a2007-09-14 23:17:45 +00001450 case X86::SHLD32rri8: // A = SHLD32rri8 B, C, I -> A = SHRD32rri8 C, B, (32-I)
1451 case X86::SHRD64rri8: // A = SHRD64rri8 B, C, I -> A = SHLD64rri8 C, B, (64-I)
1452 case X86::SHLD64rri8:{// A = SHLD64rri8 B, C, I -> A = SHRD64rri8 C, B, (64-I)
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001453 unsigned Opc;
1454 unsigned Size;
1455 switch (MI->getOpcode()) {
Edwin Törökbd448e32009-07-14 16:55:14 +00001456 default: llvm_unreachable("Unreachable!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001457 case X86::SHRD16rri8: Size = 16; Opc = X86::SHLD16rri8; break;
1458 case X86::SHLD16rri8: Size = 16; Opc = X86::SHRD16rri8; break;
1459 case X86::SHRD32rri8: Size = 32; Opc = X86::SHLD32rri8; break;
1460 case X86::SHLD32rri8: Size = 32; Opc = X86::SHRD32rri8; break;
Dan Gohman4d9fc4a2007-09-14 23:17:45 +00001461 case X86::SHRD64rri8: Size = 64; Opc = X86::SHLD64rri8; break;
1462 case X86::SHLD64rri8: Size = 64; Opc = X86::SHRD64rri8; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001463 }
Chris Lattnera96056a2007-12-30 20:49:49 +00001464 unsigned Amt = MI->getOperand(3).getImm();
Dan Gohman921581d2008-10-17 01:23:35 +00001465 if (NewMI) {
1466 MachineFunction &MF = *MI->getParent()->getParent();
1467 MI = MF.CloneMachineInstr(MI);
1468 NewMI = false;
Evan Chengb554e532008-02-13 02:46:49 +00001469 }
Dan Gohman921581d2008-10-17 01:23:35 +00001470 MI->setDesc(get(Opc));
1471 MI->getOperand(3).setImm(Size-Amt);
1472 return TargetInstrInfoImpl::commuteInstruction(MI, NewMI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001473 }
Evan Cheng926658c2007-10-05 23:13:21 +00001474 case X86::CMOVB16rr:
1475 case X86::CMOVB32rr:
1476 case X86::CMOVB64rr:
1477 case X86::CMOVAE16rr:
1478 case X86::CMOVAE32rr:
1479 case X86::CMOVAE64rr:
1480 case X86::CMOVE16rr:
1481 case X86::CMOVE32rr:
1482 case X86::CMOVE64rr:
1483 case X86::CMOVNE16rr:
1484 case X86::CMOVNE32rr:
1485 case X86::CMOVNE64rr:
1486 case X86::CMOVBE16rr:
1487 case X86::CMOVBE32rr:
1488 case X86::CMOVBE64rr:
1489 case X86::CMOVA16rr:
1490 case X86::CMOVA32rr:
1491 case X86::CMOVA64rr:
1492 case X86::CMOVL16rr:
1493 case X86::CMOVL32rr:
1494 case X86::CMOVL64rr:
1495 case X86::CMOVGE16rr:
1496 case X86::CMOVGE32rr:
1497 case X86::CMOVGE64rr:
1498 case X86::CMOVLE16rr:
1499 case X86::CMOVLE32rr:
1500 case X86::CMOVLE64rr:
1501 case X86::CMOVG16rr:
1502 case X86::CMOVG32rr:
1503 case X86::CMOVG64rr:
1504 case X86::CMOVS16rr:
1505 case X86::CMOVS32rr:
1506 case X86::CMOVS64rr:
1507 case X86::CMOVNS16rr:
1508 case X86::CMOVNS32rr:
1509 case X86::CMOVNS64rr:
1510 case X86::CMOVP16rr:
1511 case X86::CMOVP32rr:
1512 case X86::CMOVP64rr:
1513 case X86::CMOVNP16rr:
1514 case X86::CMOVNP32rr:
Dan Gohman12fd4d72009-01-07 00:35:10 +00001515 case X86::CMOVNP64rr:
1516 case X86::CMOVO16rr:
1517 case X86::CMOVO32rr:
1518 case X86::CMOVO64rr:
1519 case X86::CMOVNO16rr:
1520 case X86::CMOVNO32rr:
1521 case X86::CMOVNO64rr: {
Evan Cheng926658c2007-10-05 23:13:21 +00001522 unsigned Opc = 0;
1523 switch (MI->getOpcode()) {
1524 default: break;
1525 case X86::CMOVB16rr: Opc = X86::CMOVAE16rr; break;
1526 case X86::CMOVB32rr: Opc = X86::CMOVAE32rr; break;
1527 case X86::CMOVB64rr: Opc = X86::CMOVAE64rr; break;
1528 case X86::CMOVAE16rr: Opc = X86::CMOVB16rr; break;
1529 case X86::CMOVAE32rr: Opc = X86::CMOVB32rr; break;
1530 case X86::CMOVAE64rr: Opc = X86::CMOVB64rr; break;
1531 case X86::CMOVE16rr: Opc = X86::CMOVNE16rr; break;
1532 case X86::CMOVE32rr: Opc = X86::CMOVNE32rr; break;
1533 case X86::CMOVE64rr: Opc = X86::CMOVNE64rr; break;
1534 case X86::CMOVNE16rr: Opc = X86::CMOVE16rr; break;
1535 case X86::CMOVNE32rr: Opc = X86::CMOVE32rr; break;
1536 case X86::CMOVNE64rr: Opc = X86::CMOVE64rr; break;
1537 case X86::CMOVBE16rr: Opc = X86::CMOVA16rr; break;
1538 case X86::CMOVBE32rr: Opc = X86::CMOVA32rr; break;
1539 case X86::CMOVBE64rr: Opc = X86::CMOVA64rr; break;
1540 case X86::CMOVA16rr: Opc = X86::CMOVBE16rr; break;
1541 case X86::CMOVA32rr: Opc = X86::CMOVBE32rr; break;
1542 case X86::CMOVA64rr: Opc = X86::CMOVBE64rr; break;
1543 case X86::CMOVL16rr: Opc = X86::CMOVGE16rr; break;
1544 case X86::CMOVL32rr: Opc = X86::CMOVGE32rr; break;
1545 case X86::CMOVL64rr: Opc = X86::CMOVGE64rr; break;
1546 case X86::CMOVGE16rr: Opc = X86::CMOVL16rr; break;
1547 case X86::CMOVGE32rr: Opc = X86::CMOVL32rr; break;
1548 case X86::CMOVGE64rr: Opc = X86::CMOVL64rr; break;
1549 case X86::CMOVLE16rr: Opc = X86::CMOVG16rr; break;
1550 case X86::CMOVLE32rr: Opc = X86::CMOVG32rr; break;
1551 case X86::CMOVLE64rr: Opc = X86::CMOVG64rr; break;
1552 case X86::CMOVG16rr: Opc = X86::CMOVLE16rr; break;
1553 case X86::CMOVG32rr: Opc = X86::CMOVLE32rr; break;
1554 case X86::CMOVG64rr: Opc = X86::CMOVLE64rr; break;
1555 case X86::CMOVS16rr: Opc = X86::CMOVNS16rr; break;
1556 case X86::CMOVS32rr: Opc = X86::CMOVNS32rr; break;
Mon P Wangb866cc82009-04-18 05:16:01 +00001557 case X86::CMOVS64rr: Opc = X86::CMOVNS64rr; break;
Evan Cheng926658c2007-10-05 23:13:21 +00001558 case X86::CMOVNS16rr: Opc = X86::CMOVS16rr; break;
1559 case X86::CMOVNS32rr: Opc = X86::CMOVS32rr; break;
1560 case X86::CMOVNS64rr: Opc = X86::CMOVS64rr; break;
1561 case X86::CMOVP16rr: Opc = X86::CMOVNP16rr; break;
1562 case X86::CMOVP32rr: Opc = X86::CMOVNP32rr; break;
Mon P Wangb866cc82009-04-18 05:16:01 +00001563 case X86::CMOVP64rr: Opc = X86::CMOVNP64rr; break;
Evan Cheng926658c2007-10-05 23:13:21 +00001564 case X86::CMOVNP16rr: Opc = X86::CMOVP16rr; break;
1565 case X86::CMOVNP32rr: Opc = X86::CMOVP32rr; break;
1566 case X86::CMOVNP64rr: Opc = X86::CMOVP64rr; break;
Dan Gohman12fd4d72009-01-07 00:35:10 +00001567 case X86::CMOVO16rr: Opc = X86::CMOVNO16rr; break;
1568 case X86::CMOVO32rr: Opc = X86::CMOVNO32rr; break;
Mon P Wangb866cc82009-04-18 05:16:01 +00001569 case X86::CMOVO64rr: Opc = X86::CMOVNO64rr; break;
Dan Gohman12fd4d72009-01-07 00:35:10 +00001570 case X86::CMOVNO16rr: Opc = X86::CMOVO16rr; break;
1571 case X86::CMOVNO32rr: Opc = X86::CMOVO32rr; break;
1572 case X86::CMOVNO64rr: Opc = X86::CMOVO64rr; break;
Evan Cheng926658c2007-10-05 23:13:21 +00001573 }
Dan Gohman921581d2008-10-17 01:23:35 +00001574 if (NewMI) {
1575 MachineFunction &MF = *MI->getParent()->getParent();
1576 MI = MF.CloneMachineInstr(MI);
1577 NewMI = false;
1578 }
Chris Lattner86bb02f2008-01-11 18:10:50 +00001579 MI->setDesc(get(Opc));
Evan Cheng926658c2007-10-05 23:13:21 +00001580 // Fallthrough intended.
1581 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001582 default:
Evan Cheng5de1aaf2008-06-16 07:33:11 +00001583 return TargetInstrInfoImpl::commuteInstruction(MI, NewMI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001584 }
1585}
1586
1587static X86::CondCode GetCondFromBranchOpc(unsigned BrOpc) {
1588 switch (BrOpc) {
1589 default: return X86::COND_INVALID;
Chris Lattnerb112c022010-02-11 19:25:55 +00001590 case X86::JE_4: return X86::COND_E;
1591 case X86::JNE_4: return X86::COND_NE;
1592 case X86::JL_4: return X86::COND_L;
1593 case X86::JLE_4: return X86::COND_LE;
1594 case X86::JG_4: return X86::COND_G;
1595 case X86::JGE_4: return X86::COND_GE;
1596 case X86::JB_4: return X86::COND_B;
1597 case X86::JBE_4: return X86::COND_BE;
1598 case X86::JA_4: return X86::COND_A;
1599 case X86::JAE_4: return X86::COND_AE;
1600 case X86::JS_4: return X86::COND_S;
1601 case X86::JNS_4: return X86::COND_NS;
1602 case X86::JP_4: return X86::COND_P;
1603 case X86::JNP_4: return X86::COND_NP;
1604 case X86::JO_4: return X86::COND_O;
1605 case X86::JNO_4: return X86::COND_NO;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001606 }
1607}
1608
1609unsigned X86::GetCondBranchFromCond(X86::CondCode CC) {
1610 switch (CC) {
Edwin Törökbd448e32009-07-14 16:55:14 +00001611 default: llvm_unreachable("Illegal condition code!");
Chris Lattnerb112c022010-02-11 19:25:55 +00001612 case X86::COND_E: return X86::JE_4;
1613 case X86::COND_NE: return X86::JNE_4;
1614 case X86::COND_L: return X86::JL_4;
1615 case X86::COND_LE: return X86::JLE_4;
1616 case X86::COND_G: return X86::JG_4;
1617 case X86::COND_GE: return X86::JGE_4;
1618 case X86::COND_B: return X86::JB_4;
1619 case X86::COND_BE: return X86::JBE_4;
1620 case X86::COND_A: return X86::JA_4;
1621 case X86::COND_AE: return X86::JAE_4;
1622 case X86::COND_S: return X86::JS_4;
1623 case X86::COND_NS: return X86::JNS_4;
1624 case X86::COND_P: return X86::JP_4;
1625 case X86::COND_NP: return X86::JNP_4;
1626 case X86::COND_O: return X86::JO_4;
1627 case X86::COND_NO: return X86::JNO_4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001628 }
1629}
1630
1631/// GetOppositeBranchCondition - Return the inverse of the specified condition,
1632/// e.g. turning COND_E to COND_NE.
1633X86::CondCode X86::GetOppositeBranchCondition(X86::CondCode CC) {
1634 switch (CC) {
Edwin Törökbd448e32009-07-14 16:55:14 +00001635 default: llvm_unreachable("Illegal condition code!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001636 case X86::COND_E: return X86::COND_NE;
1637 case X86::COND_NE: return X86::COND_E;
1638 case X86::COND_L: return X86::COND_GE;
1639 case X86::COND_LE: return X86::COND_G;
1640 case X86::COND_G: return X86::COND_LE;
1641 case X86::COND_GE: return X86::COND_L;
1642 case X86::COND_B: return X86::COND_AE;
1643 case X86::COND_BE: return X86::COND_A;
1644 case X86::COND_A: return X86::COND_BE;
1645 case X86::COND_AE: return X86::COND_B;
1646 case X86::COND_S: return X86::COND_NS;
1647 case X86::COND_NS: return X86::COND_S;
1648 case X86::COND_P: return X86::COND_NP;
1649 case X86::COND_NP: return X86::COND_P;
1650 case X86::COND_O: return X86::COND_NO;
1651 case X86::COND_NO: return X86::COND_O;
1652 }
1653}
1654
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001655bool X86InstrInfo::isUnpredicatedTerminator(const MachineInstr *MI) const {
Chris Lattner5b930372008-01-07 07:27:27 +00001656 const TargetInstrDesc &TID = MI->getDesc();
1657 if (!TID.isTerminator()) return false;
Chris Lattner62327602008-01-07 01:56:04 +00001658
1659 // Conditional branch is a special case.
Chris Lattner5b930372008-01-07 07:27:27 +00001660 if (TID.isBranch() && !TID.isBarrier())
Chris Lattner62327602008-01-07 01:56:04 +00001661 return true;
Chris Lattner5b930372008-01-07 07:27:27 +00001662 if (!TID.isPredicable())
Chris Lattner62327602008-01-07 01:56:04 +00001663 return true;
1664 return !isPredicated(MI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001665}
1666
Evan Cheng12515792007-07-26 17:32:14 +00001667// For purposes of branch analysis do not count FP_REG_KILL as a terminator.
1668static bool isBrAnalysisUnpredicatedTerminator(const MachineInstr *MI,
1669 const X86InstrInfo &TII) {
1670 if (MI->getOpcode() == X86::FP_REG_KILL)
1671 return false;
1672 return TII.isUnpredicatedTerminator(MI);
1673}
1674
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001675bool X86InstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,
1676 MachineBasicBlock *&TBB,
1677 MachineBasicBlock *&FBB,
Evan Chengeac31642009-02-09 07:14:22 +00001678 SmallVectorImpl<MachineOperand> &Cond,
1679 bool AllowModify) const {
Dan Gohman6a00fcb2008-10-21 03:29:32 +00001680 // Start from the bottom of the block and work up, examining the
1681 // terminator instructions.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001682 MachineBasicBlock::iterator I = MBB.end();
Evan Cheng78d98ff2010-04-13 18:50:27 +00001683 MachineBasicBlock::iterator UnCondBrIter = MBB.end();
Dan Gohman6a00fcb2008-10-21 03:29:32 +00001684 while (I != MBB.begin()) {
1685 --I;
Dale Johannesen139dcd72010-04-02 01:38:09 +00001686 if (I->isDebugValue())
1687 continue;
Bill Wendling82332402009-12-14 06:51:19 +00001688
1689 // Working from the bottom, when we see a non-terminator instruction, we're
1690 // done.
Dan Gohman6a00fcb2008-10-21 03:29:32 +00001691 if (!isBrAnalysisUnpredicatedTerminator(I, *this))
1692 break;
Bill Wendling82332402009-12-14 06:51:19 +00001693
1694 // A terminator that isn't a branch can't easily be handled by this
1695 // analysis.
Dan Gohman6a00fcb2008-10-21 03:29:32 +00001696 if (!I->getDesc().isBranch())
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001697 return true;
Bill Wendling82332402009-12-14 06:51:19 +00001698
Dan Gohman6a00fcb2008-10-21 03:29:32 +00001699 // Handle unconditional branches.
Chris Lattnerb112c022010-02-11 19:25:55 +00001700 if (I->getOpcode() == X86::JMP_4) {
Evan Cheng78d98ff2010-04-13 18:50:27 +00001701 UnCondBrIter = I;
1702
Evan Chengeac31642009-02-09 07:14:22 +00001703 if (!AllowModify) {
1704 TBB = I->getOperand(0).getMBB();
Evan Cheng67bf8e22009-05-08 06:34:09 +00001705 continue;
Evan Chengeac31642009-02-09 07:14:22 +00001706 }
1707
Dan Gohman6a00fcb2008-10-21 03:29:32 +00001708 // If the block has any instructions after a JMP, delete them.
Chris Lattnerb44b4292009-12-03 00:50:42 +00001709 while (llvm::next(I) != MBB.end())
1710 llvm::next(I)->eraseFromParent();
Bill Wendling82332402009-12-14 06:51:19 +00001711
Dan Gohman6a00fcb2008-10-21 03:29:32 +00001712 Cond.clear();
1713 FBB = 0;
Bill Wendling82332402009-12-14 06:51:19 +00001714
Dan Gohman6a00fcb2008-10-21 03:29:32 +00001715 // Delete the JMP if it's equivalent to a fall-through.
1716 if (MBB.isLayoutSuccessor(I->getOperand(0).getMBB())) {
1717 TBB = 0;
1718 I->eraseFromParent();
1719 I = MBB.end();
Evan Cheng78d98ff2010-04-13 18:50:27 +00001720 UnCondBrIter = MBB.end();
Dan Gohman6a00fcb2008-10-21 03:29:32 +00001721 continue;
1722 }
Bill Wendling82332402009-12-14 06:51:19 +00001723
Evan Cheng78d98ff2010-04-13 18:50:27 +00001724 // TBB is used to indicate the unconditional destination.
Dan Gohman6a00fcb2008-10-21 03:29:32 +00001725 TBB = I->getOperand(0).getMBB();
1726 continue;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001727 }
Bill Wendling82332402009-12-14 06:51:19 +00001728
Dan Gohman6a00fcb2008-10-21 03:29:32 +00001729 // Handle conditional branches.
1730 X86::CondCode BranchCode = GetCondFromBranchOpc(I->getOpcode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001731 if (BranchCode == X86::COND_INVALID)
1732 return true; // Can't handle indirect branch.
Bill Wendling82332402009-12-14 06:51:19 +00001733
Dan Gohman6a00fcb2008-10-21 03:29:32 +00001734 // Working from the bottom, handle the first conditional branch.
1735 if (Cond.empty()) {
Evan Cheng78d98ff2010-04-13 18:50:27 +00001736 MachineBasicBlock *TargetBB = I->getOperand(0).getMBB();
1737 if (AllowModify && UnCondBrIter != MBB.end() &&
1738 MBB.isLayoutSuccessor(TargetBB)) {
1739 // If we can modify the code and it ends in something like:
1740 //
1741 // jCC L1
1742 // jmp L2
1743 // L1:
1744 // ...
1745 // L2:
1746 //
1747 // Then we can change this to:
1748 //
1749 // jnCC L2
1750 // L1:
1751 // ...
1752 // L2:
1753 //
1754 // Which is a bit more efficient.
1755 // We conditionally jump to the fall-through block.
1756 BranchCode = GetOppositeBranchCondition(BranchCode);
1757 unsigned JNCC = GetCondBranchFromCond(BranchCode);
1758 MachineBasicBlock::iterator OldInst = I;
1759
1760 BuildMI(MBB, UnCondBrIter, MBB.findDebugLoc(I), get(JNCC))
1761 .addMBB(UnCondBrIter->getOperand(0).getMBB());
1762 BuildMI(MBB, UnCondBrIter, MBB.findDebugLoc(I), get(X86::JMP_4))
1763 .addMBB(TargetBB);
1764 MBB.addSuccessor(TargetBB);
1765
1766 OldInst->eraseFromParent();
1767 UnCondBrIter->eraseFromParent();
1768
1769 // Restart the analysis.
1770 UnCondBrIter = MBB.end();
1771 I = MBB.end();
1772 continue;
1773 }
1774
Dan Gohman6a00fcb2008-10-21 03:29:32 +00001775 FBB = TBB;
1776 TBB = I->getOperand(0).getMBB();
1777 Cond.push_back(MachineOperand::CreateImm(BranchCode));
1778 continue;
1779 }
Bill Wendling82332402009-12-14 06:51:19 +00001780
1781 // Handle subsequent conditional branches. Only handle the case where all
1782 // conditional branches branch to the same destination and their condition
1783 // opcodes fit one of the special multi-branch idioms.
Dan Gohman6a00fcb2008-10-21 03:29:32 +00001784 assert(Cond.size() == 1);
1785 assert(TBB);
Bill Wendling82332402009-12-14 06:51:19 +00001786
1787 // Only handle the case where all conditional branches branch to the same
1788 // destination.
Dan Gohman6a00fcb2008-10-21 03:29:32 +00001789 if (TBB != I->getOperand(0).getMBB())
1790 return true;
Bill Wendling82332402009-12-14 06:51:19 +00001791
Dan Gohman6a00fcb2008-10-21 03:29:32 +00001792 // If the conditions are the same, we can leave them alone.
Bill Wendling82332402009-12-14 06:51:19 +00001793 X86::CondCode OldBranchCode = (X86::CondCode)Cond[0].getImm();
Dan Gohman6a00fcb2008-10-21 03:29:32 +00001794 if (OldBranchCode == BranchCode)
1795 continue;
Bill Wendling82332402009-12-14 06:51:19 +00001796
1797 // If they differ, see if they fit one of the known patterns. Theoretically,
1798 // we could handle more patterns here, but we shouldn't expect to see them
1799 // if instruction selection has done a reasonable job.
Dan Gohman6a00fcb2008-10-21 03:29:32 +00001800 if ((OldBranchCode == X86::COND_NP &&
1801 BranchCode == X86::COND_E) ||
1802 (OldBranchCode == X86::COND_E &&
1803 BranchCode == X86::COND_NP))
1804 BranchCode = X86::COND_NP_OR_E;
1805 else if ((OldBranchCode == X86::COND_P &&
1806 BranchCode == X86::COND_NE) ||
1807 (OldBranchCode == X86::COND_NE &&
1808 BranchCode == X86::COND_P))
1809 BranchCode = X86::COND_NE_OR_P;
1810 else
1811 return true;
Bill Wendling82332402009-12-14 06:51:19 +00001812
Dan Gohman6a00fcb2008-10-21 03:29:32 +00001813 // Update the MachineOperand.
1814 Cond[0].setImm(BranchCode);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001815 }
1816
Dan Gohman6a00fcb2008-10-21 03:29:32 +00001817 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001818}
1819
1820unsigned X86InstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
1821 MachineBasicBlock::iterator I = MBB.end();
Dan Gohman6a00fcb2008-10-21 03:29:32 +00001822 unsigned Count = 0;
1823
1824 while (I != MBB.begin()) {
1825 --I;
Dale Johannesen139dcd72010-04-02 01:38:09 +00001826 if (I->isDebugValue())
1827 continue;
Chris Lattnerb112c022010-02-11 19:25:55 +00001828 if (I->getOpcode() != X86::JMP_4 &&
Dan Gohman6a00fcb2008-10-21 03:29:32 +00001829 GetCondFromBranchOpc(I->getOpcode()) == X86::COND_INVALID)
1830 break;
1831 // Remove the branch.
1832 I->eraseFromParent();
1833 I = MBB.end();
1834 ++Count;
1835 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001836
Dan Gohman6a00fcb2008-10-21 03:29:32 +00001837 return Count;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001838}
1839
1840unsigned
1841X86InstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
1842 MachineBasicBlock *FBB,
Stuart Hastings9fa5e332010-06-17 22:43:56 +00001843 const SmallVectorImpl<MachineOperand> &Cond,
1844 DebugLoc DL) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001845 // Shouldn't be a fall through.
1846 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
1847 assert((Cond.size() == 1 || Cond.size() == 0) &&
1848 "X86 branch conditions have one component!");
1849
Dan Gohman6a00fcb2008-10-21 03:29:32 +00001850 if (Cond.empty()) {
1851 // Unconditional branch?
1852 assert(!FBB && "Unconditional branch with multiple successors!");
Stuart Hastings9fa5e332010-06-17 22:43:56 +00001853 BuildMI(&MBB, DL, get(X86::JMP_4)).addMBB(TBB);
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001854 return 1;
1855 }
Dan Gohman6a00fcb2008-10-21 03:29:32 +00001856
1857 // Conditional branch.
1858 unsigned Count = 0;
1859 X86::CondCode CC = (X86::CondCode)Cond[0].getImm();
1860 switch (CC) {
1861 case X86::COND_NP_OR_E:
1862 // Synthesize NP_OR_E with two branches.
Stuart Hastings9fa5e332010-06-17 22:43:56 +00001863 BuildMI(&MBB, DL, get(X86::JNP_4)).addMBB(TBB);
Bill Wendlingd8abe8a2010-03-05 00:33:59 +00001864 ++Count;
Stuart Hastings9fa5e332010-06-17 22:43:56 +00001865 BuildMI(&MBB, DL, get(X86::JE_4)).addMBB(TBB);
Bill Wendlingd8abe8a2010-03-05 00:33:59 +00001866 ++Count;
Dan Gohman6a00fcb2008-10-21 03:29:32 +00001867 break;
1868 case X86::COND_NE_OR_P:
1869 // Synthesize NE_OR_P with two branches.
Stuart Hastings9fa5e332010-06-17 22:43:56 +00001870 BuildMI(&MBB, DL, get(X86::JNE_4)).addMBB(TBB);
Bill Wendlingd8abe8a2010-03-05 00:33:59 +00001871 ++Count;
Stuart Hastings9fa5e332010-06-17 22:43:56 +00001872 BuildMI(&MBB, DL, get(X86::JP_4)).addMBB(TBB);
Bill Wendlingd8abe8a2010-03-05 00:33:59 +00001873 ++Count;
Dan Gohman6a00fcb2008-10-21 03:29:32 +00001874 break;
Bill Wendlingd8abe8a2010-03-05 00:33:59 +00001875 default: {
1876 unsigned Opc = GetCondBranchFromCond(CC);
Stuart Hastings9fa5e332010-06-17 22:43:56 +00001877 BuildMI(&MBB, DL, get(Opc)).addMBB(TBB);
Bill Wendlingd8abe8a2010-03-05 00:33:59 +00001878 ++Count;
Dan Gohman6a00fcb2008-10-21 03:29:32 +00001879 }
Bill Wendlingd8abe8a2010-03-05 00:33:59 +00001880 }
Dan Gohman6a00fcb2008-10-21 03:29:32 +00001881 if (FBB) {
1882 // Two-way Conditional branch. Insert the second branch.
Stuart Hastings9fa5e332010-06-17 22:43:56 +00001883 BuildMI(&MBB, DL, get(X86::JMP_4)).addMBB(FBB);
Dan Gohman6a00fcb2008-10-21 03:29:32 +00001884 ++Count;
1885 }
1886 return Count;
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001887}
1888
Dan Gohman2da0db32009-04-15 00:04:23 +00001889/// isHReg - Test if the given register is a physical h register.
1890static bool isHReg(unsigned Reg) {
Dan Gohman1d8ce9c2009-04-27 16:41:36 +00001891 return X86::GR8_ABCD_HRegClass.contains(Reg);
Dan Gohman2da0db32009-04-15 00:04:23 +00001892}
1893
Jakob Stoklund Olesen24a2d0c2010-07-08 19:46:25 +00001894void X86InstrInfo::copyPhysReg(MachineBasicBlock &MBB,
1895 MachineBasicBlock::iterator MI, DebugLoc DL,
1896 unsigned DestReg, unsigned SrcReg,
1897 bool KillSrc) const {
1898 // First deal with the normal symmetric copies.
1899 unsigned Opc = 0;
1900 if (X86::GR64RegClass.contains(DestReg, SrcReg))
1901 Opc = X86::MOV64rr;
1902 else if (X86::GR32RegClass.contains(DestReg, SrcReg))
1903 Opc = X86::MOV32rr;
1904 else if (X86::GR16RegClass.contains(DestReg, SrcReg))
1905 Opc = X86::MOV16rr;
1906 else if (X86::GR8RegClass.contains(DestReg, SrcReg)) {
1907 // Copying to or from a physical H register on x86-64 requires a NOREX
1908 // move. Otherwise use a normal move.
1909 if ((isHReg(DestReg) || isHReg(SrcReg)) &&
1910 TM.getSubtarget<X86Subtarget>().is64Bit())
1911 Opc = X86::MOV8rr_NOREX;
1912 else
1913 Opc = X86::MOV8rr;
1914 } else if (X86::VR128RegClass.contains(DestReg, SrcReg))
1915 Opc = X86::MOVAPSrr;
Jakob Stoklund Olesen22fe8102010-07-08 22:30:35 +00001916 else if (X86::VR64RegClass.contains(DestReg, SrcReg))
1917 Opc = X86::MMX_MOVQ64rr;
Jakob Stoklund Olesen24a2d0c2010-07-08 19:46:25 +00001918
1919 if (Opc) {
1920 BuildMI(MBB, MI, DL, get(Opc), DestReg)
1921 .addReg(SrcReg, getKillRegState(KillSrc));
1922 return;
1923 }
1924
1925 // Moving EFLAGS to / from another register requires a push and a pop.
1926 if (SrcReg == X86::EFLAGS) {
1927 if (X86::GR64RegClass.contains(DestReg)) {
1928 BuildMI(MBB, MI, DL, get(X86::PUSHF64));
1929 BuildMI(MBB, MI, DL, get(X86::POP64r), DestReg);
1930 return;
1931 } else if (X86::GR32RegClass.contains(DestReg)) {
1932 BuildMI(MBB, MI, DL, get(X86::PUSHF32));
1933 BuildMI(MBB, MI, DL, get(X86::POP32r), DestReg);
1934 return;
1935 }
1936 }
1937 if (DestReg == X86::EFLAGS) {
1938 if (X86::GR64RegClass.contains(SrcReg)) {
1939 BuildMI(MBB, MI, DL, get(X86::PUSH64r))
1940 .addReg(SrcReg, getKillRegState(KillSrc));
1941 BuildMI(MBB, MI, DL, get(X86::POPF64));
1942 return;
1943 } else if (X86::GR32RegClass.contains(SrcReg)) {
1944 BuildMI(MBB, MI, DL, get(X86::PUSH32r))
1945 .addReg(SrcReg, getKillRegState(KillSrc));
1946 BuildMI(MBB, MI, DL, get(X86::POPF32));
1947 return;
1948 }
1949 }
1950
1951 DEBUG(dbgs() << "Cannot copy " << RI.getName(SrcReg)
1952 << " to " << RI.getName(DestReg) << '\n');
1953 llvm_unreachable("Cannot emit physreg copy instruction");
1954}
1955
Rafael Espindolaa704f942010-06-12 20:13:29 +00001956static unsigned getLoadStoreRegOpcode(unsigned Reg,
1957 const TargetRegisterClass *RC,
1958 bool isStackAligned,
1959 const TargetMachine &TM,
1960 bool load) {
1961 if (RC == &X86::GR64RegClass || RC == &X86::GR64_NOSPRegClass) {
1962 return load ? X86::MOV64rm : X86::MOV64mr;
1963 } else if (RC == &X86::GR32RegClass || RC == &X86::GR32_NOSPRegClass) {
1964 return load ? X86::MOV32rm : X86::MOV32mr;
1965 } else if (RC == &X86::GR16RegClass) {
1966 return load ? X86::MOV16rm : X86::MOV16mr;
1967 } else if (RC == &X86::GR8RegClass) {
1968 // Copying to or from a physical H register on x86-64 requires a NOREX
1969 // move. Otherwise use a normal move.
1970 if (isHReg(Reg) &&
1971 TM.getSubtarget<X86Subtarget>().is64Bit())
1972 return load ? X86::MOV8rm_NOREX : X86::MOV8mr_NOREX;
1973 else
1974 return load ? X86::MOV8rm : X86::MOV8mr;
1975 } else if (RC == &X86::GR64_ABCDRegClass) {
1976 return load ? X86::MOV64rm : X86::MOV64mr;
1977 } else if (RC == &X86::GR32_ABCDRegClass) {
1978 return load ? X86::MOV32rm : X86::MOV32mr;
1979 } else if (RC == &X86::GR16_ABCDRegClass) {
1980 return load ? X86::MOV16rm : X86::MOV16mr;
1981 } else if (RC == &X86::GR8_ABCD_LRegClass) {
1982 return load ? X86::MOV8rm :X86::MOV8mr;
1983 } else if (RC == &X86::GR8_ABCD_HRegClass) {
1984 if (TM.getSubtarget<X86Subtarget>().is64Bit())
1985 return load ? X86::MOV8rm_NOREX : X86::MOV8mr_NOREX;
1986 else
1987 return load ? X86::MOV8rm : X86::MOV8mr;
1988 } else if (RC == &X86::GR64_NOREXRegClass ||
1989 RC == &X86::GR64_NOREX_NOSPRegClass) {
1990 return load ? X86::MOV64rm : X86::MOV64mr;
1991 } else if (RC == &X86::GR32_NOREXRegClass) {
1992 return load ? X86::MOV32rm : X86::MOV32mr;
1993 } else if (RC == &X86::GR16_NOREXRegClass) {
1994 return load ? X86::MOV16rm : X86::MOV16mr;
1995 } else if (RC == &X86::GR8_NOREXRegClass) {
1996 return load ? X86::MOV8rm : X86::MOV8mr;
1997 } else if (RC == &X86::GR64_TCRegClass) {
1998 return load ? X86::MOV64rm_TC : X86::MOV64mr_TC;
1999 } else if (RC == &X86::GR32_TCRegClass) {
2000 return load ? X86::MOV32rm_TC : X86::MOV32mr_TC;
2001 } else if (RC == &X86::RFP80RegClass) {
2002 return load ? X86::LD_Fp80m : X86::ST_FpP80m;
2003 } else if (RC == &X86::RFP64RegClass) {
2004 return load ? X86::LD_Fp64m : X86::ST_Fp64m;
2005 } else if (RC == &X86::RFP32RegClass) {
2006 return load ? X86::LD_Fp32m : X86::ST_Fp32m;
2007 } else if (RC == &X86::FR32RegClass) {
2008 return load ? X86::MOVSSrm : X86::MOVSSmr;
2009 } else if (RC == &X86::FR64RegClass) {
2010 return load ? X86::MOVSDrm : X86::MOVSDmr;
2011 } else if (RC == &X86::VR128RegClass) {
2012 // If stack is realigned we can use aligned stores.
2013 if (isStackAligned)
2014 return load ? X86::MOVAPSrm : X86::MOVAPSmr;
2015 else
2016 return load ? X86::MOVUPSrm : X86::MOVUPSmr;
2017 } else if (RC == &X86::VR64RegClass) {
2018 return load ? X86::MMX_MOVQ64rm : X86::MMX_MOVQ64mr;
2019 } else {
2020 llvm_unreachable("Unknown regclass");
2021 }
2022}
2023
Dan Gohman1d8ce9c2009-04-27 16:41:36 +00002024static unsigned getStoreRegOpcode(unsigned SrcReg,
2025 const TargetRegisterClass *RC,
2026 bool isStackAligned,
2027 TargetMachine &TM) {
Rafael Espindolaa704f942010-06-12 20:13:29 +00002028 return getLoadStoreRegOpcode(SrcReg, RC, isStackAligned, TM, false);
2029}
Owen Anderson81875432008-01-01 21:11:32 +00002030
Rafael Espindolaa704f942010-06-12 20:13:29 +00002031
2032static unsigned getLoadRegOpcode(unsigned DestReg,
2033 const TargetRegisterClass *RC,
2034 bool isStackAligned,
2035 const TargetMachine &TM) {
2036 return getLoadStoreRegOpcode(DestReg, RC, isStackAligned, TM, true);
Owen Anderson81875432008-01-01 21:11:32 +00002037}
2038
2039void X86InstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
2040 MachineBasicBlock::iterator MI,
2041 unsigned SrcReg, bool isKill, int FrameIdx,
Evan Cheng1f8534d2010-05-06 19:06:44 +00002042 const TargetRegisterClass *RC,
2043 const TargetRegisterInfo *TRI) const {
Anton Korobeynikov44cf57f2008-07-19 06:30:51 +00002044 const MachineFunction &MF = *MBB.getParent();
Jim Grosbache9b2eed2010-01-19 18:31:11 +00002045 bool isAligned = (RI.getStackAlignment() >= 16) || RI.canRealignStack(MF);
Dan Gohman1d8ce9c2009-04-27 16:41:36 +00002046 unsigned Opc = getStoreRegOpcode(SrcReg, RC, isAligned, TM);
Dale Johannesenb5ca78e2010-01-26 00:03:12 +00002047 DebugLoc DL = MBB.findDebugLoc(MI);
Bill Wendling13ee2e42009-02-11 21:51:19 +00002048 addFrameReference(BuildMI(MBB, MI, DL, get(Opc)), FrameIdx)
Bill Wendling2b739762009-05-13 21:33:08 +00002049 .addReg(SrcReg, getKillRegState(isKill));
Owen Anderson81875432008-01-01 21:11:32 +00002050}
2051
2052void X86InstrInfo::storeRegToAddr(MachineFunction &MF, unsigned SrcReg,
2053 bool isKill,
2054 SmallVectorImpl<MachineOperand> &Addr,
2055 const TargetRegisterClass *RC,
Dan Gohmanc7973eb2009-10-09 18:10:05 +00002056 MachineInstr::mmo_iterator MMOBegin,
2057 MachineInstr::mmo_iterator MMOEnd,
Owen Anderson81875432008-01-01 21:11:32 +00002058 SmallVectorImpl<MachineInstr*> &NewMIs) const {
Evan Cheng4cb1de52010-07-02 20:36:18 +00002059 bool isAligned = *MMOBegin && (*MMOBegin)->getAlignment() >= 16;
Dan Gohman1d8ce9c2009-04-27 16:41:36 +00002060 unsigned Opc = getStoreRegOpcode(SrcReg, RC, isAligned, TM);
Chris Lattnerd2c680b2010-04-02 20:16:16 +00002061 DebugLoc DL;
Dale Johannesen77cce4d2009-02-12 23:08:38 +00002062 MachineInstrBuilder MIB = BuildMI(MF, DL, get(Opc));
Owen Anderson81875432008-01-01 21:11:32 +00002063 for (unsigned i = 0, e = Addr.size(); i != e; ++i)
Dan Gohmanc909bbb2009-02-18 05:45:50 +00002064 MIB.addOperand(Addr[i]);
Bill Wendling2b739762009-05-13 21:33:08 +00002065 MIB.addReg(SrcReg, getKillRegState(isKill));
Dan Gohmanc7973eb2009-10-09 18:10:05 +00002066 (*MIB).setMemRefs(MMOBegin, MMOEnd);
Owen Anderson81875432008-01-01 21:11:32 +00002067 NewMIs.push_back(MIB);
2068}
2069
Owen Anderson81875432008-01-01 21:11:32 +00002070
2071void X86InstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
Anton Korobeynikov44cf57f2008-07-19 06:30:51 +00002072 MachineBasicBlock::iterator MI,
2073 unsigned DestReg, int FrameIdx,
Evan Cheng1f8534d2010-05-06 19:06:44 +00002074 const TargetRegisterClass *RC,
2075 const TargetRegisterInfo *TRI) const {
Anton Korobeynikov44cf57f2008-07-19 06:30:51 +00002076 const MachineFunction &MF = *MBB.getParent();
Jim Grosbache9b2eed2010-01-19 18:31:11 +00002077 bool isAligned = (RI.getStackAlignment() >= 16) || RI.canRealignStack(MF);
Dan Gohman1d8ce9c2009-04-27 16:41:36 +00002078 unsigned Opc = getLoadRegOpcode(DestReg, RC, isAligned, TM);
Dale Johannesenb5ca78e2010-01-26 00:03:12 +00002079 DebugLoc DL = MBB.findDebugLoc(MI);
Bill Wendling13ee2e42009-02-11 21:51:19 +00002080 addFrameReference(BuildMI(MBB, MI, DL, get(Opc), DestReg), FrameIdx);
Owen Anderson81875432008-01-01 21:11:32 +00002081}
2082
2083void X86InstrInfo::loadRegFromAddr(MachineFunction &MF, unsigned DestReg,
Evan Chenge52c1912008-07-03 09:09:37 +00002084 SmallVectorImpl<MachineOperand> &Addr,
2085 const TargetRegisterClass *RC,
Dan Gohmanc7973eb2009-10-09 18:10:05 +00002086 MachineInstr::mmo_iterator MMOBegin,
2087 MachineInstr::mmo_iterator MMOEnd,
Owen Anderson81875432008-01-01 21:11:32 +00002088 SmallVectorImpl<MachineInstr*> &NewMIs) const {
Evan Cheng4cb1de52010-07-02 20:36:18 +00002089 bool isAligned = *MMOBegin && (*MMOBegin)->getAlignment() >= 16;
Dan Gohman1d8ce9c2009-04-27 16:41:36 +00002090 unsigned Opc = getLoadRegOpcode(DestReg, RC, isAligned, TM);
Chris Lattnerd2c680b2010-04-02 20:16:16 +00002091 DebugLoc DL;
Dale Johannesen77cce4d2009-02-12 23:08:38 +00002092 MachineInstrBuilder MIB = BuildMI(MF, DL, get(Opc), DestReg);
Owen Anderson81875432008-01-01 21:11:32 +00002093 for (unsigned i = 0, e = Addr.size(); i != e; ++i)
Dan Gohmanc909bbb2009-02-18 05:45:50 +00002094 MIB.addOperand(Addr[i]);
Dan Gohmanc7973eb2009-10-09 18:10:05 +00002095 (*MIB).setMemRefs(MMOBegin, MMOEnd);
Owen Anderson81875432008-01-01 21:11:32 +00002096 NewMIs.push_back(MIB);
2097}
2098
Owen Anderson6690c7f2008-01-04 23:57:37 +00002099bool X86InstrInfo::spillCalleeSavedRegisters(MachineBasicBlock &MBB,
Bill Wendling13ee2e42009-02-11 21:51:19 +00002100 MachineBasicBlock::iterator MI,
Evan Cheng32d1bb92010-05-22 01:47:14 +00002101 const std::vector<CalleeSavedInfo> &CSI,
2102 const TargetRegisterInfo *TRI) const {
Owen Anderson6690c7f2008-01-04 23:57:37 +00002103 if (CSI.empty())
2104 return false;
2105
Dale Johannesene0dcaa82010-01-20 21:36:02 +00002106 DebugLoc DL = MBB.findDebugLoc(MI);
Bill Wendling13ee2e42009-02-11 21:51:19 +00002107
Evan Chengc275cf62008-09-26 19:14:21 +00002108 bool is64Bit = TM.getSubtarget<X86Subtarget>().is64Bit();
Anton Korobeynikovb5cc6d82009-08-28 16:06:41 +00002109 bool isWin64 = TM.getSubtarget<X86Subtarget>().isTargetWin64();
Anton Korobeynikov1deb2dd2008-10-04 11:09:36 +00002110 unsigned SlotSize = is64Bit ? 8 : 4;
2111
2112 MachineFunction &MF = *MBB.getParent();
Evan Cheng10b8d222009-07-09 06:53:48 +00002113 unsigned FPReg = RI.getFrameRegister(MF);
Anton Korobeynikov1deb2dd2008-10-04 11:09:36 +00002114 X86MachineFunctionInfo *X86FI = MF.getInfo<X86MachineFunctionInfo>();
Eli Friedman65b88222009-06-04 02:32:04 +00002115 unsigned CalleeFrameSize = 0;
Anton Korobeynikov1deb2dd2008-10-04 11:09:36 +00002116
Owen Anderson6690c7f2008-01-04 23:57:37 +00002117 unsigned Opc = is64Bit ? X86::PUSH64r : X86::PUSH32r;
2118 for (unsigned i = CSI.size(); i != 0; --i) {
2119 unsigned Reg = CSI[i-1].getReg();
2120 // Add the callee-saved register as live-in. It's killed at the spill.
2121 MBB.addLiveIn(Reg);
Evan Cheng10b8d222009-07-09 06:53:48 +00002122 if (Reg == FPReg)
2123 // X86RegisterInfo::emitPrologue will handle spilling of frame register.
2124 continue;
Rafael Espindola9699a4f2010-06-02 20:02:30 +00002125 if (!X86::VR128RegClass.contains(Reg) && !isWin64) {
Eli Friedman65b88222009-06-04 02:32:04 +00002126 CalleeFrameSize += SlotSize;
Evan Cheng10b8d222009-07-09 06:53:48 +00002127 BuildMI(MBB, MI, DL, get(Opc)).addReg(Reg, RegState::Kill);
Eli Friedman65b88222009-06-04 02:32:04 +00002128 } else {
Rafael Espindola9699a4f2010-06-02 20:02:30 +00002129 storeRegToStackSlot(MBB, MI, Reg, true, CSI[i-1].getFrameIdx(),
2130 &X86::VR128RegClass, &RI);
Eli Friedman65b88222009-06-04 02:32:04 +00002131 }
Owen Anderson6690c7f2008-01-04 23:57:37 +00002132 }
Eli Friedman65b88222009-06-04 02:32:04 +00002133
2134 X86FI->setCalleeSavedFrameSize(CalleeFrameSize);
Owen Anderson6690c7f2008-01-04 23:57:37 +00002135 return true;
2136}
2137
2138bool X86InstrInfo::restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
Bill Wendling13ee2e42009-02-11 21:51:19 +00002139 MachineBasicBlock::iterator MI,
Evan Cheng32d1bb92010-05-22 01:47:14 +00002140 const std::vector<CalleeSavedInfo> &CSI,
2141 const TargetRegisterInfo *TRI) const {
Owen Anderson6690c7f2008-01-04 23:57:37 +00002142 if (CSI.empty())
2143 return false;
Bill Wendling13ee2e42009-02-11 21:51:19 +00002144
Dale Johannesene0dcaa82010-01-20 21:36:02 +00002145 DebugLoc DL = MBB.findDebugLoc(MI);
Bill Wendling13ee2e42009-02-11 21:51:19 +00002146
Evan Cheng10b8d222009-07-09 06:53:48 +00002147 MachineFunction &MF = *MBB.getParent();
2148 unsigned FPReg = RI.getFrameRegister(MF);
Owen Anderson6690c7f2008-01-04 23:57:37 +00002149 bool is64Bit = TM.getSubtarget<X86Subtarget>().is64Bit();
Anton Korobeynikovb5cc6d82009-08-28 16:06:41 +00002150 bool isWin64 = TM.getSubtarget<X86Subtarget>().isTargetWin64();
Owen Anderson6690c7f2008-01-04 23:57:37 +00002151 unsigned Opc = is64Bit ? X86::POP64r : X86::POP32r;
2152 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
2153 unsigned Reg = CSI[i].getReg();
Evan Cheng10b8d222009-07-09 06:53:48 +00002154 if (Reg == FPReg)
2155 // X86RegisterInfo::emitEpilogue will handle restoring of frame register.
2156 continue;
Rafael Espindola9699a4f2010-06-02 20:02:30 +00002157 if (!X86::VR128RegClass.contains(Reg) && !isWin64) {
Eli Friedman65b88222009-06-04 02:32:04 +00002158 BuildMI(MBB, MI, DL, get(Opc), Reg);
2159 } else {
Rafael Espindola9699a4f2010-06-02 20:02:30 +00002160 loadRegFromStackSlot(MBB, MI, Reg, CSI[i].getFrameIdx(),
2161 &X86::VR128RegClass, &RI);
Eli Friedman65b88222009-06-04 02:32:04 +00002162 }
Owen Anderson6690c7f2008-01-04 23:57:37 +00002163 }
2164 return true;
2165}
2166
Evan Cheng17b0c672010-04-26 07:38:55 +00002167MachineInstr*
2168X86InstrInfo::emitFrameIndexDebugValue(MachineFunction &MF,
Evan Chengf9c420a2010-04-29 01:13:30 +00002169 int FrameIx, uint64_t Offset,
Evan Cheng17b0c672010-04-26 07:38:55 +00002170 const MDNode *MDPtr,
2171 DebugLoc DL) const {
Evan Cheng17b0c672010-04-26 07:38:55 +00002172 X86AddressMode AM;
2173 AM.BaseType = X86AddressMode::FrameIndexBase;
2174 AM.Base.FrameIndex = FrameIx;
2175 MachineInstrBuilder MIB = BuildMI(MF, DL, get(X86::DBG_VALUE));
2176 addFullAddress(MIB, AM).addImm(Offset).addMetadata(MDPtr);
2177 return &*MIB;
2178}
2179
Dan Gohman221a4372008-07-07 23:14:23 +00002180static MachineInstr *FuseTwoAddrInst(MachineFunction &MF, unsigned Opcode,
Dan Gohmanc24a3f82009-01-05 17:59:02 +00002181 const SmallVectorImpl<MachineOperand> &MOs,
Bill Wendling5aa0ddb2009-02-03 00:55:04 +00002182 MachineInstr *MI,
2183 const TargetInstrInfo &TII) {
Owen Anderson9a184ef2008-01-07 01:35:02 +00002184 // Create the base instruction with the memory operand as the first part.
Bill Wendling5aa0ddb2009-02-03 00:55:04 +00002185 MachineInstr *NewMI = MF.CreateMachineInstr(TII.get(Opcode),
2186 MI->getDebugLoc(), true);
Owen Anderson9a184ef2008-01-07 01:35:02 +00002187 MachineInstrBuilder MIB(NewMI);
2188 unsigned NumAddrOps = MOs.size();
2189 for (unsigned i = 0; i != NumAddrOps; ++i)
Dan Gohmanc909bbb2009-02-18 05:45:50 +00002190 MIB.addOperand(MOs[i]);
Owen Anderson9a184ef2008-01-07 01:35:02 +00002191 if (NumAddrOps < 4) // FrameIndex only
Rafael Espindolabca99f72009-04-08 21:14:34 +00002192 addOffset(MIB, 0);
Owen Anderson9a184ef2008-01-07 01:35:02 +00002193
2194 // Loop over the rest of the ri operands, converting them over.
Chris Lattner5b930372008-01-07 07:27:27 +00002195 unsigned NumOps = MI->getDesc().getNumOperands()-2;
Owen Anderson9a184ef2008-01-07 01:35:02 +00002196 for (unsigned i = 0; i != NumOps; ++i) {
2197 MachineOperand &MO = MI->getOperand(i+2);
Dan Gohmanc909bbb2009-02-18 05:45:50 +00002198 MIB.addOperand(MO);
Owen Anderson9a184ef2008-01-07 01:35:02 +00002199 }
2200 for (unsigned i = NumOps+2, e = MI->getNumOperands(); i != e; ++i) {
2201 MachineOperand &MO = MI->getOperand(i);
Dan Gohmanc909bbb2009-02-18 05:45:50 +00002202 MIB.addOperand(MO);
Owen Anderson9a184ef2008-01-07 01:35:02 +00002203 }
2204 return MIB;
2205}
2206
Dan Gohman221a4372008-07-07 23:14:23 +00002207static MachineInstr *FuseInst(MachineFunction &MF,
2208 unsigned Opcode, unsigned OpNo,
Dan Gohmanc24a3f82009-01-05 17:59:02 +00002209 const SmallVectorImpl<MachineOperand> &MOs,
Owen Anderson9a184ef2008-01-07 01:35:02 +00002210 MachineInstr *MI, const TargetInstrInfo &TII) {
Bill Wendling5aa0ddb2009-02-03 00:55:04 +00002211 MachineInstr *NewMI = MF.CreateMachineInstr(TII.get(Opcode),
2212 MI->getDebugLoc(), true);
Owen Anderson9a184ef2008-01-07 01:35:02 +00002213 MachineInstrBuilder MIB(NewMI);
2214
2215 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
2216 MachineOperand &MO = MI->getOperand(i);
2217 if (i == OpNo) {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00002218 assert(MO.isReg() && "Expected to fold into reg operand!");
Owen Anderson9a184ef2008-01-07 01:35:02 +00002219 unsigned NumAddrOps = MOs.size();
2220 for (unsigned i = 0; i != NumAddrOps; ++i)
Dan Gohmanc909bbb2009-02-18 05:45:50 +00002221 MIB.addOperand(MOs[i]);
Owen Anderson9a184ef2008-01-07 01:35:02 +00002222 if (NumAddrOps < 4) // FrameIndex only
Rafael Espindolabca99f72009-04-08 21:14:34 +00002223 addOffset(MIB, 0);
Owen Anderson9a184ef2008-01-07 01:35:02 +00002224 } else {
Dan Gohmanc909bbb2009-02-18 05:45:50 +00002225 MIB.addOperand(MO);
Owen Anderson9a184ef2008-01-07 01:35:02 +00002226 }
2227 }
2228 return MIB;
2229}
2230
2231static MachineInstr *MakeM0Inst(const TargetInstrInfo &TII, unsigned Opcode,
Dan Gohmanc24a3f82009-01-05 17:59:02 +00002232 const SmallVectorImpl<MachineOperand> &MOs,
Owen Anderson9a184ef2008-01-07 01:35:02 +00002233 MachineInstr *MI) {
Dan Gohman221a4372008-07-07 23:14:23 +00002234 MachineFunction &MF = *MI->getParent()->getParent();
Bill Wendling13ee2e42009-02-11 21:51:19 +00002235 MachineInstrBuilder MIB = BuildMI(MF, MI->getDebugLoc(), TII.get(Opcode));
Owen Anderson9a184ef2008-01-07 01:35:02 +00002236
2237 unsigned NumAddrOps = MOs.size();
2238 for (unsigned i = 0; i != NumAddrOps; ++i)
Dan Gohmanc909bbb2009-02-18 05:45:50 +00002239 MIB.addOperand(MOs[i]);
Owen Anderson9a184ef2008-01-07 01:35:02 +00002240 if (NumAddrOps < 4) // FrameIndex only
Rafael Espindolabca99f72009-04-08 21:14:34 +00002241 addOffset(MIB, 0);
Owen Anderson9a184ef2008-01-07 01:35:02 +00002242 return MIB.addImm(0);
2243}
2244
2245MachineInstr*
Dan Gohmanedc83d62008-12-03 18:43:12 +00002246X86InstrInfo::foldMemoryOperandImpl(MachineFunction &MF,
2247 MachineInstr *MI, unsigned i,
Evan Chenga5853792009-07-15 06:10:07 +00002248 const SmallVectorImpl<MachineOperand> &MOs,
Evan Cheng8f0797f2009-09-11 00:39:26 +00002249 unsigned Size, unsigned Align) const {
Evan Chenga5853792009-07-15 06:10:07 +00002250 const DenseMap<unsigned*, std::pair<unsigned,unsigned> > *OpcodeTablePtr=NULL;
Owen Anderson9a184ef2008-01-07 01:35:02 +00002251 bool isTwoAddrFold = false;
Chris Lattner5b930372008-01-07 07:27:27 +00002252 unsigned NumOps = MI->getDesc().getNumOperands();
Owen Anderson9a184ef2008-01-07 01:35:02 +00002253 bool isTwoAddr = NumOps > 1 &&
Chris Lattner5b930372008-01-07 07:27:27 +00002254 MI->getDesc().getOperandConstraint(1, TOI::TIED_TO) != -1;
Owen Anderson9a184ef2008-01-07 01:35:02 +00002255
2256 MachineInstr *NewMI = NULL;
2257 // Folding a memory location into the two-address part of a two-address
2258 // instruction is different than folding it other places. It requires
2259 // replacing the *two* registers with the memory location.
2260 if (isTwoAddr && NumOps >= 2 && i < 2 &&
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00002261 MI->getOperand(0).isReg() &&
2262 MI->getOperand(1).isReg() &&
Owen Anderson9a184ef2008-01-07 01:35:02 +00002263 MI->getOperand(0).getReg() == MI->getOperand(1).getReg()) {
2264 OpcodeTablePtr = &RegOp2MemOpTable2Addr;
2265 isTwoAddrFold = true;
2266 } else if (i == 0) { // If operand 0
Dan Gohmanb9e1c8d2010-01-12 04:42:54 +00002267 if (MI->getOpcode() == X86::MOV64r0)
2268 NewMI = MakeM0Inst(*this, X86::MOV64mi32, MOs, MI);
2269 else if (MI->getOpcode() == X86::MOV32r0)
Owen Anderson9a184ef2008-01-07 01:35:02 +00002270 NewMI = MakeM0Inst(*this, X86::MOV32mi, MOs, MI);
Dan Gohmanb9e1c8d2010-01-12 04:42:54 +00002271 else if (MI->getOpcode() == X86::MOV16r0)
2272 NewMI = MakeM0Inst(*this, X86::MOV16mi, MOs, MI);
Owen Anderson9a184ef2008-01-07 01:35:02 +00002273 else if (MI->getOpcode() == X86::MOV8r0)
2274 NewMI = MakeM0Inst(*this, X86::MOV8mi, MOs, MI);
Evan Chenge52c1912008-07-03 09:09:37 +00002275 if (NewMI)
Owen Anderson9a184ef2008-01-07 01:35:02 +00002276 return NewMI;
Owen Anderson9a184ef2008-01-07 01:35:02 +00002277
2278 OpcodeTablePtr = &RegOp2MemOpTable0;
2279 } else if (i == 1) {
2280 OpcodeTablePtr = &RegOp2MemOpTable1;
2281 } else if (i == 2) {
2282 OpcodeTablePtr = &RegOp2MemOpTable2;
2283 }
2284
2285 // If table selected...
2286 if (OpcodeTablePtr) {
2287 // Find the Opcode to fuse
Jeffrey Yasskin8154d2e2009-11-10 01:02:17 +00002288 DenseMap<unsigned*, std::pair<unsigned,unsigned> >::const_iterator I =
Owen Anderson9a184ef2008-01-07 01:35:02 +00002289 OpcodeTablePtr->find((unsigned*)MI->getOpcode());
2290 if (I != OpcodeTablePtr->end()) {
Evan Cheng8f0797f2009-09-11 00:39:26 +00002291 unsigned Opcode = I->second.first;
Evan Chenga5853792009-07-15 06:10:07 +00002292 unsigned MinAlign = I->second.second;
2293 if (Align < MinAlign)
2294 return NULL;
Evan Cheng5b7dbbf2009-09-11 01:01:31 +00002295 bool NarrowToMOV32rm = false;
Evan Cheng8f0797f2009-09-11 00:39:26 +00002296 if (Size) {
2297 unsigned RCSize = MI->getDesc().OpInfo[i].getRegClass(&RI)->getSize();
2298 if (Size < RCSize) {
2299 // Check if it's safe to fold the load. If the size of the object is
2300 // narrower than the load width, then it's not.
2301 if (Opcode != X86::MOV64rm || RCSize != 8 || Size != 4)
2302 return NULL;
2303 // If this is a 64-bit load, but the spill slot is 32, then we can do
2304 // a 32-bit load which is implicitly zero-extended. This likely is due
2305 // to liveintervalanalysis remat'ing a load from stack slot.
Evan Cheng5b7dbbf2009-09-11 01:01:31 +00002306 if (MI->getOperand(0).getSubReg() || MI->getOperand(1).getSubReg())
2307 return NULL;
Evan Cheng8f0797f2009-09-11 00:39:26 +00002308 Opcode = X86::MOV32rm;
Evan Cheng5b7dbbf2009-09-11 01:01:31 +00002309 NarrowToMOV32rm = true;
Evan Cheng8f0797f2009-09-11 00:39:26 +00002310 }
2311 }
2312
Owen Anderson9a184ef2008-01-07 01:35:02 +00002313 if (isTwoAddrFold)
Evan Cheng8f0797f2009-09-11 00:39:26 +00002314 NewMI = FuseTwoAddrInst(MF, Opcode, MOs, MI, *this);
Owen Anderson9a184ef2008-01-07 01:35:02 +00002315 else
Evan Cheng8f0797f2009-09-11 00:39:26 +00002316 NewMI = FuseInst(MF, Opcode, i, MOs, MI, *this);
Evan Cheng5b7dbbf2009-09-11 01:01:31 +00002317
2318 if (NarrowToMOV32rm) {
2319 // If this is the special case where we use a MOV32rm to load a 32-bit
2320 // value and zero-extend the top bits. Change the destination register
2321 // to a 32-bit one.
2322 unsigned DstReg = NewMI->getOperand(0).getReg();
2323 if (TargetRegisterInfo::isPhysicalRegister(DstReg))
2324 NewMI->getOperand(0).setReg(RI.getSubReg(DstReg,
Jakob Stoklund Olesen834b7392010-05-24 14:48:17 +00002325 X86::sub_32bit));
Evan Cheng5b7dbbf2009-09-11 01:01:31 +00002326 else
Jakob Stoklund Olesen834b7392010-05-24 14:48:17 +00002327 NewMI->getOperand(0).setSubReg(X86::sub_32bit);
Evan Cheng5b7dbbf2009-09-11 01:01:31 +00002328 }
Owen Anderson9a184ef2008-01-07 01:35:02 +00002329 return NewMI;
2330 }
2331 }
2332
2333 // No fusion
Jakob Stoklund Olesen71fd14b2010-07-09 20:43:09 +00002334 if (PrintFailedFusing && !MI->isCopy())
David Greene5fd1b6e2010-01-05 01:29:29 +00002335 dbgs() << "We failed to fuse operand " << i << " in " << *MI;
Owen Anderson9a184ef2008-01-07 01:35:02 +00002336 return NULL;
2337}
2338
2339
Dan Gohmanedc83d62008-12-03 18:43:12 +00002340MachineInstr* X86InstrInfo::foldMemoryOperandImpl(MachineFunction &MF,
2341 MachineInstr *MI,
Evan Chenga5853792009-07-15 06:10:07 +00002342 const SmallVectorImpl<unsigned> &Ops,
Dan Gohmanedc83d62008-12-03 18:43:12 +00002343 int FrameIndex) const {
Owen Anderson9a184ef2008-01-07 01:35:02 +00002344 // Check switch flag
2345 if (NoFusing) return NULL;
2346
Evan Chengd53fca12009-12-22 17:47:23 +00002347 if (!MF.getFunction()->hasFnAttr(Attribute::OptimizeForSize))
Evan Chengd3f27fb2009-12-18 07:40:29 +00002348 switch (MI->getOpcode()) {
2349 case X86::CVTSD2SSrr:
2350 case X86::Int_CVTSD2SSrr:
2351 case X86::CVTSS2SDrr:
2352 case X86::Int_CVTSS2SDrr:
2353 case X86::RCPSSr:
2354 case X86::RCPSSr_Int:
2355 case X86::ROUNDSDr_Int:
2356 case X86::ROUNDSSr_Int:
2357 case X86::RSQRTSSr:
2358 case X86::RSQRTSSr_Int:
2359 case X86::SQRTSSr:
2360 case X86::SQRTSSr_Int:
2361 return 0;
2362 }
2363
Evan Cheng4f2f3f62008-02-08 21:20:40 +00002364 const MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng8f0797f2009-09-11 00:39:26 +00002365 unsigned Size = MFI->getObjectSize(FrameIndex);
Evan Cheng4f2f3f62008-02-08 21:20:40 +00002366 unsigned Alignment = MFI->getObjectAlignment(FrameIndex);
Owen Anderson9a184ef2008-01-07 01:35:02 +00002367 if (Ops.size() == 2 && Ops[0] == 0 && Ops[1] == 1) {
2368 unsigned NewOpc = 0;
Evan Cheng8f0797f2009-09-11 00:39:26 +00002369 unsigned RCSize = 0;
Owen Anderson9a184ef2008-01-07 01:35:02 +00002370 switch (MI->getOpcode()) {
2371 default: return NULL;
Evan Cheng8f0797f2009-09-11 00:39:26 +00002372 case X86::TEST8rr: NewOpc = X86::CMP8ri; RCSize = 1; break;
Dan Gohman06de7ef2010-05-18 21:42:03 +00002373 case X86::TEST16rr: NewOpc = X86::CMP16ri8; RCSize = 2; break;
2374 case X86::TEST32rr: NewOpc = X86::CMP32ri8; RCSize = 4; break;
2375 case X86::TEST64rr: NewOpc = X86::CMP64ri8; RCSize = 8; break;
Owen Anderson9a184ef2008-01-07 01:35:02 +00002376 }
Evan Cheng8f0797f2009-09-11 00:39:26 +00002377 // Check if it's safe to fold the load. If the size of the object is
2378 // narrower than the load width, then it's not.
2379 if (Size < RCSize)
2380 return NULL;
Owen Anderson9a184ef2008-01-07 01:35:02 +00002381 // Change to CMPXXri r, 0 first.
Chris Lattner86bb02f2008-01-11 18:10:50 +00002382 MI->setDesc(get(NewOpc));
Owen Anderson9a184ef2008-01-07 01:35:02 +00002383 MI->getOperand(1).ChangeToImmediate(0);
2384 } else if (Ops.size() != 1)
2385 return NULL;
2386
2387 SmallVector<MachineOperand,4> MOs;
2388 MOs.push_back(MachineOperand::CreateFI(FrameIndex));
Evan Cheng8f0797f2009-09-11 00:39:26 +00002389 return foldMemoryOperandImpl(MF, MI, Ops[0], MOs, Size, Alignment);
Owen Anderson9a184ef2008-01-07 01:35:02 +00002390}
2391
Dan Gohmanedc83d62008-12-03 18:43:12 +00002392MachineInstr* X86InstrInfo::foldMemoryOperandImpl(MachineFunction &MF,
2393 MachineInstr *MI,
Evan Chenga5853792009-07-15 06:10:07 +00002394 const SmallVectorImpl<unsigned> &Ops,
Dan Gohmanedc83d62008-12-03 18:43:12 +00002395 MachineInstr *LoadMI) const {
Owen Anderson9a184ef2008-01-07 01:35:02 +00002396 // Check switch flag
2397 if (NoFusing) return NULL;
2398
Evan Chengd53fca12009-12-22 17:47:23 +00002399 if (!MF.getFunction()->hasFnAttr(Attribute::OptimizeForSize))
Evan Chengd3f27fb2009-12-18 07:40:29 +00002400 switch (MI->getOpcode()) {
2401 case X86::CVTSD2SSrr:
2402 case X86::Int_CVTSD2SSrr:
2403 case X86::CVTSS2SDrr:
2404 case X86::Int_CVTSS2SDrr:
2405 case X86::RCPSSr:
2406 case X86::RCPSSr_Int:
2407 case X86::ROUNDSDr_Int:
2408 case X86::ROUNDSSr_Int:
2409 case X86::RSQRTSSr:
2410 case X86::RSQRTSSr_Int:
2411 case X86::SQRTSSr:
2412 case X86::SQRTSSr_Int:
2413 return 0;
2414 }
2415
Dan Gohmand0e8c752008-07-12 00:10:52 +00002416 // Determine the alignment of the load.
Evan Cheng4f2f3f62008-02-08 21:20:40 +00002417 unsigned Alignment = 0;
Dan Gohmand0e8c752008-07-12 00:10:52 +00002418 if (LoadMI->hasOneMemOperand())
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00002419 Alignment = (*LoadMI->memoperands_begin())->getAlignment();
Dan Gohman51dbce62009-09-21 18:30:38 +00002420 else
2421 switch (LoadMI->getOpcode()) {
Jakob Stoklund Olesen8eb9c312010-03-31 00:40:13 +00002422 case X86::V_SET0PS:
2423 case X86::V_SET0PD:
2424 case X86::V_SET0PI:
Dan Gohman51dbce62009-09-21 18:30:38 +00002425 case X86::V_SETALLONES:
2426 Alignment = 16;
2427 break;
2428 case X86::FsFLD0SD:
2429 Alignment = 8;
2430 break;
2431 case X86::FsFLD0SS:
2432 Alignment = 4;
2433 break;
2434 default:
2435 llvm_unreachable("Don't know how to fold this instruction!");
2436 }
Owen Anderson9a184ef2008-01-07 01:35:02 +00002437 if (Ops.size() == 2 && Ops[0] == 0 && Ops[1] == 1) {
2438 unsigned NewOpc = 0;
2439 switch (MI->getOpcode()) {
2440 default: return NULL;
2441 case X86::TEST8rr: NewOpc = X86::CMP8ri; break;
Dan Gohman4b828512010-05-18 21:54:15 +00002442 case X86::TEST16rr: NewOpc = X86::CMP16ri8; break;
2443 case X86::TEST32rr: NewOpc = X86::CMP32ri8; break;
2444 case X86::TEST64rr: NewOpc = X86::CMP64ri8; break;
Owen Anderson9a184ef2008-01-07 01:35:02 +00002445 }
2446 // Change to CMPXXri r, 0 first.
Chris Lattner86bb02f2008-01-11 18:10:50 +00002447 MI->setDesc(get(NewOpc));
Owen Anderson9a184ef2008-01-07 01:35:02 +00002448 MI->getOperand(1).ChangeToImmediate(0);
2449 } else if (Ops.size() != 1)
2450 return NULL;
2451
Chris Lattner65d58572010-07-08 22:41:28 +00002452 SmallVector<MachineOperand,X86::AddrNumOperands> MOs;
Dan Gohman51dbce62009-09-21 18:30:38 +00002453 switch (LoadMI->getOpcode()) {
Jakob Stoklund Olesen8eb9c312010-03-31 00:40:13 +00002454 case X86::V_SET0PS:
2455 case X86::V_SET0PD:
2456 case X86::V_SET0PI:
Dan Gohman51dbce62009-09-21 18:30:38 +00002457 case X86::V_SETALLONES:
2458 case X86::FsFLD0SD:
2459 case X86::FsFLD0SS: {
Jakob Stoklund Olesen8eb9c312010-03-31 00:40:13 +00002460 // Folding a V_SET0P? or V_SETALLONES as a load, to ease register pressure.
Dan Gohman37eb6c82008-12-03 05:21:24 +00002461 // Create a constant-pool entry and operands to load from it.
2462
Dan Gohmana351bd52010-03-09 03:01:40 +00002463 // Medium and large mode can't fold loads this way.
2464 if (TM.getCodeModel() != CodeModel::Small &&
2465 TM.getCodeModel() != CodeModel::Kernel)
2466 return NULL;
2467
Dan Gohman37eb6c82008-12-03 05:21:24 +00002468 // x86-32 PIC requires a PIC base register for constant pools.
2469 unsigned PICBase = 0;
Jakob Stoklund Olesen29867f12009-07-16 21:24:13 +00002470 if (TM.getRelocationModel() == Reloc::PIC_) {
Evan Cheng3b570332009-07-16 18:44:05 +00002471 if (TM.getSubtarget<X86Subtarget>().is64Bit())
2472 PICBase = X86::RIP;
Jakob Stoklund Olesen29867f12009-07-16 21:24:13 +00002473 else
Dan Gohmanb4482cf2010-07-10 09:00:22 +00002474 // FIXME: PICBase = getGlobalBaseReg(&MF);
Evan Cheng3b570332009-07-16 18:44:05 +00002475 // This doesn't work for several reasons.
2476 // 1. GlobalBaseReg may have been spilled.
2477 // 2. It may not be live at MI.
Dan Gohman51dbce62009-09-21 18:30:38 +00002478 return NULL;
Jakob Stoklund Olesen29867f12009-07-16 21:24:13 +00002479 }
Dan Gohman37eb6c82008-12-03 05:21:24 +00002480
Dan Gohman51dbce62009-09-21 18:30:38 +00002481 // Create a constant-pool entry.
Dan Gohman37eb6c82008-12-03 05:21:24 +00002482 MachineConstantPool &MCP = *MF.getConstantPool();
Dan Gohman51dbce62009-09-21 18:30:38 +00002483 const Type *Ty;
2484 if (LoadMI->getOpcode() == X86::FsFLD0SS)
2485 Ty = Type::getFloatTy(MF.getFunction()->getContext());
2486 else if (LoadMI->getOpcode() == X86::FsFLD0SD)
2487 Ty = Type::getDoubleTy(MF.getFunction()->getContext());
2488 else
2489 Ty = VectorType::get(Type::getInt32Ty(MF.getFunction()->getContext()), 4);
Dan Gohman36c56d02010-04-15 01:51:59 +00002490 const Constant *C = LoadMI->getOpcode() == X86::V_SETALLONES ?
Dan Gohman51dbce62009-09-21 18:30:38 +00002491 Constant::getAllOnesValue(Ty) :
2492 Constant::getNullValue(Ty);
2493 unsigned CPI = MCP.getConstantPoolIndex(C, Alignment);
Dan Gohman37eb6c82008-12-03 05:21:24 +00002494
2495 // Create operands to load from the constant pool entry.
2496 MOs.push_back(MachineOperand::CreateReg(PICBase, false));
2497 MOs.push_back(MachineOperand::CreateImm(1));
2498 MOs.push_back(MachineOperand::CreateReg(0, false));
2499 MOs.push_back(MachineOperand::CreateCPI(CPI, 0));
Rafael Espindolabca99f72009-04-08 21:14:34 +00002500 MOs.push_back(MachineOperand::CreateReg(0, false));
Dan Gohman51dbce62009-09-21 18:30:38 +00002501 break;
2502 }
2503 default: {
Dan Gohman37eb6c82008-12-03 05:21:24 +00002504 // Folding a normal load. Just copy the load's address operands.
2505 unsigned NumOps = LoadMI->getDesc().getNumOperands();
Chris Lattner65d58572010-07-08 22:41:28 +00002506 for (unsigned i = NumOps - X86::AddrNumOperands; i != NumOps; ++i)
Dan Gohman37eb6c82008-12-03 05:21:24 +00002507 MOs.push_back(LoadMI->getOperand(i));
Dan Gohman51dbce62009-09-21 18:30:38 +00002508 break;
2509 }
Dan Gohman37eb6c82008-12-03 05:21:24 +00002510 }
Evan Cheng8f0797f2009-09-11 00:39:26 +00002511 return foldMemoryOperandImpl(MF, MI, Ops[0], MOs, 0, Alignment);
Owen Anderson9a184ef2008-01-07 01:35:02 +00002512}
2513
2514
Dan Gohman46b948e2008-10-16 01:49:15 +00002515bool X86InstrInfo::canFoldMemoryOperand(const MachineInstr *MI,
2516 const SmallVectorImpl<unsigned> &Ops) const {
Owen Anderson9a184ef2008-01-07 01:35:02 +00002517 // Check switch flag
2518 if (NoFusing) return 0;
2519
2520 if (Ops.size() == 2 && Ops[0] == 0 && Ops[1] == 1) {
2521 switch (MI->getOpcode()) {
2522 default: return false;
2523 case X86::TEST8rr:
2524 case X86::TEST16rr:
2525 case X86::TEST32rr:
2526 case X86::TEST64rr:
2527 return true;
2528 }
2529 }
2530
2531 if (Ops.size() != 1)
2532 return false;
2533
2534 unsigned OpNum = Ops[0];
2535 unsigned Opc = MI->getOpcode();
Chris Lattner5b930372008-01-07 07:27:27 +00002536 unsigned NumOps = MI->getDesc().getNumOperands();
Owen Anderson9a184ef2008-01-07 01:35:02 +00002537 bool isTwoAddr = NumOps > 1 &&
Chris Lattner5b930372008-01-07 07:27:27 +00002538 MI->getDesc().getOperandConstraint(1, TOI::TIED_TO) != -1;
Owen Anderson9a184ef2008-01-07 01:35:02 +00002539
2540 // Folding a memory location into the two-address part of a two-address
2541 // instruction is different than folding it other places. It requires
2542 // replacing the *two* registers with the memory location.
Evan Chenga5853792009-07-15 06:10:07 +00002543 const DenseMap<unsigned*, std::pair<unsigned,unsigned> > *OpcodeTablePtr=NULL;
Owen Anderson9a184ef2008-01-07 01:35:02 +00002544 if (isTwoAddr && NumOps >= 2 && OpNum < 2) {
2545 OpcodeTablePtr = &RegOp2MemOpTable2Addr;
2546 } else if (OpNum == 0) { // If operand 0
2547 switch (Opc) {
Chris Lattner17f62252009-07-14 20:19:57 +00002548 case X86::MOV8r0:
Dan Gohmanb9e1c8d2010-01-12 04:42:54 +00002549 case X86::MOV16r0:
Owen Anderson9a184ef2008-01-07 01:35:02 +00002550 case X86::MOV32r0:
Dan Gohmanb9e1c8d2010-01-12 04:42:54 +00002551 case X86::MOV64r0:
Owen Anderson9a184ef2008-01-07 01:35:02 +00002552 return true;
2553 default: break;
2554 }
2555 OpcodeTablePtr = &RegOp2MemOpTable0;
2556 } else if (OpNum == 1) {
2557 OpcodeTablePtr = &RegOp2MemOpTable1;
2558 } else if (OpNum == 2) {
2559 OpcodeTablePtr = &RegOp2MemOpTable2;
2560 }
2561
2562 if (OpcodeTablePtr) {
2563 // Find the Opcode to fuse
Jeffrey Yasskin8154d2e2009-11-10 01:02:17 +00002564 DenseMap<unsigned*, std::pair<unsigned,unsigned> >::const_iterator I =
Owen Anderson9a184ef2008-01-07 01:35:02 +00002565 OpcodeTablePtr->find((unsigned*)Opc);
2566 if (I != OpcodeTablePtr->end())
2567 return true;
2568 }
Jakob Stoklund Olesenb4737982010-07-09 20:43:13 +00002569 return TargetInstrInfoImpl::canFoldMemoryOperand(MI, Ops);
Owen Anderson9a184ef2008-01-07 01:35:02 +00002570}
2571
2572bool X86InstrInfo::unfoldMemoryOperand(MachineFunction &MF, MachineInstr *MI,
2573 unsigned Reg, bool UnfoldLoad, bool UnfoldStore,
Bill Wendling13ee2e42009-02-11 21:51:19 +00002574 SmallVectorImpl<MachineInstr*> &NewMIs) const {
Jeffrey Yasskin8154d2e2009-11-10 01:02:17 +00002575 DenseMap<unsigned*, std::pair<unsigned,unsigned> >::const_iterator I =
Owen Anderson9a184ef2008-01-07 01:35:02 +00002576 MemOp2RegOpTable.find((unsigned*)MI->getOpcode());
2577 if (I == MemOp2RegOpTable.end())
2578 return false;
2579 unsigned Opc = I->second.first;
2580 unsigned Index = I->second.second & 0xf;
2581 bool FoldedLoad = I->second.second & (1 << 4);
2582 bool FoldedStore = I->second.second & (1 << 5);
2583 if (UnfoldLoad && !FoldedLoad)
2584 return false;
2585 UnfoldLoad &= FoldedLoad;
2586 if (UnfoldStore && !FoldedStore)
2587 return false;
2588 UnfoldStore &= FoldedStore;
2589
Chris Lattner5b930372008-01-07 07:27:27 +00002590 const TargetInstrDesc &TID = get(Opc);
Owen Anderson9a184ef2008-01-07 01:35:02 +00002591 const TargetOperandInfo &TOI = TID.OpInfo[Index];
Chris Lattner6a66b292009-07-29 21:10:12 +00002592 const TargetRegisterClass *RC = TOI.getRegClass(&RI);
Evan Cheng4cb1de52010-07-02 20:36:18 +00002593 if (!MI->hasOneMemOperand() &&
2594 RC == &X86::VR128RegClass &&
2595 !TM.getSubtarget<X86Subtarget>().isUnalignedMemAccessFast())
2596 // Without memoperands, loadRegFromAddr and storeRegToStackSlot will
2597 // conservatively assume the address is unaligned. That's bad for
2598 // performance.
2599 return false;
Chris Lattner65d58572010-07-08 22:41:28 +00002600 SmallVector<MachineOperand, X86::AddrNumOperands> AddrOps;
Owen Anderson9a184ef2008-01-07 01:35:02 +00002601 SmallVector<MachineOperand,2> BeforeOps;
2602 SmallVector<MachineOperand,2> AfterOps;
2603 SmallVector<MachineOperand,4> ImpOps;
2604 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
2605 MachineOperand &Op = MI->getOperand(i);
Chris Lattner65d58572010-07-08 22:41:28 +00002606 if (i >= Index && i < Index + X86::AddrNumOperands)
Owen Anderson9a184ef2008-01-07 01:35:02 +00002607 AddrOps.push_back(Op);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00002608 else if (Op.isReg() && Op.isImplicit())
Owen Anderson9a184ef2008-01-07 01:35:02 +00002609 ImpOps.push_back(Op);
2610 else if (i < Index)
2611 BeforeOps.push_back(Op);
2612 else if (i > Index)
2613 AfterOps.push_back(Op);
2614 }
2615
2616 // Emit the load instruction.
2617 if (UnfoldLoad) {
Dan Gohmanc7973eb2009-10-09 18:10:05 +00002618 std::pair<MachineInstr::mmo_iterator,
2619 MachineInstr::mmo_iterator> MMOs =
2620 MF.extractLoadMemRefs(MI->memoperands_begin(),
2621 MI->memoperands_end());
2622 loadRegFromAddr(MF, Reg, AddrOps, RC, MMOs.first, MMOs.second, NewMIs);
Owen Anderson9a184ef2008-01-07 01:35:02 +00002623 if (UnfoldStore) {
2624 // Address operands cannot be marked isKill.
Chris Lattner65d58572010-07-08 22:41:28 +00002625 for (unsigned i = 1; i != 1 + X86::AddrNumOperands; ++i) {
Owen Anderson9a184ef2008-01-07 01:35:02 +00002626 MachineOperand &MO = NewMIs[0]->getOperand(i);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00002627 if (MO.isReg())
Owen Anderson9a184ef2008-01-07 01:35:02 +00002628 MO.setIsKill(false);
2629 }
2630 }
2631 }
2632
2633 // Emit the data processing instruction.
Bill Wendling5aa0ddb2009-02-03 00:55:04 +00002634 MachineInstr *DataMI = MF.CreateMachineInstr(TID, MI->getDebugLoc(), true);
Owen Anderson9a184ef2008-01-07 01:35:02 +00002635 MachineInstrBuilder MIB(DataMI);
2636
2637 if (FoldedStore)
Bill Wendling2b739762009-05-13 21:33:08 +00002638 MIB.addReg(Reg, RegState::Define);
Owen Anderson9a184ef2008-01-07 01:35:02 +00002639 for (unsigned i = 0, e = BeforeOps.size(); i != e; ++i)
Dan Gohmanc909bbb2009-02-18 05:45:50 +00002640 MIB.addOperand(BeforeOps[i]);
Owen Anderson9a184ef2008-01-07 01:35:02 +00002641 if (FoldedLoad)
2642 MIB.addReg(Reg);
2643 for (unsigned i = 0, e = AfterOps.size(); i != e; ++i)
Dan Gohmanc909bbb2009-02-18 05:45:50 +00002644 MIB.addOperand(AfterOps[i]);
Owen Anderson9a184ef2008-01-07 01:35:02 +00002645 for (unsigned i = 0, e = ImpOps.size(); i != e; ++i) {
2646 MachineOperand &MO = ImpOps[i];
Bill Wendling2b739762009-05-13 21:33:08 +00002647 MIB.addReg(MO.getReg(),
2648 getDefRegState(MO.isDef()) |
2649 RegState::Implicit |
2650 getKillRegState(MO.isKill()) |
Evan Cheng9c73db12009-06-30 08:49:04 +00002651 getDeadRegState(MO.isDead()) |
2652 getUndefRegState(MO.isUndef()));
Owen Anderson9a184ef2008-01-07 01:35:02 +00002653 }
2654 // Change CMP32ri r, 0 back to TEST32rr r, r, etc.
2655 unsigned NewOpc = 0;
2656 switch (DataMI->getOpcode()) {
2657 default: break;
2658 case X86::CMP64ri32:
Dan Gohman4b828512010-05-18 21:54:15 +00002659 case X86::CMP64ri8:
Owen Anderson9a184ef2008-01-07 01:35:02 +00002660 case X86::CMP32ri:
Dan Gohman4b828512010-05-18 21:54:15 +00002661 case X86::CMP32ri8:
Owen Anderson9a184ef2008-01-07 01:35:02 +00002662 case X86::CMP16ri:
Dan Gohman4b828512010-05-18 21:54:15 +00002663 case X86::CMP16ri8:
Owen Anderson9a184ef2008-01-07 01:35:02 +00002664 case X86::CMP8ri: {
2665 MachineOperand &MO0 = DataMI->getOperand(0);
2666 MachineOperand &MO1 = DataMI->getOperand(1);
2667 if (MO1.getImm() == 0) {
2668 switch (DataMI->getOpcode()) {
2669 default: break;
Dan Gohman4b828512010-05-18 21:54:15 +00002670 case X86::CMP64ri8:
Owen Anderson9a184ef2008-01-07 01:35:02 +00002671 case X86::CMP64ri32: NewOpc = X86::TEST64rr; break;
Dan Gohman4b828512010-05-18 21:54:15 +00002672 case X86::CMP32ri8:
Owen Anderson9a184ef2008-01-07 01:35:02 +00002673 case X86::CMP32ri: NewOpc = X86::TEST32rr; break;
Dan Gohman4b828512010-05-18 21:54:15 +00002674 case X86::CMP16ri8:
Owen Anderson9a184ef2008-01-07 01:35:02 +00002675 case X86::CMP16ri: NewOpc = X86::TEST16rr; break;
2676 case X86::CMP8ri: NewOpc = X86::TEST8rr; break;
2677 }
Chris Lattner86bb02f2008-01-11 18:10:50 +00002678 DataMI->setDesc(get(NewOpc));
Owen Anderson9a184ef2008-01-07 01:35:02 +00002679 MO1.ChangeToRegister(MO0.getReg(), false);
2680 }
2681 }
2682 }
2683 NewMIs.push_back(DataMI);
2684
2685 // Emit the store instruction.
2686 if (UnfoldStore) {
Chris Lattner6a66b292009-07-29 21:10:12 +00002687 const TargetRegisterClass *DstRC = TID.OpInfo[0].getRegClass(&RI);
Dan Gohmanc7973eb2009-10-09 18:10:05 +00002688 std::pair<MachineInstr::mmo_iterator,
2689 MachineInstr::mmo_iterator> MMOs =
2690 MF.extractStoreMemRefs(MI->memoperands_begin(),
2691 MI->memoperands_end());
2692 storeRegToAddr(MF, Reg, true, AddrOps, DstRC, MMOs.first, MMOs.second, NewMIs);
Owen Anderson9a184ef2008-01-07 01:35:02 +00002693 }
2694
2695 return true;
2696}
2697
2698bool
2699X86InstrInfo::unfoldMemoryOperand(SelectionDAG &DAG, SDNode *N,
Bill Wendling13ee2e42009-02-11 21:51:19 +00002700 SmallVectorImpl<SDNode*> &NewNodes) const {
Dan Gohmanbd68c792008-07-17 19:10:17 +00002701 if (!N->isMachineOpcode())
Owen Anderson9a184ef2008-01-07 01:35:02 +00002702 return false;
2703
Jeffrey Yasskin8154d2e2009-11-10 01:02:17 +00002704 DenseMap<unsigned*, std::pair<unsigned,unsigned> >::const_iterator I =
Dan Gohmanbd68c792008-07-17 19:10:17 +00002705 MemOp2RegOpTable.find((unsigned*)N->getMachineOpcode());
Owen Anderson9a184ef2008-01-07 01:35:02 +00002706 if (I == MemOp2RegOpTable.end())
2707 return false;
2708 unsigned Opc = I->second.first;
2709 unsigned Index = I->second.second & 0xf;
2710 bool FoldedLoad = I->second.second & (1 << 4);
2711 bool FoldedStore = I->second.second & (1 << 5);
Chris Lattner5b930372008-01-07 07:27:27 +00002712 const TargetInstrDesc &TID = get(Opc);
Chris Lattner6a66b292009-07-29 21:10:12 +00002713 const TargetRegisterClass *RC = TID.OpInfo[Index].getRegClass(&RI);
Dan Gohman31b70a62009-03-04 19:23:38 +00002714 unsigned NumDefs = TID.NumDefs;
Dan Gohman8181bd12008-07-27 21:46:04 +00002715 std::vector<SDValue> AddrOps;
2716 std::vector<SDValue> BeforeOps;
2717 std::vector<SDValue> AfterOps;
Dale Johannesen913ba762009-02-06 01:31:28 +00002718 DebugLoc dl = N->getDebugLoc();
Owen Anderson9a184ef2008-01-07 01:35:02 +00002719 unsigned NumOps = N->getNumOperands();
Dan Gohman4e3bb1b2009-09-25 20:36:54 +00002720 for (unsigned i = 0; i != NumOps-1; ++i) {
Dan Gohman8181bd12008-07-27 21:46:04 +00002721 SDValue Op = N->getOperand(i);
Chris Lattner65d58572010-07-08 22:41:28 +00002722 if (i >= Index-NumDefs && i < Index-NumDefs + X86::AddrNumOperands)
Owen Anderson9a184ef2008-01-07 01:35:02 +00002723 AddrOps.push_back(Op);
Dan Gohman31b70a62009-03-04 19:23:38 +00002724 else if (i < Index-NumDefs)
Owen Anderson9a184ef2008-01-07 01:35:02 +00002725 BeforeOps.push_back(Op);
Dan Gohman31b70a62009-03-04 19:23:38 +00002726 else if (i > Index-NumDefs)
Owen Anderson9a184ef2008-01-07 01:35:02 +00002727 AfterOps.push_back(Op);
2728 }
Dan Gohman8181bd12008-07-27 21:46:04 +00002729 SDValue Chain = N->getOperand(NumOps-1);
Owen Anderson9a184ef2008-01-07 01:35:02 +00002730 AddrOps.push_back(Chain);
2731
2732 // Emit the load instruction.
2733 SDNode *Load = 0;
Dan Gohmanc7973eb2009-10-09 18:10:05 +00002734 MachineFunction &MF = DAG.getMachineFunction();
Owen Anderson9a184ef2008-01-07 01:35:02 +00002735 if (FoldedLoad) {
Owen Andersonac9de032009-08-10 22:56:29 +00002736 EVT VT = *RC->vt_begin();
Evan Cheng9d7cd4e2009-11-16 21:56:03 +00002737 std::pair<MachineInstr::mmo_iterator,
2738 MachineInstr::mmo_iterator> MMOs =
2739 MF.extractLoadMemRefs(cast<MachineSDNode>(N)->memoperands_begin(),
2740 cast<MachineSDNode>(N)->memoperands_end());
Evan Cheng4cb1de52010-07-02 20:36:18 +00002741 if (!(*MMOs.first) &&
2742 RC == &X86::VR128RegClass &&
2743 !TM.getSubtarget<X86Subtarget>().isUnalignedMemAccessFast())
2744 // Do not introduce a slow unaligned load.
2745 return false;
2746 bool isAligned = (*MMOs.first) && (*MMOs.first)->getAlignment() >= 16;
Dan Gohman61fda0d2009-09-25 18:54:59 +00002747 Load = DAG.getMachineNode(getLoadRegOpcode(0, RC, isAligned, TM), dl,
2748 VT, MVT::Other, &AddrOps[0], AddrOps.size());
Owen Anderson9a184ef2008-01-07 01:35:02 +00002749 NewNodes.push_back(Load);
Dan Gohmanc7973eb2009-10-09 18:10:05 +00002750
2751 // Preserve memory reference information.
Dan Gohmanc7973eb2009-10-09 18:10:05 +00002752 cast<MachineSDNode>(Load)->setMemRefs(MMOs.first, MMOs.second);
Owen Anderson9a184ef2008-01-07 01:35:02 +00002753 }
2754
2755 // Emit the data processing instruction.
Owen Andersonac9de032009-08-10 22:56:29 +00002756 std::vector<EVT> VTs;
Owen Anderson9a184ef2008-01-07 01:35:02 +00002757 const TargetRegisterClass *DstRC = 0;
Chris Lattner0c2a4f32008-01-07 03:13:06 +00002758 if (TID.getNumDefs() > 0) {
Chris Lattner6a66b292009-07-29 21:10:12 +00002759 DstRC = TID.OpInfo[0].getRegClass(&RI);
Owen Anderson9a184ef2008-01-07 01:35:02 +00002760 VTs.push_back(*DstRC->vt_begin());
2761 }
2762 for (unsigned i = 0, e = N->getNumValues(); i != e; ++i) {
Owen Andersonac9de032009-08-10 22:56:29 +00002763 EVT VT = N->getValueType(i);
Owen Anderson36e3a6e2009-08-11 20:47:22 +00002764 if (VT != MVT::Other && i >= (unsigned)TID.getNumDefs())
Owen Anderson9a184ef2008-01-07 01:35:02 +00002765 VTs.push_back(VT);
2766 }
2767 if (Load)
Dan Gohman8181bd12008-07-27 21:46:04 +00002768 BeforeOps.push_back(SDValue(Load, 0));
Owen Anderson9a184ef2008-01-07 01:35:02 +00002769 std::copy(AfterOps.begin(), AfterOps.end(), std::back_inserter(BeforeOps));
Dan Gohman61fda0d2009-09-25 18:54:59 +00002770 SDNode *NewNode= DAG.getMachineNode(Opc, dl, VTs, &BeforeOps[0],
2771 BeforeOps.size());
Owen Anderson9a184ef2008-01-07 01:35:02 +00002772 NewNodes.push_back(NewNode);
2773
2774 // Emit the store instruction.
2775 if (FoldedStore) {
2776 AddrOps.pop_back();
Dan Gohman8181bd12008-07-27 21:46:04 +00002777 AddrOps.push_back(SDValue(NewNode, 0));
Owen Anderson9a184ef2008-01-07 01:35:02 +00002778 AddrOps.push_back(Chain);
Evan Cheng9d7cd4e2009-11-16 21:56:03 +00002779 std::pair<MachineInstr::mmo_iterator,
2780 MachineInstr::mmo_iterator> MMOs =
2781 MF.extractStoreMemRefs(cast<MachineSDNode>(N)->memoperands_begin(),
2782 cast<MachineSDNode>(N)->memoperands_end());
Evan Cheng4cb1de52010-07-02 20:36:18 +00002783 if (!(*MMOs.first) &&
2784 RC == &X86::VR128RegClass &&
2785 !TM.getSubtarget<X86Subtarget>().isUnalignedMemAccessFast())
2786 // Do not introduce a slow unaligned store.
2787 return false;
2788 bool isAligned = (*MMOs.first) && (*MMOs.first)->getAlignment() >= 16;
Dan Gohman61fda0d2009-09-25 18:54:59 +00002789 SDNode *Store = DAG.getMachineNode(getStoreRegOpcode(0, DstRC,
2790 isAligned, TM),
2791 dl, MVT::Other,
2792 &AddrOps[0], AddrOps.size());
Owen Anderson9a184ef2008-01-07 01:35:02 +00002793 NewNodes.push_back(Store);
Dan Gohmanc7973eb2009-10-09 18:10:05 +00002794
2795 // Preserve memory reference information.
Dan Gohmanc7973eb2009-10-09 18:10:05 +00002796 cast<MachineSDNode>(Load)->setMemRefs(MMOs.first, MMOs.second);
Owen Anderson9a184ef2008-01-07 01:35:02 +00002797 }
2798
2799 return true;
2800}
2801
2802unsigned X86InstrInfo::getOpcodeAfterMemoryUnfold(unsigned Opc,
Dan Gohmanf0116582009-10-30 22:18:41 +00002803 bool UnfoldLoad, bool UnfoldStore,
2804 unsigned *LoadRegIndex) const {
Jeffrey Yasskin8154d2e2009-11-10 01:02:17 +00002805 DenseMap<unsigned*, std::pair<unsigned,unsigned> >::const_iterator I =
Owen Anderson9a184ef2008-01-07 01:35:02 +00002806 MemOp2RegOpTable.find((unsigned*)Opc);
2807 if (I == MemOp2RegOpTable.end())
2808 return 0;
2809 bool FoldedLoad = I->second.second & (1 << 4);
2810 bool FoldedStore = I->second.second & (1 << 5);
2811 if (UnfoldLoad && !FoldedLoad)
2812 return 0;
2813 if (UnfoldStore && !FoldedStore)
2814 return 0;
Dan Gohmanf0116582009-10-30 22:18:41 +00002815 if (LoadRegIndex)
2816 *LoadRegIndex = I->second.second & 0xf;
Owen Anderson9a184ef2008-01-07 01:35:02 +00002817 return I->second.first;
2818}
2819
Evan Cheng0a4cae12010-01-22 03:34:51 +00002820bool
2821X86InstrInfo::areLoadsFromSameBasePtr(SDNode *Load1, SDNode *Load2,
2822 int64_t &Offset1, int64_t &Offset2) const {
2823 if (!Load1->isMachineOpcode() || !Load2->isMachineOpcode())
2824 return false;
2825 unsigned Opc1 = Load1->getMachineOpcode();
2826 unsigned Opc2 = Load2->getMachineOpcode();
2827 switch (Opc1) {
2828 default: return false;
2829 case X86::MOV8rm:
2830 case X86::MOV16rm:
2831 case X86::MOV32rm:
2832 case X86::MOV64rm:
2833 case X86::LD_Fp32m:
2834 case X86::LD_Fp64m:
2835 case X86::LD_Fp80m:
2836 case X86::MOVSSrm:
2837 case X86::MOVSDrm:
2838 case X86::MMX_MOVD64rm:
2839 case X86::MMX_MOVQ64rm:
2840 case X86::FsMOVAPSrm:
2841 case X86::FsMOVAPDrm:
2842 case X86::MOVAPSrm:
2843 case X86::MOVUPSrm:
2844 case X86::MOVUPSrm_Int:
2845 case X86::MOVAPDrm:
2846 case X86::MOVDQArm:
2847 case X86::MOVDQUrm:
2848 case X86::MOVDQUrm_Int:
2849 break;
2850 }
2851 switch (Opc2) {
2852 default: return false;
2853 case X86::MOV8rm:
2854 case X86::MOV16rm:
2855 case X86::MOV32rm:
2856 case X86::MOV64rm:
2857 case X86::LD_Fp32m:
2858 case X86::LD_Fp64m:
2859 case X86::LD_Fp80m:
2860 case X86::MOVSSrm:
2861 case X86::MOVSDrm:
2862 case X86::MMX_MOVD64rm:
2863 case X86::MMX_MOVQ64rm:
2864 case X86::FsMOVAPSrm:
2865 case X86::FsMOVAPDrm:
2866 case X86::MOVAPSrm:
2867 case X86::MOVUPSrm:
2868 case X86::MOVUPSrm_Int:
2869 case X86::MOVAPDrm:
2870 case X86::MOVDQArm:
2871 case X86::MOVDQUrm:
2872 case X86::MOVDQUrm_Int:
2873 break;
2874 }
2875
2876 // Check if chain operands and base addresses match.
2877 if (Load1->getOperand(0) != Load2->getOperand(0) ||
2878 Load1->getOperand(5) != Load2->getOperand(5))
2879 return false;
2880 // Segment operands should match as well.
2881 if (Load1->getOperand(4) != Load2->getOperand(4))
2882 return false;
2883 // Scale should be 1, Index should be Reg0.
2884 if (Load1->getOperand(1) == Load2->getOperand(1) &&
2885 Load1->getOperand(2) == Load2->getOperand(2)) {
2886 if (cast<ConstantSDNode>(Load1->getOperand(1))->getZExtValue() != 1)
2887 return false;
Evan Cheng0a4cae12010-01-22 03:34:51 +00002888
2889 // Now let's examine the displacements.
2890 if (isa<ConstantSDNode>(Load1->getOperand(3)) &&
2891 isa<ConstantSDNode>(Load2->getOperand(3))) {
2892 Offset1 = cast<ConstantSDNode>(Load1->getOperand(3))->getSExtValue();
2893 Offset2 = cast<ConstantSDNode>(Load2->getOperand(3))->getSExtValue();
2894 return true;
2895 }
2896 }
2897 return false;
2898}
2899
2900bool X86InstrInfo::shouldScheduleLoadsNear(SDNode *Load1, SDNode *Load2,
2901 int64_t Offset1, int64_t Offset2,
2902 unsigned NumLoads) const {
2903 assert(Offset2 > Offset1);
2904 if ((Offset2 - Offset1) / 8 > 64)
2905 return false;
2906
2907 unsigned Opc1 = Load1->getMachineOpcode();
2908 unsigned Opc2 = Load2->getMachineOpcode();
2909 if (Opc1 != Opc2)
2910 return false; // FIXME: overly conservative?
2911
2912 switch (Opc1) {
2913 default: break;
2914 case X86::LD_Fp32m:
2915 case X86::LD_Fp64m:
2916 case X86::LD_Fp80m:
2917 case X86::MMX_MOVD64rm:
2918 case X86::MMX_MOVQ64rm:
2919 return false;
2920 }
2921
2922 EVT VT = Load1->getValueType(0);
2923 switch (VT.getSimpleVT().SimpleTy) {
Bill Wendlingf5e1a712010-06-22 22:16:17 +00002924 default:
Evan Cheng0a4cae12010-01-22 03:34:51 +00002925 // XMM registers. In 64-bit mode we can be a bit more aggressive since we
2926 // have 16 of them to play with.
2927 if (TM.getSubtargetImpl()->is64Bit()) {
2928 if (NumLoads >= 3)
2929 return false;
Bill Wendlingf5e1a712010-06-22 22:16:17 +00002930 } else if (NumLoads) {
Evan Cheng0a4cae12010-01-22 03:34:51 +00002931 return false;
Bill Wendlingf5e1a712010-06-22 22:16:17 +00002932 }
Evan Cheng0a4cae12010-01-22 03:34:51 +00002933 break;
Evan Cheng0a4cae12010-01-22 03:34:51 +00002934 case MVT::i8:
2935 case MVT::i16:
2936 case MVT::i32:
2937 case MVT::i64:
Evan Cheng81eb1662010-01-22 23:49:11 +00002938 case MVT::f32:
2939 case MVT::f64:
Evan Cheng0a4cae12010-01-22 03:34:51 +00002940 if (NumLoads)
2941 return false;
Bill Wendlingf5e1a712010-06-22 22:16:17 +00002942 break;
Evan Cheng0a4cae12010-01-22 03:34:51 +00002943 }
2944
2945 return true;
2946}
2947
2948
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002949bool X86InstrInfo::
Owen Andersond131b5b2008-08-14 22:49:33 +00002950ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002951 assert(Cond.size() == 1 && "Invalid X86 branch condition!");
Evan Chenge3f1a412008-08-29 23:21:31 +00002952 X86::CondCode CC = static_cast<X86::CondCode>(Cond[0].getImm());
Dan Gohman6a00fcb2008-10-21 03:29:32 +00002953 if (CC == X86::COND_NE_OR_P || CC == X86::COND_NP_OR_E)
2954 return true;
Evan Chenge3f1a412008-08-29 23:21:31 +00002955 Cond[0].setImm(GetOppositeBranchCondition(CC));
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002956 return false;
2957}
2958
Evan Cheng0e4a5a92008-10-27 07:14:50 +00002959bool X86InstrInfo::
Evan Chengf5a8a362009-02-06 17:17:30 +00002960isSafeToMoveRegClassDefs(const TargetRegisterClass *RC) const {
2961 // FIXME: Return false for x87 stack register classes for now. We can't
Evan Cheng0e4a5a92008-10-27 07:14:50 +00002962 // allow any loads of these registers before FpGet_ST0_80.
Evan Chengf5a8a362009-02-06 17:17:30 +00002963 return !(RC == &X86::CCRRegClass || RC == &X86::RFP32RegClass ||
2964 RC == &X86::RFP64RegClass || RC == &X86::RFP80RegClass);
Evan Cheng0e4a5a92008-10-27 07:14:50 +00002965}
2966
Nicolas Geoffraycb162a02008-04-16 20:10:13 +00002967
Chris Lattnerb98aa8a2010-02-05 22:10:22 +00002968/// isX86_64ExtendedReg - Is the MachineOperand a x86-64 extended (r8 or higher)
2969/// register? e.g. r8, xmm8, xmm13, etc.
2970bool X86InstrInfo::isX86_64ExtendedReg(unsigned RegNo) {
2971 switch (RegNo) {
Nicolas Geoffraycb162a02008-04-16 20:10:13 +00002972 default: break;
2973 case X86::R8: case X86::R9: case X86::R10: case X86::R11:
2974 case X86::R12: case X86::R13: case X86::R14: case X86::R15:
2975 case X86::R8D: case X86::R9D: case X86::R10D: case X86::R11D:
2976 case X86::R12D: case X86::R13D: case X86::R14D: case X86::R15D:
2977 case X86::R8W: case X86::R9W: case X86::R10W: case X86::R11W:
2978 case X86::R12W: case X86::R13W: case X86::R14W: case X86::R15W:
2979 case X86::R8B: case X86::R9B: case X86::R10B: case X86::R11B:
2980 case X86::R12B: case X86::R13B: case X86::R14B: case X86::R15B:
2981 case X86::XMM8: case X86::XMM9: case X86::XMM10: case X86::XMM11:
2982 case X86::XMM12: case X86::XMM13: case X86::XMM14: case X86::XMM15:
Bruno Cardoso Lopes46aa8ce2010-07-09 18:27:43 +00002983 case X86::YMM8: case X86::YMM9: case X86::YMM10: case X86::YMM11:
2984 case X86::YMM12: case X86::YMM13: case X86::YMM14: case X86::YMM15:
Nicolas Geoffraycb162a02008-04-16 20:10:13 +00002985 return true;
2986 }
2987 return false;
2988}
2989
2990
2991/// determineREX - Determine if the MachineInstr has to be encoded with a X86-64
2992/// REX prefix which specifies 1) 64-bit instructions, 2) non-default operand
2993/// size, and 3) use of X86-64 extended registers.
2994unsigned X86InstrInfo::determineREX(const MachineInstr &MI) {
2995 unsigned REX = 0;
2996 const TargetInstrDesc &Desc = MI.getDesc();
2997
2998 // Pseudo instructions do not need REX prefix byte.
2999 if ((Desc.TSFlags & X86II::FormMask) == X86II::Pseudo)
3000 return 0;
3001 if (Desc.TSFlags & X86II::REX_W)
3002 REX |= 1 << 3;
3003
3004 unsigned NumOps = Desc.getNumOperands();
3005 if (NumOps) {
3006 bool isTwoAddr = NumOps > 1 &&
3007 Desc.getOperandConstraint(1, TOI::TIED_TO) != -1;
3008
3009 // If it accesses SPL, BPL, SIL, or DIL, then it requires a 0x40 REX prefix.
3010 unsigned i = isTwoAddr ? 1 : 0;
3011 for (unsigned e = NumOps; i != e; ++i) {
3012 const MachineOperand& MO = MI.getOperand(i);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00003013 if (MO.isReg()) {
Nicolas Geoffraycb162a02008-04-16 20:10:13 +00003014 unsigned Reg = MO.getReg();
3015 if (isX86_64NonExtLowByteReg(Reg))
3016 REX |= 0x40;
3017 }
3018 }
3019
3020 switch (Desc.TSFlags & X86II::FormMask) {
3021 case X86II::MRMInitReg:
3022 if (isX86_64ExtendedReg(MI.getOperand(0)))
3023 REX |= (1 << 0) | (1 << 2);
3024 break;
3025 case X86II::MRMSrcReg: {
3026 if (isX86_64ExtendedReg(MI.getOperand(0)))
3027 REX |= 1 << 2;
3028 i = isTwoAddr ? 2 : 1;
3029 for (unsigned e = NumOps; i != e; ++i) {
3030 const MachineOperand& MO = MI.getOperand(i);
3031 if (isX86_64ExtendedReg(MO))
3032 REX |= 1 << 0;
3033 }
3034 break;
3035 }
3036 case X86II::MRMSrcMem: {
3037 if (isX86_64ExtendedReg(MI.getOperand(0)))
3038 REX |= 1 << 2;
3039 unsigned Bit = 0;
3040 i = isTwoAddr ? 2 : 1;
3041 for (; i != NumOps; ++i) {
3042 const MachineOperand& MO = MI.getOperand(i);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00003043 if (MO.isReg()) {
Nicolas Geoffraycb162a02008-04-16 20:10:13 +00003044 if (isX86_64ExtendedReg(MO))
3045 REX |= 1 << Bit;
3046 Bit++;
3047 }
3048 }
3049 break;
3050 }
3051 case X86II::MRM0m: case X86II::MRM1m:
3052 case X86II::MRM2m: case X86II::MRM3m:
3053 case X86II::MRM4m: case X86II::MRM5m:
3054 case X86II::MRM6m: case X86II::MRM7m:
3055 case X86II::MRMDestMem: {
Chris Lattner65d58572010-07-08 22:41:28 +00003056 unsigned e = (isTwoAddr ? X86::AddrNumOperands+1 : X86::AddrNumOperands);
Nicolas Geoffraycb162a02008-04-16 20:10:13 +00003057 i = isTwoAddr ? 1 : 0;
3058 if (NumOps > e && isX86_64ExtendedReg(MI.getOperand(e)))
3059 REX |= 1 << 2;
3060 unsigned Bit = 0;
3061 for (; i != e; ++i) {
3062 const MachineOperand& MO = MI.getOperand(i);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00003063 if (MO.isReg()) {
Nicolas Geoffraycb162a02008-04-16 20:10:13 +00003064 if (isX86_64ExtendedReg(MO))
3065 REX |= 1 << Bit;
3066 Bit++;
3067 }
3068 }
3069 break;
3070 }
3071 default: {
3072 if (isX86_64ExtendedReg(MI.getOperand(0)))
3073 REX |= 1 << 0;
3074 i = isTwoAddr ? 2 : 1;
3075 for (unsigned e = NumOps; i != e; ++i) {
3076 const MachineOperand& MO = MI.getOperand(i);
3077 if (isX86_64ExtendedReg(MO))
3078 REX |= 1 << 2;
3079 }
3080 break;
3081 }
3082 }
3083 }
3084 return REX;
3085}
3086
3087/// sizePCRelativeBlockAddress - This method returns the size of a PC
3088/// relative block address instruction
3089///
3090static unsigned sizePCRelativeBlockAddress() {
3091 return 4;
3092}
3093
3094/// sizeGlobalAddress - Give the size of the emission of this global address
3095///
3096static unsigned sizeGlobalAddress(bool dword) {
3097 return dword ? 8 : 4;
3098}
3099
3100/// sizeConstPoolAddress - Give the size of the emission of this constant
3101/// pool address
3102///
3103static unsigned sizeConstPoolAddress(bool dword) {
3104 return dword ? 8 : 4;
3105}
3106
3107/// sizeExternalSymbolAddress - Give the size of the emission of this external
3108/// symbol
3109///
3110static unsigned sizeExternalSymbolAddress(bool dword) {
3111 return dword ? 8 : 4;
3112}
3113
3114/// sizeJumpTableAddress - Give the size of the emission of this jump
3115/// table address
3116///
3117static unsigned sizeJumpTableAddress(bool dword) {
3118 return dword ? 8 : 4;
3119}
3120
3121static unsigned sizeConstant(unsigned Size) {
3122 return Size;
3123}
3124
3125static unsigned sizeRegModRMByte(){
3126 return 1;
3127}
3128
3129static unsigned sizeSIBByte(){
3130 return 1;
3131}
3132
3133static unsigned getDisplacementFieldSize(const MachineOperand *RelocOp) {
3134 unsigned FinalSize = 0;
3135 // If this is a simple integer displacement that doesn't require a relocation.
3136 if (!RelocOp) {
3137 FinalSize += sizeConstant(4);
3138 return FinalSize;
3139 }
3140
3141 // Otherwise, this is something that requires a relocation.
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00003142 if (RelocOp->isGlobal()) {
Nicolas Geoffraycb162a02008-04-16 20:10:13 +00003143 FinalSize += sizeGlobalAddress(false);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00003144 } else if (RelocOp->isCPI()) {
Nicolas Geoffraycb162a02008-04-16 20:10:13 +00003145 FinalSize += sizeConstPoolAddress(false);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00003146 } else if (RelocOp->isJTI()) {
Nicolas Geoffraycb162a02008-04-16 20:10:13 +00003147 FinalSize += sizeJumpTableAddress(false);
3148 } else {
Edwin Törökbd448e32009-07-14 16:55:14 +00003149 llvm_unreachable("Unknown value to relocate!");
Nicolas Geoffraycb162a02008-04-16 20:10:13 +00003150 }
3151 return FinalSize;
3152}
3153
3154static unsigned getMemModRMByteSize(const MachineInstr &MI, unsigned Op,
3155 bool IsPIC, bool Is64BitMode) {
3156 const MachineOperand &Op3 = MI.getOperand(Op+3);
3157 int DispVal = 0;
3158 const MachineOperand *DispForReloc = 0;
3159 unsigned FinalSize = 0;
3160
3161 // Figure out what sort of displacement we have to handle here.
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00003162 if (Op3.isGlobal()) {
Nicolas Geoffraycb162a02008-04-16 20:10:13 +00003163 DispForReloc = &Op3;
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00003164 } else if (Op3.isCPI()) {
Nicolas Geoffraycb162a02008-04-16 20:10:13 +00003165 if (Is64BitMode || IsPIC) {
3166 DispForReloc = &Op3;
3167 } else {
3168 DispVal = 1;
3169 }
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00003170 } else if (Op3.isJTI()) {
Nicolas Geoffraycb162a02008-04-16 20:10:13 +00003171 if (Is64BitMode || IsPIC) {
3172 DispForReloc = &Op3;
3173 } else {
3174 DispVal = 1;
3175 }
3176 } else {
3177 DispVal = 1;
3178 }
3179
3180 const MachineOperand &Base = MI.getOperand(Op);
3181 const MachineOperand &IndexReg = MI.getOperand(Op+2);
3182
3183 unsigned BaseReg = Base.getReg();
3184
3185 // Is a SIB byte needed?
Evan Cheng92569ce2009-05-12 00:07:35 +00003186 if ((!Is64BitMode || DispForReloc || BaseReg != 0) &&
3187 IndexReg.getReg() == 0 &&
Evan Cheng099109d2009-05-04 22:49:16 +00003188 (BaseReg == 0 || X86RegisterInfo::getX86RegNum(BaseReg) != N86::ESP)) {
Nicolas Geoffraycb162a02008-04-16 20:10:13 +00003189 if (BaseReg == 0) { // Just a displacement?
3190 // Emit special case [disp32] encoding
3191 ++FinalSize;
3192 FinalSize += getDisplacementFieldSize(DispForReloc);
3193 } else {
3194 unsigned BaseRegNo = X86RegisterInfo::getX86RegNum(BaseReg);
3195 if (!DispForReloc && DispVal == 0 && BaseRegNo != N86::EBP) {
3196 // Emit simple indirect register encoding... [EAX] f.e.
3197 ++FinalSize;
3198 // Be pessimistic and assume it's a disp32, not a disp8
3199 } else {
3200 // Emit the most general non-SIB encoding: [REG+disp32]
3201 ++FinalSize;
3202 FinalSize += getDisplacementFieldSize(DispForReloc);
3203 }
3204 }
3205
3206 } else { // We need a SIB byte, so start by outputting the ModR/M byte first
3207 assert(IndexReg.getReg() != X86::ESP &&
3208 IndexReg.getReg() != X86::RSP && "Cannot use ESP as index reg!");
3209
3210 bool ForceDisp32 = false;
3211 if (BaseReg == 0 || DispForReloc) {
3212 // Emit the normal disp32 encoding.
3213 ++FinalSize;
3214 ForceDisp32 = true;
3215 } else {
3216 ++FinalSize;
3217 }
3218
3219 FinalSize += sizeSIBByte();
3220
3221 // Do we need to output a displacement?
3222 if (DispVal != 0 || ForceDisp32) {
3223 FinalSize += getDisplacementFieldSize(DispForReloc);
3224 }
3225 }
3226 return FinalSize;
3227}
3228
3229
3230static unsigned GetInstSizeWithDesc(const MachineInstr &MI,
3231 const TargetInstrDesc *Desc,
3232 bool IsPIC, bool Is64BitMode) {
3233
3234 unsigned Opcode = Desc->Opcode;
3235 unsigned FinalSize = 0;
3236
3237 // Emit the lock opcode prefix as needed.
3238 if (Desc->TSFlags & X86II::LOCK) ++FinalSize;
3239
Bill Wendling6ee76552009-05-28 23:40:46 +00003240 // Emit segment override opcode prefix as needed.
Anton Korobeynikov4b7be802008-10-12 10:30:11 +00003241 switch (Desc->TSFlags & X86II::SegOvrMask) {
3242 case X86II::FS:
3243 case X86II::GS:
3244 ++FinalSize;
3245 break;
Edwin Törökbd448e32009-07-14 16:55:14 +00003246 default: llvm_unreachable("Invalid segment!");
Anton Korobeynikov4b7be802008-10-12 10:30:11 +00003247 case 0: break; // No segment override!
3248 }
3249
Nicolas Geoffraycb162a02008-04-16 20:10:13 +00003250 // Emit the repeat opcode prefix as needed.
3251 if ((Desc->TSFlags & X86II::Op0Mask) == X86II::REP) ++FinalSize;
3252
3253 // Emit the operand size opcode prefix as needed.
3254 if (Desc->TSFlags & X86II::OpSize) ++FinalSize;
3255
3256 // Emit the address size opcode prefix as needed.
3257 if (Desc->TSFlags & X86II::AdSize) ++FinalSize;
3258
3259 bool Need0FPrefix = false;
3260 switch (Desc->TSFlags & X86II::Op0Mask) {
3261 case X86II::TB: // Two-byte opcode prefix
3262 case X86II::T8: // 0F 38
3263 case X86II::TA: // 0F 3A
3264 Need0FPrefix = true;
3265 break;
Eric Christopherb5f948c2009-08-08 21:55:08 +00003266 case X86II::TF: // F2 0F 38
3267 ++FinalSize;
3268 Need0FPrefix = true;
3269 break;
Nicolas Geoffraycb162a02008-04-16 20:10:13 +00003270 case X86II::REP: break; // already handled.
3271 case X86II::XS: // F3 0F
3272 ++FinalSize;
3273 Need0FPrefix = true;
3274 break;
3275 case X86II::XD: // F2 0F
3276 ++FinalSize;
3277 Need0FPrefix = true;
3278 break;
3279 case X86II::D8: case X86II::D9: case X86II::DA: case X86II::DB:
3280 case X86II::DC: case X86II::DD: case X86II::DE: case X86II::DF:
3281 ++FinalSize;
3282 break; // Two-byte opcode prefix
Edwin Törökbd448e32009-07-14 16:55:14 +00003283 default: llvm_unreachable("Invalid prefix!");
Nicolas Geoffraycb162a02008-04-16 20:10:13 +00003284 case 0: break; // No prefix!
3285 }
3286
3287 if (Is64BitMode) {
3288 // REX prefix
3289 unsigned REX = X86InstrInfo::determineREX(MI);
3290 if (REX)
3291 ++FinalSize;
3292 }
3293
3294 // 0x0F escape code must be emitted just before the opcode.
3295 if (Need0FPrefix)
3296 ++FinalSize;
3297
3298 switch (Desc->TSFlags & X86II::Op0Mask) {
3299 case X86II::T8: // 0F 38
3300 ++FinalSize;
3301 break;
Bill Wendling6ee76552009-05-28 23:40:46 +00003302 case X86II::TA: // 0F 3A
Nicolas Geoffraycb162a02008-04-16 20:10:13 +00003303 ++FinalSize;
3304 break;
Eric Christopherb5f948c2009-08-08 21:55:08 +00003305 case X86II::TF: // F2 0F 38
3306 ++FinalSize;
3307 break;
Nicolas Geoffraycb162a02008-04-16 20:10:13 +00003308 }
3309
3310 // If this is a two-address instruction, skip one of the register operands.
3311 unsigned NumOps = Desc->getNumOperands();
3312 unsigned CurOp = 0;
3313 if (NumOps > 1 && Desc->getOperandConstraint(1, TOI::TIED_TO) != -1)
3314 CurOp++;
Evan Cheng099109d2009-05-04 22:49:16 +00003315 else if (NumOps > 2 && Desc->getOperandConstraint(NumOps-1, TOI::TIED_TO)== 0)
3316 // Skip the last source operand that is tied_to the dest reg. e.g. LXADD32
3317 --NumOps;
Nicolas Geoffraycb162a02008-04-16 20:10:13 +00003318
3319 switch (Desc->TSFlags & X86II::FormMask) {
Edwin Törökbd448e32009-07-14 16:55:14 +00003320 default: llvm_unreachable("Unknown FormMask value in X86 MachineCodeEmitter!");
Nicolas Geoffraycb162a02008-04-16 20:10:13 +00003321 case X86II::Pseudo:
3322 // Remember the current PC offset, this is the PIC relocation
3323 // base address.
3324 switch (Opcode) {
3325 default:
3326 break;
Chris Lattner4052b292010-02-09 19:54:29 +00003327 case TargetOpcode::INLINEASM: {
Nicolas Geoffraycb162a02008-04-16 20:10:13 +00003328 const MachineFunction *MF = MI.getParent()->getParent();
Chris Lattner5f1fdb32009-08-02 05:20:37 +00003329 const TargetInstrInfo &TII = *MF->getTarget().getInstrInfo();
3330 FinalSize += TII.getInlineAsmLength(MI.getOperand(0).getSymbolName(),
Chris Lattner621c44d2009-08-22 20:48:53 +00003331 *MF->getTarget().getMCAsmInfo());
Nicolas Geoffraycb162a02008-04-16 20:10:13 +00003332 break;
3333 }
Chris Lattner4052b292010-02-09 19:54:29 +00003334 case TargetOpcode::DBG_LABEL:
3335 case TargetOpcode::EH_LABEL:
Dale Johannesenac548972010-04-07 19:51:44 +00003336 case TargetOpcode::DBG_VALUE:
Nicolas Geoffraycb162a02008-04-16 20:10:13 +00003337 break;
Chris Lattner4052b292010-02-09 19:54:29 +00003338 case TargetOpcode::IMPLICIT_DEF:
3339 case TargetOpcode::KILL:
Nicolas Geoffraycb162a02008-04-16 20:10:13 +00003340 case X86::FP_REG_KILL:
3341 break;
3342 case X86::MOVPC32r: {
3343 // This emits the "call" portion of this pseudo instruction.
3344 ++FinalSize;
Chris Lattnerdae24402010-02-05 19:24:13 +00003345 FinalSize += sizeConstant(X86II::getSizeOfImm(Desc->TSFlags));
Nicolas Geoffraycb162a02008-04-16 20:10:13 +00003346 break;
3347 }
3348 }
3349 CurOp = NumOps;
3350 break;
3351 case X86II::RawFrm:
3352 ++FinalSize;
3353
3354 if (CurOp != NumOps) {
3355 const MachineOperand &MO = MI.getOperand(CurOp++);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00003356 if (MO.isMBB()) {
Nicolas Geoffraycb162a02008-04-16 20:10:13 +00003357 FinalSize += sizePCRelativeBlockAddress();
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00003358 } else if (MO.isGlobal()) {
Nicolas Geoffraycb162a02008-04-16 20:10:13 +00003359 FinalSize += sizeGlobalAddress(false);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00003360 } else if (MO.isSymbol()) {
Nicolas Geoffraycb162a02008-04-16 20:10:13 +00003361 FinalSize += sizeExternalSymbolAddress(false);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00003362 } else if (MO.isImm()) {
Chris Lattnerdae24402010-02-05 19:24:13 +00003363 FinalSize += sizeConstant(X86II::getSizeOfImm(Desc->TSFlags));
Nicolas Geoffraycb162a02008-04-16 20:10:13 +00003364 } else {
Edwin Törökbd448e32009-07-14 16:55:14 +00003365 llvm_unreachable("Unknown RawFrm operand!");
Nicolas Geoffraycb162a02008-04-16 20:10:13 +00003366 }
3367 }
3368 break;
3369
3370 case X86II::AddRegFrm:
3371 ++FinalSize;
Nicolas Geoffrayf22f1cd2008-04-20 23:36:47 +00003372 ++CurOp;
Nicolas Geoffraycb162a02008-04-16 20:10:13 +00003373
3374 if (CurOp != NumOps) {
3375 const MachineOperand &MO1 = MI.getOperand(CurOp++);
Chris Lattnerdae24402010-02-05 19:24:13 +00003376 unsigned Size = X86II::getSizeOfImm(Desc->TSFlags);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00003377 if (MO1.isImm())
Nicolas Geoffraycb162a02008-04-16 20:10:13 +00003378 FinalSize += sizeConstant(Size);
3379 else {
3380 bool dword = false;
3381 if (Opcode == X86::MOV64ri)
3382 dword = true;
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00003383 if (MO1.isGlobal()) {
Nicolas Geoffraycb162a02008-04-16 20:10:13 +00003384 FinalSize += sizeGlobalAddress(dword);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00003385 } else if (MO1.isSymbol())
Nicolas Geoffraycb162a02008-04-16 20:10:13 +00003386 FinalSize += sizeExternalSymbolAddress(dword);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00003387 else if (MO1.isCPI())
Nicolas Geoffraycb162a02008-04-16 20:10:13 +00003388 FinalSize += sizeConstPoolAddress(dword);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00003389 else if (MO1.isJTI())
Nicolas Geoffraycb162a02008-04-16 20:10:13 +00003390 FinalSize += sizeJumpTableAddress(dword);
3391 }
3392 }
3393 break;
3394
3395 case X86II::MRMDestReg: {
3396 ++FinalSize;
3397 FinalSize += sizeRegModRMByte();
3398 CurOp += 2;
Nicolas Geoffrayf22f1cd2008-04-20 23:36:47 +00003399 if (CurOp != NumOps) {
3400 ++CurOp;
Chris Lattnerdae24402010-02-05 19:24:13 +00003401 FinalSize += sizeConstant(X86II::getSizeOfImm(Desc->TSFlags));
Nicolas Geoffrayf22f1cd2008-04-20 23:36:47 +00003402 }
Nicolas Geoffraycb162a02008-04-16 20:10:13 +00003403 break;
3404 }
3405 case X86II::MRMDestMem: {
3406 ++FinalSize;
3407 FinalSize += getMemModRMByteSize(MI, CurOp, IsPIC, Is64BitMode);
Chris Lattner65d58572010-07-08 22:41:28 +00003408 CurOp += X86::AddrNumOperands + 1;
Nicolas Geoffrayf22f1cd2008-04-20 23:36:47 +00003409 if (CurOp != NumOps) {
3410 ++CurOp;
Chris Lattnerdae24402010-02-05 19:24:13 +00003411 FinalSize += sizeConstant(X86II::getSizeOfImm(Desc->TSFlags));
Nicolas Geoffrayf22f1cd2008-04-20 23:36:47 +00003412 }
Nicolas Geoffraycb162a02008-04-16 20:10:13 +00003413 break;
3414 }
3415
3416 case X86II::MRMSrcReg:
3417 ++FinalSize;
3418 FinalSize += sizeRegModRMByte();
3419 CurOp += 2;
Nicolas Geoffrayf22f1cd2008-04-20 23:36:47 +00003420 if (CurOp != NumOps) {
3421 ++CurOp;
Chris Lattnerdae24402010-02-05 19:24:13 +00003422 FinalSize += sizeConstant(X86II::getSizeOfImm(Desc->TSFlags));
Nicolas Geoffrayf22f1cd2008-04-20 23:36:47 +00003423 }
Nicolas Geoffraycb162a02008-04-16 20:10:13 +00003424 break;
3425
3426 case X86II::MRMSrcMem: {
Nicolas Geoffraycb162a02008-04-16 20:10:13 +00003427 ++FinalSize;
3428 FinalSize += getMemModRMByteSize(MI, CurOp+1, IsPIC, Is64BitMode);
Chris Lattnereed3f012010-07-08 23:46:44 +00003429 CurOp += X86::AddrNumOperands + 1;
Nicolas Geoffrayf22f1cd2008-04-20 23:36:47 +00003430 if (CurOp != NumOps) {
3431 ++CurOp;
Chris Lattnerdae24402010-02-05 19:24:13 +00003432 FinalSize += sizeConstant(X86II::getSizeOfImm(Desc->TSFlags));
Nicolas Geoffrayf22f1cd2008-04-20 23:36:47 +00003433 }
Nicolas Geoffraycb162a02008-04-16 20:10:13 +00003434 break;
3435 }
3436
3437 case X86II::MRM0r: case X86II::MRM1r:
3438 case X86II::MRM2r: case X86II::MRM3r:
3439 case X86II::MRM4r: case X86II::MRM5r:
3440 case X86II::MRM6r: case X86II::MRM7r:
3441 ++FinalSize;
Evan Cheng099109d2009-05-04 22:49:16 +00003442 if (Desc->getOpcode() == X86::LFENCE ||
Bill Wendling6ee76552009-05-28 23:40:46 +00003443 Desc->getOpcode() == X86::MFENCE) {
3444 // Special handling of lfence and mfence;
Evan Cheng099109d2009-05-04 22:49:16 +00003445 FinalSize += sizeRegModRMByte();
Bill Wendling6ee76552009-05-28 23:40:46 +00003446 } else if (Desc->getOpcode() == X86::MONITOR ||
3447 Desc->getOpcode() == X86::MWAIT) {
3448 // Special handling of monitor and mwait.
3449 FinalSize += sizeRegModRMByte() + 1; // +1 for the opcode.
3450 } else {
Evan Cheng099109d2009-05-04 22:49:16 +00003451 ++CurOp;
3452 FinalSize += sizeRegModRMByte();
3453 }
Nicolas Geoffraycb162a02008-04-16 20:10:13 +00003454
3455 if (CurOp != NumOps) {
3456 const MachineOperand &MO1 = MI.getOperand(CurOp++);
Chris Lattnerdae24402010-02-05 19:24:13 +00003457 unsigned Size = X86II::getSizeOfImm(Desc->TSFlags);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00003458 if (MO1.isImm())
Nicolas Geoffraycb162a02008-04-16 20:10:13 +00003459 FinalSize += sizeConstant(Size);
3460 else {
3461 bool dword = false;
3462 if (Opcode == X86::MOV64ri32)
3463 dword = true;
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00003464 if (MO1.isGlobal()) {
Nicolas Geoffraycb162a02008-04-16 20:10:13 +00003465 FinalSize += sizeGlobalAddress(dword);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00003466 } else if (MO1.isSymbol())
Nicolas Geoffraycb162a02008-04-16 20:10:13 +00003467 FinalSize += sizeExternalSymbolAddress(dword);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00003468 else if (MO1.isCPI())
Nicolas Geoffraycb162a02008-04-16 20:10:13 +00003469 FinalSize += sizeConstPoolAddress(dword);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00003470 else if (MO1.isJTI())
Nicolas Geoffraycb162a02008-04-16 20:10:13 +00003471 FinalSize += sizeJumpTableAddress(dword);
3472 }
3473 }
3474 break;
3475
3476 case X86II::MRM0m: case X86II::MRM1m:
3477 case X86II::MRM2m: case X86II::MRM3m:
3478 case X86II::MRM4m: case X86II::MRM5m:
3479 case X86II::MRM6m: case X86II::MRM7m: {
3480
3481 ++FinalSize;
3482 FinalSize += getMemModRMByteSize(MI, CurOp, IsPIC, Is64BitMode);
Chris Lattner65d58572010-07-08 22:41:28 +00003483 CurOp += X86::AddrNumOperands;
Nicolas Geoffraycb162a02008-04-16 20:10:13 +00003484
3485 if (CurOp != NumOps) {
3486 const MachineOperand &MO = MI.getOperand(CurOp++);
Chris Lattnerdae24402010-02-05 19:24:13 +00003487 unsigned Size = X86II::getSizeOfImm(Desc->TSFlags);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00003488 if (MO.isImm())
Nicolas Geoffraycb162a02008-04-16 20:10:13 +00003489 FinalSize += sizeConstant(Size);
3490 else {
3491 bool dword = false;
3492 if (Opcode == X86::MOV64mi32)
3493 dword = true;
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00003494 if (MO.isGlobal()) {
Nicolas Geoffraycb162a02008-04-16 20:10:13 +00003495 FinalSize += sizeGlobalAddress(dword);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00003496 } else if (MO.isSymbol())
Nicolas Geoffraycb162a02008-04-16 20:10:13 +00003497 FinalSize += sizeExternalSymbolAddress(dword);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00003498 else if (MO.isCPI())
Nicolas Geoffraycb162a02008-04-16 20:10:13 +00003499 FinalSize += sizeConstPoolAddress(dword);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +00003500 else if (MO.isJTI())
Nicolas Geoffraycb162a02008-04-16 20:10:13 +00003501 FinalSize += sizeJumpTableAddress(dword);
3502 }
3503 }
3504 break;
Chris Lattneraf0b8b72010-02-12 02:06:33 +00003505
3506 case X86II::MRM_C1:
3507 case X86II::MRM_C8:
3508 case X86II::MRM_C9:
3509 case X86II::MRM_E8:
3510 case X86II::MRM_F0:
3511 FinalSize += 2;
3512 break;
Nicolas Geoffraycb162a02008-04-16 20:10:13 +00003513 }
3514
3515 case X86II::MRMInitReg:
3516 ++FinalSize;
3517 // Duplicate register, used by things like MOV8r0 (aka xor reg,reg).
3518 FinalSize += sizeRegModRMByte();
3519 ++CurOp;
3520 break;
3521 }
3522
3523 if (!Desc->isVariadic() && CurOp != NumOps) {
Edwin Török3cb88482009-07-08 18:01:40 +00003524 std::string msg;
3525 raw_string_ostream Msg(msg);
3526 Msg << "Cannot determine size: " << MI;
Chris Lattner8316f2d2010-04-07 22:58:41 +00003527 report_fatal_error(Msg.str());
Nicolas Geoffraycb162a02008-04-16 20:10:13 +00003528 }
3529
3530
3531 return FinalSize;
3532}
3533
3534
3535unsigned X86InstrInfo::GetInstSizeInBytes(const MachineInstr *MI) const {
3536 const TargetInstrDesc &Desc = MI->getDesc();
Chris Lattner144e3482009-07-10 20:53:38 +00003537 bool IsPIC = TM.getRelocationModel() == Reloc::PIC_;
Dan Gohmanb41dfba2008-05-14 01:58:56 +00003538 bool Is64BitMode = TM.getSubtargetImpl()->is64Bit();
Nicolas Geoffraycb162a02008-04-16 20:10:13 +00003539 unsigned Size = GetInstSizeWithDesc(*MI, &Desc, IsPIC, Is64BitMode);
Chris Lattner739b0102009-06-25 17:28:07 +00003540 if (Desc.getOpcode() == X86::MOVPC32r)
Nicolas Geoffraycb162a02008-04-16 20:10:13 +00003541 Size += GetInstSizeWithDesc(*MI, &get(X86::POP32r), IsPIC, Is64BitMode);
Nicolas Geoffraycb162a02008-04-16 20:10:13 +00003542 return Size;
3543}
Dan Gohmanb60482f2008-09-23 18:22:58 +00003544
Dan Gohman882ab732008-09-30 00:58:23 +00003545/// getGlobalBaseReg - Return a virtual register initialized with the
3546/// the global base register value. Output instructions required to
3547/// initialize the register in the function entry block, if necessary.
Dan Gohmanb60482f2008-09-23 18:22:58 +00003548///
Dan Gohmanb4482cf2010-07-10 09:00:22 +00003549/// TODO: Eliminate this and move the code to X86MachineFunctionInfo.
3550///
Dan Gohman882ab732008-09-30 00:58:23 +00003551unsigned X86InstrInfo::getGlobalBaseReg(MachineFunction *MF) const {
3552 assert(!TM.getSubtarget<X86Subtarget>().is64Bit() &&
3553 "X86-64 PIC uses RIP relative addressing");
3554
3555 X86MachineFunctionInfo *X86FI = MF->getInfo<X86MachineFunctionInfo>();
3556 unsigned GlobalBaseReg = X86FI->getGlobalBaseReg();
3557 if (GlobalBaseReg != 0)
3558 return GlobalBaseReg;
3559
Dan Gohmanb4482cf2010-07-10 09:00:22 +00003560 // Create the register. The code to initialize it is inserted
3561 // later, by the CGBR pass (below).
Dan Gohmanb60482f2008-09-23 18:22:58 +00003562 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Dan Gohmanb4482cf2010-07-10 09:00:22 +00003563 GlobalBaseReg = RegInfo.createVirtualRegister(X86::GR32RegisterClass);
Dan Gohman882ab732008-09-30 00:58:23 +00003564 X86FI->setGlobalBaseReg(GlobalBaseReg);
3565 return GlobalBaseReg;
Dan Gohmanb60482f2008-09-23 18:22:58 +00003566}
Jakob Stoklund Olesen0ba75302010-03-25 17:25:00 +00003567
Jakob Stoklund Olesen9b3a5512010-03-29 23:24:21 +00003568// These are the replaceable SSE instructions. Some of these have Int variants
3569// that we don't include here. We don't want to replace instructions selected
3570// by intrinsics.
3571static const unsigned ReplaceableInstrs[][3] = {
3572 //PackedInt PackedSingle PackedDouble
Jakob Stoklund Olesendeeb9df2010-03-30 22:46:53 +00003573 { X86::MOVAPSmr, X86::MOVAPDmr, X86::MOVDQAmr },
3574 { X86::MOVAPSrm, X86::MOVAPDrm, X86::MOVDQArm },
3575 { X86::MOVAPSrr, X86::MOVAPDrr, X86::MOVDQArr },
3576 { X86::MOVUPSmr, X86::MOVUPDmr, X86::MOVDQUmr },
3577 { X86::MOVUPSrm, X86::MOVUPDrm, X86::MOVDQUrm },
3578 { X86::MOVNTPSmr, X86::MOVNTPDmr, X86::MOVNTDQmr },
3579 { X86::ANDNPSrm, X86::ANDNPDrm, X86::PANDNrm },
3580 { X86::ANDNPSrr, X86::ANDNPDrr, X86::PANDNrr },
3581 { X86::ANDPSrm, X86::ANDPDrm, X86::PANDrm },
3582 { X86::ANDPSrr, X86::ANDPDrr, X86::PANDrr },
3583 { X86::ORPSrm, X86::ORPDrm, X86::PORrm },
3584 { X86::ORPSrr, X86::ORPDrr, X86::PORrr },
Jakob Stoklund Olesen8eb9c312010-03-31 00:40:13 +00003585 { X86::V_SET0PS, X86::V_SET0PD, X86::V_SET0PI },
Jakob Stoklund Olesendeeb9df2010-03-30 22:46:53 +00003586 { X86::XORPSrm, X86::XORPDrm, X86::PXORrm },
3587 { X86::XORPSrr, X86::XORPDrr, X86::PXORrr },
Jakob Stoklund Olesen9b3a5512010-03-29 23:24:21 +00003588};
Jakob Stoklund Olesen0ba75302010-03-25 17:25:00 +00003589
Jakob Stoklund Olesen9b3a5512010-03-29 23:24:21 +00003590// FIXME: Some shuffle and unpack instructions have equivalents in different
3591// domains, but they require a bit more work than just switching opcodes.
Jakob Stoklund Olesen0ba75302010-03-25 17:25:00 +00003592
Jakob Stoklund Olesen9b3a5512010-03-29 23:24:21 +00003593static const unsigned *lookup(unsigned opcode, unsigned domain) {
Jakob Stoklund Olesen0ba75302010-03-25 17:25:00 +00003594 for (unsigned i = 0, e = array_lengthof(ReplaceableInstrs); i != e; ++i)
Jakob Stoklund Olesen9b3a5512010-03-29 23:24:21 +00003595 if (ReplaceableInstrs[i][domain-1] == opcode)
3596 return ReplaceableInstrs[i];
3597 return 0;
3598}
3599
3600std::pair<uint16_t, uint16_t>
3601X86InstrInfo::GetSSEDomain(const MachineInstr *MI) const {
3602 uint16_t domain = (MI->getDesc().TSFlags >> X86II::SSEDomainShift) & 3;
Jakob Stoklund Olesendeeb9df2010-03-30 22:46:53 +00003603 return std::make_pair(domain,
3604 domain && lookup(MI->getOpcode(), domain) ? 0xe : 0);
Jakob Stoklund Olesen9b3a5512010-03-29 23:24:21 +00003605}
3606
3607void X86InstrInfo::SetSSEDomain(MachineInstr *MI, unsigned Domain) const {
3608 assert(Domain>0 && Domain<4 && "Invalid execution domain");
3609 uint16_t dom = (MI->getDesc().TSFlags >> X86II::SSEDomainShift) & 3;
3610 assert(dom && "Not an SSE instruction");
3611 const unsigned *table = lookup(MI->getOpcode(), dom);
3612 assert(table && "Cannot change domain");
3613 MI->setDesc(get(table[Domain-1]));
Jakob Stoklund Olesen0ba75302010-03-25 17:25:00 +00003614}
Chris Lattnera4083332010-04-26 23:37:21 +00003615
3616/// getNoopForMachoTarget - Return the noop instruction to use for a noop.
3617void X86InstrInfo::getNoopForMachoTarget(MCInst &NopInst) const {
3618 NopInst.setOpcode(X86::NOOP);
3619}
Dan Gohmanb4482cf2010-07-10 09:00:22 +00003620
3621namespace {
3622 /// CGBR - Create Global Base Reg pass. This initializes the PIC
3623 /// global base register for x86-32.
3624 struct CGBR : public MachineFunctionPass {
3625 static char ID;
3626 CGBR() : MachineFunctionPass(&ID) {}
3627
3628 virtual bool runOnMachineFunction(MachineFunction &MF) {
3629 const X86TargetMachine *TM =
3630 static_cast<const X86TargetMachine *>(&MF.getTarget());
3631
3632 assert(!TM->getSubtarget<X86Subtarget>().is64Bit() &&
3633 "X86-64 PIC uses RIP relative addressing");
3634
3635 // Only emit a global base reg in PIC mode.
3636 if (TM->getRelocationModel() != Reloc::PIC_)
3637 return false;
3638
3639 // Insert the set of GlobalBaseReg into the first MBB of the function
3640 MachineBasicBlock &FirstMBB = MF.front();
3641 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
3642 DebugLoc DL = FirstMBB.findDebugLoc(MBBI);
3643 MachineRegisterInfo &RegInfo = MF.getRegInfo();
3644 const X86InstrInfo *TII = TM->getInstrInfo();
3645
3646 unsigned PC;
3647 if (TM->getSubtarget<X86Subtarget>().isPICStyleGOT())
3648 PC = RegInfo.createVirtualRegister(X86::GR32RegisterClass);
3649 else
3650 PC = TII->getGlobalBaseReg(&MF);
3651
3652 // Operand of MovePCtoStack is completely ignored by asm printer. It's
3653 // only used in JIT code emission as displacement to pc.
3654 BuildMI(FirstMBB, MBBI, DL, TII->get(X86::MOVPC32r), PC).addImm(0);
3655
3656 // If we're using vanilla 'GOT' PIC style, we should use relative addressing
3657 // not to pc, but to _GLOBAL_OFFSET_TABLE_ external.
3658 if (TM->getSubtarget<X86Subtarget>().isPICStyleGOT()) {
3659 unsigned GlobalBaseReg = TII->getGlobalBaseReg(&MF);
3660 // Generate addl $__GLOBAL_OFFSET_TABLE_ + [.-piclabel], %some_register
3661 BuildMI(FirstMBB, MBBI, DL, TII->get(X86::ADD32ri), GlobalBaseReg)
3662 .addReg(PC).addExternalSymbol("_GLOBAL_OFFSET_TABLE_",
3663 X86II::MO_GOT_ABSOLUTE_ADDRESS);
3664 }
3665
3666 return true;
3667 }
3668
3669 virtual const char *getPassName() const {
3670 return "X86 PIC Global Base Reg Initialization";
3671 }
3672
3673 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
3674 AU.setPreservesCFG();
3675 MachineFunctionPass::getAnalysisUsage(AU);
3676 }
3677 };
3678}
3679
3680char CGBR::ID = 0;
3681FunctionPass*
3682llvm::createGlobalBaseRegPass() { return new CGBR(); }