blob: 5abdce45e27a90ec0b4c73fed6da73f4677d94f6 [file] [log] [blame]
Misha Brukman1a72c632002-11-22 22:42:50 +00001//===- X86InstrInfo.cpp - X86 Instruction Information -----------*- C++ -*-===//
Misha Brukmanc88330a2005-04-21 23:38:14 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanc88330a2005-04-21 23:38:14 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattnerd92fb002002-10-25 22:55:53 +00009//
Chris Lattnerb4d58d72003-01-14 22:00:31 +000010// This file contains the X86 implementation of the TargetInstrInfo class.
Chris Lattnerd92fb002002-10-25 22:55:53 +000011//
12//===----------------------------------------------------------------------===//
13
Chris Lattner27d24792002-10-29 21:05:24 +000014#include "X86InstrInfo.h"
Chris Lattner0d808742002-12-03 05:42:53 +000015#include "X86.h"
Chris Lattner05e2f382003-08-03 21:55:55 +000016#include "X86GenInstrInfo.inc"
Evan Chengc8c172e2006-05-30 21:45:53 +000017#include "X86InstrBuilder.h"
Owen Anderson6bb0c522008-01-04 23:57:37 +000018#include "X86MachineFunctionInfo.h"
Evan Chengc8c172e2006-05-30 21:45:53 +000019#include "X86Subtarget.h"
20#include "X86TargetMachine.h"
Dan Gohman906152a2009-01-05 17:59:02 +000021#include "llvm/DerivedTypes.h"
Owen Anderson53a52212009-07-13 04:09:18 +000022#include "llvm/LLVMContext.h"
Owen Andersone2f23a32007-09-07 04:06:50 +000023#include "llvm/ADT/STLExtras.h"
Dan Gohmancc78cdf2008-12-03 05:21:24 +000024#include "llvm/CodeGen/MachineConstantPool.h"
Owen Anderson6bb0c522008-01-04 23:57:37 +000025#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Chengc8c172e2006-05-30 21:45:53 +000026#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattnera10fff52007-12-31 04:13:23 +000027#include "llvm/CodeGen/MachineRegisterInfo.h"
Evan Cheng07fc1072006-12-01 21:52:41 +000028#include "llvm/CodeGen/LiveVariables.h"
David Greene70fdd572009-11-12 20:55:29 +000029#include "llvm/CodeGen/PseudoSourceValue.h"
Chris Lattner6a5e7062010-04-26 23:37:21 +000030#include "llvm/MC/MCInst.h"
Owen Anderson2a3be7b2008-01-07 01:35:02 +000031#include "llvm/Support/CommandLine.h"
David Greened589daf2010-01-05 01:29:29 +000032#include "llvm/Support/Debug.h"
Torok Edwin6dd27302009-07-08 18:01:40 +000033#include "llvm/Support/ErrorHandling.h"
34#include "llvm/Support/raw_ostream.h"
Evan Chenge95f3912007-09-25 01:57:46 +000035#include "llvm/Target/TargetOptions.h"
Chris Lattner7b26fce2009-08-22 20:48:53 +000036#include "llvm/MC/MCAsmInfo.h"
David Greene70fdd572009-11-12 20:55:29 +000037#include <limits>
38
Brian Gaeke960707c2003-11-11 22:41:34 +000039using namespace llvm;
40
Chris Lattnera6f074f2009-08-23 03:41:05 +000041static cl::opt<bool>
42NoFusing("disable-spill-fusing",
43 cl::desc("Disable fusing of spill code into instructions"));
44static cl::opt<bool>
45PrintFailedFusing("print-failed-fuse-candidates",
46 cl::desc("Print instructions that the allocator wants to"
47 " fuse, but the X86 backend currently can't"),
48 cl::Hidden);
49static cl::opt<bool>
50ReMatPICStubLoad("remat-pic-stub-load",
51 cl::desc("Re-materialize load from stub in PIC mode"),
52 cl::init(false), cl::Hidden);
Owen Anderson2a3be7b2008-01-07 01:35:02 +000053
Evan Chengc8c172e2006-05-30 21:45:53 +000054X86InstrInfo::X86InstrInfo(X86TargetMachine &tm)
Chris Lattner25568e42008-01-01 01:03:04 +000055 : TargetInstrInfoImpl(X86Insts, array_lengthof(X86Insts)),
Evan Cheng11b0a5d2006-09-08 06:48:29 +000056 TM(tm), RI(tm, *this) {
Owen Anderson2a3be7b2008-01-07 01:35:02 +000057 static const unsigned OpTbl2Addr[][2] = {
58 { X86::ADC32ri, X86::ADC32mi },
59 { X86::ADC32ri8, X86::ADC32mi8 },
60 { X86::ADC32rr, X86::ADC32mr },
61 { X86::ADC64ri32, X86::ADC64mi32 },
62 { X86::ADC64ri8, X86::ADC64mi8 },
63 { X86::ADC64rr, X86::ADC64mr },
64 { X86::ADD16ri, X86::ADD16mi },
65 { X86::ADD16ri8, X86::ADD16mi8 },
66 { X86::ADD16rr, X86::ADD16mr },
67 { X86::ADD32ri, X86::ADD32mi },
68 { X86::ADD32ri8, X86::ADD32mi8 },
69 { X86::ADD32rr, X86::ADD32mr },
70 { X86::ADD64ri32, X86::ADD64mi32 },
71 { X86::ADD64ri8, X86::ADD64mi8 },
72 { X86::ADD64rr, X86::ADD64mr },
73 { X86::ADD8ri, X86::ADD8mi },
74 { X86::ADD8rr, X86::ADD8mr },
75 { X86::AND16ri, X86::AND16mi },
76 { X86::AND16ri8, X86::AND16mi8 },
77 { X86::AND16rr, X86::AND16mr },
78 { X86::AND32ri, X86::AND32mi },
79 { X86::AND32ri8, X86::AND32mi8 },
80 { X86::AND32rr, X86::AND32mr },
81 { X86::AND64ri32, X86::AND64mi32 },
82 { X86::AND64ri8, X86::AND64mi8 },
83 { X86::AND64rr, X86::AND64mr },
84 { X86::AND8ri, X86::AND8mi },
85 { X86::AND8rr, X86::AND8mr },
86 { X86::DEC16r, X86::DEC16m },
87 { X86::DEC32r, X86::DEC32m },
88 { X86::DEC64_16r, X86::DEC64_16m },
89 { X86::DEC64_32r, X86::DEC64_32m },
90 { X86::DEC64r, X86::DEC64m },
91 { X86::DEC8r, X86::DEC8m },
92 { X86::INC16r, X86::INC16m },
93 { X86::INC32r, X86::INC32m },
94 { X86::INC64_16r, X86::INC64_16m },
95 { X86::INC64_32r, X86::INC64_32m },
96 { X86::INC64r, X86::INC64m },
97 { X86::INC8r, X86::INC8m },
98 { X86::NEG16r, X86::NEG16m },
99 { X86::NEG32r, X86::NEG32m },
100 { X86::NEG64r, X86::NEG64m },
101 { X86::NEG8r, X86::NEG8m },
102 { X86::NOT16r, X86::NOT16m },
103 { X86::NOT32r, X86::NOT32m },
104 { X86::NOT64r, X86::NOT64m },
105 { X86::NOT8r, X86::NOT8m },
106 { X86::OR16ri, X86::OR16mi },
107 { X86::OR16ri8, X86::OR16mi8 },
108 { X86::OR16rr, X86::OR16mr },
109 { X86::OR32ri, X86::OR32mi },
110 { X86::OR32ri8, X86::OR32mi8 },
111 { X86::OR32rr, X86::OR32mr },
112 { X86::OR64ri32, X86::OR64mi32 },
113 { X86::OR64ri8, X86::OR64mi8 },
114 { X86::OR64rr, X86::OR64mr },
115 { X86::OR8ri, X86::OR8mi },
116 { X86::OR8rr, X86::OR8mr },
117 { X86::ROL16r1, X86::ROL16m1 },
118 { X86::ROL16rCL, X86::ROL16mCL },
119 { X86::ROL16ri, X86::ROL16mi },
120 { X86::ROL32r1, X86::ROL32m1 },
121 { X86::ROL32rCL, X86::ROL32mCL },
122 { X86::ROL32ri, X86::ROL32mi },
123 { X86::ROL64r1, X86::ROL64m1 },
124 { X86::ROL64rCL, X86::ROL64mCL },
125 { X86::ROL64ri, X86::ROL64mi },
126 { X86::ROL8r1, X86::ROL8m1 },
127 { X86::ROL8rCL, X86::ROL8mCL },
128 { X86::ROL8ri, X86::ROL8mi },
129 { X86::ROR16r1, X86::ROR16m1 },
130 { X86::ROR16rCL, X86::ROR16mCL },
131 { X86::ROR16ri, X86::ROR16mi },
132 { X86::ROR32r1, X86::ROR32m1 },
133 { X86::ROR32rCL, X86::ROR32mCL },
134 { X86::ROR32ri, X86::ROR32mi },
135 { X86::ROR64r1, X86::ROR64m1 },
136 { X86::ROR64rCL, X86::ROR64mCL },
137 { X86::ROR64ri, X86::ROR64mi },
138 { X86::ROR8r1, X86::ROR8m1 },
139 { X86::ROR8rCL, X86::ROR8mCL },
140 { X86::ROR8ri, X86::ROR8mi },
141 { X86::SAR16r1, X86::SAR16m1 },
142 { X86::SAR16rCL, X86::SAR16mCL },
143 { X86::SAR16ri, X86::SAR16mi },
144 { X86::SAR32r1, X86::SAR32m1 },
145 { X86::SAR32rCL, X86::SAR32mCL },
146 { X86::SAR32ri, X86::SAR32mi },
147 { X86::SAR64r1, X86::SAR64m1 },
148 { X86::SAR64rCL, X86::SAR64mCL },
149 { X86::SAR64ri, X86::SAR64mi },
150 { X86::SAR8r1, X86::SAR8m1 },
151 { X86::SAR8rCL, X86::SAR8mCL },
152 { X86::SAR8ri, X86::SAR8mi },
153 { X86::SBB32ri, X86::SBB32mi },
154 { X86::SBB32ri8, X86::SBB32mi8 },
155 { X86::SBB32rr, X86::SBB32mr },
156 { X86::SBB64ri32, X86::SBB64mi32 },
157 { X86::SBB64ri8, X86::SBB64mi8 },
158 { X86::SBB64rr, X86::SBB64mr },
Owen Anderson2a3be7b2008-01-07 01:35:02 +0000159 { X86::SHL16rCL, X86::SHL16mCL },
160 { X86::SHL16ri, X86::SHL16mi },
Owen Anderson2a3be7b2008-01-07 01:35:02 +0000161 { X86::SHL32rCL, X86::SHL32mCL },
162 { X86::SHL32ri, X86::SHL32mi },
Owen Anderson2a3be7b2008-01-07 01:35:02 +0000163 { X86::SHL64rCL, X86::SHL64mCL },
164 { X86::SHL64ri, X86::SHL64mi },
Owen Anderson2a3be7b2008-01-07 01:35:02 +0000165 { X86::SHL8rCL, X86::SHL8mCL },
166 { X86::SHL8ri, X86::SHL8mi },
167 { X86::SHLD16rrCL, X86::SHLD16mrCL },
168 { X86::SHLD16rri8, X86::SHLD16mri8 },
169 { X86::SHLD32rrCL, X86::SHLD32mrCL },
170 { X86::SHLD32rri8, X86::SHLD32mri8 },
171 { X86::SHLD64rrCL, X86::SHLD64mrCL },
172 { X86::SHLD64rri8, X86::SHLD64mri8 },
173 { X86::SHR16r1, X86::SHR16m1 },
174 { X86::SHR16rCL, X86::SHR16mCL },
175 { X86::SHR16ri, X86::SHR16mi },
176 { X86::SHR32r1, X86::SHR32m1 },
177 { X86::SHR32rCL, X86::SHR32mCL },
178 { X86::SHR32ri, X86::SHR32mi },
179 { X86::SHR64r1, X86::SHR64m1 },
180 { X86::SHR64rCL, X86::SHR64mCL },
181 { X86::SHR64ri, X86::SHR64mi },
182 { X86::SHR8r1, X86::SHR8m1 },
183 { X86::SHR8rCL, X86::SHR8mCL },
184 { X86::SHR8ri, X86::SHR8mi },
185 { X86::SHRD16rrCL, X86::SHRD16mrCL },
186 { X86::SHRD16rri8, X86::SHRD16mri8 },
187 { X86::SHRD32rrCL, X86::SHRD32mrCL },
188 { X86::SHRD32rri8, X86::SHRD32mri8 },
189 { X86::SHRD64rrCL, X86::SHRD64mrCL },
190 { X86::SHRD64rri8, X86::SHRD64mri8 },
191 { X86::SUB16ri, X86::SUB16mi },
192 { X86::SUB16ri8, X86::SUB16mi8 },
193 { X86::SUB16rr, X86::SUB16mr },
194 { X86::SUB32ri, X86::SUB32mi },
195 { X86::SUB32ri8, X86::SUB32mi8 },
196 { X86::SUB32rr, X86::SUB32mr },
197 { X86::SUB64ri32, X86::SUB64mi32 },
198 { X86::SUB64ri8, X86::SUB64mi8 },
199 { X86::SUB64rr, X86::SUB64mr },
200 { X86::SUB8ri, X86::SUB8mi },
201 { X86::SUB8rr, X86::SUB8mr },
202 { X86::XOR16ri, X86::XOR16mi },
203 { X86::XOR16ri8, X86::XOR16mi8 },
204 { X86::XOR16rr, X86::XOR16mr },
205 { X86::XOR32ri, X86::XOR32mi },
206 { X86::XOR32ri8, X86::XOR32mi8 },
207 { X86::XOR32rr, X86::XOR32mr },
208 { X86::XOR64ri32, X86::XOR64mi32 },
209 { X86::XOR64ri8, X86::XOR64mi8 },
210 { X86::XOR64rr, X86::XOR64mr },
211 { X86::XOR8ri, X86::XOR8mi },
212 { X86::XOR8rr, X86::XOR8mr }
213 };
214
215 for (unsigned i = 0, e = array_lengthof(OpTbl2Addr); i != e; ++i) {
216 unsigned RegOp = OpTbl2Addr[i][0];
Daniel Dunbar8f21f9c2010-10-08 02:07:32 +0000217 unsigned MemOp = OpTbl2Addr[i][1];
218 if (!RegOp2MemOpTable2Addr.insert(std::make_pair(RegOp,
219 std::make_pair(MemOp,0))).second)
220 assert(false && "Duplicated entries?");
Evan Cheng9e0c7f22009-07-15 06:10:07 +0000221 // Index 0, folded load and store, no alignment requirement.
222 unsigned AuxInfo = 0 | (1 << 4) | (1 << 5);
Daniel Dunbar8f21f9c2010-10-08 02:07:32 +0000223 if (!MemOp2RegOpTable.insert(std::make_pair(MemOp,
224 std::make_pair(RegOp,
225 AuxInfo))).second)
226 assert(false && "Duplicated entries in unfolding maps?");
Owen Anderson2a3be7b2008-01-07 01:35:02 +0000227 }
228
229 // If the third value is 1, then it's folding either a load or a store.
Evan Cheng9e0c7f22009-07-15 06:10:07 +0000230 static const unsigned OpTbl0[][4] = {
231 { X86::BT16ri8, X86::BT16mi8, 1, 0 },
232 { X86::BT32ri8, X86::BT32mi8, 1, 0 },
233 { X86::BT64ri8, X86::BT64mi8, 1, 0 },
234 { X86::CALL32r, X86::CALL32m, 1, 0 },
235 { X86::CALL64r, X86::CALL64m, 1, 0 },
Anton Korobeynikovcd78af62010-08-17 21:06:01 +0000236 { X86::WINCALL64r, X86::WINCALL64m, 1, 0 },
Evan Cheng9e0c7f22009-07-15 06:10:07 +0000237 { X86::CMP16ri, X86::CMP16mi, 1, 0 },
238 { X86::CMP16ri8, X86::CMP16mi8, 1, 0 },
239 { X86::CMP16rr, X86::CMP16mr, 1, 0 },
240 { X86::CMP32ri, X86::CMP32mi, 1, 0 },
241 { X86::CMP32ri8, X86::CMP32mi8, 1, 0 },
242 { X86::CMP32rr, X86::CMP32mr, 1, 0 },
243 { X86::CMP64ri32, X86::CMP64mi32, 1, 0 },
244 { X86::CMP64ri8, X86::CMP64mi8, 1, 0 },
245 { X86::CMP64rr, X86::CMP64mr, 1, 0 },
246 { X86::CMP8ri, X86::CMP8mi, 1, 0 },
247 { X86::CMP8rr, X86::CMP8mr, 1, 0 },
248 { X86::DIV16r, X86::DIV16m, 1, 0 },
249 { X86::DIV32r, X86::DIV32m, 1, 0 },
250 { X86::DIV64r, X86::DIV64m, 1, 0 },
251 { X86::DIV8r, X86::DIV8m, 1, 0 },
252 { X86::EXTRACTPSrr, X86::EXTRACTPSmr, 0, 16 },
Daniel Dunbar5b2a4112010-10-08 02:07:29 +0000253 { X86::FsMOVAPDrr, X86::MOVSDmr, 0, 0 },
254 { X86::FsMOVAPSrr, X86::MOVSSmr, 0, 0 },
Evan Cheng9e0c7f22009-07-15 06:10:07 +0000255 { X86::IDIV16r, X86::IDIV16m, 1, 0 },
256 { X86::IDIV32r, X86::IDIV32m, 1, 0 },
257 { X86::IDIV64r, X86::IDIV64m, 1, 0 },
258 { X86::IDIV8r, X86::IDIV8m, 1, 0 },
259 { X86::IMUL16r, X86::IMUL16m, 1, 0 },
260 { X86::IMUL32r, X86::IMUL32m, 1, 0 },
261 { X86::IMUL64r, X86::IMUL64m, 1, 0 },
262 { X86::IMUL8r, X86::IMUL8m, 1, 0 },
263 { X86::JMP32r, X86::JMP32m, 1, 0 },
264 { X86::JMP64r, X86::JMP64m, 1, 0 },
265 { X86::MOV16ri, X86::MOV16mi, 0, 0 },
266 { X86::MOV16rr, X86::MOV16mr, 0, 0 },
267 { X86::MOV32ri, X86::MOV32mi, 0, 0 },
268 { X86::MOV32rr, X86::MOV32mr, 0, 0 },
Evan Chengd703df62010-03-14 03:48:46 +0000269 { X86::MOV32rr_TC, X86::MOV32mr_TC, 0, 0 },
Evan Cheng9e0c7f22009-07-15 06:10:07 +0000270 { X86::MOV64ri32, X86::MOV64mi32, 0, 0 },
271 { X86::MOV64rr, X86::MOV64mr, 0, 0 },
272 { X86::MOV8ri, X86::MOV8mi, 0, 0 },
273 { X86::MOV8rr, X86::MOV8mr, 0, 0 },
274 { X86::MOV8rr_NOREX, X86::MOV8mr_NOREX, 0, 0 },
275 { X86::MOVAPDrr, X86::MOVAPDmr, 0, 16 },
276 { X86::MOVAPSrr, X86::MOVAPSmr, 0, 16 },
277 { X86::MOVDQArr, X86::MOVDQAmr, 0, 16 },
278 { X86::MOVPDI2DIrr, X86::MOVPDI2DImr, 0, 0 },
279 { X86::MOVPQIto64rr,X86::MOVPQI2QImr, 0, 0 },
Evan Cheng9e0c7f22009-07-15 06:10:07 +0000280 { X86::MOVSDto64rr, X86::MOVSDto64mr, 0, 0 },
281 { X86::MOVSS2DIrr, X86::MOVSS2DImr, 0, 0 },
Evan Cheng9e0c7f22009-07-15 06:10:07 +0000282 { X86::MOVUPDrr, X86::MOVUPDmr, 0, 0 },
283 { X86::MOVUPSrr, X86::MOVUPSmr, 0, 0 },
284 { X86::MUL16r, X86::MUL16m, 1, 0 },
285 { X86::MUL32r, X86::MUL32m, 1, 0 },
286 { X86::MUL64r, X86::MUL64m, 1, 0 },
287 { X86::MUL8r, X86::MUL8m, 1, 0 },
288 { X86::SETAEr, X86::SETAEm, 0, 0 },
289 { X86::SETAr, X86::SETAm, 0, 0 },
290 { X86::SETBEr, X86::SETBEm, 0, 0 },
291 { X86::SETBr, X86::SETBm, 0, 0 },
292 { X86::SETEr, X86::SETEm, 0, 0 },
293 { X86::SETGEr, X86::SETGEm, 0, 0 },
294 { X86::SETGr, X86::SETGm, 0, 0 },
295 { X86::SETLEr, X86::SETLEm, 0, 0 },
296 { X86::SETLr, X86::SETLm, 0, 0 },
297 { X86::SETNEr, X86::SETNEm, 0, 0 },
298 { X86::SETNOr, X86::SETNOm, 0, 0 },
299 { X86::SETNPr, X86::SETNPm, 0, 0 },
300 { X86::SETNSr, X86::SETNSm, 0, 0 },
301 { X86::SETOr, X86::SETOm, 0, 0 },
302 { X86::SETPr, X86::SETPm, 0, 0 },
303 { X86::SETSr, X86::SETSm, 0, 0 },
304 { X86::TAILJMPr, X86::TAILJMPm, 1, 0 },
Evan Chengd703df62010-03-14 03:48:46 +0000305 { X86::TAILJMPr64, X86::TAILJMPm64, 1, 0 },
Evan Cheng9e0c7f22009-07-15 06:10:07 +0000306 { X86::TEST16ri, X86::TEST16mi, 1, 0 },
307 { X86::TEST32ri, X86::TEST32mi, 1, 0 },
308 { X86::TEST64ri32, X86::TEST64mi32, 1, 0 },
309 { X86::TEST8ri, X86::TEST8mi, 1, 0 }
Owen Anderson2a3be7b2008-01-07 01:35:02 +0000310 };
311
312 for (unsigned i = 0, e = array_lengthof(OpTbl0); i != e; ++i) {
Daniel Dunbar5b2a4112010-10-08 02:07:29 +0000313 unsigned RegOp = OpTbl0[i][0];
314 unsigned MemOp = OpTbl0[i][1];
315 unsigned Align = OpTbl0[i][3];
Chris Lattnere2245542010-10-08 00:03:02 +0000316 assert(!RegOp2MemOpTable0.count(RegOp) && "Duplicated entries?");
Daniel Dunbar5b2a4112010-10-08 02:07:29 +0000317 RegOp2MemOpTable0[RegOp] = std::make_pair(MemOp,Align);
318 unsigned FoldedLoad = OpTbl0[i][2];
Owen Anderson2a3be7b2008-01-07 01:35:02 +0000319 // Index 0, folded load or store.
320 unsigned AuxInfo = 0 | (FoldedLoad << 4) | ((FoldedLoad^1) << 5);
Daniel Dunbar5b2a4112010-10-08 02:07:29 +0000321 if (RegOp != X86::FsMOVAPDrr && RegOp != X86::FsMOVAPSrr) {
322 assert(!MemOp2RegOpTable.count(MemOp) && "Duplicated entries?");
323 MemOp2RegOpTable[MemOp] = std::make_pair(RegOp, AuxInfo);
324 }
Owen Anderson2a3be7b2008-01-07 01:35:02 +0000325 }
326
Evan Cheng9e0c7f22009-07-15 06:10:07 +0000327 static const unsigned OpTbl1[][3] = {
328 { X86::CMP16rr, X86::CMP16rm, 0 },
329 { X86::CMP32rr, X86::CMP32rm, 0 },
330 { X86::CMP64rr, X86::CMP64rm, 0 },
331 { X86::CMP8rr, X86::CMP8rm, 0 },
332 { X86::CVTSD2SSrr, X86::CVTSD2SSrm, 0 },
333 { X86::CVTSI2SD64rr, X86::CVTSI2SD64rm, 0 },
334 { X86::CVTSI2SDrr, X86::CVTSI2SDrm, 0 },
335 { X86::CVTSI2SS64rr, X86::CVTSI2SS64rm, 0 },
336 { X86::CVTSI2SSrr, X86::CVTSI2SSrm, 0 },
337 { X86::CVTSS2SDrr, X86::CVTSS2SDrm, 0 },
338 { X86::CVTTSD2SI64rr, X86::CVTTSD2SI64rm, 0 },
339 { X86::CVTTSD2SIrr, X86::CVTTSD2SIrm, 0 },
340 { X86::CVTTSS2SI64rr, X86::CVTTSS2SI64rm, 0 },
341 { X86::CVTTSS2SIrr, X86::CVTTSS2SIrm, 0 },
Daniel Dunbar5b2a4112010-10-08 02:07:29 +0000342 { X86::FsMOVAPDrr, X86::MOVSDrm, 0 },
343 { X86::FsMOVAPSrr, X86::MOVSSrm, 0 },
Evan Cheng9e0c7f22009-07-15 06:10:07 +0000344 { X86::IMUL16rri, X86::IMUL16rmi, 0 },
345 { X86::IMUL16rri8, X86::IMUL16rmi8, 0 },
346 { X86::IMUL32rri, X86::IMUL32rmi, 0 },
347 { X86::IMUL32rri8, X86::IMUL32rmi8, 0 },
348 { X86::IMUL64rri32, X86::IMUL64rmi32, 0 },
349 { X86::IMUL64rri8, X86::IMUL64rmi8, 0 },
350 { X86::Int_CMPSDrr, X86::Int_CMPSDrm, 0 },
351 { X86::Int_CMPSSrr, X86::Int_CMPSSrm, 0 },
352 { X86::Int_COMISDrr, X86::Int_COMISDrm, 0 },
353 { X86::Int_COMISSrr, X86::Int_COMISSrm, 0 },
354 { X86::Int_CVTDQ2PDrr, X86::Int_CVTDQ2PDrm, 16 },
355 { X86::Int_CVTDQ2PSrr, X86::Int_CVTDQ2PSrm, 16 },
356 { X86::Int_CVTPD2DQrr, X86::Int_CVTPD2DQrm, 16 },
357 { X86::Int_CVTPD2PSrr, X86::Int_CVTPD2PSrm, 16 },
358 { X86::Int_CVTPS2DQrr, X86::Int_CVTPS2DQrm, 16 },
359 { X86::Int_CVTPS2PDrr, X86::Int_CVTPS2PDrm, 0 },
Chris Lattneref1c2fc2010-09-29 02:24:57 +0000360 { X86::CVTSD2SI64rr, X86::CVTSD2SI64rm, 0 },
361 { X86::CVTSD2SIrr, X86::CVTSD2SIrm, 0 },
Evan Cheng9e0c7f22009-07-15 06:10:07 +0000362 { X86::Int_CVTSD2SSrr, X86::Int_CVTSD2SSrm, 0 },
363 { X86::Int_CVTSI2SD64rr,X86::Int_CVTSI2SD64rm, 0 },
364 { X86::Int_CVTSI2SDrr, X86::Int_CVTSI2SDrm, 0 },
365 { X86::Int_CVTSI2SS64rr,X86::Int_CVTSI2SS64rm, 0 },
366 { X86::Int_CVTSI2SSrr, X86::Int_CVTSI2SSrm, 0 },
367 { X86::Int_CVTSS2SDrr, X86::Int_CVTSS2SDrm, 0 },
368 { X86::Int_CVTSS2SI64rr,X86::Int_CVTSS2SI64rm, 0 },
369 { X86::Int_CVTSS2SIrr, X86::Int_CVTSS2SIrm, 0 },
Chris Lattnerff3a3932010-09-29 02:36:32 +0000370 { X86::CVTTPD2DQrr, X86::CVTTPD2DQrm, 16 },
371 { X86::CVTTPS2DQrr, X86::CVTTPS2DQrm, 16 },
Evan Cheng9e0c7f22009-07-15 06:10:07 +0000372 { X86::Int_CVTTSD2SI64rr,X86::Int_CVTTSD2SI64rm, 0 },
373 { X86::Int_CVTTSD2SIrr, X86::Int_CVTTSD2SIrm, 0 },
374 { X86::Int_CVTTSS2SI64rr,X86::Int_CVTTSS2SI64rm, 0 },
375 { X86::Int_CVTTSS2SIrr, X86::Int_CVTTSS2SIrm, 0 },
376 { X86::Int_UCOMISDrr, X86::Int_UCOMISDrm, 0 },
377 { X86::Int_UCOMISSrr, X86::Int_UCOMISSrm, 0 },
378 { X86::MOV16rr, X86::MOV16rm, 0 },
379 { X86::MOV32rr, X86::MOV32rm, 0 },
Evan Chengd703df62010-03-14 03:48:46 +0000380 { X86::MOV32rr_TC, X86::MOV32rm_TC, 0 },
Evan Cheng9e0c7f22009-07-15 06:10:07 +0000381 { X86::MOV64rr, X86::MOV64rm, 0 },
382 { X86::MOV64toPQIrr, X86::MOVQI2PQIrm, 0 },
383 { X86::MOV64toSDrr, X86::MOV64toSDrm, 0 },
384 { X86::MOV8rr, X86::MOV8rm, 0 },
385 { X86::MOVAPDrr, X86::MOVAPDrm, 16 },
386 { X86::MOVAPSrr, X86::MOVAPSrm, 16 },
387 { X86::MOVDDUPrr, X86::MOVDDUPrm, 0 },
388 { X86::MOVDI2PDIrr, X86::MOVDI2PDIrm, 0 },
389 { X86::MOVDI2SSrr, X86::MOVDI2SSrm, 0 },
390 { X86::MOVDQArr, X86::MOVDQArm, 16 },
Evan Cheng9e0c7f22009-07-15 06:10:07 +0000391 { X86::MOVSHDUPrr, X86::MOVSHDUPrm, 16 },
392 { X86::MOVSLDUPrr, X86::MOVSLDUPrm, 16 },
Evan Cheng9e0c7f22009-07-15 06:10:07 +0000393 { X86::MOVSX16rr8, X86::MOVSX16rm8, 0 },
394 { X86::MOVSX32rr16, X86::MOVSX32rm16, 0 },
395 { X86::MOVSX32rr8, X86::MOVSX32rm8, 0 },
396 { X86::MOVSX64rr16, X86::MOVSX64rm16, 0 },
397 { X86::MOVSX64rr32, X86::MOVSX64rm32, 0 },
398 { X86::MOVSX64rr8, X86::MOVSX64rm8, 0 },
399 { X86::MOVUPDrr, X86::MOVUPDrm, 16 },
Evan Cheng5d30f7c2010-01-21 00:55:14 +0000400 { X86::MOVUPSrr, X86::MOVUPSrm, 0 },
Evan Cheng9e0c7f22009-07-15 06:10:07 +0000401 { X86::MOVZDI2PDIrr, X86::MOVZDI2PDIrm, 0 },
402 { X86::MOVZQI2PQIrr, X86::MOVZQI2PQIrm, 0 },
403 { X86::MOVZPQILo2PQIrr, X86::MOVZPQILo2PQIrm, 16 },
404 { X86::MOVZX16rr8, X86::MOVZX16rm8, 0 },
405 { X86::MOVZX32rr16, X86::MOVZX32rm16, 0 },
406 { X86::MOVZX32_NOREXrr8, X86::MOVZX32_NOREXrm8, 0 },
407 { X86::MOVZX32rr8, X86::MOVZX32rm8, 0 },
408 { X86::MOVZX64rr16, X86::MOVZX64rm16, 0 },
409 { X86::MOVZX64rr32, X86::MOVZX64rm32, 0 },
410 { X86::MOVZX64rr8, X86::MOVZX64rm8, 0 },
411 { X86::PSHUFDri, X86::PSHUFDmi, 16 },
412 { X86::PSHUFHWri, X86::PSHUFHWmi, 16 },
413 { X86::PSHUFLWri, X86::PSHUFLWmi, 16 },
414 { X86::RCPPSr, X86::RCPPSm, 16 },
415 { X86::RCPPSr_Int, X86::RCPPSm_Int, 16 },
416 { X86::RSQRTPSr, X86::RSQRTPSm, 16 },
417 { X86::RSQRTPSr_Int, X86::RSQRTPSm_Int, 16 },
418 { X86::RSQRTSSr, X86::RSQRTSSm, 0 },
419 { X86::RSQRTSSr_Int, X86::RSQRTSSm_Int, 0 },
420 { X86::SQRTPDr, X86::SQRTPDm, 16 },
421 { X86::SQRTPDr_Int, X86::SQRTPDm_Int, 16 },
422 { X86::SQRTPSr, X86::SQRTPSm, 16 },
423 { X86::SQRTPSr_Int, X86::SQRTPSm_Int, 16 },
424 { X86::SQRTSDr, X86::SQRTSDm, 0 },
425 { X86::SQRTSDr_Int, X86::SQRTSDm_Int, 0 },
426 { X86::SQRTSSr, X86::SQRTSSm, 0 },
427 { X86::SQRTSSr_Int, X86::SQRTSSm_Int, 0 },
428 { X86::TEST16rr, X86::TEST16rm, 0 },
429 { X86::TEST32rr, X86::TEST32rm, 0 },
430 { X86::TEST64rr, X86::TEST64rm, 0 },
431 { X86::TEST8rr, X86::TEST8rm, 0 },
Owen Anderson2a3be7b2008-01-07 01:35:02 +0000432 // FIXME: TEST*rr EAX,EAX ---> CMP [mem], 0
Evan Cheng9e0c7f22009-07-15 06:10:07 +0000433 { X86::UCOMISDrr, X86::UCOMISDrm, 0 },
434 { X86::UCOMISSrr, X86::UCOMISSrm, 0 }
Owen Anderson2a3be7b2008-01-07 01:35:02 +0000435 };
436
437 for (unsigned i = 0, e = array_lengthof(OpTbl1); i != e; ++i) {
438 unsigned RegOp = OpTbl1[i][0];
Daniel Dunbar5b2a4112010-10-08 02:07:29 +0000439 unsigned MemOp = OpTbl1[i][1];
Evan Cheng9e0c7f22009-07-15 06:10:07 +0000440 unsigned Align = OpTbl1[i][2];
Chris Lattner0921bfd2010-10-07 23:57:02 +0000441 assert(!RegOp2MemOpTable1.count(RegOp) && "Duplicate entries");
Daniel Dunbar5b2a4112010-10-08 02:07:29 +0000442 RegOp2MemOpTable1[RegOp] = std::make_pair(MemOp,Align);
Chris Lattner0921bfd2010-10-07 23:57:02 +0000443
Evan Cheng9e0c7f22009-07-15 06:10:07 +0000444 // Index 1, folded load
445 unsigned AuxInfo = 1 | (1 << 4);
Daniel Dunbar5b2a4112010-10-08 02:07:29 +0000446 if (RegOp != X86::FsMOVAPDrr && RegOp != X86::FsMOVAPSrr) {
447 assert(!MemOp2RegOpTable.count(MemOp) && "Duplicate entries");
448 MemOp2RegOpTable[MemOp] = std::make_pair(RegOp, AuxInfo);
449 }
Owen Anderson2a3be7b2008-01-07 01:35:02 +0000450 }
451
Evan Cheng9e0c7f22009-07-15 06:10:07 +0000452 static const unsigned OpTbl2[][3] = {
453 { X86::ADC32rr, X86::ADC32rm, 0 },
454 { X86::ADC64rr, X86::ADC64rm, 0 },
455 { X86::ADD16rr, X86::ADD16rm, 0 },
456 { X86::ADD32rr, X86::ADD32rm, 0 },
457 { X86::ADD64rr, X86::ADD64rm, 0 },
458 { X86::ADD8rr, X86::ADD8rm, 0 },
459 { X86::ADDPDrr, X86::ADDPDrm, 16 },
460 { X86::ADDPSrr, X86::ADDPSrm, 16 },
461 { X86::ADDSDrr, X86::ADDSDrm, 0 },
462 { X86::ADDSSrr, X86::ADDSSrm, 0 },
463 { X86::ADDSUBPDrr, X86::ADDSUBPDrm, 16 },
464 { X86::ADDSUBPSrr, X86::ADDSUBPSrm, 16 },
465 { X86::AND16rr, X86::AND16rm, 0 },
466 { X86::AND32rr, X86::AND32rm, 0 },
467 { X86::AND64rr, X86::AND64rm, 0 },
468 { X86::AND8rr, X86::AND8rm, 0 },
469 { X86::ANDNPDrr, X86::ANDNPDrm, 16 },
470 { X86::ANDNPSrr, X86::ANDNPSrm, 16 },
471 { X86::ANDPDrr, X86::ANDPDrm, 16 },
472 { X86::ANDPSrr, X86::ANDPSrm, 16 },
473 { X86::CMOVA16rr, X86::CMOVA16rm, 0 },
474 { X86::CMOVA32rr, X86::CMOVA32rm, 0 },
475 { X86::CMOVA64rr, X86::CMOVA64rm, 0 },
476 { X86::CMOVAE16rr, X86::CMOVAE16rm, 0 },
477 { X86::CMOVAE32rr, X86::CMOVAE32rm, 0 },
478 { X86::CMOVAE64rr, X86::CMOVAE64rm, 0 },
479 { X86::CMOVB16rr, X86::CMOVB16rm, 0 },
480 { X86::CMOVB32rr, X86::CMOVB32rm, 0 },
481 { X86::CMOVB64rr, X86::CMOVB64rm, 0 },
Chris Lattner1a1c6002010-10-05 23:00:14 +0000482 { X86::CMOVBE16rr, X86::CMOVBE16rm, 0 },
483 { X86::CMOVBE32rr, X86::CMOVBE32rm, 0 },
484 { X86::CMOVBE64rr, X86::CMOVBE64rm, 0 },
Evan Cheng9e0c7f22009-07-15 06:10:07 +0000485 { X86::CMOVE16rr, X86::CMOVE16rm, 0 },
486 { X86::CMOVE32rr, X86::CMOVE32rm, 0 },
487 { X86::CMOVE64rr, X86::CMOVE64rm, 0 },
488 { X86::CMOVG16rr, X86::CMOVG16rm, 0 },
489 { X86::CMOVG32rr, X86::CMOVG32rm, 0 },
490 { X86::CMOVG64rr, X86::CMOVG64rm, 0 },
491 { X86::CMOVGE16rr, X86::CMOVGE16rm, 0 },
492 { X86::CMOVGE32rr, X86::CMOVGE32rm, 0 },
493 { X86::CMOVGE64rr, X86::CMOVGE64rm, 0 },
494 { X86::CMOVL16rr, X86::CMOVL16rm, 0 },
495 { X86::CMOVL32rr, X86::CMOVL32rm, 0 },
496 { X86::CMOVL64rr, X86::CMOVL64rm, 0 },
497 { X86::CMOVLE16rr, X86::CMOVLE16rm, 0 },
498 { X86::CMOVLE32rr, X86::CMOVLE32rm, 0 },
499 { X86::CMOVLE64rr, X86::CMOVLE64rm, 0 },
500 { X86::CMOVNE16rr, X86::CMOVNE16rm, 0 },
501 { X86::CMOVNE32rr, X86::CMOVNE32rm, 0 },
502 { X86::CMOVNE64rr, X86::CMOVNE64rm, 0 },
503 { X86::CMOVNO16rr, X86::CMOVNO16rm, 0 },
504 { X86::CMOVNO32rr, X86::CMOVNO32rm, 0 },
505 { X86::CMOVNO64rr, X86::CMOVNO64rm, 0 },
506 { X86::CMOVNP16rr, X86::CMOVNP16rm, 0 },
507 { X86::CMOVNP32rr, X86::CMOVNP32rm, 0 },
508 { X86::CMOVNP64rr, X86::CMOVNP64rm, 0 },
509 { X86::CMOVNS16rr, X86::CMOVNS16rm, 0 },
510 { X86::CMOVNS32rr, X86::CMOVNS32rm, 0 },
511 { X86::CMOVNS64rr, X86::CMOVNS64rm, 0 },
512 { X86::CMOVO16rr, X86::CMOVO16rm, 0 },
513 { X86::CMOVO32rr, X86::CMOVO32rm, 0 },
514 { X86::CMOVO64rr, X86::CMOVO64rm, 0 },
515 { X86::CMOVP16rr, X86::CMOVP16rm, 0 },
516 { X86::CMOVP32rr, X86::CMOVP32rm, 0 },
517 { X86::CMOVP64rr, X86::CMOVP64rm, 0 },
518 { X86::CMOVS16rr, X86::CMOVS16rm, 0 },
519 { X86::CMOVS32rr, X86::CMOVS32rm, 0 },
520 { X86::CMOVS64rr, X86::CMOVS64rm, 0 },
521 { X86::CMPPDrri, X86::CMPPDrmi, 16 },
522 { X86::CMPPSrri, X86::CMPPSrmi, 16 },
523 { X86::CMPSDrr, X86::CMPSDrm, 0 },
524 { X86::CMPSSrr, X86::CMPSSrm, 0 },
525 { X86::DIVPDrr, X86::DIVPDrm, 16 },
526 { X86::DIVPSrr, X86::DIVPSrm, 16 },
527 { X86::DIVSDrr, X86::DIVSDrm, 0 },
528 { X86::DIVSSrr, X86::DIVSSrm, 0 },
529 { X86::FsANDNPDrr, X86::FsANDNPDrm, 16 },
530 { X86::FsANDNPSrr, X86::FsANDNPSrm, 16 },
531 { X86::FsANDPDrr, X86::FsANDPDrm, 16 },
532 { X86::FsANDPSrr, X86::FsANDPSrm, 16 },
533 { X86::FsORPDrr, X86::FsORPDrm, 16 },
534 { X86::FsORPSrr, X86::FsORPSrm, 16 },
535 { X86::FsXORPDrr, X86::FsXORPDrm, 16 },
536 { X86::FsXORPSrr, X86::FsXORPSrm, 16 },
537 { X86::HADDPDrr, X86::HADDPDrm, 16 },
538 { X86::HADDPSrr, X86::HADDPSrm, 16 },
539 { X86::HSUBPDrr, X86::HSUBPDrm, 16 },
540 { X86::HSUBPSrr, X86::HSUBPSrm, 16 },
541 { X86::IMUL16rr, X86::IMUL16rm, 0 },
542 { X86::IMUL32rr, X86::IMUL32rm, 0 },
543 { X86::IMUL64rr, X86::IMUL64rm, 0 },
544 { X86::MAXPDrr, X86::MAXPDrm, 16 },
545 { X86::MAXPDrr_Int, X86::MAXPDrm_Int, 16 },
546 { X86::MAXPSrr, X86::MAXPSrm, 16 },
547 { X86::MAXPSrr_Int, X86::MAXPSrm_Int, 16 },
548 { X86::MAXSDrr, X86::MAXSDrm, 0 },
549 { X86::MAXSDrr_Int, X86::MAXSDrm_Int, 0 },
550 { X86::MAXSSrr, X86::MAXSSrm, 0 },
551 { X86::MAXSSrr_Int, X86::MAXSSrm_Int, 0 },
552 { X86::MINPDrr, X86::MINPDrm, 16 },
553 { X86::MINPDrr_Int, X86::MINPDrm_Int, 16 },
554 { X86::MINPSrr, X86::MINPSrm, 16 },
555 { X86::MINPSrr_Int, X86::MINPSrm_Int, 16 },
556 { X86::MINSDrr, X86::MINSDrm, 0 },
557 { X86::MINSDrr_Int, X86::MINSDrm_Int, 0 },
558 { X86::MINSSrr, X86::MINSSrm, 0 },
559 { X86::MINSSrr_Int, X86::MINSSrm_Int, 0 },
560 { X86::MULPDrr, X86::MULPDrm, 16 },
561 { X86::MULPSrr, X86::MULPSrm, 16 },
562 { X86::MULSDrr, X86::MULSDrm, 0 },
563 { X86::MULSSrr, X86::MULSSrm, 0 },
564 { X86::OR16rr, X86::OR16rm, 0 },
565 { X86::OR32rr, X86::OR32rm, 0 },
566 { X86::OR64rr, X86::OR64rm, 0 },
567 { X86::OR8rr, X86::OR8rm, 0 },
568 { X86::ORPDrr, X86::ORPDrm, 16 },
569 { X86::ORPSrr, X86::ORPSrm, 16 },
570 { X86::PACKSSDWrr, X86::PACKSSDWrm, 16 },
571 { X86::PACKSSWBrr, X86::PACKSSWBrm, 16 },
572 { X86::PACKUSWBrr, X86::PACKUSWBrm, 16 },
573 { X86::PADDBrr, X86::PADDBrm, 16 },
574 { X86::PADDDrr, X86::PADDDrm, 16 },
575 { X86::PADDQrr, X86::PADDQrm, 16 },
576 { X86::PADDSBrr, X86::PADDSBrm, 16 },
577 { X86::PADDSWrr, X86::PADDSWrm, 16 },
578 { X86::PADDWrr, X86::PADDWrm, 16 },
579 { X86::PANDNrr, X86::PANDNrm, 16 },
580 { X86::PANDrr, X86::PANDrm, 16 },
581 { X86::PAVGBrr, X86::PAVGBrm, 16 },
582 { X86::PAVGWrr, X86::PAVGWrm, 16 },
583 { X86::PCMPEQBrr, X86::PCMPEQBrm, 16 },
584 { X86::PCMPEQDrr, X86::PCMPEQDrm, 16 },
585 { X86::PCMPEQWrr, X86::PCMPEQWrm, 16 },
586 { X86::PCMPGTBrr, X86::PCMPGTBrm, 16 },
587 { X86::PCMPGTDrr, X86::PCMPGTDrm, 16 },
588 { X86::PCMPGTWrr, X86::PCMPGTWrm, 16 },
589 { X86::PINSRWrri, X86::PINSRWrmi, 16 },
590 { X86::PMADDWDrr, X86::PMADDWDrm, 16 },
591 { X86::PMAXSWrr, X86::PMAXSWrm, 16 },
592 { X86::PMAXUBrr, X86::PMAXUBrm, 16 },
593 { X86::PMINSWrr, X86::PMINSWrm, 16 },
594 { X86::PMINUBrr, X86::PMINUBrm, 16 },
595 { X86::PMULDQrr, X86::PMULDQrm, 16 },
596 { X86::PMULHUWrr, X86::PMULHUWrm, 16 },
597 { X86::PMULHWrr, X86::PMULHWrm, 16 },
598 { X86::PMULLDrr, X86::PMULLDrm, 16 },
Evan Cheng9e0c7f22009-07-15 06:10:07 +0000599 { X86::PMULLWrr, X86::PMULLWrm, 16 },
600 { X86::PMULUDQrr, X86::PMULUDQrm, 16 },
601 { X86::PORrr, X86::PORrm, 16 },
602 { X86::PSADBWrr, X86::PSADBWrm, 16 },
603 { X86::PSLLDrr, X86::PSLLDrm, 16 },
604 { X86::PSLLQrr, X86::PSLLQrm, 16 },
605 { X86::PSLLWrr, X86::PSLLWrm, 16 },
606 { X86::PSRADrr, X86::PSRADrm, 16 },
607 { X86::PSRAWrr, X86::PSRAWrm, 16 },
608 { X86::PSRLDrr, X86::PSRLDrm, 16 },
609 { X86::PSRLQrr, X86::PSRLQrm, 16 },
610 { X86::PSRLWrr, X86::PSRLWrm, 16 },
611 { X86::PSUBBrr, X86::PSUBBrm, 16 },
612 { X86::PSUBDrr, X86::PSUBDrm, 16 },
613 { X86::PSUBSBrr, X86::PSUBSBrm, 16 },
614 { X86::PSUBSWrr, X86::PSUBSWrm, 16 },
615 { X86::PSUBWrr, X86::PSUBWrm, 16 },
616 { X86::PUNPCKHBWrr, X86::PUNPCKHBWrm, 16 },
617 { X86::PUNPCKHDQrr, X86::PUNPCKHDQrm, 16 },
618 { X86::PUNPCKHQDQrr, X86::PUNPCKHQDQrm, 16 },
619 { X86::PUNPCKHWDrr, X86::PUNPCKHWDrm, 16 },
620 { X86::PUNPCKLBWrr, X86::PUNPCKLBWrm, 16 },
621 { X86::PUNPCKLDQrr, X86::PUNPCKLDQrm, 16 },
622 { X86::PUNPCKLQDQrr, X86::PUNPCKLQDQrm, 16 },
623 { X86::PUNPCKLWDrr, X86::PUNPCKLWDrm, 16 },
624 { X86::PXORrr, X86::PXORrm, 16 },
625 { X86::SBB32rr, X86::SBB32rm, 0 },
626 { X86::SBB64rr, X86::SBB64rm, 0 },
627 { X86::SHUFPDrri, X86::SHUFPDrmi, 16 },
628 { X86::SHUFPSrri, X86::SHUFPSrmi, 16 },
629 { X86::SUB16rr, X86::SUB16rm, 0 },
630 { X86::SUB32rr, X86::SUB32rm, 0 },
631 { X86::SUB64rr, X86::SUB64rm, 0 },
632 { X86::SUB8rr, X86::SUB8rm, 0 },
633 { X86::SUBPDrr, X86::SUBPDrm, 16 },
634 { X86::SUBPSrr, X86::SUBPSrm, 16 },
635 { X86::SUBSDrr, X86::SUBSDrm, 0 },
636 { X86::SUBSSrr, X86::SUBSSrm, 0 },
Owen Anderson2a3be7b2008-01-07 01:35:02 +0000637 // FIXME: TEST*rr -> swapped operand of TEST*mr.
Evan Cheng9e0c7f22009-07-15 06:10:07 +0000638 { X86::UNPCKHPDrr, X86::UNPCKHPDrm, 16 },
639 { X86::UNPCKHPSrr, X86::UNPCKHPSrm, 16 },
640 { X86::UNPCKLPDrr, X86::UNPCKLPDrm, 16 },
641 { X86::UNPCKLPSrr, X86::UNPCKLPSrm, 16 },
642 { X86::XOR16rr, X86::XOR16rm, 0 },
643 { X86::XOR32rr, X86::XOR32rm, 0 },
644 { X86::XOR64rr, X86::XOR64rm, 0 },
645 { X86::XOR8rr, X86::XOR8rm, 0 },
646 { X86::XORPDrr, X86::XORPDrm, 16 },
647 { X86::XORPSrr, X86::XORPSrm, 16 }
Owen Anderson2a3be7b2008-01-07 01:35:02 +0000648 };
649
650 for (unsigned i = 0, e = array_lengthof(OpTbl2); i != e; ++i) {
651 unsigned RegOp = OpTbl2[i][0];
Daniel Dunbar8f21f9c2010-10-08 02:07:32 +0000652 unsigned MemOp = OpTbl2[i][1];
Evan Cheng9e0c7f22009-07-15 06:10:07 +0000653 unsigned Align = OpTbl2[i][2];
Daniel Dunbar8f21f9c2010-10-08 02:07:32 +0000654 if (!RegOp2MemOpTable2.insert(std::make_pair(RegOp,
655 std::make_pair(MemOp,Align))).second)
656 assert(false && "Duplicated entries?");
Evan Cheng9e0c7f22009-07-15 06:10:07 +0000657 // Index 2, folded load
658 unsigned AuxInfo = 2 | (1 << 4);
Daniel Dunbar8f21f9c2010-10-08 02:07:32 +0000659 if (!MemOp2RegOpTable.insert(std::make_pair(MemOp,
660 std::make_pair(RegOp, AuxInfo))).second)
661 assert(false && "Duplicated entries in unfolding maps?");
Owen Anderson2a3be7b2008-01-07 01:35:02 +0000662 }
Chris Lattnerd92fb002002-10-25 22:55:53 +0000663}
664
Evan Cheng42166152010-01-12 00:09:37 +0000665bool
Evan Cheng30bebff2010-01-13 00:30:23 +0000666X86InstrInfo::isCoalescableExtInstr(const MachineInstr &MI,
667 unsigned &SrcReg, unsigned &DstReg,
668 unsigned &SubIdx) const {
Evan Cheng42166152010-01-12 00:09:37 +0000669 switch (MI.getOpcode()) {
670 default: break;
671 case X86::MOVSX16rr8:
672 case X86::MOVZX16rr8:
673 case X86::MOVSX32rr8:
674 case X86::MOVZX32rr8:
675 case X86::MOVSX64rr8:
676 case X86::MOVZX64rr8:
Evan Chengceb5a4e2010-01-13 08:01:32 +0000677 if (!TM.getSubtarget<X86Subtarget>().is64Bit())
678 // It's not always legal to reference the low 8-bit of the larger
679 // register in 32-bit mode.
680 return false;
Evan Cheng42166152010-01-12 00:09:37 +0000681 case X86::MOVSX32rr16:
682 case X86::MOVZX32rr16:
683 case X86::MOVSX64rr16:
684 case X86::MOVZX64rr16:
685 case X86::MOVSX64rr32:
686 case X86::MOVZX64rr32: {
687 if (MI.getOperand(0).getSubReg() || MI.getOperand(1).getSubReg())
688 // Be conservative.
689 return false;
Evan Cheng42166152010-01-12 00:09:37 +0000690 SrcReg = MI.getOperand(1).getReg();
691 DstReg = MI.getOperand(0).getReg();
Evan Cheng42166152010-01-12 00:09:37 +0000692 switch (MI.getOpcode()) {
693 default:
694 llvm_unreachable(0);
695 break;
696 case X86::MOVSX16rr8:
697 case X86::MOVZX16rr8:
698 case X86::MOVSX32rr8:
699 case X86::MOVZX32rr8:
700 case X86::MOVSX64rr8:
701 case X86::MOVZX64rr8:
Jakob Stoklund Olesen396c8802010-05-25 17:04:16 +0000702 SubIdx = X86::sub_8bit;
Evan Cheng42166152010-01-12 00:09:37 +0000703 break;
704 case X86::MOVSX32rr16:
705 case X86::MOVZX32rr16:
706 case X86::MOVSX64rr16:
707 case X86::MOVZX64rr16:
Jakob Stoklund Olesen396c8802010-05-25 17:04:16 +0000708 SubIdx = X86::sub_16bit;
Evan Cheng42166152010-01-12 00:09:37 +0000709 break;
710 case X86::MOVSX64rr32:
711 case X86::MOVZX64rr32:
Jakob Stoklund Olesen396c8802010-05-25 17:04:16 +0000712 SubIdx = X86::sub_32bit;
Evan Cheng42166152010-01-12 00:09:37 +0000713 break;
714 }
Evan Cheng30bebff2010-01-13 00:30:23 +0000715 return true;
Evan Cheng42166152010-01-12 00:09:37 +0000716 }
717 }
Evan Cheng30bebff2010-01-13 00:30:23 +0000718 return false;
Evan Cheng42166152010-01-12 00:09:37 +0000719}
720
David Greene70fdd572009-11-12 20:55:29 +0000721/// isFrameOperand - Return true and the FrameIndex if the specified
722/// operand and follow operands form a reference to the stack frame.
723bool X86InstrInfo::isFrameOperand(const MachineInstr *MI, unsigned int Op,
724 int &FrameIndex) const {
725 if (MI->getOperand(Op).isFI() && MI->getOperand(Op+1).isImm() &&
726 MI->getOperand(Op+2).isReg() && MI->getOperand(Op+3).isImm() &&
727 MI->getOperand(Op+1).getImm() == 1 &&
728 MI->getOperand(Op+2).getReg() == 0 &&
729 MI->getOperand(Op+3).getImm() == 0) {
730 FrameIndex = MI->getOperand(Op).getIndex();
731 return true;
732 }
733 return false;
734}
735
David Greene2f4c3742009-11-13 00:29:53 +0000736static bool isFrameLoadOpcode(int Opcode) {
737 switch (Opcode) {
Chris Lattnerbb53acd2006-02-02 20:12:32 +0000738 default: break;
739 case X86::MOV8rm:
740 case X86::MOV16rm:
741 case X86::MOV32rm:
Jakob Stoklund Olesene2614a92010-07-09 21:27:55 +0000742 case X86::MOV32rm_TC:
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000743 case X86::MOV64rm:
Jakob Stoklund Olesene2614a92010-07-09 21:27:55 +0000744 case X86::MOV64rm_TC:
Dale Johannesen3d7008c2007-07-04 21:07:47 +0000745 case X86::LD_Fp64m:
Chris Lattnerbb53acd2006-02-02 20:12:32 +0000746 case X86::MOVSSrm:
747 case X86::MOVSDrm:
Chris Lattnerbfc2c682006-04-18 16:44:51 +0000748 case X86::MOVAPSrm:
749 case X86::MOVAPDrm:
Dan Gohmanbdc0f8b2009-01-09 02:40:34 +0000750 case X86::MOVDQArm:
Bill Wendlinge7b2a862007-04-03 06:00:37 +0000751 case X86::MMX_MOVD64rm:
752 case X86::MMX_MOVQ64rm:
David Greene2f4c3742009-11-13 00:29:53 +0000753 return true;
754 break;
755 }
756 return false;
757}
758
759static bool isFrameStoreOpcode(int Opcode) {
760 switch (Opcode) {
761 default: break;
762 case X86::MOV8mr:
763 case X86::MOV16mr:
764 case X86::MOV32mr:
Jakob Stoklund Olesene2614a92010-07-09 21:27:55 +0000765 case X86::MOV32mr_TC:
David Greene2f4c3742009-11-13 00:29:53 +0000766 case X86::MOV64mr:
Jakob Stoklund Olesene2614a92010-07-09 21:27:55 +0000767 case X86::MOV64mr_TC:
David Greene2f4c3742009-11-13 00:29:53 +0000768 case X86::ST_FpP64m:
769 case X86::MOVSSmr:
770 case X86::MOVSDmr:
771 case X86::MOVAPSmr:
772 case X86::MOVAPDmr:
773 case X86::MOVDQAmr:
774 case X86::MMX_MOVD64mr:
775 case X86::MMX_MOVQ64mr:
776 case X86::MMX_MOVNTQmr:
777 return true;
778 }
779 return false;
780}
781
782unsigned X86InstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
783 int &FrameIndex) const {
784 if (isFrameLoadOpcode(MI->getOpcode()))
Jakob Stoklund Olesen96a890a2010-07-27 04:17:01 +0000785 if (MI->getOperand(0).getSubReg() == 0 && isFrameOperand(MI, 1, FrameIndex))
Chris Lattnerbb53acd2006-02-02 20:12:32 +0000786 return MI->getOperand(0).getReg();
David Greene2f4c3742009-11-13 00:29:53 +0000787 return 0;
788}
789
790unsigned X86InstrInfo::isLoadFromStackSlotPostFE(const MachineInstr *MI,
791 int &FrameIndex) const {
792 if (isFrameLoadOpcode(MI->getOpcode())) {
793 unsigned Reg;
794 if ((Reg = isLoadFromStackSlot(MI, FrameIndex)))
795 return Reg;
David Greene70fdd572009-11-12 20:55:29 +0000796 // Check for post-frame index elimination operations
David Greene0508e432009-12-04 22:38:46 +0000797 const MachineMemOperand *Dummy;
798 return hasLoadFromStackSlot(MI, Dummy, FrameIndex);
Chris Lattnerbb53acd2006-02-02 20:12:32 +0000799 }
800 return 0;
801}
802
David Greene70fdd572009-11-12 20:55:29 +0000803bool X86InstrInfo::hasLoadFromStackSlot(const MachineInstr *MI,
David Greene0508e432009-12-04 22:38:46 +0000804 const MachineMemOperand *&MMO,
David Greene70fdd572009-11-12 20:55:29 +0000805 int &FrameIndex) const {
806 for (MachineInstr::mmo_iterator o = MI->memoperands_begin(),
807 oe = MI->memoperands_end();
808 o != oe;
809 ++o) {
810 if ((*o)->isLoad() && (*o)->getValue())
811 if (const FixedStackPseudoSourceValue *Value =
812 dyn_cast<const FixedStackPseudoSourceValue>((*o)->getValue())) {
813 FrameIndex = Value->getFrameIndex();
David Greene0508e432009-12-04 22:38:46 +0000814 MMO = *o;
David Greene70fdd572009-11-12 20:55:29 +0000815 return true;
816 }
817 }
818 return false;
819}
820
Dan Gohman0b273252008-11-18 19:49:32 +0000821unsigned X86InstrInfo::isStoreToStackSlot(const MachineInstr *MI,
Chris Lattnerbb53acd2006-02-02 20:12:32 +0000822 int &FrameIndex) const {
David Greene2f4c3742009-11-13 00:29:53 +0000823 if (isFrameStoreOpcode(MI->getOpcode()))
Jakob Stoklund Olesen96a890a2010-07-27 04:17:01 +0000824 if (MI->getOperand(X86::AddrNumOperands).getSubReg() == 0 &&
825 isFrameOperand(MI, 0, FrameIndex))
Chris Lattnerec536272010-07-08 22:41:28 +0000826 return MI->getOperand(X86::AddrNumOperands).getReg();
David Greene2f4c3742009-11-13 00:29:53 +0000827 return 0;
828}
829
830unsigned X86InstrInfo::isStoreToStackSlotPostFE(const MachineInstr *MI,
831 int &FrameIndex) const {
832 if (isFrameStoreOpcode(MI->getOpcode())) {
833 unsigned Reg;
834 if ((Reg = isStoreToStackSlot(MI, FrameIndex)))
835 return Reg;
David Greene70fdd572009-11-12 20:55:29 +0000836 // Check for post-frame index elimination operations
David Greene0508e432009-12-04 22:38:46 +0000837 const MachineMemOperand *Dummy;
838 return hasStoreToStackSlot(MI, Dummy, FrameIndex);
Chris Lattnerbb53acd2006-02-02 20:12:32 +0000839 }
840 return 0;
841}
842
David Greene70fdd572009-11-12 20:55:29 +0000843bool X86InstrInfo::hasStoreToStackSlot(const MachineInstr *MI,
David Greene0508e432009-12-04 22:38:46 +0000844 const MachineMemOperand *&MMO,
David Greene70fdd572009-11-12 20:55:29 +0000845 int &FrameIndex) const {
846 for (MachineInstr::mmo_iterator o = MI->memoperands_begin(),
847 oe = MI->memoperands_end();
848 o != oe;
849 ++o) {
850 if ((*o)->isStore() && (*o)->getValue())
851 if (const FixedStackPseudoSourceValue *Value =
852 dyn_cast<const FixedStackPseudoSourceValue>((*o)->getValue())) {
853 FrameIndex = Value->getFrameIndex();
David Greene0508e432009-12-04 22:38:46 +0000854 MMO = *o;
David Greene70fdd572009-11-12 20:55:29 +0000855 return true;
856 }
857 }
858 return false;
859}
860
Evan Cheng308e5642008-03-27 01:45:11 +0000861/// regIsPICBase - Return true if register is PIC base (i.e.g defined by
862/// X86::MOVPC32r.
Dan Gohman3b460302008-07-07 23:14:23 +0000863static bool regIsPICBase(unsigned BaseReg, const MachineRegisterInfo &MRI) {
Evan Cheng308e5642008-03-27 01:45:11 +0000864 bool isPICBase = false;
865 for (MachineRegisterInfo::def_iterator I = MRI.def_begin(BaseReg),
866 E = MRI.def_end(); I != E; ++I) {
867 MachineInstr *DefMI = I.getOperand().getParent();
868 if (DefMI->getOpcode() != X86::MOVPC32r)
869 return false;
870 assert(!isPICBase && "More than one PIC base?");
871 isPICBase = true;
872 }
873 return isPICBase;
874}
Evan Cheng1973a462008-03-31 07:54:19 +0000875
Bill Wendling1e117682008-05-12 20:54:26 +0000876bool
Dan Gohmane919de52009-10-10 00:34:18 +0000877X86InstrInfo::isReallyTriviallyReMaterializable(const MachineInstr *MI,
878 AliasAnalysis *AA) const {
Dan Gohman4a4a8eb2007-06-14 20:50:44 +0000879 switch (MI->getOpcode()) {
880 default: break;
Evan Cheng29e62a52008-03-27 01:41:09 +0000881 case X86::MOV8rm:
882 case X86::MOV16rm:
Evan Cheng29e62a52008-03-27 01:41:09 +0000883 case X86::MOV32rm:
Evan Cheng29e62a52008-03-27 01:41:09 +0000884 case X86::MOV64rm:
885 case X86::LD_Fp64m:
886 case X86::MOVSSrm:
887 case X86::MOVSDrm:
888 case X86::MOVAPSrm:
Evan Chengf25ef4f2009-11-16 21:56:03 +0000889 case X86::MOVUPSrm:
Evan Cheng5392cc92009-11-17 09:51:18 +0000890 case X86::MOVUPSrm_Int:
Evan Cheng29e62a52008-03-27 01:41:09 +0000891 case X86::MOVAPDrm:
Dan Gohmanbdc0f8b2009-01-09 02:40:34 +0000892 case X86::MOVDQArm:
Evan Cheng29e62a52008-03-27 01:41:09 +0000893 case X86::MMX_MOVD64rm:
Evan Cheng5392cc92009-11-17 09:51:18 +0000894 case X86::MMX_MOVQ64rm:
895 case X86::FsMOVAPSrm:
896 case X86::FsMOVAPDrm: {
Evan Cheng29e62a52008-03-27 01:41:09 +0000897 // Loads from constant pools are trivially rematerializable.
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000898 if (MI->getOperand(1).isReg() &&
899 MI->getOperand(2).isImm() &&
900 MI->getOperand(3).isReg() && MI->getOperand(3).getReg() == 0 &&
Dan Gohmane919de52009-10-10 00:34:18 +0000901 MI->isInvariantLoad(AA)) {
Evan Cheng29e62a52008-03-27 01:41:09 +0000902 unsigned BaseReg = MI->getOperand(1).getReg();
Chris Lattnerfea81da2009-06-27 04:16:01 +0000903 if (BaseReg == 0 || BaseReg == X86::RIP)
Evan Cheng29e62a52008-03-27 01:41:09 +0000904 return true;
905 // Allow re-materialization of PIC load.
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000906 if (!ReMatPICStubLoad && MI->getOperand(4).isGlobal())
Evan Chengb86595f2008-04-01 23:26:12 +0000907 return false;
Dan Gohman3b460302008-07-07 23:14:23 +0000908 const MachineFunction &MF = *MI->getParent()->getParent();
909 const MachineRegisterInfo &MRI = MF.getRegInfo();
Evan Cheng29e62a52008-03-27 01:41:09 +0000910 bool isPICBase = false;
911 for (MachineRegisterInfo::def_iterator I = MRI.def_begin(BaseReg),
912 E = MRI.def_end(); I != E; ++I) {
913 MachineInstr *DefMI = I.getOperand().getParent();
914 if (DefMI->getOpcode() != X86::MOVPC32r)
915 return false;
916 assert(!isPICBase && "More than one PIC base?");
917 isPICBase = true;
918 }
919 return isPICBase;
920 }
921 return false;
Evan Cheng94ba37f2008-02-22 09:25:47 +0000922 }
Evan Cheng29e62a52008-03-27 01:41:09 +0000923
924 case X86::LEA32r:
925 case X86::LEA64r: {
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000926 if (MI->getOperand(2).isImm() &&
927 MI->getOperand(3).isReg() && MI->getOperand(3).getReg() == 0 &&
928 !MI->getOperand(4).isReg()) {
Evan Cheng29e62a52008-03-27 01:41:09 +0000929 // lea fi#, lea GV, etc. are all rematerializable.
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000930 if (!MI->getOperand(1).isReg())
Dan Gohman7e922aa2008-09-26 21:30:20 +0000931 return true;
Evan Cheng29e62a52008-03-27 01:41:09 +0000932 unsigned BaseReg = MI->getOperand(1).getReg();
933 if (BaseReg == 0)
934 return true;
935 // Allow re-materialization of lea PICBase + x.
Dan Gohman3b460302008-07-07 23:14:23 +0000936 const MachineFunction &MF = *MI->getParent()->getParent();
937 const MachineRegisterInfo &MRI = MF.getRegInfo();
Evan Cheng308e5642008-03-27 01:45:11 +0000938 return regIsPICBase(BaseReg, MRI);
Evan Cheng29e62a52008-03-27 01:41:09 +0000939 }
940 return false;
941 }
Dan Gohman4a4a8eb2007-06-14 20:50:44 +0000942 }
Evan Cheng29e62a52008-03-27 01:41:09 +0000943
Dan Gohmane8c1e422007-06-26 00:48:07 +0000944 // All other instructions marked M_REMATERIALIZABLE are always trivially
945 // rematerializable.
946 return true;
Dan Gohman4a4a8eb2007-06-14 20:50:44 +0000947}
948
Evan Cheng3f2ceac2008-06-24 07:10:51 +0000949/// isSafeToClobberEFLAGS - Return true if it's safe insert an instruction that
950/// would clobber the EFLAGS condition register. Note the result may be
951/// conservative. If it cannot definitely determine the safety after visiting
Dan Gohman0be8c2b2009-10-14 00:08:59 +0000952/// a few instructions in each direction it assumes it's not safe.
Evan Cheng3f2ceac2008-06-24 07:10:51 +0000953static bool isSafeToClobberEFLAGS(MachineBasicBlock &MBB,
954 MachineBasicBlock::iterator I) {
Evan Chengb6dee6e2010-03-23 20:35:45 +0000955 MachineBasicBlock::iterator E = MBB.end();
956
Dan Gohmanc8354582008-10-21 03:24:31 +0000957 // It's always safe to clobber EFLAGS at the end of a block.
Evan Chengb6dee6e2010-03-23 20:35:45 +0000958 if (I == E)
Dan Gohmanc8354582008-10-21 03:24:31 +0000959 return true;
960
Evan Cheng3f2ceac2008-06-24 07:10:51 +0000961 // For compile time consideration, if we are not able to determine the
Dan Gohman0be8c2b2009-10-14 00:08:59 +0000962 // safety after visiting 4 instructions in each direction, we will assume
963 // it's not safe.
964 MachineBasicBlock::iterator Iter = I;
965 for (unsigned i = 0; i < 4; ++i) {
Evan Cheng3f2ceac2008-06-24 07:10:51 +0000966 bool SeenDef = false;
Dan Gohman0be8c2b2009-10-14 00:08:59 +0000967 for (unsigned j = 0, e = Iter->getNumOperands(); j != e; ++j) {
968 MachineOperand &MO = Iter->getOperand(j);
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000969 if (!MO.isReg())
Evan Cheng3f2ceac2008-06-24 07:10:51 +0000970 continue;
971 if (MO.getReg() == X86::EFLAGS) {
972 if (MO.isUse())
973 return false;
974 SeenDef = true;
975 }
976 }
977
978 if (SeenDef)
979 // This instruction defines EFLAGS, no need to look any further.
980 return true;
Dan Gohman0be8c2b2009-10-14 00:08:59 +0000981 ++Iter;
Evan Chengb6dee6e2010-03-23 20:35:45 +0000982 // Skip over DBG_VALUE.
983 while (Iter != E && Iter->isDebugValue())
984 ++Iter;
Dan Gohmanc8354582008-10-21 03:24:31 +0000985
986 // If we make it to the end of the block, it's safe to clobber EFLAGS.
Evan Chengb6dee6e2010-03-23 20:35:45 +0000987 if (Iter == E)
Dan Gohman0be8c2b2009-10-14 00:08:59 +0000988 return true;
989 }
990
Evan Chengb6dee6e2010-03-23 20:35:45 +0000991 MachineBasicBlock::iterator B = MBB.begin();
Dan Gohman0be8c2b2009-10-14 00:08:59 +0000992 Iter = I;
993 for (unsigned i = 0; i < 4; ++i) {
994 // If we make it to the beginning of the block, it's safe to clobber
995 // EFLAGS iff EFLAGS is not live-in.
Evan Chengb6dee6e2010-03-23 20:35:45 +0000996 if (Iter == B)
Dan Gohman0be8c2b2009-10-14 00:08:59 +0000997 return !MBB.isLiveIn(X86::EFLAGS);
998
999 --Iter;
Evan Chengb6dee6e2010-03-23 20:35:45 +00001000 // Skip over DBG_VALUE.
1001 while (Iter != B && Iter->isDebugValue())
1002 --Iter;
1003
Dan Gohman0be8c2b2009-10-14 00:08:59 +00001004 bool SawKill = false;
1005 for (unsigned j = 0, e = Iter->getNumOperands(); j != e; ++j) {
1006 MachineOperand &MO = Iter->getOperand(j);
1007 if (MO.isReg() && MO.getReg() == X86::EFLAGS) {
1008 if (MO.isDef()) return MO.isDead();
1009 if (MO.isKill()) SawKill = true;
1010 }
1011 }
1012
1013 if (SawKill)
1014 // This instruction kills EFLAGS and doesn't redefine it, so
1015 // there's no need to look further.
Dan Gohmanc8354582008-10-21 03:24:31 +00001016 return true;
Evan Cheng3f2ceac2008-06-24 07:10:51 +00001017 }
1018
1019 // Conservative answer.
1020 return false;
1021}
1022
Evan Chenged6e34f2008-03-31 20:40:39 +00001023void X86InstrInfo::reMaterialize(MachineBasicBlock &MBB,
1024 MachineBasicBlock::iterator I,
Evan Cheng84517442009-07-16 09:20:10 +00001025 unsigned DestReg, unsigned SubIdx,
Evan Cheng6ad7da92009-11-14 02:55:43 +00001026 const MachineInstr *Orig,
Jakob Stoklund Olesena8ad9772010-06-02 22:47:25 +00001027 const TargetRegisterInfo &TRI) const {
Dan Gohman90c600d2010-05-07 01:28:10 +00001028 DebugLoc DL = Orig->getDebugLoc();
Bill Wendling27b508d2009-02-11 21:51:19 +00001029
Evan Chenged6e34f2008-03-31 20:40:39 +00001030 // MOV32r0 etc. are implemented with xor which clobbers condition code.
1031 // Re-materialize them as movri instructions to avoid side effects.
Evan Cheng84517442009-07-16 09:20:10 +00001032 bool Clone = true;
1033 unsigned Opc = Orig->getOpcode();
1034 switch (Opc) {
Evan Cheng3f2ceac2008-06-24 07:10:51 +00001035 default: break;
Evan Chenged6e34f2008-03-31 20:40:39 +00001036 case X86::MOV8r0:
Dan Gohmanc1195802010-01-12 04:42:54 +00001037 case X86::MOV16r0:
1038 case X86::MOV32r0:
1039 case X86::MOV64r0: {
Evan Cheng3f2ceac2008-06-24 07:10:51 +00001040 if (!isSafeToClobberEFLAGS(MBB, I)) {
Evan Cheng84517442009-07-16 09:20:10 +00001041 switch (Opc) {
Evan Cheng3f2ceac2008-06-24 07:10:51 +00001042 default: break;
1043 case X86::MOV8r0: Opc = X86::MOV8ri; break;
Dan Gohmanc1195802010-01-12 04:42:54 +00001044 case X86::MOV16r0: Opc = X86::MOV16ri; break;
Evan Cheng3f2ceac2008-06-24 07:10:51 +00001045 case X86::MOV32r0: Opc = X86::MOV32ri; break;
Dan Gohman952f6f92010-02-26 16:49:27 +00001046 case X86::MOV64r0: Opc = X86::MOV64ri64i32; break;
Evan Cheng3f2ceac2008-06-24 07:10:51 +00001047 }
Evan Cheng84517442009-07-16 09:20:10 +00001048 Clone = false;
Evan Cheng3f2ceac2008-06-24 07:10:51 +00001049 }
Evan Chenged6e34f2008-03-31 20:40:39 +00001050 break;
Evan Cheng3f2ceac2008-06-24 07:10:51 +00001051 }
1052 }
1053
Evan Cheng84517442009-07-16 09:20:10 +00001054 if (Clone) {
Dan Gohman3b460302008-07-07 23:14:23 +00001055 MachineInstr *MI = MBB.getParent()->CloneMachineInstr(Orig);
Evan Chenged6e34f2008-03-31 20:40:39 +00001056 MBB.insert(I, MI);
Evan Cheng84517442009-07-16 09:20:10 +00001057 } else {
Jakob Stoklund Olesena8ad9772010-06-02 22:47:25 +00001058 BuildMI(MBB, I, DL, get(Opc)).addOperand(Orig->getOperand(0)).addImm(0);
Evan Chenged6e34f2008-03-31 20:40:39 +00001059 }
Evan Cheng147cb762008-04-16 23:44:44 +00001060
Evan Cheng84517442009-07-16 09:20:10 +00001061 MachineInstr *NewMI = prior(I);
Jakob Stoklund Olesena8ad9772010-06-02 22:47:25 +00001062 NewMI->substituteRegister(Orig->getOperand(0).getReg(), DestReg, SubIdx, TRI);
Evan Chenged6e34f2008-03-31 20:40:39 +00001063}
1064
Evan Chenga8a9c152007-10-05 08:04:01 +00001065/// hasLiveCondCodeDef - True if MI has a condition code def, e.g. EFLAGS, that
1066/// is not marked dead.
1067static bool hasLiveCondCodeDef(MachineInstr *MI) {
Evan Chenga8a9c152007-10-05 08:04:01 +00001068 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1069 MachineOperand &MO = MI->getOperand(i);
Dan Gohman0d1e9a82008-10-03 15:45:36 +00001070 if (MO.isReg() && MO.isDef() &&
Evan Chenga8a9c152007-10-05 08:04:01 +00001071 MO.getReg() == X86::EFLAGS && !MO.isDead()) {
1072 return true;
1073 }
1074 }
1075 return false;
1076}
1077
Evan Cheng26fdd722009-12-12 20:03:14 +00001078/// convertToThreeAddressWithLEA - Helper for convertToThreeAddress when
Evan Cheng766a73f2009-12-11 06:01:48 +00001079/// 16-bit LEA is disabled, use 32-bit LEA to form 3-address code by promoting
1080/// to a 32-bit superregister and then truncating back down to a 16-bit
1081/// subregister.
1082MachineInstr *
1083X86InstrInfo::convertToThreeAddressWithLEA(unsigned MIOpc,
1084 MachineFunction::iterator &MFI,
1085 MachineBasicBlock::iterator &MBBI,
1086 LiveVariables *LV) const {
1087 MachineInstr *MI = MBBI;
1088 unsigned Dest = MI->getOperand(0).getReg();
1089 unsigned Src = MI->getOperand(1).getReg();
1090 bool isDead = MI->getOperand(0).isDead();
1091 bool isKill = MI->getOperand(1).isKill();
1092
1093 unsigned Opc = TM.getSubtarget<X86Subtarget>().is64Bit()
1094 ? X86::LEA64_32r : X86::LEA32r;
1095 MachineRegisterInfo &RegInfo = MFI->getParent()->getRegInfo();
Jakob Stoklund Olesenb19bae42010-10-07 00:07:26 +00001096 unsigned leaInReg = RegInfo.createVirtualRegister(&X86::GR32_NOSPRegClass);
Evan Cheng766a73f2009-12-11 06:01:48 +00001097 unsigned leaOutReg = RegInfo.createVirtualRegister(&X86::GR32RegClass);
1098
1099 // Build and insert into an implicit UNDEF value. This is OK because
1100 // well be shifting and then extracting the lower 16-bits.
Evan Cheng26fdd722009-12-12 20:03:14 +00001101 // This has the potential to cause partial register stall. e.g.
Evan Cheng3974c8d2009-12-12 18:55:26 +00001102 // movw (%rbp,%rcx,2), %dx
1103 // leal -65(%rdx), %esi
Evan Cheng26fdd722009-12-12 20:03:14 +00001104 // But testing has shown this *does* help performance in 64-bit mode (at
1105 // least on modern x86 machines).
Evan Cheng766a73f2009-12-11 06:01:48 +00001106 BuildMI(*MFI, MBBI, MI->getDebugLoc(), get(X86::IMPLICIT_DEF), leaInReg);
1107 MachineInstr *InsMI =
Jakob Stoklund Olesena1e883d2010-07-08 16:40:15 +00001108 BuildMI(*MFI, MBBI, MI->getDebugLoc(), get(TargetOpcode::COPY))
1109 .addReg(leaInReg, RegState::Define, X86::sub_16bit)
1110 .addReg(Src, getKillRegState(isKill));
Evan Cheng766a73f2009-12-11 06:01:48 +00001111
1112 MachineInstrBuilder MIB = BuildMI(*MFI, MBBI, MI->getDebugLoc(),
1113 get(Opc), leaOutReg);
1114 switch (MIOpc) {
1115 default:
1116 llvm_unreachable(0);
1117 break;
1118 case X86::SHL16ri: {
1119 unsigned ShAmt = MI->getOperand(2).getImm();
1120 MIB.addReg(0).addImm(1 << ShAmt)
Chris Lattnerf4693072010-07-08 23:46:44 +00001121 .addReg(leaInReg, RegState::Kill).addImm(0).addReg(0);
Evan Cheng766a73f2009-12-11 06:01:48 +00001122 break;
1123 }
1124 case X86::INC16r:
1125 case X86::INC64_16r:
Chris Lattnerf4693072010-07-08 23:46:44 +00001126 addRegOffset(MIB, leaInReg, true, 1);
Evan Cheng766a73f2009-12-11 06:01:48 +00001127 break;
1128 case X86::DEC16r:
1129 case X86::DEC64_16r:
Chris Lattnerf4693072010-07-08 23:46:44 +00001130 addRegOffset(MIB, leaInReg, true, -1);
Evan Cheng766a73f2009-12-11 06:01:48 +00001131 break;
1132 case X86::ADD16ri:
1133 case X86::ADD16ri8:
Chris Lattnerf4693072010-07-08 23:46:44 +00001134 addRegOffset(MIB, leaInReg, true, MI->getOperand(2).getImm());
Evan Cheng766a73f2009-12-11 06:01:48 +00001135 break;
Daniel Dunbar8f21f9c2010-10-08 02:07:32 +00001136 case X86::ADD16rr: {
Evan Cheng766a73f2009-12-11 06:01:48 +00001137 unsigned Src2 = MI->getOperand(2).getReg();
1138 bool isKill2 = MI->getOperand(2).isKill();
1139 unsigned leaInReg2 = 0;
1140 MachineInstr *InsMI2 = 0;
1141 if (Src == Src2) {
1142 // ADD16rr %reg1028<kill>, %reg1028
1143 // just a single insert_subreg.
1144 addRegReg(MIB, leaInReg, true, leaInReg, false);
1145 } else {
Jakob Stoklund Olesenb19bae42010-10-07 00:07:26 +00001146 leaInReg2 = RegInfo.createVirtualRegister(&X86::GR32_NOSPRegClass);
Evan Cheng766a73f2009-12-11 06:01:48 +00001147 // Build and insert into an implicit UNDEF value. This is OK because
1148 // well be shifting and then extracting the lower 16-bits.
1149 BuildMI(*MFI, MIB, MI->getDebugLoc(), get(X86::IMPLICIT_DEF), leaInReg2);
1150 InsMI2 =
Jakob Stoklund Olesena1e883d2010-07-08 16:40:15 +00001151 BuildMI(*MFI, MIB, MI->getDebugLoc(), get(TargetOpcode::COPY))
1152 .addReg(leaInReg2, RegState::Define, X86::sub_16bit)
1153 .addReg(Src2, getKillRegState(isKill2));
Evan Cheng766a73f2009-12-11 06:01:48 +00001154 addRegReg(MIB, leaInReg, true, leaInReg2, true);
1155 }
1156 if (LV && isKill2 && InsMI2)
1157 LV->replaceKillInstruction(Src2, MI, InsMI2);
1158 break;
1159 }
1160 }
1161
1162 MachineInstr *NewMI = MIB;
1163 MachineInstr *ExtMI =
Jakob Stoklund Olesen00264622010-07-08 16:40:22 +00001164 BuildMI(*MFI, MBBI, MI->getDebugLoc(), get(TargetOpcode::COPY))
Evan Cheng766a73f2009-12-11 06:01:48 +00001165 .addReg(Dest, RegState::Define | getDeadRegState(isDead))
Jakob Stoklund Olesen00264622010-07-08 16:40:22 +00001166 .addReg(leaOutReg, RegState::Kill, X86::sub_16bit);
Evan Cheng766a73f2009-12-11 06:01:48 +00001167
1168 if (LV) {
1169 // Update live variables
1170 LV->getVarInfo(leaInReg).Kills.push_back(NewMI);
1171 LV->getVarInfo(leaOutReg).Kills.push_back(ExtMI);
1172 if (isKill)
1173 LV->replaceKillInstruction(Src, MI, InsMI);
1174 if (isDead)
1175 LV->replaceKillInstruction(Dest, MI, ExtMI);
1176 }
1177
1178 return ExtMI;
1179}
1180
Chris Lattnerb7782d72005-01-02 02:37:07 +00001181/// convertToThreeAddress - This method must be implemented by targets that
1182/// set the M_CONVERTIBLE_TO_3_ADDR flag. When this flag is set, the target
1183/// may be able to convert a two-address instruction into a true
1184/// three-address instruction on demand. This allows the X86 target (for
1185/// example) to convert ADD and SHL instructions into LEA instructions if they
1186/// would require register copies due to two-addressness.
1187///
1188/// This method returns a null pointer if the transformation cannot be
1189/// performed, otherwise it returns the new instruction.
1190///
Evan Cheng07fc1072006-12-01 21:52:41 +00001191MachineInstr *
1192X86InstrInfo::convertToThreeAddress(MachineFunction::iterator &MFI,
1193 MachineBasicBlock::iterator &MBBI,
Owen Anderson30cc0282008-07-02 23:41:07 +00001194 LiveVariables *LV) const {
Evan Cheng07fc1072006-12-01 21:52:41 +00001195 MachineInstr *MI = MBBI;
Dan Gohman3b460302008-07-07 23:14:23 +00001196 MachineFunction &MF = *MI->getParent()->getParent();
Chris Lattnerb7782d72005-01-02 02:37:07 +00001197 // All instructions input are two-addr instructions. Get the known operands.
1198 unsigned Dest = MI->getOperand(0).getReg();
1199 unsigned Src = MI->getOperand(1).getReg();
Evan Cheng7d98a482008-07-03 09:09:37 +00001200 bool isDead = MI->getOperand(0).isDead();
1201 bool isKill = MI->getOperand(1).isKill();
Chris Lattnerb7782d72005-01-02 02:37:07 +00001202
Evan Chengdc2c8742006-11-15 20:58:11 +00001203 MachineInstr *NewMI = NULL;
Evan Cheng07fc1072006-12-01 21:52:41 +00001204 // FIXME: 16-bit LEA's are really slow on Athlons, but not bad on P4's. When
Chris Lattner3e1d9172007-03-20 06:08:29 +00001205 // we have better subtarget support, enable the 16-bit LEA generation here.
Evan Cheng26fdd722009-12-12 20:03:14 +00001206 // 16-bit LEA is also slow on Core2.
Evan Cheng07fc1072006-12-01 21:52:41 +00001207 bool DisableLEA16 = true;
Evan Cheng26fdd722009-12-12 20:03:14 +00001208 bool is64Bit = TM.getSubtarget<X86Subtarget>().is64Bit();
Evan Cheng07fc1072006-12-01 21:52:41 +00001209
Evan Chengfa2c8282007-10-05 20:34:26 +00001210 unsigned MIOpc = MI->getOpcode();
1211 switch (MIOpc) {
Evan Cheng66f849b2006-05-30 20:26:50 +00001212 case X86::SHUFPSrri: {
1213 assert(MI->getNumOperands() == 4 && "Unknown shufps instruction!");
Chris Lattner3e1d9172007-03-20 06:08:29 +00001214 if (!TM.getSubtarget<X86Subtarget>().hasSSE2()) return 0;
1215
Evan Chengc8c172e2006-05-30 21:45:53 +00001216 unsigned B = MI->getOperand(1).getReg();
1217 unsigned C = MI->getOperand(2).getReg();
Chris Lattner3e1d9172007-03-20 06:08:29 +00001218 if (B != C) return 0;
Evan Cheng7d98a482008-07-03 09:09:37 +00001219 unsigned A = MI->getOperand(0).getReg();
1220 unsigned M = MI->getOperand(3).getImm();
Bill Wendling27b508d2009-02-11 21:51:19 +00001221 NewMI = BuildMI(MF, MI->getDebugLoc(), get(X86::PSHUFDri))
Bill Wendlingf7b83c72009-05-13 21:33:08 +00001222 .addReg(A, RegState::Define | getDeadRegState(isDead))
1223 .addReg(B, getKillRegState(isKill)).addImm(M);
Chris Lattner3e1d9172007-03-20 06:08:29 +00001224 break;
1225 }
Chris Lattnerbcd38852007-03-28 18:12:31 +00001226 case X86::SHL64ri: {
Evan Cheng483e1ce2007-09-14 21:48:26 +00001227 assert(MI->getNumOperands() >= 3 && "Unknown shift instruction!");
Chris Lattnerbcd38852007-03-28 18:12:31 +00001228 // NOTE: LEA doesn't produce flags like shift does, but LLVM never uses
1229 // the flags produced by a shift yet, so this is safe.
Chris Lattnerbcd38852007-03-28 18:12:31 +00001230 unsigned ShAmt = MI->getOperand(2).getImm();
1231 if (ShAmt == 0 || ShAmt >= 4) return 0;
Evan Cheng7d98a482008-07-03 09:09:37 +00001232
Jakob Stoklund Olesenb19bae42010-10-07 00:07:26 +00001233 // LEA can't handle RSP.
1234 if (TargetRegisterInfo::isVirtualRegister(Src) &&
1235 !MF.getRegInfo().constrainRegClass(Src, &X86::GR64_NOSPRegClass))
1236 return 0;
1237
Bill Wendling27b508d2009-02-11 21:51:19 +00001238 NewMI = BuildMI(MF, MI->getDebugLoc(), get(X86::LEA64r))
Bill Wendlingf7b83c72009-05-13 21:33:08 +00001239 .addReg(Dest, RegState::Define | getDeadRegState(isDead))
1240 .addReg(0).addImm(1 << ShAmt)
1241 .addReg(Src, getKillRegState(isKill))
Chris Lattnerf4693072010-07-08 23:46:44 +00001242 .addImm(0).addReg(0);
Chris Lattnerbcd38852007-03-28 18:12:31 +00001243 break;
1244 }
Chris Lattner3e1d9172007-03-20 06:08:29 +00001245 case X86::SHL32ri: {
Evan Cheng483e1ce2007-09-14 21:48:26 +00001246 assert(MI->getNumOperands() >= 3 && "Unknown shift instruction!");
Chris Lattner3e1d9172007-03-20 06:08:29 +00001247 // NOTE: LEA doesn't produce flags like shift does, but LLVM never uses
1248 // the flags produced by a shift yet, so this is safe.
Chris Lattner3e1d9172007-03-20 06:08:29 +00001249 unsigned ShAmt = MI->getOperand(2).getImm();
1250 if (ShAmt == 0 || ShAmt >= 4) return 0;
Evan Cheng7d98a482008-07-03 09:09:37 +00001251
Jakob Stoklund Olesenb19bae42010-10-07 00:07:26 +00001252 // LEA can't handle ESP.
1253 if (TargetRegisterInfo::isVirtualRegister(Src) &&
1254 !MF.getRegInfo().constrainRegClass(Src, &X86::GR32_NOSPRegClass))
1255 return 0;
1256
Evan Cheng26fdd722009-12-12 20:03:14 +00001257 unsigned Opc = is64Bit ? X86::LEA64_32r : X86::LEA32r;
Bill Wendling27b508d2009-02-11 21:51:19 +00001258 NewMI = BuildMI(MF, MI->getDebugLoc(), get(Opc))
Bill Wendlingf7b83c72009-05-13 21:33:08 +00001259 .addReg(Dest, RegState::Define | getDeadRegState(isDead))
Evan Cheng7d98a482008-07-03 09:09:37 +00001260 .addReg(0).addImm(1 << ShAmt)
Chris Lattnerf4693072010-07-08 23:46:44 +00001261 .addReg(Src, getKillRegState(isKill)).addImm(0).addReg(0);
Chris Lattner3e1d9172007-03-20 06:08:29 +00001262 break;
1263 }
1264 case X86::SHL16ri: {
Evan Cheng483e1ce2007-09-14 21:48:26 +00001265 assert(MI->getNumOperands() >= 3 && "Unknown shift instruction!");
Evan Cheng189df732007-09-06 00:14:41 +00001266 // NOTE: LEA doesn't produce flags like shift does, but LLVM never uses
1267 // the flags produced by a shift yet, so this is safe.
Evan Cheng189df732007-09-06 00:14:41 +00001268 unsigned ShAmt = MI->getOperand(2).getImm();
1269 if (ShAmt == 0 || ShAmt >= 4) return 0;
Evan Cheng7d98a482008-07-03 09:09:37 +00001270
Evan Cheng766a73f2009-12-11 06:01:48 +00001271 if (DisableLEA16)
Evan Cheng26fdd722009-12-12 20:03:14 +00001272 return is64Bit ? convertToThreeAddressWithLEA(MIOpc, MFI, MBBI, LV) : 0;
Evan Cheng766a73f2009-12-11 06:01:48 +00001273 NewMI = BuildMI(MF, MI->getDebugLoc(), get(X86::LEA16r))
1274 .addReg(Dest, RegState::Define | getDeadRegState(isDead))
1275 .addReg(0).addImm(1 << ShAmt)
1276 .addReg(Src, getKillRegState(isKill))
Chris Lattnerf4693072010-07-08 23:46:44 +00001277 .addImm(0).addReg(0);
Chris Lattner3e1d9172007-03-20 06:08:29 +00001278 break;
Evan Cheng66f849b2006-05-30 20:26:50 +00001279 }
Evan Chengfa2c8282007-10-05 20:34:26 +00001280 default: {
1281 // The following opcodes also sets the condition code register(s). Only
1282 // convert them to equivalent lea if the condition code register def's
1283 // are dead!
1284 if (hasLiveCondCodeDef(MI))
1285 return 0;
Evan Cheng66f849b2006-05-30 20:26:50 +00001286
Evan Chengfa2c8282007-10-05 20:34:26 +00001287 switch (MIOpc) {
1288 default: return 0;
1289 case X86::INC64r:
Dan Gohmanbeac19e2009-01-06 23:34:46 +00001290 case X86::INC32r:
1291 case X86::INC64_32r: {
Evan Chengfa2c8282007-10-05 20:34:26 +00001292 assert(MI->getNumOperands() >= 2 && "Unknown inc instruction!");
Evan Cheng82bc90a2007-10-09 07:14:53 +00001293 unsigned Opc = MIOpc == X86::INC64r ? X86::LEA64r
1294 : (is64Bit ? X86::LEA64_32r : X86::LEA32r);
Jakob Stoklund Olesenb19bae42010-10-07 00:07:26 +00001295
1296 // LEA can't handle RSP.
1297 if (TargetRegisterInfo::isVirtualRegister(Src) &&
1298 !MF.getRegInfo().constrainRegClass(Src,
1299 MIOpc == X86::INC64r ? X86::GR64_NOSPRegisterClass :
1300 X86::GR32_NOSPRegisterClass))
1301 return 0;
1302
Chris Lattnerf4693072010-07-08 23:46:44 +00001303 NewMI = addRegOffset(BuildMI(MF, MI->getDebugLoc(), get(Opc))
Bill Wendlingf7b83c72009-05-13 21:33:08 +00001304 .addReg(Dest, RegState::Define |
1305 getDeadRegState(isDead)),
Rafael Espindola3b2df102009-04-08 21:14:34 +00001306 Src, isKill, 1);
Evan Chengfa2c8282007-10-05 20:34:26 +00001307 break;
Chris Lattnerb7782d72005-01-02 02:37:07 +00001308 }
Evan Chengfa2c8282007-10-05 20:34:26 +00001309 case X86::INC16r:
1310 case X86::INC64_16r:
Evan Cheng766a73f2009-12-11 06:01:48 +00001311 if (DisableLEA16)
Evan Cheng26fdd722009-12-12 20:03:14 +00001312 return is64Bit ? convertToThreeAddressWithLEA(MIOpc, MFI, MBBI, LV) : 0;
Evan Chengfa2c8282007-10-05 20:34:26 +00001313 assert(MI->getNumOperands() >= 2 && "Unknown inc instruction!");
Bill Wendling27b508d2009-02-11 21:51:19 +00001314 NewMI = addRegOffset(BuildMI(MF, MI->getDebugLoc(), get(X86::LEA16r))
Bill Wendlingf7b83c72009-05-13 21:33:08 +00001315 .addReg(Dest, RegState::Define |
1316 getDeadRegState(isDead)),
Evan Cheng7d98a482008-07-03 09:09:37 +00001317 Src, isKill, 1);
Evan Chengfa2c8282007-10-05 20:34:26 +00001318 break;
1319 case X86::DEC64r:
Dan Gohmanbeac19e2009-01-06 23:34:46 +00001320 case X86::DEC32r:
1321 case X86::DEC64_32r: {
Evan Chengfa2c8282007-10-05 20:34:26 +00001322 assert(MI->getNumOperands() >= 2 && "Unknown dec instruction!");
Evan Cheng82bc90a2007-10-09 07:14:53 +00001323 unsigned Opc = MIOpc == X86::DEC64r ? X86::LEA64r
1324 : (is64Bit ? X86::LEA64_32r : X86::LEA32r);
Jakob Stoklund Olesenb19bae42010-10-07 00:07:26 +00001325 // LEA can't handle RSP.
1326 if (TargetRegisterInfo::isVirtualRegister(Src) &&
1327 !MF.getRegInfo().constrainRegClass(Src,
1328 MIOpc == X86::DEC64r ? X86::GR64_NOSPRegisterClass :
1329 X86::GR32_NOSPRegisterClass))
1330 return 0;
1331
Chris Lattnerf4693072010-07-08 23:46:44 +00001332 NewMI = addRegOffset(BuildMI(MF, MI->getDebugLoc(), get(Opc))
Bill Wendlingf7b83c72009-05-13 21:33:08 +00001333 .addReg(Dest, RegState::Define |
1334 getDeadRegState(isDead)),
Rafael Espindola3b2df102009-04-08 21:14:34 +00001335 Src, isKill, -1);
Evan Chengfa2c8282007-10-05 20:34:26 +00001336 break;
1337 }
1338 case X86::DEC16r:
1339 case X86::DEC64_16r:
Evan Cheng766a73f2009-12-11 06:01:48 +00001340 if (DisableLEA16)
Evan Cheng26fdd722009-12-12 20:03:14 +00001341 return is64Bit ? convertToThreeAddressWithLEA(MIOpc, MFI, MBBI, LV) : 0;
Evan Chengfa2c8282007-10-05 20:34:26 +00001342 assert(MI->getNumOperands() >= 2 && "Unknown dec instruction!");
Bill Wendling27b508d2009-02-11 21:51:19 +00001343 NewMI = addRegOffset(BuildMI(MF, MI->getDebugLoc(), get(X86::LEA16r))
Bill Wendlingf7b83c72009-05-13 21:33:08 +00001344 .addReg(Dest, RegState::Define |
1345 getDeadRegState(isDead)),
Evan Cheng7d98a482008-07-03 09:09:37 +00001346 Src, isKill, -1);
Evan Chengfa2c8282007-10-05 20:34:26 +00001347 break;
1348 case X86::ADD64rr:
Daniel Dunbar8f21f9c2010-10-08 02:07:32 +00001349 case X86::ADD32rr: {
Evan Chengfa2c8282007-10-05 20:34:26 +00001350 assert(MI->getNumOperands() >= 3 && "Unknown add instruction!");
Daniel Dunbar8f21f9c2010-10-08 02:07:32 +00001351 unsigned Opc = MIOpc == X86::ADD64rr ? X86::LEA64r
1352 : (is64Bit ? X86::LEA64_32r : X86::LEA32r);
Evan Cheng7d98a482008-07-03 09:09:37 +00001353 unsigned Src2 = MI->getOperand(2).getReg();
1354 bool isKill2 = MI->getOperand(2).isKill();
Jakob Stoklund Olesenb19bae42010-10-07 00:07:26 +00001355
1356 // LEA can't handle RSP.
1357 if (TargetRegisterInfo::isVirtualRegister(Src2) &&
Daniel Dunbar8f21f9c2010-10-08 02:07:32 +00001358 !MF.getRegInfo().constrainRegClass(Src2,
1359 MIOpc == X86::ADD64rr ? X86::GR64_NOSPRegisterClass :
1360 X86::GR32_NOSPRegisterClass))
Jakob Stoklund Olesenb19bae42010-10-07 00:07:26 +00001361 return 0;
1362
Bill Wendling27b508d2009-02-11 21:51:19 +00001363 NewMI = addRegReg(BuildMI(MF, MI->getDebugLoc(), get(Opc))
Bill Wendlingf7b83c72009-05-13 21:33:08 +00001364 .addReg(Dest, RegState::Define |
1365 getDeadRegState(isDead)),
Evan Cheng7d98a482008-07-03 09:09:37 +00001366 Src, isKill, Src2, isKill2);
1367 if (LV && isKill2)
1368 LV->replaceKillInstruction(Src2, MI, NewMI);
Evan Chengfa2c8282007-10-05 20:34:26 +00001369 break;
1370 }
Daniel Dunbar8f21f9c2010-10-08 02:07:32 +00001371 case X86::ADD16rr: {
Evan Cheng766a73f2009-12-11 06:01:48 +00001372 if (DisableLEA16)
Evan Cheng26fdd722009-12-12 20:03:14 +00001373 return is64Bit ? convertToThreeAddressWithLEA(MIOpc, MFI, MBBI, LV) : 0;
Evan Chengfa2c8282007-10-05 20:34:26 +00001374 assert(MI->getNumOperands() >= 3 && "Unknown add instruction!");
Evan Cheng7d98a482008-07-03 09:09:37 +00001375 unsigned Src2 = MI->getOperand(2).getReg();
1376 bool isKill2 = MI->getOperand(2).isKill();
Bill Wendling27b508d2009-02-11 21:51:19 +00001377 NewMI = addRegReg(BuildMI(MF, MI->getDebugLoc(), get(X86::LEA16r))
Bill Wendlingf7b83c72009-05-13 21:33:08 +00001378 .addReg(Dest, RegState::Define |
1379 getDeadRegState(isDead)),
Evan Cheng7d98a482008-07-03 09:09:37 +00001380 Src, isKill, Src2, isKill2);
1381 if (LV && isKill2)
1382 LV->replaceKillInstruction(Src2, MI, NewMI);
Evan Chengfa2c8282007-10-05 20:34:26 +00001383 break;
Evan Cheng7d98a482008-07-03 09:09:37 +00001384 }
Evan Chengfa2c8282007-10-05 20:34:26 +00001385 case X86::ADD64ri32:
1386 case X86::ADD64ri8:
1387 assert(MI->getNumOperands() >= 3 && "Unknown add instruction!");
Chris Lattnerf4693072010-07-08 23:46:44 +00001388 NewMI = addRegOffset(BuildMI(MF, MI->getDebugLoc(), get(X86::LEA64r))
Evan Cheng766a73f2009-12-11 06:01:48 +00001389 .addReg(Dest, RegState::Define |
1390 getDeadRegState(isDead)),
1391 Src, isKill, MI->getOperand(2).getImm());
Evan Chengfa2c8282007-10-05 20:34:26 +00001392 break;
1393 case X86::ADD32ri:
Daniel Dunbarefdf08b2010-10-08 02:07:26 +00001394 case X86::ADD32ri8: {
Evan Chengfa2c8282007-10-05 20:34:26 +00001395 assert(MI->getNumOperands() >= 3 && "Unknown add instruction!");
Evan Cheng766a73f2009-12-11 06:01:48 +00001396 unsigned Opc = is64Bit ? X86::LEA64_32r : X86::LEA32r;
Chris Lattnerf4693072010-07-08 23:46:44 +00001397 NewMI = addRegOffset(BuildMI(MF, MI->getDebugLoc(), get(Opc))
Evan Cheng766a73f2009-12-11 06:01:48 +00001398 .addReg(Dest, RegState::Define |
1399 getDeadRegState(isDead)),
Rafael Espindola3b2df102009-04-08 21:14:34 +00001400 Src, isKill, MI->getOperand(2).getImm());
Evan Chengfa2c8282007-10-05 20:34:26 +00001401 break;
1402 }
Evan Cheng766a73f2009-12-11 06:01:48 +00001403 case X86::ADD16ri:
1404 case X86::ADD16ri8:
1405 if (DisableLEA16)
Evan Cheng26fdd722009-12-12 20:03:14 +00001406 return is64Bit ? convertToThreeAddressWithLEA(MIOpc, MFI, MBBI, LV) : 0;
Evan Cheng766a73f2009-12-11 06:01:48 +00001407 assert(MI->getNumOperands() >= 3 && "Unknown add instruction!");
Chris Lattnerf4693072010-07-08 23:46:44 +00001408 NewMI = addRegOffset(BuildMI(MF, MI->getDebugLoc(), get(X86::LEA16r))
Evan Cheng766a73f2009-12-11 06:01:48 +00001409 .addReg(Dest, RegState::Define |
1410 getDeadRegState(isDead)),
1411 Src, isKill, MI->getOperand(2).getImm());
1412 break;
Evan Chengfa2c8282007-10-05 20:34:26 +00001413 }
1414 }
Chris Lattnerb7782d72005-01-02 02:37:07 +00001415 }
1416
Evan Cheng1bc1cae2008-02-07 08:29:53 +00001417 if (!NewMI) return 0;
1418
Evan Cheng7d98a482008-07-03 09:09:37 +00001419 if (LV) { // Update live variables
1420 if (isKill)
1421 LV->replaceKillInstruction(Src, MI, NewMI);
1422 if (isDead)
1423 LV->replaceKillInstruction(Dest, MI, NewMI);
1424 }
1425
Evan Chengfa2c8282007-10-05 20:34:26 +00001426 MFI->insert(MBBI, NewMI); // Insert the new inst
Evan Chengdc2c8742006-11-15 20:58:11 +00001427 return NewMI;
Chris Lattnerb7782d72005-01-02 02:37:07 +00001428}
1429
Chris Lattner29478012005-01-19 07:11:01 +00001430/// commuteInstruction - We have a few instructions that must be hacked on to
1431/// commute them.
1432///
Evan Cheng03553bb2008-06-16 07:33:11 +00001433MachineInstr *
1434X86InstrInfo::commuteInstruction(MachineInstr *MI, bool NewMI) const {
Chris Lattner29478012005-01-19 07:11:01 +00001435 switch (MI->getOpcode()) {
Chris Lattnerd54845f2005-01-19 07:31:24 +00001436 case X86::SHRD16rri8: // A = SHRD16rri8 B, C, I -> A = SHLD16rri8 C, B, (16-I)
1437 case X86::SHLD16rri8: // A = SHLD16rri8 B, C, I -> A = SHRD16rri8 C, B, (16-I)
Chris Lattner29478012005-01-19 07:11:01 +00001438 case X86::SHRD32rri8: // A = SHRD32rri8 B, C, I -> A = SHLD32rri8 C, B, (32-I)
Dan Gohman48ea03d2007-09-14 23:17:45 +00001439 case X86::SHLD32rri8: // A = SHLD32rri8 B, C, I -> A = SHRD32rri8 C, B, (32-I)
1440 case X86::SHRD64rri8: // A = SHRD64rri8 B, C, I -> A = SHLD64rri8 C, B, (64-I)
1441 case X86::SHLD64rri8:{// A = SHLD64rri8 B, C, I -> A = SHRD64rri8 C, B, (64-I)
Chris Lattnerd54845f2005-01-19 07:31:24 +00001442 unsigned Opc;
1443 unsigned Size;
1444 switch (MI->getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001445 default: llvm_unreachable("Unreachable!");
Chris Lattnerd54845f2005-01-19 07:31:24 +00001446 case X86::SHRD16rri8: Size = 16; Opc = X86::SHLD16rri8; break;
1447 case X86::SHLD16rri8: Size = 16; Opc = X86::SHRD16rri8; break;
1448 case X86::SHRD32rri8: Size = 32; Opc = X86::SHLD32rri8; break;
1449 case X86::SHLD32rri8: Size = 32; Opc = X86::SHRD32rri8; break;
Dan Gohman48ea03d2007-09-14 23:17:45 +00001450 case X86::SHRD64rri8: Size = 64; Opc = X86::SHLD64rri8; break;
1451 case X86::SHLD64rri8: Size = 64; Opc = X86::SHRD64rri8; break;
Chris Lattnerd54845f2005-01-19 07:31:24 +00001452 }
Chris Lattner5c463782007-12-30 20:49:49 +00001453 unsigned Amt = MI->getOperand(3).getImm();
Dan Gohmana39b0a12008-10-17 01:23:35 +00001454 if (NewMI) {
1455 MachineFunction &MF = *MI->getParent()->getParent();
1456 MI = MF.CloneMachineInstr(MI);
1457 NewMI = false;
Evan Cheng244183e2008-02-13 02:46:49 +00001458 }
Dan Gohmana39b0a12008-10-17 01:23:35 +00001459 MI->setDesc(get(Opc));
1460 MI->getOperand(3).setImm(Size-Amt);
1461 return TargetInstrInfoImpl::commuteInstruction(MI, NewMI);
Chris Lattner29478012005-01-19 07:11:01 +00001462 }
Evan Cheng1151ffd2007-10-05 23:13:21 +00001463 case X86::CMOVB16rr:
1464 case X86::CMOVB32rr:
1465 case X86::CMOVB64rr:
1466 case X86::CMOVAE16rr:
1467 case X86::CMOVAE32rr:
1468 case X86::CMOVAE64rr:
1469 case X86::CMOVE16rr:
1470 case X86::CMOVE32rr:
1471 case X86::CMOVE64rr:
1472 case X86::CMOVNE16rr:
1473 case X86::CMOVNE32rr:
1474 case X86::CMOVNE64rr:
Chris Lattner1a1c6002010-10-05 23:00:14 +00001475 case X86::CMOVBE16rr:
1476 case X86::CMOVBE32rr:
1477 case X86::CMOVBE64rr:
Evan Cheng1151ffd2007-10-05 23:13:21 +00001478 case X86::CMOVA16rr:
1479 case X86::CMOVA32rr:
1480 case X86::CMOVA64rr:
1481 case X86::CMOVL16rr:
1482 case X86::CMOVL32rr:
1483 case X86::CMOVL64rr:
1484 case X86::CMOVGE16rr:
1485 case X86::CMOVGE32rr:
1486 case X86::CMOVGE64rr:
1487 case X86::CMOVLE16rr:
1488 case X86::CMOVLE32rr:
1489 case X86::CMOVLE64rr:
1490 case X86::CMOVG16rr:
1491 case X86::CMOVG32rr:
1492 case X86::CMOVG64rr:
1493 case X86::CMOVS16rr:
1494 case X86::CMOVS32rr:
1495 case X86::CMOVS64rr:
1496 case X86::CMOVNS16rr:
1497 case X86::CMOVNS32rr:
1498 case X86::CMOVNS64rr:
1499 case X86::CMOVP16rr:
1500 case X86::CMOVP32rr:
1501 case X86::CMOVP64rr:
1502 case X86::CMOVNP16rr:
1503 case X86::CMOVNP32rr:
Dan Gohman7e47cc72009-01-07 00:35:10 +00001504 case X86::CMOVNP64rr:
1505 case X86::CMOVO16rr:
1506 case X86::CMOVO32rr:
1507 case X86::CMOVO64rr:
1508 case X86::CMOVNO16rr:
1509 case X86::CMOVNO32rr:
1510 case X86::CMOVNO64rr: {
Evan Cheng1151ffd2007-10-05 23:13:21 +00001511 unsigned Opc = 0;
1512 switch (MI->getOpcode()) {
1513 default: break;
1514 case X86::CMOVB16rr: Opc = X86::CMOVAE16rr; break;
1515 case X86::CMOVB32rr: Opc = X86::CMOVAE32rr; break;
1516 case X86::CMOVB64rr: Opc = X86::CMOVAE64rr; break;
1517 case X86::CMOVAE16rr: Opc = X86::CMOVB16rr; break;
1518 case X86::CMOVAE32rr: Opc = X86::CMOVB32rr; break;
1519 case X86::CMOVAE64rr: Opc = X86::CMOVB64rr; break;
1520 case X86::CMOVE16rr: Opc = X86::CMOVNE16rr; break;
1521 case X86::CMOVE32rr: Opc = X86::CMOVNE32rr; break;
1522 case X86::CMOVE64rr: Opc = X86::CMOVNE64rr; break;
1523 case X86::CMOVNE16rr: Opc = X86::CMOVE16rr; break;
1524 case X86::CMOVNE32rr: Opc = X86::CMOVE32rr; break;
1525 case X86::CMOVNE64rr: Opc = X86::CMOVE64rr; break;
Chris Lattner1a1c6002010-10-05 23:00:14 +00001526 case X86::CMOVBE16rr: Opc = X86::CMOVA16rr; break;
1527 case X86::CMOVBE32rr: Opc = X86::CMOVA32rr; break;
1528 case X86::CMOVBE64rr: Opc = X86::CMOVA64rr; break;
1529 case X86::CMOVA16rr: Opc = X86::CMOVBE16rr; break;
1530 case X86::CMOVA32rr: Opc = X86::CMOVBE32rr; break;
1531 case X86::CMOVA64rr: Opc = X86::CMOVBE64rr; break;
Evan Cheng1151ffd2007-10-05 23:13:21 +00001532 case X86::CMOVL16rr: Opc = X86::CMOVGE16rr; break;
1533 case X86::CMOVL32rr: Opc = X86::CMOVGE32rr; break;
1534 case X86::CMOVL64rr: Opc = X86::CMOVGE64rr; break;
1535 case X86::CMOVGE16rr: Opc = X86::CMOVL16rr; break;
1536 case X86::CMOVGE32rr: Opc = X86::CMOVL32rr; break;
1537 case X86::CMOVGE64rr: Opc = X86::CMOVL64rr; break;
1538 case X86::CMOVLE16rr: Opc = X86::CMOVG16rr; break;
1539 case X86::CMOVLE32rr: Opc = X86::CMOVG32rr; break;
1540 case X86::CMOVLE64rr: Opc = X86::CMOVG64rr; break;
1541 case X86::CMOVG16rr: Opc = X86::CMOVLE16rr; break;
1542 case X86::CMOVG32rr: Opc = X86::CMOVLE32rr; break;
1543 case X86::CMOVG64rr: Opc = X86::CMOVLE64rr; break;
1544 case X86::CMOVS16rr: Opc = X86::CMOVNS16rr; break;
1545 case X86::CMOVS32rr: Opc = X86::CMOVNS32rr; break;
Mon P Wang6c8bcf92009-04-18 05:16:01 +00001546 case X86::CMOVS64rr: Opc = X86::CMOVNS64rr; break;
Evan Cheng1151ffd2007-10-05 23:13:21 +00001547 case X86::CMOVNS16rr: Opc = X86::CMOVS16rr; break;
1548 case X86::CMOVNS32rr: Opc = X86::CMOVS32rr; break;
1549 case X86::CMOVNS64rr: Opc = X86::CMOVS64rr; break;
1550 case X86::CMOVP16rr: Opc = X86::CMOVNP16rr; break;
1551 case X86::CMOVP32rr: Opc = X86::CMOVNP32rr; break;
Mon P Wang6c8bcf92009-04-18 05:16:01 +00001552 case X86::CMOVP64rr: Opc = X86::CMOVNP64rr; break;
Evan Cheng1151ffd2007-10-05 23:13:21 +00001553 case X86::CMOVNP16rr: Opc = X86::CMOVP16rr; break;
1554 case X86::CMOVNP32rr: Opc = X86::CMOVP32rr; break;
1555 case X86::CMOVNP64rr: Opc = X86::CMOVP64rr; break;
Dan Gohman7e47cc72009-01-07 00:35:10 +00001556 case X86::CMOVO16rr: Opc = X86::CMOVNO16rr; break;
1557 case X86::CMOVO32rr: Opc = X86::CMOVNO32rr; break;
Mon P Wang6c8bcf92009-04-18 05:16:01 +00001558 case X86::CMOVO64rr: Opc = X86::CMOVNO64rr; break;
Dan Gohman7e47cc72009-01-07 00:35:10 +00001559 case X86::CMOVNO16rr: Opc = X86::CMOVO16rr; break;
1560 case X86::CMOVNO32rr: Opc = X86::CMOVO32rr; break;
1561 case X86::CMOVNO64rr: Opc = X86::CMOVO64rr; break;
Evan Cheng1151ffd2007-10-05 23:13:21 +00001562 }
Dan Gohmana39b0a12008-10-17 01:23:35 +00001563 if (NewMI) {
1564 MachineFunction &MF = *MI->getParent()->getParent();
1565 MI = MF.CloneMachineInstr(MI);
1566 NewMI = false;
1567 }
Chris Lattner59687512008-01-11 18:10:50 +00001568 MI->setDesc(get(Opc));
Evan Cheng1151ffd2007-10-05 23:13:21 +00001569 // Fallthrough intended.
1570 }
Chris Lattner29478012005-01-19 07:11:01 +00001571 default:
Evan Cheng03553bb2008-06-16 07:33:11 +00001572 return TargetInstrInfoImpl::commuteInstruction(MI, NewMI);
Chris Lattner29478012005-01-19 07:11:01 +00001573 }
1574}
1575
Chris Lattnerc0fb5672006-10-20 17:42:20 +00001576static X86::CondCode GetCondFromBranchOpc(unsigned BrOpc) {
1577 switch (BrOpc) {
1578 default: return X86::COND_INVALID;
Chris Lattner2b0a7a22010-02-11 19:25:55 +00001579 case X86::JE_4: return X86::COND_E;
1580 case X86::JNE_4: return X86::COND_NE;
1581 case X86::JL_4: return X86::COND_L;
1582 case X86::JLE_4: return X86::COND_LE;
1583 case X86::JG_4: return X86::COND_G;
1584 case X86::JGE_4: return X86::COND_GE;
1585 case X86::JB_4: return X86::COND_B;
1586 case X86::JBE_4: return X86::COND_BE;
1587 case X86::JA_4: return X86::COND_A;
1588 case X86::JAE_4: return X86::COND_AE;
1589 case X86::JS_4: return X86::COND_S;
1590 case X86::JNS_4: return X86::COND_NS;
1591 case X86::JP_4: return X86::COND_P;
1592 case X86::JNP_4: return X86::COND_NP;
1593 case X86::JO_4: return X86::COND_O;
1594 case X86::JNO_4: return X86::COND_NO;
Chris Lattnerc0fb5672006-10-20 17:42:20 +00001595 }
1596}
1597
1598unsigned X86::GetCondBranchFromCond(X86::CondCode CC) {
1599 switch (CC) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001600 default: llvm_unreachable("Illegal condition code!");
Chris Lattner2b0a7a22010-02-11 19:25:55 +00001601 case X86::COND_E: return X86::JE_4;
1602 case X86::COND_NE: return X86::JNE_4;
1603 case X86::COND_L: return X86::JL_4;
1604 case X86::COND_LE: return X86::JLE_4;
1605 case X86::COND_G: return X86::JG_4;
1606 case X86::COND_GE: return X86::JGE_4;
1607 case X86::COND_B: return X86::JB_4;
1608 case X86::COND_BE: return X86::JBE_4;
1609 case X86::COND_A: return X86::JA_4;
1610 case X86::COND_AE: return X86::JAE_4;
1611 case X86::COND_S: return X86::JS_4;
1612 case X86::COND_NS: return X86::JNS_4;
1613 case X86::COND_P: return X86::JP_4;
1614 case X86::COND_NP: return X86::JNP_4;
1615 case X86::COND_O: return X86::JO_4;
1616 case X86::COND_NO: return X86::JNO_4;
Chris Lattnerc0fb5672006-10-20 17:42:20 +00001617 }
1618}
1619
Chris Lattner3a897f32006-10-21 05:52:40 +00001620/// GetOppositeBranchCondition - Return the inverse of the specified condition,
1621/// e.g. turning COND_E to COND_NE.
1622X86::CondCode X86::GetOppositeBranchCondition(X86::CondCode CC) {
1623 switch (CC) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001624 default: llvm_unreachable("Illegal condition code!");
Chris Lattner3a897f32006-10-21 05:52:40 +00001625 case X86::COND_E: return X86::COND_NE;
1626 case X86::COND_NE: return X86::COND_E;
1627 case X86::COND_L: return X86::COND_GE;
1628 case X86::COND_LE: return X86::COND_G;
1629 case X86::COND_G: return X86::COND_LE;
1630 case X86::COND_GE: return X86::COND_L;
1631 case X86::COND_B: return X86::COND_AE;
1632 case X86::COND_BE: return X86::COND_A;
1633 case X86::COND_A: return X86::COND_BE;
1634 case X86::COND_AE: return X86::COND_B;
1635 case X86::COND_S: return X86::COND_NS;
1636 case X86::COND_NS: return X86::COND_S;
1637 case X86::COND_P: return X86::COND_NP;
1638 case X86::COND_NP: return X86::COND_P;
1639 case X86::COND_O: return X86::COND_NO;
1640 case X86::COND_NO: return X86::COND_O;
1641 }
1642}
1643
Dale Johannesen616627b2007-06-14 22:03:45 +00001644bool X86InstrInfo::isUnpredicatedTerminator(const MachineInstr *MI) const {
Chris Lattner03ad8852008-01-07 07:27:27 +00001645 const TargetInstrDesc &TID = MI->getDesc();
1646 if (!TID.isTerminator()) return false;
Chris Lattnera98c6792008-01-07 01:56:04 +00001647
1648 // Conditional branch is a special case.
Chris Lattner03ad8852008-01-07 07:27:27 +00001649 if (TID.isBranch() && !TID.isBarrier())
Chris Lattnera98c6792008-01-07 01:56:04 +00001650 return true;
Chris Lattner03ad8852008-01-07 07:27:27 +00001651 if (!TID.isPredicable())
Chris Lattnera98c6792008-01-07 01:56:04 +00001652 return true;
1653 return !isPredicated(MI);
Dale Johannesen616627b2007-06-14 22:03:45 +00001654}
Chris Lattner3a897f32006-10-21 05:52:40 +00001655
Chris Lattnerc0fb5672006-10-20 17:42:20 +00001656bool X86InstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,
1657 MachineBasicBlock *&TBB,
1658 MachineBasicBlock *&FBB,
Evan Cheng64dfcac2009-02-09 07:14:22 +00001659 SmallVectorImpl<MachineOperand> &Cond,
1660 bool AllowModify) const {
Dan Gohman97d95d62008-10-21 03:29:32 +00001661 // Start from the bottom of the block and work up, examining the
1662 // terminator instructions.
Chris Lattnerc0fb5672006-10-20 17:42:20 +00001663 MachineBasicBlock::iterator I = MBB.end();
Evan Cheng4ca4bc62010-04-13 18:50:27 +00001664 MachineBasicBlock::iterator UnCondBrIter = MBB.end();
Dan Gohman97d95d62008-10-21 03:29:32 +00001665 while (I != MBB.begin()) {
1666 --I;
Dale Johannesen4244d122010-04-02 01:38:09 +00001667 if (I->isDebugValue())
1668 continue;
Bill Wendling277381f2009-12-14 06:51:19 +00001669
1670 // Working from the bottom, when we see a non-terminator instruction, we're
1671 // done.
Jakob Stoklund Olesenc30b4dd2010-07-16 17:41:44 +00001672 if (!isUnpredicatedTerminator(I))
Dan Gohman97d95d62008-10-21 03:29:32 +00001673 break;
Bill Wendling277381f2009-12-14 06:51:19 +00001674
1675 // A terminator that isn't a branch can't easily be handled by this
1676 // analysis.
Dan Gohman97d95d62008-10-21 03:29:32 +00001677 if (!I->getDesc().isBranch())
Chris Lattnerc0fb5672006-10-20 17:42:20 +00001678 return true;
Bill Wendling277381f2009-12-14 06:51:19 +00001679
Dan Gohman97d95d62008-10-21 03:29:32 +00001680 // Handle unconditional branches.
Chris Lattner2b0a7a22010-02-11 19:25:55 +00001681 if (I->getOpcode() == X86::JMP_4) {
Evan Cheng4ca4bc62010-04-13 18:50:27 +00001682 UnCondBrIter = I;
1683
Evan Cheng64dfcac2009-02-09 07:14:22 +00001684 if (!AllowModify) {
1685 TBB = I->getOperand(0).getMBB();
Evan Cheng2fa28112009-05-08 06:34:09 +00001686 continue;
Evan Cheng64dfcac2009-02-09 07:14:22 +00001687 }
1688
Dan Gohman97d95d62008-10-21 03:29:32 +00001689 // If the block has any instructions after a JMP, delete them.
Chris Lattnera48f44d2009-12-03 00:50:42 +00001690 while (llvm::next(I) != MBB.end())
1691 llvm::next(I)->eraseFromParent();
Bill Wendling277381f2009-12-14 06:51:19 +00001692
Dan Gohman97d95d62008-10-21 03:29:32 +00001693 Cond.clear();
1694 FBB = 0;
Bill Wendling277381f2009-12-14 06:51:19 +00001695
Dan Gohman97d95d62008-10-21 03:29:32 +00001696 // Delete the JMP if it's equivalent to a fall-through.
1697 if (MBB.isLayoutSuccessor(I->getOperand(0).getMBB())) {
1698 TBB = 0;
1699 I->eraseFromParent();
1700 I = MBB.end();
Evan Cheng4ca4bc62010-04-13 18:50:27 +00001701 UnCondBrIter = MBB.end();
Dan Gohman97d95d62008-10-21 03:29:32 +00001702 continue;
1703 }
Bill Wendling277381f2009-12-14 06:51:19 +00001704
Evan Cheng4ca4bc62010-04-13 18:50:27 +00001705 // TBB is used to indicate the unconditional destination.
Dan Gohman97d95d62008-10-21 03:29:32 +00001706 TBB = I->getOperand(0).getMBB();
1707 continue;
Chris Lattnerc0fb5672006-10-20 17:42:20 +00001708 }
Bill Wendling277381f2009-12-14 06:51:19 +00001709
Dan Gohman97d95d62008-10-21 03:29:32 +00001710 // Handle conditional branches.
1711 X86::CondCode BranchCode = GetCondFromBranchOpc(I->getOpcode());
Chris Lattnerc0fb5672006-10-20 17:42:20 +00001712 if (BranchCode == X86::COND_INVALID)
1713 return true; // Can't handle indirect branch.
Bill Wendling277381f2009-12-14 06:51:19 +00001714
Dan Gohman97d95d62008-10-21 03:29:32 +00001715 // Working from the bottom, handle the first conditional branch.
1716 if (Cond.empty()) {
Evan Cheng4ca4bc62010-04-13 18:50:27 +00001717 MachineBasicBlock *TargetBB = I->getOperand(0).getMBB();
1718 if (AllowModify && UnCondBrIter != MBB.end() &&
1719 MBB.isLayoutSuccessor(TargetBB)) {
1720 // If we can modify the code and it ends in something like:
1721 //
1722 // jCC L1
1723 // jmp L2
1724 // L1:
1725 // ...
1726 // L2:
1727 //
1728 // Then we can change this to:
1729 //
1730 // jnCC L2
1731 // L1:
1732 // ...
1733 // L2:
1734 //
1735 // Which is a bit more efficient.
1736 // We conditionally jump to the fall-through block.
1737 BranchCode = GetOppositeBranchCondition(BranchCode);
1738 unsigned JNCC = GetCondBranchFromCond(BranchCode);
1739 MachineBasicBlock::iterator OldInst = I;
1740
1741 BuildMI(MBB, UnCondBrIter, MBB.findDebugLoc(I), get(JNCC))
1742 .addMBB(UnCondBrIter->getOperand(0).getMBB());
1743 BuildMI(MBB, UnCondBrIter, MBB.findDebugLoc(I), get(X86::JMP_4))
1744 .addMBB(TargetBB);
1745 MBB.addSuccessor(TargetBB);
1746
1747 OldInst->eraseFromParent();
1748 UnCondBrIter->eraseFromParent();
1749
1750 // Restart the analysis.
1751 UnCondBrIter = MBB.end();
1752 I = MBB.end();
1753 continue;
1754 }
1755
Dan Gohman97d95d62008-10-21 03:29:32 +00001756 FBB = TBB;
1757 TBB = I->getOperand(0).getMBB();
1758 Cond.push_back(MachineOperand::CreateImm(BranchCode));
1759 continue;
1760 }
Bill Wendling277381f2009-12-14 06:51:19 +00001761
1762 // Handle subsequent conditional branches. Only handle the case where all
1763 // conditional branches branch to the same destination and their condition
1764 // opcodes fit one of the special multi-branch idioms.
Dan Gohman97d95d62008-10-21 03:29:32 +00001765 assert(Cond.size() == 1);
1766 assert(TBB);
Bill Wendling277381f2009-12-14 06:51:19 +00001767
1768 // Only handle the case where all conditional branches branch to the same
1769 // destination.
Dan Gohman97d95d62008-10-21 03:29:32 +00001770 if (TBB != I->getOperand(0).getMBB())
1771 return true;
Bill Wendling277381f2009-12-14 06:51:19 +00001772
Dan Gohman97d95d62008-10-21 03:29:32 +00001773 // If the conditions are the same, we can leave them alone.
Bill Wendling277381f2009-12-14 06:51:19 +00001774 X86::CondCode OldBranchCode = (X86::CondCode)Cond[0].getImm();
Dan Gohman97d95d62008-10-21 03:29:32 +00001775 if (OldBranchCode == BranchCode)
1776 continue;
Bill Wendling277381f2009-12-14 06:51:19 +00001777
1778 // If they differ, see if they fit one of the known patterns. Theoretically,
1779 // we could handle more patterns here, but we shouldn't expect to see them
1780 // if instruction selection has done a reasonable job.
Dan Gohman97d95d62008-10-21 03:29:32 +00001781 if ((OldBranchCode == X86::COND_NP &&
1782 BranchCode == X86::COND_E) ||
1783 (OldBranchCode == X86::COND_E &&
1784 BranchCode == X86::COND_NP))
1785 BranchCode = X86::COND_NP_OR_E;
1786 else if ((OldBranchCode == X86::COND_P &&
1787 BranchCode == X86::COND_NE) ||
1788 (OldBranchCode == X86::COND_NE &&
1789 BranchCode == X86::COND_P))
1790 BranchCode = X86::COND_NE_OR_P;
1791 else
1792 return true;
Bill Wendling277381f2009-12-14 06:51:19 +00001793
Dan Gohman97d95d62008-10-21 03:29:32 +00001794 // Update the MachineOperand.
1795 Cond[0].setImm(BranchCode);
Chris Lattner74436002006-10-30 22:27:23 +00001796 }
Chris Lattnerc0fb5672006-10-20 17:42:20 +00001797
Dan Gohman97d95d62008-10-21 03:29:32 +00001798 return false;
Chris Lattnerc0fb5672006-10-20 17:42:20 +00001799}
1800
Evan Chenge20dd922007-05-18 00:18:17 +00001801unsigned X86InstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
Chris Lattnerc0fb5672006-10-20 17:42:20 +00001802 MachineBasicBlock::iterator I = MBB.end();
Dan Gohman97d95d62008-10-21 03:29:32 +00001803 unsigned Count = 0;
1804
1805 while (I != MBB.begin()) {
1806 --I;
Dale Johannesen4244d122010-04-02 01:38:09 +00001807 if (I->isDebugValue())
1808 continue;
Chris Lattner2b0a7a22010-02-11 19:25:55 +00001809 if (I->getOpcode() != X86::JMP_4 &&
Dan Gohman97d95d62008-10-21 03:29:32 +00001810 GetCondFromBranchOpc(I->getOpcode()) == X86::COND_INVALID)
1811 break;
1812 // Remove the branch.
1813 I->eraseFromParent();
1814 I = MBB.end();
1815 ++Count;
1816 }
Chris Lattnerc0fb5672006-10-20 17:42:20 +00001817
Dan Gohman97d95d62008-10-21 03:29:32 +00001818 return Count;
Chris Lattnerc0fb5672006-10-20 17:42:20 +00001819}
1820
Evan Chenge20dd922007-05-18 00:18:17 +00001821unsigned
1822X86InstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
1823 MachineBasicBlock *FBB,
Stuart Hastings0125b642010-06-17 22:43:56 +00001824 const SmallVectorImpl<MachineOperand> &Cond,
1825 DebugLoc DL) const {
Chris Lattnerc0fb5672006-10-20 17:42:20 +00001826 // Shouldn't be a fall through.
1827 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
Chris Lattner6fca75e2006-10-21 05:34:23 +00001828 assert((Cond.size() == 1 || Cond.size() == 0) &&
1829 "X86 branch conditions have one component!");
1830
Dan Gohman97d95d62008-10-21 03:29:32 +00001831 if (Cond.empty()) {
1832 // Unconditional branch?
1833 assert(!FBB && "Unconditional branch with multiple successors!");
Stuart Hastings0125b642010-06-17 22:43:56 +00001834 BuildMI(&MBB, DL, get(X86::JMP_4)).addMBB(TBB);
Evan Chenge20dd922007-05-18 00:18:17 +00001835 return 1;
Chris Lattnerc0fb5672006-10-20 17:42:20 +00001836 }
Dan Gohman97d95d62008-10-21 03:29:32 +00001837
1838 // Conditional branch.
1839 unsigned Count = 0;
1840 X86::CondCode CC = (X86::CondCode)Cond[0].getImm();
1841 switch (CC) {
1842 case X86::COND_NP_OR_E:
1843 // Synthesize NP_OR_E with two branches.
Stuart Hastings0125b642010-06-17 22:43:56 +00001844 BuildMI(&MBB, DL, get(X86::JNP_4)).addMBB(TBB);
Bill Wendling543ce1f2010-03-05 00:33:59 +00001845 ++Count;
Stuart Hastings0125b642010-06-17 22:43:56 +00001846 BuildMI(&MBB, DL, get(X86::JE_4)).addMBB(TBB);
Bill Wendling543ce1f2010-03-05 00:33:59 +00001847 ++Count;
Dan Gohman97d95d62008-10-21 03:29:32 +00001848 break;
1849 case X86::COND_NE_OR_P:
1850 // Synthesize NE_OR_P with two branches.
Stuart Hastings0125b642010-06-17 22:43:56 +00001851 BuildMI(&MBB, DL, get(X86::JNE_4)).addMBB(TBB);
Bill Wendling543ce1f2010-03-05 00:33:59 +00001852 ++Count;
Stuart Hastings0125b642010-06-17 22:43:56 +00001853 BuildMI(&MBB, DL, get(X86::JP_4)).addMBB(TBB);
Bill Wendling543ce1f2010-03-05 00:33:59 +00001854 ++Count;
Dan Gohman97d95d62008-10-21 03:29:32 +00001855 break;
Bill Wendling543ce1f2010-03-05 00:33:59 +00001856 default: {
1857 unsigned Opc = GetCondBranchFromCond(CC);
Stuart Hastings0125b642010-06-17 22:43:56 +00001858 BuildMI(&MBB, DL, get(Opc)).addMBB(TBB);
Bill Wendling543ce1f2010-03-05 00:33:59 +00001859 ++Count;
Dan Gohman97d95d62008-10-21 03:29:32 +00001860 }
Bill Wendling543ce1f2010-03-05 00:33:59 +00001861 }
Dan Gohman97d95d62008-10-21 03:29:32 +00001862 if (FBB) {
1863 // Two-way Conditional branch. Insert the second branch.
Stuart Hastings0125b642010-06-17 22:43:56 +00001864 BuildMI(&MBB, DL, get(X86::JMP_4)).addMBB(FBB);
Dan Gohman97d95d62008-10-21 03:29:32 +00001865 ++Count;
1866 }
1867 return Count;
Chris Lattnerc0fb5672006-10-20 17:42:20 +00001868}
1869
Dan Gohman7913ea52009-04-15 00:04:23 +00001870/// isHReg - Test if the given register is a physical h register.
1871static bool isHReg(unsigned Reg) {
Dan Gohman29869722009-04-27 16:41:36 +00001872 return X86::GR8_ABCD_HRegClass.contains(Reg);
Dan Gohman7913ea52009-04-15 00:04:23 +00001873}
1874
Anton Korobeynikovc0b36922010-08-27 14:43:06 +00001875// Try and copy between VR128/VR64 and GR64 registers.
1876static unsigned CopyToFromAsymmetricReg(unsigned DestReg, unsigned SrcReg) {
1877 // SrcReg(VR128) -> DestReg(GR64)
1878 // SrcReg(VR64) -> DestReg(GR64)
1879 // SrcReg(GR64) -> DestReg(VR128)
1880 // SrcReg(GR64) -> DestReg(VR64)
1881
1882 if (X86::GR64RegClass.contains(DestReg)) {
1883 if (X86::VR128RegClass.contains(SrcReg)) {
1884 // Copy from a VR128 register to a GR64 register.
1885 return X86::MOVPQIto64rr;
1886 } else if (X86::VR64RegClass.contains(SrcReg)) {
1887 // Copy from a VR64 register to a GR64 register.
1888 return X86::MOVSDto64rr;
1889 }
1890 } else if (X86::GR64RegClass.contains(SrcReg)) {
1891 // Copy from a GR64 register to a VR128 register.
1892 if (X86::VR128RegClass.contains(DestReg))
1893 return X86::MOV64toPQIrr;
1894 // Copy from a GR64 register to a VR64 register.
1895 else if (X86::VR64RegClass.contains(DestReg))
1896 return X86::MOV64toSDrr;
1897 }
1898
1899 return 0;
1900}
1901
Jakob Stoklund Olesen930f8082010-07-08 19:46:25 +00001902void X86InstrInfo::copyPhysReg(MachineBasicBlock &MBB,
1903 MachineBasicBlock::iterator MI, DebugLoc DL,
1904 unsigned DestReg, unsigned SrcReg,
1905 bool KillSrc) const {
1906 // First deal with the normal symmetric copies.
1907 unsigned Opc = 0;
1908 if (X86::GR64RegClass.contains(DestReg, SrcReg))
1909 Opc = X86::MOV64rr;
1910 else if (X86::GR32RegClass.contains(DestReg, SrcReg))
1911 Opc = X86::MOV32rr;
1912 else if (X86::GR16RegClass.contains(DestReg, SrcReg))
1913 Opc = X86::MOV16rr;
1914 else if (X86::GR8RegClass.contains(DestReg, SrcReg)) {
1915 // Copying to or from a physical H register on x86-64 requires a NOREX
1916 // move. Otherwise use a normal move.
1917 if ((isHReg(DestReg) || isHReg(SrcReg)) &&
1918 TM.getSubtarget<X86Subtarget>().is64Bit())
1919 Opc = X86::MOV8rr_NOREX;
1920 else
1921 Opc = X86::MOV8rr;
1922 } else if (X86::VR128RegClass.contains(DestReg, SrcReg))
1923 Opc = X86::MOVAPSrr;
Jakob Stoklund Olesenec58a432010-07-08 22:30:35 +00001924 else if (X86::VR64RegClass.contains(DestReg, SrcReg))
1925 Opc = X86::MMX_MOVQ64rr;
Anton Korobeynikovc0b36922010-08-27 14:43:06 +00001926 else
1927 Opc = CopyToFromAsymmetricReg(DestReg, SrcReg);
Jakob Stoklund Olesen930f8082010-07-08 19:46:25 +00001928
1929 if (Opc) {
1930 BuildMI(MBB, MI, DL, get(Opc), DestReg)
1931 .addReg(SrcReg, getKillRegState(KillSrc));
1932 return;
1933 }
1934
1935 // Moving EFLAGS to / from another register requires a push and a pop.
1936 if (SrcReg == X86::EFLAGS) {
1937 if (X86::GR64RegClass.contains(DestReg)) {
1938 BuildMI(MBB, MI, DL, get(X86::PUSHF64));
1939 BuildMI(MBB, MI, DL, get(X86::POP64r), DestReg);
1940 return;
1941 } else if (X86::GR32RegClass.contains(DestReg)) {
1942 BuildMI(MBB, MI, DL, get(X86::PUSHF32));
1943 BuildMI(MBB, MI, DL, get(X86::POP32r), DestReg);
1944 return;
1945 }
1946 }
1947 if (DestReg == X86::EFLAGS) {
1948 if (X86::GR64RegClass.contains(SrcReg)) {
1949 BuildMI(MBB, MI, DL, get(X86::PUSH64r))
1950 .addReg(SrcReg, getKillRegState(KillSrc));
1951 BuildMI(MBB, MI, DL, get(X86::POPF64));
1952 return;
1953 } else if (X86::GR32RegClass.contains(SrcReg)) {
1954 BuildMI(MBB, MI, DL, get(X86::PUSH32r))
1955 .addReg(SrcReg, getKillRegState(KillSrc));
1956 BuildMI(MBB, MI, DL, get(X86::POPF32));
1957 return;
1958 }
1959 }
1960
1961 DEBUG(dbgs() << "Cannot copy " << RI.getName(SrcReg)
1962 << " to " << RI.getName(DestReg) << '\n');
1963 llvm_unreachable("Cannot emit physreg copy instruction");
1964}
1965
Rafael Espindolae302f832010-06-12 20:13:29 +00001966static unsigned getLoadStoreRegOpcode(unsigned Reg,
1967 const TargetRegisterClass *RC,
1968 bool isStackAligned,
1969 const TargetMachine &TM,
1970 bool load) {
Rafael Espindola6635f982010-07-12 03:43:04 +00001971 switch (RC->getID()) {
1972 default:
1973 llvm_unreachable("Unknown regclass");
1974 case X86::GR64RegClassID:
1975 case X86::GR64_NOSPRegClassID:
Rafael Espindolae302f832010-06-12 20:13:29 +00001976 return load ? X86::MOV64rm : X86::MOV64mr;
Rafael Espindola6635f982010-07-12 03:43:04 +00001977 case X86::GR32RegClassID:
1978 case X86::GR32_NOSPRegClassID:
1979 case X86::GR32_ADRegClassID:
Rafael Espindolae302f832010-06-12 20:13:29 +00001980 return load ? X86::MOV32rm : X86::MOV32mr;
Rafael Espindola6635f982010-07-12 03:43:04 +00001981 case X86::GR16RegClassID:
Rafael Espindolae302f832010-06-12 20:13:29 +00001982 return load ? X86::MOV16rm : X86::MOV16mr;
Rafael Espindola6635f982010-07-12 03:43:04 +00001983 case X86::GR8RegClassID:
Rafael Espindolae302f832010-06-12 20:13:29 +00001984 // Copying to or from a physical H register on x86-64 requires a NOREX
1985 // move. Otherwise use a normal move.
1986 if (isHReg(Reg) &&
1987 TM.getSubtarget<X86Subtarget>().is64Bit())
1988 return load ? X86::MOV8rm_NOREX : X86::MOV8mr_NOREX;
1989 else
1990 return load ? X86::MOV8rm : X86::MOV8mr;
Rafael Espindola6635f982010-07-12 03:43:04 +00001991 case X86::GR64_ABCDRegClassID:
Rafael Espindolae302f832010-06-12 20:13:29 +00001992 return load ? X86::MOV64rm : X86::MOV64mr;
Rafael Espindola6635f982010-07-12 03:43:04 +00001993 case X86::GR32_ABCDRegClassID:
Rafael Espindolae302f832010-06-12 20:13:29 +00001994 return load ? X86::MOV32rm : X86::MOV32mr;
Rafael Espindola6635f982010-07-12 03:43:04 +00001995 case X86::GR16_ABCDRegClassID:
Rafael Espindolae302f832010-06-12 20:13:29 +00001996 return load ? X86::MOV16rm : X86::MOV16mr;
Rafael Espindola6635f982010-07-12 03:43:04 +00001997 case X86::GR8_ABCD_LRegClassID:
Rafael Espindolae302f832010-06-12 20:13:29 +00001998 return load ? X86::MOV8rm :X86::MOV8mr;
Rafael Espindola6635f982010-07-12 03:43:04 +00001999 case X86::GR8_ABCD_HRegClassID:
Rafael Espindolae302f832010-06-12 20:13:29 +00002000 if (TM.getSubtarget<X86Subtarget>().is64Bit())
2001 return load ? X86::MOV8rm_NOREX : X86::MOV8mr_NOREX;
2002 else
2003 return load ? X86::MOV8rm : X86::MOV8mr;
Rafael Espindola6635f982010-07-12 03:43:04 +00002004 case X86::GR64_NOREXRegClassID:
2005 case X86::GR64_NOREX_NOSPRegClassID:
Rafael Espindolae302f832010-06-12 20:13:29 +00002006 return load ? X86::MOV64rm : X86::MOV64mr;
Rafael Espindola6635f982010-07-12 03:43:04 +00002007 case X86::GR32_NOREXRegClassID:
Rafael Espindolae302f832010-06-12 20:13:29 +00002008 return load ? X86::MOV32rm : X86::MOV32mr;
Rafael Espindola6635f982010-07-12 03:43:04 +00002009 case X86::GR16_NOREXRegClassID:
Rafael Espindolae302f832010-06-12 20:13:29 +00002010 return load ? X86::MOV16rm : X86::MOV16mr;
Rafael Espindola6635f982010-07-12 03:43:04 +00002011 case X86::GR8_NOREXRegClassID:
Rafael Espindolae302f832010-06-12 20:13:29 +00002012 return load ? X86::MOV8rm : X86::MOV8mr;
Rafael Espindola6635f982010-07-12 03:43:04 +00002013 case X86::GR64_TCRegClassID:
Rafael Espindolae302f832010-06-12 20:13:29 +00002014 return load ? X86::MOV64rm_TC : X86::MOV64mr_TC;
Rafael Espindola6635f982010-07-12 03:43:04 +00002015 case X86::GR32_TCRegClassID:
Rafael Espindolae302f832010-06-12 20:13:29 +00002016 return load ? X86::MOV32rm_TC : X86::MOV32mr_TC;
Rafael Espindola6635f982010-07-12 03:43:04 +00002017 case X86::RFP80RegClassID:
Rafael Espindolae302f832010-06-12 20:13:29 +00002018 return load ? X86::LD_Fp80m : X86::ST_FpP80m;
Rafael Espindola6635f982010-07-12 03:43:04 +00002019 case X86::RFP64RegClassID:
Rafael Espindolae302f832010-06-12 20:13:29 +00002020 return load ? X86::LD_Fp64m : X86::ST_Fp64m;
Rafael Espindola6635f982010-07-12 03:43:04 +00002021 case X86::RFP32RegClassID:
Rafael Espindolae302f832010-06-12 20:13:29 +00002022 return load ? X86::LD_Fp32m : X86::ST_Fp32m;
Rafael Espindola6635f982010-07-12 03:43:04 +00002023 case X86::FR32RegClassID:
Rafael Espindolae302f832010-06-12 20:13:29 +00002024 return load ? X86::MOVSSrm : X86::MOVSSmr;
Rafael Espindola6635f982010-07-12 03:43:04 +00002025 case X86::FR64RegClassID:
Rafael Espindolae302f832010-06-12 20:13:29 +00002026 return load ? X86::MOVSDrm : X86::MOVSDmr;
Rafael Espindola6635f982010-07-12 03:43:04 +00002027 case X86::VR128RegClassID:
Rafael Espindolae302f832010-06-12 20:13:29 +00002028 // If stack is realigned we can use aligned stores.
2029 if (isStackAligned)
2030 return load ? X86::MOVAPSrm : X86::MOVAPSmr;
2031 else
2032 return load ? X86::MOVUPSrm : X86::MOVUPSmr;
Rafael Espindola6635f982010-07-12 03:43:04 +00002033 case X86::VR64RegClassID:
Rafael Espindolae302f832010-06-12 20:13:29 +00002034 return load ? X86::MMX_MOVQ64rm : X86::MMX_MOVQ64mr;
Rafael Espindolae302f832010-06-12 20:13:29 +00002035 }
2036}
2037
Dan Gohman29869722009-04-27 16:41:36 +00002038static unsigned getStoreRegOpcode(unsigned SrcReg,
2039 const TargetRegisterClass *RC,
2040 bool isStackAligned,
2041 TargetMachine &TM) {
Rafael Espindolae302f832010-06-12 20:13:29 +00002042 return getLoadStoreRegOpcode(SrcReg, RC, isStackAligned, TM, false);
2043}
Owen Andersoneee14602008-01-01 21:11:32 +00002044
Rafael Espindolae302f832010-06-12 20:13:29 +00002045
2046static unsigned getLoadRegOpcode(unsigned DestReg,
2047 const TargetRegisterClass *RC,
2048 bool isStackAligned,
2049 const TargetMachine &TM) {
2050 return getLoadStoreRegOpcode(DestReg, RC, isStackAligned, TM, true);
Owen Andersoneee14602008-01-01 21:11:32 +00002051}
2052
2053void X86InstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
2054 MachineBasicBlock::iterator MI,
2055 unsigned SrcReg, bool isKill, int FrameIdx,
Evan Chengefb126a2010-05-06 19:06:44 +00002056 const TargetRegisterClass *RC,
2057 const TargetRegisterInfo *TRI) const {
Anton Korobeynikovb7a49922008-07-19 06:30:51 +00002058 const MachineFunction &MF = *MBB.getParent();
Jakob Stoklund Olesenc3c05ed2010-07-27 04:16:58 +00002059 assert(MF.getFrameInfo()->getObjectSize(FrameIdx) >= RC->getSize() &&
2060 "Stack slot too small for store");
Jim Grosbach04770f22010-01-19 18:31:11 +00002061 bool isAligned = (RI.getStackAlignment() >= 16) || RI.canRealignStack(MF);
Dan Gohman29869722009-04-27 16:41:36 +00002062 unsigned Opc = getStoreRegOpcode(SrcReg, RC, isAligned, TM);
Dale Johannesene5a41342010-01-26 00:03:12 +00002063 DebugLoc DL = MBB.findDebugLoc(MI);
Bill Wendling27b508d2009-02-11 21:51:19 +00002064 addFrameReference(BuildMI(MBB, MI, DL, get(Opc)), FrameIdx)
Bill Wendlingf7b83c72009-05-13 21:33:08 +00002065 .addReg(SrcReg, getKillRegState(isKill));
Owen Andersoneee14602008-01-01 21:11:32 +00002066}
2067
2068void X86InstrInfo::storeRegToAddr(MachineFunction &MF, unsigned SrcReg,
2069 bool isKill,
2070 SmallVectorImpl<MachineOperand> &Addr,
2071 const TargetRegisterClass *RC,
Dan Gohmandd76bb22009-10-09 18:10:05 +00002072 MachineInstr::mmo_iterator MMOBegin,
2073 MachineInstr::mmo_iterator MMOEnd,
Owen Andersoneee14602008-01-01 21:11:32 +00002074 SmallVectorImpl<MachineInstr*> &NewMIs) const {
Dan Gohman425b3562010-07-12 18:12:35 +00002075 bool isAligned = MMOBegin != MMOEnd && (*MMOBegin)->getAlignment() >= 16;
Dan Gohman29869722009-04-27 16:41:36 +00002076 unsigned Opc = getStoreRegOpcode(SrcReg, RC, isAligned, TM);
Chris Lattner6f306d72010-04-02 20:16:16 +00002077 DebugLoc DL;
Dale Johannesen6b8c76a2009-02-12 23:08:38 +00002078 MachineInstrBuilder MIB = BuildMI(MF, DL, get(Opc));
Owen Andersoneee14602008-01-01 21:11:32 +00002079 for (unsigned i = 0, e = Addr.size(); i != e; ++i)
Dan Gohman2af1f852009-02-18 05:45:50 +00002080 MIB.addOperand(Addr[i]);
Bill Wendlingf7b83c72009-05-13 21:33:08 +00002081 MIB.addReg(SrcReg, getKillRegState(isKill));
Dan Gohmandd76bb22009-10-09 18:10:05 +00002082 (*MIB).setMemRefs(MMOBegin, MMOEnd);
Owen Andersoneee14602008-01-01 21:11:32 +00002083 NewMIs.push_back(MIB);
2084}
2085
Owen Andersoneee14602008-01-01 21:11:32 +00002086
2087void X86InstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
Anton Korobeynikovb7a49922008-07-19 06:30:51 +00002088 MachineBasicBlock::iterator MI,
2089 unsigned DestReg, int FrameIdx,
Evan Chengefb126a2010-05-06 19:06:44 +00002090 const TargetRegisterClass *RC,
2091 const TargetRegisterInfo *TRI) const {
Anton Korobeynikovb7a49922008-07-19 06:30:51 +00002092 const MachineFunction &MF = *MBB.getParent();
Jim Grosbach04770f22010-01-19 18:31:11 +00002093 bool isAligned = (RI.getStackAlignment() >= 16) || RI.canRealignStack(MF);
Dan Gohman29869722009-04-27 16:41:36 +00002094 unsigned Opc = getLoadRegOpcode(DestReg, RC, isAligned, TM);
Dale Johannesene5a41342010-01-26 00:03:12 +00002095 DebugLoc DL = MBB.findDebugLoc(MI);
Bill Wendling27b508d2009-02-11 21:51:19 +00002096 addFrameReference(BuildMI(MBB, MI, DL, get(Opc), DestReg), FrameIdx);
Owen Andersoneee14602008-01-01 21:11:32 +00002097}
2098
2099void X86InstrInfo::loadRegFromAddr(MachineFunction &MF, unsigned DestReg,
Evan Cheng7d98a482008-07-03 09:09:37 +00002100 SmallVectorImpl<MachineOperand> &Addr,
2101 const TargetRegisterClass *RC,
Dan Gohmandd76bb22009-10-09 18:10:05 +00002102 MachineInstr::mmo_iterator MMOBegin,
2103 MachineInstr::mmo_iterator MMOEnd,
Owen Andersoneee14602008-01-01 21:11:32 +00002104 SmallVectorImpl<MachineInstr*> &NewMIs) const {
Dan Gohman425b3562010-07-12 18:12:35 +00002105 bool isAligned = MMOBegin != MMOEnd && (*MMOBegin)->getAlignment() >= 16;
Dan Gohman29869722009-04-27 16:41:36 +00002106 unsigned Opc = getLoadRegOpcode(DestReg, RC, isAligned, TM);
Chris Lattner6f306d72010-04-02 20:16:16 +00002107 DebugLoc DL;
Dale Johannesen6b8c76a2009-02-12 23:08:38 +00002108 MachineInstrBuilder MIB = BuildMI(MF, DL, get(Opc), DestReg);
Owen Andersoneee14602008-01-01 21:11:32 +00002109 for (unsigned i = 0, e = Addr.size(); i != e; ++i)
Dan Gohman2af1f852009-02-18 05:45:50 +00002110 MIB.addOperand(Addr[i]);
Dan Gohmandd76bb22009-10-09 18:10:05 +00002111 (*MIB).setMemRefs(MMOBegin, MMOEnd);
Owen Andersoneee14602008-01-01 21:11:32 +00002112 NewMIs.push_back(MIB);
2113}
2114
Owen Anderson6bb0c522008-01-04 23:57:37 +00002115bool X86InstrInfo::spillCalleeSavedRegisters(MachineBasicBlock &MBB,
Bill Wendling27b508d2009-02-11 21:51:19 +00002116 MachineBasicBlock::iterator MI,
Evan Cheng168ced92010-05-22 01:47:14 +00002117 const std::vector<CalleeSavedInfo> &CSI,
2118 const TargetRegisterInfo *TRI) const {
Owen Anderson6bb0c522008-01-04 23:57:37 +00002119 if (CSI.empty())
2120 return false;
2121
Dale Johannesenc5db5992010-01-20 21:36:02 +00002122 DebugLoc DL = MBB.findDebugLoc(MI);
Bill Wendling27b508d2009-02-11 21:51:19 +00002123
Evan Cheng994dd0b2008-09-26 19:14:21 +00002124 bool is64Bit = TM.getSubtarget<X86Subtarget>().is64Bit();
Rafael Espindola350b1a42010-07-21 23:19:57 +00002125 bool isWin64 = TM.getSubtarget<X86Subtarget>().isTargetWin64();
Anton Korobeynikovb52ef062008-10-04 11:09:36 +00002126 unsigned SlotSize = is64Bit ? 8 : 4;
2127
2128 MachineFunction &MF = *MBB.getParent();
Evan Cheng7452c962009-07-09 06:53:48 +00002129 unsigned FPReg = RI.getFrameRegister(MF);
Anton Korobeynikovb52ef062008-10-04 11:09:36 +00002130 X86MachineFunctionInfo *X86FI = MF.getInfo<X86MachineFunctionInfo>();
Eli Friedman63488f12009-06-04 02:32:04 +00002131 unsigned CalleeFrameSize = 0;
Anton Korobeynikovb52ef062008-10-04 11:09:36 +00002132
Owen Anderson6bb0c522008-01-04 23:57:37 +00002133 unsigned Opc = is64Bit ? X86::PUSH64r : X86::PUSH32r;
2134 for (unsigned i = CSI.size(); i != 0; --i) {
2135 unsigned Reg = CSI[i-1].getReg();
2136 // Add the callee-saved register as live-in. It's killed at the spill.
2137 MBB.addLiveIn(Reg);
Evan Cheng7452c962009-07-09 06:53:48 +00002138 if (Reg == FPReg)
2139 // X86RegisterInfo::emitPrologue will handle spilling of frame register.
2140 continue;
Rafael Espindola350b1a42010-07-21 23:19:57 +00002141 if (!X86::VR128RegClass.contains(Reg) && !isWin64) {
Eli Friedman63488f12009-06-04 02:32:04 +00002142 CalleeFrameSize += SlotSize;
Evan Cheng7452c962009-07-09 06:53:48 +00002143 BuildMI(MBB, MI, DL, get(Opc)).addReg(Reg, RegState::Kill);
Eli Friedman63488f12009-06-04 02:32:04 +00002144 } else {
Rafael Espindola350b1a42010-07-21 23:19:57 +00002145 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg);
Rafael Espindolaf2dffce2010-06-02 20:02:30 +00002146 storeRegToStackSlot(MBB, MI, Reg, true, CSI[i-1].getFrameIdx(),
Rafael Espindola350b1a42010-07-21 23:19:57 +00002147 RC, &RI);
Eli Friedman63488f12009-06-04 02:32:04 +00002148 }
Owen Anderson6bb0c522008-01-04 23:57:37 +00002149 }
Eli Friedman63488f12009-06-04 02:32:04 +00002150
2151 X86FI->setCalleeSavedFrameSize(CalleeFrameSize);
Owen Anderson6bb0c522008-01-04 23:57:37 +00002152 return true;
2153}
2154
2155bool X86InstrInfo::restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
Bill Wendling27b508d2009-02-11 21:51:19 +00002156 MachineBasicBlock::iterator MI,
Evan Cheng168ced92010-05-22 01:47:14 +00002157 const std::vector<CalleeSavedInfo> &CSI,
2158 const TargetRegisterInfo *TRI) const {
Owen Anderson6bb0c522008-01-04 23:57:37 +00002159 if (CSI.empty())
2160 return false;
Bill Wendling27b508d2009-02-11 21:51:19 +00002161
Dale Johannesenc5db5992010-01-20 21:36:02 +00002162 DebugLoc DL = MBB.findDebugLoc(MI);
Bill Wendling27b508d2009-02-11 21:51:19 +00002163
Evan Cheng7452c962009-07-09 06:53:48 +00002164 MachineFunction &MF = *MBB.getParent();
2165 unsigned FPReg = RI.getFrameRegister(MF);
Owen Anderson6bb0c522008-01-04 23:57:37 +00002166 bool is64Bit = TM.getSubtarget<X86Subtarget>().is64Bit();
Rafael Espindola350b1a42010-07-21 23:19:57 +00002167 bool isWin64 = TM.getSubtarget<X86Subtarget>().isTargetWin64();
Owen Anderson6bb0c522008-01-04 23:57:37 +00002168 unsigned Opc = is64Bit ? X86::POP64r : X86::POP32r;
2169 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
2170 unsigned Reg = CSI[i].getReg();
Evan Cheng7452c962009-07-09 06:53:48 +00002171 if (Reg == FPReg)
2172 // X86RegisterInfo::emitEpilogue will handle restoring of frame register.
2173 continue;
Rafael Espindola350b1a42010-07-21 23:19:57 +00002174 if (!X86::VR128RegClass.contains(Reg) && !isWin64) {
Eli Friedman63488f12009-06-04 02:32:04 +00002175 BuildMI(MBB, MI, DL, get(Opc), Reg);
2176 } else {
Rafael Espindola350b1a42010-07-21 23:19:57 +00002177 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg);
Rafael Espindolaf2dffce2010-06-02 20:02:30 +00002178 loadRegFromStackSlot(MBB, MI, Reg, CSI[i].getFrameIdx(),
Rafael Espindola350b1a42010-07-21 23:19:57 +00002179 RC, &RI);
Eli Friedman63488f12009-06-04 02:32:04 +00002180 }
Owen Anderson6bb0c522008-01-04 23:57:37 +00002181 }
2182 return true;
2183}
2184
Evan Chenged69b382010-04-26 07:38:55 +00002185MachineInstr*
2186X86InstrInfo::emitFrameIndexDebugValue(MachineFunction &MF,
Evan Cheng250e9172010-04-29 01:13:30 +00002187 int FrameIx, uint64_t Offset,
Evan Chenged69b382010-04-26 07:38:55 +00002188 const MDNode *MDPtr,
2189 DebugLoc DL) const {
Evan Chenged69b382010-04-26 07:38:55 +00002190 X86AddressMode AM;
2191 AM.BaseType = X86AddressMode::FrameIndexBase;
2192 AM.Base.FrameIndex = FrameIx;
2193 MachineInstrBuilder MIB = BuildMI(MF, DL, get(X86::DBG_VALUE));
2194 addFullAddress(MIB, AM).addImm(Offset).addMetadata(MDPtr);
2195 return &*MIB;
2196}
2197
Dan Gohman3b460302008-07-07 23:14:23 +00002198static MachineInstr *FuseTwoAddrInst(MachineFunction &MF, unsigned Opcode,
Dan Gohman906152a2009-01-05 17:59:02 +00002199 const SmallVectorImpl<MachineOperand> &MOs,
Bill Wendlinge3c78362009-02-03 00:55:04 +00002200 MachineInstr *MI,
2201 const TargetInstrInfo &TII) {
Owen Anderson2a3be7b2008-01-07 01:35:02 +00002202 // Create the base instruction with the memory operand as the first part.
Bill Wendlinge3c78362009-02-03 00:55:04 +00002203 MachineInstr *NewMI = MF.CreateMachineInstr(TII.get(Opcode),
2204 MI->getDebugLoc(), true);
Owen Anderson2a3be7b2008-01-07 01:35:02 +00002205 MachineInstrBuilder MIB(NewMI);
2206 unsigned NumAddrOps = MOs.size();
2207 for (unsigned i = 0; i != NumAddrOps; ++i)
Dan Gohman2af1f852009-02-18 05:45:50 +00002208 MIB.addOperand(MOs[i]);
Owen Anderson2a3be7b2008-01-07 01:35:02 +00002209 if (NumAddrOps < 4) // FrameIndex only
Rafael Espindola3b2df102009-04-08 21:14:34 +00002210 addOffset(MIB, 0);
Owen Anderson2a3be7b2008-01-07 01:35:02 +00002211
2212 // Loop over the rest of the ri operands, converting them over.
Chris Lattner03ad8852008-01-07 07:27:27 +00002213 unsigned NumOps = MI->getDesc().getNumOperands()-2;
Owen Anderson2a3be7b2008-01-07 01:35:02 +00002214 for (unsigned i = 0; i != NumOps; ++i) {
2215 MachineOperand &MO = MI->getOperand(i+2);
Dan Gohman2af1f852009-02-18 05:45:50 +00002216 MIB.addOperand(MO);
Owen Anderson2a3be7b2008-01-07 01:35:02 +00002217 }
2218 for (unsigned i = NumOps+2, e = MI->getNumOperands(); i != e; ++i) {
2219 MachineOperand &MO = MI->getOperand(i);
Dan Gohman2af1f852009-02-18 05:45:50 +00002220 MIB.addOperand(MO);
Owen Anderson2a3be7b2008-01-07 01:35:02 +00002221 }
2222 return MIB;
2223}
2224
Dan Gohman3b460302008-07-07 23:14:23 +00002225static MachineInstr *FuseInst(MachineFunction &MF,
2226 unsigned Opcode, unsigned OpNo,
Dan Gohman906152a2009-01-05 17:59:02 +00002227 const SmallVectorImpl<MachineOperand> &MOs,
Owen Anderson2a3be7b2008-01-07 01:35:02 +00002228 MachineInstr *MI, const TargetInstrInfo &TII) {
Bill Wendlinge3c78362009-02-03 00:55:04 +00002229 MachineInstr *NewMI = MF.CreateMachineInstr(TII.get(Opcode),
2230 MI->getDebugLoc(), true);
Owen Anderson2a3be7b2008-01-07 01:35:02 +00002231 MachineInstrBuilder MIB(NewMI);
2232
2233 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
2234 MachineOperand &MO = MI->getOperand(i);
2235 if (i == OpNo) {
Dan Gohman0d1e9a82008-10-03 15:45:36 +00002236 assert(MO.isReg() && "Expected to fold into reg operand!");
Owen Anderson2a3be7b2008-01-07 01:35:02 +00002237 unsigned NumAddrOps = MOs.size();
2238 for (unsigned i = 0; i != NumAddrOps; ++i)
Dan Gohman2af1f852009-02-18 05:45:50 +00002239 MIB.addOperand(MOs[i]);
Owen Anderson2a3be7b2008-01-07 01:35:02 +00002240 if (NumAddrOps < 4) // FrameIndex only
Rafael Espindola3b2df102009-04-08 21:14:34 +00002241 addOffset(MIB, 0);
Owen Anderson2a3be7b2008-01-07 01:35:02 +00002242 } else {
Dan Gohman2af1f852009-02-18 05:45:50 +00002243 MIB.addOperand(MO);
Owen Anderson2a3be7b2008-01-07 01:35:02 +00002244 }
2245 }
2246 return MIB;
2247}
2248
2249static MachineInstr *MakeM0Inst(const TargetInstrInfo &TII, unsigned Opcode,
Dan Gohman906152a2009-01-05 17:59:02 +00002250 const SmallVectorImpl<MachineOperand> &MOs,
Owen Anderson2a3be7b2008-01-07 01:35:02 +00002251 MachineInstr *MI) {
Dan Gohman3b460302008-07-07 23:14:23 +00002252 MachineFunction &MF = *MI->getParent()->getParent();
Bill Wendling27b508d2009-02-11 21:51:19 +00002253 MachineInstrBuilder MIB = BuildMI(MF, MI->getDebugLoc(), TII.get(Opcode));
Owen Anderson2a3be7b2008-01-07 01:35:02 +00002254
2255 unsigned NumAddrOps = MOs.size();
2256 for (unsigned i = 0; i != NumAddrOps; ++i)
Dan Gohman2af1f852009-02-18 05:45:50 +00002257 MIB.addOperand(MOs[i]);
Owen Anderson2a3be7b2008-01-07 01:35:02 +00002258 if (NumAddrOps < 4) // FrameIndex only
Rafael Espindola3b2df102009-04-08 21:14:34 +00002259 addOffset(MIB, 0);
Owen Anderson2a3be7b2008-01-07 01:35:02 +00002260 return MIB.addImm(0);
2261}
2262
2263MachineInstr*
Dan Gohman3f86b512008-12-03 18:43:12 +00002264X86InstrInfo::foldMemoryOperandImpl(MachineFunction &MF,
2265 MachineInstr *MI, unsigned i,
Evan Cheng9e0c7f22009-07-15 06:10:07 +00002266 const SmallVectorImpl<MachineOperand> &MOs,
Evan Cheng3cad6282009-09-11 00:39:26 +00002267 unsigned Size, unsigned Align) const {
Chris Lattner1c090c02010-10-07 23:08:41 +00002268 const DenseMap<unsigned, std::pair<unsigned,unsigned> > *OpcodeTablePtr = 0;
Owen Anderson2a3be7b2008-01-07 01:35:02 +00002269 bool isTwoAddrFold = false;
Chris Lattner03ad8852008-01-07 07:27:27 +00002270 unsigned NumOps = MI->getDesc().getNumOperands();
Owen Anderson2a3be7b2008-01-07 01:35:02 +00002271 bool isTwoAddr = NumOps > 1 &&
Chris Lattner03ad8852008-01-07 07:27:27 +00002272 MI->getDesc().getOperandConstraint(1, TOI::TIED_TO) != -1;
Owen Anderson2a3be7b2008-01-07 01:35:02 +00002273
2274 MachineInstr *NewMI = NULL;
2275 // Folding a memory location into the two-address part of a two-address
2276 // instruction is different than folding it other places. It requires
2277 // replacing the *two* registers with the memory location.
2278 if (isTwoAddr && NumOps >= 2 && i < 2 &&
Dan Gohman0d1e9a82008-10-03 15:45:36 +00002279 MI->getOperand(0).isReg() &&
2280 MI->getOperand(1).isReg() &&
Owen Anderson2a3be7b2008-01-07 01:35:02 +00002281 MI->getOperand(0).getReg() == MI->getOperand(1).getReg()) {
2282 OpcodeTablePtr = &RegOp2MemOpTable2Addr;
2283 isTwoAddrFold = true;
2284 } else if (i == 0) { // If operand 0
Dan Gohmanc1195802010-01-12 04:42:54 +00002285 if (MI->getOpcode() == X86::MOV64r0)
2286 NewMI = MakeM0Inst(*this, X86::MOV64mi32, MOs, MI);
2287 else if (MI->getOpcode() == X86::MOV32r0)
Owen Anderson2a3be7b2008-01-07 01:35:02 +00002288 NewMI = MakeM0Inst(*this, X86::MOV32mi, MOs, MI);
Dan Gohmanc1195802010-01-12 04:42:54 +00002289 else if (MI->getOpcode() == X86::MOV16r0)
2290 NewMI = MakeM0Inst(*this, X86::MOV16mi, MOs, MI);
Owen Anderson2a3be7b2008-01-07 01:35:02 +00002291 else if (MI->getOpcode() == X86::MOV8r0)
2292 NewMI = MakeM0Inst(*this, X86::MOV8mi, MOs, MI);
Evan Cheng7d98a482008-07-03 09:09:37 +00002293 if (NewMI)
Owen Anderson2a3be7b2008-01-07 01:35:02 +00002294 return NewMI;
Owen Anderson2a3be7b2008-01-07 01:35:02 +00002295
2296 OpcodeTablePtr = &RegOp2MemOpTable0;
2297 } else if (i == 1) {
2298 OpcodeTablePtr = &RegOp2MemOpTable1;
2299 } else if (i == 2) {
2300 OpcodeTablePtr = &RegOp2MemOpTable2;
2301 }
2302
2303 // If table selected...
2304 if (OpcodeTablePtr) {
2305 // Find the Opcode to fuse
Chris Lattner1c090c02010-10-07 23:08:41 +00002306 DenseMap<unsigned, std::pair<unsigned,unsigned> >::const_iterator I =
2307 OpcodeTablePtr->find(MI->getOpcode());
Owen Anderson2a3be7b2008-01-07 01:35:02 +00002308 if (I != OpcodeTablePtr->end()) {
Evan Cheng3cad6282009-09-11 00:39:26 +00002309 unsigned Opcode = I->second.first;
Evan Cheng9e0c7f22009-07-15 06:10:07 +00002310 unsigned MinAlign = I->second.second;
2311 if (Align < MinAlign)
2312 return NULL;
Evan Cheng74a32312009-09-11 01:01:31 +00002313 bool NarrowToMOV32rm = false;
Evan Cheng3cad6282009-09-11 00:39:26 +00002314 if (Size) {
2315 unsigned RCSize = MI->getDesc().OpInfo[i].getRegClass(&RI)->getSize();
2316 if (Size < RCSize) {
2317 // Check if it's safe to fold the load. If the size of the object is
2318 // narrower than the load width, then it's not.
2319 if (Opcode != X86::MOV64rm || RCSize != 8 || Size != 4)
2320 return NULL;
2321 // If this is a 64-bit load, but the spill slot is 32, then we can do
2322 // a 32-bit load which is implicitly zero-extended. This likely is due
2323 // to liveintervalanalysis remat'ing a load from stack slot.
Evan Cheng74a32312009-09-11 01:01:31 +00002324 if (MI->getOperand(0).getSubReg() || MI->getOperand(1).getSubReg())
2325 return NULL;
Evan Cheng3cad6282009-09-11 00:39:26 +00002326 Opcode = X86::MOV32rm;
Evan Cheng74a32312009-09-11 01:01:31 +00002327 NarrowToMOV32rm = true;
Evan Cheng3cad6282009-09-11 00:39:26 +00002328 }
2329 }
2330
Owen Anderson2a3be7b2008-01-07 01:35:02 +00002331 if (isTwoAddrFold)
Evan Cheng3cad6282009-09-11 00:39:26 +00002332 NewMI = FuseTwoAddrInst(MF, Opcode, MOs, MI, *this);
Owen Anderson2a3be7b2008-01-07 01:35:02 +00002333 else
Evan Cheng3cad6282009-09-11 00:39:26 +00002334 NewMI = FuseInst(MF, Opcode, i, MOs, MI, *this);
Evan Cheng74a32312009-09-11 01:01:31 +00002335
2336 if (NarrowToMOV32rm) {
2337 // If this is the special case where we use a MOV32rm to load a 32-bit
2338 // value and zero-extend the top bits. Change the destination register
2339 // to a 32-bit one.
2340 unsigned DstReg = NewMI->getOperand(0).getReg();
2341 if (TargetRegisterInfo::isPhysicalRegister(DstReg))
2342 NewMI->getOperand(0).setReg(RI.getSubReg(DstReg,
Jakob Stoklund Olesen9340ea52010-05-24 14:48:17 +00002343 X86::sub_32bit));
Evan Cheng74a32312009-09-11 01:01:31 +00002344 else
Jakob Stoklund Olesen9340ea52010-05-24 14:48:17 +00002345 NewMI->getOperand(0).setSubReg(X86::sub_32bit);
Evan Cheng74a32312009-09-11 01:01:31 +00002346 }
Owen Anderson2a3be7b2008-01-07 01:35:02 +00002347 return NewMI;
2348 }
2349 }
2350
2351 // No fusion
Jakob Stoklund Olesen51702ec2010-07-09 20:43:09 +00002352 if (PrintFailedFusing && !MI->isCopy())
David Greened589daf2010-01-05 01:29:29 +00002353 dbgs() << "We failed to fuse operand " << i << " in " << *MI;
Owen Anderson2a3be7b2008-01-07 01:35:02 +00002354 return NULL;
2355}
2356
2357
Dan Gohman3f86b512008-12-03 18:43:12 +00002358MachineInstr* X86InstrInfo::foldMemoryOperandImpl(MachineFunction &MF,
2359 MachineInstr *MI,
Evan Cheng9e0c7f22009-07-15 06:10:07 +00002360 const SmallVectorImpl<unsigned> &Ops,
Dan Gohman3f86b512008-12-03 18:43:12 +00002361 int FrameIndex) const {
Owen Anderson2a3be7b2008-01-07 01:35:02 +00002362 // Check switch flag
2363 if (NoFusing) return NULL;
2364
Evan Cheng71d7eaa2009-12-22 17:47:23 +00002365 if (!MF.getFunction()->hasFnAttr(Attribute::OptimizeForSize))
Evan Cheng4cf30b72009-12-18 07:40:29 +00002366 switch (MI->getOpcode()) {
2367 case X86::CVTSD2SSrr:
2368 case X86::Int_CVTSD2SSrr:
2369 case X86::CVTSS2SDrr:
2370 case X86::Int_CVTSS2SDrr:
2371 case X86::RCPSSr:
2372 case X86::RCPSSr_Int:
Chris Lattnerf60062f2010-09-29 02:57:56 +00002373 case X86::ROUNDSDr:
2374 case X86::ROUNDSSr:
Evan Cheng4cf30b72009-12-18 07:40:29 +00002375 case X86::RSQRTSSr:
2376 case X86::RSQRTSSr_Int:
2377 case X86::SQRTSSr:
2378 case X86::SQRTSSr_Int:
2379 return 0;
2380 }
2381
Evan Cheng3b3286d2008-02-08 21:20:40 +00002382 const MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng3cad6282009-09-11 00:39:26 +00002383 unsigned Size = MFI->getObjectSize(FrameIndex);
Evan Cheng3b3286d2008-02-08 21:20:40 +00002384 unsigned Alignment = MFI->getObjectAlignment(FrameIndex);
Owen Anderson2a3be7b2008-01-07 01:35:02 +00002385 if (Ops.size() == 2 && Ops[0] == 0 && Ops[1] == 1) {
2386 unsigned NewOpc = 0;
Evan Cheng3cad6282009-09-11 00:39:26 +00002387 unsigned RCSize = 0;
Owen Anderson2a3be7b2008-01-07 01:35:02 +00002388 switch (MI->getOpcode()) {
2389 default: return NULL;
Evan Cheng3cad6282009-09-11 00:39:26 +00002390 case X86::TEST8rr: NewOpc = X86::CMP8ri; RCSize = 1; break;
Dan Gohman887dd1c2010-05-18 21:42:03 +00002391 case X86::TEST16rr: NewOpc = X86::CMP16ri8; RCSize = 2; break;
2392 case X86::TEST32rr: NewOpc = X86::CMP32ri8; RCSize = 4; break;
2393 case X86::TEST64rr: NewOpc = X86::CMP64ri8; RCSize = 8; break;
Owen Anderson2a3be7b2008-01-07 01:35:02 +00002394 }
Evan Cheng3cad6282009-09-11 00:39:26 +00002395 // Check if it's safe to fold the load. If the size of the object is
2396 // narrower than the load width, then it's not.
2397 if (Size < RCSize)
2398 return NULL;
Owen Anderson2a3be7b2008-01-07 01:35:02 +00002399 // Change to CMPXXri r, 0 first.
Chris Lattner59687512008-01-11 18:10:50 +00002400 MI->setDesc(get(NewOpc));
Owen Anderson2a3be7b2008-01-07 01:35:02 +00002401 MI->getOperand(1).ChangeToImmediate(0);
2402 } else if (Ops.size() != 1)
2403 return NULL;
2404
2405 SmallVector<MachineOperand,4> MOs;
2406 MOs.push_back(MachineOperand::CreateFI(FrameIndex));
Evan Cheng3cad6282009-09-11 00:39:26 +00002407 return foldMemoryOperandImpl(MF, MI, Ops[0], MOs, Size, Alignment);
Owen Anderson2a3be7b2008-01-07 01:35:02 +00002408}
2409
Dan Gohman3f86b512008-12-03 18:43:12 +00002410MachineInstr* X86InstrInfo::foldMemoryOperandImpl(MachineFunction &MF,
2411 MachineInstr *MI,
Evan Cheng9e0c7f22009-07-15 06:10:07 +00002412 const SmallVectorImpl<unsigned> &Ops,
Dan Gohman3f86b512008-12-03 18:43:12 +00002413 MachineInstr *LoadMI) const {
Owen Anderson2a3be7b2008-01-07 01:35:02 +00002414 // Check switch flag
2415 if (NoFusing) return NULL;
2416
Evan Cheng71d7eaa2009-12-22 17:47:23 +00002417 if (!MF.getFunction()->hasFnAttr(Attribute::OptimizeForSize))
Evan Cheng4cf30b72009-12-18 07:40:29 +00002418 switch (MI->getOpcode()) {
2419 case X86::CVTSD2SSrr:
2420 case X86::Int_CVTSD2SSrr:
2421 case X86::CVTSS2SDrr:
2422 case X86::Int_CVTSS2SDrr:
2423 case X86::RCPSSr:
2424 case X86::RCPSSr_Int:
Chris Lattnerf60062f2010-09-29 02:57:56 +00002425 case X86::ROUNDSDr:
2426 case X86::ROUNDSSr:
Evan Cheng4cf30b72009-12-18 07:40:29 +00002427 case X86::RSQRTSSr:
2428 case X86::RSQRTSSr_Int:
2429 case X86::SQRTSSr:
2430 case X86::SQRTSSr_Int:
2431 return 0;
2432 }
2433
Dan Gohman9a542a42008-07-12 00:10:52 +00002434 // Determine the alignment of the load.
Evan Cheng3b3286d2008-02-08 21:20:40 +00002435 unsigned Alignment = 0;
Dan Gohman9a542a42008-07-12 00:10:52 +00002436 if (LoadMI->hasOneMemOperand())
Dan Gohman48b185d2009-09-25 20:36:54 +00002437 Alignment = (*LoadMI->memoperands_begin())->getAlignment();
Dan Gohman69499b132009-09-21 18:30:38 +00002438 else
2439 switch (LoadMI->getOpcode()) {
Bruno Cardoso Lopes7f704b32010-08-12 20:20:53 +00002440 case X86::AVX_SET0PSY:
2441 case X86::AVX_SET0PDY:
2442 Alignment = 32;
2443 break;
Jakob Stoklund Olesen9986ba92010-03-31 00:40:13 +00002444 case X86::V_SET0PS:
2445 case X86::V_SET0PD:
2446 case X86::V_SET0PI:
Dan Gohman69499b132009-09-21 18:30:38 +00002447 case X86::V_SETALLONES:
Bruno Cardoso Lopes7f704b32010-08-12 20:20:53 +00002448 case X86::AVX_SET0PS:
2449 case X86::AVX_SET0PD:
2450 case X86::AVX_SET0PI:
Dan Gohman69499b132009-09-21 18:30:38 +00002451 Alignment = 16;
2452 break;
2453 case X86::FsFLD0SD:
2454 Alignment = 8;
2455 break;
2456 case X86::FsFLD0SS:
2457 Alignment = 4;
2458 break;
2459 default:
2460 llvm_unreachable("Don't know how to fold this instruction!");
2461 }
Owen Anderson2a3be7b2008-01-07 01:35:02 +00002462 if (Ops.size() == 2 && Ops[0] == 0 && Ops[1] == 1) {
2463 unsigned NewOpc = 0;
2464 switch (MI->getOpcode()) {
2465 default: return NULL;
2466 case X86::TEST8rr: NewOpc = X86::CMP8ri; break;
Dan Gohmanf8bf6632010-05-18 21:54:15 +00002467 case X86::TEST16rr: NewOpc = X86::CMP16ri8; break;
2468 case X86::TEST32rr: NewOpc = X86::CMP32ri8; break;
2469 case X86::TEST64rr: NewOpc = X86::CMP64ri8; break;
Owen Anderson2a3be7b2008-01-07 01:35:02 +00002470 }
2471 // Change to CMPXXri r, 0 first.
Chris Lattner59687512008-01-11 18:10:50 +00002472 MI->setDesc(get(NewOpc));
Owen Anderson2a3be7b2008-01-07 01:35:02 +00002473 MI->getOperand(1).ChangeToImmediate(0);
2474 } else if (Ops.size() != 1)
2475 return NULL;
2476
Jakob Stoklund Olesen9c473e42010-08-11 23:08:22 +00002477 // Make sure the subregisters match.
2478 // Otherwise we risk changing the size of the load.
2479 if (LoadMI->getOperand(0).getSubReg() != MI->getOperand(Ops[0]).getSubReg())
2480 return NULL;
2481
Chris Lattnerec536272010-07-08 22:41:28 +00002482 SmallVector<MachineOperand,X86::AddrNumOperands> MOs;
Dan Gohman69499b132009-09-21 18:30:38 +00002483 switch (LoadMI->getOpcode()) {
Jakob Stoklund Olesen9986ba92010-03-31 00:40:13 +00002484 case X86::V_SET0PS:
2485 case X86::V_SET0PD:
2486 case X86::V_SET0PI:
Dan Gohman69499b132009-09-21 18:30:38 +00002487 case X86::V_SETALLONES:
Bruno Cardoso Lopes7f704b32010-08-12 20:20:53 +00002488 case X86::AVX_SET0PS:
2489 case X86::AVX_SET0PD:
2490 case X86::AVX_SET0PI:
2491 case X86::AVX_SET0PSY:
2492 case X86::AVX_SET0PDY:
Dan Gohman69499b132009-09-21 18:30:38 +00002493 case X86::FsFLD0SD:
2494 case X86::FsFLD0SS: {
Jakob Stoklund Olesen9986ba92010-03-31 00:40:13 +00002495 // Folding a V_SET0P? or V_SETALLONES as a load, to ease register pressure.
Dan Gohmancc78cdf2008-12-03 05:21:24 +00002496 // Create a constant-pool entry and operands to load from it.
2497
Dan Gohman772952f2010-03-09 03:01:40 +00002498 // Medium and large mode can't fold loads this way.
2499 if (TM.getCodeModel() != CodeModel::Small &&
2500 TM.getCodeModel() != CodeModel::Kernel)
2501 return NULL;
2502
Dan Gohmancc78cdf2008-12-03 05:21:24 +00002503 // x86-32 PIC requires a PIC base register for constant pools.
2504 unsigned PICBase = 0;
Jakob Stoklund Olesenc7895d32009-07-16 21:24:13 +00002505 if (TM.getRelocationModel() == Reloc::PIC_) {
Evan Chengfdd0eb42009-07-16 18:44:05 +00002506 if (TM.getSubtarget<X86Subtarget>().is64Bit())
2507 PICBase = X86::RIP;
Jakob Stoklund Olesenc7895d32009-07-16 21:24:13 +00002508 else
Dan Gohmand7b5ce32010-07-10 09:00:22 +00002509 // FIXME: PICBase = getGlobalBaseReg(&MF);
Evan Chengfdd0eb42009-07-16 18:44:05 +00002510 // This doesn't work for several reasons.
2511 // 1. GlobalBaseReg may have been spilled.
2512 // 2. It may not be live at MI.
Dan Gohman69499b132009-09-21 18:30:38 +00002513 return NULL;
Jakob Stoklund Olesenc7895d32009-07-16 21:24:13 +00002514 }
Dan Gohmancc78cdf2008-12-03 05:21:24 +00002515
Dan Gohman69499b132009-09-21 18:30:38 +00002516 // Create a constant-pool entry.
Dan Gohmancc78cdf2008-12-03 05:21:24 +00002517 MachineConstantPool &MCP = *MF.getConstantPool();
Dan Gohman69499b132009-09-21 18:30:38 +00002518 const Type *Ty;
Bruno Cardoso Lopes7f704b32010-08-12 20:20:53 +00002519 unsigned Opc = LoadMI->getOpcode();
2520 if (Opc == X86::FsFLD0SS)
Dan Gohman69499b132009-09-21 18:30:38 +00002521 Ty = Type::getFloatTy(MF.getFunction()->getContext());
Bruno Cardoso Lopes7f704b32010-08-12 20:20:53 +00002522 else if (Opc == X86::FsFLD0SD)
Dan Gohman69499b132009-09-21 18:30:38 +00002523 Ty = Type::getDoubleTy(MF.getFunction()->getContext());
Bruno Cardoso Lopes7f704b32010-08-12 20:20:53 +00002524 else if (Opc == X86::AVX_SET0PSY || Opc == X86::AVX_SET0PDY)
2525 Ty = VectorType::get(Type::getFloatTy(MF.getFunction()->getContext()), 8);
Dan Gohman69499b132009-09-21 18:30:38 +00002526 else
2527 Ty = VectorType::get(Type::getInt32Ty(MF.getFunction()->getContext()), 4);
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002528 const Constant *C = LoadMI->getOpcode() == X86::V_SETALLONES ?
Dan Gohman69499b132009-09-21 18:30:38 +00002529 Constant::getAllOnesValue(Ty) :
2530 Constant::getNullValue(Ty);
2531 unsigned CPI = MCP.getConstantPoolIndex(C, Alignment);
Dan Gohmancc78cdf2008-12-03 05:21:24 +00002532
2533 // Create operands to load from the constant pool entry.
2534 MOs.push_back(MachineOperand::CreateReg(PICBase, false));
2535 MOs.push_back(MachineOperand::CreateImm(1));
2536 MOs.push_back(MachineOperand::CreateReg(0, false));
2537 MOs.push_back(MachineOperand::CreateCPI(CPI, 0));
Rafael Espindola3b2df102009-04-08 21:14:34 +00002538 MOs.push_back(MachineOperand::CreateReg(0, false));
Dan Gohman69499b132009-09-21 18:30:38 +00002539 break;
2540 }
2541 default: {
Dan Gohmancc78cdf2008-12-03 05:21:24 +00002542 // Folding a normal load. Just copy the load's address operands.
2543 unsigned NumOps = LoadMI->getDesc().getNumOperands();
Chris Lattnerec536272010-07-08 22:41:28 +00002544 for (unsigned i = NumOps - X86::AddrNumOperands; i != NumOps; ++i)
Dan Gohmancc78cdf2008-12-03 05:21:24 +00002545 MOs.push_back(LoadMI->getOperand(i));
Dan Gohman69499b132009-09-21 18:30:38 +00002546 break;
2547 }
Dan Gohmancc78cdf2008-12-03 05:21:24 +00002548 }
Evan Cheng3cad6282009-09-11 00:39:26 +00002549 return foldMemoryOperandImpl(MF, MI, Ops[0], MOs, 0, Alignment);
Owen Anderson2a3be7b2008-01-07 01:35:02 +00002550}
2551
2552
Dan Gohman33332bc2008-10-16 01:49:15 +00002553bool X86InstrInfo::canFoldMemoryOperand(const MachineInstr *MI,
2554 const SmallVectorImpl<unsigned> &Ops) const {
Owen Anderson2a3be7b2008-01-07 01:35:02 +00002555 // Check switch flag
2556 if (NoFusing) return 0;
2557
2558 if (Ops.size() == 2 && Ops[0] == 0 && Ops[1] == 1) {
2559 switch (MI->getOpcode()) {
2560 default: return false;
2561 case X86::TEST8rr:
2562 case X86::TEST16rr:
2563 case X86::TEST32rr:
2564 case X86::TEST64rr:
2565 return true;
2566 }
2567 }
2568
2569 if (Ops.size() != 1)
2570 return false;
2571
2572 unsigned OpNum = Ops[0];
2573 unsigned Opc = MI->getOpcode();
Chris Lattner03ad8852008-01-07 07:27:27 +00002574 unsigned NumOps = MI->getDesc().getNumOperands();
Owen Anderson2a3be7b2008-01-07 01:35:02 +00002575 bool isTwoAddr = NumOps > 1 &&
Chris Lattner03ad8852008-01-07 07:27:27 +00002576 MI->getDesc().getOperandConstraint(1, TOI::TIED_TO) != -1;
Owen Anderson2a3be7b2008-01-07 01:35:02 +00002577
2578 // Folding a memory location into the two-address part of a two-address
2579 // instruction is different than folding it other places. It requires
2580 // replacing the *two* registers with the memory location.
Chris Lattner1c090c02010-10-07 23:08:41 +00002581 const DenseMap<unsigned, std::pair<unsigned,unsigned> > *OpcodeTablePtr = 0;
Owen Anderson2a3be7b2008-01-07 01:35:02 +00002582 if (isTwoAddr && NumOps >= 2 && OpNum < 2) {
2583 OpcodeTablePtr = &RegOp2MemOpTable2Addr;
2584 } else if (OpNum == 0) { // If operand 0
2585 switch (Opc) {
Chris Lattner79c136d2009-07-14 20:19:57 +00002586 case X86::MOV8r0:
Dan Gohmanc1195802010-01-12 04:42:54 +00002587 case X86::MOV16r0:
Owen Anderson2a3be7b2008-01-07 01:35:02 +00002588 case X86::MOV32r0:
Chris Lattner1c090c02010-10-07 23:08:41 +00002589 case X86::MOV64r0: return true;
Owen Anderson2a3be7b2008-01-07 01:35:02 +00002590 default: break;
2591 }
2592 OpcodeTablePtr = &RegOp2MemOpTable0;
2593 } else if (OpNum == 1) {
2594 OpcodeTablePtr = &RegOp2MemOpTable1;
2595 } else if (OpNum == 2) {
2596 OpcodeTablePtr = &RegOp2MemOpTable2;
2597 }
2598
Daniel Dunbar8f21f9c2010-10-08 02:07:32 +00002599 if (OpcodeTablePtr) {
2600 // Find the Opcode to fuse
2601 DenseMap<unsigned, std::pair<unsigned,unsigned> >::const_iterator I =
2602 OpcodeTablePtr->find(Opc);
2603 if (I != OpcodeTablePtr->end())
2604 return true;
2605 }
Jakob Stoklund Olesen7a7b55e2010-07-09 20:43:13 +00002606 return TargetInstrInfoImpl::canFoldMemoryOperand(MI, Ops);
Owen Anderson2a3be7b2008-01-07 01:35:02 +00002607}
2608
2609bool X86InstrInfo::unfoldMemoryOperand(MachineFunction &MF, MachineInstr *MI,
2610 unsigned Reg, bool UnfoldLoad, bool UnfoldStore,
Bill Wendling27b508d2009-02-11 21:51:19 +00002611 SmallVectorImpl<MachineInstr*> &NewMIs) const {
Chris Lattner1c090c02010-10-07 23:08:41 +00002612 DenseMap<unsigned, std::pair<unsigned,unsigned> >::const_iterator I =
2613 MemOp2RegOpTable.find(MI->getOpcode());
Owen Anderson2a3be7b2008-01-07 01:35:02 +00002614 if (I == MemOp2RegOpTable.end())
2615 return false;
2616 unsigned Opc = I->second.first;
2617 unsigned Index = I->second.second & 0xf;
2618 bool FoldedLoad = I->second.second & (1 << 4);
2619 bool FoldedStore = I->second.second & (1 << 5);
2620 if (UnfoldLoad && !FoldedLoad)
2621 return false;
2622 UnfoldLoad &= FoldedLoad;
2623 if (UnfoldStore && !FoldedStore)
2624 return false;
2625 UnfoldStore &= FoldedStore;
2626
Chris Lattner03ad8852008-01-07 07:27:27 +00002627 const TargetInstrDesc &TID = get(Opc);
Owen Anderson2a3be7b2008-01-07 01:35:02 +00002628 const TargetOperandInfo &TOI = TID.OpInfo[Index];
Chris Lattnerf3239532009-07-29 21:10:12 +00002629 const TargetRegisterClass *RC = TOI.getRegClass(&RI);
Evan Cheng0ce84482010-07-02 20:36:18 +00002630 if (!MI->hasOneMemOperand() &&
2631 RC == &X86::VR128RegClass &&
2632 !TM.getSubtarget<X86Subtarget>().isUnalignedMemAccessFast())
2633 // Without memoperands, loadRegFromAddr and storeRegToStackSlot will
2634 // conservatively assume the address is unaligned. That's bad for
2635 // performance.
2636 return false;
Chris Lattnerec536272010-07-08 22:41:28 +00002637 SmallVector<MachineOperand, X86::AddrNumOperands> AddrOps;
Owen Anderson2a3be7b2008-01-07 01:35:02 +00002638 SmallVector<MachineOperand,2> BeforeOps;
2639 SmallVector<MachineOperand,2> AfterOps;
2640 SmallVector<MachineOperand,4> ImpOps;
2641 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
2642 MachineOperand &Op = MI->getOperand(i);
Chris Lattnerec536272010-07-08 22:41:28 +00002643 if (i >= Index && i < Index + X86::AddrNumOperands)
Owen Anderson2a3be7b2008-01-07 01:35:02 +00002644 AddrOps.push_back(Op);
Dan Gohman0d1e9a82008-10-03 15:45:36 +00002645 else if (Op.isReg() && Op.isImplicit())
Owen Anderson2a3be7b2008-01-07 01:35:02 +00002646 ImpOps.push_back(Op);
2647 else if (i < Index)
2648 BeforeOps.push_back(Op);
2649 else if (i > Index)
2650 AfterOps.push_back(Op);
2651 }
2652
2653 // Emit the load instruction.
2654 if (UnfoldLoad) {
Dan Gohmandd76bb22009-10-09 18:10:05 +00002655 std::pair<MachineInstr::mmo_iterator,
2656 MachineInstr::mmo_iterator> MMOs =
2657 MF.extractLoadMemRefs(MI->memoperands_begin(),
2658 MI->memoperands_end());
2659 loadRegFromAddr(MF, Reg, AddrOps, RC, MMOs.first, MMOs.second, NewMIs);
Owen Anderson2a3be7b2008-01-07 01:35:02 +00002660 if (UnfoldStore) {
2661 // Address operands cannot be marked isKill.
Chris Lattnerec536272010-07-08 22:41:28 +00002662 for (unsigned i = 1; i != 1 + X86::AddrNumOperands; ++i) {
Owen Anderson2a3be7b2008-01-07 01:35:02 +00002663 MachineOperand &MO = NewMIs[0]->getOperand(i);
Dan Gohman0d1e9a82008-10-03 15:45:36 +00002664 if (MO.isReg())
Owen Anderson2a3be7b2008-01-07 01:35:02 +00002665 MO.setIsKill(false);
2666 }
2667 }
2668 }
2669
2670 // Emit the data processing instruction.
Bill Wendlinge3c78362009-02-03 00:55:04 +00002671 MachineInstr *DataMI = MF.CreateMachineInstr(TID, MI->getDebugLoc(), true);
Owen Anderson2a3be7b2008-01-07 01:35:02 +00002672 MachineInstrBuilder MIB(DataMI);
2673
2674 if (FoldedStore)
Bill Wendlingf7b83c72009-05-13 21:33:08 +00002675 MIB.addReg(Reg, RegState::Define);
Owen Anderson2a3be7b2008-01-07 01:35:02 +00002676 for (unsigned i = 0, e = BeforeOps.size(); i != e; ++i)
Dan Gohman2af1f852009-02-18 05:45:50 +00002677 MIB.addOperand(BeforeOps[i]);
Owen Anderson2a3be7b2008-01-07 01:35:02 +00002678 if (FoldedLoad)
2679 MIB.addReg(Reg);
2680 for (unsigned i = 0, e = AfterOps.size(); i != e; ++i)
Dan Gohman2af1f852009-02-18 05:45:50 +00002681 MIB.addOperand(AfterOps[i]);
Owen Anderson2a3be7b2008-01-07 01:35:02 +00002682 for (unsigned i = 0, e = ImpOps.size(); i != e; ++i) {
2683 MachineOperand &MO = ImpOps[i];
Bill Wendlingf7b83c72009-05-13 21:33:08 +00002684 MIB.addReg(MO.getReg(),
2685 getDefRegState(MO.isDef()) |
2686 RegState::Implicit |
2687 getKillRegState(MO.isKill()) |
Evan Cheng0dc101b2009-06-30 08:49:04 +00002688 getDeadRegState(MO.isDead()) |
2689 getUndefRegState(MO.isUndef()));
Owen Anderson2a3be7b2008-01-07 01:35:02 +00002690 }
2691 // Change CMP32ri r, 0 back to TEST32rr r, r, etc.
2692 unsigned NewOpc = 0;
2693 switch (DataMI->getOpcode()) {
2694 default: break;
2695 case X86::CMP64ri32:
Dan Gohmanf8bf6632010-05-18 21:54:15 +00002696 case X86::CMP64ri8:
Owen Anderson2a3be7b2008-01-07 01:35:02 +00002697 case X86::CMP32ri:
Dan Gohmanf8bf6632010-05-18 21:54:15 +00002698 case X86::CMP32ri8:
Owen Anderson2a3be7b2008-01-07 01:35:02 +00002699 case X86::CMP16ri:
Dan Gohmanf8bf6632010-05-18 21:54:15 +00002700 case X86::CMP16ri8:
Owen Anderson2a3be7b2008-01-07 01:35:02 +00002701 case X86::CMP8ri: {
2702 MachineOperand &MO0 = DataMI->getOperand(0);
2703 MachineOperand &MO1 = DataMI->getOperand(1);
2704 if (MO1.getImm() == 0) {
2705 switch (DataMI->getOpcode()) {
2706 default: break;
Dan Gohmanf8bf6632010-05-18 21:54:15 +00002707 case X86::CMP64ri8:
Owen Anderson2a3be7b2008-01-07 01:35:02 +00002708 case X86::CMP64ri32: NewOpc = X86::TEST64rr; break;
Dan Gohmanf8bf6632010-05-18 21:54:15 +00002709 case X86::CMP32ri8:
Owen Anderson2a3be7b2008-01-07 01:35:02 +00002710 case X86::CMP32ri: NewOpc = X86::TEST32rr; break;
Dan Gohmanf8bf6632010-05-18 21:54:15 +00002711 case X86::CMP16ri8:
Owen Anderson2a3be7b2008-01-07 01:35:02 +00002712 case X86::CMP16ri: NewOpc = X86::TEST16rr; break;
2713 case X86::CMP8ri: NewOpc = X86::TEST8rr; break;
2714 }
Chris Lattner59687512008-01-11 18:10:50 +00002715 DataMI->setDesc(get(NewOpc));
Owen Anderson2a3be7b2008-01-07 01:35:02 +00002716 MO1.ChangeToRegister(MO0.getReg(), false);
2717 }
2718 }
2719 }
2720 NewMIs.push_back(DataMI);
2721
2722 // Emit the store instruction.
2723 if (UnfoldStore) {
Chris Lattnerf3239532009-07-29 21:10:12 +00002724 const TargetRegisterClass *DstRC = TID.OpInfo[0].getRegClass(&RI);
Dan Gohmandd76bb22009-10-09 18:10:05 +00002725 std::pair<MachineInstr::mmo_iterator,
2726 MachineInstr::mmo_iterator> MMOs =
2727 MF.extractStoreMemRefs(MI->memoperands_begin(),
2728 MI->memoperands_end());
2729 storeRegToAddr(MF, Reg, true, AddrOps, DstRC, MMOs.first, MMOs.second, NewMIs);
Owen Anderson2a3be7b2008-01-07 01:35:02 +00002730 }
2731
2732 return true;
2733}
2734
2735bool
2736X86InstrInfo::unfoldMemoryOperand(SelectionDAG &DAG, SDNode *N,
Bill Wendling27b508d2009-02-11 21:51:19 +00002737 SmallVectorImpl<SDNode*> &NewNodes) const {
Dan Gohman17059682008-07-17 19:10:17 +00002738 if (!N->isMachineOpcode())
Owen Anderson2a3be7b2008-01-07 01:35:02 +00002739 return false;
2740
Chris Lattner1c090c02010-10-07 23:08:41 +00002741 DenseMap<unsigned, std::pair<unsigned,unsigned> >::const_iterator I =
2742 MemOp2RegOpTable.find(N->getMachineOpcode());
Owen Anderson2a3be7b2008-01-07 01:35:02 +00002743 if (I == MemOp2RegOpTable.end())
2744 return false;
2745 unsigned Opc = I->second.first;
2746 unsigned Index = I->second.second & 0xf;
2747 bool FoldedLoad = I->second.second & (1 << 4);
2748 bool FoldedStore = I->second.second & (1 << 5);
Chris Lattner03ad8852008-01-07 07:27:27 +00002749 const TargetInstrDesc &TID = get(Opc);
Chris Lattnerf3239532009-07-29 21:10:12 +00002750 const TargetRegisterClass *RC = TID.OpInfo[Index].getRegClass(&RI);
Dan Gohmancc329b52009-03-04 19:23:38 +00002751 unsigned NumDefs = TID.NumDefs;
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002752 std::vector<SDValue> AddrOps;
2753 std::vector<SDValue> BeforeOps;
2754 std::vector<SDValue> AfterOps;
Dale Johannesen9f3f72f2009-02-06 01:31:28 +00002755 DebugLoc dl = N->getDebugLoc();
Owen Anderson2a3be7b2008-01-07 01:35:02 +00002756 unsigned NumOps = N->getNumOperands();
Dan Gohman48b185d2009-09-25 20:36:54 +00002757 for (unsigned i = 0; i != NumOps-1; ++i) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002758 SDValue Op = N->getOperand(i);
Chris Lattnerec536272010-07-08 22:41:28 +00002759 if (i >= Index-NumDefs && i < Index-NumDefs + X86::AddrNumOperands)
Owen Anderson2a3be7b2008-01-07 01:35:02 +00002760 AddrOps.push_back(Op);
Dan Gohmancc329b52009-03-04 19:23:38 +00002761 else if (i < Index-NumDefs)
Owen Anderson2a3be7b2008-01-07 01:35:02 +00002762 BeforeOps.push_back(Op);
Dan Gohmancc329b52009-03-04 19:23:38 +00002763 else if (i > Index-NumDefs)
Owen Anderson2a3be7b2008-01-07 01:35:02 +00002764 AfterOps.push_back(Op);
2765 }
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002766 SDValue Chain = N->getOperand(NumOps-1);
Owen Anderson2a3be7b2008-01-07 01:35:02 +00002767 AddrOps.push_back(Chain);
2768
2769 // Emit the load instruction.
2770 SDNode *Load = 0;
Dan Gohmandd76bb22009-10-09 18:10:05 +00002771 MachineFunction &MF = DAG.getMachineFunction();
Owen Anderson2a3be7b2008-01-07 01:35:02 +00002772 if (FoldedLoad) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002773 EVT VT = *RC->vt_begin();
Evan Chengf25ef4f2009-11-16 21:56:03 +00002774 std::pair<MachineInstr::mmo_iterator,
2775 MachineInstr::mmo_iterator> MMOs =
2776 MF.extractLoadMemRefs(cast<MachineSDNode>(N)->memoperands_begin(),
2777 cast<MachineSDNode>(N)->memoperands_end());
Evan Cheng0ce84482010-07-02 20:36:18 +00002778 if (!(*MMOs.first) &&
2779 RC == &X86::VR128RegClass &&
2780 !TM.getSubtarget<X86Subtarget>().isUnalignedMemAccessFast())
2781 // Do not introduce a slow unaligned load.
2782 return false;
2783 bool isAligned = (*MMOs.first) && (*MMOs.first)->getAlignment() >= 16;
Dan Gohman32f71d72009-09-25 18:54:59 +00002784 Load = DAG.getMachineNode(getLoadRegOpcode(0, RC, isAligned, TM), dl,
2785 VT, MVT::Other, &AddrOps[0], AddrOps.size());
Owen Anderson2a3be7b2008-01-07 01:35:02 +00002786 NewNodes.push_back(Load);
Dan Gohmandd76bb22009-10-09 18:10:05 +00002787
2788 // Preserve memory reference information.
Dan Gohmandd76bb22009-10-09 18:10:05 +00002789 cast<MachineSDNode>(Load)->setMemRefs(MMOs.first, MMOs.second);
Owen Anderson2a3be7b2008-01-07 01:35:02 +00002790 }
2791
2792 // Emit the data processing instruction.
Owen Anderson53aa7a92009-08-10 22:56:29 +00002793 std::vector<EVT> VTs;
Owen Anderson2a3be7b2008-01-07 01:35:02 +00002794 const TargetRegisterClass *DstRC = 0;
Chris Lattnerb0d06b42008-01-07 03:13:06 +00002795 if (TID.getNumDefs() > 0) {
Chris Lattnerf3239532009-07-29 21:10:12 +00002796 DstRC = TID.OpInfo[0].getRegClass(&RI);
Owen Anderson2a3be7b2008-01-07 01:35:02 +00002797 VTs.push_back(*DstRC->vt_begin());
2798 }
2799 for (unsigned i = 0, e = N->getNumValues(); i != e; ++i) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002800 EVT VT = N->getValueType(i);
Owen Anderson9f944592009-08-11 20:47:22 +00002801 if (VT != MVT::Other && i >= (unsigned)TID.getNumDefs())
Owen Anderson2a3be7b2008-01-07 01:35:02 +00002802 VTs.push_back(VT);
2803 }
2804 if (Load)
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002805 BeforeOps.push_back(SDValue(Load, 0));
Owen Anderson2a3be7b2008-01-07 01:35:02 +00002806 std::copy(AfterOps.begin(), AfterOps.end(), std::back_inserter(BeforeOps));
Dan Gohman32f71d72009-09-25 18:54:59 +00002807 SDNode *NewNode= DAG.getMachineNode(Opc, dl, VTs, &BeforeOps[0],
2808 BeforeOps.size());
Owen Anderson2a3be7b2008-01-07 01:35:02 +00002809 NewNodes.push_back(NewNode);
2810
2811 // Emit the store instruction.
2812 if (FoldedStore) {
2813 AddrOps.pop_back();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002814 AddrOps.push_back(SDValue(NewNode, 0));
Owen Anderson2a3be7b2008-01-07 01:35:02 +00002815 AddrOps.push_back(Chain);
Evan Chengf25ef4f2009-11-16 21:56:03 +00002816 std::pair<MachineInstr::mmo_iterator,
2817 MachineInstr::mmo_iterator> MMOs =
2818 MF.extractStoreMemRefs(cast<MachineSDNode>(N)->memoperands_begin(),
2819 cast<MachineSDNode>(N)->memoperands_end());
Evan Cheng0ce84482010-07-02 20:36:18 +00002820 if (!(*MMOs.first) &&
2821 RC == &X86::VR128RegClass &&
2822 !TM.getSubtarget<X86Subtarget>().isUnalignedMemAccessFast())
2823 // Do not introduce a slow unaligned store.
2824 return false;
2825 bool isAligned = (*MMOs.first) && (*MMOs.first)->getAlignment() >= 16;
Dan Gohman32f71d72009-09-25 18:54:59 +00002826 SDNode *Store = DAG.getMachineNode(getStoreRegOpcode(0, DstRC,
2827 isAligned, TM),
2828 dl, MVT::Other,
2829 &AddrOps[0], AddrOps.size());
Owen Anderson2a3be7b2008-01-07 01:35:02 +00002830 NewNodes.push_back(Store);
Dan Gohmandd76bb22009-10-09 18:10:05 +00002831
2832 // Preserve memory reference information.
Dan Gohmandd76bb22009-10-09 18:10:05 +00002833 cast<MachineSDNode>(Load)->setMemRefs(MMOs.first, MMOs.second);
Owen Anderson2a3be7b2008-01-07 01:35:02 +00002834 }
2835
2836 return true;
2837}
2838
2839unsigned X86InstrInfo::getOpcodeAfterMemoryUnfold(unsigned Opc,
Dan Gohman49fa51d2009-10-30 22:18:41 +00002840 bool UnfoldLoad, bool UnfoldStore,
2841 unsigned *LoadRegIndex) const {
Chris Lattner1c090c02010-10-07 23:08:41 +00002842 DenseMap<unsigned, std::pair<unsigned,unsigned> >::const_iterator I =
2843 MemOp2RegOpTable.find(Opc);
Owen Anderson2a3be7b2008-01-07 01:35:02 +00002844 if (I == MemOp2RegOpTable.end())
2845 return 0;
2846 bool FoldedLoad = I->second.second & (1 << 4);
2847 bool FoldedStore = I->second.second & (1 << 5);
2848 if (UnfoldLoad && !FoldedLoad)
2849 return 0;
2850 if (UnfoldStore && !FoldedStore)
2851 return 0;
Dan Gohman49fa51d2009-10-30 22:18:41 +00002852 if (LoadRegIndex)
2853 *LoadRegIndex = I->second.second & 0xf;
Owen Anderson2a3be7b2008-01-07 01:35:02 +00002854 return I->second.first;
2855}
2856
Evan Cheng4f026f32010-01-22 03:34:51 +00002857bool
2858X86InstrInfo::areLoadsFromSameBasePtr(SDNode *Load1, SDNode *Load2,
2859 int64_t &Offset1, int64_t &Offset2) const {
2860 if (!Load1->isMachineOpcode() || !Load2->isMachineOpcode())
2861 return false;
2862 unsigned Opc1 = Load1->getMachineOpcode();
2863 unsigned Opc2 = Load2->getMachineOpcode();
2864 switch (Opc1) {
2865 default: return false;
2866 case X86::MOV8rm:
2867 case X86::MOV16rm:
2868 case X86::MOV32rm:
2869 case X86::MOV64rm:
2870 case X86::LD_Fp32m:
2871 case X86::LD_Fp64m:
2872 case X86::LD_Fp80m:
2873 case X86::MOVSSrm:
2874 case X86::MOVSDrm:
2875 case X86::MMX_MOVD64rm:
2876 case X86::MMX_MOVQ64rm:
2877 case X86::FsMOVAPSrm:
2878 case X86::FsMOVAPDrm:
2879 case X86::MOVAPSrm:
2880 case X86::MOVUPSrm:
2881 case X86::MOVUPSrm_Int:
2882 case X86::MOVAPDrm:
2883 case X86::MOVDQArm:
2884 case X86::MOVDQUrm:
2885 case X86::MOVDQUrm_Int:
2886 break;
2887 }
2888 switch (Opc2) {
2889 default: return false;
2890 case X86::MOV8rm:
2891 case X86::MOV16rm:
2892 case X86::MOV32rm:
2893 case X86::MOV64rm:
2894 case X86::LD_Fp32m:
2895 case X86::LD_Fp64m:
2896 case X86::LD_Fp80m:
2897 case X86::MOVSSrm:
2898 case X86::MOVSDrm:
2899 case X86::MMX_MOVD64rm:
2900 case X86::MMX_MOVQ64rm:
2901 case X86::FsMOVAPSrm:
2902 case X86::FsMOVAPDrm:
2903 case X86::MOVAPSrm:
2904 case X86::MOVUPSrm:
2905 case X86::MOVUPSrm_Int:
2906 case X86::MOVAPDrm:
2907 case X86::MOVDQArm:
2908 case X86::MOVDQUrm:
2909 case X86::MOVDQUrm_Int:
2910 break;
2911 }
2912
2913 // Check if chain operands and base addresses match.
2914 if (Load1->getOperand(0) != Load2->getOperand(0) ||
2915 Load1->getOperand(5) != Load2->getOperand(5))
2916 return false;
2917 // Segment operands should match as well.
2918 if (Load1->getOperand(4) != Load2->getOperand(4))
2919 return false;
2920 // Scale should be 1, Index should be Reg0.
2921 if (Load1->getOperand(1) == Load2->getOperand(1) &&
2922 Load1->getOperand(2) == Load2->getOperand(2)) {
2923 if (cast<ConstantSDNode>(Load1->getOperand(1))->getZExtValue() != 1)
2924 return false;
Evan Cheng4f026f32010-01-22 03:34:51 +00002925
2926 // Now let's examine the displacements.
2927 if (isa<ConstantSDNode>(Load1->getOperand(3)) &&
2928 isa<ConstantSDNode>(Load2->getOperand(3))) {
2929 Offset1 = cast<ConstantSDNode>(Load1->getOperand(3))->getSExtValue();
2930 Offset2 = cast<ConstantSDNode>(Load2->getOperand(3))->getSExtValue();
2931 return true;
2932 }
2933 }
2934 return false;
2935}
2936
2937bool X86InstrInfo::shouldScheduleLoadsNear(SDNode *Load1, SDNode *Load2,
2938 int64_t Offset1, int64_t Offset2,
2939 unsigned NumLoads) const {
2940 assert(Offset2 > Offset1);
2941 if ((Offset2 - Offset1) / 8 > 64)
2942 return false;
2943
2944 unsigned Opc1 = Load1->getMachineOpcode();
2945 unsigned Opc2 = Load2->getMachineOpcode();
2946 if (Opc1 != Opc2)
2947 return false; // FIXME: overly conservative?
2948
2949 switch (Opc1) {
2950 default: break;
2951 case X86::LD_Fp32m:
2952 case X86::LD_Fp64m:
2953 case X86::LD_Fp80m:
2954 case X86::MMX_MOVD64rm:
2955 case X86::MMX_MOVQ64rm:
2956 return false;
2957 }
2958
2959 EVT VT = Load1->getValueType(0);
2960 switch (VT.getSimpleVT().SimpleTy) {
Bill Wendling8ce69cd2010-06-22 22:16:17 +00002961 default:
Evan Cheng4f026f32010-01-22 03:34:51 +00002962 // XMM registers. In 64-bit mode we can be a bit more aggressive since we
2963 // have 16 of them to play with.
2964 if (TM.getSubtargetImpl()->is64Bit()) {
2965 if (NumLoads >= 3)
2966 return false;
Bill Wendling8ce69cd2010-06-22 22:16:17 +00002967 } else if (NumLoads) {
Evan Cheng4f026f32010-01-22 03:34:51 +00002968 return false;
Bill Wendling8ce69cd2010-06-22 22:16:17 +00002969 }
Evan Cheng4f026f32010-01-22 03:34:51 +00002970 break;
Evan Cheng4f026f32010-01-22 03:34:51 +00002971 case MVT::i8:
2972 case MVT::i16:
2973 case MVT::i32:
2974 case MVT::i64:
Evan Cheng16cf9342010-01-22 23:49:11 +00002975 case MVT::f32:
2976 case MVT::f64:
Evan Cheng4f026f32010-01-22 03:34:51 +00002977 if (NumLoads)
2978 return false;
Bill Wendling8ce69cd2010-06-22 22:16:17 +00002979 break;
Evan Cheng4f026f32010-01-22 03:34:51 +00002980 }
2981
2982 return true;
2983}
2984
2985
Chris Lattnerc0fb5672006-10-20 17:42:20 +00002986bool X86InstrInfo::
Owen Anderson4f6bf042008-08-14 22:49:33 +00002987ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
Chris Lattner3a897f32006-10-21 05:52:40 +00002988 assert(Cond.size() == 1 && "Invalid X86 branch condition!");
Evan Chengf93bc7f2008-08-29 23:21:31 +00002989 X86::CondCode CC = static_cast<X86::CondCode>(Cond[0].getImm());
Dan Gohman97d95d62008-10-21 03:29:32 +00002990 if (CC == X86::COND_NE_OR_P || CC == X86::COND_NP_OR_E)
2991 return true;
Evan Chengf93bc7f2008-08-29 23:21:31 +00002992 Cond[0].setImm(GetOppositeBranchCondition(CC));
Chris Lattner3a897f32006-10-21 05:52:40 +00002993 return false;
Chris Lattnerc0fb5672006-10-20 17:42:20 +00002994}
2995
Evan Chengf7137222008-10-27 07:14:50 +00002996bool X86InstrInfo::
Evan Chengb5f0ec32009-02-06 17:17:30 +00002997isSafeToMoveRegClassDefs(const TargetRegisterClass *RC) const {
2998 // FIXME: Return false for x87 stack register classes for now. We can't
Evan Chengf7137222008-10-27 07:14:50 +00002999 // allow any loads of these registers before FpGet_ST0_80.
Evan Chengb5f0ec32009-02-06 17:17:30 +00003000 return !(RC == &X86::CCRRegClass || RC == &X86::RFP32RegClass ||
3001 RC == &X86::RFP64RegClass || RC == &X86::RFP80RegClass);
Evan Chengf7137222008-10-27 07:14:50 +00003002}
3003
Nicolas Geoffrayae84bbd2008-04-16 20:10:13 +00003004
Chris Lattner58827ff2010-02-05 22:10:22 +00003005/// isX86_64ExtendedReg - Is the MachineOperand a x86-64 extended (r8 or higher)
3006/// register? e.g. r8, xmm8, xmm13, etc.
3007bool X86InstrInfo::isX86_64ExtendedReg(unsigned RegNo) {
3008 switch (RegNo) {
Nicolas Geoffrayae84bbd2008-04-16 20:10:13 +00003009 default: break;
3010 case X86::R8: case X86::R9: case X86::R10: case X86::R11:
3011 case X86::R12: case X86::R13: case X86::R14: case X86::R15:
3012 case X86::R8D: case X86::R9D: case X86::R10D: case X86::R11D:
3013 case X86::R12D: case X86::R13D: case X86::R14D: case X86::R15D:
3014 case X86::R8W: case X86::R9W: case X86::R10W: case X86::R11W:
3015 case X86::R12W: case X86::R13W: case X86::R14W: case X86::R15W:
3016 case X86::R8B: case X86::R9B: case X86::R10B: case X86::R11B:
3017 case X86::R12B: case X86::R13B: case X86::R14B: case X86::R15B:
3018 case X86::XMM8: case X86::XMM9: case X86::XMM10: case X86::XMM11:
3019 case X86::XMM12: case X86::XMM13: case X86::XMM14: case X86::XMM15:
Bruno Cardoso Lopes792e9062010-07-09 18:27:43 +00003020 case X86::YMM8: case X86::YMM9: case X86::YMM10: case X86::YMM11:
3021 case X86::YMM12: case X86::YMM13: case X86::YMM14: case X86::YMM15:
Chris Lattner37fc4692010-09-22 05:29:50 +00003022 case X86::CR8: case X86::CR9: case X86::CR10: case X86::CR11:
3023 case X86::CR12: case X86::CR13: case X86::CR14: case X86::CR15:
Nicolas Geoffrayae84bbd2008-04-16 20:10:13 +00003024 return true;
3025 }
3026 return false;
3027}
3028
Dan Gohman6ebe7342008-09-30 00:58:23 +00003029/// getGlobalBaseReg - Return a virtual register initialized with the
3030/// the global base register value. Output instructions required to
3031/// initialize the register in the function entry block, if necessary.
Dan Gohman24300732008-09-23 18:22:58 +00003032///
Dan Gohmand7b5ce32010-07-10 09:00:22 +00003033/// TODO: Eliminate this and move the code to X86MachineFunctionInfo.
3034///
Dan Gohman6ebe7342008-09-30 00:58:23 +00003035unsigned X86InstrInfo::getGlobalBaseReg(MachineFunction *MF) const {
3036 assert(!TM.getSubtarget<X86Subtarget>().is64Bit() &&
3037 "X86-64 PIC uses RIP relative addressing");
3038
3039 X86MachineFunctionInfo *X86FI = MF->getInfo<X86MachineFunctionInfo>();
3040 unsigned GlobalBaseReg = X86FI->getGlobalBaseReg();
3041 if (GlobalBaseReg != 0)
3042 return GlobalBaseReg;
3043
Dan Gohmand7b5ce32010-07-10 09:00:22 +00003044 // Create the register. The code to initialize it is inserted
3045 // later, by the CGBR pass (below).
Dan Gohman24300732008-09-23 18:22:58 +00003046 MachineRegisterInfo &RegInfo = MF->getRegInfo();
Dan Gohmand7b5ce32010-07-10 09:00:22 +00003047 GlobalBaseReg = RegInfo.createVirtualRegister(X86::GR32RegisterClass);
Dan Gohman6ebe7342008-09-30 00:58:23 +00003048 X86FI->setGlobalBaseReg(GlobalBaseReg);
3049 return GlobalBaseReg;
Dan Gohman24300732008-09-23 18:22:58 +00003050}
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +00003051
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +00003052// These are the replaceable SSE instructions. Some of these have Int variants
3053// that we don't include here. We don't want to replace instructions selected
3054// by intrinsics.
3055static const unsigned ReplaceableInstrs[][3] = {
Bruno Cardoso Lopes1401e042010-08-12 02:08:52 +00003056 //PackedSingle PackedDouble PackedInt
Jakob Stoklund Olesendbff4e82010-03-30 22:46:53 +00003057 { X86::MOVAPSmr, X86::MOVAPDmr, X86::MOVDQAmr },
3058 { X86::MOVAPSrm, X86::MOVAPDrm, X86::MOVDQArm },
3059 { X86::MOVAPSrr, X86::MOVAPDrr, X86::MOVDQArr },
3060 { X86::MOVUPSmr, X86::MOVUPDmr, X86::MOVDQUmr },
3061 { X86::MOVUPSrm, X86::MOVUPDrm, X86::MOVDQUrm },
3062 { X86::MOVNTPSmr, X86::MOVNTPDmr, X86::MOVNTDQmr },
3063 { X86::ANDNPSrm, X86::ANDNPDrm, X86::PANDNrm },
3064 { X86::ANDNPSrr, X86::ANDNPDrr, X86::PANDNrr },
3065 { X86::ANDPSrm, X86::ANDPDrm, X86::PANDrm },
3066 { X86::ANDPSrr, X86::ANDPDrr, X86::PANDrr },
3067 { X86::ORPSrm, X86::ORPDrm, X86::PORrm },
3068 { X86::ORPSrr, X86::ORPDrr, X86::PORrr },
Jakob Stoklund Olesen9986ba92010-03-31 00:40:13 +00003069 { X86::V_SET0PS, X86::V_SET0PD, X86::V_SET0PI },
Jakob Stoklund Olesendbff4e82010-03-30 22:46:53 +00003070 { X86::XORPSrm, X86::XORPDrm, X86::PXORrm },
3071 { X86::XORPSrr, X86::XORPDrr, X86::PXORrr },
Bruno Cardoso Lopes7f704b32010-08-12 20:20:53 +00003072 // AVX 128-bit support
3073 { X86::VMOVAPSmr, X86::VMOVAPDmr, X86::VMOVDQAmr },
3074 { X86::VMOVAPSrm, X86::VMOVAPDrm, X86::VMOVDQArm },
3075 { X86::VMOVAPSrr, X86::VMOVAPDrr, X86::VMOVDQArr },
3076 { X86::VMOVUPSmr, X86::VMOVUPDmr, X86::VMOVDQUmr },
3077 { X86::VMOVUPSrm, X86::VMOVUPDrm, X86::VMOVDQUrm },
3078 { X86::VMOVNTPSmr, X86::VMOVNTPDmr, X86::VMOVNTDQmr },
3079 { X86::VANDNPSrm, X86::VANDNPDrm, X86::VPANDNrm },
3080 { X86::VANDNPSrr, X86::VANDNPDrr, X86::VPANDNrr },
3081 { X86::VANDPSrm, X86::VANDPDrm, X86::VPANDrm },
3082 { X86::VANDPSrr, X86::VANDPDrr, X86::VPANDrr },
3083 { X86::VORPSrm, X86::VORPDrm, X86::VPORrm },
3084 { X86::VORPSrr, X86::VORPDrr, X86::VPORrr },
3085 { X86::AVX_SET0PS, X86::AVX_SET0PD, X86::AVX_SET0PI },
3086 { X86::VXORPSrm, X86::VXORPDrm, X86::VPXORrm },
3087 { X86::VXORPSrr, X86::VXORPDrr, X86::VPXORrr },
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +00003088};
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +00003089
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +00003090// FIXME: Some shuffle and unpack instructions have equivalents in different
3091// domains, but they require a bit more work than just switching opcodes.
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +00003092
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +00003093static const unsigned *lookup(unsigned opcode, unsigned domain) {
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +00003094 for (unsigned i = 0, e = array_lengthof(ReplaceableInstrs); i != e; ++i)
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +00003095 if (ReplaceableInstrs[i][domain-1] == opcode)
3096 return ReplaceableInstrs[i];
3097 return 0;
3098}
3099
3100std::pair<uint16_t, uint16_t>
3101X86InstrInfo::GetSSEDomain(const MachineInstr *MI) const {
3102 uint16_t domain = (MI->getDesc().TSFlags >> X86II::SSEDomainShift) & 3;
Jakob Stoklund Olesendbff4e82010-03-30 22:46:53 +00003103 return std::make_pair(domain,
3104 domain && lookup(MI->getOpcode(), domain) ? 0xe : 0);
Jakob Stoklund Olesenb551aa42010-03-29 23:24:21 +00003105}
3106
3107void X86InstrInfo::SetSSEDomain(MachineInstr *MI, unsigned Domain) const {
3108 assert(Domain>0 && Domain<4 && "Invalid execution domain");
3109 uint16_t dom = (MI->getDesc().TSFlags >> X86II::SSEDomainShift) & 3;
3110 assert(dom && "Not an SSE instruction");
3111 const unsigned *table = lookup(MI->getOpcode(), dom);
3112 assert(table && "Cannot change domain");
3113 MI->setDesc(get(table[Domain-1]));
Jakob Stoklund Olesen49e121d2010-03-25 17:25:00 +00003114}
Chris Lattner6a5e7062010-04-26 23:37:21 +00003115
3116/// getNoopForMachoTarget - Return the noop instruction to use for a noop.
3117void X86InstrInfo::getNoopForMachoTarget(MCInst &NopInst) const {
3118 NopInst.setOpcode(X86::NOOP);
3119}
Dan Gohmand7b5ce32010-07-10 09:00:22 +00003120
3121namespace {
3122 /// CGBR - Create Global Base Reg pass. This initializes the PIC
3123 /// global base register for x86-32.
3124 struct CGBR : public MachineFunctionPass {
3125 static char ID;
Owen Andersona7aed182010-08-06 18:33:48 +00003126 CGBR() : MachineFunctionPass(ID) {}
Dan Gohmand7b5ce32010-07-10 09:00:22 +00003127
3128 virtual bool runOnMachineFunction(MachineFunction &MF) {
3129 const X86TargetMachine *TM =
3130 static_cast<const X86TargetMachine *>(&MF.getTarget());
3131
3132 assert(!TM->getSubtarget<X86Subtarget>().is64Bit() &&
3133 "X86-64 PIC uses RIP relative addressing");
3134
3135 // Only emit a global base reg in PIC mode.
3136 if (TM->getRelocationModel() != Reloc::PIC_)
3137 return false;
3138
Dan Gohman534db8a2010-09-17 20:24:24 +00003139 X86MachineFunctionInfo *X86FI = MF.getInfo<X86MachineFunctionInfo>();
3140 unsigned GlobalBaseReg = X86FI->getGlobalBaseReg();
3141
3142 // If we didn't need a GlobalBaseReg, don't insert code.
3143 if (GlobalBaseReg == 0)
3144 return false;
3145
Dan Gohmand7b5ce32010-07-10 09:00:22 +00003146 // Insert the set of GlobalBaseReg into the first MBB of the function
3147 MachineBasicBlock &FirstMBB = MF.front();
3148 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
3149 DebugLoc DL = FirstMBB.findDebugLoc(MBBI);
3150 MachineRegisterInfo &RegInfo = MF.getRegInfo();
3151 const X86InstrInfo *TII = TM->getInstrInfo();
3152
3153 unsigned PC;
3154 if (TM->getSubtarget<X86Subtarget>().isPICStyleGOT())
3155 PC = RegInfo.createVirtualRegister(X86::GR32RegisterClass);
3156 else
Dan Gohman534db8a2010-09-17 20:24:24 +00003157 PC = GlobalBaseReg;
Dan Gohmand7b5ce32010-07-10 09:00:22 +00003158
3159 // Operand of MovePCtoStack is completely ignored by asm printer. It's
3160 // only used in JIT code emission as displacement to pc.
3161 BuildMI(FirstMBB, MBBI, DL, TII->get(X86::MOVPC32r), PC).addImm(0);
3162
3163 // If we're using vanilla 'GOT' PIC style, we should use relative addressing
3164 // not to pc, but to _GLOBAL_OFFSET_TABLE_ external.
3165 if (TM->getSubtarget<X86Subtarget>().isPICStyleGOT()) {
Dan Gohmand7b5ce32010-07-10 09:00:22 +00003166 // Generate addl $__GLOBAL_OFFSET_TABLE_ + [.-piclabel], %some_register
3167 BuildMI(FirstMBB, MBBI, DL, TII->get(X86::ADD32ri), GlobalBaseReg)
3168 .addReg(PC).addExternalSymbol("_GLOBAL_OFFSET_TABLE_",
3169 X86II::MO_GOT_ABSOLUTE_ADDRESS);
3170 }
3171
3172 return true;
3173 }
3174
3175 virtual const char *getPassName() const {
3176 return "X86 PIC Global Base Reg Initialization";
3177 }
3178
3179 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
3180 AU.setPreservesCFG();
3181 MachineFunctionPass::getAnalysisUsage(AU);
3182 }
3183 };
3184}
3185
3186char CGBR::ID = 0;
3187FunctionPass*
3188llvm::createGlobalBaseRegPass() { return new CGBR(); }