blob: 4d9e2bef7422907cea175caea1f6e88bff5846c5 [file] [log] [blame]
Misha Brukmana85d6bc2002-11-22 22:42:50 +00001//===- X86InstrInfo.cpp - X86 Instruction Information -----------*- C++ -*-===//
Misha Brukman0e0a7a452005-04-21 23:38:14 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-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 Brukman0e0a7a452005-04-21 23:38:14 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner72614082002-10-25 22:55:53 +00009//
Chris Lattner3501fea2003-01-14 22:00:31 +000010// This file contains the X86 implementation of the TargetInstrInfo class.
Chris Lattner72614082002-10-25 22:55:53 +000011//
12//===----------------------------------------------------------------------===//
13
Chris Lattner055c9652002-10-29 21:05:24 +000014#include "X86InstrInfo.h"
Chris Lattner4ce42a72002-12-03 05:42:53 +000015#include "X86.h"
Chris Lattnerabf05b22003-08-03 21:55:55 +000016#include "X86GenInstrInfo.inc"
Evan Chengaa3c1412006-05-30 21:45:53 +000017#include "X86InstrBuilder.h"
Owen Andersond94b6a12008-01-04 23:57:37 +000018#include "X86MachineFunctionInfo.h"
Evan Chengaa3c1412006-05-30 21:45:53 +000019#include "X86Subtarget.h"
20#include "X86TargetMachine.h"
Dan Gohmand68a0762009-01-05 17:59:02 +000021#include "llvm/DerivedTypes.h"
Owen Anderson718cb662007-09-07 04:06:50 +000022#include "llvm/ADT/STLExtras.h"
Dan Gohman62c939d2008-12-03 05:21:24 +000023#include "llvm/CodeGen/MachineConstantPool.h"
Owen Andersond94b6a12008-01-04 23:57:37 +000024#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Chengaa3c1412006-05-30 21:45:53 +000025#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000026#include "llvm/CodeGen/MachineRegisterInfo.h"
Evan Cheng258ff672006-12-01 21:52:41 +000027#include "llvm/CodeGen/LiveVariables.h"
Owen Anderson43dbe052008-01-07 01:35:02 +000028#include "llvm/Support/CommandLine.h"
Evan Cheng0488db92007-09-25 01:57:46 +000029#include "llvm/Target/TargetOptions.h"
Nicolas Geoffray52e724a2008-04-16 20:10:13 +000030#include "llvm/Target/TargetAsmInfo.h"
Owen Anderson43dbe052008-01-07 01:35:02 +000031
Brian Gaeked0fde302003-11-11 22:41:34 +000032using namespace llvm;
33
Owen Anderson43dbe052008-01-07 01:35:02 +000034namespace {
35 cl::opt<bool>
36 NoFusing("disable-spill-fusing",
37 cl::desc("Disable fusing of spill code into instructions"));
38 cl::opt<bool>
39 PrintFailedFusing("print-failed-fuse-candidates",
40 cl::desc("Print instructions that the allocator wants to"
41 " fuse, but the X86 backend currently can't"),
42 cl::Hidden);
Evan Chengffe2eb02008-04-01 23:26:12 +000043 cl::opt<bool>
44 ReMatPICStubLoad("remat-pic-stub-load",
45 cl::desc("Re-materialize load from stub in PIC mode"),
46 cl::init(false), cl::Hidden);
Owen Anderson43dbe052008-01-07 01:35:02 +000047}
48
Evan Chengaa3c1412006-05-30 21:45:53 +000049X86InstrInfo::X86InstrInfo(X86TargetMachine &tm)
Chris Lattner64105522008-01-01 01:03:04 +000050 : TargetInstrInfoImpl(X86Insts, array_lengthof(X86Insts)),
Evan Cheng25ab6902006-09-08 06:48:29 +000051 TM(tm), RI(tm, *this) {
Owen Anderson43dbe052008-01-07 01:35:02 +000052 SmallVector<unsigned,16> AmbEntries;
53 static const unsigned OpTbl2Addr[][2] = {
54 { X86::ADC32ri, X86::ADC32mi },
55 { X86::ADC32ri8, X86::ADC32mi8 },
56 { X86::ADC32rr, X86::ADC32mr },
57 { X86::ADC64ri32, X86::ADC64mi32 },
58 { X86::ADC64ri8, X86::ADC64mi8 },
59 { X86::ADC64rr, X86::ADC64mr },
60 { X86::ADD16ri, X86::ADD16mi },
61 { X86::ADD16ri8, X86::ADD16mi8 },
62 { X86::ADD16rr, X86::ADD16mr },
63 { X86::ADD32ri, X86::ADD32mi },
64 { X86::ADD32ri8, X86::ADD32mi8 },
65 { X86::ADD32rr, X86::ADD32mr },
66 { X86::ADD64ri32, X86::ADD64mi32 },
67 { X86::ADD64ri8, X86::ADD64mi8 },
68 { X86::ADD64rr, X86::ADD64mr },
69 { X86::ADD8ri, X86::ADD8mi },
70 { X86::ADD8rr, X86::ADD8mr },
71 { X86::AND16ri, X86::AND16mi },
72 { X86::AND16ri8, X86::AND16mi8 },
73 { X86::AND16rr, X86::AND16mr },
74 { X86::AND32ri, X86::AND32mi },
75 { X86::AND32ri8, X86::AND32mi8 },
76 { X86::AND32rr, X86::AND32mr },
77 { X86::AND64ri32, X86::AND64mi32 },
78 { X86::AND64ri8, X86::AND64mi8 },
79 { X86::AND64rr, X86::AND64mr },
80 { X86::AND8ri, X86::AND8mi },
81 { X86::AND8rr, X86::AND8mr },
82 { X86::DEC16r, X86::DEC16m },
83 { X86::DEC32r, X86::DEC32m },
84 { X86::DEC64_16r, X86::DEC64_16m },
85 { X86::DEC64_32r, X86::DEC64_32m },
86 { X86::DEC64r, X86::DEC64m },
87 { X86::DEC8r, X86::DEC8m },
88 { X86::INC16r, X86::INC16m },
89 { X86::INC32r, X86::INC32m },
90 { X86::INC64_16r, X86::INC64_16m },
91 { X86::INC64_32r, X86::INC64_32m },
92 { X86::INC64r, X86::INC64m },
93 { X86::INC8r, X86::INC8m },
94 { X86::NEG16r, X86::NEG16m },
95 { X86::NEG32r, X86::NEG32m },
96 { X86::NEG64r, X86::NEG64m },
97 { X86::NEG8r, X86::NEG8m },
98 { X86::NOT16r, X86::NOT16m },
99 { X86::NOT32r, X86::NOT32m },
100 { X86::NOT64r, X86::NOT64m },
101 { X86::NOT8r, X86::NOT8m },
102 { X86::OR16ri, X86::OR16mi },
103 { X86::OR16ri8, X86::OR16mi8 },
104 { X86::OR16rr, X86::OR16mr },
105 { X86::OR32ri, X86::OR32mi },
106 { X86::OR32ri8, X86::OR32mi8 },
107 { X86::OR32rr, X86::OR32mr },
108 { X86::OR64ri32, X86::OR64mi32 },
109 { X86::OR64ri8, X86::OR64mi8 },
110 { X86::OR64rr, X86::OR64mr },
111 { X86::OR8ri, X86::OR8mi },
112 { X86::OR8rr, X86::OR8mr },
113 { X86::ROL16r1, X86::ROL16m1 },
114 { X86::ROL16rCL, X86::ROL16mCL },
115 { X86::ROL16ri, X86::ROL16mi },
116 { X86::ROL32r1, X86::ROL32m1 },
117 { X86::ROL32rCL, X86::ROL32mCL },
118 { X86::ROL32ri, X86::ROL32mi },
119 { X86::ROL64r1, X86::ROL64m1 },
120 { X86::ROL64rCL, X86::ROL64mCL },
121 { X86::ROL64ri, X86::ROL64mi },
122 { X86::ROL8r1, X86::ROL8m1 },
123 { X86::ROL8rCL, X86::ROL8mCL },
124 { X86::ROL8ri, X86::ROL8mi },
125 { X86::ROR16r1, X86::ROR16m1 },
126 { X86::ROR16rCL, X86::ROR16mCL },
127 { X86::ROR16ri, X86::ROR16mi },
128 { X86::ROR32r1, X86::ROR32m1 },
129 { X86::ROR32rCL, X86::ROR32mCL },
130 { X86::ROR32ri, X86::ROR32mi },
131 { X86::ROR64r1, X86::ROR64m1 },
132 { X86::ROR64rCL, X86::ROR64mCL },
133 { X86::ROR64ri, X86::ROR64mi },
134 { X86::ROR8r1, X86::ROR8m1 },
135 { X86::ROR8rCL, X86::ROR8mCL },
136 { X86::ROR8ri, X86::ROR8mi },
137 { X86::SAR16r1, X86::SAR16m1 },
138 { X86::SAR16rCL, X86::SAR16mCL },
139 { X86::SAR16ri, X86::SAR16mi },
140 { X86::SAR32r1, X86::SAR32m1 },
141 { X86::SAR32rCL, X86::SAR32mCL },
142 { X86::SAR32ri, X86::SAR32mi },
143 { X86::SAR64r1, X86::SAR64m1 },
144 { X86::SAR64rCL, X86::SAR64mCL },
145 { X86::SAR64ri, X86::SAR64mi },
146 { X86::SAR8r1, X86::SAR8m1 },
147 { X86::SAR8rCL, X86::SAR8mCL },
148 { X86::SAR8ri, X86::SAR8mi },
149 { X86::SBB32ri, X86::SBB32mi },
150 { X86::SBB32ri8, X86::SBB32mi8 },
151 { X86::SBB32rr, X86::SBB32mr },
152 { X86::SBB64ri32, X86::SBB64mi32 },
153 { X86::SBB64ri8, X86::SBB64mi8 },
154 { X86::SBB64rr, X86::SBB64mr },
Owen Anderson43dbe052008-01-07 01:35:02 +0000155 { X86::SHL16rCL, X86::SHL16mCL },
156 { X86::SHL16ri, X86::SHL16mi },
Owen Anderson43dbe052008-01-07 01:35:02 +0000157 { X86::SHL32rCL, X86::SHL32mCL },
158 { X86::SHL32ri, X86::SHL32mi },
Owen Anderson43dbe052008-01-07 01:35:02 +0000159 { X86::SHL64rCL, X86::SHL64mCL },
160 { X86::SHL64ri, X86::SHL64mi },
Owen Anderson43dbe052008-01-07 01:35:02 +0000161 { X86::SHL8rCL, X86::SHL8mCL },
162 { X86::SHL8ri, X86::SHL8mi },
163 { X86::SHLD16rrCL, X86::SHLD16mrCL },
164 { X86::SHLD16rri8, X86::SHLD16mri8 },
165 { X86::SHLD32rrCL, X86::SHLD32mrCL },
166 { X86::SHLD32rri8, X86::SHLD32mri8 },
167 { X86::SHLD64rrCL, X86::SHLD64mrCL },
168 { X86::SHLD64rri8, X86::SHLD64mri8 },
169 { X86::SHR16r1, X86::SHR16m1 },
170 { X86::SHR16rCL, X86::SHR16mCL },
171 { X86::SHR16ri, X86::SHR16mi },
172 { X86::SHR32r1, X86::SHR32m1 },
173 { X86::SHR32rCL, X86::SHR32mCL },
174 { X86::SHR32ri, X86::SHR32mi },
175 { X86::SHR64r1, X86::SHR64m1 },
176 { X86::SHR64rCL, X86::SHR64mCL },
177 { X86::SHR64ri, X86::SHR64mi },
178 { X86::SHR8r1, X86::SHR8m1 },
179 { X86::SHR8rCL, X86::SHR8mCL },
180 { X86::SHR8ri, X86::SHR8mi },
181 { X86::SHRD16rrCL, X86::SHRD16mrCL },
182 { X86::SHRD16rri8, X86::SHRD16mri8 },
183 { X86::SHRD32rrCL, X86::SHRD32mrCL },
184 { X86::SHRD32rri8, X86::SHRD32mri8 },
185 { X86::SHRD64rrCL, X86::SHRD64mrCL },
186 { X86::SHRD64rri8, X86::SHRD64mri8 },
187 { X86::SUB16ri, X86::SUB16mi },
188 { X86::SUB16ri8, X86::SUB16mi8 },
189 { X86::SUB16rr, X86::SUB16mr },
190 { X86::SUB32ri, X86::SUB32mi },
191 { X86::SUB32ri8, X86::SUB32mi8 },
192 { X86::SUB32rr, X86::SUB32mr },
193 { X86::SUB64ri32, X86::SUB64mi32 },
194 { X86::SUB64ri8, X86::SUB64mi8 },
195 { X86::SUB64rr, X86::SUB64mr },
196 { X86::SUB8ri, X86::SUB8mi },
197 { X86::SUB8rr, X86::SUB8mr },
198 { X86::XOR16ri, X86::XOR16mi },
199 { X86::XOR16ri8, X86::XOR16mi8 },
200 { X86::XOR16rr, X86::XOR16mr },
201 { X86::XOR32ri, X86::XOR32mi },
202 { X86::XOR32ri8, X86::XOR32mi8 },
203 { X86::XOR32rr, X86::XOR32mr },
204 { X86::XOR64ri32, X86::XOR64mi32 },
205 { X86::XOR64ri8, X86::XOR64mi8 },
206 { X86::XOR64rr, X86::XOR64mr },
207 { X86::XOR8ri, X86::XOR8mi },
208 { X86::XOR8rr, X86::XOR8mr }
209 };
210
211 for (unsigned i = 0, e = array_lengthof(OpTbl2Addr); i != e; ++i) {
212 unsigned RegOp = OpTbl2Addr[i][0];
213 unsigned MemOp = OpTbl2Addr[i][1];
Dan Gohman6b345ee2008-07-07 17:46:23 +0000214 if (!RegOp2MemOpTable2Addr.insert(std::make_pair((unsigned*)RegOp,
215 MemOp)).second)
Owen Anderson43dbe052008-01-07 01:35:02 +0000216 assert(false && "Duplicated entries?");
217 unsigned AuxInfo = 0 | (1 << 4) | (1 << 5); // Index 0,folded load and store
218 if (!MemOp2RegOpTable.insert(std::make_pair((unsigned*)MemOp,
Dan Gohman6b345ee2008-07-07 17:46:23 +0000219 std::make_pair(RegOp,
220 AuxInfo))).second)
Owen Anderson43dbe052008-01-07 01:35:02 +0000221 AmbEntries.push_back(MemOp);
222 }
223
224 // If the third value is 1, then it's folding either a load or a store.
225 static const unsigned OpTbl0[][3] = {
Dan Gohman41b762a2009-01-15 17:57:09 +0000226 { X86::BT16ri8, X86::BT16mi8, 1 },
227 { X86::BT32ri8, X86::BT32mi8, 1 },
228 { X86::BT64ri8, X86::BT64mi8, 1 },
Owen Anderson43dbe052008-01-07 01:35:02 +0000229 { X86::CALL32r, X86::CALL32m, 1 },
230 { X86::CALL64r, X86::CALL64m, 1 },
231 { X86::CMP16ri, X86::CMP16mi, 1 },
232 { X86::CMP16ri8, X86::CMP16mi8, 1 },
Dan Gohman27845362008-03-25 16:53:19 +0000233 { X86::CMP16rr, X86::CMP16mr, 1 },
Owen Anderson43dbe052008-01-07 01:35:02 +0000234 { X86::CMP32ri, X86::CMP32mi, 1 },
235 { X86::CMP32ri8, X86::CMP32mi8, 1 },
Dan Gohman27845362008-03-25 16:53:19 +0000236 { X86::CMP32rr, X86::CMP32mr, 1 },
Owen Anderson43dbe052008-01-07 01:35:02 +0000237 { X86::CMP64ri32, X86::CMP64mi32, 1 },
238 { X86::CMP64ri8, X86::CMP64mi8, 1 },
Dan Gohman27845362008-03-25 16:53:19 +0000239 { X86::CMP64rr, X86::CMP64mr, 1 },
Owen Anderson43dbe052008-01-07 01:35:02 +0000240 { X86::CMP8ri, X86::CMP8mi, 1 },
Dan Gohman27845362008-03-25 16:53:19 +0000241 { X86::CMP8rr, X86::CMP8mr, 1 },
Owen Anderson43dbe052008-01-07 01:35:02 +0000242 { X86::DIV16r, X86::DIV16m, 1 },
243 { X86::DIV32r, X86::DIV32m, 1 },
244 { X86::DIV64r, X86::DIV64m, 1 },
245 { X86::DIV8r, X86::DIV8m, 1 },
Dan Gohmand9ced092008-08-08 18:30:21 +0000246 { X86::EXTRACTPSrr, X86::EXTRACTPSmr, 0 },
Owen Anderson43dbe052008-01-07 01:35:02 +0000247 { X86::FsMOVAPDrr, X86::MOVSDmr, 0 },
248 { X86::FsMOVAPSrr, X86::MOVSSmr, 0 },
249 { X86::IDIV16r, X86::IDIV16m, 1 },
250 { X86::IDIV32r, X86::IDIV32m, 1 },
251 { X86::IDIV64r, X86::IDIV64m, 1 },
252 { X86::IDIV8r, X86::IDIV8m, 1 },
253 { X86::IMUL16r, X86::IMUL16m, 1 },
254 { X86::IMUL32r, X86::IMUL32m, 1 },
255 { X86::IMUL64r, X86::IMUL64m, 1 },
256 { X86::IMUL8r, X86::IMUL8m, 1 },
257 { X86::JMP32r, X86::JMP32m, 1 },
258 { X86::JMP64r, X86::JMP64m, 1 },
259 { X86::MOV16ri, X86::MOV16mi, 0 },
260 { X86::MOV16rr, X86::MOV16mr, 0 },
261 { X86::MOV16to16_, X86::MOV16_mr, 0 },
262 { X86::MOV32ri, X86::MOV32mi, 0 },
263 { X86::MOV32rr, X86::MOV32mr, 0 },
264 { X86::MOV32to32_, X86::MOV32_mr, 0 },
265 { X86::MOV64ri32, X86::MOV64mi32, 0 },
266 { X86::MOV64rr, X86::MOV64mr, 0 },
267 { X86::MOV8ri, X86::MOV8mi, 0 },
268 { X86::MOV8rr, X86::MOV8mr, 0 },
269 { X86::MOVAPDrr, X86::MOVAPDmr, 0 },
270 { X86::MOVAPSrr, X86::MOVAPSmr, 0 },
Dan Gohman54462742009-01-09 02:40:34 +0000271 { X86::MOVDQArr, X86::MOVDQAmr, 0 },
Owen Anderson43dbe052008-01-07 01:35:02 +0000272 { X86::MOVPDI2DIrr, X86::MOVPDI2DImr, 0 },
273 { X86::MOVPQIto64rr,X86::MOVPQI2QImr, 0 },
274 { X86::MOVPS2SSrr, X86::MOVPS2SSmr, 0 },
275 { X86::MOVSDrr, X86::MOVSDmr, 0 },
276 { X86::MOVSDto64rr, X86::MOVSDto64mr, 0 },
277 { X86::MOVSS2DIrr, X86::MOVSS2DImr, 0 },
278 { X86::MOVSSrr, X86::MOVSSmr, 0 },
279 { X86::MOVUPDrr, X86::MOVUPDmr, 0 },
280 { X86::MOVUPSrr, X86::MOVUPSmr, 0 },
281 { X86::MUL16r, X86::MUL16m, 1 },
282 { X86::MUL32r, X86::MUL32m, 1 },
283 { X86::MUL64r, X86::MUL64m, 1 },
284 { X86::MUL8r, X86::MUL8m, 1 },
285 { X86::SETAEr, X86::SETAEm, 0 },
286 { X86::SETAr, X86::SETAm, 0 },
287 { X86::SETBEr, X86::SETBEm, 0 },
288 { X86::SETBr, X86::SETBm, 0 },
289 { X86::SETEr, X86::SETEm, 0 },
290 { X86::SETGEr, X86::SETGEm, 0 },
291 { X86::SETGr, X86::SETGm, 0 },
292 { X86::SETLEr, X86::SETLEm, 0 },
293 { X86::SETLr, X86::SETLm, 0 },
294 { X86::SETNEr, X86::SETNEm, 0 },
Bill Wendling9f248742008-12-02 00:07:05 +0000295 { X86::SETNOr, X86::SETNOm, 0 },
Owen Anderson43dbe052008-01-07 01:35:02 +0000296 { X86::SETNPr, X86::SETNPm, 0 },
297 { X86::SETNSr, X86::SETNSm, 0 },
Bill Wendling9f248742008-12-02 00:07:05 +0000298 { X86::SETOr, X86::SETOm, 0 },
Owen Anderson43dbe052008-01-07 01:35:02 +0000299 { X86::SETPr, X86::SETPm, 0 },
300 { X86::SETSr, X86::SETSm, 0 },
301 { X86::TAILJMPr, X86::TAILJMPm, 1 },
302 { X86::TEST16ri, X86::TEST16mi, 1 },
303 { X86::TEST32ri, X86::TEST32mi, 1 },
304 { X86::TEST64ri32, X86::TEST64mi32, 1 },
Chris Lattnerf9b3f372008-01-11 18:00:50 +0000305 { X86::TEST8ri, X86::TEST8mi, 1 }
Owen Anderson43dbe052008-01-07 01:35:02 +0000306 };
307
308 for (unsigned i = 0, e = array_lengthof(OpTbl0); i != e; ++i) {
309 unsigned RegOp = OpTbl0[i][0];
310 unsigned MemOp = OpTbl0[i][1];
Dan Gohman6b345ee2008-07-07 17:46:23 +0000311 if (!RegOp2MemOpTable0.insert(std::make_pair((unsigned*)RegOp,
312 MemOp)).second)
Owen Anderson43dbe052008-01-07 01:35:02 +0000313 assert(false && "Duplicated entries?");
314 unsigned FoldedLoad = OpTbl0[i][2];
315 // Index 0, folded load or store.
316 unsigned AuxInfo = 0 | (FoldedLoad << 4) | ((FoldedLoad^1) << 5);
317 if (RegOp != X86::FsMOVAPDrr && RegOp != X86::FsMOVAPSrr)
318 if (!MemOp2RegOpTable.insert(std::make_pair((unsigned*)MemOp,
Dan Gohman6b345ee2008-07-07 17:46:23 +0000319 std::make_pair(RegOp, AuxInfo))).second)
Owen Anderson43dbe052008-01-07 01:35:02 +0000320 AmbEntries.push_back(MemOp);
321 }
322
323 static const unsigned OpTbl1[][2] = {
324 { X86::CMP16rr, X86::CMP16rm },
325 { X86::CMP32rr, X86::CMP32rm },
326 { X86::CMP64rr, X86::CMP64rm },
327 { X86::CMP8rr, X86::CMP8rm },
328 { X86::CVTSD2SSrr, X86::CVTSD2SSrm },
329 { X86::CVTSI2SD64rr, X86::CVTSI2SD64rm },
330 { X86::CVTSI2SDrr, X86::CVTSI2SDrm },
331 { X86::CVTSI2SS64rr, X86::CVTSI2SS64rm },
332 { X86::CVTSI2SSrr, X86::CVTSI2SSrm },
333 { X86::CVTSS2SDrr, X86::CVTSS2SDrm },
334 { X86::CVTTSD2SI64rr, X86::CVTTSD2SI64rm },
335 { X86::CVTTSD2SIrr, X86::CVTTSD2SIrm },
336 { X86::CVTTSS2SI64rr, X86::CVTTSS2SI64rm },
337 { X86::CVTTSS2SIrr, X86::CVTTSS2SIrm },
338 { X86::FsMOVAPDrr, X86::MOVSDrm },
339 { X86::FsMOVAPSrr, X86::MOVSSrm },
340 { X86::IMUL16rri, X86::IMUL16rmi },
341 { X86::IMUL16rri8, X86::IMUL16rmi8 },
342 { X86::IMUL32rri, X86::IMUL32rmi },
343 { X86::IMUL32rri8, X86::IMUL32rmi8 },
344 { X86::IMUL64rri32, X86::IMUL64rmi32 },
345 { X86::IMUL64rri8, X86::IMUL64rmi8 },
346 { X86::Int_CMPSDrr, X86::Int_CMPSDrm },
347 { X86::Int_CMPSSrr, X86::Int_CMPSSrm },
348 { X86::Int_COMISDrr, X86::Int_COMISDrm },
349 { X86::Int_COMISSrr, X86::Int_COMISSrm },
350 { X86::Int_CVTDQ2PDrr, X86::Int_CVTDQ2PDrm },
351 { X86::Int_CVTDQ2PSrr, X86::Int_CVTDQ2PSrm },
352 { X86::Int_CVTPD2DQrr, X86::Int_CVTPD2DQrm },
353 { X86::Int_CVTPD2PSrr, X86::Int_CVTPD2PSrm },
354 { X86::Int_CVTPS2DQrr, X86::Int_CVTPS2DQrm },
355 { X86::Int_CVTPS2PDrr, X86::Int_CVTPS2PDrm },
356 { X86::Int_CVTSD2SI64rr,X86::Int_CVTSD2SI64rm },
357 { X86::Int_CVTSD2SIrr, X86::Int_CVTSD2SIrm },
358 { X86::Int_CVTSD2SSrr, X86::Int_CVTSD2SSrm },
359 { X86::Int_CVTSI2SD64rr,X86::Int_CVTSI2SD64rm },
360 { X86::Int_CVTSI2SDrr, X86::Int_CVTSI2SDrm },
361 { X86::Int_CVTSI2SS64rr,X86::Int_CVTSI2SS64rm },
362 { X86::Int_CVTSI2SSrr, X86::Int_CVTSI2SSrm },
363 { X86::Int_CVTSS2SDrr, X86::Int_CVTSS2SDrm },
364 { X86::Int_CVTSS2SI64rr,X86::Int_CVTSS2SI64rm },
365 { X86::Int_CVTSS2SIrr, X86::Int_CVTSS2SIrm },
366 { X86::Int_CVTTPD2DQrr, X86::Int_CVTTPD2DQrm },
367 { X86::Int_CVTTPS2DQrr, X86::Int_CVTTPS2DQrm },
368 { X86::Int_CVTTSD2SI64rr,X86::Int_CVTTSD2SI64rm },
369 { X86::Int_CVTTSD2SIrr, X86::Int_CVTTSD2SIrm },
370 { X86::Int_CVTTSS2SI64rr,X86::Int_CVTTSS2SI64rm },
371 { X86::Int_CVTTSS2SIrr, X86::Int_CVTTSS2SIrm },
372 { X86::Int_UCOMISDrr, X86::Int_UCOMISDrm },
373 { X86::Int_UCOMISSrr, X86::Int_UCOMISSrm },
374 { X86::MOV16rr, X86::MOV16rm },
375 { X86::MOV16to16_, X86::MOV16_rm },
376 { X86::MOV32rr, X86::MOV32rm },
377 { X86::MOV32to32_, X86::MOV32_rm },
378 { X86::MOV64rr, X86::MOV64rm },
379 { X86::MOV64toPQIrr, X86::MOVQI2PQIrm },
380 { X86::MOV64toSDrr, X86::MOV64toSDrm },
381 { X86::MOV8rr, X86::MOV8rm },
382 { X86::MOVAPDrr, X86::MOVAPDrm },
383 { X86::MOVAPSrr, X86::MOVAPSrm },
384 { X86::MOVDDUPrr, X86::MOVDDUPrm },
385 { X86::MOVDI2PDIrr, X86::MOVDI2PDIrm },
386 { X86::MOVDI2SSrr, X86::MOVDI2SSrm },
Dan Gohman54462742009-01-09 02:40:34 +0000387 { X86::MOVDQArr, X86::MOVDQArm },
Owen Anderson43dbe052008-01-07 01:35:02 +0000388 { X86::MOVSD2PDrr, X86::MOVSD2PDrm },
389 { X86::MOVSDrr, X86::MOVSDrm },
390 { X86::MOVSHDUPrr, X86::MOVSHDUPrm },
391 { X86::MOVSLDUPrr, X86::MOVSLDUPrm },
392 { X86::MOVSS2PSrr, X86::MOVSS2PSrm },
393 { X86::MOVSSrr, X86::MOVSSrm },
394 { X86::MOVSX16rr8, X86::MOVSX16rm8 },
395 { X86::MOVSX32rr16, X86::MOVSX32rm16 },
396 { X86::MOVSX32rr8, X86::MOVSX32rm8 },
397 { X86::MOVSX64rr16, X86::MOVSX64rm16 },
398 { X86::MOVSX64rr32, X86::MOVSX64rm32 },
399 { X86::MOVSX64rr8, X86::MOVSX64rm8 },
400 { X86::MOVUPDrr, X86::MOVUPDrm },
401 { X86::MOVUPSrr, X86::MOVUPSrm },
402 { X86::MOVZDI2PDIrr, X86::MOVZDI2PDIrm },
403 { X86::MOVZQI2PQIrr, X86::MOVZQI2PQIrm },
404 { X86::MOVZPQILo2PQIrr, X86::MOVZPQILo2PQIrm },
405 { X86::MOVZX16rr8, X86::MOVZX16rm8 },
406 { X86::MOVZX32rr16, X86::MOVZX32rm16 },
407 { X86::MOVZX32rr8, X86::MOVZX32rm8 },
408 { X86::MOVZX64rr16, X86::MOVZX64rm16 },
Dan Gohmane3d92062008-08-07 02:54:50 +0000409 { X86::MOVZX64rr32, X86::MOVZX64rm32 },
Owen Anderson43dbe052008-01-07 01:35:02 +0000410 { X86::MOVZX64rr8, X86::MOVZX64rm8 },
411 { X86::PSHUFDri, X86::PSHUFDmi },
412 { X86::PSHUFHWri, X86::PSHUFHWmi },
413 { X86::PSHUFLWri, X86::PSHUFLWmi },
Owen Anderson43dbe052008-01-07 01:35:02 +0000414 { X86::RCPPSr, X86::RCPPSm },
415 { X86::RCPPSr_Int, X86::RCPPSm_Int },
416 { X86::RSQRTPSr, X86::RSQRTPSm },
417 { X86::RSQRTPSr_Int, X86::RSQRTPSm_Int },
418 { X86::RSQRTSSr, X86::RSQRTSSm },
419 { X86::RSQRTSSr_Int, X86::RSQRTSSm_Int },
420 { X86::SQRTPDr, X86::SQRTPDm },
421 { X86::SQRTPDr_Int, X86::SQRTPDm_Int },
422 { X86::SQRTPSr, X86::SQRTPSm },
423 { X86::SQRTPSr_Int, X86::SQRTPSm_Int },
424 { X86::SQRTSDr, X86::SQRTSDm },
425 { X86::SQRTSDr_Int, X86::SQRTSDm_Int },
426 { X86::SQRTSSr, X86::SQRTSSm },
427 { X86::SQRTSSr_Int, X86::SQRTSSm_Int },
428 { X86::TEST16rr, X86::TEST16rm },
429 { X86::TEST32rr, X86::TEST32rm },
430 { X86::TEST64rr, X86::TEST64rm },
431 { X86::TEST8rr, X86::TEST8rm },
432 // FIXME: TEST*rr EAX,EAX ---> CMP [mem], 0
433 { X86::UCOMISDrr, X86::UCOMISDrm },
Chris Lattnerf9b3f372008-01-11 18:00:50 +0000434 { X86::UCOMISSrr, X86::UCOMISSrm }
Owen Anderson43dbe052008-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];
439 unsigned MemOp = OpTbl1[i][1];
Dan Gohman6b345ee2008-07-07 17:46:23 +0000440 if (!RegOp2MemOpTable1.insert(std::make_pair((unsigned*)RegOp,
441 MemOp)).second)
Owen Anderson43dbe052008-01-07 01:35:02 +0000442 assert(false && "Duplicated entries?");
443 unsigned AuxInfo = 1 | (1 << 4); // Index 1, folded load
444 if (RegOp != X86::FsMOVAPDrr && RegOp != X86::FsMOVAPSrr)
445 if (!MemOp2RegOpTable.insert(std::make_pair((unsigned*)MemOp,
Dan Gohman6b345ee2008-07-07 17:46:23 +0000446 std::make_pair(RegOp, AuxInfo))).second)
Owen Anderson43dbe052008-01-07 01:35:02 +0000447 AmbEntries.push_back(MemOp);
448 }
449
450 static const unsigned OpTbl2[][2] = {
451 { X86::ADC32rr, X86::ADC32rm },
452 { X86::ADC64rr, X86::ADC64rm },
453 { X86::ADD16rr, X86::ADD16rm },
454 { X86::ADD32rr, X86::ADD32rm },
455 { X86::ADD64rr, X86::ADD64rm },
456 { X86::ADD8rr, X86::ADD8rm },
457 { X86::ADDPDrr, X86::ADDPDrm },
458 { X86::ADDPSrr, X86::ADDPSrm },
459 { X86::ADDSDrr, X86::ADDSDrm },
460 { X86::ADDSSrr, X86::ADDSSrm },
461 { X86::ADDSUBPDrr, X86::ADDSUBPDrm },
462 { X86::ADDSUBPSrr, X86::ADDSUBPSrm },
463 { X86::AND16rr, X86::AND16rm },
464 { X86::AND32rr, X86::AND32rm },
465 { X86::AND64rr, X86::AND64rm },
466 { X86::AND8rr, X86::AND8rm },
467 { X86::ANDNPDrr, X86::ANDNPDrm },
468 { X86::ANDNPSrr, X86::ANDNPSrm },
469 { X86::ANDPDrr, X86::ANDPDrm },
470 { X86::ANDPSrr, X86::ANDPSrm },
471 { X86::CMOVA16rr, X86::CMOVA16rm },
472 { X86::CMOVA32rr, X86::CMOVA32rm },
473 { X86::CMOVA64rr, X86::CMOVA64rm },
474 { X86::CMOVAE16rr, X86::CMOVAE16rm },
475 { X86::CMOVAE32rr, X86::CMOVAE32rm },
476 { X86::CMOVAE64rr, X86::CMOVAE64rm },
477 { X86::CMOVB16rr, X86::CMOVB16rm },
478 { X86::CMOVB32rr, X86::CMOVB32rm },
479 { X86::CMOVB64rr, X86::CMOVB64rm },
480 { X86::CMOVBE16rr, X86::CMOVBE16rm },
481 { X86::CMOVBE32rr, X86::CMOVBE32rm },
482 { X86::CMOVBE64rr, X86::CMOVBE64rm },
483 { X86::CMOVE16rr, X86::CMOVE16rm },
484 { X86::CMOVE32rr, X86::CMOVE32rm },
485 { X86::CMOVE64rr, X86::CMOVE64rm },
486 { X86::CMOVG16rr, X86::CMOVG16rm },
487 { X86::CMOVG32rr, X86::CMOVG32rm },
488 { X86::CMOVG64rr, X86::CMOVG64rm },
489 { X86::CMOVGE16rr, X86::CMOVGE16rm },
490 { X86::CMOVGE32rr, X86::CMOVGE32rm },
491 { X86::CMOVGE64rr, X86::CMOVGE64rm },
492 { X86::CMOVL16rr, X86::CMOVL16rm },
493 { X86::CMOVL32rr, X86::CMOVL32rm },
494 { X86::CMOVL64rr, X86::CMOVL64rm },
495 { X86::CMOVLE16rr, X86::CMOVLE16rm },
496 { X86::CMOVLE32rr, X86::CMOVLE32rm },
497 { X86::CMOVLE64rr, X86::CMOVLE64rm },
498 { X86::CMOVNE16rr, X86::CMOVNE16rm },
499 { X86::CMOVNE32rr, X86::CMOVNE32rm },
500 { X86::CMOVNE64rr, X86::CMOVNE64rm },
Dan Gohmanb33fc7b2009-01-07 00:44:53 +0000501 { X86::CMOVNO16rr, X86::CMOVNO16rm },
502 { X86::CMOVNO32rr, X86::CMOVNO32rm },
503 { X86::CMOVNO64rr, X86::CMOVNO64rm },
Owen Anderson43dbe052008-01-07 01:35:02 +0000504 { X86::CMOVNP16rr, X86::CMOVNP16rm },
505 { X86::CMOVNP32rr, X86::CMOVNP32rm },
506 { X86::CMOVNP64rr, X86::CMOVNP64rm },
507 { X86::CMOVNS16rr, X86::CMOVNS16rm },
508 { X86::CMOVNS32rr, X86::CMOVNS32rm },
509 { X86::CMOVNS64rr, X86::CMOVNS64rm },
Dan Gohman305fceb2009-01-07 00:35:10 +0000510 { X86::CMOVO16rr, X86::CMOVO16rm },
511 { X86::CMOVO32rr, X86::CMOVO32rm },
512 { X86::CMOVO64rr, X86::CMOVO64rm },
Owen Anderson43dbe052008-01-07 01:35:02 +0000513 { X86::CMOVP16rr, X86::CMOVP16rm },
514 { X86::CMOVP32rr, X86::CMOVP32rm },
515 { X86::CMOVP64rr, X86::CMOVP64rm },
516 { X86::CMOVS16rr, X86::CMOVS16rm },
517 { X86::CMOVS32rr, X86::CMOVS32rm },
518 { X86::CMOVS64rr, X86::CMOVS64rm },
519 { X86::CMPPDrri, X86::CMPPDrmi },
520 { X86::CMPPSrri, X86::CMPPSrmi },
521 { X86::CMPSDrr, X86::CMPSDrm },
522 { X86::CMPSSrr, X86::CMPSSrm },
523 { X86::DIVPDrr, X86::DIVPDrm },
524 { X86::DIVPSrr, X86::DIVPSrm },
525 { X86::DIVSDrr, X86::DIVSDrm },
526 { X86::DIVSSrr, X86::DIVSSrm },
Evan Cheng082f1162008-05-02 17:01:01 +0000527 { X86::FsANDNPDrr, X86::FsANDNPDrm },
528 { X86::FsANDNPSrr, X86::FsANDNPSrm },
529 { X86::FsANDPDrr, X86::FsANDPDrm },
530 { X86::FsANDPSrr, X86::FsANDPSrm },
531 { X86::FsORPDrr, X86::FsORPDrm },
532 { X86::FsORPSrr, X86::FsORPSrm },
533 { X86::FsXORPDrr, X86::FsXORPDrm },
534 { X86::FsXORPSrr, X86::FsXORPSrm },
Owen Anderson43dbe052008-01-07 01:35:02 +0000535 { X86::HADDPDrr, X86::HADDPDrm },
536 { X86::HADDPSrr, X86::HADDPSrm },
537 { X86::HSUBPDrr, X86::HSUBPDrm },
538 { X86::HSUBPSrr, X86::HSUBPSrm },
539 { X86::IMUL16rr, X86::IMUL16rm },
540 { X86::IMUL32rr, X86::IMUL32rm },
541 { X86::IMUL64rr, X86::IMUL64rm },
542 { X86::MAXPDrr, X86::MAXPDrm },
543 { X86::MAXPDrr_Int, X86::MAXPDrm_Int },
544 { X86::MAXPSrr, X86::MAXPSrm },
545 { X86::MAXPSrr_Int, X86::MAXPSrm_Int },
546 { X86::MAXSDrr, X86::MAXSDrm },
547 { X86::MAXSDrr_Int, X86::MAXSDrm_Int },
548 { X86::MAXSSrr, X86::MAXSSrm },
549 { X86::MAXSSrr_Int, X86::MAXSSrm_Int },
550 { X86::MINPDrr, X86::MINPDrm },
551 { X86::MINPDrr_Int, X86::MINPDrm_Int },
552 { X86::MINPSrr, X86::MINPSrm },
553 { X86::MINPSrr_Int, X86::MINPSrm_Int },
554 { X86::MINSDrr, X86::MINSDrm },
555 { X86::MINSDrr_Int, X86::MINSDrm_Int },
556 { X86::MINSSrr, X86::MINSSrm },
557 { X86::MINSSrr_Int, X86::MINSSrm_Int },
558 { X86::MULPDrr, X86::MULPDrm },
559 { X86::MULPSrr, X86::MULPSrm },
560 { X86::MULSDrr, X86::MULSDrm },
561 { X86::MULSSrr, X86::MULSSrm },
562 { X86::OR16rr, X86::OR16rm },
563 { X86::OR32rr, X86::OR32rm },
564 { X86::OR64rr, X86::OR64rm },
565 { X86::OR8rr, X86::OR8rm },
566 { X86::ORPDrr, X86::ORPDrm },
567 { X86::ORPSrr, X86::ORPSrm },
568 { X86::PACKSSDWrr, X86::PACKSSDWrm },
569 { X86::PACKSSWBrr, X86::PACKSSWBrm },
570 { X86::PACKUSWBrr, X86::PACKUSWBrm },
571 { X86::PADDBrr, X86::PADDBrm },
572 { X86::PADDDrr, X86::PADDDrm },
573 { X86::PADDQrr, X86::PADDQrm },
574 { X86::PADDSBrr, X86::PADDSBrm },
575 { X86::PADDSWrr, X86::PADDSWrm },
576 { X86::PADDWrr, X86::PADDWrm },
577 { X86::PANDNrr, X86::PANDNrm },
578 { X86::PANDrr, X86::PANDrm },
579 { X86::PAVGBrr, X86::PAVGBrm },
580 { X86::PAVGWrr, X86::PAVGWrm },
581 { X86::PCMPEQBrr, X86::PCMPEQBrm },
582 { X86::PCMPEQDrr, X86::PCMPEQDrm },
583 { X86::PCMPEQWrr, X86::PCMPEQWrm },
584 { X86::PCMPGTBrr, X86::PCMPGTBrm },
585 { X86::PCMPGTDrr, X86::PCMPGTDrm },
586 { X86::PCMPGTWrr, X86::PCMPGTWrm },
587 { X86::PINSRWrri, X86::PINSRWrmi },
588 { X86::PMADDWDrr, X86::PMADDWDrm },
589 { X86::PMAXSWrr, X86::PMAXSWrm },
590 { X86::PMAXUBrr, X86::PMAXUBrm },
591 { X86::PMINSWrr, X86::PMINSWrm },
592 { X86::PMINUBrr, X86::PMINUBrm },
Dan Gohman0b924dc2008-05-23 17:49:40 +0000593 { X86::PMULDQrr, X86::PMULDQrm },
Owen Anderson43dbe052008-01-07 01:35:02 +0000594 { X86::PMULHUWrr, X86::PMULHUWrm },
595 { X86::PMULHWrr, X86::PMULHWrm },
Dan Gohman0b924dc2008-05-23 17:49:40 +0000596 { X86::PMULLDrr, X86::PMULLDrm },
597 { X86::PMULLDrr_int, X86::PMULLDrm_int },
Owen Anderson43dbe052008-01-07 01:35:02 +0000598 { X86::PMULLWrr, X86::PMULLWrm },
599 { X86::PMULUDQrr, X86::PMULUDQrm },
600 { X86::PORrr, X86::PORrm },
601 { X86::PSADBWrr, X86::PSADBWrm },
602 { X86::PSLLDrr, X86::PSLLDrm },
603 { X86::PSLLQrr, X86::PSLLQrm },
604 { X86::PSLLWrr, X86::PSLLWrm },
605 { X86::PSRADrr, X86::PSRADrm },
606 { X86::PSRAWrr, X86::PSRAWrm },
607 { X86::PSRLDrr, X86::PSRLDrm },
608 { X86::PSRLQrr, X86::PSRLQrm },
609 { X86::PSRLWrr, X86::PSRLWrm },
610 { X86::PSUBBrr, X86::PSUBBrm },
611 { X86::PSUBDrr, X86::PSUBDrm },
612 { X86::PSUBSBrr, X86::PSUBSBrm },
613 { X86::PSUBSWrr, X86::PSUBSWrm },
614 { X86::PSUBWrr, X86::PSUBWrm },
615 { X86::PUNPCKHBWrr, X86::PUNPCKHBWrm },
616 { X86::PUNPCKHDQrr, X86::PUNPCKHDQrm },
617 { X86::PUNPCKHQDQrr, X86::PUNPCKHQDQrm },
618 { X86::PUNPCKHWDrr, X86::PUNPCKHWDrm },
619 { X86::PUNPCKLBWrr, X86::PUNPCKLBWrm },
620 { X86::PUNPCKLDQrr, X86::PUNPCKLDQrm },
621 { X86::PUNPCKLQDQrr, X86::PUNPCKLQDQrm },
622 { X86::PUNPCKLWDrr, X86::PUNPCKLWDrm },
623 { X86::PXORrr, X86::PXORrm },
624 { X86::SBB32rr, X86::SBB32rm },
625 { X86::SBB64rr, X86::SBB64rm },
626 { X86::SHUFPDrri, X86::SHUFPDrmi },
627 { X86::SHUFPSrri, X86::SHUFPSrmi },
628 { X86::SUB16rr, X86::SUB16rm },
629 { X86::SUB32rr, X86::SUB32rm },
630 { X86::SUB64rr, X86::SUB64rm },
631 { X86::SUB8rr, X86::SUB8rm },
632 { X86::SUBPDrr, X86::SUBPDrm },
633 { X86::SUBPSrr, X86::SUBPSrm },
634 { X86::SUBSDrr, X86::SUBSDrm },
635 { X86::SUBSSrr, X86::SUBSSrm },
636 // FIXME: TEST*rr -> swapped operand of TEST*mr.
637 { X86::UNPCKHPDrr, X86::UNPCKHPDrm },
638 { X86::UNPCKHPSrr, X86::UNPCKHPSrm },
639 { X86::UNPCKLPDrr, X86::UNPCKLPDrm },
640 { X86::UNPCKLPSrr, X86::UNPCKLPSrm },
641 { X86::XOR16rr, X86::XOR16rm },
642 { X86::XOR32rr, X86::XOR32rm },
643 { X86::XOR64rr, X86::XOR64rm },
644 { X86::XOR8rr, X86::XOR8rm },
645 { X86::XORPDrr, X86::XORPDrm },
646 { X86::XORPSrr, X86::XORPSrm }
647 };
648
649 for (unsigned i = 0, e = array_lengthof(OpTbl2); i != e; ++i) {
650 unsigned RegOp = OpTbl2[i][0];
651 unsigned MemOp = OpTbl2[i][1];
Dan Gohman6b345ee2008-07-07 17:46:23 +0000652 if (!RegOp2MemOpTable2.insert(std::make_pair((unsigned*)RegOp,
653 MemOp)).second)
Owen Anderson43dbe052008-01-07 01:35:02 +0000654 assert(false && "Duplicated entries?");
Dan Gohmanfc53fc62009-03-04 19:24:25 +0000655 unsigned AuxInfo = 2 | (1 << 4); // Index 2, folded load
Owen Anderson43dbe052008-01-07 01:35:02 +0000656 if (!MemOp2RegOpTable.insert(std::make_pair((unsigned*)MemOp,
Dan Gohman6b345ee2008-07-07 17:46:23 +0000657 std::make_pair(RegOp, AuxInfo))).second)
Owen Anderson43dbe052008-01-07 01:35:02 +0000658 AmbEntries.push_back(MemOp);
659 }
660
661 // Remove ambiguous entries.
662 assert(AmbEntries.empty() && "Duplicated entries in unfolding maps?");
Chris Lattner72614082002-10-25 22:55:53 +0000663}
664
Alkis Evlogimenos5e300022003-12-28 17:35:08 +0000665bool X86InstrInfo::isMoveInstr(const MachineInstr& MI,
Evan Cheng04ee5a12009-01-20 19:12:24 +0000666 unsigned &SrcReg, unsigned &DstReg,
667 unsigned &SrcSubIdx, unsigned &DstSubIdx) const {
Chris Lattner07f7cc32008-03-11 19:28:17 +0000668 switch (MI.getOpcode()) {
669 default:
670 return false;
671 case X86::MOV8rr:
672 case X86::MOV16rr:
673 case X86::MOV32rr:
674 case X86::MOV64rr:
675 case X86::MOV16to16_:
676 case X86::MOV32to32_:
Chris Lattner07f7cc32008-03-11 19:28:17 +0000677 case X86::MOVSSrr:
678 case X86::MOVSDrr:
Chris Lattner1d386772008-03-11 19:30:09 +0000679
680 // FP Stack register class copies
681 case X86::MOV_Fp3232: case X86::MOV_Fp6464: case X86::MOV_Fp8080:
682 case X86::MOV_Fp3264: case X86::MOV_Fp3280:
683 case X86::MOV_Fp6432: case X86::MOV_Fp8032:
684
Chris Lattner07f7cc32008-03-11 19:28:17 +0000685 case X86::FsMOVAPSrr:
686 case X86::FsMOVAPDrr:
687 case X86::MOVAPSrr:
688 case X86::MOVAPDrr:
Dan Gohman54462742009-01-09 02:40:34 +0000689 case X86::MOVDQArr:
Chris Lattner07f7cc32008-03-11 19:28:17 +0000690 case X86::MOVSS2PSrr:
691 case X86::MOVSD2PDrr:
692 case X86::MOVPS2SSrr:
693 case X86::MOVPD2SDrr:
Chris Lattner07f7cc32008-03-11 19:28:17 +0000694 case X86::MMX_MOVQ64rr:
695 assert(MI.getNumOperands() >= 2 &&
Dan Gohmand735b802008-10-03 15:45:36 +0000696 MI.getOperand(0).isReg() &&
697 MI.getOperand(1).isReg() &&
Chris Lattner07f7cc32008-03-11 19:28:17 +0000698 "invalid register-register move instruction");
Evan Cheng04ee5a12009-01-20 19:12:24 +0000699 SrcReg = MI.getOperand(1).getReg();
700 DstReg = MI.getOperand(0).getReg();
701 SrcSubIdx = MI.getOperand(1).getSubReg();
702 DstSubIdx = MI.getOperand(0).getSubReg();
Chris Lattner07f7cc32008-03-11 19:28:17 +0000703 return true;
Alkis Evlogimenos5e300022003-12-28 17:35:08 +0000704 }
Alkis Evlogimenos5e300022003-12-28 17:35:08 +0000705}
Alkis Evlogimenos36f506e2004-07-31 09:38:47 +0000706
Dan Gohmancbad42c2008-11-18 19:49:32 +0000707unsigned X86InstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
Chris Lattner40839602006-02-02 20:12:32 +0000708 int &FrameIndex) const {
709 switch (MI->getOpcode()) {
710 default: break;
711 case X86::MOV8rm:
712 case X86::MOV16rm:
Evan Chengf4df6802006-05-11 07:33:49 +0000713 case X86::MOV16_rm:
Chris Lattner40839602006-02-02 20:12:32 +0000714 case X86::MOV32rm:
Evan Chengf4df6802006-05-11 07:33:49 +0000715 case X86::MOV32_rm:
Evan Cheng25ab6902006-09-08 06:48:29 +0000716 case X86::MOV64rm:
Dale Johannesene377d4d2007-07-04 21:07:47 +0000717 case X86::LD_Fp64m:
Chris Lattner40839602006-02-02 20:12:32 +0000718 case X86::MOVSSrm:
719 case X86::MOVSDrm:
Chris Lattner993c8972006-04-18 16:44:51 +0000720 case X86::MOVAPSrm:
721 case X86::MOVAPDrm:
Dan Gohman54462742009-01-09 02:40:34 +0000722 case X86::MOVDQArm:
Bill Wendling823efee2007-04-03 06:00:37 +0000723 case X86::MMX_MOVD64rm:
724 case X86::MMX_MOVQ64rm:
Dan Gohmand735b802008-10-03 15:45:36 +0000725 if (MI->getOperand(1).isFI() && MI->getOperand(2).isImm() &&
726 MI->getOperand(3).isReg() && MI->getOperand(4).isImm() &&
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000727 MI->getOperand(2).getImm() == 1 &&
Chris Lattner40839602006-02-02 20:12:32 +0000728 MI->getOperand(3).getReg() == 0 &&
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000729 MI->getOperand(4).getImm() == 0) {
Chris Lattner8aa797a2007-12-30 23:10:15 +0000730 FrameIndex = MI->getOperand(1).getIndex();
Chris Lattner40839602006-02-02 20:12:32 +0000731 return MI->getOperand(0).getReg();
732 }
733 break;
734 }
735 return 0;
736}
737
Dan Gohmancbad42c2008-11-18 19:49:32 +0000738unsigned X86InstrInfo::isStoreToStackSlot(const MachineInstr *MI,
Chris Lattner40839602006-02-02 20:12:32 +0000739 int &FrameIndex) const {
740 switch (MI->getOpcode()) {
741 default: break;
742 case X86::MOV8mr:
743 case X86::MOV16mr:
Evan Chengf4df6802006-05-11 07:33:49 +0000744 case X86::MOV16_mr:
Chris Lattner40839602006-02-02 20:12:32 +0000745 case X86::MOV32mr:
Evan Chengf4df6802006-05-11 07:33:49 +0000746 case X86::MOV32_mr:
Evan Cheng25ab6902006-09-08 06:48:29 +0000747 case X86::MOV64mr:
Dale Johannesene377d4d2007-07-04 21:07:47 +0000748 case X86::ST_FpP64m:
Chris Lattner40839602006-02-02 20:12:32 +0000749 case X86::MOVSSmr:
750 case X86::MOVSDmr:
Chris Lattner993c8972006-04-18 16:44:51 +0000751 case X86::MOVAPSmr:
752 case X86::MOVAPDmr:
Dan Gohman54462742009-01-09 02:40:34 +0000753 case X86::MOVDQAmr:
Bill Wendling823efee2007-04-03 06:00:37 +0000754 case X86::MMX_MOVD64mr:
755 case X86::MMX_MOVQ64mr:
Bill Wendling71bfd112007-04-03 23:48:32 +0000756 case X86::MMX_MOVNTQmr:
Dan Gohmand735b802008-10-03 15:45:36 +0000757 if (MI->getOperand(0).isFI() && MI->getOperand(1).isImm() &&
758 MI->getOperand(2).isReg() && MI->getOperand(3).isImm() &&
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000759 MI->getOperand(1).getImm() == 1 &&
Chris Lattner1c07e722006-02-02 20:38:12 +0000760 MI->getOperand(2).getReg() == 0 &&
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000761 MI->getOperand(3).getImm() == 0) {
Chris Lattner8aa797a2007-12-30 23:10:15 +0000762 FrameIndex = MI->getOperand(0).getIndex();
Rafael Espindolab449a682009-03-28 17:03:24 +0000763 return MI->getOperand(X86AddrNumOperands).getReg();
Chris Lattner40839602006-02-02 20:12:32 +0000764 }
765 break;
766 }
767 return 0;
768}
769
770
Evan Chenge3d8dbf2008-03-27 01:45:11 +0000771/// regIsPICBase - Return true if register is PIC base (i.e.g defined by
772/// X86::MOVPC32r.
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000773static bool regIsPICBase(unsigned BaseReg, const MachineRegisterInfo &MRI) {
Evan Chenge3d8dbf2008-03-27 01:45:11 +0000774 bool isPICBase = false;
775 for (MachineRegisterInfo::def_iterator I = MRI.def_begin(BaseReg),
776 E = MRI.def_end(); I != E; ++I) {
777 MachineInstr *DefMI = I.getOperand().getParent();
778 if (DefMI->getOpcode() != X86::MOVPC32r)
779 return false;
780 assert(!isPICBase && "More than one PIC base?");
781 isPICBase = true;
782 }
783 return isPICBase;
784}
Evan Cheng9d15abe2008-03-31 07:54:19 +0000785
786/// isGVStub - Return true if the GV requires an extra load to get the
787/// real address.
788static inline bool isGVStub(GlobalValue *GV, X86TargetMachine &TM) {
789 return TM.getSubtarget<X86Subtarget>().GVRequiresExtraLoad(GV, TM, false);
790}
Evan Chenge771ebd2008-03-27 01:41:09 +0000791
Bill Wendling9f8fea32008-05-12 20:54:26 +0000792bool
793X86InstrInfo::isReallyTriviallyReMaterializable(const MachineInstr *MI) const {
Dan Gohmanc101e952007-06-14 20:50:44 +0000794 switch (MI->getOpcode()) {
795 default: break;
Evan Chenge771ebd2008-03-27 01:41:09 +0000796 case X86::MOV8rm:
797 case X86::MOV16rm:
798 case X86::MOV16_rm:
799 case X86::MOV32rm:
800 case X86::MOV32_rm:
801 case X86::MOV64rm:
802 case X86::LD_Fp64m:
803 case X86::MOVSSrm:
804 case X86::MOVSDrm:
805 case X86::MOVAPSrm:
806 case X86::MOVAPDrm:
Dan Gohman54462742009-01-09 02:40:34 +0000807 case X86::MOVDQArm:
Evan Chenge771ebd2008-03-27 01:41:09 +0000808 case X86::MMX_MOVD64rm:
809 case X86::MMX_MOVQ64rm: {
810 // Loads from constant pools are trivially rematerializable.
Dan Gohmand735b802008-10-03 15:45:36 +0000811 if (MI->getOperand(1).isReg() &&
812 MI->getOperand(2).isImm() &&
813 MI->getOperand(3).isReg() && MI->getOperand(3).getReg() == 0 &&
814 (MI->getOperand(4).isCPI() ||
815 (MI->getOperand(4).isGlobal() &&
Evan Cheng9d15abe2008-03-31 07:54:19 +0000816 isGVStub(MI->getOperand(4).getGlobal(), TM)))) {
Evan Chenge771ebd2008-03-27 01:41:09 +0000817 unsigned BaseReg = MI->getOperand(1).getReg();
818 if (BaseReg == 0)
819 return true;
820 // Allow re-materialization of PIC load.
Dan Gohmand735b802008-10-03 15:45:36 +0000821 if (!ReMatPICStubLoad && MI->getOperand(4).isGlobal())
Evan Chengffe2eb02008-04-01 23:26:12 +0000822 return false;
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000823 const MachineFunction &MF = *MI->getParent()->getParent();
824 const MachineRegisterInfo &MRI = MF.getRegInfo();
Evan Chenge771ebd2008-03-27 01:41:09 +0000825 bool isPICBase = false;
826 for (MachineRegisterInfo::def_iterator I = MRI.def_begin(BaseReg),
827 E = MRI.def_end(); I != E; ++I) {
828 MachineInstr *DefMI = I.getOperand().getParent();
829 if (DefMI->getOpcode() != X86::MOVPC32r)
830 return false;
831 assert(!isPICBase && "More than one PIC base?");
832 isPICBase = true;
833 }
834 return isPICBase;
835 }
836 return false;
Evan Chengd8850a52008-02-22 09:25:47 +0000837 }
Evan Chenge771ebd2008-03-27 01:41:09 +0000838
839 case X86::LEA32r:
840 case X86::LEA64r: {
Dan Gohmand735b802008-10-03 15:45:36 +0000841 if (MI->getOperand(2).isImm() &&
842 MI->getOperand(3).isReg() && MI->getOperand(3).getReg() == 0 &&
843 !MI->getOperand(4).isReg()) {
Evan Chenge771ebd2008-03-27 01:41:09 +0000844 // lea fi#, lea GV, etc. are all rematerializable.
Dan Gohmand735b802008-10-03 15:45:36 +0000845 if (!MI->getOperand(1).isReg())
Dan Gohman83ccd142008-09-26 21:30:20 +0000846 return true;
Evan Chenge771ebd2008-03-27 01:41:09 +0000847 unsigned BaseReg = MI->getOperand(1).getReg();
848 if (BaseReg == 0)
849 return true;
850 // Allow re-materialization of lea PICBase + x.
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000851 const MachineFunction &MF = *MI->getParent()->getParent();
852 const MachineRegisterInfo &MRI = MF.getRegInfo();
Evan Chenge3d8dbf2008-03-27 01:45:11 +0000853 return regIsPICBase(BaseReg, MRI);
Evan Chenge771ebd2008-03-27 01:41:09 +0000854 }
855 return false;
856 }
Dan Gohmanc101e952007-06-14 20:50:44 +0000857 }
Evan Chenge771ebd2008-03-27 01:41:09 +0000858
Dan Gohmand45eddd2007-06-26 00:48:07 +0000859 // All other instructions marked M_REMATERIALIZABLE are always trivially
860 // rematerializable.
861 return true;
Dan Gohmanc101e952007-06-14 20:50:44 +0000862}
863
Evan Cheng9ef4ca22008-06-24 07:10:51 +0000864/// isSafeToClobberEFLAGS - Return true if it's safe insert an instruction that
865/// would clobber the EFLAGS condition register. Note the result may be
866/// conservative. If it cannot definitely determine the safety after visiting
867/// two instructions it assumes it's not safe.
868static bool isSafeToClobberEFLAGS(MachineBasicBlock &MBB,
869 MachineBasicBlock::iterator I) {
Dan Gohman3afda6e2008-10-21 03:24:31 +0000870 // It's always safe to clobber EFLAGS at the end of a block.
871 if (I == MBB.end())
872 return true;
873
Evan Cheng9ef4ca22008-06-24 07:10:51 +0000874 // For compile time consideration, if we are not able to determine the
875 // safety after visiting 2 instructions, we will assume it's not safe.
876 for (unsigned i = 0; i < 2; ++i) {
Evan Cheng9ef4ca22008-06-24 07:10:51 +0000877 bool SeenDef = false;
878 for (unsigned j = 0, e = I->getNumOperands(); j != e; ++j) {
879 MachineOperand &MO = I->getOperand(j);
Dan Gohmand735b802008-10-03 15:45:36 +0000880 if (!MO.isReg())
Evan Cheng9ef4ca22008-06-24 07:10:51 +0000881 continue;
882 if (MO.getReg() == X86::EFLAGS) {
883 if (MO.isUse())
884 return false;
885 SeenDef = true;
886 }
887 }
888
889 if (SeenDef)
890 // This instruction defines EFLAGS, no need to look any further.
891 return true;
892 ++I;
Dan Gohman3afda6e2008-10-21 03:24:31 +0000893
894 // If we make it to the end of the block, it's safe to clobber EFLAGS.
895 if (I == MBB.end())
896 return true;
Evan Cheng9ef4ca22008-06-24 07:10:51 +0000897 }
898
899 // Conservative answer.
900 return false;
901}
902
Evan Chengca1267c2008-03-31 20:40:39 +0000903void X86InstrInfo::reMaterialize(MachineBasicBlock &MBB,
904 MachineBasicBlock::iterator I,
905 unsigned DestReg,
906 const MachineInstr *Orig) const {
Bill Wendlingfbef3102009-02-11 21:51:19 +0000907 DebugLoc DL = DebugLoc::getUnknownLoc();
908 if (I != MBB.end()) DL = I->getDebugLoc();
909
Dan Gohmand735b802008-10-03 15:45:36 +0000910 unsigned SubIdx = Orig->getOperand(0).isReg()
Evan Cheng03eb3882008-04-16 23:44:44 +0000911 ? Orig->getOperand(0).getSubReg() : 0;
912 bool ChangeSubIdx = SubIdx != 0;
913 if (SubIdx && TargetRegisterInfo::isPhysicalRegister(DestReg)) {
914 DestReg = RI.getSubReg(DestReg, SubIdx);
915 SubIdx = 0;
916 }
917
Evan Chengca1267c2008-03-31 20:40:39 +0000918 // MOV32r0 etc. are implemented with xor which clobbers condition code.
919 // Re-materialize them as movri instructions to avoid side effects.
Evan Cheng9ef4ca22008-06-24 07:10:51 +0000920 bool Emitted = false;
Evan Chengca1267c2008-03-31 20:40:39 +0000921 switch (Orig->getOpcode()) {
Evan Cheng9ef4ca22008-06-24 07:10:51 +0000922 default: break;
Evan Chengca1267c2008-03-31 20:40:39 +0000923 case X86::MOV8r0:
Evan Chengca1267c2008-03-31 20:40:39 +0000924 case X86::MOV16r0:
Evan Chengca1267c2008-03-31 20:40:39 +0000925 case X86::MOV32r0:
Evan Cheng9ef4ca22008-06-24 07:10:51 +0000926 case X86::MOV64r0: {
927 if (!isSafeToClobberEFLAGS(MBB, I)) {
928 unsigned Opc = 0;
929 switch (Orig->getOpcode()) {
930 default: break;
931 case X86::MOV8r0: Opc = X86::MOV8ri; break;
932 case X86::MOV16r0: Opc = X86::MOV16ri; break;
933 case X86::MOV32r0: Opc = X86::MOV32ri; break;
934 case X86::MOV64r0: Opc = X86::MOV64ri32; break;
935 }
Bill Wendlingfbef3102009-02-11 21:51:19 +0000936 BuildMI(MBB, I, DL, get(Opc), DestReg).addImm(0);
Evan Cheng9ef4ca22008-06-24 07:10:51 +0000937 Emitted = true;
938 }
Evan Chengca1267c2008-03-31 20:40:39 +0000939 break;
Evan Cheng9ef4ca22008-06-24 07:10:51 +0000940 }
941 }
942
943 if (!Emitted) {
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000944 MachineInstr *MI = MBB.getParent()->CloneMachineInstr(Orig);
Evan Chengca1267c2008-03-31 20:40:39 +0000945 MI->getOperand(0).setReg(DestReg);
946 MBB.insert(I, MI);
Evan Chengca1267c2008-03-31 20:40:39 +0000947 }
Evan Cheng03eb3882008-04-16 23:44:44 +0000948
949 if (ChangeSubIdx) {
950 MachineInstr *NewMI = prior(I);
951 NewMI->getOperand(0).setSubReg(SubIdx);
952 }
Evan Chengca1267c2008-03-31 20:40:39 +0000953}
954
Chris Lattnera22edc82008-01-10 23:08:24 +0000955/// isInvariantLoad - Return true if the specified instruction (which is marked
956/// mayLoad) is loading from a location whose value is invariant across the
957/// function. For example, loading a value from the constant pool or from
958/// from the argument area of a function if it does not change. This should
959/// only return true of *all* loads the instruction does are invariant (if it
960/// does multiple loads).
Dan Gohmancbad42c2008-11-18 19:49:32 +0000961bool X86InstrInfo::isInvariantLoad(const MachineInstr *MI) const {
Chris Lattner828bb6c2008-01-12 00:35:08 +0000962 // This code cares about loads from three cases: constant pool entries,
963 // invariant argument slots, and global stubs. In order to handle these cases
964 // for all of the myriad of X86 instructions, we just scan for a CP/FI/GV
Chris Lattner144ad582008-01-12 00:53:16 +0000965 // operand and base our analysis on it. This is safe because the address of
Chris Lattner828bb6c2008-01-12 00:35:08 +0000966 // none of these three cases is ever used as anything other than a load base
967 // and X86 doesn't have any instructions that load from multiple places.
968
969 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
970 const MachineOperand &MO = MI->getOperand(i);
Chris Lattnera22edc82008-01-10 23:08:24 +0000971 // Loads from constant pools are trivially invariant.
Dan Gohmand735b802008-10-03 15:45:36 +0000972 if (MO.isCPI())
Chris Lattner3b5a2212008-01-05 05:28:30 +0000973 return true;
Evan Cheng9d15abe2008-03-31 07:54:19 +0000974
Dan Gohmand735b802008-10-03 15:45:36 +0000975 if (MO.isGlobal())
Evan Cheng9d15abe2008-03-31 07:54:19 +0000976 return isGVStub(MO.getGlobal(), TM);
Chris Lattner828bb6c2008-01-12 00:35:08 +0000977
978 // If this is a load from an invariant stack slot, the load is a constant.
Dan Gohmand735b802008-10-03 15:45:36 +0000979 if (MO.isFI()) {
Chris Lattner828bb6c2008-01-12 00:35:08 +0000980 const MachineFrameInfo &MFI =
981 *MI->getParent()->getParent()->getFrameInfo();
982 int Idx = MO.getIndex();
Chris Lattner87943902008-01-10 04:16:31 +0000983 return MFI.isFixedObjectIndex(Idx) && MFI.isImmutableObjectIndex(Idx);
984 }
Bill Wendling627c00b2007-12-17 23:07:56 +0000985 }
Chris Lattner828bb6c2008-01-12 00:35:08 +0000986
Chris Lattnera22edc82008-01-10 23:08:24 +0000987 // All other instances of these instructions are presumed to have other
988 // issues.
Chris Lattnera83b34b2008-01-05 05:26:26 +0000989 return false;
Bill Wendling627c00b2007-12-17 23:07:56 +0000990}
991
Evan Cheng3f411c72007-10-05 08:04:01 +0000992/// hasLiveCondCodeDef - True if MI has a condition code def, e.g. EFLAGS, that
993/// is not marked dead.
994static bool hasLiveCondCodeDef(MachineInstr *MI) {
Evan Cheng3f411c72007-10-05 08:04:01 +0000995 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
996 MachineOperand &MO = MI->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +0000997 if (MO.isReg() && MO.isDef() &&
Evan Cheng3f411c72007-10-05 08:04:01 +0000998 MO.getReg() == X86::EFLAGS && !MO.isDead()) {
999 return true;
1000 }
1001 }
1002 return false;
1003}
1004
Chris Lattnerbcea4d62005-01-02 02:37:07 +00001005/// convertToThreeAddress - This method must be implemented by targets that
1006/// set the M_CONVERTIBLE_TO_3_ADDR flag. When this flag is set, the target
1007/// may be able to convert a two-address instruction into a true
1008/// three-address instruction on demand. This allows the X86 target (for
1009/// example) to convert ADD and SHL instructions into LEA instructions if they
1010/// would require register copies due to two-addressness.
1011///
1012/// This method returns a null pointer if the transformation cannot be
1013/// performed, otherwise it returns the new instruction.
1014///
Evan Cheng258ff672006-12-01 21:52:41 +00001015MachineInstr *
1016X86InstrInfo::convertToThreeAddress(MachineFunction::iterator &MFI,
1017 MachineBasicBlock::iterator &MBBI,
Owen Andersonf660c172008-07-02 23:41:07 +00001018 LiveVariables *LV) const {
Evan Cheng258ff672006-12-01 21:52:41 +00001019 MachineInstr *MI = MBBI;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00001020 MachineFunction &MF = *MI->getParent()->getParent();
Chris Lattnerbcea4d62005-01-02 02:37:07 +00001021 // All instructions input are two-addr instructions. Get the known operands.
1022 unsigned Dest = MI->getOperand(0).getReg();
1023 unsigned Src = MI->getOperand(1).getReg();
Evan Cheng9f1c8312008-07-03 09:09:37 +00001024 bool isDead = MI->getOperand(0).isDead();
1025 bool isKill = MI->getOperand(1).isKill();
Chris Lattnerbcea4d62005-01-02 02:37:07 +00001026
Evan Cheng6ce7dc22006-11-15 20:58:11 +00001027 MachineInstr *NewMI = NULL;
Evan Cheng258ff672006-12-01 21:52:41 +00001028 // FIXME: 16-bit LEA's are really slow on Athlons, but not bad on P4's. When
Chris Lattnera16b7cb2007-03-20 06:08:29 +00001029 // we have better subtarget support, enable the 16-bit LEA generation here.
Evan Cheng258ff672006-12-01 21:52:41 +00001030 bool DisableLEA16 = true;
1031
Evan Cheng559dc462007-10-05 20:34:26 +00001032 unsigned MIOpc = MI->getOpcode();
1033 switch (MIOpc) {
Evan Chengccba76b2006-05-30 20:26:50 +00001034 case X86::SHUFPSrri: {
1035 assert(MI->getNumOperands() == 4 && "Unknown shufps instruction!");
Chris Lattnera16b7cb2007-03-20 06:08:29 +00001036 if (!TM.getSubtarget<X86Subtarget>().hasSSE2()) return 0;
1037
Evan Chengaa3c1412006-05-30 21:45:53 +00001038 unsigned B = MI->getOperand(1).getReg();
1039 unsigned C = MI->getOperand(2).getReg();
Chris Lattnera16b7cb2007-03-20 06:08:29 +00001040 if (B != C) return 0;
Evan Cheng9f1c8312008-07-03 09:09:37 +00001041 unsigned A = MI->getOperand(0).getReg();
1042 unsigned M = MI->getOperand(3).getImm();
Bill Wendlingfbef3102009-02-11 21:51:19 +00001043 NewMI = BuildMI(MF, MI->getDebugLoc(), get(X86::PSHUFDri))
1044 .addReg(A, true, false, false, isDead)
Evan Cheng9f1c8312008-07-03 09:09:37 +00001045 .addReg(B, false, false, isKill).addImm(M);
Chris Lattnera16b7cb2007-03-20 06:08:29 +00001046 break;
1047 }
Chris Lattner995f5502007-03-28 18:12:31 +00001048 case X86::SHL64ri: {
Evan Cheng24f2ea32007-09-14 21:48:26 +00001049 assert(MI->getNumOperands() >= 3 && "Unknown shift instruction!");
Chris Lattner995f5502007-03-28 18:12:31 +00001050 // NOTE: LEA doesn't produce flags like shift does, but LLVM never uses
1051 // the flags produced by a shift yet, so this is safe.
Chris Lattner995f5502007-03-28 18:12:31 +00001052 unsigned ShAmt = MI->getOperand(2).getImm();
1053 if (ShAmt == 0 || ShAmt >= 4) return 0;
Evan Cheng9f1c8312008-07-03 09:09:37 +00001054
Bill Wendlingfbef3102009-02-11 21:51:19 +00001055 NewMI = BuildMI(MF, MI->getDebugLoc(), get(X86::LEA64r))
1056 .addReg(Dest, true, false, false, isDead)
Evan Cheng9f1c8312008-07-03 09:09:37 +00001057 .addReg(0).addImm(1 << ShAmt).addReg(Src, false, false, isKill).addImm(0);
Chris Lattner995f5502007-03-28 18:12:31 +00001058 break;
1059 }
Chris Lattnera16b7cb2007-03-20 06:08:29 +00001060 case X86::SHL32ri: {
Evan Cheng24f2ea32007-09-14 21:48:26 +00001061 assert(MI->getNumOperands() >= 3 && "Unknown shift instruction!");
Chris Lattnera16b7cb2007-03-20 06:08:29 +00001062 // NOTE: LEA doesn't produce flags like shift does, but LLVM never uses
1063 // the flags produced by a shift yet, so this is safe.
Chris Lattnera16b7cb2007-03-20 06:08:29 +00001064 unsigned ShAmt = MI->getOperand(2).getImm();
1065 if (ShAmt == 0 || ShAmt >= 4) return 0;
Evan Cheng9f1c8312008-07-03 09:09:37 +00001066
Chris Lattnerf2177b82007-03-28 00:58:40 +00001067 unsigned Opc = TM.getSubtarget<X86Subtarget>().is64Bit() ?
1068 X86::LEA64_32r : X86::LEA32r;
Bill Wendlingfbef3102009-02-11 21:51:19 +00001069 NewMI = BuildMI(MF, MI->getDebugLoc(), get(Opc))
1070 .addReg(Dest, true, false, false, isDead)
Evan Cheng9f1c8312008-07-03 09:09:37 +00001071 .addReg(0).addImm(1 << ShAmt)
1072 .addReg(Src, false, false, isKill).addImm(0);
Chris Lattnera16b7cb2007-03-20 06:08:29 +00001073 break;
1074 }
1075 case X86::SHL16ri: {
Evan Cheng24f2ea32007-09-14 21:48:26 +00001076 assert(MI->getNumOperands() >= 3 && "Unknown shift instruction!");
Evan Cheng61d9c862007-09-06 00:14:41 +00001077 // NOTE: LEA doesn't produce flags like shift does, but LLVM never uses
1078 // the flags produced by a shift yet, so this is safe.
Evan Cheng61d9c862007-09-06 00:14:41 +00001079 unsigned ShAmt = MI->getOperand(2).getImm();
1080 if (ShAmt == 0 || ShAmt >= 4) return 0;
Evan Cheng9f1c8312008-07-03 09:09:37 +00001081
Christopher Lambb8133712007-08-10 21:18:25 +00001082 if (DisableLEA16) {
1083 // If 16-bit LEA is disabled, use 32-bit LEA via subregisters.
Chris Lattner84bc5422007-12-31 04:13:23 +00001084 MachineRegisterInfo &RegInfo = MFI->getParent()->getRegInfo();
Evan Cheng61d9c862007-09-06 00:14:41 +00001085 unsigned Opc = TM.getSubtarget<X86Subtarget>().is64Bit()
1086 ? X86::LEA64_32r : X86::LEA32r;
Chris Lattner84bc5422007-12-31 04:13:23 +00001087 unsigned leaInReg = RegInfo.createVirtualRegister(&X86::GR32RegClass);
1088 unsigned leaOutReg = RegInfo.createVirtualRegister(&X86::GR32RegClass);
Evan Cheng4499e492008-03-10 19:31:26 +00001089
Christopher Lamb1bc10082008-03-11 10:27:36 +00001090 // Build and insert into an implicit UNDEF value. This is OK because
1091 // well be shifting and then extracting the lower 16-bits.
Bill Wendlingfbef3102009-02-11 21:51:19 +00001092 BuildMI(*MFI, MBBI, MI->getDebugLoc(), get(X86::IMPLICIT_DEF), leaInReg);
1093 MachineInstr *InsMI =
1094 BuildMI(*MFI, MBBI, MI->getDebugLoc(), get(X86::INSERT_SUBREG),leaInReg)
Evan Cheng9f1c8312008-07-03 09:09:37 +00001095 .addReg(leaInReg).addReg(Src, false, false, isKill)
1096 .addImm(X86::SUBREG_16BIT);
Christopher Lambc9298232008-03-16 03:12:01 +00001097
Bill Wendlingfbef3102009-02-11 21:51:19 +00001098 NewMI = BuildMI(*MFI, MBBI, MI->getDebugLoc(), get(Opc), leaOutReg)
1099 .addReg(0).addImm(1 << ShAmt)
Evan Cheng9f1c8312008-07-03 09:09:37 +00001100 .addReg(leaInReg, false, false, true).addImm(0);
Christopher Lambb8133712007-08-10 21:18:25 +00001101
Bill Wendlingfbef3102009-02-11 21:51:19 +00001102 MachineInstr *ExtMI =
1103 BuildMI(*MFI, MBBI, MI->getDebugLoc(), get(X86::EXTRACT_SUBREG))
Evan Cheng9f1c8312008-07-03 09:09:37 +00001104 .addReg(Dest, true, false, false, isDead)
1105 .addReg(leaOutReg, false, false, true).addImm(X86::SUBREG_16BIT);
Bill Wendlingfbef3102009-02-11 21:51:19 +00001106
Owen Andersonf660c172008-07-02 23:41:07 +00001107 if (LV) {
Evan Cheng9f1c8312008-07-03 09:09:37 +00001108 // Update live variables
1109 LV->getVarInfo(leaInReg).Kills.push_back(NewMI);
1110 LV->getVarInfo(leaOutReg).Kills.push_back(ExtMI);
1111 if (isKill)
1112 LV->replaceKillInstruction(Src, MI, InsMI);
1113 if (isDead)
1114 LV->replaceKillInstruction(Dest, MI, ExtMI);
Owen Andersonf660c172008-07-02 23:41:07 +00001115 }
Evan Cheng9f1c8312008-07-03 09:09:37 +00001116 return ExtMI;
Christopher Lambb8133712007-08-10 21:18:25 +00001117 } else {
Bill Wendlingfbef3102009-02-11 21:51:19 +00001118 NewMI = BuildMI(MF, MI->getDebugLoc(), get(X86::LEA16r))
1119 .addReg(Dest, true, false, false, isDead)
Evan Cheng9f1c8312008-07-03 09:09:37 +00001120 .addReg(0).addImm(1 << ShAmt)
1121 .addReg(Src, false, false, isKill).addImm(0);
Christopher Lambb8133712007-08-10 21:18:25 +00001122 }
Chris Lattnera16b7cb2007-03-20 06:08:29 +00001123 break;
Evan Chengccba76b2006-05-30 20:26:50 +00001124 }
Evan Cheng559dc462007-10-05 20:34:26 +00001125 default: {
1126 // The following opcodes also sets the condition code register(s). Only
1127 // convert them to equivalent lea if the condition code register def's
1128 // are dead!
1129 if (hasLiveCondCodeDef(MI))
1130 return 0;
Evan Chengccba76b2006-05-30 20:26:50 +00001131
Evan Chengb76143c2007-10-09 07:14:53 +00001132 bool is64Bit = TM.getSubtarget<X86Subtarget>().is64Bit();
Evan Cheng559dc462007-10-05 20:34:26 +00001133 switch (MIOpc) {
1134 default: return 0;
1135 case X86::INC64r:
Dan Gohmancca29832009-01-06 23:34:46 +00001136 case X86::INC32r:
1137 case X86::INC64_32r: {
Evan Cheng559dc462007-10-05 20:34:26 +00001138 assert(MI->getNumOperands() >= 2 && "Unknown inc instruction!");
Evan Chengb76143c2007-10-09 07:14:53 +00001139 unsigned Opc = MIOpc == X86::INC64r ? X86::LEA64r
1140 : (is64Bit ? X86::LEA64_32r : X86::LEA32r);
Rafael Espindola2a6411b2009-04-07 21:37:46 +00001141 NewMI = addLeaRegOffset(BuildMI(MF, MI->getDebugLoc(), get(Opc))
1142 .addReg(Dest, true, false, false, isDead),
1143 Src, isKill, 1);
Evan Cheng559dc462007-10-05 20:34:26 +00001144 break;
Chris Lattnerbcea4d62005-01-02 02:37:07 +00001145 }
Evan Cheng559dc462007-10-05 20:34:26 +00001146 case X86::INC16r:
1147 case X86::INC64_16r:
1148 if (DisableLEA16) return 0;
1149 assert(MI->getNumOperands() >= 2 && "Unknown inc instruction!");
Bill Wendlingfbef3102009-02-11 21:51:19 +00001150 NewMI = addRegOffset(BuildMI(MF, MI->getDebugLoc(), get(X86::LEA16r))
Evan Cheng9f1c8312008-07-03 09:09:37 +00001151 .addReg(Dest, true, false, false, isDead),
1152 Src, isKill, 1);
Evan Cheng559dc462007-10-05 20:34:26 +00001153 break;
1154 case X86::DEC64r:
Dan Gohmancca29832009-01-06 23:34:46 +00001155 case X86::DEC32r:
1156 case X86::DEC64_32r: {
Evan Cheng559dc462007-10-05 20:34:26 +00001157 assert(MI->getNumOperands() >= 2 && "Unknown dec instruction!");
Evan Chengb76143c2007-10-09 07:14:53 +00001158 unsigned Opc = MIOpc == X86::DEC64r ? X86::LEA64r
1159 : (is64Bit ? X86::LEA64_32r : X86::LEA32r);
Rafael Espindola2a6411b2009-04-07 21:37:46 +00001160 NewMI = addLeaRegOffset(BuildMI(MF, MI->getDebugLoc(), get(Opc))
1161 .addReg(Dest, true, false, false, isDead),
1162 Src, isKill, -1);
Evan Cheng559dc462007-10-05 20:34:26 +00001163 break;
1164 }
1165 case X86::DEC16r:
1166 case X86::DEC64_16r:
1167 if (DisableLEA16) return 0;
1168 assert(MI->getNumOperands() >= 2 && "Unknown dec instruction!");
Bill Wendlingfbef3102009-02-11 21:51:19 +00001169 NewMI = addRegOffset(BuildMI(MF, MI->getDebugLoc(), get(X86::LEA16r))
Evan Cheng9f1c8312008-07-03 09:09:37 +00001170 .addReg(Dest, true, false, false, isDead),
1171 Src, isKill, -1);
Evan Cheng559dc462007-10-05 20:34:26 +00001172 break;
1173 case X86::ADD64rr:
1174 case X86::ADD32rr: {
1175 assert(MI->getNumOperands() >= 3 && "Unknown add instruction!");
Evan Chengb76143c2007-10-09 07:14:53 +00001176 unsigned Opc = MIOpc == X86::ADD64rr ? X86::LEA64r
1177 : (is64Bit ? X86::LEA64_32r : X86::LEA32r);
Evan Cheng9f1c8312008-07-03 09:09:37 +00001178 unsigned Src2 = MI->getOperand(2).getReg();
1179 bool isKill2 = MI->getOperand(2).isKill();
Bill Wendlingfbef3102009-02-11 21:51:19 +00001180 NewMI = addRegReg(BuildMI(MF, MI->getDebugLoc(), get(Opc))
Evan Cheng9f1c8312008-07-03 09:09:37 +00001181 .addReg(Dest, true, false, false, isDead),
1182 Src, isKill, Src2, isKill2);
1183 if (LV && isKill2)
1184 LV->replaceKillInstruction(Src2, MI, NewMI);
Evan Cheng559dc462007-10-05 20:34:26 +00001185 break;
1186 }
Evan Cheng9f1c8312008-07-03 09:09:37 +00001187 case X86::ADD16rr: {
Evan Cheng559dc462007-10-05 20:34:26 +00001188 if (DisableLEA16) return 0;
1189 assert(MI->getNumOperands() >= 3 && "Unknown add instruction!");
Evan Cheng9f1c8312008-07-03 09:09:37 +00001190 unsigned Src2 = MI->getOperand(2).getReg();
1191 bool isKill2 = MI->getOperand(2).isKill();
Bill Wendlingfbef3102009-02-11 21:51:19 +00001192 NewMI = addRegReg(BuildMI(MF, MI->getDebugLoc(), get(X86::LEA16r))
Evan Cheng9f1c8312008-07-03 09:09:37 +00001193 .addReg(Dest, true, false, false, isDead),
1194 Src, isKill, Src2, isKill2);
1195 if (LV && isKill2)
1196 LV->replaceKillInstruction(Src2, MI, NewMI);
Evan Cheng559dc462007-10-05 20:34:26 +00001197 break;
Evan Cheng9f1c8312008-07-03 09:09:37 +00001198 }
Evan Cheng559dc462007-10-05 20:34:26 +00001199 case X86::ADD64ri32:
1200 case X86::ADD64ri8:
1201 assert(MI->getNumOperands() >= 3 && "Unknown add instruction!");
Dan Gohmand735b802008-10-03 15:45:36 +00001202 if (MI->getOperand(2).isImm())
Rafael Espindola2a6411b2009-04-07 21:37:46 +00001203 NewMI = addLeaRegOffset(BuildMI(MF, MI->getDebugLoc(), get(X86::LEA64r))
1204 .addReg(Dest, true, false, false, isDead),
1205 Src, isKill, MI->getOperand(2).getImm());
Evan Cheng559dc462007-10-05 20:34:26 +00001206 break;
1207 case X86::ADD32ri:
1208 case X86::ADD32ri8:
1209 assert(MI->getNumOperands() >= 3 && "Unknown add instruction!");
Dan Gohmand735b802008-10-03 15:45:36 +00001210 if (MI->getOperand(2).isImm()) {
Evan Chengb76143c2007-10-09 07:14:53 +00001211 unsigned Opc = is64Bit ? X86::LEA64_32r : X86::LEA32r;
Rafael Espindola2a6411b2009-04-07 21:37:46 +00001212 NewMI = addLeaRegOffset(BuildMI(MF, MI->getDebugLoc(), get(Opc))
1213 .addReg(Dest, true, false, false, isDead),
1214 Src, isKill, MI->getOperand(2).getImm());
Evan Chengb76143c2007-10-09 07:14:53 +00001215 }
Evan Cheng559dc462007-10-05 20:34:26 +00001216 break;
1217 case X86::ADD16ri:
1218 case X86::ADD16ri8:
1219 if (DisableLEA16) return 0;
1220 assert(MI->getNumOperands() >= 3 && "Unknown add instruction!");
Dan Gohmand735b802008-10-03 15:45:36 +00001221 if (MI->getOperand(2).isImm())
Bill Wendlingfbef3102009-02-11 21:51:19 +00001222 NewMI = addRegOffset(BuildMI(MF, MI->getDebugLoc(), get(X86::LEA16r))
Evan Cheng9f1c8312008-07-03 09:09:37 +00001223 .addReg(Dest, true, false, false, isDead),
1224 Src, isKill, MI->getOperand(2).getImm());
Evan Cheng559dc462007-10-05 20:34:26 +00001225 break;
1226 case X86::SHL16ri:
1227 if (DisableLEA16) return 0;
1228 case X86::SHL32ri:
1229 case X86::SHL64ri: {
Dan Gohmand735b802008-10-03 15:45:36 +00001230 assert(MI->getNumOperands() >= 3 && MI->getOperand(2).isImm() &&
Evan Cheng559dc462007-10-05 20:34:26 +00001231 "Unknown shl instruction!");
Chris Lattner9a1ceae2007-12-30 20:49:49 +00001232 unsigned ShAmt = MI->getOperand(2).getImm();
Evan Cheng559dc462007-10-05 20:34:26 +00001233 if (ShAmt == 1 || ShAmt == 2 || ShAmt == 3) {
1234 X86AddressMode AM;
1235 AM.Scale = 1 << ShAmt;
1236 AM.IndexReg = Src;
1237 unsigned Opc = MIOpc == X86::SHL64ri ? X86::LEA64r
Evan Chengb76143c2007-10-09 07:14:53 +00001238 : (MIOpc == X86::SHL32ri
1239 ? (is64Bit ? X86::LEA64_32r : X86::LEA32r) : X86::LEA16r);
Bill Wendlingfbef3102009-02-11 21:51:19 +00001240 NewMI = addFullAddress(BuildMI(MF, MI->getDebugLoc(), get(Opc))
Evan Cheng9f1c8312008-07-03 09:09:37 +00001241 .addReg(Dest, true, false, false, isDead), AM);
1242 if (isKill)
1243 NewMI->getOperand(3).setIsKill(true);
Evan Cheng559dc462007-10-05 20:34:26 +00001244 }
1245 break;
1246 }
1247 }
1248 }
Chris Lattnerbcea4d62005-01-02 02:37:07 +00001249 }
1250
Evan Cheng15246732008-02-07 08:29:53 +00001251 if (!NewMI) return 0;
1252
Evan Cheng9f1c8312008-07-03 09:09:37 +00001253 if (LV) { // Update live variables
1254 if (isKill)
1255 LV->replaceKillInstruction(Src, MI, NewMI);
1256 if (isDead)
1257 LV->replaceKillInstruction(Dest, MI, NewMI);
1258 }
1259
Evan Cheng559dc462007-10-05 20:34:26 +00001260 MFI->insert(MBBI, NewMI); // Insert the new inst
Evan Cheng6ce7dc22006-11-15 20:58:11 +00001261 return NewMI;
Chris Lattnerbcea4d62005-01-02 02:37:07 +00001262}
1263
Chris Lattner41e431b2005-01-19 07:11:01 +00001264/// commuteInstruction - We have a few instructions that must be hacked on to
1265/// commute them.
1266///
Evan Cheng58dcb0e2008-06-16 07:33:11 +00001267MachineInstr *
1268X86InstrInfo::commuteInstruction(MachineInstr *MI, bool NewMI) const {
Chris Lattner41e431b2005-01-19 07:11:01 +00001269 switch (MI->getOpcode()) {
Chris Lattner0df53d22005-01-19 07:31:24 +00001270 case X86::SHRD16rri8: // A = SHRD16rri8 B, C, I -> A = SHLD16rri8 C, B, (16-I)
1271 case X86::SHLD16rri8: // A = SHLD16rri8 B, C, I -> A = SHRD16rri8 C, B, (16-I)
Chris Lattner41e431b2005-01-19 07:11:01 +00001272 case X86::SHRD32rri8: // A = SHRD32rri8 B, C, I -> A = SHLD32rri8 C, B, (32-I)
Dan Gohmane47f1f92007-09-14 23:17:45 +00001273 case X86::SHLD32rri8: // A = SHLD32rri8 B, C, I -> A = SHRD32rri8 C, B, (32-I)
1274 case X86::SHRD64rri8: // A = SHRD64rri8 B, C, I -> A = SHLD64rri8 C, B, (64-I)
1275 case X86::SHLD64rri8:{// A = SHLD64rri8 B, C, I -> A = SHRD64rri8 C, B, (64-I)
Chris Lattner0df53d22005-01-19 07:31:24 +00001276 unsigned Opc;
1277 unsigned Size;
1278 switch (MI->getOpcode()) {
1279 default: assert(0 && "Unreachable!");
1280 case X86::SHRD16rri8: Size = 16; Opc = X86::SHLD16rri8; break;
1281 case X86::SHLD16rri8: Size = 16; Opc = X86::SHRD16rri8; break;
1282 case X86::SHRD32rri8: Size = 32; Opc = X86::SHLD32rri8; break;
1283 case X86::SHLD32rri8: Size = 32; Opc = X86::SHRD32rri8; break;
Dan Gohmane47f1f92007-09-14 23:17:45 +00001284 case X86::SHRD64rri8: Size = 64; Opc = X86::SHLD64rri8; break;
1285 case X86::SHLD64rri8: Size = 64; Opc = X86::SHRD64rri8; break;
Chris Lattner0df53d22005-01-19 07:31:24 +00001286 }
Chris Lattner9a1ceae2007-12-30 20:49:49 +00001287 unsigned Amt = MI->getOperand(3).getImm();
Dan Gohman74feef22008-10-17 01:23:35 +00001288 if (NewMI) {
1289 MachineFunction &MF = *MI->getParent()->getParent();
1290 MI = MF.CloneMachineInstr(MI);
1291 NewMI = false;
Evan Chenga4d16a12008-02-13 02:46:49 +00001292 }
Dan Gohman74feef22008-10-17 01:23:35 +00001293 MI->setDesc(get(Opc));
1294 MI->getOperand(3).setImm(Size-Amt);
1295 return TargetInstrInfoImpl::commuteInstruction(MI, NewMI);
Chris Lattner41e431b2005-01-19 07:11:01 +00001296 }
Evan Cheng7ad42d92007-10-05 23:13:21 +00001297 case X86::CMOVB16rr:
1298 case X86::CMOVB32rr:
1299 case X86::CMOVB64rr:
1300 case X86::CMOVAE16rr:
1301 case X86::CMOVAE32rr:
1302 case X86::CMOVAE64rr:
1303 case X86::CMOVE16rr:
1304 case X86::CMOVE32rr:
1305 case X86::CMOVE64rr:
1306 case X86::CMOVNE16rr:
1307 case X86::CMOVNE32rr:
1308 case X86::CMOVNE64rr:
1309 case X86::CMOVBE16rr:
1310 case X86::CMOVBE32rr:
1311 case X86::CMOVBE64rr:
1312 case X86::CMOVA16rr:
1313 case X86::CMOVA32rr:
1314 case X86::CMOVA64rr:
1315 case X86::CMOVL16rr:
1316 case X86::CMOVL32rr:
1317 case X86::CMOVL64rr:
1318 case X86::CMOVGE16rr:
1319 case X86::CMOVGE32rr:
1320 case X86::CMOVGE64rr:
1321 case X86::CMOVLE16rr:
1322 case X86::CMOVLE32rr:
1323 case X86::CMOVLE64rr:
1324 case X86::CMOVG16rr:
1325 case X86::CMOVG32rr:
1326 case X86::CMOVG64rr:
1327 case X86::CMOVS16rr:
1328 case X86::CMOVS32rr:
1329 case X86::CMOVS64rr:
1330 case X86::CMOVNS16rr:
1331 case X86::CMOVNS32rr:
1332 case X86::CMOVNS64rr:
1333 case X86::CMOVP16rr:
1334 case X86::CMOVP32rr:
1335 case X86::CMOVP64rr:
1336 case X86::CMOVNP16rr:
1337 case X86::CMOVNP32rr:
Dan Gohman305fceb2009-01-07 00:35:10 +00001338 case X86::CMOVNP64rr:
1339 case X86::CMOVO16rr:
1340 case X86::CMOVO32rr:
1341 case X86::CMOVO64rr:
1342 case X86::CMOVNO16rr:
1343 case X86::CMOVNO32rr:
1344 case X86::CMOVNO64rr: {
Evan Cheng7ad42d92007-10-05 23:13:21 +00001345 unsigned Opc = 0;
1346 switch (MI->getOpcode()) {
1347 default: break;
1348 case X86::CMOVB16rr: Opc = X86::CMOVAE16rr; break;
1349 case X86::CMOVB32rr: Opc = X86::CMOVAE32rr; break;
1350 case X86::CMOVB64rr: Opc = X86::CMOVAE64rr; break;
1351 case X86::CMOVAE16rr: Opc = X86::CMOVB16rr; break;
1352 case X86::CMOVAE32rr: Opc = X86::CMOVB32rr; break;
1353 case X86::CMOVAE64rr: Opc = X86::CMOVB64rr; break;
1354 case X86::CMOVE16rr: Opc = X86::CMOVNE16rr; break;
1355 case X86::CMOVE32rr: Opc = X86::CMOVNE32rr; break;
1356 case X86::CMOVE64rr: Opc = X86::CMOVNE64rr; break;
1357 case X86::CMOVNE16rr: Opc = X86::CMOVE16rr; break;
1358 case X86::CMOVNE32rr: Opc = X86::CMOVE32rr; break;
1359 case X86::CMOVNE64rr: Opc = X86::CMOVE64rr; break;
1360 case X86::CMOVBE16rr: Opc = X86::CMOVA16rr; break;
1361 case X86::CMOVBE32rr: Opc = X86::CMOVA32rr; break;
1362 case X86::CMOVBE64rr: Opc = X86::CMOVA64rr; break;
1363 case X86::CMOVA16rr: Opc = X86::CMOVBE16rr; break;
1364 case X86::CMOVA32rr: Opc = X86::CMOVBE32rr; break;
1365 case X86::CMOVA64rr: Opc = X86::CMOVBE64rr; break;
1366 case X86::CMOVL16rr: Opc = X86::CMOVGE16rr; break;
1367 case X86::CMOVL32rr: Opc = X86::CMOVGE32rr; break;
1368 case X86::CMOVL64rr: Opc = X86::CMOVGE64rr; break;
1369 case X86::CMOVGE16rr: Opc = X86::CMOVL16rr; break;
1370 case X86::CMOVGE32rr: Opc = X86::CMOVL32rr; break;
1371 case X86::CMOVGE64rr: Opc = X86::CMOVL64rr; break;
1372 case X86::CMOVLE16rr: Opc = X86::CMOVG16rr; break;
1373 case X86::CMOVLE32rr: Opc = X86::CMOVG32rr; break;
1374 case X86::CMOVLE64rr: Opc = X86::CMOVG64rr; break;
1375 case X86::CMOVG16rr: Opc = X86::CMOVLE16rr; break;
1376 case X86::CMOVG32rr: Opc = X86::CMOVLE32rr; break;
1377 case X86::CMOVG64rr: Opc = X86::CMOVLE64rr; break;
1378 case X86::CMOVS16rr: Opc = X86::CMOVNS16rr; break;
1379 case X86::CMOVS32rr: Opc = X86::CMOVNS32rr; break;
1380 case X86::CMOVS64rr: Opc = X86::CMOVNS32rr; break;
1381 case X86::CMOVNS16rr: Opc = X86::CMOVS16rr; break;
1382 case X86::CMOVNS32rr: Opc = X86::CMOVS32rr; break;
1383 case X86::CMOVNS64rr: Opc = X86::CMOVS64rr; break;
1384 case X86::CMOVP16rr: Opc = X86::CMOVNP16rr; break;
1385 case X86::CMOVP32rr: Opc = X86::CMOVNP32rr; break;
1386 case X86::CMOVP64rr: Opc = X86::CMOVNP32rr; break;
1387 case X86::CMOVNP16rr: Opc = X86::CMOVP16rr; break;
1388 case X86::CMOVNP32rr: Opc = X86::CMOVP32rr; break;
1389 case X86::CMOVNP64rr: Opc = X86::CMOVP64rr; break;
Dan Gohman305fceb2009-01-07 00:35:10 +00001390 case X86::CMOVO16rr: Opc = X86::CMOVNO16rr; break;
1391 case X86::CMOVO32rr: Opc = X86::CMOVNO32rr; break;
1392 case X86::CMOVO64rr: Opc = X86::CMOVNO32rr; break;
1393 case X86::CMOVNO16rr: Opc = X86::CMOVO16rr; break;
1394 case X86::CMOVNO32rr: Opc = X86::CMOVO32rr; break;
1395 case X86::CMOVNO64rr: Opc = X86::CMOVO64rr; break;
Evan Cheng7ad42d92007-10-05 23:13:21 +00001396 }
Dan Gohman74feef22008-10-17 01:23:35 +00001397 if (NewMI) {
1398 MachineFunction &MF = *MI->getParent()->getParent();
1399 MI = MF.CloneMachineInstr(MI);
1400 NewMI = false;
1401 }
Chris Lattner5080f4d2008-01-11 18:10:50 +00001402 MI->setDesc(get(Opc));
Evan Cheng7ad42d92007-10-05 23:13:21 +00001403 // Fallthrough intended.
1404 }
Chris Lattner41e431b2005-01-19 07:11:01 +00001405 default:
Evan Cheng58dcb0e2008-06-16 07:33:11 +00001406 return TargetInstrInfoImpl::commuteInstruction(MI, NewMI);
Chris Lattner41e431b2005-01-19 07:11:01 +00001407 }
1408}
1409
Chris Lattner7fbe9722006-10-20 17:42:20 +00001410static X86::CondCode GetCondFromBranchOpc(unsigned BrOpc) {
1411 switch (BrOpc) {
1412 default: return X86::COND_INVALID;
1413 case X86::JE: return X86::COND_E;
1414 case X86::JNE: return X86::COND_NE;
1415 case X86::JL: return X86::COND_L;
1416 case X86::JLE: return X86::COND_LE;
1417 case X86::JG: return X86::COND_G;
1418 case X86::JGE: return X86::COND_GE;
1419 case X86::JB: return X86::COND_B;
1420 case X86::JBE: return X86::COND_BE;
1421 case X86::JA: return X86::COND_A;
1422 case X86::JAE: return X86::COND_AE;
1423 case X86::JS: return X86::COND_S;
1424 case X86::JNS: return X86::COND_NS;
1425 case X86::JP: return X86::COND_P;
1426 case X86::JNP: return X86::COND_NP;
1427 case X86::JO: return X86::COND_O;
1428 case X86::JNO: return X86::COND_NO;
1429 }
1430}
1431
1432unsigned X86::GetCondBranchFromCond(X86::CondCode CC) {
1433 switch (CC) {
1434 default: assert(0 && "Illegal condition code!");
Evan Chenge5f62042007-09-29 00:00:36 +00001435 case X86::COND_E: return X86::JE;
1436 case X86::COND_NE: return X86::JNE;
1437 case X86::COND_L: return X86::JL;
1438 case X86::COND_LE: return X86::JLE;
1439 case X86::COND_G: return X86::JG;
1440 case X86::COND_GE: return X86::JGE;
1441 case X86::COND_B: return X86::JB;
1442 case X86::COND_BE: return X86::JBE;
1443 case X86::COND_A: return X86::JA;
1444 case X86::COND_AE: return X86::JAE;
1445 case X86::COND_S: return X86::JS;
1446 case X86::COND_NS: return X86::JNS;
1447 case X86::COND_P: return X86::JP;
1448 case X86::COND_NP: return X86::JNP;
1449 case X86::COND_O: return X86::JO;
1450 case X86::COND_NO: return X86::JNO;
Chris Lattner7fbe9722006-10-20 17:42:20 +00001451 }
1452}
1453
Chris Lattner9cd68752006-10-21 05:52:40 +00001454/// GetOppositeBranchCondition - Return the inverse of the specified condition,
1455/// e.g. turning COND_E to COND_NE.
1456X86::CondCode X86::GetOppositeBranchCondition(X86::CondCode CC) {
1457 switch (CC) {
1458 default: assert(0 && "Illegal condition code!");
1459 case X86::COND_E: return X86::COND_NE;
1460 case X86::COND_NE: return X86::COND_E;
1461 case X86::COND_L: return X86::COND_GE;
1462 case X86::COND_LE: return X86::COND_G;
1463 case X86::COND_G: return X86::COND_LE;
1464 case X86::COND_GE: return X86::COND_L;
1465 case X86::COND_B: return X86::COND_AE;
1466 case X86::COND_BE: return X86::COND_A;
1467 case X86::COND_A: return X86::COND_BE;
1468 case X86::COND_AE: return X86::COND_B;
1469 case X86::COND_S: return X86::COND_NS;
1470 case X86::COND_NS: return X86::COND_S;
1471 case X86::COND_P: return X86::COND_NP;
1472 case X86::COND_NP: return X86::COND_P;
1473 case X86::COND_O: return X86::COND_NO;
1474 case X86::COND_NO: return X86::COND_O;
1475 }
1476}
1477
Dale Johannesen318093b2007-06-14 22:03:45 +00001478bool X86InstrInfo::isUnpredicatedTerminator(const MachineInstr *MI) const {
Chris Lattner749c6f62008-01-07 07:27:27 +00001479 const TargetInstrDesc &TID = MI->getDesc();
1480 if (!TID.isTerminator()) return false;
Chris Lattner69244302008-01-07 01:56:04 +00001481
1482 // Conditional branch is a special case.
Chris Lattner749c6f62008-01-07 07:27:27 +00001483 if (TID.isBranch() && !TID.isBarrier())
Chris Lattner69244302008-01-07 01:56:04 +00001484 return true;
Chris Lattner749c6f62008-01-07 07:27:27 +00001485 if (!TID.isPredicable())
Chris Lattner69244302008-01-07 01:56:04 +00001486 return true;
1487 return !isPredicated(MI);
Dale Johannesen318093b2007-06-14 22:03:45 +00001488}
Chris Lattner9cd68752006-10-21 05:52:40 +00001489
Evan Cheng85dce6c2007-07-26 17:32:14 +00001490// For purposes of branch analysis do not count FP_REG_KILL as a terminator.
1491static bool isBrAnalysisUnpredicatedTerminator(const MachineInstr *MI,
1492 const X86InstrInfo &TII) {
1493 if (MI->getOpcode() == X86::FP_REG_KILL)
1494 return false;
1495 return TII.isUnpredicatedTerminator(MI);
1496}
1497
Chris Lattner7fbe9722006-10-20 17:42:20 +00001498bool X86InstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,
1499 MachineBasicBlock *&TBB,
1500 MachineBasicBlock *&FBB,
Evan Chengdc54d312009-02-09 07:14:22 +00001501 SmallVectorImpl<MachineOperand> &Cond,
1502 bool AllowModify) const {
Dan Gohman279c22e2008-10-21 03:29:32 +00001503 // Start from the bottom of the block and work up, examining the
1504 // terminator instructions.
Chris Lattner7fbe9722006-10-20 17:42:20 +00001505 MachineBasicBlock::iterator I = MBB.end();
Dan Gohman279c22e2008-10-21 03:29:32 +00001506 while (I != MBB.begin()) {
1507 --I;
1508 // Working from the bottom, when we see a non-terminator
1509 // instruction, we're done.
1510 if (!isBrAnalysisUnpredicatedTerminator(I, *this))
1511 break;
1512 // A terminator that isn't a branch can't easily be handled
1513 // by this analysis.
1514 if (!I->getDesc().isBranch())
Chris Lattner7fbe9722006-10-20 17:42:20 +00001515 return true;
Dan Gohman279c22e2008-10-21 03:29:32 +00001516 // Handle unconditional branches.
1517 if (I->getOpcode() == X86::JMP) {
Evan Chengdc54d312009-02-09 07:14:22 +00001518 if (!AllowModify) {
1519 TBB = I->getOperand(0).getMBB();
1520 return false;
1521 }
1522
Dan Gohman279c22e2008-10-21 03:29:32 +00001523 // If the block has any instructions after a JMP, delete them.
1524 while (next(I) != MBB.end())
1525 next(I)->eraseFromParent();
1526 Cond.clear();
1527 FBB = 0;
1528 // Delete the JMP if it's equivalent to a fall-through.
1529 if (MBB.isLayoutSuccessor(I->getOperand(0).getMBB())) {
1530 TBB = 0;
1531 I->eraseFromParent();
1532 I = MBB.end();
1533 continue;
1534 }
1535 // TBB is used to indicate the unconditinal destination.
1536 TBB = I->getOperand(0).getMBB();
1537 continue;
Chris Lattner7fbe9722006-10-20 17:42:20 +00001538 }
Dan Gohman279c22e2008-10-21 03:29:32 +00001539 // Handle conditional branches.
1540 X86::CondCode BranchCode = GetCondFromBranchOpc(I->getOpcode());
Chris Lattner7fbe9722006-10-20 17:42:20 +00001541 if (BranchCode == X86::COND_INVALID)
1542 return true; // Can't handle indirect branch.
Dan Gohman279c22e2008-10-21 03:29:32 +00001543 // Working from the bottom, handle the first conditional branch.
1544 if (Cond.empty()) {
1545 FBB = TBB;
1546 TBB = I->getOperand(0).getMBB();
1547 Cond.push_back(MachineOperand::CreateImm(BranchCode));
1548 continue;
1549 }
1550 // Handle subsequent conditional branches. Only handle the case
1551 // where all conditional branches branch to the same destination
1552 // and their condition opcodes fit one of the special
1553 // multi-branch idioms.
1554 assert(Cond.size() == 1);
1555 assert(TBB);
1556 // Only handle the case where all conditional branches branch to
1557 // the same destination.
1558 if (TBB != I->getOperand(0).getMBB())
1559 return true;
1560 X86::CondCode OldBranchCode = (X86::CondCode)Cond[0].getImm();
1561 // If the conditions are the same, we can leave them alone.
1562 if (OldBranchCode == BranchCode)
1563 continue;
1564 // If they differ, see if they fit one of the known patterns.
1565 // Theoretically we could handle more patterns here, but
1566 // we shouldn't expect to see them if instruction selection
1567 // has done a reasonable job.
1568 if ((OldBranchCode == X86::COND_NP &&
1569 BranchCode == X86::COND_E) ||
1570 (OldBranchCode == X86::COND_E &&
1571 BranchCode == X86::COND_NP))
1572 BranchCode = X86::COND_NP_OR_E;
1573 else if ((OldBranchCode == X86::COND_P &&
1574 BranchCode == X86::COND_NE) ||
1575 (OldBranchCode == X86::COND_NE &&
1576 BranchCode == X86::COND_P))
1577 BranchCode = X86::COND_NE_OR_P;
1578 else
1579 return true;
1580 // Update the MachineOperand.
1581 Cond[0].setImm(BranchCode);
Chris Lattner6ce64432006-10-30 22:27:23 +00001582 }
Chris Lattner7fbe9722006-10-20 17:42:20 +00001583
Dan Gohman279c22e2008-10-21 03:29:32 +00001584 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00001585}
1586
Evan Cheng6ae36262007-05-18 00:18:17 +00001587unsigned X86InstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
Chris Lattner7fbe9722006-10-20 17:42:20 +00001588 MachineBasicBlock::iterator I = MBB.end();
Dan Gohman279c22e2008-10-21 03:29:32 +00001589 unsigned Count = 0;
1590
1591 while (I != MBB.begin()) {
1592 --I;
1593 if (I->getOpcode() != X86::JMP &&
1594 GetCondFromBranchOpc(I->getOpcode()) == X86::COND_INVALID)
1595 break;
1596 // Remove the branch.
1597 I->eraseFromParent();
1598 I = MBB.end();
1599 ++Count;
1600 }
Chris Lattner7fbe9722006-10-20 17:42:20 +00001601
Dan Gohman279c22e2008-10-21 03:29:32 +00001602 return Count;
Chris Lattner7fbe9722006-10-20 17:42:20 +00001603}
1604
Evan Cheng6ae36262007-05-18 00:18:17 +00001605unsigned
1606X86InstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
1607 MachineBasicBlock *FBB,
Owen Anderson44eb65c2008-08-14 22:49:33 +00001608 const SmallVectorImpl<MachineOperand> &Cond) const {
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001609 // FIXME this should probably have a DebugLoc operand
1610 DebugLoc dl = DebugLoc::getUnknownLoc();
Chris Lattner7fbe9722006-10-20 17:42:20 +00001611 // Shouldn't be a fall through.
1612 assert(TBB && "InsertBranch must not be told to insert a fallthrough");
Chris Lattner34a84ac2006-10-21 05:34:23 +00001613 assert((Cond.size() == 1 || Cond.size() == 0) &&
1614 "X86 branch conditions have one component!");
1615
Dan Gohman279c22e2008-10-21 03:29:32 +00001616 if (Cond.empty()) {
1617 // Unconditional branch?
1618 assert(!FBB && "Unconditional branch with multiple successors!");
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001619 BuildMI(&MBB, dl, get(X86::JMP)).addMBB(TBB);
Evan Cheng6ae36262007-05-18 00:18:17 +00001620 return 1;
Chris Lattner7fbe9722006-10-20 17:42:20 +00001621 }
Dan Gohman279c22e2008-10-21 03:29:32 +00001622
1623 // Conditional branch.
1624 unsigned Count = 0;
1625 X86::CondCode CC = (X86::CondCode)Cond[0].getImm();
1626 switch (CC) {
1627 case X86::COND_NP_OR_E:
1628 // Synthesize NP_OR_E with two branches.
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001629 BuildMI(&MBB, dl, get(X86::JNP)).addMBB(TBB);
Dan Gohman279c22e2008-10-21 03:29:32 +00001630 ++Count;
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001631 BuildMI(&MBB, dl, get(X86::JE)).addMBB(TBB);
Dan Gohman279c22e2008-10-21 03:29:32 +00001632 ++Count;
1633 break;
1634 case X86::COND_NE_OR_P:
1635 // Synthesize NE_OR_P with two branches.
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001636 BuildMI(&MBB, dl, get(X86::JNE)).addMBB(TBB);
Dan Gohman279c22e2008-10-21 03:29:32 +00001637 ++Count;
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001638 BuildMI(&MBB, dl, get(X86::JP)).addMBB(TBB);
Dan Gohman279c22e2008-10-21 03:29:32 +00001639 ++Count;
1640 break;
1641 default: {
1642 unsigned Opc = GetCondBranchFromCond(CC);
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001643 BuildMI(&MBB, dl, get(Opc)).addMBB(TBB);
Dan Gohman279c22e2008-10-21 03:29:32 +00001644 ++Count;
1645 }
1646 }
1647 if (FBB) {
1648 // Two-way Conditional branch. Insert the second branch.
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001649 BuildMI(&MBB, dl, get(X86::JMP)).addMBB(FBB);
Dan Gohman279c22e2008-10-21 03:29:32 +00001650 ++Count;
1651 }
1652 return Count;
Chris Lattner7fbe9722006-10-20 17:42:20 +00001653}
1654
Owen Anderson940f83e2008-08-26 18:03:31 +00001655bool X86InstrInfo::copyRegToReg(MachineBasicBlock &MBB,
Chris Lattner5c927502008-03-09 08:46:19 +00001656 MachineBasicBlock::iterator MI,
1657 unsigned DestReg, unsigned SrcReg,
1658 const TargetRegisterClass *DestRC,
1659 const TargetRegisterClass *SrcRC) const {
Bill Wendlingfbef3102009-02-11 21:51:19 +00001660 DebugLoc DL = DebugLoc::getUnknownLoc();
1661 if (MI != MBB.end()) DL = MI->getDebugLoc();
1662
Chris Lattner90b347d2008-03-09 07:58:04 +00001663 if (DestRC == SrcRC) {
1664 unsigned Opc;
1665 if (DestRC == &X86::GR64RegClass) {
1666 Opc = X86::MOV64rr;
1667 } else if (DestRC == &X86::GR32RegClass) {
1668 Opc = X86::MOV32rr;
1669 } else if (DestRC == &X86::GR16RegClass) {
1670 Opc = X86::MOV16rr;
1671 } else if (DestRC == &X86::GR8RegClass) {
1672 Opc = X86::MOV8rr;
1673 } else if (DestRC == &X86::GR32_RegClass) {
1674 Opc = X86::MOV32_rr;
1675 } else if (DestRC == &X86::GR16_RegClass) {
1676 Opc = X86::MOV16_rr;
1677 } else if (DestRC == &X86::RFP32RegClass) {
1678 Opc = X86::MOV_Fp3232;
1679 } else if (DestRC == &X86::RFP64RegClass || DestRC == &X86::RSTRegClass) {
1680 Opc = X86::MOV_Fp6464;
1681 } else if (DestRC == &X86::RFP80RegClass) {
1682 Opc = X86::MOV_Fp8080;
1683 } else if (DestRC == &X86::FR32RegClass) {
1684 Opc = X86::FsMOVAPSrr;
1685 } else if (DestRC == &X86::FR64RegClass) {
1686 Opc = X86::FsMOVAPDrr;
1687 } else if (DestRC == &X86::VR128RegClass) {
1688 Opc = X86::MOVAPSrr;
1689 } else if (DestRC == &X86::VR64RegClass) {
1690 Opc = X86::MMX_MOVQ64rr;
1691 } else {
Owen Anderson940f83e2008-08-26 18:03:31 +00001692 return false;
Owen Andersond10fd972007-12-31 06:32:00 +00001693 }
Bill Wendlingfbef3102009-02-11 21:51:19 +00001694 BuildMI(MBB, MI, DL, get(Opc), DestReg).addReg(SrcReg);
Owen Anderson940f83e2008-08-26 18:03:31 +00001695 return true;
Owen Andersond10fd972007-12-31 06:32:00 +00001696 }
Chris Lattner90b347d2008-03-09 07:58:04 +00001697
1698 // Moving EFLAGS to / from another register requires a push and a pop.
1699 if (SrcRC == &X86::CCRRegClass) {
Owen Andersona3177672008-08-26 18:50:40 +00001700 if (SrcReg != X86::EFLAGS)
1701 return false;
Chris Lattner90b347d2008-03-09 07:58:04 +00001702 if (DestRC == &X86::GR64RegClass) {
Bill Wendlingfbef3102009-02-11 21:51:19 +00001703 BuildMI(MBB, MI, DL, get(X86::PUSHFQ));
1704 BuildMI(MBB, MI, DL, get(X86::POP64r), DestReg);
Owen Anderson940f83e2008-08-26 18:03:31 +00001705 return true;
Chris Lattner90b347d2008-03-09 07:58:04 +00001706 } else if (DestRC == &X86::GR32RegClass) {
Bill Wendlingfbef3102009-02-11 21:51:19 +00001707 BuildMI(MBB, MI, DL, get(X86::PUSHFD));
1708 BuildMI(MBB, MI, DL, get(X86::POP32r), DestReg);
Owen Anderson940f83e2008-08-26 18:03:31 +00001709 return true;
Chris Lattner90b347d2008-03-09 07:58:04 +00001710 }
1711 } else if (DestRC == &X86::CCRRegClass) {
Owen Andersona3177672008-08-26 18:50:40 +00001712 if (DestReg != X86::EFLAGS)
1713 return false;
Chris Lattner90b347d2008-03-09 07:58:04 +00001714 if (SrcRC == &X86::GR64RegClass) {
Bill Wendlingfbef3102009-02-11 21:51:19 +00001715 BuildMI(MBB, MI, DL, get(X86::PUSH64r)).addReg(SrcReg);
1716 BuildMI(MBB, MI, DL, get(X86::POPFQ));
Owen Anderson940f83e2008-08-26 18:03:31 +00001717 return true;
Chris Lattner90b347d2008-03-09 07:58:04 +00001718 } else if (SrcRC == &X86::GR32RegClass) {
Bill Wendlingfbef3102009-02-11 21:51:19 +00001719 BuildMI(MBB, MI, DL, get(X86::PUSH32r)).addReg(SrcReg);
1720 BuildMI(MBB, MI, DL, get(X86::POPFD));
Owen Anderson940f83e2008-08-26 18:03:31 +00001721 return true;
Chris Lattner90b347d2008-03-09 07:58:04 +00001722 }
Owen Andersond10fd972007-12-31 06:32:00 +00001723 }
Chris Lattner5c927502008-03-09 08:46:19 +00001724
Chris Lattnerf30e1cf2008-03-09 09:15:31 +00001725 // Moving from ST(0) turns into FpGET_ST0_32 etc.
Chris Lattner5c927502008-03-09 08:46:19 +00001726 if (SrcRC == &X86::RSTRegClass) {
Chris Lattner24e0a542008-03-21 06:38:26 +00001727 // Copying from ST(0)/ST(1).
Owen Anderson940f83e2008-08-26 18:03:31 +00001728 if (SrcReg != X86::ST0 && SrcReg != X86::ST1)
1729 // Can only copy from ST(0)/ST(1) right now
1730 return false;
Chris Lattner24e0a542008-03-21 06:38:26 +00001731 bool isST0 = SrcReg == X86::ST0;
Chris Lattner5c927502008-03-09 08:46:19 +00001732 unsigned Opc;
1733 if (DestRC == &X86::RFP32RegClass)
Chris Lattner24e0a542008-03-21 06:38:26 +00001734 Opc = isST0 ? X86::FpGET_ST0_32 : X86::FpGET_ST1_32;
Chris Lattner5c927502008-03-09 08:46:19 +00001735 else if (DestRC == &X86::RFP64RegClass)
Chris Lattner24e0a542008-03-21 06:38:26 +00001736 Opc = isST0 ? X86::FpGET_ST0_64 : X86::FpGET_ST1_64;
Chris Lattner5c927502008-03-09 08:46:19 +00001737 else {
Owen Andersona3177672008-08-26 18:50:40 +00001738 if (DestRC != &X86::RFP80RegClass)
1739 return false;
Chris Lattner24e0a542008-03-21 06:38:26 +00001740 Opc = isST0 ? X86::FpGET_ST0_80 : X86::FpGET_ST1_80;
Chris Lattner5c927502008-03-09 08:46:19 +00001741 }
Bill Wendlingfbef3102009-02-11 21:51:19 +00001742 BuildMI(MBB, MI, DL, get(Opc), DestReg);
Owen Anderson940f83e2008-08-26 18:03:31 +00001743 return true;
Chris Lattner5c927502008-03-09 08:46:19 +00001744 }
Chris Lattnerf30e1cf2008-03-09 09:15:31 +00001745
1746 // Moving to ST(0) turns into FpSET_ST0_32 etc.
1747 if (DestRC == &X86::RSTRegClass) {
Evan Chenga0eedac2009-02-09 23:32:07 +00001748 // Copying to ST(0) / ST(1).
1749 if (DestReg != X86::ST0 && DestReg != X86::ST1)
Owen Anderson940f83e2008-08-26 18:03:31 +00001750 // Can only copy to TOS right now
1751 return false;
Evan Chenga0eedac2009-02-09 23:32:07 +00001752 bool isST0 = DestReg == X86::ST0;
Chris Lattnerf30e1cf2008-03-09 09:15:31 +00001753 unsigned Opc;
1754 if (SrcRC == &X86::RFP32RegClass)
Evan Chenga0eedac2009-02-09 23:32:07 +00001755 Opc = isST0 ? X86::FpSET_ST0_32 : X86::FpSET_ST1_32;
Chris Lattnerf30e1cf2008-03-09 09:15:31 +00001756 else if (SrcRC == &X86::RFP64RegClass)
Evan Chenga0eedac2009-02-09 23:32:07 +00001757 Opc = isST0 ? X86::FpSET_ST0_64 : X86::FpSET_ST1_64;
Chris Lattnerf30e1cf2008-03-09 09:15:31 +00001758 else {
Owen Andersona3177672008-08-26 18:50:40 +00001759 if (SrcRC != &X86::RFP80RegClass)
1760 return false;
Evan Chenga0eedac2009-02-09 23:32:07 +00001761 Opc = isST0 ? X86::FpSET_ST0_80 : X86::FpSET_ST1_80;
Chris Lattnerf30e1cf2008-03-09 09:15:31 +00001762 }
Bill Wendlingfbef3102009-02-11 21:51:19 +00001763 BuildMI(MBB, MI, DL, get(Opc)).addReg(SrcReg);
Owen Anderson940f83e2008-08-26 18:03:31 +00001764 return true;
Chris Lattnerf30e1cf2008-03-09 09:15:31 +00001765 }
Chris Lattner5c927502008-03-09 08:46:19 +00001766
Owen Anderson940f83e2008-08-26 18:03:31 +00001767 // Not yet supported!
1768 return false;
Owen Andersond10fd972007-12-31 06:32:00 +00001769}
1770
Owen Andersonf6372aa2008-01-01 21:11:32 +00001771static unsigned getStoreRegOpcode(const TargetRegisterClass *RC,
Anton Korobeynikov88bbf692008-07-19 06:30:51 +00001772 bool isStackAligned) {
Owen Andersonf6372aa2008-01-01 21:11:32 +00001773 unsigned Opc = 0;
1774 if (RC == &X86::GR64RegClass) {
1775 Opc = X86::MOV64mr;
1776 } else if (RC == &X86::GR32RegClass) {
1777 Opc = X86::MOV32mr;
1778 } else if (RC == &X86::GR16RegClass) {
1779 Opc = X86::MOV16mr;
1780 } else if (RC == &X86::GR8RegClass) {
1781 Opc = X86::MOV8mr;
1782 } else if (RC == &X86::GR32_RegClass) {
1783 Opc = X86::MOV32_mr;
1784 } else if (RC == &X86::GR16_RegClass) {
1785 Opc = X86::MOV16_mr;
1786 } else if (RC == &X86::RFP80RegClass) {
1787 Opc = X86::ST_FpP80m; // pops
1788 } else if (RC == &X86::RFP64RegClass) {
1789 Opc = X86::ST_Fp64m;
1790 } else if (RC == &X86::RFP32RegClass) {
1791 Opc = X86::ST_Fp32m;
1792 } else if (RC == &X86::FR32RegClass) {
1793 Opc = X86::MOVSSmr;
1794 } else if (RC == &X86::FR64RegClass) {
1795 Opc = X86::MOVSDmr;
1796 } else if (RC == &X86::VR128RegClass) {
Anton Korobeynikov88bbf692008-07-19 06:30:51 +00001797 // If stack is realigned we can use aligned stores.
1798 Opc = isStackAligned ? X86::MOVAPSmr : X86::MOVUPSmr;
Owen Andersonf6372aa2008-01-01 21:11:32 +00001799 } else if (RC == &X86::VR64RegClass) {
1800 Opc = X86::MMX_MOVQ64mr;
1801 } else {
1802 assert(0 && "Unknown regclass");
1803 abort();
1804 }
1805
1806 return Opc;
1807}
1808
1809void X86InstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
1810 MachineBasicBlock::iterator MI,
1811 unsigned SrcReg, bool isKill, int FrameIdx,
1812 const TargetRegisterClass *RC) const {
Anton Korobeynikov88bbf692008-07-19 06:30:51 +00001813 const MachineFunction &MF = *MBB.getParent();
Evan Cheng41c08402008-07-21 06:34:17 +00001814 bool isAligned = (RI.getStackAlignment() >= 16) ||
1815 RI.needsStackRealignment(MF);
1816 unsigned Opc = getStoreRegOpcode(RC, isAligned);
Bill Wendlingfbef3102009-02-11 21:51:19 +00001817 DebugLoc DL = DebugLoc::getUnknownLoc();
1818 if (MI != MBB.end()) DL = MI->getDebugLoc();
1819 addFrameReference(BuildMI(MBB, MI, DL, get(Opc)), FrameIdx)
1820 .addReg(SrcReg, false, false, isKill);
Owen Andersonf6372aa2008-01-01 21:11:32 +00001821}
1822
1823void X86InstrInfo::storeRegToAddr(MachineFunction &MF, unsigned SrcReg,
1824 bool isKill,
1825 SmallVectorImpl<MachineOperand> &Addr,
1826 const TargetRegisterClass *RC,
1827 SmallVectorImpl<MachineInstr*> &NewMIs) const {
Evan Cheng41c08402008-07-21 06:34:17 +00001828 bool isAligned = (RI.getStackAlignment() >= 16) ||
1829 RI.needsStackRealignment(MF);
1830 unsigned Opc = getStoreRegOpcode(RC, isAligned);
Dale Johannesen21b55412009-02-12 23:08:38 +00001831 DebugLoc DL = DebugLoc::getUnknownLoc();
1832 MachineInstrBuilder MIB = BuildMI(MF, DL, get(Opc));
Owen Andersonf6372aa2008-01-01 21:11:32 +00001833 for (unsigned i = 0, e = Addr.size(); i != e; ++i)
Dan Gohman97357612009-02-18 05:45:50 +00001834 MIB.addOperand(Addr[i]);
Owen Andersonf6372aa2008-01-01 21:11:32 +00001835 MIB.addReg(SrcReg, false, false, isKill);
1836 NewMIs.push_back(MIB);
1837}
1838
1839static unsigned getLoadRegOpcode(const TargetRegisterClass *RC,
Anton Korobeynikov88bbf692008-07-19 06:30:51 +00001840 bool isStackAligned) {
Owen Andersonf6372aa2008-01-01 21:11:32 +00001841 unsigned Opc = 0;
1842 if (RC == &X86::GR64RegClass) {
1843 Opc = X86::MOV64rm;
1844 } else if (RC == &X86::GR32RegClass) {
1845 Opc = X86::MOV32rm;
1846 } else if (RC == &X86::GR16RegClass) {
1847 Opc = X86::MOV16rm;
1848 } else if (RC == &X86::GR8RegClass) {
1849 Opc = X86::MOV8rm;
1850 } else if (RC == &X86::GR32_RegClass) {
1851 Opc = X86::MOV32_rm;
1852 } else if (RC == &X86::GR16_RegClass) {
1853 Opc = X86::MOV16_rm;
1854 } else if (RC == &X86::RFP80RegClass) {
1855 Opc = X86::LD_Fp80m;
1856 } else if (RC == &X86::RFP64RegClass) {
1857 Opc = X86::LD_Fp64m;
1858 } else if (RC == &X86::RFP32RegClass) {
1859 Opc = X86::LD_Fp32m;
1860 } else if (RC == &X86::FR32RegClass) {
1861 Opc = X86::MOVSSrm;
1862 } else if (RC == &X86::FR64RegClass) {
1863 Opc = X86::MOVSDrm;
1864 } else if (RC == &X86::VR128RegClass) {
Anton Korobeynikov88bbf692008-07-19 06:30:51 +00001865 // If stack is realigned we can use aligned loads.
1866 Opc = isStackAligned ? X86::MOVAPSrm : X86::MOVUPSrm;
Owen Andersonf6372aa2008-01-01 21:11:32 +00001867 } else if (RC == &X86::VR64RegClass) {
1868 Opc = X86::MMX_MOVQ64rm;
1869 } else {
1870 assert(0 && "Unknown regclass");
1871 abort();
1872 }
1873
1874 return Opc;
1875}
1876
1877void X86InstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
Anton Korobeynikov88bbf692008-07-19 06:30:51 +00001878 MachineBasicBlock::iterator MI,
1879 unsigned DestReg, int FrameIdx,
1880 const TargetRegisterClass *RC) const{
1881 const MachineFunction &MF = *MBB.getParent();
Evan Cheng41c08402008-07-21 06:34:17 +00001882 bool isAligned = (RI.getStackAlignment() >= 16) ||
1883 RI.needsStackRealignment(MF);
1884 unsigned Opc = getLoadRegOpcode(RC, isAligned);
Bill Wendlingfbef3102009-02-11 21:51:19 +00001885 DebugLoc DL = DebugLoc::getUnknownLoc();
1886 if (MI != MBB.end()) DL = MI->getDebugLoc();
1887 addFrameReference(BuildMI(MBB, MI, DL, get(Opc), DestReg), FrameIdx);
Owen Andersonf6372aa2008-01-01 21:11:32 +00001888}
1889
1890void X86InstrInfo::loadRegFromAddr(MachineFunction &MF, unsigned DestReg,
Evan Cheng9f1c8312008-07-03 09:09:37 +00001891 SmallVectorImpl<MachineOperand> &Addr,
1892 const TargetRegisterClass *RC,
Owen Andersonf6372aa2008-01-01 21:11:32 +00001893 SmallVectorImpl<MachineInstr*> &NewMIs) const {
Evan Cheng41c08402008-07-21 06:34:17 +00001894 bool isAligned = (RI.getStackAlignment() >= 16) ||
1895 RI.needsStackRealignment(MF);
1896 unsigned Opc = getLoadRegOpcode(RC, isAligned);
Dale Johannesen21b55412009-02-12 23:08:38 +00001897 DebugLoc DL = DebugLoc::getUnknownLoc();
1898 MachineInstrBuilder MIB = BuildMI(MF, DL, get(Opc), DestReg);
Owen Andersonf6372aa2008-01-01 21:11:32 +00001899 for (unsigned i = 0, e = Addr.size(); i != e; ++i)
Dan Gohman97357612009-02-18 05:45:50 +00001900 MIB.addOperand(Addr[i]);
Owen Andersonf6372aa2008-01-01 21:11:32 +00001901 NewMIs.push_back(MIB);
1902}
1903
Owen Andersond94b6a12008-01-04 23:57:37 +00001904bool X86InstrInfo::spillCalleeSavedRegisters(MachineBasicBlock &MBB,
Bill Wendlingfbef3102009-02-11 21:51:19 +00001905 MachineBasicBlock::iterator MI,
Owen Andersond94b6a12008-01-04 23:57:37 +00001906 const std::vector<CalleeSavedInfo> &CSI) const {
1907 if (CSI.empty())
1908 return false;
1909
Bill Wendlingfbef3102009-02-11 21:51:19 +00001910 DebugLoc DL = DebugLoc::getUnknownLoc();
1911 if (MI != MBB.end()) DL = MI->getDebugLoc();
1912
Evan Chenga67f32a2008-09-26 19:14:21 +00001913 bool is64Bit = TM.getSubtarget<X86Subtarget>().is64Bit();
Anton Korobeynikovc4e8bec2008-10-04 11:09:36 +00001914 unsigned SlotSize = is64Bit ? 8 : 4;
1915
1916 MachineFunction &MF = *MBB.getParent();
1917 X86MachineFunctionInfo *X86FI = MF.getInfo<X86MachineFunctionInfo>();
1918 X86FI->setCalleeSavedFrameSize(CSI.size() * SlotSize);
1919
Owen Andersond94b6a12008-01-04 23:57:37 +00001920 unsigned Opc = is64Bit ? X86::PUSH64r : X86::PUSH32r;
1921 for (unsigned i = CSI.size(); i != 0; --i) {
1922 unsigned Reg = CSI[i-1].getReg();
1923 // Add the callee-saved register as live-in. It's killed at the spill.
1924 MBB.addLiveIn(Reg);
Dale Johannesen8d13f8f2009-02-13 02:33:27 +00001925 BuildMI(MBB, MI, DL, get(Opc))
Dan Gohman25a1b472008-11-26 06:39:12 +00001926 .addReg(Reg, /*isDef=*/false, /*isImp=*/false, /*isKill=*/true);
Owen Andersond94b6a12008-01-04 23:57:37 +00001927 }
1928 return true;
1929}
1930
1931bool X86InstrInfo::restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
Bill Wendlingfbef3102009-02-11 21:51:19 +00001932 MachineBasicBlock::iterator MI,
Owen Andersond94b6a12008-01-04 23:57:37 +00001933 const std::vector<CalleeSavedInfo> &CSI) const {
1934 if (CSI.empty())
1935 return false;
Bill Wendlingfbef3102009-02-11 21:51:19 +00001936
1937 DebugLoc DL = DebugLoc::getUnknownLoc();
1938 if (MI != MBB.end()) DL = MI->getDebugLoc();
1939
Owen Andersond94b6a12008-01-04 23:57:37 +00001940 bool is64Bit = TM.getSubtarget<X86Subtarget>().is64Bit();
1941
1942 unsigned Opc = is64Bit ? X86::POP64r : X86::POP32r;
1943 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
1944 unsigned Reg = CSI[i].getReg();
Bill Wendlingfbef3102009-02-11 21:51:19 +00001945 BuildMI(MBB, MI, DL, get(Opc), Reg);
Owen Andersond94b6a12008-01-04 23:57:37 +00001946 }
1947 return true;
1948}
1949
Dan Gohman8e5f2c62008-07-07 23:14:23 +00001950static MachineInstr *FuseTwoAddrInst(MachineFunction &MF, unsigned Opcode,
Dan Gohmand68a0762009-01-05 17:59:02 +00001951 const SmallVectorImpl<MachineOperand> &MOs,
Bill Wendling9bc96a52009-02-03 00:55:04 +00001952 MachineInstr *MI,
1953 const TargetInstrInfo &TII) {
Owen Anderson43dbe052008-01-07 01:35:02 +00001954 // Create the base instruction with the memory operand as the first part.
Bill Wendling9bc96a52009-02-03 00:55:04 +00001955 MachineInstr *NewMI = MF.CreateMachineInstr(TII.get(Opcode),
1956 MI->getDebugLoc(), true);
Owen Anderson43dbe052008-01-07 01:35:02 +00001957 MachineInstrBuilder MIB(NewMI);
1958 unsigned NumAddrOps = MOs.size();
1959 for (unsigned i = 0; i != NumAddrOps; ++i)
Dan Gohman97357612009-02-18 05:45:50 +00001960 MIB.addOperand(MOs[i]);
Owen Anderson43dbe052008-01-07 01:35:02 +00001961 if (NumAddrOps < 4) // FrameIndex only
Rafael Espindola2a6411b2009-04-07 21:37:46 +00001962 addOffset(MIB, 0);
Owen Anderson43dbe052008-01-07 01:35:02 +00001963
1964 // Loop over the rest of the ri operands, converting them over.
Chris Lattner749c6f62008-01-07 07:27:27 +00001965 unsigned NumOps = MI->getDesc().getNumOperands()-2;
Owen Anderson43dbe052008-01-07 01:35:02 +00001966 for (unsigned i = 0; i != NumOps; ++i) {
1967 MachineOperand &MO = MI->getOperand(i+2);
Dan Gohman97357612009-02-18 05:45:50 +00001968 MIB.addOperand(MO);
Owen Anderson43dbe052008-01-07 01:35:02 +00001969 }
1970 for (unsigned i = NumOps+2, e = MI->getNumOperands(); i != e; ++i) {
1971 MachineOperand &MO = MI->getOperand(i);
Dan Gohman97357612009-02-18 05:45:50 +00001972 MIB.addOperand(MO);
Owen Anderson43dbe052008-01-07 01:35:02 +00001973 }
1974 return MIB;
1975}
1976
Dan Gohman8e5f2c62008-07-07 23:14:23 +00001977static MachineInstr *FuseInst(MachineFunction &MF,
1978 unsigned Opcode, unsigned OpNo,
Dan Gohmand68a0762009-01-05 17:59:02 +00001979 const SmallVectorImpl<MachineOperand> &MOs,
Owen Anderson43dbe052008-01-07 01:35:02 +00001980 MachineInstr *MI, const TargetInstrInfo &TII) {
Bill Wendling9bc96a52009-02-03 00:55:04 +00001981 MachineInstr *NewMI = MF.CreateMachineInstr(TII.get(Opcode),
1982 MI->getDebugLoc(), true);
Owen Anderson43dbe052008-01-07 01:35:02 +00001983 MachineInstrBuilder MIB(NewMI);
1984
1985 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1986 MachineOperand &MO = MI->getOperand(i);
1987 if (i == OpNo) {
Dan Gohmand735b802008-10-03 15:45:36 +00001988 assert(MO.isReg() && "Expected to fold into reg operand!");
Owen Anderson43dbe052008-01-07 01:35:02 +00001989 unsigned NumAddrOps = MOs.size();
1990 for (unsigned i = 0; i != NumAddrOps; ++i)
Dan Gohman97357612009-02-18 05:45:50 +00001991 MIB.addOperand(MOs[i]);
Owen Anderson43dbe052008-01-07 01:35:02 +00001992 if (NumAddrOps < 4) // FrameIndex only
Rafael Espindola2a6411b2009-04-07 21:37:46 +00001993 addOffset(MIB, 0);
Owen Anderson43dbe052008-01-07 01:35:02 +00001994 } else {
Dan Gohman97357612009-02-18 05:45:50 +00001995 MIB.addOperand(MO);
Owen Anderson43dbe052008-01-07 01:35:02 +00001996 }
1997 }
1998 return MIB;
1999}
2000
2001static MachineInstr *MakeM0Inst(const TargetInstrInfo &TII, unsigned Opcode,
Dan Gohmand68a0762009-01-05 17:59:02 +00002002 const SmallVectorImpl<MachineOperand> &MOs,
Owen Anderson43dbe052008-01-07 01:35:02 +00002003 MachineInstr *MI) {
Dan Gohman8e5f2c62008-07-07 23:14:23 +00002004 MachineFunction &MF = *MI->getParent()->getParent();
Bill Wendlingfbef3102009-02-11 21:51:19 +00002005 MachineInstrBuilder MIB = BuildMI(MF, MI->getDebugLoc(), TII.get(Opcode));
Owen Anderson43dbe052008-01-07 01:35:02 +00002006
2007 unsigned NumAddrOps = MOs.size();
2008 for (unsigned i = 0; i != NumAddrOps; ++i)
Dan Gohman97357612009-02-18 05:45:50 +00002009 MIB.addOperand(MOs[i]);
Owen Anderson43dbe052008-01-07 01:35:02 +00002010 if (NumAddrOps < 4) // FrameIndex only
Rafael Espindola2a6411b2009-04-07 21:37:46 +00002011 addOffset(MIB, 0);
Owen Anderson43dbe052008-01-07 01:35:02 +00002012 return MIB.addImm(0);
2013}
2014
2015MachineInstr*
Dan Gohmanc54baa22008-12-03 18:43:12 +00002016X86InstrInfo::foldMemoryOperandImpl(MachineFunction &MF,
2017 MachineInstr *MI, unsigned i,
Dan Gohmand68a0762009-01-05 17:59:02 +00002018 const SmallVectorImpl<MachineOperand> &MOs) const{
Owen Anderson43dbe052008-01-07 01:35:02 +00002019 const DenseMap<unsigned*, unsigned> *OpcodeTablePtr = NULL;
2020 bool isTwoAddrFold = false;
Chris Lattner749c6f62008-01-07 07:27:27 +00002021 unsigned NumOps = MI->getDesc().getNumOperands();
Owen Anderson43dbe052008-01-07 01:35:02 +00002022 bool isTwoAddr = NumOps > 1 &&
Chris Lattner749c6f62008-01-07 07:27:27 +00002023 MI->getDesc().getOperandConstraint(1, TOI::TIED_TO) != -1;
Owen Anderson43dbe052008-01-07 01:35:02 +00002024
2025 MachineInstr *NewMI = NULL;
2026 // Folding a memory location into the two-address part of a two-address
2027 // instruction is different than folding it other places. It requires
2028 // replacing the *two* registers with the memory location.
2029 if (isTwoAddr && NumOps >= 2 && i < 2 &&
Dan Gohmand735b802008-10-03 15:45:36 +00002030 MI->getOperand(0).isReg() &&
2031 MI->getOperand(1).isReg() &&
Owen Anderson43dbe052008-01-07 01:35:02 +00002032 MI->getOperand(0).getReg() == MI->getOperand(1).getReg()) {
2033 OpcodeTablePtr = &RegOp2MemOpTable2Addr;
2034 isTwoAddrFold = true;
2035 } else if (i == 0) { // If operand 0
2036 if (MI->getOpcode() == X86::MOV16r0)
2037 NewMI = MakeM0Inst(*this, X86::MOV16mi, MOs, MI);
2038 else if (MI->getOpcode() == X86::MOV32r0)
2039 NewMI = MakeM0Inst(*this, X86::MOV32mi, MOs, MI);
2040 else if (MI->getOpcode() == X86::MOV64r0)
2041 NewMI = MakeM0Inst(*this, X86::MOV64mi32, MOs, MI);
2042 else if (MI->getOpcode() == X86::MOV8r0)
2043 NewMI = MakeM0Inst(*this, X86::MOV8mi, MOs, MI);
Evan Cheng9f1c8312008-07-03 09:09:37 +00002044 if (NewMI)
Owen Anderson43dbe052008-01-07 01:35:02 +00002045 return NewMI;
Owen Anderson43dbe052008-01-07 01:35:02 +00002046
2047 OpcodeTablePtr = &RegOp2MemOpTable0;
2048 } else if (i == 1) {
2049 OpcodeTablePtr = &RegOp2MemOpTable1;
2050 } else if (i == 2) {
2051 OpcodeTablePtr = &RegOp2MemOpTable2;
2052 }
2053
2054 // If table selected...
2055 if (OpcodeTablePtr) {
2056 // Find the Opcode to fuse
2057 DenseMap<unsigned*, unsigned>::iterator I =
2058 OpcodeTablePtr->find((unsigned*)MI->getOpcode());
2059 if (I != OpcodeTablePtr->end()) {
2060 if (isTwoAddrFold)
Dan Gohman8e5f2c62008-07-07 23:14:23 +00002061 NewMI = FuseTwoAddrInst(MF, I->second, MOs, MI, *this);
Owen Anderson43dbe052008-01-07 01:35:02 +00002062 else
Dan Gohman8e5f2c62008-07-07 23:14:23 +00002063 NewMI = FuseInst(MF, I->second, i, MOs, MI, *this);
Owen Anderson43dbe052008-01-07 01:35:02 +00002064 return NewMI;
2065 }
2066 }
2067
2068 // No fusion
2069 if (PrintFailedFusing)
Dan Gohman15a3a492008-12-23 00:19:20 +00002070 cerr << "We failed to fuse operand " << i << " in " << *MI;
Owen Anderson43dbe052008-01-07 01:35:02 +00002071 return NULL;
2072}
2073
2074
Dan Gohmanc54baa22008-12-03 18:43:12 +00002075MachineInstr* X86InstrInfo::foldMemoryOperandImpl(MachineFunction &MF,
2076 MachineInstr *MI,
2077 const SmallVectorImpl<unsigned> &Ops,
2078 int FrameIndex) const {
Owen Anderson43dbe052008-01-07 01:35:02 +00002079 // Check switch flag
2080 if (NoFusing) return NULL;
2081
Evan Cheng5fd79d02008-02-08 21:20:40 +00002082 const MachineFrameInfo *MFI = MF.getFrameInfo();
2083 unsigned Alignment = MFI->getObjectAlignment(FrameIndex);
2084 // FIXME: Move alignment requirement into tables?
2085 if (Alignment < 16) {
2086 switch (MI->getOpcode()) {
2087 default: break;
2088 // Not always safe to fold movsd into these instructions since their load
2089 // folding variants expects the address to be 16 byte aligned.
2090 case X86::FsANDNPDrr:
2091 case X86::FsANDNPSrr:
2092 case X86::FsANDPDrr:
2093 case X86::FsANDPSrr:
2094 case X86::FsORPDrr:
2095 case X86::FsORPSrr:
2096 case X86::FsXORPDrr:
2097 case X86::FsXORPSrr:
2098 return NULL;
2099 }
2100 }
2101
Owen Anderson43dbe052008-01-07 01:35:02 +00002102 if (Ops.size() == 2 && Ops[0] == 0 && Ops[1] == 1) {
2103 unsigned NewOpc = 0;
2104 switch (MI->getOpcode()) {
2105 default: return NULL;
2106 case X86::TEST8rr: NewOpc = X86::CMP8ri; break;
2107 case X86::TEST16rr: NewOpc = X86::CMP16ri; break;
2108 case X86::TEST32rr: NewOpc = X86::CMP32ri; break;
2109 case X86::TEST64rr: NewOpc = X86::CMP64ri32; break;
2110 }
2111 // Change to CMPXXri r, 0 first.
Chris Lattner5080f4d2008-01-11 18:10:50 +00002112 MI->setDesc(get(NewOpc));
Owen Anderson43dbe052008-01-07 01:35:02 +00002113 MI->getOperand(1).ChangeToImmediate(0);
2114 } else if (Ops.size() != 1)
2115 return NULL;
2116
2117 SmallVector<MachineOperand,4> MOs;
2118 MOs.push_back(MachineOperand::CreateFI(FrameIndex));
Dan Gohmanc54baa22008-12-03 18:43:12 +00002119 return foldMemoryOperandImpl(MF, MI, Ops[0], MOs);
Owen Anderson43dbe052008-01-07 01:35:02 +00002120}
2121
Dan Gohmanc54baa22008-12-03 18:43:12 +00002122MachineInstr* X86InstrInfo::foldMemoryOperandImpl(MachineFunction &MF,
2123 MachineInstr *MI,
2124 const SmallVectorImpl<unsigned> &Ops,
2125 MachineInstr *LoadMI) const {
Owen Anderson43dbe052008-01-07 01:35:02 +00002126 // Check switch flag
2127 if (NoFusing) return NULL;
2128
Dan Gohmancddc11e2008-07-12 00:10:52 +00002129 // Determine the alignment of the load.
Evan Cheng5fd79d02008-02-08 21:20:40 +00002130 unsigned Alignment = 0;
Dan Gohmancddc11e2008-07-12 00:10:52 +00002131 if (LoadMI->hasOneMemOperand())
2132 Alignment = LoadMI->memoperands_begin()->getAlignment();
Evan Cheng5fd79d02008-02-08 21:20:40 +00002133
2134 // FIXME: Move alignment requirement into tables?
2135 if (Alignment < 16) {
2136 switch (MI->getOpcode()) {
2137 default: break;
2138 // Not always safe to fold movsd into these instructions since their load
2139 // folding variants expects the address to be 16 byte aligned.
2140 case X86::FsANDNPDrr:
2141 case X86::FsANDNPSrr:
2142 case X86::FsANDPDrr:
2143 case X86::FsANDPSrr:
2144 case X86::FsORPDrr:
2145 case X86::FsORPSrr:
2146 case X86::FsXORPDrr:
2147 case X86::FsXORPSrr:
2148 return NULL;
2149 }
2150 }
2151
Owen Anderson43dbe052008-01-07 01:35:02 +00002152 if (Ops.size() == 2 && Ops[0] == 0 && Ops[1] == 1) {
2153 unsigned NewOpc = 0;
2154 switch (MI->getOpcode()) {
2155 default: return NULL;
2156 case X86::TEST8rr: NewOpc = X86::CMP8ri; break;
2157 case X86::TEST16rr: NewOpc = X86::CMP16ri; break;
2158 case X86::TEST32rr: NewOpc = X86::CMP32ri; break;
2159 case X86::TEST64rr: NewOpc = X86::CMP64ri32; break;
2160 }
2161 // Change to CMPXXri r, 0 first.
Chris Lattner5080f4d2008-01-11 18:10:50 +00002162 MI->setDesc(get(NewOpc));
Owen Anderson43dbe052008-01-07 01:35:02 +00002163 MI->getOperand(1).ChangeToImmediate(0);
2164 } else if (Ops.size() != 1)
2165 return NULL;
2166
Rafael Espindola2a6411b2009-04-07 21:37:46 +00002167 SmallVector<MachineOperand,X86AddrNumOperands> MOs;
Dan Gohman62c939d2008-12-03 05:21:24 +00002168 if (LoadMI->getOpcode() == X86::V_SET0 ||
2169 LoadMI->getOpcode() == X86::V_SETALLONES) {
2170 // Folding a V_SET0 or V_SETALLONES as a load, to ease register pressure.
2171 // Create a constant-pool entry and operands to load from it.
2172
2173 // x86-32 PIC requires a PIC base register for constant pools.
2174 unsigned PICBase = 0;
2175 if (TM.getRelocationModel() == Reloc::PIC_ &&
2176 !TM.getSubtarget<X86Subtarget>().is64Bit())
Evan Cheng84100ba2008-12-05 17:23:48 +00002177 // FIXME: PICBase = TM.getInstrInfo()->getGlobalBaseReg(&MF);
2178 // This doesn't work for several reasons.
2179 // 1. GlobalBaseReg may have been spilled.
2180 // 2. It may not be live at MI.
Evan Cheng84100ba2008-12-05 17:23:48 +00002181 return false;
Dan Gohman62c939d2008-12-03 05:21:24 +00002182
2183 // Create a v4i32 constant-pool entry.
2184 MachineConstantPool &MCP = *MF.getConstantPool();
2185 const VectorType *Ty = VectorType::get(Type::Int32Ty, 4);
2186 Constant *C = LoadMI->getOpcode() == X86::V_SET0 ?
2187 ConstantVector::getNullValue(Ty) :
2188 ConstantVector::getAllOnesValue(Ty);
Evan Cheng1606e8e2009-03-13 07:51:59 +00002189 unsigned CPI = MCP.getConstantPoolIndex(C, 16);
Dan Gohman62c939d2008-12-03 05:21:24 +00002190
2191 // Create operands to load from the constant pool entry.
2192 MOs.push_back(MachineOperand::CreateReg(PICBase, false));
2193 MOs.push_back(MachineOperand::CreateImm(1));
2194 MOs.push_back(MachineOperand::CreateReg(0, false));
2195 MOs.push_back(MachineOperand::CreateCPI(CPI, 0));
Rafael Espindola2a6411b2009-04-07 21:37:46 +00002196 MOs.push_back(MachineOperand::CreateReg(0, false));
Dan Gohman62c939d2008-12-03 05:21:24 +00002197 } else {
2198 // Folding a normal load. Just copy the load's address operands.
2199 unsigned NumOps = LoadMI->getDesc().getNumOperands();
Rafael Espindola705d8002009-03-27 15:57:50 +00002200 for (unsigned i = NumOps - X86AddrNumOperands; i != NumOps; ++i)
Dan Gohman62c939d2008-12-03 05:21:24 +00002201 MOs.push_back(LoadMI->getOperand(i));
2202 }
Dan Gohmanc54baa22008-12-03 18:43:12 +00002203 return foldMemoryOperandImpl(MF, MI, Ops[0], MOs);
Owen Anderson43dbe052008-01-07 01:35:02 +00002204}
2205
2206
Dan Gohman8e8b8a22008-10-16 01:49:15 +00002207bool X86InstrInfo::canFoldMemoryOperand(const MachineInstr *MI,
2208 const SmallVectorImpl<unsigned> &Ops) const {
Owen Anderson43dbe052008-01-07 01:35:02 +00002209 // Check switch flag
2210 if (NoFusing) return 0;
2211
2212 if (Ops.size() == 2 && Ops[0] == 0 && Ops[1] == 1) {
2213 switch (MI->getOpcode()) {
2214 default: return false;
2215 case X86::TEST8rr:
2216 case X86::TEST16rr:
2217 case X86::TEST32rr:
2218 case X86::TEST64rr:
2219 return true;
2220 }
2221 }
2222
2223 if (Ops.size() != 1)
2224 return false;
2225
2226 unsigned OpNum = Ops[0];
2227 unsigned Opc = MI->getOpcode();
Chris Lattner749c6f62008-01-07 07:27:27 +00002228 unsigned NumOps = MI->getDesc().getNumOperands();
Owen Anderson43dbe052008-01-07 01:35:02 +00002229 bool isTwoAddr = NumOps > 1 &&
Chris Lattner749c6f62008-01-07 07:27:27 +00002230 MI->getDesc().getOperandConstraint(1, TOI::TIED_TO) != -1;
Owen Anderson43dbe052008-01-07 01:35:02 +00002231
2232 // Folding a memory location into the two-address part of a two-address
2233 // instruction is different than folding it other places. It requires
2234 // replacing the *two* registers with the memory location.
2235 const DenseMap<unsigned*, unsigned> *OpcodeTablePtr = NULL;
2236 if (isTwoAddr && NumOps >= 2 && OpNum < 2) {
2237 OpcodeTablePtr = &RegOp2MemOpTable2Addr;
2238 } else if (OpNum == 0) { // If operand 0
2239 switch (Opc) {
2240 case X86::MOV16r0:
2241 case X86::MOV32r0:
2242 case X86::MOV64r0:
2243 case X86::MOV8r0:
2244 return true;
2245 default: break;
2246 }
2247 OpcodeTablePtr = &RegOp2MemOpTable0;
2248 } else if (OpNum == 1) {
2249 OpcodeTablePtr = &RegOp2MemOpTable1;
2250 } else if (OpNum == 2) {
2251 OpcodeTablePtr = &RegOp2MemOpTable2;
2252 }
2253
2254 if (OpcodeTablePtr) {
2255 // Find the Opcode to fuse
2256 DenseMap<unsigned*, unsigned>::iterator I =
2257 OpcodeTablePtr->find((unsigned*)Opc);
2258 if (I != OpcodeTablePtr->end())
2259 return true;
2260 }
2261 return false;
2262}
2263
2264bool X86InstrInfo::unfoldMemoryOperand(MachineFunction &MF, MachineInstr *MI,
2265 unsigned Reg, bool UnfoldLoad, bool UnfoldStore,
Bill Wendlingfbef3102009-02-11 21:51:19 +00002266 SmallVectorImpl<MachineInstr*> &NewMIs) const {
Owen Anderson43dbe052008-01-07 01:35:02 +00002267 DenseMap<unsigned*, std::pair<unsigned,unsigned> >::iterator I =
2268 MemOp2RegOpTable.find((unsigned*)MI->getOpcode());
2269 if (I == MemOp2RegOpTable.end())
2270 return false;
Dale Johannesen21b55412009-02-12 23:08:38 +00002271 DebugLoc dl = MI->getDebugLoc();
Owen Anderson43dbe052008-01-07 01:35:02 +00002272 unsigned Opc = I->second.first;
2273 unsigned Index = I->second.second & 0xf;
2274 bool FoldedLoad = I->second.second & (1 << 4);
2275 bool FoldedStore = I->second.second & (1 << 5);
2276 if (UnfoldLoad && !FoldedLoad)
2277 return false;
2278 UnfoldLoad &= FoldedLoad;
2279 if (UnfoldStore && !FoldedStore)
2280 return false;
2281 UnfoldStore &= FoldedStore;
2282
Chris Lattner749c6f62008-01-07 07:27:27 +00002283 const TargetInstrDesc &TID = get(Opc);
Owen Anderson43dbe052008-01-07 01:35:02 +00002284 const TargetOperandInfo &TOI = TID.OpInfo[Index];
Chris Lattner8ca5c672008-01-07 02:39:19 +00002285 const TargetRegisterClass *RC = TOI.isLookupPtrRegClass()
Evan Cheng770bcc72009-02-06 17:43:24 +00002286 ? RI.getPointerRegClass() : RI.getRegClass(TOI.RegClass);
Rafael Espindola705d8002009-03-27 15:57:50 +00002287 SmallVector<MachineOperand, X86AddrNumOperands> AddrOps;
Owen Anderson43dbe052008-01-07 01:35:02 +00002288 SmallVector<MachineOperand,2> BeforeOps;
2289 SmallVector<MachineOperand,2> AfterOps;
2290 SmallVector<MachineOperand,4> ImpOps;
2291 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
2292 MachineOperand &Op = MI->getOperand(i);
Rafael Espindola705d8002009-03-27 15:57:50 +00002293 if (i >= Index && i < Index + X86AddrNumOperands)
Owen Anderson43dbe052008-01-07 01:35:02 +00002294 AddrOps.push_back(Op);
Dan Gohmand735b802008-10-03 15:45:36 +00002295 else if (Op.isReg() && Op.isImplicit())
Owen Anderson43dbe052008-01-07 01:35:02 +00002296 ImpOps.push_back(Op);
2297 else if (i < Index)
2298 BeforeOps.push_back(Op);
2299 else if (i > Index)
2300 AfterOps.push_back(Op);
2301 }
2302
2303 // Emit the load instruction.
2304 if (UnfoldLoad) {
2305 loadRegFromAddr(MF, Reg, AddrOps, RC, NewMIs);
2306 if (UnfoldStore) {
2307 // Address operands cannot be marked isKill.
Rafael Espindola705d8002009-03-27 15:57:50 +00002308 for (unsigned i = 1; i != 1 + X86AddrNumOperands; ++i) {
Owen Anderson43dbe052008-01-07 01:35:02 +00002309 MachineOperand &MO = NewMIs[0]->getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +00002310 if (MO.isReg())
Owen Anderson43dbe052008-01-07 01:35:02 +00002311 MO.setIsKill(false);
2312 }
2313 }
2314 }
2315
2316 // Emit the data processing instruction.
Bill Wendling9bc96a52009-02-03 00:55:04 +00002317 MachineInstr *DataMI = MF.CreateMachineInstr(TID, MI->getDebugLoc(), true);
Owen Anderson43dbe052008-01-07 01:35:02 +00002318 MachineInstrBuilder MIB(DataMI);
2319
2320 if (FoldedStore)
2321 MIB.addReg(Reg, true);
2322 for (unsigned i = 0, e = BeforeOps.size(); i != e; ++i)
Dan Gohman97357612009-02-18 05:45:50 +00002323 MIB.addOperand(BeforeOps[i]);
Owen Anderson43dbe052008-01-07 01:35:02 +00002324 if (FoldedLoad)
2325 MIB.addReg(Reg);
2326 for (unsigned i = 0, e = AfterOps.size(); i != e; ++i)
Dan Gohman97357612009-02-18 05:45:50 +00002327 MIB.addOperand(AfterOps[i]);
Owen Anderson43dbe052008-01-07 01:35:02 +00002328 for (unsigned i = 0, e = ImpOps.size(); i != e; ++i) {
2329 MachineOperand &MO = ImpOps[i];
2330 MIB.addReg(MO.getReg(), MO.isDef(), true, MO.isKill(), MO.isDead());
2331 }
2332 // Change CMP32ri r, 0 back to TEST32rr r, r, etc.
2333 unsigned NewOpc = 0;
2334 switch (DataMI->getOpcode()) {
2335 default: break;
2336 case X86::CMP64ri32:
2337 case X86::CMP32ri:
2338 case X86::CMP16ri:
2339 case X86::CMP8ri: {
2340 MachineOperand &MO0 = DataMI->getOperand(0);
2341 MachineOperand &MO1 = DataMI->getOperand(1);
2342 if (MO1.getImm() == 0) {
2343 switch (DataMI->getOpcode()) {
2344 default: break;
2345 case X86::CMP64ri32: NewOpc = X86::TEST64rr; break;
2346 case X86::CMP32ri: NewOpc = X86::TEST32rr; break;
2347 case X86::CMP16ri: NewOpc = X86::TEST16rr; break;
2348 case X86::CMP8ri: NewOpc = X86::TEST8rr; break;
2349 }
Chris Lattner5080f4d2008-01-11 18:10:50 +00002350 DataMI->setDesc(get(NewOpc));
Owen Anderson43dbe052008-01-07 01:35:02 +00002351 MO1.ChangeToRegister(MO0.getReg(), false);
2352 }
2353 }
2354 }
2355 NewMIs.push_back(DataMI);
2356
2357 // Emit the store instruction.
2358 if (UnfoldStore) {
2359 const TargetOperandInfo &DstTOI = TID.OpInfo[0];
Chris Lattner8ca5c672008-01-07 02:39:19 +00002360 const TargetRegisterClass *DstRC = DstTOI.isLookupPtrRegClass()
Evan Cheng770bcc72009-02-06 17:43:24 +00002361 ? RI.getPointerRegClass() : RI.getRegClass(DstTOI.RegClass);
Owen Anderson43dbe052008-01-07 01:35:02 +00002362 storeRegToAddr(MF, Reg, true, AddrOps, DstRC, NewMIs);
2363 }
2364
2365 return true;
2366}
2367
2368bool
2369X86InstrInfo::unfoldMemoryOperand(SelectionDAG &DAG, SDNode *N,
Bill Wendlingfbef3102009-02-11 21:51:19 +00002370 SmallVectorImpl<SDNode*> &NewNodes) const {
Dan Gohmane8be6c62008-07-17 19:10:17 +00002371 if (!N->isMachineOpcode())
Owen Anderson43dbe052008-01-07 01:35:02 +00002372 return false;
2373
2374 DenseMap<unsigned*, std::pair<unsigned,unsigned> >::iterator I =
Dan Gohmane8be6c62008-07-17 19:10:17 +00002375 MemOp2RegOpTable.find((unsigned*)N->getMachineOpcode());
Owen Anderson43dbe052008-01-07 01:35:02 +00002376 if (I == MemOp2RegOpTable.end())
2377 return false;
2378 unsigned Opc = I->second.first;
2379 unsigned Index = I->second.second & 0xf;
2380 bool FoldedLoad = I->second.second & (1 << 4);
2381 bool FoldedStore = I->second.second & (1 << 5);
Chris Lattner749c6f62008-01-07 07:27:27 +00002382 const TargetInstrDesc &TID = get(Opc);
Owen Anderson43dbe052008-01-07 01:35:02 +00002383 const TargetOperandInfo &TOI = TID.OpInfo[Index];
Chris Lattner8ca5c672008-01-07 02:39:19 +00002384 const TargetRegisterClass *RC = TOI.isLookupPtrRegClass()
Evan Cheng770bcc72009-02-06 17:43:24 +00002385 ? RI.getPointerRegClass() : RI.getRegClass(TOI.RegClass);
Dan Gohmanb37a8202009-03-04 19:23:38 +00002386 unsigned NumDefs = TID.NumDefs;
Dan Gohman475871a2008-07-27 21:46:04 +00002387 std::vector<SDValue> AddrOps;
2388 std::vector<SDValue> BeforeOps;
2389 std::vector<SDValue> AfterOps;
Dale Johannesened2eee62009-02-06 01:31:28 +00002390 DebugLoc dl = N->getDebugLoc();
Owen Anderson43dbe052008-01-07 01:35:02 +00002391 unsigned NumOps = N->getNumOperands();
2392 for (unsigned i = 0; i != NumOps-1; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002393 SDValue Op = N->getOperand(i);
Rafael Espindola705d8002009-03-27 15:57:50 +00002394 if (i >= Index-NumDefs && i < Index-NumDefs + X86AddrNumOperands)
Owen Anderson43dbe052008-01-07 01:35:02 +00002395 AddrOps.push_back(Op);
Dan Gohmanb37a8202009-03-04 19:23:38 +00002396 else if (i < Index-NumDefs)
Owen Anderson43dbe052008-01-07 01:35:02 +00002397 BeforeOps.push_back(Op);
Dan Gohmanb37a8202009-03-04 19:23:38 +00002398 else if (i > Index-NumDefs)
Owen Anderson43dbe052008-01-07 01:35:02 +00002399 AfterOps.push_back(Op);
2400 }
Dan Gohman475871a2008-07-27 21:46:04 +00002401 SDValue Chain = N->getOperand(NumOps-1);
Owen Anderson43dbe052008-01-07 01:35:02 +00002402 AddrOps.push_back(Chain);
2403
2404 // Emit the load instruction.
2405 SDNode *Load = 0;
Anton Korobeynikov88bbf692008-07-19 06:30:51 +00002406 const MachineFunction &MF = DAG.getMachineFunction();
Owen Anderson43dbe052008-01-07 01:35:02 +00002407 if (FoldedLoad) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002408 MVT VT = *RC->vt_begin();
Evan Cheng41c08402008-07-21 06:34:17 +00002409 bool isAligned = (RI.getStackAlignment() >= 16) ||
2410 RI.needsStackRealignment(MF);
Dale Johannesened2eee62009-02-06 01:31:28 +00002411 Load = DAG.getTargetNode(getLoadRegOpcode(RC, isAligned), dl,
Anton Korobeynikov88bbf692008-07-19 06:30:51 +00002412 VT, MVT::Other,
2413 &AddrOps[0], AddrOps.size());
Owen Anderson43dbe052008-01-07 01:35:02 +00002414 NewNodes.push_back(Load);
2415 }
2416
2417 // Emit the data processing instruction.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002418 std::vector<MVT> VTs;
Owen Anderson43dbe052008-01-07 01:35:02 +00002419 const TargetRegisterClass *DstRC = 0;
Chris Lattner349c4952008-01-07 03:13:06 +00002420 if (TID.getNumDefs() > 0) {
Owen Anderson43dbe052008-01-07 01:35:02 +00002421 const TargetOperandInfo &DstTOI = TID.OpInfo[0];
Chris Lattner8ca5c672008-01-07 02:39:19 +00002422 DstRC = DstTOI.isLookupPtrRegClass()
Evan Cheng770bcc72009-02-06 17:43:24 +00002423 ? RI.getPointerRegClass() : RI.getRegClass(DstTOI.RegClass);
Owen Anderson43dbe052008-01-07 01:35:02 +00002424 VTs.push_back(*DstRC->vt_begin());
2425 }
2426 for (unsigned i = 0, e = N->getNumValues(); i != e; ++i) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002427 MVT VT = N->getValueType(i);
Chris Lattner349c4952008-01-07 03:13:06 +00002428 if (VT != MVT::Other && i >= (unsigned)TID.getNumDefs())
Owen Anderson43dbe052008-01-07 01:35:02 +00002429 VTs.push_back(VT);
2430 }
2431 if (Load)
Dan Gohman475871a2008-07-27 21:46:04 +00002432 BeforeOps.push_back(SDValue(Load, 0));
Owen Anderson43dbe052008-01-07 01:35:02 +00002433 std::copy(AfterOps.begin(), AfterOps.end(), std::back_inserter(BeforeOps));
Dale Johannesened2eee62009-02-06 01:31:28 +00002434 SDNode *NewNode= DAG.getTargetNode(Opc, dl, VTs, &BeforeOps[0],
2435 BeforeOps.size());
Owen Anderson43dbe052008-01-07 01:35:02 +00002436 NewNodes.push_back(NewNode);
2437
2438 // Emit the store instruction.
2439 if (FoldedStore) {
2440 AddrOps.pop_back();
Dan Gohman475871a2008-07-27 21:46:04 +00002441 AddrOps.push_back(SDValue(NewNode, 0));
Owen Anderson43dbe052008-01-07 01:35:02 +00002442 AddrOps.push_back(Chain);
Evan Cheng41c08402008-07-21 06:34:17 +00002443 bool isAligned = (RI.getStackAlignment() >= 16) ||
2444 RI.needsStackRealignment(MF);
Dale Johannesened2eee62009-02-06 01:31:28 +00002445 SDNode *Store = DAG.getTargetNode(getStoreRegOpcode(DstRC, isAligned), dl,
Evan Cheng41c08402008-07-21 06:34:17 +00002446 MVT::Other, &AddrOps[0], AddrOps.size());
Owen Anderson43dbe052008-01-07 01:35:02 +00002447 NewNodes.push_back(Store);
2448 }
2449
2450 return true;
2451}
2452
2453unsigned X86InstrInfo::getOpcodeAfterMemoryUnfold(unsigned Opc,
2454 bool UnfoldLoad, bool UnfoldStore) const {
2455 DenseMap<unsigned*, std::pair<unsigned,unsigned> >::iterator I =
2456 MemOp2RegOpTable.find((unsigned*)Opc);
2457 if (I == MemOp2RegOpTable.end())
2458 return 0;
2459 bool FoldedLoad = I->second.second & (1 << 4);
2460 bool FoldedStore = I->second.second & (1 << 5);
2461 if (UnfoldLoad && !FoldedLoad)
2462 return 0;
2463 if (UnfoldStore && !FoldedStore)
2464 return 0;
2465 return I->second.first;
2466}
2467
Dan Gohman8e8b8a22008-10-16 01:49:15 +00002468bool X86InstrInfo::BlockHasNoFallThrough(const MachineBasicBlock &MBB) const {
Chris Lattnerc24ff8e2006-10-28 17:29:57 +00002469 if (MBB.empty()) return false;
2470
2471 switch (MBB.back().getOpcode()) {
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002472 case X86::TCRETURNri:
2473 case X86::TCRETURNdi:
Evan Cheng126f17a2007-05-21 18:44:17 +00002474 case X86::RET: // Return.
2475 case X86::RETI:
2476 case X86::TAILJMPd:
2477 case X86::TAILJMPr:
2478 case X86::TAILJMPm:
Chris Lattnerc24ff8e2006-10-28 17:29:57 +00002479 case X86::JMP: // Uncond branch.
2480 case X86::JMP32r: // Indirect branch.
Dan Gohmana0a7c1d2007-09-17 15:19:08 +00002481 case X86::JMP64r: // Indirect branch (64-bit).
Chris Lattnerc24ff8e2006-10-28 17:29:57 +00002482 case X86::JMP32m: // Indirect branch through mem.
Dan Gohmana0a7c1d2007-09-17 15:19:08 +00002483 case X86::JMP64m: // Indirect branch through mem (64-bit).
Chris Lattnerc24ff8e2006-10-28 17:29:57 +00002484 return true;
2485 default: return false;
2486 }
2487}
2488
Chris Lattner7fbe9722006-10-20 17:42:20 +00002489bool X86InstrInfo::
Owen Anderson44eb65c2008-08-14 22:49:33 +00002490ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
Chris Lattner9cd68752006-10-21 05:52:40 +00002491 assert(Cond.size() == 1 && "Invalid X86 branch condition!");
Evan Cheng97af60b2008-08-29 23:21:31 +00002492 X86::CondCode CC = static_cast<X86::CondCode>(Cond[0].getImm());
Dan Gohman279c22e2008-10-21 03:29:32 +00002493 if (CC == X86::COND_NE_OR_P || CC == X86::COND_NP_OR_E)
2494 return true;
Evan Cheng97af60b2008-08-29 23:21:31 +00002495 Cond[0].setImm(GetOppositeBranchCondition(CC));
Chris Lattner9cd68752006-10-21 05:52:40 +00002496 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00002497}
2498
Evan Cheng23066282008-10-27 07:14:50 +00002499bool X86InstrInfo::
Evan Cheng4350eb82009-02-06 17:17:30 +00002500isSafeToMoveRegClassDefs(const TargetRegisterClass *RC) const {
2501 // FIXME: Return false for x87 stack register classes for now. We can't
Evan Cheng23066282008-10-27 07:14:50 +00002502 // allow any loads of these registers before FpGet_ST0_80.
Evan Cheng4350eb82009-02-06 17:17:30 +00002503 return !(RC == &X86::CCRRegClass || RC == &X86::RFP32RegClass ||
2504 RC == &X86::RFP64RegClass || RC == &X86::RFP80RegClass);
Evan Cheng23066282008-10-27 07:14:50 +00002505}
2506
Nicolas Geoffray52e724a2008-04-16 20:10:13 +00002507unsigned X86InstrInfo::sizeOfImm(const TargetInstrDesc *Desc) {
2508 switch (Desc->TSFlags & X86II::ImmMask) {
2509 case X86II::Imm8: return 1;
2510 case X86II::Imm16: return 2;
2511 case X86II::Imm32: return 4;
2512 case X86II::Imm64: return 8;
2513 default: assert(0 && "Immediate size not set!");
2514 return 0;
2515 }
2516}
2517
2518/// isX86_64ExtendedReg - Is the MachineOperand a x86-64 extended register?
2519/// e.g. r8, xmm8, etc.
2520bool X86InstrInfo::isX86_64ExtendedReg(const MachineOperand &MO) {
Dan Gohmand735b802008-10-03 15:45:36 +00002521 if (!MO.isReg()) return false;
Nicolas Geoffray52e724a2008-04-16 20:10:13 +00002522 switch (MO.getReg()) {
2523 default: break;
2524 case X86::R8: case X86::R9: case X86::R10: case X86::R11:
2525 case X86::R12: case X86::R13: case X86::R14: case X86::R15:
2526 case X86::R8D: case X86::R9D: case X86::R10D: case X86::R11D:
2527 case X86::R12D: case X86::R13D: case X86::R14D: case X86::R15D:
2528 case X86::R8W: case X86::R9W: case X86::R10W: case X86::R11W:
2529 case X86::R12W: case X86::R13W: case X86::R14W: case X86::R15W:
2530 case X86::R8B: case X86::R9B: case X86::R10B: case X86::R11B:
2531 case X86::R12B: case X86::R13B: case X86::R14B: case X86::R15B:
2532 case X86::XMM8: case X86::XMM9: case X86::XMM10: case X86::XMM11:
2533 case X86::XMM12: case X86::XMM13: case X86::XMM14: case X86::XMM15:
2534 return true;
2535 }
2536 return false;
2537}
2538
2539
2540/// determineREX - Determine if the MachineInstr has to be encoded with a X86-64
2541/// REX prefix which specifies 1) 64-bit instructions, 2) non-default operand
2542/// size, and 3) use of X86-64 extended registers.
2543unsigned X86InstrInfo::determineREX(const MachineInstr &MI) {
2544 unsigned REX = 0;
2545 const TargetInstrDesc &Desc = MI.getDesc();
2546
2547 // Pseudo instructions do not need REX prefix byte.
2548 if ((Desc.TSFlags & X86II::FormMask) == X86II::Pseudo)
2549 return 0;
2550 if (Desc.TSFlags & X86II::REX_W)
2551 REX |= 1 << 3;
2552
2553 unsigned NumOps = Desc.getNumOperands();
2554 if (NumOps) {
2555 bool isTwoAddr = NumOps > 1 &&
2556 Desc.getOperandConstraint(1, TOI::TIED_TO) != -1;
2557
2558 // If it accesses SPL, BPL, SIL, or DIL, then it requires a 0x40 REX prefix.
2559 unsigned i = isTwoAddr ? 1 : 0;
2560 for (unsigned e = NumOps; i != e; ++i) {
2561 const MachineOperand& MO = MI.getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +00002562 if (MO.isReg()) {
Nicolas Geoffray52e724a2008-04-16 20:10:13 +00002563 unsigned Reg = MO.getReg();
2564 if (isX86_64NonExtLowByteReg(Reg))
2565 REX |= 0x40;
2566 }
2567 }
2568
2569 switch (Desc.TSFlags & X86II::FormMask) {
2570 case X86II::MRMInitReg:
2571 if (isX86_64ExtendedReg(MI.getOperand(0)))
2572 REX |= (1 << 0) | (1 << 2);
2573 break;
2574 case X86II::MRMSrcReg: {
2575 if (isX86_64ExtendedReg(MI.getOperand(0)))
2576 REX |= 1 << 2;
2577 i = isTwoAddr ? 2 : 1;
2578 for (unsigned e = NumOps; i != e; ++i) {
2579 const MachineOperand& MO = MI.getOperand(i);
2580 if (isX86_64ExtendedReg(MO))
2581 REX |= 1 << 0;
2582 }
2583 break;
2584 }
2585 case X86II::MRMSrcMem: {
2586 if (isX86_64ExtendedReg(MI.getOperand(0)))
2587 REX |= 1 << 2;
2588 unsigned Bit = 0;
2589 i = isTwoAddr ? 2 : 1;
2590 for (; i != NumOps; ++i) {
2591 const MachineOperand& MO = MI.getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +00002592 if (MO.isReg()) {
Nicolas Geoffray52e724a2008-04-16 20:10:13 +00002593 if (isX86_64ExtendedReg(MO))
2594 REX |= 1 << Bit;
2595 Bit++;
2596 }
2597 }
2598 break;
2599 }
2600 case X86II::MRM0m: case X86II::MRM1m:
2601 case X86II::MRM2m: case X86II::MRM3m:
2602 case X86II::MRM4m: case X86II::MRM5m:
2603 case X86II::MRM6m: case X86II::MRM7m:
2604 case X86II::MRMDestMem: {
2605 unsigned e = isTwoAddr ? 5 : 4;
2606 i = isTwoAddr ? 1 : 0;
2607 if (NumOps > e && isX86_64ExtendedReg(MI.getOperand(e)))
2608 REX |= 1 << 2;
2609 unsigned Bit = 0;
2610 for (; i != e; ++i) {
2611 const MachineOperand& MO = MI.getOperand(i);
Dan Gohmand735b802008-10-03 15:45:36 +00002612 if (MO.isReg()) {
Nicolas Geoffray52e724a2008-04-16 20:10:13 +00002613 if (isX86_64ExtendedReg(MO))
2614 REX |= 1 << Bit;
2615 Bit++;
2616 }
2617 }
2618 break;
2619 }
2620 default: {
2621 if (isX86_64ExtendedReg(MI.getOperand(0)))
2622 REX |= 1 << 0;
2623 i = isTwoAddr ? 2 : 1;
2624 for (unsigned e = NumOps; i != e; ++i) {
2625 const MachineOperand& MO = MI.getOperand(i);
2626 if (isX86_64ExtendedReg(MO))
2627 REX |= 1 << 2;
2628 }
2629 break;
2630 }
2631 }
2632 }
2633 return REX;
2634}
2635
2636/// sizePCRelativeBlockAddress - This method returns the size of a PC
2637/// relative block address instruction
2638///
2639static unsigned sizePCRelativeBlockAddress() {
2640 return 4;
2641}
2642
2643/// sizeGlobalAddress - Give the size of the emission of this global address
2644///
2645static unsigned sizeGlobalAddress(bool dword) {
2646 return dword ? 8 : 4;
2647}
2648
2649/// sizeConstPoolAddress - Give the size of the emission of this constant
2650/// pool address
2651///
2652static unsigned sizeConstPoolAddress(bool dword) {
2653 return dword ? 8 : 4;
2654}
2655
2656/// sizeExternalSymbolAddress - Give the size of the emission of this external
2657/// symbol
2658///
2659static unsigned sizeExternalSymbolAddress(bool dword) {
2660 return dword ? 8 : 4;
2661}
2662
2663/// sizeJumpTableAddress - Give the size of the emission of this jump
2664/// table address
2665///
2666static unsigned sizeJumpTableAddress(bool dword) {
2667 return dword ? 8 : 4;
2668}
2669
2670static unsigned sizeConstant(unsigned Size) {
2671 return Size;
2672}
2673
2674static unsigned sizeRegModRMByte(){
2675 return 1;
2676}
2677
2678static unsigned sizeSIBByte(){
2679 return 1;
2680}
2681
2682static unsigned getDisplacementFieldSize(const MachineOperand *RelocOp) {
2683 unsigned FinalSize = 0;
2684 // If this is a simple integer displacement that doesn't require a relocation.
2685 if (!RelocOp) {
2686 FinalSize += sizeConstant(4);
2687 return FinalSize;
2688 }
2689
2690 // Otherwise, this is something that requires a relocation.
Dan Gohmand735b802008-10-03 15:45:36 +00002691 if (RelocOp->isGlobal()) {
Nicolas Geoffray52e724a2008-04-16 20:10:13 +00002692 FinalSize += sizeGlobalAddress(false);
Dan Gohmand735b802008-10-03 15:45:36 +00002693 } else if (RelocOp->isCPI()) {
Nicolas Geoffray52e724a2008-04-16 20:10:13 +00002694 FinalSize += sizeConstPoolAddress(false);
Dan Gohmand735b802008-10-03 15:45:36 +00002695 } else if (RelocOp->isJTI()) {
Nicolas Geoffray52e724a2008-04-16 20:10:13 +00002696 FinalSize += sizeJumpTableAddress(false);
2697 } else {
2698 assert(0 && "Unknown value to relocate!");
2699 }
2700 return FinalSize;
2701}
2702
2703static unsigned getMemModRMByteSize(const MachineInstr &MI, unsigned Op,
2704 bool IsPIC, bool Is64BitMode) {
2705 const MachineOperand &Op3 = MI.getOperand(Op+3);
2706 int DispVal = 0;
2707 const MachineOperand *DispForReloc = 0;
2708 unsigned FinalSize = 0;
2709
2710 // Figure out what sort of displacement we have to handle here.
Dan Gohmand735b802008-10-03 15:45:36 +00002711 if (Op3.isGlobal()) {
Nicolas Geoffray52e724a2008-04-16 20:10:13 +00002712 DispForReloc = &Op3;
Dan Gohmand735b802008-10-03 15:45:36 +00002713 } else if (Op3.isCPI()) {
Nicolas Geoffray52e724a2008-04-16 20:10:13 +00002714 if (Is64BitMode || IsPIC) {
2715 DispForReloc = &Op3;
2716 } else {
2717 DispVal = 1;
2718 }
Dan Gohmand735b802008-10-03 15:45:36 +00002719 } else if (Op3.isJTI()) {
Nicolas Geoffray52e724a2008-04-16 20:10:13 +00002720 if (Is64BitMode || IsPIC) {
2721 DispForReloc = &Op3;
2722 } else {
2723 DispVal = 1;
2724 }
2725 } else {
2726 DispVal = 1;
2727 }
2728
2729 const MachineOperand &Base = MI.getOperand(Op);
2730 const MachineOperand &IndexReg = MI.getOperand(Op+2);
2731
2732 unsigned BaseReg = Base.getReg();
2733
2734 // Is a SIB byte needed?
2735 if (IndexReg.getReg() == 0 &&
2736 (BaseReg == 0 || X86RegisterInfo::getX86RegNum(BaseReg) != N86::ESP)) {
2737 if (BaseReg == 0) { // Just a displacement?
2738 // Emit special case [disp32] encoding
2739 ++FinalSize;
2740 FinalSize += getDisplacementFieldSize(DispForReloc);
2741 } else {
2742 unsigned BaseRegNo = X86RegisterInfo::getX86RegNum(BaseReg);
2743 if (!DispForReloc && DispVal == 0 && BaseRegNo != N86::EBP) {
2744 // Emit simple indirect register encoding... [EAX] f.e.
2745 ++FinalSize;
2746 // Be pessimistic and assume it's a disp32, not a disp8
2747 } else {
2748 // Emit the most general non-SIB encoding: [REG+disp32]
2749 ++FinalSize;
2750 FinalSize += getDisplacementFieldSize(DispForReloc);
2751 }
2752 }
2753
2754 } else { // We need a SIB byte, so start by outputting the ModR/M byte first
2755 assert(IndexReg.getReg() != X86::ESP &&
2756 IndexReg.getReg() != X86::RSP && "Cannot use ESP as index reg!");
2757
2758 bool ForceDisp32 = false;
2759 if (BaseReg == 0 || DispForReloc) {
2760 // Emit the normal disp32 encoding.
2761 ++FinalSize;
2762 ForceDisp32 = true;
2763 } else {
2764 ++FinalSize;
2765 }
2766
2767 FinalSize += sizeSIBByte();
2768
2769 // Do we need to output a displacement?
2770 if (DispVal != 0 || ForceDisp32) {
2771 FinalSize += getDisplacementFieldSize(DispForReloc);
2772 }
2773 }
2774 return FinalSize;
2775}
2776
2777
2778static unsigned GetInstSizeWithDesc(const MachineInstr &MI,
2779 const TargetInstrDesc *Desc,
2780 bool IsPIC, bool Is64BitMode) {
2781
2782 unsigned Opcode = Desc->Opcode;
2783 unsigned FinalSize = 0;
2784
2785 // Emit the lock opcode prefix as needed.
2786 if (Desc->TSFlags & X86II::LOCK) ++FinalSize;
2787
Anton Korobeynikovd21a6302008-10-12 10:30:11 +00002788 // Emit segment overrid opcode prefix as needed.
2789 switch (Desc->TSFlags & X86II::SegOvrMask) {
2790 case X86II::FS:
2791 case X86II::GS:
2792 ++FinalSize;
2793 break;
2794 default: assert(0 && "Invalid segment!");
2795 case 0: break; // No segment override!
2796 }
2797
Nicolas Geoffray52e724a2008-04-16 20:10:13 +00002798 // Emit the repeat opcode prefix as needed.
2799 if ((Desc->TSFlags & X86II::Op0Mask) == X86II::REP) ++FinalSize;
2800
2801 // Emit the operand size opcode prefix as needed.
2802 if (Desc->TSFlags & X86II::OpSize) ++FinalSize;
2803
2804 // Emit the address size opcode prefix as needed.
2805 if (Desc->TSFlags & X86II::AdSize) ++FinalSize;
2806
2807 bool Need0FPrefix = false;
2808 switch (Desc->TSFlags & X86II::Op0Mask) {
2809 case X86II::TB: // Two-byte opcode prefix
2810 case X86II::T8: // 0F 38
2811 case X86II::TA: // 0F 3A
2812 Need0FPrefix = true;
2813 break;
2814 case X86II::REP: break; // already handled.
2815 case X86II::XS: // F3 0F
2816 ++FinalSize;
2817 Need0FPrefix = true;
2818 break;
2819 case X86II::XD: // F2 0F
2820 ++FinalSize;
2821 Need0FPrefix = true;
2822 break;
2823 case X86II::D8: case X86II::D9: case X86II::DA: case X86II::DB:
2824 case X86II::DC: case X86II::DD: case X86II::DE: case X86II::DF:
2825 ++FinalSize;
2826 break; // Two-byte opcode prefix
2827 default: assert(0 && "Invalid prefix!");
2828 case 0: break; // No prefix!
2829 }
2830
2831 if (Is64BitMode) {
2832 // REX prefix
2833 unsigned REX = X86InstrInfo::determineREX(MI);
2834 if (REX)
2835 ++FinalSize;
2836 }
2837
2838 // 0x0F escape code must be emitted just before the opcode.
2839 if (Need0FPrefix)
2840 ++FinalSize;
2841
2842 switch (Desc->TSFlags & X86II::Op0Mask) {
2843 case X86II::T8: // 0F 38
2844 ++FinalSize;
2845 break;
2846 case X86II::TA: // 0F 3A
2847 ++FinalSize;
2848 break;
2849 }
2850
2851 // If this is a two-address instruction, skip one of the register operands.
2852 unsigned NumOps = Desc->getNumOperands();
2853 unsigned CurOp = 0;
2854 if (NumOps > 1 && Desc->getOperandConstraint(1, TOI::TIED_TO) != -1)
2855 CurOp++;
2856
2857 switch (Desc->TSFlags & X86II::FormMask) {
2858 default: assert(0 && "Unknown FormMask value in X86 MachineCodeEmitter!");
2859 case X86II::Pseudo:
2860 // Remember the current PC offset, this is the PIC relocation
2861 // base address.
2862 switch (Opcode) {
2863 default:
2864 break;
2865 case TargetInstrInfo::INLINEASM: {
2866 const MachineFunction *MF = MI.getParent()->getParent();
2867 const char *AsmStr = MI.getOperand(0).getSymbolName();
2868 const TargetAsmInfo* AI = MF->getTarget().getTargetAsmInfo();
2869 FinalSize += AI->getInlineAsmLength(AsmStr);
2870 break;
2871 }
Dan Gohman44066042008-07-01 00:05:16 +00002872 case TargetInstrInfo::DBG_LABEL:
2873 case TargetInstrInfo::EH_LABEL:
Nicolas Geoffray52e724a2008-04-16 20:10:13 +00002874 break;
2875 case TargetInstrInfo::IMPLICIT_DEF:
2876 case TargetInstrInfo::DECLARE:
2877 case X86::DWARF_LOC:
2878 case X86::FP_REG_KILL:
2879 break;
2880 case X86::MOVPC32r: {
2881 // This emits the "call" portion of this pseudo instruction.
2882 ++FinalSize;
2883 FinalSize += sizeConstant(X86InstrInfo::sizeOfImm(Desc));
2884 break;
2885 }
2886 }
2887 CurOp = NumOps;
2888 break;
2889 case X86II::RawFrm:
2890 ++FinalSize;
2891
2892 if (CurOp != NumOps) {
2893 const MachineOperand &MO = MI.getOperand(CurOp++);
Dan Gohmand735b802008-10-03 15:45:36 +00002894 if (MO.isMBB()) {
Nicolas Geoffray52e724a2008-04-16 20:10:13 +00002895 FinalSize += sizePCRelativeBlockAddress();
Dan Gohmand735b802008-10-03 15:45:36 +00002896 } else if (MO.isGlobal()) {
Nicolas Geoffray52e724a2008-04-16 20:10:13 +00002897 FinalSize += sizeGlobalAddress(false);
Dan Gohmand735b802008-10-03 15:45:36 +00002898 } else if (MO.isSymbol()) {
Nicolas Geoffray52e724a2008-04-16 20:10:13 +00002899 FinalSize += sizeExternalSymbolAddress(false);
Dan Gohmand735b802008-10-03 15:45:36 +00002900 } else if (MO.isImm()) {
Nicolas Geoffray52e724a2008-04-16 20:10:13 +00002901 FinalSize += sizeConstant(X86InstrInfo::sizeOfImm(Desc));
2902 } else {
2903 assert(0 && "Unknown RawFrm operand!");
2904 }
2905 }
2906 break;
2907
2908 case X86II::AddRegFrm:
2909 ++FinalSize;
Nicolas Geoffray546e36a2008-04-20 23:36:47 +00002910 ++CurOp;
Nicolas Geoffray52e724a2008-04-16 20:10:13 +00002911
2912 if (CurOp != NumOps) {
2913 const MachineOperand &MO1 = MI.getOperand(CurOp++);
2914 unsigned Size = X86InstrInfo::sizeOfImm(Desc);
Dan Gohmand735b802008-10-03 15:45:36 +00002915 if (MO1.isImm())
Nicolas Geoffray52e724a2008-04-16 20:10:13 +00002916 FinalSize += sizeConstant(Size);
2917 else {
2918 bool dword = false;
2919 if (Opcode == X86::MOV64ri)
2920 dword = true;
Dan Gohmand735b802008-10-03 15:45:36 +00002921 if (MO1.isGlobal()) {
Nicolas Geoffray52e724a2008-04-16 20:10:13 +00002922 FinalSize += sizeGlobalAddress(dword);
Dan Gohmand735b802008-10-03 15:45:36 +00002923 } else if (MO1.isSymbol())
Nicolas Geoffray52e724a2008-04-16 20:10:13 +00002924 FinalSize += sizeExternalSymbolAddress(dword);
Dan Gohmand735b802008-10-03 15:45:36 +00002925 else if (MO1.isCPI())
Nicolas Geoffray52e724a2008-04-16 20:10:13 +00002926 FinalSize += sizeConstPoolAddress(dword);
Dan Gohmand735b802008-10-03 15:45:36 +00002927 else if (MO1.isJTI())
Nicolas Geoffray52e724a2008-04-16 20:10:13 +00002928 FinalSize += sizeJumpTableAddress(dword);
2929 }
2930 }
2931 break;
2932
2933 case X86II::MRMDestReg: {
2934 ++FinalSize;
2935 FinalSize += sizeRegModRMByte();
2936 CurOp += 2;
Nicolas Geoffray546e36a2008-04-20 23:36:47 +00002937 if (CurOp != NumOps) {
2938 ++CurOp;
Nicolas Geoffray52e724a2008-04-16 20:10:13 +00002939 FinalSize += sizeConstant(X86InstrInfo::sizeOfImm(Desc));
Nicolas Geoffray546e36a2008-04-20 23:36:47 +00002940 }
Nicolas Geoffray52e724a2008-04-16 20:10:13 +00002941 break;
2942 }
2943 case X86II::MRMDestMem: {
2944 ++FinalSize;
2945 FinalSize += getMemModRMByteSize(MI, CurOp, IsPIC, Is64BitMode);
2946 CurOp += 5;
Nicolas Geoffray546e36a2008-04-20 23:36:47 +00002947 if (CurOp != NumOps) {
2948 ++CurOp;
Nicolas Geoffray52e724a2008-04-16 20:10:13 +00002949 FinalSize += sizeConstant(X86InstrInfo::sizeOfImm(Desc));
Nicolas Geoffray546e36a2008-04-20 23:36:47 +00002950 }
Nicolas Geoffray52e724a2008-04-16 20:10:13 +00002951 break;
2952 }
2953
2954 case X86II::MRMSrcReg:
2955 ++FinalSize;
2956 FinalSize += sizeRegModRMByte();
2957 CurOp += 2;
Nicolas Geoffray546e36a2008-04-20 23:36:47 +00002958 if (CurOp != NumOps) {
2959 ++CurOp;
Nicolas Geoffray52e724a2008-04-16 20:10:13 +00002960 FinalSize += sizeConstant(X86InstrInfo::sizeOfImm(Desc));
Nicolas Geoffray546e36a2008-04-20 23:36:47 +00002961 }
Nicolas Geoffray52e724a2008-04-16 20:10:13 +00002962 break;
2963
2964 case X86II::MRMSrcMem: {
2965
2966 ++FinalSize;
2967 FinalSize += getMemModRMByteSize(MI, CurOp+1, IsPIC, Is64BitMode);
2968 CurOp += 5;
Nicolas Geoffray546e36a2008-04-20 23:36:47 +00002969 if (CurOp != NumOps) {
2970 ++CurOp;
Nicolas Geoffray52e724a2008-04-16 20:10:13 +00002971 FinalSize += sizeConstant(X86InstrInfo::sizeOfImm(Desc));
Nicolas Geoffray546e36a2008-04-20 23:36:47 +00002972 }
Nicolas Geoffray52e724a2008-04-16 20:10:13 +00002973 break;
2974 }
2975
2976 case X86II::MRM0r: case X86II::MRM1r:
2977 case X86II::MRM2r: case X86II::MRM3r:
2978 case X86II::MRM4r: case X86II::MRM5r:
2979 case X86II::MRM6r: case X86II::MRM7r:
2980 ++FinalSize;
Nicolas Geoffray546e36a2008-04-20 23:36:47 +00002981 ++CurOp;
Nicolas Geoffray52e724a2008-04-16 20:10:13 +00002982 FinalSize += sizeRegModRMByte();
2983
2984 if (CurOp != NumOps) {
2985 const MachineOperand &MO1 = MI.getOperand(CurOp++);
2986 unsigned Size = X86InstrInfo::sizeOfImm(Desc);
Dan Gohmand735b802008-10-03 15:45:36 +00002987 if (MO1.isImm())
Nicolas Geoffray52e724a2008-04-16 20:10:13 +00002988 FinalSize += sizeConstant(Size);
2989 else {
2990 bool dword = false;
2991 if (Opcode == X86::MOV64ri32)
2992 dword = true;
Dan Gohmand735b802008-10-03 15:45:36 +00002993 if (MO1.isGlobal()) {
Nicolas Geoffray52e724a2008-04-16 20:10:13 +00002994 FinalSize += sizeGlobalAddress(dword);
Dan Gohmand735b802008-10-03 15:45:36 +00002995 } else if (MO1.isSymbol())
Nicolas Geoffray52e724a2008-04-16 20:10:13 +00002996 FinalSize += sizeExternalSymbolAddress(dword);
Dan Gohmand735b802008-10-03 15:45:36 +00002997 else if (MO1.isCPI())
Nicolas Geoffray52e724a2008-04-16 20:10:13 +00002998 FinalSize += sizeConstPoolAddress(dword);
Dan Gohmand735b802008-10-03 15:45:36 +00002999 else if (MO1.isJTI())
Nicolas Geoffray52e724a2008-04-16 20:10:13 +00003000 FinalSize += sizeJumpTableAddress(dword);
3001 }
3002 }
3003 break;
3004
3005 case X86II::MRM0m: case X86II::MRM1m:
3006 case X86II::MRM2m: case X86II::MRM3m:
3007 case X86II::MRM4m: case X86II::MRM5m:
3008 case X86II::MRM6m: case X86II::MRM7m: {
3009
3010 ++FinalSize;
3011 FinalSize += getMemModRMByteSize(MI, CurOp, IsPIC, Is64BitMode);
3012 CurOp += 4;
3013
3014 if (CurOp != NumOps) {
3015 const MachineOperand &MO = MI.getOperand(CurOp++);
3016 unsigned Size = X86InstrInfo::sizeOfImm(Desc);
Dan Gohmand735b802008-10-03 15:45:36 +00003017 if (MO.isImm())
Nicolas Geoffray52e724a2008-04-16 20:10:13 +00003018 FinalSize += sizeConstant(Size);
3019 else {
3020 bool dword = false;
3021 if (Opcode == X86::MOV64mi32)
3022 dword = true;
Dan Gohmand735b802008-10-03 15:45:36 +00003023 if (MO.isGlobal()) {
Nicolas Geoffray52e724a2008-04-16 20:10:13 +00003024 FinalSize += sizeGlobalAddress(dword);
Dan Gohmand735b802008-10-03 15:45:36 +00003025 } else if (MO.isSymbol())
Nicolas Geoffray52e724a2008-04-16 20:10:13 +00003026 FinalSize += sizeExternalSymbolAddress(dword);
Dan Gohmand735b802008-10-03 15:45:36 +00003027 else if (MO.isCPI())
Nicolas Geoffray52e724a2008-04-16 20:10:13 +00003028 FinalSize += sizeConstPoolAddress(dword);
Dan Gohmand735b802008-10-03 15:45:36 +00003029 else if (MO.isJTI())
Nicolas Geoffray52e724a2008-04-16 20:10:13 +00003030 FinalSize += sizeJumpTableAddress(dword);
3031 }
3032 }
3033 break;
3034 }
3035
3036 case X86II::MRMInitReg:
3037 ++FinalSize;
3038 // Duplicate register, used by things like MOV8r0 (aka xor reg,reg).
3039 FinalSize += sizeRegModRMByte();
3040 ++CurOp;
3041 break;
3042 }
3043
3044 if (!Desc->isVariadic() && CurOp != NumOps) {
3045 cerr << "Cannot determine size: ";
3046 MI.dump();
3047 cerr << '\n';
3048 abort();
3049 }
3050
3051
3052 return FinalSize;
3053}
3054
3055
3056unsigned X86InstrInfo::GetInstSizeInBytes(const MachineInstr *MI) const {
3057 const TargetInstrDesc &Desc = MI->getDesc();
3058 bool IsPIC = (TM.getRelocationModel() == Reloc::PIC_);
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00003059 bool Is64BitMode = TM.getSubtargetImpl()->is64Bit();
Nicolas Geoffray52e724a2008-04-16 20:10:13 +00003060 unsigned Size = GetInstSizeWithDesc(*MI, &Desc, IsPIC, Is64BitMode);
3061 if (Desc.getOpcode() == X86::MOVPC32r) {
3062 Size += GetInstSizeWithDesc(*MI, &get(X86::POP32r), IsPIC, Is64BitMode);
3063 }
3064 return Size;
3065}
Dan Gohman8b746962008-09-23 18:22:58 +00003066
Dan Gohman57c3dac2008-09-30 00:58:23 +00003067/// getGlobalBaseReg - Return a virtual register initialized with the
3068/// the global base register value. Output instructions required to
3069/// initialize the register in the function entry block, if necessary.
Dan Gohman8b746962008-09-23 18:22:58 +00003070///
Dan Gohman57c3dac2008-09-30 00:58:23 +00003071unsigned X86InstrInfo::getGlobalBaseReg(MachineFunction *MF) const {
3072 assert(!TM.getSubtarget<X86Subtarget>().is64Bit() &&
3073 "X86-64 PIC uses RIP relative addressing");
3074
3075 X86MachineFunctionInfo *X86FI = MF->getInfo<X86MachineFunctionInfo>();
3076 unsigned GlobalBaseReg = X86FI->getGlobalBaseReg();
3077 if (GlobalBaseReg != 0)
3078 return GlobalBaseReg;
3079
Dan Gohman8b746962008-09-23 18:22:58 +00003080 // Insert the set of GlobalBaseReg into the first MBB of the function
3081 MachineBasicBlock &FirstMBB = MF->front();
3082 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
Bill Wendlingfbef3102009-02-11 21:51:19 +00003083 DebugLoc DL = DebugLoc::getUnknownLoc();
3084 if (MBBI != FirstMBB.end()) DL = MBBI->getDebugLoc();
Dan Gohman8b746962008-09-23 18:22:58 +00003085 MachineRegisterInfo &RegInfo = MF->getRegInfo();
3086 unsigned PC = RegInfo.createVirtualRegister(X86::GR32RegisterClass);
3087
3088 const TargetInstrInfo *TII = TM.getInstrInfo();
3089 // Operand of MovePCtoStack is completely ignored by asm printer. It's
3090 // only used in JIT code emission as displacement to pc.
Bill Wendlingfbef3102009-02-11 21:51:19 +00003091 BuildMI(FirstMBB, MBBI, DL, TII->get(X86::MOVPC32r), PC)
3092 .addImm(0);
Dan Gohman8b746962008-09-23 18:22:58 +00003093
3094 // If we're using vanilla 'GOT' PIC style, we should use relative addressing
3095 // not to pc, but to _GLOBAL_ADDRESS_TABLE_ external
3096 if (TM.getRelocationModel() == Reloc::PIC_ &&
3097 TM.getSubtarget<X86Subtarget>().isPICStyleGOT()) {
Dan Gohman57c3dac2008-09-30 00:58:23 +00003098 GlobalBaseReg =
Dan Gohman8b746962008-09-23 18:22:58 +00003099 RegInfo.createVirtualRegister(X86::GR32RegisterClass);
Bill Wendlingfbef3102009-02-11 21:51:19 +00003100 BuildMI(FirstMBB, MBBI, DL, TII->get(X86::ADD32ri), GlobalBaseReg)
Dan Gohman8b746962008-09-23 18:22:58 +00003101 .addReg(PC).addExternalSymbol("_GLOBAL_OFFSET_TABLE_");
Dan Gohman57c3dac2008-09-30 00:58:23 +00003102 } else {
3103 GlobalBaseReg = PC;
Dan Gohman8b746962008-09-23 18:22:58 +00003104 }
3105
Dan Gohman57c3dac2008-09-30 00:58:23 +00003106 X86FI->setGlobalBaseReg(GlobalBaseReg);
3107 return GlobalBaseReg;
Dan Gohman8b746962008-09-23 18:22:58 +00003108}