blob: 80cf3866ab80e35ccdfca7d31d4de04183fe0935 [file] [log] [blame]
Chris Lattner7a125372005-11-16 22:59:19 +00001//===- X86ISelDAGToDAG.cpp - A DAG pattern matching inst selector for X86 -===//
Chris Lattnerc961eea2005-11-16 01:54:32 +00002//
3// 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.
Chris Lattnerc961eea2005-11-16 01:54:32 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines a DAG pattern matching instruction selector for X86,
11// converting from a legalized dag to a X86 dag.
12//
13//===----------------------------------------------------------------------===//
14
Evan Cheng2ef88a02006-08-07 22:28:20 +000015#define DEBUG_TYPE "x86-isel"
Chris Lattnerc961eea2005-11-16 01:54:32 +000016#include "X86.h"
Evan Cheng8700e142006-01-11 06:09:51 +000017#include "X86InstrBuilder.h"
Evan Chengc4c62572006-03-13 23:20:37 +000018#include "X86ISelLowering.h"
Evan Cheng0475ab52008-01-05 00:41:47 +000019#include "X86MachineFunctionInfo.h"
Chris Lattner92cb0af2006-01-11 01:15:34 +000020#include "X86RegisterInfo.h"
Chris Lattnerc961eea2005-11-16 01:54:32 +000021#include "X86Subtarget.h"
Evan Chengc4c62572006-03-13 23:20:37 +000022#include "X86TargetMachine.h"
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000023#include "llvm/GlobalValue.h"
Chris Lattner92cb0af2006-01-11 01:15:34 +000024#include "llvm/Instructions.h"
Chris Lattner420736d2006-03-25 06:47:10 +000025#include "llvm/Intrinsics.h"
Chris Lattner92cb0af2006-01-11 01:15:34 +000026#include "llvm/Support/CFG.h"
Reid Spencer7aa8a452007-01-12 23:22:14 +000027#include "llvm/Type.h"
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000028#include "llvm/CodeGen/MachineConstantPool.h"
Chris Lattnerc961eea2005-11-16 01:54:32 +000029#include "llvm/CodeGen/MachineFunction.h"
Evan Chengaaca22c2006-01-10 20:26:56 +000030#include "llvm/CodeGen/MachineFrameInfo.h"
Chris Lattner92cb0af2006-01-11 01:15:34 +000031#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000032#include "llvm/CodeGen/MachineRegisterInfo.h"
Chris Lattnerc961eea2005-11-16 01:54:32 +000033#include "llvm/CodeGen/SelectionDAGISel.h"
34#include "llvm/Target/TargetMachine.h"
Evan Chengb7a75a52008-09-26 23:41:32 +000035#include "llvm/Target/TargetOptions.h"
Evan Cheng25ab6902006-09-08 06:48:29 +000036#include "llvm/Support/Debug.h"
Torok Edwindac237e2009-07-08 20:53:28 +000037#include "llvm/Support/ErrorHandling.h"
Evan Cheng25ab6902006-09-08 06:48:29 +000038#include "llvm/Support/MathExtras.h"
Torok Edwindac237e2009-07-08 20:53:28 +000039#include "llvm/Support/raw_ostream.h"
Evan Chengcdda25d2008-04-25 08:22:20 +000040#include "llvm/ADT/SmallPtrSet.h"
Chris Lattnerc961eea2005-11-16 01:54:32 +000041#include "llvm/ADT/Statistic.h"
42using namespace llvm;
43
Evan Cheng4d952322009-03-31 01:13:53 +000044#include "llvm/Support/CommandLine.h"
45static cl::opt<bool> AvoidDupAddrCompute("x86-avoid-dup-address", cl::Hidden);
46
Chris Lattner95b2c7d2006-12-19 22:59:26 +000047STATISTIC(NumLoadMoved, "Number of loads moved below TokenFactor");
48
Chris Lattnerc961eea2005-11-16 01:54:32 +000049//===----------------------------------------------------------------------===//
50// Pattern Matcher Implementation
51//===----------------------------------------------------------------------===//
52
53namespace {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000054 /// X86ISelAddressMode - This corresponds to X86AddressMode, but uses
Dan Gohman475871a2008-07-27 21:46:04 +000055 /// SDValue's instead of register numbers for the leaves of the matched
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000056 /// tree.
57 struct X86ISelAddressMode {
58 enum {
59 RegBase,
Chris Lattnerd74ea2b2006-05-24 17:04:05 +000060 FrameIndexBase
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000061 } BaseType;
62
63 struct { // This is really a union, discriminated by BaseType!
Dan Gohman475871a2008-07-27 21:46:04 +000064 SDValue Reg;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000065 int FrameIndex;
66 } Base;
67
68 unsigned Scale;
Dan Gohman475871a2008-07-27 21:46:04 +000069 SDValue IndexReg;
Dan Gohman27cae7b2008-11-11 15:52:29 +000070 int32_t Disp;
Rafael Espindola094fad32009-04-08 21:14:34 +000071 SDValue Segment;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000072 GlobalValue *GV;
Evan Cheng51a9ed92006-02-25 10:09:08 +000073 Constant *CP;
Chris Lattner43f44aa2009-11-01 03:25:03 +000074 BlockAddress *BlockAddr;
Evan Cheng25ab6902006-09-08 06:48:29 +000075 const char *ES;
76 int JT;
Evan Cheng51a9ed92006-02-25 10:09:08 +000077 unsigned Align; // CP alignment.
Chris Lattnerb8afeb92009-06-26 05:51:45 +000078 unsigned char SymbolFlags; // X86II::MO_*
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000079
80 X86ISelAddressMode()
Chris Lattner18c59872009-06-27 04:16:01 +000081 : BaseType(RegBase), Scale(1), IndexReg(), Disp(0),
Chris Lattner43f44aa2009-11-01 03:25:03 +000082 Segment(), GV(0), CP(0), BlockAddr(0), ES(0), JT(-1), Align(0),
Dan Gohman79b765d2009-08-25 17:47:44 +000083 SymbolFlags(X86II::MO_NO_FLAG) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +000084 }
Dan Gohman2d0a1cc2009-02-07 00:43:41 +000085
86 bool hasSymbolicDisplacement() const {
Chris Lattner43f44aa2009-11-01 03:25:03 +000087 return GV != 0 || CP != 0 || ES != 0 || JT != -1 || BlockAddr != 0;
Dan Gohman2d0a1cc2009-02-07 00:43:41 +000088 }
Chris Lattner18c59872009-06-27 04:16:01 +000089
90 bool hasBaseOrIndexReg() const {
91 return IndexReg.getNode() != 0 || Base.Reg.getNode() != 0;
92 }
93
94 /// isRIPRelative - Return true if this addressing mode is already RIP
95 /// relative.
96 bool isRIPRelative() const {
97 if (BaseType != RegBase) return false;
98 if (RegisterSDNode *RegNode =
99 dyn_cast_or_null<RegisterSDNode>(Base.Reg.getNode()))
100 return RegNode->getReg() == X86::RIP;
101 return false;
102 }
103
104 void setBaseReg(SDValue Reg) {
105 BaseType = RegBase;
106 Base.Reg = Reg;
107 }
Dan Gohman2d0a1cc2009-02-07 00:43:41 +0000108
Dale Johannesen50dd1d02008-08-11 23:46:25 +0000109 void dump() {
Benjamin Kramer072a56e2009-08-23 11:52:17 +0000110 errs() << "X86ISelAddressMode " << this << '\n';
111 errs() << "Base.Reg ";
Bill Wendling12321672009-08-07 21:33:25 +0000112 if (Base.Reg.getNode() != 0)
113 Base.Reg.getNode()->dump();
114 else
Benjamin Kramer072a56e2009-08-23 11:52:17 +0000115 errs() << "nul";
116 errs() << " Base.FrameIndex " << Base.FrameIndex << '\n'
117 << " Scale" << Scale << '\n'
118 << "IndexReg ";
Bill Wendling12321672009-08-07 21:33:25 +0000119 if (IndexReg.getNode() != 0)
120 IndexReg.getNode()->dump();
121 else
Benjamin Kramer072a56e2009-08-23 11:52:17 +0000122 errs() << "nul";
123 errs() << " Disp " << Disp << '\n'
124 << "GV ";
Bill Wendling12321672009-08-07 21:33:25 +0000125 if (GV)
126 GV->dump();
127 else
Benjamin Kramer072a56e2009-08-23 11:52:17 +0000128 errs() << "nul";
129 errs() << " CP ";
Bill Wendling12321672009-08-07 21:33:25 +0000130 if (CP)
131 CP->dump();
132 else
Benjamin Kramer072a56e2009-08-23 11:52:17 +0000133 errs() << "nul";
134 errs() << '\n'
135 << "ES ";
Bill Wendling12321672009-08-07 21:33:25 +0000136 if (ES)
Benjamin Kramer072a56e2009-08-23 11:52:17 +0000137 errs() << ES;
Bill Wendling12321672009-08-07 21:33:25 +0000138 else
Benjamin Kramer072a56e2009-08-23 11:52:17 +0000139 errs() << "nul";
140 errs() << " JT" << JT << " Align" << Align << '\n';
Dale Johannesen50dd1d02008-08-11 23:46:25 +0000141 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000142 };
143}
144
145namespace {
Chris Lattnerc961eea2005-11-16 01:54:32 +0000146 //===--------------------------------------------------------------------===//
147 /// ISel - X86 specific code to select X86 machine instructions for
148 /// SelectionDAG operations.
149 ///
Nick Lewycky6726b6d2009-10-25 06:33:48 +0000150 class X86DAGToDAGISel : public SelectionDAGISel {
Chris Lattnerc961eea2005-11-16 01:54:32 +0000151 /// X86Lowering - This object fully describes how to lower LLVM code to an
152 /// X86-specific SelectionDAG.
Dan Gohmanda8ac5f2008-10-03 16:55:19 +0000153 X86TargetLowering &X86Lowering;
Chris Lattnerc961eea2005-11-16 01:54:32 +0000154
155 /// Subtarget - Keep a pointer to the X86Subtarget around so that we can
156 /// make the right decision when generating code for different targets.
157 const X86Subtarget *Subtarget;
Evan Cheng7ccced62006-02-18 00:15:05 +0000158
Evan Chengb7a75a52008-09-26 23:41:32 +0000159 /// OptForSize - If true, selector should try to optimize for code size
160 /// instead of performance.
161 bool OptForSize;
162
Chris Lattnerc961eea2005-11-16 01:54:32 +0000163 public:
Bill Wendling98a366d2009-04-29 23:29:43 +0000164 explicit X86DAGToDAGISel(X86TargetMachine &tm, CodeGenOpt::Level OptLevel)
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000165 : SelectionDAGISel(tm, OptLevel),
Dan Gohmanc5534622009-06-03 20:20:00 +0000166 X86Lowering(*tm.getTargetLowering()),
167 Subtarget(&tm.getSubtarget<X86Subtarget>()),
Devang Patel4ae641f2008-10-01 23:18:38 +0000168 OptForSize(false) {}
Chris Lattnerc961eea2005-11-16 01:54:32 +0000169
170 virtual const char *getPassName() const {
171 return "X86 DAG->DAG Instruction Selection";
172 }
173
Evan Chengdb8d56b2008-06-30 20:45:06 +0000174 /// InstructionSelect - This callback is invoked by
Chris Lattnerc961eea2005-11-16 01:54:32 +0000175 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Dan Gohmanf350b272008-08-23 02:25:05 +0000176 virtual void InstructionSelect();
Evan Chengdb8d56b2008-06-30 20:45:06 +0000177
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000178 virtual void EmitFunctionEntryCode(Function &Fn, MachineFunction &MF);
179
Evan Cheng884c70c2008-11-27 00:49:46 +0000180 virtual
181 bool IsLegalAndProfitableToFold(SDNode *N, SDNode *U, SDNode *Root) const;
Evan Chenga8df1b42006-07-27 16:44:36 +0000182
Chris Lattnerc961eea2005-11-16 01:54:32 +0000183// Include the pieces autogenerated from the target description.
184#include "X86GenDAGISel.inc"
185
186 private:
Dan Gohman475871a2008-07-27 21:46:04 +0000187 SDNode *Select(SDValue N);
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000188 SDNode *SelectAtomic64(SDNode *Node, unsigned Opc);
Owen Andersone50ed302009-08-10 22:56:29 +0000189 SDNode *SelectAtomicLoadAdd(SDNode *Node, EVT NVT);
Chris Lattnerc961eea2005-11-16 01:54:32 +0000190
Rafael Espindola094fad32009-04-08 21:14:34 +0000191 bool MatchSegmentBaseAddress(SDValue N, X86ISelAddressMode &AM);
192 bool MatchLoad(SDValue N, X86ISelAddressMode &AM);
Rafael Espindola49a168d2009-04-12 21:55:03 +0000193 bool MatchWrapper(SDValue N, X86ISelAddressMode &AM);
Dan Gohman41d0b9d2009-07-22 23:26:55 +0000194 bool MatchAddress(SDValue N, X86ISelAddressMode &AM);
195 bool MatchAddressRecursively(SDValue N, X86ISelAddressMode &AM,
196 unsigned Depth);
Rafael Espindola523249f2009-03-31 16:16:57 +0000197 bool MatchAddressBase(SDValue N, X86ISelAddressMode &AM);
Dan Gohman475871a2008-07-27 21:46:04 +0000198 bool SelectAddr(SDValue Op, SDValue N, SDValue &Base,
Rafael Espindola094fad32009-04-08 21:14:34 +0000199 SDValue &Scale, SDValue &Index, SDValue &Disp,
200 SDValue &Segment);
Dan Gohman475871a2008-07-27 21:46:04 +0000201 bool SelectLEAAddr(SDValue Op, SDValue N, SDValue &Base,
202 SDValue &Scale, SDValue &Index, SDValue &Disp);
Chris Lattner5c0b16d2009-06-20 20:38:48 +0000203 bool SelectTLSADDRAddr(SDValue Op, SDValue N, SDValue &Base,
204 SDValue &Scale, SDValue &Index, SDValue &Disp);
Dan Gohman475871a2008-07-27 21:46:04 +0000205 bool SelectScalarSSELoad(SDValue Op, SDValue Pred,
206 SDValue N, SDValue &Base, SDValue &Scale,
207 SDValue &Index, SDValue &Disp,
Rafael Espindola094fad32009-04-08 21:14:34 +0000208 SDValue &Segment,
Dan Gohman475871a2008-07-27 21:46:04 +0000209 SDValue &InChain, SDValue &OutChain);
210 bool TryFoldLoad(SDValue P, SDValue N,
211 SDValue &Base, SDValue &Scale,
Rafael Espindola094fad32009-04-08 21:14:34 +0000212 SDValue &Index, SDValue &Disp,
213 SDValue &Segment);
Dan Gohmanf350b272008-08-23 02:25:05 +0000214 void PreprocessForRMW();
215 void PreprocessForFPConvert();
Evan Cheng2ef88a02006-08-07 22:28:20 +0000216
Chris Lattnerc0bad572006-06-08 18:03:49 +0000217 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
218 /// inline asm expressions.
Dan Gohman475871a2008-07-27 21:46:04 +0000219 virtual bool SelectInlineAsmMemoryOperand(const SDValue &Op,
Chris Lattnerc0bad572006-06-08 18:03:49 +0000220 char ConstraintCode,
Dan Gohmanf350b272008-08-23 02:25:05 +0000221 std::vector<SDValue> &OutOps);
Chris Lattnerc0bad572006-06-08 18:03:49 +0000222
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000223 void EmitSpecialCodeForMain(MachineBasicBlock *BB, MachineFrameInfo *MFI);
224
Dan Gohman475871a2008-07-27 21:46:04 +0000225 inline void getAddressOperands(X86ISelAddressMode &AM, SDValue &Base,
226 SDValue &Scale, SDValue &Index,
Rafael Espindola094fad32009-04-08 21:14:34 +0000227 SDValue &Disp, SDValue &Segment) {
Evan Chenge5280532005-12-12 21:49:40 +0000228 Base = (AM.BaseType == X86ISelAddressMode::FrameIndexBase) ?
Evan Cheng25ab6902006-09-08 06:48:29 +0000229 CurDAG->getTargetFrameIndex(AM.Base.FrameIndex, TLI.getPointerTy()) :
230 AM.Base.Reg;
Evan Chengbdce7b42005-12-17 09:13:43 +0000231 Scale = getI8Imm(AM.Scale);
Evan Chenge5280532005-12-12 21:49:40 +0000232 Index = AM.IndexReg;
Evan Cheng25ab6902006-09-08 06:48:29 +0000233 // These are 32-bit even in 64-bit mode since RIP relative offset
234 // is 32-bit.
235 if (AM.GV)
Owen Anderson825b72b2009-08-11 20:47:22 +0000236 Disp = CurDAG->getTargetGlobalAddress(AM.GV, MVT::i32, AM.Disp,
Chris Lattnerb8afeb92009-06-26 05:51:45 +0000237 AM.SymbolFlags);
Evan Cheng25ab6902006-09-08 06:48:29 +0000238 else if (AM.CP)
Owen Anderson825b72b2009-08-11 20:47:22 +0000239 Disp = CurDAG->getTargetConstantPool(AM.CP, MVT::i32,
Chris Lattnerb8afeb92009-06-26 05:51:45 +0000240 AM.Align, AM.Disp, AM.SymbolFlags);
Evan Cheng25ab6902006-09-08 06:48:29 +0000241 else if (AM.ES)
Owen Anderson825b72b2009-08-11 20:47:22 +0000242 Disp = CurDAG->getTargetExternalSymbol(AM.ES, MVT::i32, AM.SymbolFlags);
Evan Cheng25ab6902006-09-08 06:48:29 +0000243 else if (AM.JT != -1)
Owen Anderson825b72b2009-08-11 20:47:22 +0000244 Disp = CurDAG->getTargetJumpTable(AM.JT, MVT::i32, AM.SymbolFlags);
Chris Lattner43f44aa2009-11-01 03:25:03 +0000245 else if (AM.BlockAddr)
246 Disp = CurDAG->getBlockAddress(AM.BlockAddr, DebugLoc()/*MVT::i32*/,
247 true /*AM.SymbolFlags*/);
Evan Cheng25ab6902006-09-08 06:48:29 +0000248 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000249 Disp = CurDAG->getTargetConstant(AM.Disp, MVT::i32);
Rafael Espindola094fad32009-04-08 21:14:34 +0000250
251 if (AM.Segment.getNode())
252 Segment = AM.Segment;
253 else
Owen Anderson825b72b2009-08-11 20:47:22 +0000254 Segment = CurDAG->getRegister(0, MVT::i32);
Evan Chenge5280532005-12-12 21:49:40 +0000255 }
256
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000257 /// getI8Imm - Return a target constant with the specified value, of type
258 /// i8.
Dan Gohman475871a2008-07-27 21:46:04 +0000259 inline SDValue getI8Imm(unsigned Imm) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000260 return CurDAG->getTargetConstant(Imm, MVT::i8);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000261 }
262
Chris Lattnerc961eea2005-11-16 01:54:32 +0000263 /// getI16Imm - Return a target constant with the specified value, of type
264 /// i16.
Dan Gohman475871a2008-07-27 21:46:04 +0000265 inline SDValue getI16Imm(unsigned Imm) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000266 return CurDAG->getTargetConstant(Imm, MVT::i16);
Chris Lattnerc961eea2005-11-16 01:54:32 +0000267 }
268
269 /// getI32Imm - Return a target constant with the specified value, of type
270 /// i32.
Dan Gohman475871a2008-07-27 21:46:04 +0000271 inline SDValue getI32Imm(unsigned Imm) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000272 return CurDAG->getTargetConstant(Imm, MVT::i32);
Chris Lattnerc961eea2005-11-16 01:54:32 +0000273 }
Evan Chengf597dc72006-02-10 22:24:32 +0000274
Dan Gohman8b746962008-09-23 18:22:58 +0000275 /// getGlobalBaseReg - Return an SDNode that returns the value of
276 /// the global base register. Output instructions required to
277 /// initialize the global base register, if necessary.
278 ///
Evan Cheng9ade2182006-08-26 05:34:46 +0000279 SDNode *getGlobalBaseReg();
Evan Cheng7ccced62006-02-18 00:15:05 +0000280
Dan Gohmanc5534622009-06-03 20:20:00 +0000281 /// getTargetMachine - Return a reference to the TargetMachine, casted
282 /// to the target-specific type.
283 const X86TargetMachine &getTargetMachine() {
284 return static_cast<const X86TargetMachine &>(TM);
285 }
286
287 /// getInstrInfo - Return a reference to the TargetInstrInfo, casted
288 /// to the target-specific type.
289 const X86InstrInfo *getInstrInfo() {
290 return getTargetMachine().getInstrInfo();
291 }
292
Evan Cheng23addc02006-02-10 22:46:26 +0000293#ifndef NDEBUG
294 unsigned Indent;
295#endif
Chris Lattnerc961eea2005-11-16 01:54:32 +0000296 };
297}
298
Evan Chengf4b4c412006-08-08 00:31:00 +0000299
Evan Cheng884c70c2008-11-27 00:49:46 +0000300bool X86DAGToDAGISel::IsLegalAndProfitableToFold(SDNode *N, SDNode *U,
301 SDNode *Root) const {
Bill Wendling98a366d2009-04-29 23:29:43 +0000302 if (OptLevel == CodeGenOpt::None) return false;
Evan Cheng27e1fe92006-10-14 08:33:25 +0000303
Evan Cheng884c70c2008-11-27 00:49:46 +0000304 if (U == Root)
305 switch (U->getOpcode()) {
306 default: break;
307 case ISD::ADD:
308 case ISD::ADDC:
309 case ISD::ADDE:
310 case ISD::AND:
311 case ISD::OR:
312 case ISD::XOR: {
Rafael Espindoladbcfb302009-04-10 10:09:34 +0000313 SDValue Op1 = U->getOperand(1);
314
Evan Cheng884c70c2008-11-27 00:49:46 +0000315 // If the other operand is a 8-bit immediate we should fold the immediate
316 // instead. This reduces code size.
317 // e.g.
318 // movl 4(%esp), %eax
319 // addl $4, %eax
320 // vs.
321 // movl $4, %eax
322 // addl 4(%esp), %eax
323 // The former is 2 bytes shorter. In case where the increment is 1, then
324 // the saving can be 4 bytes (by using incl %eax).
Rafael Espindoladbcfb302009-04-10 10:09:34 +0000325 if (ConstantSDNode *Imm = dyn_cast<ConstantSDNode>(Op1))
Dan Gohman9a49d312009-03-14 02:07:16 +0000326 if (Imm->getAPIntValue().isSignedIntN(8))
327 return false;
Rafael Espindoladbcfb302009-04-10 10:09:34 +0000328
329 // If the other operand is a TLS address, we should fold it instead.
330 // This produces
331 // movl %gs:0, %eax
332 // leal i@NTPOFF(%eax), %eax
333 // instead of
334 // movl $i@NTPOFF, %eax
335 // addl %gs:0, %eax
336 // if the block also has an access to a second TLS address this will save
337 // a load.
338 // FIXME: This is probably also true for non TLS addresses.
339 if (Op1.getOpcode() == X86ISD::Wrapper) {
340 SDValue Val = Op1.getOperand(0);
341 if (Val.getOpcode() == ISD::TargetGlobalTLSAddress)
342 return false;
343 }
Evan Cheng884c70c2008-11-27 00:49:46 +0000344 }
345 }
346
Anton Korobeynikovc1c6ef82009-05-08 18:51:58 +0000347 // Proceed to 'generic' cycle finder code
348 return SelectionDAGISel::IsLegalAndProfitableToFold(N, U, Root);
Evan Chenga8df1b42006-07-27 16:44:36 +0000349}
350
Evan Cheng70e674e2006-08-28 20:10:17 +0000351/// MoveBelowTokenFactor - Replace TokenFactor operand with load's chain operand
352/// and move load below the TokenFactor. Replace store's chain operand with
353/// load's chain result.
Dan Gohmanf350b272008-08-23 02:25:05 +0000354static void MoveBelowTokenFactor(SelectionDAG *CurDAG, SDValue Load,
Dan Gohman475871a2008-07-27 21:46:04 +0000355 SDValue Store, SDValue TF) {
Evan Chengab6c3bb2008-08-25 21:27:18 +0000356 SmallVector<SDValue, 4> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +0000357 for (unsigned i = 0, e = TF.getNode()->getNumOperands(); i != e; ++i)
358 if (Load.getNode() == TF.getOperand(i).getNode())
Evan Chengab6c3bb2008-08-25 21:27:18 +0000359 Ops.push_back(Load.getOperand(0));
Evan Cheng70e674e2006-08-28 20:10:17 +0000360 else
Evan Chengab6c3bb2008-08-25 21:27:18 +0000361 Ops.push_back(TF.getOperand(i));
Dan Gohmanaae317a2009-08-06 09:22:57 +0000362 SDValue NewTF = CurDAG->UpdateNodeOperands(TF, &Ops[0], Ops.size());
363 SDValue NewLoad = CurDAG->UpdateNodeOperands(Load, NewTF,
364 Load.getOperand(1),
365 Load.getOperand(2));
366 CurDAG->UpdateNodeOperands(Store, NewLoad.getValue(1), Store.getOperand(1),
Dan Gohmanf350b272008-08-23 02:25:05 +0000367 Store.getOperand(2), Store.getOperand(3));
Evan Cheng70e674e2006-08-28 20:10:17 +0000368}
369
Nate Begeman206a3572009-09-16 03:20:46 +0000370/// isRMWLoad - Return true if N is a load that's part of RMW sub-DAG. The
371/// chain produced by the load must only be used by the store's chain operand,
372/// otherwise this may produce a cycle in the DAG.
Evan Chengcd0baf22008-05-23 21:23:16 +0000373///
Dan Gohman475871a2008-07-27 21:46:04 +0000374static bool isRMWLoad(SDValue N, SDValue Chain, SDValue Address,
375 SDValue &Load) {
Evan Chengcd0baf22008-05-23 21:23:16 +0000376 if (N.getOpcode() == ISD::BIT_CONVERT)
377 N = N.getOperand(0);
378
379 LoadSDNode *LD = dyn_cast<LoadSDNode>(N);
380 if (!LD || LD->isVolatile())
381 return false;
382 if (LD->getAddressingMode() != ISD::UNINDEXED)
383 return false;
384
385 ISD::LoadExtType ExtType = LD->getExtensionType();
386 if (ExtType != ISD::NON_EXTLOAD && ExtType != ISD::EXTLOAD)
387 return false;
388
389 if (N.hasOneUse() &&
Nate Begeman206a3572009-09-16 03:20:46 +0000390 LD->hasNUsesOfValue(1, 1) &&
Evan Chengcd0baf22008-05-23 21:23:16 +0000391 N.getOperand(1) == Address &&
Nate Begeman206a3572009-09-16 03:20:46 +0000392 LD->isOperandOf(Chain.getNode())) {
Evan Chengcd0baf22008-05-23 21:23:16 +0000393 Load = N;
394 return true;
395 }
396 return false;
397}
398
Evan Chengab6c3bb2008-08-25 21:27:18 +0000399/// MoveBelowCallSeqStart - Replace CALLSEQ_START operand with load's chain
400/// operand and move load below the call's chain operand.
401static void MoveBelowCallSeqStart(SelectionDAG *CurDAG, SDValue Load,
Evan Cheng5b2e5892009-01-26 18:43:34 +0000402 SDValue Call, SDValue CallSeqStart) {
Evan Chengab6c3bb2008-08-25 21:27:18 +0000403 SmallVector<SDValue, 8> Ops;
Evan Cheng5b2e5892009-01-26 18:43:34 +0000404 SDValue Chain = CallSeqStart.getOperand(0);
405 if (Chain.getNode() == Load.getNode())
406 Ops.push_back(Load.getOperand(0));
407 else {
408 assert(Chain.getOpcode() == ISD::TokenFactor &&
409 "Unexpected CallSeqStart chain operand");
410 for (unsigned i = 0, e = Chain.getNumOperands(); i != e; ++i)
411 if (Chain.getOperand(i).getNode() == Load.getNode())
412 Ops.push_back(Load.getOperand(0));
413 else
414 Ops.push_back(Chain.getOperand(i));
415 SDValue NewChain =
Dale Johannesened2eee62009-02-06 01:31:28 +0000416 CurDAG->getNode(ISD::TokenFactor, Load.getDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +0000417 MVT::Other, &Ops[0], Ops.size());
Evan Cheng5b2e5892009-01-26 18:43:34 +0000418 Ops.clear();
419 Ops.push_back(NewChain);
420 }
421 for (unsigned i = 1, e = CallSeqStart.getNumOperands(); i != e; ++i)
422 Ops.push_back(CallSeqStart.getOperand(i));
423 CurDAG->UpdateNodeOperands(CallSeqStart, &Ops[0], Ops.size());
Evan Chengab6c3bb2008-08-25 21:27:18 +0000424 CurDAG->UpdateNodeOperands(Load, Call.getOperand(0),
425 Load.getOperand(1), Load.getOperand(2));
426 Ops.clear();
Gabor Greifba36cb52008-08-28 21:40:38 +0000427 Ops.push_back(SDValue(Load.getNode(), 1));
428 for (unsigned i = 1, e = Call.getNode()->getNumOperands(); i != e; ++i)
Evan Chengab6c3bb2008-08-25 21:27:18 +0000429 Ops.push_back(Call.getOperand(i));
430 CurDAG->UpdateNodeOperands(Call, &Ops[0], Ops.size());
431}
432
433/// isCalleeLoad - Return true if call address is a load and it can be
434/// moved below CALLSEQ_START and the chains leading up to the call.
435/// Return the CALLSEQ_START by reference as a second output.
436static bool isCalleeLoad(SDValue Callee, SDValue &Chain) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000437 if (Callee.getNode() == Chain.getNode() || !Callee.hasOneUse())
Evan Chengab6c3bb2008-08-25 21:27:18 +0000438 return false;
Gabor Greifba36cb52008-08-28 21:40:38 +0000439 LoadSDNode *LD = dyn_cast<LoadSDNode>(Callee.getNode());
Evan Chengab6c3bb2008-08-25 21:27:18 +0000440 if (!LD ||
441 LD->isVolatile() ||
442 LD->getAddressingMode() != ISD::UNINDEXED ||
443 LD->getExtensionType() != ISD::NON_EXTLOAD)
444 return false;
445
446 // Now let's find the callseq_start.
447 while (Chain.getOpcode() != ISD::CALLSEQ_START) {
448 if (!Chain.hasOneUse())
449 return false;
450 Chain = Chain.getOperand(0);
451 }
Evan Cheng5b2e5892009-01-26 18:43:34 +0000452
453 if (Chain.getOperand(0).getNode() == Callee.getNode())
454 return true;
455 if (Chain.getOperand(0).getOpcode() == ISD::TokenFactor &&
Dan Gohman1e038a82009-09-15 01:22:01 +0000456 Callee.getValue(1).isOperandOf(Chain.getOperand(0).getNode()) &&
457 Callee.getValue(1).hasOneUse())
Evan Cheng5b2e5892009-01-26 18:43:34 +0000458 return true;
459 return false;
Evan Chengab6c3bb2008-08-25 21:27:18 +0000460}
461
462
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000463/// PreprocessForRMW - Preprocess the DAG to make instruction selection better.
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000464/// This is only run if not in -O0 mode.
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000465/// This allows the instruction selector to pick more read-modify-write
466/// instructions. This is a common case:
Evan Cheng70e674e2006-08-28 20:10:17 +0000467///
468/// [Load chain]
469/// ^
470/// |
471/// [Load]
472/// ^ ^
473/// | |
474/// / \-
475/// / |
476/// [TokenFactor] [Op]
477/// ^ ^
478/// | |
479/// \ /
480/// \ /
481/// [Store]
482///
483/// The fact the store's chain operand != load's chain will prevent the
484/// (store (op (load))) instruction from being selected. We can transform it to:
485///
486/// [Load chain]
487/// ^
488/// |
489/// [TokenFactor]
490/// ^
491/// |
492/// [Load]
493/// ^ ^
494/// | |
495/// | \-
496/// | |
497/// | [Op]
498/// | ^
499/// | |
500/// \ /
501/// \ /
502/// [Store]
Dan Gohmanf350b272008-08-23 02:25:05 +0000503void X86DAGToDAGISel::PreprocessForRMW() {
504 for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
505 E = CurDAG->allnodes_end(); I != E; ++I) {
Evan Chengab6c3bb2008-08-25 21:27:18 +0000506 if (I->getOpcode() == X86ISD::CALL) {
507 /// Also try moving call address load from outside callseq_start to just
508 /// before the call to allow it to be folded.
509 ///
510 /// [Load chain]
511 /// ^
512 /// |
513 /// [Load]
514 /// ^ ^
515 /// | |
516 /// / \--
517 /// / |
518 ///[CALLSEQ_START] |
519 /// ^ |
520 /// | |
521 /// [LOAD/C2Reg] |
522 /// | |
523 /// \ /
524 /// \ /
525 /// [CALL]
526 SDValue Chain = I->getOperand(0);
527 SDValue Load = I->getOperand(1);
528 if (!isCalleeLoad(Load, Chain))
529 continue;
530 MoveBelowCallSeqStart(CurDAG, Load, SDValue(I, 0), Chain);
531 ++NumLoadMoved;
532 continue;
533 }
534
Evan Cheng8b2794a2006-10-13 21:14:26 +0000535 if (!ISD::isNON_TRUNCStore(I))
Evan Cheng70e674e2006-08-28 20:10:17 +0000536 continue;
Dan Gohman475871a2008-07-27 21:46:04 +0000537 SDValue Chain = I->getOperand(0);
Evan Chengab6c3bb2008-08-25 21:27:18 +0000538
Gabor Greifba36cb52008-08-28 21:40:38 +0000539 if (Chain.getNode()->getOpcode() != ISD::TokenFactor)
Evan Cheng70e674e2006-08-28 20:10:17 +0000540 continue;
541
Dan Gohman475871a2008-07-27 21:46:04 +0000542 SDValue N1 = I->getOperand(1);
543 SDValue N2 = I->getOperand(2);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000544 if ((N1.getValueType().isFloatingPoint() &&
545 !N1.getValueType().isVector()) ||
Evan Cheng780413d2006-08-29 18:37:37 +0000546 !N1.hasOneUse())
Evan Cheng70e674e2006-08-28 20:10:17 +0000547 continue;
548
549 bool RModW = false;
Dan Gohman475871a2008-07-27 21:46:04 +0000550 SDValue Load;
Gabor Greifba36cb52008-08-28 21:40:38 +0000551 unsigned Opcode = N1.getNode()->getOpcode();
Evan Cheng70e674e2006-08-28 20:10:17 +0000552 switch (Opcode) {
Evan Chengab6c3bb2008-08-25 21:27:18 +0000553 case ISD::ADD:
554 case ISD::MUL:
555 case ISD::AND:
556 case ISD::OR:
557 case ISD::XOR:
558 case ISD::ADDC:
559 case ISD::ADDE:
560 case ISD::VECTOR_SHUFFLE: {
561 SDValue N10 = N1.getOperand(0);
562 SDValue N11 = N1.getOperand(1);
563 RModW = isRMWLoad(N10, Chain, N2, Load);
564 if (!RModW)
565 RModW = isRMWLoad(N11, Chain, N2, Load);
566 break;
567 }
568 case ISD::SUB:
569 case ISD::SHL:
570 case ISD::SRA:
571 case ISD::SRL:
572 case ISD::ROTL:
573 case ISD::ROTR:
574 case ISD::SUBC:
575 case ISD::SUBE:
576 case X86ISD::SHLD:
577 case X86ISD::SHRD: {
578 SDValue N10 = N1.getOperand(0);
579 RModW = isRMWLoad(N10, Chain, N2, Load);
580 break;
581 }
Evan Cheng70e674e2006-08-28 20:10:17 +0000582 }
583
Evan Cheng82a35b32006-08-29 06:44:17 +0000584 if (RModW) {
Dan Gohmanf350b272008-08-23 02:25:05 +0000585 MoveBelowTokenFactor(CurDAG, Load, SDValue(I, 0), Chain);
Evan Cheng82a35b32006-08-29 06:44:17 +0000586 ++NumLoadMoved;
587 }
Evan Cheng70e674e2006-08-28 20:10:17 +0000588 }
589}
590
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000591
592/// PreprocessForFPConvert - Walk over the dag lowering fpround and fpextend
593/// nodes that target the FP stack to be store and load to the stack. This is a
594/// gross hack. We would like to simply mark these as being illegal, but when
595/// we do that, legalize produces these when it expands calls, then expands
596/// these in the same legalize pass. We would like dag combine to be able to
597/// hack on these between the call expansion and the node legalization. As such
598/// this pass basically does "really late" legalization of these inline with the
599/// X86 isel pass.
Dan Gohmanf350b272008-08-23 02:25:05 +0000600void X86DAGToDAGISel::PreprocessForFPConvert() {
601 for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
602 E = CurDAG->allnodes_end(); I != E; ) {
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000603 SDNode *N = I++; // Preincrement iterator to avoid invalidation issues.
604 if (N->getOpcode() != ISD::FP_ROUND && N->getOpcode() != ISD::FP_EXTEND)
605 continue;
606
607 // If the source and destination are SSE registers, then this is a legal
608 // conversion that should not be lowered.
Owen Andersone50ed302009-08-10 22:56:29 +0000609 EVT SrcVT = N->getOperand(0).getValueType();
610 EVT DstVT = N->getValueType(0);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000611 bool SrcIsSSE = X86Lowering.isScalarFPTypeInSSEReg(SrcVT);
612 bool DstIsSSE = X86Lowering.isScalarFPTypeInSSEReg(DstVT);
613 if (SrcIsSSE && DstIsSSE)
614 continue;
615
Chris Lattner6fa2f9c2008-03-09 07:05:32 +0000616 if (!SrcIsSSE && !DstIsSSE) {
617 // If this is an FPStack extension, it is a noop.
618 if (N->getOpcode() == ISD::FP_EXTEND)
619 continue;
620 // If this is a value-preserving FPStack truncation, it is a noop.
621 if (N->getConstantOperandVal(1))
622 continue;
623 }
624
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000625 // Here we could have an FP stack truncation or an FPStack <-> SSE convert.
626 // FPStack has extload and truncstore. SSE can fold direct loads into other
627 // operations. Based on this, decide what we want to do.
Owen Andersone50ed302009-08-10 22:56:29 +0000628 EVT MemVT;
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000629 if (N->getOpcode() == ISD::FP_ROUND)
630 MemVT = DstVT; // FP_ROUND must use DstVT, we can't do a 'trunc load'.
631 else
632 MemVT = SrcIsSSE ? SrcVT : DstVT;
633
Dan Gohmanf350b272008-08-23 02:25:05 +0000634 SDValue MemTmp = CurDAG->CreateStackTemporary(MemVT);
Dale Johannesend8392542009-02-03 21:48:12 +0000635 DebugLoc dl = N->getDebugLoc();
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000636
637 // FIXME: optimize the case where the src/dest is a load or store?
Dale Johannesend8392542009-02-03 21:48:12 +0000638 SDValue Store = CurDAG->getTruncStore(CurDAG->getEntryNode(), dl,
Dan Gohmanf350b272008-08-23 02:25:05 +0000639 N->getOperand(0),
640 MemTmp, NULL, 0, MemVT);
Dale Johannesend8392542009-02-03 21:48:12 +0000641 SDValue Result = CurDAG->getExtLoad(ISD::EXTLOAD, dl, DstVT, Store, MemTmp,
Dan Gohmanf350b272008-08-23 02:25:05 +0000642 NULL, 0, MemVT);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000643
644 // We're about to replace all uses of the FP_ROUND/FP_EXTEND with the
645 // extload we created. This will cause general havok on the dag because
646 // anything below the conversion could be folded into other existing nodes.
647 // To avoid invalidating 'I', back it up to the convert node.
648 --I;
Dan Gohmanf350b272008-08-23 02:25:05 +0000649 CurDAG->ReplaceAllUsesOfValueWith(SDValue(N, 0), Result);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000650
651 // Now that we did that, the node is dead. Increment the iterator to the
652 // next node to process, then delete N.
653 ++I;
Dan Gohmanf350b272008-08-23 02:25:05 +0000654 CurDAG->DeleteNode(N);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000655 }
656}
657
Chris Lattnerc961eea2005-11-16 01:54:32 +0000658/// InstructionSelectBasicBlock - This callback is invoked by SelectionDAGISel
659/// when it has created a SelectionDAG for us to codegen.
Dan Gohmanf350b272008-08-23 02:25:05 +0000660void X86DAGToDAGISel::InstructionSelect() {
Dan Gohman7571eb52009-08-01 03:42:59 +0000661 const Function *F = MF->getFunction();
Devang Patele76225a2008-10-06 18:03:39 +0000662 OptForSize = F->hasFnAttr(Attribute::OptimizeForSize);
Chris Lattnerc961eea2005-11-16 01:54:32 +0000663
Bill Wendling98a366d2009-04-29 23:29:43 +0000664 if (OptLevel != CodeGenOpt::None)
Dan Gohmanf350b272008-08-23 02:25:05 +0000665 PreprocessForRMW();
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000666
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000667 // FIXME: This should only happen when not compiled with -O0.
Dan Gohmanf350b272008-08-23 02:25:05 +0000668 PreprocessForFPConvert();
Evan Cheng70e674e2006-08-28 20:10:17 +0000669
Chris Lattnerc961eea2005-11-16 01:54:32 +0000670 // Codegen the basic block.
Evan Chengf597dc72006-02-10 22:24:32 +0000671#ifndef NDEBUG
Bill Wendling0ea8bf32009-08-03 00:11:34 +0000672 DEBUG(errs() << "===== Instruction selection begins:\n");
Evan Cheng23addc02006-02-10 22:46:26 +0000673 Indent = 0;
Evan Chengf597dc72006-02-10 22:24:32 +0000674#endif
David Greene8ad4c002008-10-27 21:56:29 +0000675 SelectRoot(*CurDAG);
Evan Chengf597dc72006-02-10 22:24:32 +0000676#ifndef NDEBUG
Bill Wendling0ea8bf32009-08-03 00:11:34 +0000677 DEBUG(errs() << "===== Instruction selection ends:\n");
Evan Chengf597dc72006-02-10 22:24:32 +0000678#endif
Evan Cheng63ce5682006-07-28 00:10:59 +0000679
Dan Gohmanf350b272008-08-23 02:25:05 +0000680 CurDAG->RemoveDeadNodes();
Evan Chengdb8d56b2008-06-30 20:45:06 +0000681}
Chris Lattnerc961eea2005-11-16 01:54:32 +0000682
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000683/// EmitSpecialCodeForMain - Emit any code that needs to be executed only in
684/// the main function.
685void X86DAGToDAGISel::EmitSpecialCodeForMain(MachineBasicBlock *BB,
686 MachineFrameInfo *MFI) {
687 const TargetInstrInfo *TII = TM.getInstrInfo();
688 if (Subtarget->isTargetCygMing())
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000689 BuildMI(BB, DebugLoc::getUnknownLoc(),
690 TII->get(X86::CALLpcrel32)).addExternalSymbol("__main");
Anton Korobeynikov2fe12592007-09-25 21:52:30 +0000691}
692
693void X86DAGToDAGISel::EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) {
694 // If this is main, emit special code for main.
695 MachineBasicBlock *BB = MF.begin();
696 if (Fn.hasExternalLinkage() && Fn.getName() == "main")
697 EmitSpecialCodeForMain(BB, MF.getFrameInfo());
698}
699
Rafael Espindola094fad32009-04-08 21:14:34 +0000700
701bool X86DAGToDAGISel::MatchSegmentBaseAddress(SDValue N,
702 X86ISelAddressMode &AM) {
703 assert(N.getOpcode() == X86ISD::SegmentBaseAddress);
704 SDValue Segment = N.getOperand(0);
705
706 if (AM.Segment.getNode() == 0) {
707 AM.Segment = Segment;
708 return false;
709 }
710
711 return true;
712}
713
714bool X86DAGToDAGISel::MatchLoad(SDValue N, X86ISelAddressMode &AM) {
715 // This optimization is valid because the GNU TLS model defines that
716 // gs:0 (or fs:0 on X86-64) contains its own address.
717 // For more information see http://people.redhat.com/drepper/tls.pdf
718
719 SDValue Address = N.getOperand(1);
720 if (Address.getOpcode() == X86ISD::SegmentBaseAddress &&
721 !MatchSegmentBaseAddress (Address, AM))
722 return false;
723
724 return true;
725}
726
Chris Lattner18c59872009-06-27 04:16:01 +0000727/// MatchWrapper - Try to match X86ISD::Wrapper and X86ISD::WrapperRIP nodes
728/// into an addressing mode. These wrap things that will resolve down into a
729/// symbol reference. If no match is possible, this returns true, otherwise it
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000730/// returns false.
Rafael Espindola49a168d2009-04-12 21:55:03 +0000731bool X86DAGToDAGISel::MatchWrapper(SDValue N, X86ISelAddressMode &AM) {
Chris Lattner18c59872009-06-27 04:16:01 +0000732 // If the addressing mode already has a symbol as the displacement, we can
733 // never match another symbol.
Rafael Espindola49a168d2009-04-12 21:55:03 +0000734 if (AM.hasSymbolicDisplacement())
735 return true;
Rafael Espindola49a168d2009-04-12 21:55:03 +0000736
737 SDValue N0 = N.getOperand(0);
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000738 CodeModel::Model M = TM.getCodeModel();
739
Chris Lattner18c59872009-06-27 04:16:01 +0000740 // Handle X86-64 rip-relative addresses. We check this before checking direct
741 // folding because RIP is preferable to non-RIP accesses.
742 if (Subtarget->is64Bit() &&
743 // Under X86-64 non-small code model, GV (and friends) are 64-bits, so
744 // they cannot be folded into immediate fields.
745 // FIXME: This can be improved for kernel and other models?
Anton Korobeynikov25f1aa02009-08-21 15:41:56 +0000746 (M == CodeModel::Small || M == CodeModel::Kernel) &&
Chris Lattner18c59872009-06-27 04:16:01 +0000747 // Base and index reg must be 0 in order to use %rip as base and lowering
748 // must allow RIP.
749 !AM.hasBaseOrIndexReg() && N.getOpcode() == X86ISD::WrapperRIP) {
Chris Lattner18c59872009-06-27 04:16:01 +0000750 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(N0)) {
751 int64_t Offset = AM.Disp + G->getOffset();
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000752 if (!X86::isOffsetSuitableForCodeModel(Offset, M)) return true;
Chris Lattner18c59872009-06-27 04:16:01 +0000753 AM.GV = G->getGlobal();
754 AM.Disp = Offset;
Chris Lattnerb8afeb92009-06-26 05:51:45 +0000755 AM.SymbolFlags = G->getTargetFlags();
Chris Lattner18c59872009-06-27 04:16:01 +0000756 } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N0)) {
757 int64_t Offset = AM.Disp + CP->getOffset();
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000758 if (!X86::isOffsetSuitableForCodeModel(Offset, M)) return true;
Rafael Espindola49a168d2009-04-12 21:55:03 +0000759 AM.CP = CP->getConstVal();
760 AM.Align = CP->getAlignment();
Chris Lattner18c59872009-06-27 04:16:01 +0000761 AM.Disp = Offset;
Chris Lattner0b0deab2009-06-26 05:56:49 +0000762 AM.SymbolFlags = CP->getTargetFlags();
Chris Lattner18c59872009-06-27 04:16:01 +0000763 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(N0)) {
764 AM.ES = S->getSymbol();
765 AM.SymbolFlags = S->getTargetFlags();
Chris Lattner43f44aa2009-11-01 03:25:03 +0000766 } else if (JumpTableSDNode *J = dyn_cast<JumpTableSDNode>(N0)) {
Chris Lattner18c59872009-06-27 04:16:01 +0000767 AM.JT = J->getIndex();
768 AM.SymbolFlags = J->getTargetFlags();
Chris Lattner43f44aa2009-11-01 03:25:03 +0000769 } else {
770 AM.BlockAddr = cast<BlockAddressSDNode>(N0)->getBlockAddress();
771 //AM.SymbolFlags = cast<BlockAddressSDNode>(N0)->getTargetFlags();
Rafael Espindola49a168d2009-04-12 21:55:03 +0000772 }
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000773
Chris Lattner18c59872009-06-27 04:16:01 +0000774 if (N.getOpcode() == X86ISD::WrapperRIP)
Owen Anderson825b72b2009-08-11 20:47:22 +0000775 AM.setBaseReg(CurDAG->getRegister(X86::RIP, MVT::i64));
Rafael Espindola49a168d2009-04-12 21:55:03 +0000776 return false;
Chris Lattner18c59872009-06-27 04:16:01 +0000777 }
778
779 // Handle the case when globals fit in our immediate field: This is true for
780 // X86-32 always and X86-64 when in -static -mcmodel=small mode. In 64-bit
781 // mode, this results in a non-RIP-relative computation.
782 if (!Subtarget->is64Bit() ||
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000783 ((M == CodeModel::Small || M == CodeModel::Kernel) &&
Chris Lattner18c59872009-06-27 04:16:01 +0000784 TM.getRelocationModel() == Reloc::Static)) {
785 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(N0)) {
786 AM.GV = G->getGlobal();
787 AM.Disp += G->getOffset();
788 AM.SymbolFlags = G->getTargetFlags();
789 } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N0)) {
790 AM.CP = CP->getConstVal();
791 AM.Align = CP->getAlignment();
792 AM.Disp += CP->getOffset();
793 AM.SymbolFlags = CP->getTargetFlags();
794 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(N0)) {
795 AM.ES = S->getSymbol();
796 AM.SymbolFlags = S->getTargetFlags();
Chris Lattner43f44aa2009-11-01 03:25:03 +0000797 } else if (JumpTableSDNode *J = dyn_cast<JumpTableSDNode>(N0)) {
Chris Lattner18c59872009-06-27 04:16:01 +0000798 AM.JT = J->getIndex();
799 AM.SymbolFlags = J->getTargetFlags();
Chris Lattner43f44aa2009-11-01 03:25:03 +0000800 } else {
801 AM.BlockAddr = cast<BlockAddressSDNode>(N0)->getBlockAddress();
802 //AM.SymbolFlags = cast<BlockAddressSDNode>(N0)->getTargetFlags();
Chris Lattner18c59872009-06-27 04:16:01 +0000803 }
Rafael Espindola49a168d2009-04-12 21:55:03 +0000804 return false;
805 }
806
807 return true;
808}
809
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000810/// MatchAddress - Add the specified node to the specified addressing mode,
811/// returning true if it cannot be done. This just pattern matches for the
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000812/// addressing mode.
Dan Gohman41d0b9d2009-07-22 23:26:55 +0000813bool X86DAGToDAGISel::MatchAddress(SDValue N, X86ISelAddressMode &AM) {
814 if (MatchAddressRecursively(N, AM, 0))
815 return true;
816
817 // Post-processing: Convert lea(,%reg,2) to lea(%reg,%reg), which has
818 // a smaller encoding and avoids a scaled-index.
819 if (AM.Scale == 2 &&
820 AM.BaseType == X86ISelAddressMode::RegBase &&
821 AM.Base.Reg.getNode() == 0) {
822 AM.Base.Reg = AM.IndexReg;
823 AM.Scale = 1;
824 }
825
Dan Gohmanef74e9b2009-08-20 18:23:44 +0000826 // Post-processing: Convert foo to foo(%rip), even in non-PIC mode,
827 // because it has a smaller encoding.
828 // TODO: Which other code models can use this?
829 if (TM.getCodeModel() == CodeModel::Small &&
830 Subtarget->is64Bit() &&
831 AM.Scale == 1 &&
832 AM.BaseType == X86ISelAddressMode::RegBase &&
833 AM.Base.Reg.getNode() == 0 &&
834 AM.IndexReg.getNode() == 0 &&
Dan Gohman79b765d2009-08-25 17:47:44 +0000835 AM.SymbolFlags == X86II::MO_NO_FLAG &&
Dan Gohmanef74e9b2009-08-20 18:23:44 +0000836 AM.hasSymbolicDisplacement())
837 AM.Base.Reg = CurDAG->getRegister(X86::RIP, MVT::i64);
838
Dan Gohman41d0b9d2009-07-22 23:26:55 +0000839 return false;
840}
841
842bool X86DAGToDAGISel::MatchAddressRecursively(SDValue N, X86ISelAddressMode &AM,
843 unsigned Depth) {
Dan Gohman6520e202008-10-18 02:06:02 +0000844 bool is64Bit = Subtarget->is64Bit();
Dale Johannesen6f38cb62009-02-07 19:59:05 +0000845 DebugLoc dl = N.getDebugLoc();
Bill Wendling12321672009-08-07 21:33:25 +0000846 DEBUG({
847 errs() << "MatchAddress: ";
848 AM.dump();
849 });
Dan Gohmanbadb2d22007-08-13 20:03:06 +0000850 // Limit recursion.
851 if (Depth > 5)
Rafael Espindola523249f2009-03-31 16:16:57 +0000852 return MatchAddressBase(N, AM);
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000853
854 CodeModel::Model M = TM.getCodeModel();
855
Chris Lattner18c59872009-06-27 04:16:01 +0000856 // If this is already a %rip relative address, we can only merge immediates
857 // into it. Instead of handling this in every case, we handle it here.
Evan Cheng25ab6902006-09-08 06:48:29 +0000858 // RIP relative addressing: %rip + 32-bit displacement!
Chris Lattner18c59872009-06-27 04:16:01 +0000859 if (AM.isRIPRelative()) {
860 // FIXME: JumpTable and ExternalSymbol address currently don't like
861 // displacements. It isn't very important, but this should be fixed for
862 // consistency.
863 if (!AM.ES && AM.JT != -1) return true;
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000864
Chris Lattner18c59872009-06-27 04:16:01 +0000865 if (ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(N)) {
866 int64_t Val = AM.Disp + Cst->getSExtValue();
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000867 if (X86::isOffsetSuitableForCodeModel(Val, M,
868 AM.hasSymbolicDisplacement())) {
Chris Lattner18c59872009-06-27 04:16:01 +0000869 AM.Disp = Val;
Evan Cheng25ab6902006-09-08 06:48:29 +0000870 return false;
871 }
872 }
873 return true;
874 }
875
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000876 switch (N.getOpcode()) {
877 default: break;
Evan Cheng25ab6902006-09-08 06:48:29 +0000878 case ISD::Constant: {
Dan Gohman27cae7b2008-11-11 15:52:29 +0000879 uint64_t Val = cast<ConstantSDNode>(N)->getSExtValue();
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000880 if (!is64Bit ||
881 X86::isOffsetSuitableForCodeModel(AM.Disp + Val, M,
882 AM.hasSymbolicDisplacement())) {
Evan Cheng25ab6902006-09-08 06:48:29 +0000883 AM.Disp += Val;
884 return false;
885 }
886 break;
887 }
Evan Cheng51a9ed92006-02-25 10:09:08 +0000888
Rafael Espindola094fad32009-04-08 21:14:34 +0000889 case X86ISD::SegmentBaseAddress:
890 if (!MatchSegmentBaseAddress(N, AM))
891 return false;
892 break;
893
Rafael Espindola49a168d2009-04-12 21:55:03 +0000894 case X86ISD::Wrapper:
Chris Lattner18c59872009-06-27 04:16:01 +0000895 case X86ISD::WrapperRIP:
Rafael Espindola49a168d2009-04-12 21:55:03 +0000896 if (!MatchWrapper(N, AM))
897 return false;
Evan Cheng51a9ed92006-02-25 10:09:08 +0000898 break;
899
Rafael Espindola094fad32009-04-08 21:14:34 +0000900 case ISD::LOAD:
901 if (!MatchLoad(N, AM))
902 return false;
903 break;
904
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000905 case ISD::FrameIndex:
Gabor Greif93c53e52008-08-31 15:37:04 +0000906 if (AM.BaseType == X86ISelAddressMode::RegBase
907 && AM.Base.Reg.getNode() == 0) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000908 AM.BaseType = X86ISelAddressMode::FrameIndexBase;
909 AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
910 return false;
911 }
912 break;
Evan Chengec693f72005-12-08 02:01:35 +0000913
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000914 case ISD::SHL:
Chris Lattner18c59872009-06-27 04:16:01 +0000915 if (AM.IndexReg.getNode() != 0 || AM.Scale != 1)
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000916 break;
917
Gabor Greif93c53e52008-08-31 15:37:04 +0000918 if (ConstantSDNode
919 *CN = dyn_cast<ConstantSDNode>(N.getNode()->getOperand(1))) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000920 unsigned Val = CN->getZExtValue();
Dan Gohman41d0b9d2009-07-22 23:26:55 +0000921 // Note that we handle x<<1 as (,x,2) rather than (x,x) here so
922 // that the base operand remains free for further matching. If
923 // the base doesn't end up getting used, a post-processing step
924 // in MatchAddress turns (,x,2) into (x,x), which is cheaper.
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000925 if (Val == 1 || Val == 2 || Val == 3) {
926 AM.Scale = 1 << Val;
Gabor Greifba36cb52008-08-28 21:40:38 +0000927 SDValue ShVal = N.getNode()->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000928
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000929 // Okay, we know that we have a scale by now. However, if the scaled
930 // value is an add of something and a constant, we can fold the
931 // constant into the disp field here.
Gabor Greifba36cb52008-08-28 21:40:38 +0000932 if (ShVal.getNode()->getOpcode() == ISD::ADD && ShVal.hasOneUse() &&
933 isa<ConstantSDNode>(ShVal.getNode()->getOperand(1))) {
934 AM.IndexReg = ShVal.getNode()->getOperand(0);
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000935 ConstantSDNode *AddVal =
Gabor Greifba36cb52008-08-28 21:40:38 +0000936 cast<ConstantSDNode>(ShVal.getNode()->getOperand(1));
Evan Cheng8e278262009-01-17 07:09:27 +0000937 uint64_t Disp = AM.Disp + (AddVal->getSExtValue() << Val);
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000938 if (!is64Bit ||
939 X86::isOffsetSuitableForCodeModel(Disp, M,
940 AM.hasSymbolicDisplacement()))
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000941 AM.Disp = Disp;
942 else
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000943 AM.IndexReg = ShVal;
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000944 } else {
945 AM.IndexReg = ShVal;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000946 }
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000947 return false;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000948 }
949 break;
Chris Lattner5aaddaa2007-12-08 07:22:58 +0000950 }
Evan Chengec693f72005-12-08 02:01:35 +0000951
Dan Gohman83688052007-10-22 20:22:24 +0000952 case ISD::SMUL_LOHI:
953 case ISD::UMUL_LOHI:
954 // A mul_lohi where we need the low part can be folded as a plain multiply.
Gabor Greif99a6cb92008-08-26 22:36:50 +0000955 if (N.getResNo() != 0) break;
Dan Gohman83688052007-10-22 20:22:24 +0000956 // FALL THROUGH
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000957 case ISD::MUL:
Evan Cheng73f24c92009-03-30 21:36:47 +0000958 case X86ISD::MUL_IMM:
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000959 // X*[3,5,9] -> X+X*[2,4,8]
Dan Gohman8be6bbe2008-11-05 04:14:16 +0000960 if (AM.BaseType == X86ISelAddressMode::RegBase &&
Gabor Greifba36cb52008-08-28 21:40:38 +0000961 AM.Base.Reg.getNode() == 0 &&
Chris Lattner18c59872009-06-27 04:16:01 +0000962 AM.IndexReg.getNode() == 0) {
Gabor Greif93c53e52008-08-31 15:37:04 +0000963 if (ConstantSDNode
964 *CN = dyn_cast<ConstantSDNode>(N.getNode()->getOperand(1)))
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000965 if (CN->getZExtValue() == 3 || CN->getZExtValue() == 5 ||
966 CN->getZExtValue() == 9) {
967 AM.Scale = unsigned(CN->getZExtValue())-1;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000968
Gabor Greifba36cb52008-08-28 21:40:38 +0000969 SDValue MulVal = N.getNode()->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +0000970 SDValue Reg;
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000971
972 // Okay, we know that we have a scale by now. However, if the scaled
973 // value is an add of something and a constant, we can fold the
974 // constant into the disp field here.
Gabor Greifba36cb52008-08-28 21:40:38 +0000975 if (MulVal.getNode()->getOpcode() == ISD::ADD && MulVal.hasOneUse() &&
976 isa<ConstantSDNode>(MulVal.getNode()->getOperand(1))) {
977 Reg = MulVal.getNode()->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000978 ConstantSDNode *AddVal =
Gabor Greifba36cb52008-08-28 21:40:38 +0000979 cast<ConstantSDNode>(MulVal.getNode()->getOperand(1));
Evan Cheng8e278262009-01-17 07:09:27 +0000980 uint64_t Disp = AM.Disp + AddVal->getSExtValue() *
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000981 CN->getZExtValue();
Anton Korobeynikovb5e01722009-08-05 23:01:26 +0000982 if (!is64Bit ||
983 X86::isOffsetSuitableForCodeModel(Disp, M,
984 AM.hasSymbolicDisplacement()))
Evan Cheng25ab6902006-09-08 06:48:29 +0000985 AM.Disp = Disp;
986 else
Gabor Greifba36cb52008-08-28 21:40:38 +0000987 Reg = N.getNode()->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000988 } else {
Gabor Greifba36cb52008-08-28 21:40:38 +0000989 Reg = N.getNode()->getOperand(0);
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000990 }
991
992 AM.IndexReg = AM.Base.Reg = Reg;
993 return false;
994 }
Chris Lattner62412262007-02-04 20:18:17 +0000995 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +0000996 break;
997
Dan Gohman3cd90a12009-05-11 18:02:53 +0000998 case ISD::SUB: {
999 // Given A-B, if A can be completely folded into the address and
1000 // the index field with the index field unused, use -B as the index.
1001 // This is a win if a has multiple parts that can be folded into
1002 // the address. Also, this saves a mov if the base register has
1003 // other uses, since it avoids a two-address sub instruction, however
1004 // it costs an additional mov if the index register has other uses.
1005
1006 // Test if the LHS of the sub can be folded.
1007 X86ISelAddressMode Backup = AM;
Dan Gohman41d0b9d2009-07-22 23:26:55 +00001008 if (MatchAddressRecursively(N.getNode()->getOperand(0), AM, Depth+1)) {
Dan Gohman3cd90a12009-05-11 18:02:53 +00001009 AM = Backup;
1010 break;
1011 }
1012 // Test if the index field is free for use.
Chris Lattner18c59872009-06-27 04:16:01 +00001013 if (AM.IndexReg.getNode() || AM.isRIPRelative()) {
Dan Gohman3cd90a12009-05-11 18:02:53 +00001014 AM = Backup;
1015 break;
1016 }
1017 int Cost = 0;
1018 SDValue RHS = N.getNode()->getOperand(1);
1019 // If the RHS involves a register with multiple uses, this
1020 // transformation incurs an extra mov, due to the neg instruction
1021 // clobbering its operand.
1022 if (!RHS.getNode()->hasOneUse() ||
1023 RHS.getNode()->getOpcode() == ISD::CopyFromReg ||
1024 RHS.getNode()->getOpcode() == ISD::TRUNCATE ||
1025 RHS.getNode()->getOpcode() == ISD::ANY_EXTEND ||
1026 (RHS.getNode()->getOpcode() == ISD::ZERO_EXTEND &&
Owen Anderson825b72b2009-08-11 20:47:22 +00001027 RHS.getNode()->getOperand(0).getValueType() == MVT::i32))
Dan Gohman3cd90a12009-05-11 18:02:53 +00001028 ++Cost;
1029 // If the base is a register with multiple uses, this
1030 // transformation may save a mov.
1031 if ((AM.BaseType == X86ISelAddressMode::RegBase &&
1032 AM.Base.Reg.getNode() &&
1033 !AM.Base.Reg.getNode()->hasOneUse()) ||
1034 AM.BaseType == X86ISelAddressMode::FrameIndexBase)
1035 --Cost;
1036 // If the folded LHS was interesting, this transformation saves
1037 // address arithmetic.
1038 if ((AM.hasSymbolicDisplacement() && !Backup.hasSymbolicDisplacement()) +
1039 ((AM.Disp != 0) && (Backup.Disp == 0)) +
1040 (AM.Segment.getNode() && !Backup.Segment.getNode()) >= 2)
1041 --Cost;
1042 // If it doesn't look like it may be an overall win, don't do it.
1043 if (Cost >= 0) {
1044 AM = Backup;
1045 break;
1046 }
1047
1048 // Ok, the transformation is legal and appears profitable. Go for it.
1049 SDValue Zero = CurDAG->getConstant(0, N.getValueType());
1050 SDValue Neg = CurDAG->getNode(ISD::SUB, dl, N.getValueType(), Zero, RHS);
1051 AM.IndexReg = Neg;
1052 AM.Scale = 1;
1053
1054 // Insert the new nodes into the topological ordering.
1055 if (Zero.getNode()->getNodeId() == -1 ||
1056 Zero.getNode()->getNodeId() > N.getNode()->getNodeId()) {
1057 CurDAG->RepositionNode(N.getNode(), Zero.getNode());
1058 Zero.getNode()->setNodeId(N.getNode()->getNodeId());
1059 }
1060 if (Neg.getNode()->getNodeId() == -1 ||
1061 Neg.getNode()->getNodeId() > N.getNode()->getNodeId()) {
1062 CurDAG->RepositionNode(N.getNode(), Neg.getNode());
1063 Neg.getNode()->setNodeId(N.getNode()->getNodeId());
1064 }
1065 return false;
1066 }
1067
Evan Cheng8e278262009-01-17 07:09:27 +00001068 case ISD::ADD: {
1069 X86ISelAddressMode Backup = AM;
Dan Gohman41d0b9d2009-07-22 23:26:55 +00001070 if (!MatchAddressRecursively(N.getNode()->getOperand(0), AM, Depth+1) &&
1071 !MatchAddressRecursively(N.getNode()->getOperand(1), AM, Depth+1))
Evan Cheng8e278262009-01-17 07:09:27 +00001072 return false;
1073 AM = Backup;
Dan Gohman41d0b9d2009-07-22 23:26:55 +00001074 if (!MatchAddressRecursively(N.getNode()->getOperand(1), AM, Depth+1) &&
1075 !MatchAddressRecursively(N.getNode()->getOperand(0), AM, Depth+1))
Evan Cheng8e278262009-01-17 07:09:27 +00001076 return false;
1077 AM = Backup;
Dan Gohman77502c92009-03-13 02:25:09 +00001078
1079 // If we couldn't fold both operands into the address at the same time,
1080 // see if we can just put each operand into a register and fold at least
1081 // the add.
1082 if (AM.BaseType == X86ISelAddressMode::RegBase &&
1083 !AM.Base.Reg.getNode() &&
Chris Lattner18c59872009-06-27 04:16:01 +00001084 !AM.IndexReg.getNode()) {
Dan Gohman77502c92009-03-13 02:25:09 +00001085 AM.Base.Reg = N.getNode()->getOperand(0);
1086 AM.IndexReg = N.getNode()->getOperand(1);
1087 AM.Scale = 1;
1088 return false;
1089 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001090 break;
Evan Cheng8e278262009-01-17 07:09:27 +00001091 }
Evan Chenge6ad27e2006-05-30 06:59:36 +00001092
Chris Lattner62412262007-02-04 20:18:17 +00001093 case ISD::OR:
1094 // Handle "X | C" as "X + C" iff X is known to have C bits clear.
Chris Lattner5aaddaa2007-12-08 07:22:58 +00001095 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1096 X86ISelAddressMode Backup = AM;
Dan Gohman27cae7b2008-11-11 15:52:29 +00001097 uint64_t Offset = CN->getSExtValue();
Chris Lattner5aaddaa2007-12-08 07:22:58 +00001098 // Start with the LHS as an addr mode.
Dan Gohman41d0b9d2009-07-22 23:26:55 +00001099 if (!MatchAddressRecursively(N.getOperand(0), AM, Depth+1) &&
Chris Lattner5aaddaa2007-12-08 07:22:58 +00001100 // Address could not have picked a GV address for the displacement.
1101 AM.GV == NULL &&
1102 // On x86-64, the resultant disp must fit in 32-bits.
Anton Korobeynikovb5e01722009-08-05 23:01:26 +00001103 (!is64Bit ||
1104 X86::isOffsetSuitableForCodeModel(AM.Disp + Offset, M,
1105 AM.hasSymbolicDisplacement())) &&
Chris Lattner5aaddaa2007-12-08 07:22:58 +00001106 // Check to see if the LHS & C is zero.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001107 CurDAG->MaskedValueIsZero(N.getOperand(0), CN->getAPIntValue())) {
Dan Gohman27cae7b2008-11-11 15:52:29 +00001108 AM.Disp += Offset;
Chris Lattner5aaddaa2007-12-08 07:22:58 +00001109 return false;
Evan Chenge6ad27e2006-05-30 06:59:36 +00001110 }
Chris Lattner5aaddaa2007-12-08 07:22:58 +00001111 AM = Backup;
Evan Chenge6ad27e2006-05-30 06:59:36 +00001112 }
1113 break;
Evan Cheng1314b002007-12-13 00:43:27 +00001114
1115 case ISD::AND: {
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001116 // Perform some heroic transforms on an and of a constant-count shift
1117 // with a constant to enable use of the scaled offset field.
1118
Dan Gohman475871a2008-07-27 21:46:04 +00001119 SDValue Shift = N.getOperand(0);
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001120 if (Shift.getNumOperands() != 2) break;
Dan Gohman8be6bbe2008-11-05 04:14:16 +00001121
Evan Cheng1314b002007-12-13 00:43:27 +00001122 // Scale must not be used already.
Gabor Greifba36cb52008-08-28 21:40:38 +00001123 if (AM.IndexReg.getNode() != 0 || AM.Scale != 1) break;
Evan Chengbe3bf422008-02-07 08:53:49 +00001124
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001125 SDValue X = Shift.getOperand(0);
Evan Cheng1314b002007-12-13 00:43:27 +00001126 ConstantSDNode *C2 = dyn_cast<ConstantSDNode>(N.getOperand(1));
1127 ConstantSDNode *C1 = dyn_cast<ConstantSDNode>(Shift.getOperand(1));
1128 if (!C1 || !C2) break;
1129
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001130 // Handle "(X >> (8-C1)) & C2" as "(X >> 8) & 0xff)" if safe. This
1131 // allows us to convert the shift and and into an h-register extract and
1132 // a scaled index.
1133 if (Shift.getOpcode() == ISD::SRL && Shift.hasOneUse()) {
1134 unsigned ScaleLog = 8 - C1->getZExtValue();
Rafael Espindola7c366832009-04-16 12:34:53 +00001135 if (ScaleLog > 0 && ScaleLog < 4 &&
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001136 C2->getZExtValue() == (UINT64_C(0xff) << ScaleLog)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001137 SDValue Eight = CurDAG->getConstant(8, MVT::i8);
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001138 SDValue Mask = CurDAG->getConstant(0xff, N.getValueType());
1139 SDValue Srl = CurDAG->getNode(ISD::SRL, dl, N.getValueType(),
1140 X, Eight);
1141 SDValue And = CurDAG->getNode(ISD::AND, dl, N.getValueType(),
1142 Srl, Mask);
Owen Anderson825b72b2009-08-11 20:47:22 +00001143 SDValue ShlCount = CurDAG->getConstant(ScaleLog, MVT::i8);
Dan Gohman62ad1382009-04-14 22:45:05 +00001144 SDValue Shl = CurDAG->getNode(ISD::SHL, dl, N.getValueType(),
1145 And, ShlCount);
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001146
1147 // Insert the new nodes into the topological ordering.
1148 if (Eight.getNode()->getNodeId() == -1 ||
1149 Eight.getNode()->getNodeId() > X.getNode()->getNodeId()) {
1150 CurDAG->RepositionNode(X.getNode(), Eight.getNode());
1151 Eight.getNode()->setNodeId(X.getNode()->getNodeId());
1152 }
1153 if (Mask.getNode()->getNodeId() == -1 ||
1154 Mask.getNode()->getNodeId() > X.getNode()->getNodeId()) {
1155 CurDAG->RepositionNode(X.getNode(), Mask.getNode());
1156 Mask.getNode()->setNodeId(X.getNode()->getNodeId());
1157 }
1158 if (Srl.getNode()->getNodeId() == -1 ||
1159 Srl.getNode()->getNodeId() > Shift.getNode()->getNodeId()) {
1160 CurDAG->RepositionNode(Shift.getNode(), Srl.getNode());
1161 Srl.getNode()->setNodeId(Shift.getNode()->getNodeId());
1162 }
1163 if (And.getNode()->getNodeId() == -1 ||
1164 And.getNode()->getNodeId() > N.getNode()->getNodeId()) {
1165 CurDAG->RepositionNode(N.getNode(), And.getNode());
1166 And.getNode()->setNodeId(N.getNode()->getNodeId());
1167 }
Dan Gohman62ad1382009-04-14 22:45:05 +00001168 if (ShlCount.getNode()->getNodeId() == -1 ||
1169 ShlCount.getNode()->getNodeId() > X.getNode()->getNodeId()) {
1170 CurDAG->RepositionNode(X.getNode(), ShlCount.getNode());
1171 ShlCount.getNode()->setNodeId(N.getNode()->getNodeId());
1172 }
1173 if (Shl.getNode()->getNodeId() == -1 ||
1174 Shl.getNode()->getNodeId() > N.getNode()->getNodeId()) {
1175 CurDAG->RepositionNode(N.getNode(), Shl.getNode());
1176 Shl.getNode()->setNodeId(N.getNode()->getNodeId());
1177 }
1178 CurDAG->ReplaceAllUsesWith(N, Shl);
Dan Gohman21e3dfb2009-04-13 16:09:41 +00001179 AM.IndexReg = And;
1180 AM.Scale = (1 << ScaleLog);
1181 return false;
1182 }
1183 }
1184
1185 // Handle "(X << C1) & C2" as "(X & (C2>>C1)) << C1" if safe and if this
1186 // allows us to fold the shift into this addressing mode.
1187 if (Shift.getOpcode() != ISD::SHL) break;
1188
Evan Cheng1314b002007-12-13 00:43:27 +00001189 // Not likely to be profitable if either the AND or SHIFT node has more
1190 // than one use (unless all uses are for address computation). Besides,
1191 // isel mechanism requires their node ids to be reused.
1192 if (!N.hasOneUse() || !Shift.hasOneUse())
1193 break;
1194
1195 // Verify that the shift amount is something we can fold.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001196 unsigned ShiftCst = C1->getZExtValue();
Evan Cheng1314b002007-12-13 00:43:27 +00001197 if (ShiftCst != 1 && ShiftCst != 2 && ShiftCst != 3)
1198 break;
1199
1200 // Get the new AND mask, this folds to a constant.
Dale Johannesend8392542009-02-03 21:48:12 +00001201 SDValue NewANDMask = CurDAG->getNode(ISD::SRL, dl, N.getValueType(),
Evan Cheng552e3be2008-10-14 17:15:39 +00001202 SDValue(C2, 0), SDValue(C1, 0));
Dale Johannesend8392542009-02-03 21:48:12 +00001203 SDValue NewAND = CurDAG->getNode(ISD::AND, dl, N.getValueType(), X,
1204 NewANDMask);
1205 SDValue NewSHIFT = CurDAG->getNode(ISD::SHL, dl, N.getValueType(),
Dan Gohman7b8e9642008-10-13 20:52:04 +00001206 NewAND, SDValue(C1, 0));
Dan Gohman8be6bbe2008-11-05 04:14:16 +00001207
1208 // Insert the new nodes into the topological ordering.
1209 if (C1->getNodeId() > X.getNode()->getNodeId()) {
1210 CurDAG->RepositionNode(X.getNode(), C1);
1211 C1->setNodeId(X.getNode()->getNodeId());
1212 }
1213 if (NewANDMask.getNode()->getNodeId() == -1 ||
1214 NewANDMask.getNode()->getNodeId() > X.getNode()->getNodeId()) {
1215 CurDAG->RepositionNode(X.getNode(), NewANDMask.getNode());
1216 NewANDMask.getNode()->setNodeId(X.getNode()->getNodeId());
1217 }
1218 if (NewAND.getNode()->getNodeId() == -1 ||
1219 NewAND.getNode()->getNodeId() > Shift.getNode()->getNodeId()) {
1220 CurDAG->RepositionNode(Shift.getNode(), NewAND.getNode());
1221 NewAND.getNode()->setNodeId(Shift.getNode()->getNodeId());
1222 }
1223 if (NewSHIFT.getNode()->getNodeId() == -1 ||
1224 NewSHIFT.getNode()->getNodeId() > N.getNode()->getNodeId()) {
1225 CurDAG->RepositionNode(N.getNode(), NewSHIFT.getNode());
1226 NewSHIFT.getNode()->setNodeId(N.getNode()->getNodeId());
1227 }
1228
Dan Gohman7b8e9642008-10-13 20:52:04 +00001229 CurDAG->ReplaceAllUsesWith(N, NewSHIFT);
Evan Cheng1314b002007-12-13 00:43:27 +00001230
1231 AM.Scale = 1 << ShiftCst;
1232 AM.IndexReg = NewAND;
1233 return false;
1234 }
Evan Chenge6ad27e2006-05-30 06:59:36 +00001235 }
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001236
Rafael Espindola523249f2009-03-31 16:16:57 +00001237 return MatchAddressBase(N, AM);
Dan Gohmanbadb2d22007-08-13 20:03:06 +00001238}
1239
1240/// MatchAddressBase - Helper for MatchAddress. Add the specified node to the
1241/// specified addressing mode without any further recursion.
Rafael Espindola523249f2009-03-31 16:16:57 +00001242bool X86DAGToDAGISel::MatchAddressBase(SDValue N, X86ISelAddressMode &AM) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001243 // Is the base register already occupied?
Gabor Greifba36cb52008-08-28 21:40:38 +00001244 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base.Reg.getNode()) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001245 // If so, check to see if the scale index register is set.
Chris Lattner18c59872009-06-27 04:16:01 +00001246 if (AM.IndexReg.getNode() == 0) {
Chris Lattnerf9ce9fb2005-11-19 02:11:08 +00001247 AM.IndexReg = N;
1248 AM.Scale = 1;
1249 return false;
1250 }
1251
1252 // Otherwise, we cannot select it.
1253 return true;
1254 }
1255
1256 // Default, generate it as a register.
1257 AM.BaseType = X86ISelAddressMode::RegBase;
1258 AM.Base.Reg = N;
1259 return false;
1260}
1261
Evan Chengec693f72005-12-08 02:01:35 +00001262/// SelectAddr - returns true if it is able pattern match an addressing mode.
1263/// It returns the operands which make up the maximal addressing mode it can
1264/// match by reference.
Dan Gohman475871a2008-07-27 21:46:04 +00001265bool X86DAGToDAGISel::SelectAddr(SDValue Op, SDValue N, SDValue &Base,
1266 SDValue &Scale, SDValue &Index,
Rafael Espindola094fad32009-04-08 21:14:34 +00001267 SDValue &Disp, SDValue &Segment) {
Evan Chengec693f72005-12-08 02:01:35 +00001268 X86ISelAddressMode AM;
Evan Cheng4d952322009-03-31 01:13:53 +00001269 bool Done = false;
1270 if (AvoidDupAddrCompute && !N.hasOneUse()) {
1271 unsigned Opcode = N.getOpcode();
1272 if (Opcode != ISD::Constant && Opcode != ISD::FrameIndex &&
Chris Lattner18c59872009-06-27 04:16:01 +00001273 Opcode != X86ISD::Wrapper && Opcode != X86ISD::WrapperRIP) {
Evan Cheng4d952322009-03-31 01:13:53 +00001274 // If we are able to fold N into addressing mode, then we'll allow it even
1275 // if N has multiple uses. In general, addressing computation is used as
1276 // addresses by all of its uses. But watch out for CopyToReg uses, that
1277 // means the address computation is liveout. It will be computed by a LEA
1278 // so we want to avoid computing the address twice.
1279 for (SDNode::use_iterator UI = N.getNode()->use_begin(),
1280 UE = N.getNode()->use_end(); UI != UE; ++UI) {
1281 if (UI->getOpcode() == ISD::CopyToReg) {
Rafael Espindola523249f2009-03-31 16:16:57 +00001282 MatchAddressBase(N, AM);
Evan Cheng4d952322009-03-31 01:13:53 +00001283 Done = true;
1284 break;
1285 }
1286 }
1287 }
1288 }
1289
1290 if (!Done && MatchAddress(N, AM))
Evan Cheng8700e142006-01-11 06:09:51 +00001291 return false;
Evan Chengec693f72005-12-08 02:01:35 +00001292
Owen Andersone50ed302009-08-10 22:56:29 +00001293 EVT VT = N.getValueType();
Evan Cheng8700e142006-01-11 06:09:51 +00001294 if (AM.BaseType == X86ISelAddressMode::RegBase) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001295 if (!AM.Base.Reg.getNode())
Evan Cheng25ab6902006-09-08 06:48:29 +00001296 AM.Base.Reg = CurDAG->getRegister(0, VT);
Evan Chengec693f72005-12-08 02:01:35 +00001297 }
Evan Cheng8700e142006-01-11 06:09:51 +00001298
Gabor Greifba36cb52008-08-28 21:40:38 +00001299 if (!AM.IndexReg.getNode())
Evan Cheng25ab6902006-09-08 06:48:29 +00001300 AM.IndexReg = CurDAG->getRegister(0, VT);
Evan Cheng8700e142006-01-11 06:09:51 +00001301
Rafael Espindola094fad32009-04-08 21:14:34 +00001302 getAddressOperands(AM, Base, Scale, Index, Disp, Segment);
Evan Cheng8700e142006-01-11 06:09:51 +00001303 return true;
Evan Chengec693f72005-12-08 02:01:35 +00001304}
1305
Chris Lattner3a7cd952006-10-07 21:55:32 +00001306/// SelectScalarSSELoad - Match a scalar SSE load. In particular, we want to
1307/// match a load whose top elements are either undef or zeros. The load flavor
1308/// is derived from the type of N, which is either v4f32 or v2f64.
Dan Gohman475871a2008-07-27 21:46:04 +00001309bool X86DAGToDAGISel::SelectScalarSSELoad(SDValue Op, SDValue Pred,
1310 SDValue N, SDValue &Base,
1311 SDValue &Scale, SDValue &Index,
Rafael Espindola094fad32009-04-08 21:14:34 +00001312 SDValue &Disp, SDValue &Segment,
1313 SDValue &InChain,
Dan Gohman475871a2008-07-27 21:46:04 +00001314 SDValue &OutChain) {
Chris Lattner3a7cd952006-10-07 21:55:32 +00001315 if (N.getOpcode() == ISD::SCALAR_TO_VECTOR) {
Chris Lattner4fe4f252006-10-11 22:09:58 +00001316 InChain = N.getOperand(0).getValue(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00001317 if (ISD::isNON_EXTLoad(InChain.getNode()) &&
Evan Cheng07e4b002006-10-16 06:34:55 +00001318 InChain.getValue(0).hasOneUse() &&
Evan Chengd6373bc2006-11-10 21:23:04 +00001319 N.hasOneUse() &&
Evan Cheng884c70c2008-11-27 00:49:46 +00001320 IsLegalAndProfitableToFold(N.getNode(), Pred.getNode(), Op.getNode())) {
Evan Cheng82a91642006-10-11 21:06:01 +00001321 LoadSDNode *LD = cast<LoadSDNode>(InChain);
Rafael Espindola094fad32009-04-08 21:14:34 +00001322 if (!SelectAddr(Op, LD->getBasePtr(), Base, Scale, Index, Disp, Segment))
Chris Lattner3a7cd952006-10-07 21:55:32 +00001323 return false;
Evan Cheng82a91642006-10-11 21:06:01 +00001324 OutChain = LD->getChain();
Chris Lattner3a7cd952006-10-07 21:55:32 +00001325 return true;
1326 }
1327 }
Chris Lattner4fe4f252006-10-11 22:09:58 +00001328
1329 // Also handle the case where we explicitly require zeros in the top
Chris Lattner3a7cd952006-10-07 21:55:32 +00001330 // elements. This is a vector shuffle from the zero vector.
Gabor Greifba36cb52008-08-28 21:40:38 +00001331 if (N.getOpcode() == X86ISD::VZEXT_MOVL && N.getNode()->hasOneUse() &&
Chris Lattner8a594482007-11-25 00:24:49 +00001332 // Check to see if the top elements are all zeros (or bitcast of zeros).
Evan Cheng7e2ff772008-05-08 00:57:18 +00001333 N.getOperand(0).getOpcode() == ISD::SCALAR_TO_VECTOR &&
Gabor Greifba36cb52008-08-28 21:40:38 +00001334 N.getOperand(0).getNode()->hasOneUse() &&
1335 ISD::isNON_EXTLoad(N.getOperand(0).getOperand(0).getNode()) &&
Evan Cheng7e2ff772008-05-08 00:57:18 +00001336 N.getOperand(0).getOperand(0).hasOneUse()) {
1337 // Okay, this is a zero extending load. Fold it.
1338 LoadSDNode *LD = cast<LoadSDNode>(N.getOperand(0).getOperand(0));
Rafael Espindola094fad32009-04-08 21:14:34 +00001339 if (!SelectAddr(Op, LD->getBasePtr(), Base, Scale, Index, Disp, Segment))
Evan Cheng7e2ff772008-05-08 00:57:18 +00001340 return false;
1341 OutChain = LD->getChain();
Dan Gohman475871a2008-07-27 21:46:04 +00001342 InChain = SDValue(LD, 1);
Evan Cheng7e2ff772008-05-08 00:57:18 +00001343 return true;
Chris Lattner4fe4f252006-10-11 22:09:58 +00001344 }
Chris Lattner3a7cd952006-10-07 21:55:32 +00001345 return false;
1346}
1347
1348
Evan Cheng51a9ed92006-02-25 10:09:08 +00001349/// SelectLEAAddr - it calls SelectAddr and determines if the maximal addressing
1350/// mode it matches can be cost effectively emitted as an LEA instruction.
Dan Gohman475871a2008-07-27 21:46:04 +00001351bool X86DAGToDAGISel::SelectLEAAddr(SDValue Op, SDValue N,
1352 SDValue &Base, SDValue &Scale,
1353 SDValue &Index, SDValue &Disp) {
Evan Cheng51a9ed92006-02-25 10:09:08 +00001354 X86ISelAddressMode AM;
Rafael Espindoladbcfb302009-04-10 10:09:34 +00001355
1356 // Set AM.Segment to prevent MatchAddress from using one. LEA doesn't support
1357 // segments.
1358 SDValue Copy = AM.Segment;
Owen Anderson825b72b2009-08-11 20:47:22 +00001359 SDValue T = CurDAG->getRegister(0, MVT::i32);
Rafael Espindoladbcfb302009-04-10 10:09:34 +00001360 AM.Segment = T;
Evan Cheng51a9ed92006-02-25 10:09:08 +00001361 if (MatchAddress(N, AM))
1362 return false;
Rafael Espindoladbcfb302009-04-10 10:09:34 +00001363 assert (T == AM.Segment);
1364 AM.Segment = Copy;
Rafael Espindola094fad32009-04-08 21:14:34 +00001365
Owen Andersone50ed302009-08-10 22:56:29 +00001366 EVT VT = N.getValueType();
Evan Cheng51a9ed92006-02-25 10:09:08 +00001367 unsigned Complexity = 0;
1368 if (AM.BaseType == X86ISelAddressMode::RegBase)
Gabor Greifba36cb52008-08-28 21:40:38 +00001369 if (AM.Base.Reg.getNode())
Evan Cheng51a9ed92006-02-25 10:09:08 +00001370 Complexity = 1;
1371 else
Evan Cheng25ab6902006-09-08 06:48:29 +00001372 AM.Base.Reg = CurDAG->getRegister(0, VT);
Evan Cheng51a9ed92006-02-25 10:09:08 +00001373 else if (AM.BaseType == X86ISelAddressMode::FrameIndexBase)
1374 Complexity = 4;
1375
Gabor Greifba36cb52008-08-28 21:40:38 +00001376 if (AM.IndexReg.getNode())
Evan Cheng51a9ed92006-02-25 10:09:08 +00001377 Complexity++;
1378 else
Evan Cheng25ab6902006-09-08 06:48:29 +00001379 AM.IndexReg = CurDAG->getRegister(0, VT);
Evan Cheng51a9ed92006-02-25 10:09:08 +00001380
Chris Lattnera16b7cb2007-03-20 06:08:29 +00001381 // Don't match just leal(,%reg,2). It's cheaper to do addl %reg, %reg, or with
1382 // a simple shift.
1383 if (AM.Scale > 1)
Evan Cheng8c03fe42006-02-28 21:13:57 +00001384 Complexity++;
Evan Cheng51a9ed92006-02-25 10:09:08 +00001385
1386 // FIXME: We are artificially lowering the criteria to turn ADD %reg, $GA
1387 // to a LEA. This is determined with some expermentation but is by no means
1388 // optimal (especially for code size consideration). LEA is nice because of
1389 // its three-address nature. Tweak the cost function again when we can run
1390 // convertToThreeAddress() at register allocation time.
Dan Gohman2d0a1cc2009-02-07 00:43:41 +00001391 if (AM.hasSymbolicDisplacement()) {
Evan Cheng25ab6902006-09-08 06:48:29 +00001392 // For X86-64, we should always use lea to materialize RIP relative
1393 // addresses.
Evan Cheng953fa042006-12-05 22:03:40 +00001394 if (Subtarget->is64Bit())
Evan Cheng25ab6902006-09-08 06:48:29 +00001395 Complexity = 4;
1396 else
1397 Complexity += 2;
1398 }
Evan Cheng51a9ed92006-02-25 10:09:08 +00001399
Gabor Greifba36cb52008-08-28 21:40:38 +00001400 if (AM.Disp && (AM.Base.Reg.getNode() || AM.IndexReg.getNode()))
Evan Cheng51a9ed92006-02-25 10:09:08 +00001401 Complexity++;
1402
Chris Lattner25142782009-07-11 22:50:33 +00001403 // If it isn't worth using an LEA, reject it.
Chris Lattner14f75112009-07-11 23:07:30 +00001404 if (Complexity <= 2)
Chris Lattner25142782009-07-11 22:50:33 +00001405 return false;
1406
1407 SDValue Segment;
1408 getAddressOperands(AM, Base, Scale, Index, Disp, Segment);
1409 return true;
Evan Cheng51a9ed92006-02-25 10:09:08 +00001410}
1411
Chris Lattner5c0b16d2009-06-20 20:38:48 +00001412/// SelectTLSADDRAddr - This is only run on TargetGlobalTLSAddress nodes.
1413bool X86DAGToDAGISel::SelectTLSADDRAddr(SDValue Op, SDValue N, SDValue &Base,
1414 SDValue &Scale, SDValue &Index,
1415 SDValue &Disp) {
1416 assert(Op.getOpcode() == X86ISD::TLSADDR);
1417 assert(N.getOpcode() == ISD::TargetGlobalTLSAddress);
1418 const GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N);
1419
1420 X86ISelAddressMode AM;
1421 AM.GV = GA->getGlobal();
1422 AM.Disp += GA->getOffset();
1423 AM.Base.Reg = CurDAG->getRegister(0, N.getValueType());
Chris Lattnerba8ef452009-06-26 21:18:37 +00001424 AM.SymbolFlags = GA->getTargetFlags();
1425
Owen Anderson825b72b2009-08-11 20:47:22 +00001426 if (N.getValueType() == MVT::i32) {
Chris Lattner5c0b16d2009-06-20 20:38:48 +00001427 AM.Scale = 1;
Owen Anderson825b72b2009-08-11 20:47:22 +00001428 AM.IndexReg = CurDAG->getRegister(X86::EBX, MVT::i32);
Chris Lattner5c0b16d2009-06-20 20:38:48 +00001429 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +00001430 AM.IndexReg = CurDAG->getRegister(0, MVT::i64);
Chris Lattner5c0b16d2009-06-20 20:38:48 +00001431 }
1432
1433 SDValue Segment;
1434 getAddressOperands(AM, Base, Scale, Index, Disp, Segment);
1435 return true;
1436}
1437
1438
Dan Gohman475871a2008-07-27 21:46:04 +00001439bool X86DAGToDAGISel::TryFoldLoad(SDValue P, SDValue N,
1440 SDValue &Base, SDValue &Scale,
Rafael Espindola094fad32009-04-08 21:14:34 +00001441 SDValue &Index, SDValue &Disp,
1442 SDValue &Segment) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001443 if (ISD::isNON_EXTLoad(N.getNode()) &&
Evan Cheng5e351682006-02-06 06:02:33 +00001444 N.hasOneUse() &&
Evan Cheng884c70c2008-11-27 00:49:46 +00001445 IsLegalAndProfitableToFold(N.getNode(), P.getNode(), P.getNode()))
Rafael Espindola094fad32009-04-08 21:14:34 +00001446 return SelectAddr(P, N.getOperand(1), Base, Scale, Index, Disp, Segment);
Evan Cheng0114e942006-01-06 20:36:21 +00001447 return false;
1448}
1449
Dan Gohman8b746962008-09-23 18:22:58 +00001450/// getGlobalBaseReg - Return an SDNode that returns the value of
1451/// the global base register. Output instructions required to
1452/// initialize the global base register, if necessary.
Evan Cheng7ccced62006-02-18 00:15:05 +00001453///
Evan Cheng9ade2182006-08-26 05:34:46 +00001454SDNode *X86DAGToDAGISel::getGlobalBaseReg() {
Dan Gohmanc5534622009-06-03 20:20:00 +00001455 unsigned GlobalBaseReg = getInstrInfo()->getGlobalBaseReg(MF);
Gabor Greifba36cb52008-08-28 21:40:38 +00001456 return CurDAG->getRegister(GlobalBaseReg, TLI.getPointerTy()).getNode();
Evan Cheng7ccced62006-02-18 00:15:05 +00001457}
1458
Evan Chengb245d922006-05-20 01:36:52 +00001459static SDNode *FindCallStartFromCall(SDNode *Node) {
1460 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
Owen Anderson825b72b2009-08-11 20:47:22 +00001461 assert(Node->getOperand(0).getValueType() == MVT::Other &&
Evan Chengb245d922006-05-20 01:36:52 +00001462 "Node doesn't have a token chain argument!");
Gabor Greifba36cb52008-08-28 21:40:38 +00001463 return FindCallStartFromCall(Node->getOperand(0).getNode());
Evan Chengb245d922006-05-20 01:36:52 +00001464}
1465
Dale Johannesen48c1bc22008-10-02 18:53:47 +00001466SDNode *X86DAGToDAGISel::SelectAtomic64(SDNode *Node, unsigned Opc) {
1467 SDValue Chain = Node->getOperand(0);
1468 SDValue In1 = Node->getOperand(1);
1469 SDValue In2L = Node->getOperand(2);
1470 SDValue In2H = Node->getOperand(3);
Rafael Espindola094fad32009-04-08 21:14:34 +00001471 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4;
1472 if (!SelectAddr(In1, In1, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4))
Dale Johannesen48c1bc22008-10-02 18:53:47 +00001473 return NULL;
Dan Gohmanc76909a2009-09-25 20:36:54 +00001474 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
1475 MemOp[0] = cast<MemSDNode>(Node)->getMemOperand();
1476 const SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, In2L, In2H, Chain};
1477 SDNode *ResNode = CurDAG->getMachineNode(Opc, Node->getDebugLoc(),
1478 MVT::i32, MVT::i32, MVT::Other, Ops,
1479 array_lengthof(Ops));
1480 cast<MachineSDNode>(ResNode)->setMemRefs(MemOp, MemOp + 1);
1481 return ResNode;
Dale Johannesen48c1bc22008-10-02 18:53:47 +00001482}
Christopher Lambc59e5212007-08-10 21:48:46 +00001483
Owen Andersone50ed302009-08-10 22:56:29 +00001484SDNode *X86DAGToDAGISel::SelectAtomicLoadAdd(SDNode *Node, EVT NVT) {
Evan Cheng37b73872009-07-30 08:33:02 +00001485 if (Node->hasAnyUseOfValue(0))
1486 return 0;
1487
1488 // Optimize common patterns for __sync_add_and_fetch and
1489 // __sync_sub_and_fetch where the result is not used. This allows us
1490 // to use "lock" version of add, sub, inc, dec instructions.
1491 // FIXME: Do not use special instructions but instead add the "lock"
1492 // prefix to the target node somehow. The extra information will then be
1493 // transferred to machine instruction and it denotes the prefix.
1494 SDValue Chain = Node->getOperand(0);
1495 SDValue Ptr = Node->getOperand(1);
1496 SDValue Val = Node->getOperand(2);
1497 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4;
1498 if (!SelectAddr(Ptr, Ptr, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4))
1499 return 0;
1500
1501 bool isInc = false, isDec = false, isSub = false, isCN = false;
1502 ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Val);
1503 if (CN) {
1504 isCN = true;
1505 int64_t CNVal = CN->getSExtValue();
1506 if (CNVal == 1)
1507 isInc = true;
1508 else if (CNVal == -1)
1509 isDec = true;
1510 else if (CNVal >= 0)
1511 Val = CurDAG->getTargetConstant(CNVal, NVT);
1512 else {
1513 isSub = true;
1514 Val = CurDAG->getTargetConstant(-CNVal, NVT);
1515 }
1516 } else if (Val.hasOneUse() &&
1517 Val.getOpcode() == ISD::SUB &&
1518 X86::isZeroNode(Val.getOperand(0))) {
1519 isSub = true;
1520 Val = Val.getOperand(1);
1521 }
1522
1523 unsigned Opc = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00001524 switch (NVT.getSimpleVT().SimpleTy) {
Evan Cheng37b73872009-07-30 08:33:02 +00001525 default: return 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00001526 case MVT::i8:
Evan Cheng37b73872009-07-30 08:33:02 +00001527 if (isInc)
1528 Opc = X86::LOCK_INC8m;
1529 else if (isDec)
1530 Opc = X86::LOCK_DEC8m;
1531 else if (isSub) {
1532 if (isCN)
1533 Opc = X86::LOCK_SUB8mi;
1534 else
1535 Opc = X86::LOCK_SUB8mr;
1536 } else {
1537 if (isCN)
1538 Opc = X86::LOCK_ADD8mi;
1539 else
1540 Opc = X86::LOCK_ADD8mr;
1541 }
1542 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001543 case MVT::i16:
Evan Cheng37b73872009-07-30 08:33:02 +00001544 if (isInc)
1545 Opc = X86::LOCK_INC16m;
1546 else if (isDec)
1547 Opc = X86::LOCK_DEC16m;
1548 else if (isSub) {
1549 if (isCN) {
1550 if (Predicate_i16immSExt8(Val.getNode()))
1551 Opc = X86::LOCK_SUB16mi8;
1552 else
1553 Opc = X86::LOCK_SUB16mi;
1554 } else
1555 Opc = X86::LOCK_SUB16mr;
1556 } else {
1557 if (isCN) {
1558 if (Predicate_i16immSExt8(Val.getNode()))
1559 Opc = X86::LOCK_ADD16mi8;
1560 else
1561 Opc = X86::LOCK_ADD16mi;
1562 } else
1563 Opc = X86::LOCK_ADD16mr;
1564 }
1565 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001566 case MVT::i32:
Evan Cheng37b73872009-07-30 08:33:02 +00001567 if (isInc)
1568 Opc = X86::LOCK_INC32m;
1569 else if (isDec)
1570 Opc = X86::LOCK_DEC32m;
1571 else if (isSub) {
1572 if (isCN) {
1573 if (Predicate_i32immSExt8(Val.getNode()))
1574 Opc = X86::LOCK_SUB32mi8;
1575 else
1576 Opc = X86::LOCK_SUB32mi;
1577 } else
1578 Opc = X86::LOCK_SUB32mr;
1579 } else {
1580 if (isCN) {
1581 if (Predicate_i32immSExt8(Val.getNode()))
1582 Opc = X86::LOCK_ADD32mi8;
1583 else
1584 Opc = X86::LOCK_ADD32mi;
1585 } else
1586 Opc = X86::LOCK_ADD32mr;
1587 }
1588 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001589 case MVT::i64:
Evan Cheng37b73872009-07-30 08:33:02 +00001590 if (isInc)
1591 Opc = X86::LOCK_INC64m;
1592 else if (isDec)
1593 Opc = X86::LOCK_DEC64m;
1594 else if (isSub) {
1595 Opc = X86::LOCK_SUB64mr;
1596 if (isCN) {
1597 if (Predicate_i64immSExt8(Val.getNode()))
1598 Opc = X86::LOCK_SUB64mi8;
1599 else if (Predicate_i64immSExt32(Val.getNode()))
1600 Opc = X86::LOCK_SUB64mi32;
1601 }
1602 } else {
1603 Opc = X86::LOCK_ADD64mr;
1604 if (isCN) {
1605 if (Predicate_i64immSExt8(Val.getNode()))
1606 Opc = X86::LOCK_ADD64mi8;
1607 else if (Predicate_i64immSExt32(Val.getNode()))
1608 Opc = X86::LOCK_ADD64mi32;
1609 }
1610 }
1611 break;
1612 }
1613
1614 DebugLoc dl = Node->getDebugLoc();
Dan Gohman602b0c82009-09-25 18:54:59 +00001615 SDValue Undef = SDValue(CurDAG->getMachineNode(TargetInstrInfo::IMPLICIT_DEF,
1616 dl, NVT), 0);
Dan Gohmanc76909a2009-09-25 20:36:54 +00001617 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
1618 MemOp[0] = cast<MemSDNode>(Node)->getMemOperand();
Evan Cheng37b73872009-07-30 08:33:02 +00001619 if (isInc || isDec) {
Dan Gohmanc76909a2009-09-25 20:36:54 +00001620 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, Chain };
1621 SDValue Ret = SDValue(CurDAG->getMachineNode(Opc, dl, MVT::Other, Ops, 6), 0);
1622 cast<MachineSDNode>(Ret)->setMemRefs(MemOp, MemOp + 1);
Evan Cheng37b73872009-07-30 08:33:02 +00001623 SDValue RetVals[] = { Undef, Ret };
1624 return CurDAG->getMergeValues(RetVals, 2, dl).getNode();
1625 } else {
Dan Gohmanc76909a2009-09-25 20:36:54 +00001626 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, Val, Chain };
1627 SDValue Ret = SDValue(CurDAG->getMachineNode(Opc, dl, MVT::Other, Ops, 7), 0);
1628 cast<MachineSDNode>(Ret)->setMemRefs(MemOp, MemOp + 1);
Evan Cheng37b73872009-07-30 08:33:02 +00001629 SDValue RetVals[] = { Undef, Ret };
1630 return CurDAG->getMergeValues(RetVals, 2, dl).getNode();
1631 }
1632}
1633
Dan Gohman11596ed2009-10-09 20:35:19 +00001634/// HasNoSignedComparisonUses - Test whether the given X86ISD::CMP node has
1635/// any uses which require the SF or OF bits to be accurate.
1636static bool HasNoSignedComparisonUses(SDNode *N) {
1637 // Examine each user of the node.
1638 for (SDNode::use_iterator UI = N->use_begin(),
1639 UE = N->use_end(); UI != UE; ++UI) {
1640 // Only examine CopyToReg uses.
1641 if (UI->getOpcode() != ISD::CopyToReg)
1642 return false;
1643 // Only examine CopyToReg uses that copy to EFLAGS.
1644 if (cast<RegisterSDNode>(UI->getOperand(1))->getReg() !=
1645 X86::EFLAGS)
1646 return false;
1647 // Examine each user of the CopyToReg use.
1648 for (SDNode::use_iterator FlagUI = UI->use_begin(),
1649 FlagUE = UI->use_end(); FlagUI != FlagUE; ++FlagUI) {
1650 // Only examine the Flag result.
1651 if (FlagUI.getUse().getResNo() != 1) continue;
1652 // Anything unusual: assume conservatively.
1653 if (!FlagUI->isMachineOpcode()) return false;
1654 // Examine the opcode of the user.
1655 switch (FlagUI->getMachineOpcode()) {
1656 // These comparisons don't treat the most significant bit specially.
1657 case X86::SETAr: case X86::SETAEr: case X86::SETBr: case X86::SETBEr:
1658 case X86::SETEr: case X86::SETNEr: case X86::SETPr: case X86::SETNPr:
1659 case X86::SETAm: case X86::SETAEm: case X86::SETBm: case X86::SETBEm:
1660 case X86::SETEm: case X86::SETNEm: case X86::SETPm: case X86::SETNPm:
1661 case X86::JA: case X86::JAE: case X86::JB: case X86::JBE:
1662 case X86::JE: case X86::JNE: case X86::JP: case X86::JNP:
1663 case X86::CMOVA16rr: case X86::CMOVA16rm:
1664 case X86::CMOVA32rr: case X86::CMOVA32rm:
1665 case X86::CMOVA64rr: case X86::CMOVA64rm:
1666 case X86::CMOVAE16rr: case X86::CMOVAE16rm:
1667 case X86::CMOVAE32rr: case X86::CMOVAE32rm:
1668 case X86::CMOVAE64rr: case X86::CMOVAE64rm:
1669 case X86::CMOVB16rr: case X86::CMOVB16rm:
1670 case X86::CMOVB32rr: case X86::CMOVB32rm:
1671 case X86::CMOVB64rr: case X86::CMOVB64rm:
1672 case X86::CMOVBE16rr: case X86::CMOVBE16rm:
1673 case X86::CMOVBE32rr: case X86::CMOVBE32rm:
1674 case X86::CMOVBE64rr: case X86::CMOVBE64rm:
1675 case X86::CMOVE16rr: case X86::CMOVE16rm:
1676 case X86::CMOVE32rr: case X86::CMOVE32rm:
1677 case X86::CMOVE64rr: case X86::CMOVE64rm:
1678 case X86::CMOVNE16rr: case X86::CMOVNE16rm:
1679 case X86::CMOVNE32rr: case X86::CMOVNE32rm:
1680 case X86::CMOVNE64rr: case X86::CMOVNE64rm:
1681 case X86::CMOVNP16rr: case X86::CMOVNP16rm:
1682 case X86::CMOVNP32rr: case X86::CMOVNP32rm:
1683 case X86::CMOVNP64rr: case X86::CMOVNP64rm:
1684 case X86::CMOVP16rr: case X86::CMOVP16rm:
1685 case X86::CMOVP32rr: case X86::CMOVP32rm:
1686 case X86::CMOVP64rr: case X86::CMOVP64rm:
1687 continue;
1688 // Anything else: assume conservatively.
1689 default: return false;
1690 }
1691 }
1692 }
1693 return true;
1694}
1695
Dan Gohman475871a2008-07-27 21:46:04 +00001696SDNode *X86DAGToDAGISel::Select(SDValue N) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001697 SDNode *Node = N.getNode();
Owen Andersone50ed302009-08-10 22:56:29 +00001698 EVT NVT = Node->getValueType(0);
Evan Cheng0114e942006-01-06 20:36:21 +00001699 unsigned Opc, MOpc;
1700 unsigned Opcode = Node->getOpcode();
Dale Johannesend8392542009-02-03 21:48:12 +00001701 DebugLoc dl = Node->getDebugLoc();
1702
Evan Chengf597dc72006-02-10 22:24:32 +00001703#ifndef NDEBUG
Bill Wendling12321672009-08-07 21:33:25 +00001704 DEBUG({
1705 errs() << std::string(Indent, ' ') << "Selecting: ";
1706 Node->dump(CurDAG);
1707 errs() << '\n';
1708 });
Evan Cheng23addc02006-02-10 22:46:26 +00001709 Indent += 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001710#endif
1711
Dan Gohmane8be6c62008-07-17 19:10:17 +00001712 if (Node->isMachineOpcode()) {
Evan Chengf597dc72006-02-10 22:24:32 +00001713#ifndef NDEBUG
Bill Wendling12321672009-08-07 21:33:25 +00001714 DEBUG({
1715 errs() << std::string(Indent-2, ' ') << "== ";
1716 Node->dump(CurDAG);
1717 errs() << '\n';
1718 });
Evan Cheng23addc02006-02-10 22:46:26 +00001719 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00001720#endif
Evan Cheng64a752f2006-08-11 09:08:15 +00001721 return NULL; // Already selected.
Evan Cheng34167212006-02-09 00:37:58 +00001722 }
Evan Cheng38262ca2006-01-11 22:15:18 +00001723
Evan Cheng0114e942006-01-06 20:36:21 +00001724 switch (Opcode) {
Dan Gohman72677342009-08-02 16:10:52 +00001725 default: break;
1726 case X86ISD::GlobalBaseReg:
1727 return getGlobalBaseReg();
Evan Cheng020d2e82006-02-23 20:41:18 +00001728
Dan Gohman72677342009-08-02 16:10:52 +00001729 case X86ISD::ATOMOR64_DAG:
1730 return SelectAtomic64(Node, X86::ATOMOR6432);
1731 case X86ISD::ATOMXOR64_DAG:
1732 return SelectAtomic64(Node, X86::ATOMXOR6432);
1733 case X86ISD::ATOMADD64_DAG:
1734 return SelectAtomic64(Node, X86::ATOMADD6432);
1735 case X86ISD::ATOMSUB64_DAG:
1736 return SelectAtomic64(Node, X86::ATOMSUB6432);
1737 case X86ISD::ATOMNAND64_DAG:
1738 return SelectAtomic64(Node, X86::ATOMNAND6432);
1739 case X86ISD::ATOMAND64_DAG:
1740 return SelectAtomic64(Node, X86::ATOMAND6432);
1741 case X86ISD::ATOMSWAP64_DAG:
1742 return SelectAtomic64(Node, X86::ATOMSWAP6432);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00001743
Dan Gohman72677342009-08-02 16:10:52 +00001744 case ISD::ATOMIC_LOAD_ADD: {
1745 SDNode *RetVal = SelectAtomicLoadAdd(Node, NVT);
1746 if (RetVal)
1747 return RetVal;
1748 break;
1749 }
1750
1751 case ISD::SMUL_LOHI:
1752 case ISD::UMUL_LOHI: {
1753 SDValue N0 = Node->getOperand(0);
1754 SDValue N1 = Node->getOperand(1);
1755
1756 bool isSigned = Opcode == ISD::SMUL_LOHI;
Bill Wendling12321672009-08-07 21:33:25 +00001757 if (!isSigned) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001758 switch (NVT.getSimpleVT().SimpleTy) {
Dan Gohman72677342009-08-02 16:10:52 +00001759 default: llvm_unreachable("Unsupported VT!");
Owen Anderson825b72b2009-08-11 20:47:22 +00001760 case MVT::i8: Opc = X86::MUL8r; MOpc = X86::MUL8m; break;
1761 case MVT::i16: Opc = X86::MUL16r; MOpc = X86::MUL16m; break;
1762 case MVT::i32: Opc = X86::MUL32r; MOpc = X86::MUL32m; break;
1763 case MVT::i64: Opc = X86::MUL64r; MOpc = X86::MUL64m; break;
Dan Gohman72677342009-08-02 16:10:52 +00001764 }
Bill Wendling12321672009-08-07 21:33:25 +00001765 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +00001766 switch (NVT.getSimpleVT().SimpleTy) {
Dan Gohman72677342009-08-02 16:10:52 +00001767 default: llvm_unreachable("Unsupported VT!");
Owen Anderson825b72b2009-08-11 20:47:22 +00001768 case MVT::i8: Opc = X86::IMUL8r; MOpc = X86::IMUL8m; break;
1769 case MVT::i16: Opc = X86::IMUL16r; MOpc = X86::IMUL16m; break;
1770 case MVT::i32: Opc = X86::IMUL32r; MOpc = X86::IMUL32m; break;
1771 case MVT::i64: Opc = X86::IMUL64r; MOpc = X86::IMUL64m; break;
Dan Gohman72677342009-08-02 16:10:52 +00001772 }
Bill Wendling12321672009-08-07 21:33:25 +00001773 }
Dan Gohman72677342009-08-02 16:10:52 +00001774
1775 unsigned LoReg, HiReg;
Owen Anderson825b72b2009-08-11 20:47:22 +00001776 switch (NVT.getSimpleVT().SimpleTy) {
Dan Gohman72677342009-08-02 16:10:52 +00001777 default: llvm_unreachable("Unsupported VT!");
Owen Anderson825b72b2009-08-11 20:47:22 +00001778 case MVT::i8: LoReg = X86::AL; HiReg = X86::AH; break;
1779 case MVT::i16: LoReg = X86::AX; HiReg = X86::DX; break;
1780 case MVT::i32: LoReg = X86::EAX; HiReg = X86::EDX; break;
1781 case MVT::i64: LoReg = X86::RAX; HiReg = X86::RDX; break;
Dan Gohman72677342009-08-02 16:10:52 +00001782 }
1783
1784 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4;
1785 bool foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4);
Bill Wendling12321672009-08-07 21:33:25 +00001786 // Multiply is commmutative.
Dan Gohman72677342009-08-02 16:10:52 +00001787 if (!foldedLoad) {
1788 foldedLoad = TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4);
1789 if (foldedLoad)
1790 std::swap(N0, N1);
1791 }
1792
1793 SDValue InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, LoReg,
1794 N0, SDValue()).getValue(1);
1795
1796 if (foldedLoad) {
1797 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, N1.getOperand(0),
1798 InFlag };
1799 SDNode *CNode =
Dan Gohman602b0c82009-09-25 18:54:59 +00001800 CurDAG->getMachineNode(MOpc, dl, MVT::Other, MVT::Flag, Ops,
1801 array_lengthof(Ops));
Dan Gohman72677342009-08-02 16:10:52 +00001802 InFlag = SDValue(CNode, 1);
1803 // Update the chain.
1804 ReplaceUses(N1.getValue(1), SDValue(CNode, 0));
1805 } else {
1806 InFlag =
Dan Gohman602b0c82009-09-25 18:54:59 +00001807 SDValue(CurDAG->getMachineNode(Opc, dl, MVT::Flag, N1, InFlag), 0);
Dan Gohman72677342009-08-02 16:10:52 +00001808 }
1809
1810 // Copy the low half of the result, if it is needed.
1811 if (!N.getValue(0).use_empty()) {
1812 SDValue Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
1813 LoReg, NVT, InFlag);
1814 InFlag = Result.getValue(2);
1815 ReplaceUses(N.getValue(0), Result);
1816#ifndef NDEBUG
Bill Wendling12321672009-08-07 21:33:25 +00001817 DEBUG({
1818 errs() << std::string(Indent-2, ' ') << "=> ";
1819 Result.getNode()->dump(CurDAG);
1820 errs() << '\n';
1821 });
Dan Gohman72677342009-08-02 16:10:52 +00001822#endif
1823 }
1824 // Copy the high half of the result, if it is needed.
1825 if (!N.getValue(1).use_empty()) {
1826 SDValue Result;
1827 if (HiReg == X86::AH && Subtarget->is64Bit()) {
1828 // Prevent use of AH in a REX instruction by referencing AX instead.
1829 // Shift it down 8 bits.
1830 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001831 X86::AX, MVT::i16, InFlag);
Dan Gohman72677342009-08-02 16:10:52 +00001832 InFlag = Result.getValue(2);
Dan Gohman602b0c82009-09-25 18:54:59 +00001833 Result = SDValue(CurDAG->getMachineNode(X86::SHR16ri, dl, MVT::i16,
1834 Result,
Owen Anderson825b72b2009-08-11 20:47:22 +00001835 CurDAG->getTargetConstant(8, MVT::i8)), 0);
Dan Gohman72677342009-08-02 16:10:52 +00001836 // Then truncate it down to i8.
Dan Gohman6a402dc2009-08-19 18:16:17 +00001837 Result = CurDAG->getTargetExtractSubreg(X86::SUBREG_8BIT, dl,
1838 MVT::i8, Result);
Dan Gohman72677342009-08-02 16:10:52 +00001839 } else {
1840 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
1841 HiReg, NVT, InFlag);
1842 InFlag = Result.getValue(2);
1843 }
1844 ReplaceUses(N.getValue(1), Result);
1845#ifndef NDEBUG
Bill Wendling12321672009-08-07 21:33:25 +00001846 DEBUG({
1847 errs() << std::string(Indent-2, ' ') << "=> ";
1848 Result.getNode()->dump(CurDAG);
1849 errs() << '\n';
1850 });
Dan Gohman72677342009-08-02 16:10:52 +00001851#endif
1852 }
1853
1854#ifndef NDEBUG
1855 Indent -= 2;
1856#endif
1857
1858 return NULL;
1859 }
1860
1861 case ISD::SDIVREM:
1862 case ISD::UDIVREM: {
1863 SDValue N0 = Node->getOperand(0);
1864 SDValue N1 = Node->getOperand(1);
1865
1866 bool isSigned = Opcode == ISD::SDIVREM;
Bill Wendling12321672009-08-07 21:33:25 +00001867 if (!isSigned) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001868 switch (NVT.getSimpleVT().SimpleTy) {
Dan Gohman72677342009-08-02 16:10:52 +00001869 default: llvm_unreachable("Unsupported VT!");
Owen Anderson825b72b2009-08-11 20:47:22 +00001870 case MVT::i8: Opc = X86::DIV8r; MOpc = X86::DIV8m; break;
1871 case MVT::i16: Opc = X86::DIV16r; MOpc = X86::DIV16m; break;
1872 case MVT::i32: Opc = X86::DIV32r; MOpc = X86::DIV32m; break;
1873 case MVT::i64: Opc = X86::DIV64r; MOpc = X86::DIV64m; break;
Dan Gohman72677342009-08-02 16:10:52 +00001874 }
Bill Wendling12321672009-08-07 21:33:25 +00001875 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +00001876 switch (NVT.getSimpleVT().SimpleTy) {
Dan Gohman72677342009-08-02 16:10:52 +00001877 default: llvm_unreachable("Unsupported VT!");
Owen Anderson825b72b2009-08-11 20:47:22 +00001878 case MVT::i8: Opc = X86::IDIV8r; MOpc = X86::IDIV8m; break;
1879 case MVT::i16: Opc = X86::IDIV16r; MOpc = X86::IDIV16m; break;
1880 case MVT::i32: Opc = X86::IDIV32r; MOpc = X86::IDIV32m; break;
1881 case MVT::i64: Opc = X86::IDIV64r; MOpc = X86::IDIV64m; break;
Dan Gohman72677342009-08-02 16:10:52 +00001882 }
Bill Wendling12321672009-08-07 21:33:25 +00001883 }
Dan Gohman72677342009-08-02 16:10:52 +00001884
1885 unsigned LoReg, HiReg;
1886 unsigned ClrOpcode, SExtOpcode;
Owen Anderson825b72b2009-08-11 20:47:22 +00001887 switch (NVT.getSimpleVT().SimpleTy) {
Dan Gohman72677342009-08-02 16:10:52 +00001888 default: llvm_unreachable("Unsupported VT!");
Owen Anderson825b72b2009-08-11 20:47:22 +00001889 case MVT::i8:
Dan Gohman72677342009-08-02 16:10:52 +00001890 LoReg = X86::AL; HiReg = X86::AH;
1891 ClrOpcode = 0;
1892 SExtOpcode = X86::CBW;
1893 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001894 case MVT::i16:
Dan Gohman72677342009-08-02 16:10:52 +00001895 LoReg = X86::AX; HiReg = X86::DX;
1896 ClrOpcode = X86::MOV16r0;
1897 SExtOpcode = X86::CWD;
1898 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001899 case MVT::i32:
Dan Gohman72677342009-08-02 16:10:52 +00001900 LoReg = X86::EAX; HiReg = X86::EDX;
1901 ClrOpcode = X86::MOV32r0;
1902 SExtOpcode = X86::CDQ;
1903 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00001904 case MVT::i64:
Dan Gohman72677342009-08-02 16:10:52 +00001905 LoReg = X86::RAX; HiReg = X86::RDX;
1906 ClrOpcode = ~0U; // NOT USED.
1907 SExtOpcode = X86::CQO;
Evan Cheng37b73872009-07-30 08:33:02 +00001908 break;
1909 }
1910
Dan Gohman72677342009-08-02 16:10:52 +00001911 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4;
1912 bool foldedLoad = TryFoldLoad(N, N1, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4);
1913 bool signBitIsZero = CurDAG->SignBitIsZero(N0);
Dan Gohman525178c2007-10-08 18:33:35 +00001914
Dan Gohman72677342009-08-02 16:10:52 +00001915 SDValue InFlag;
Owen Anderson825b72b2009-08-11 20:47:22 +00001916 if (NVT == MVT::i8 && (!isSigned || signBitIsZero)) {
Dan Gohman72677342009-08-02 16:10:52 +00001917 // Special case for div8, just use a move with zero extension to AX to
1918 // clear the upper 8 bits (AH).
1919 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, Move, Chain;
1920 if (TryFoldLoad(N, N0, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4)) {
1921 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, N0.getOperand(0) };
1922 Move =
Dan Gohman602b0c82009-09-25 18:54:59 +00001923 SDValue(CurDAG->getMachineNode(X86::MOVZX16rm8, dl, MVT::i16,
1924 MVT::Other, Ops,
1925 array_lengthof(Ops)), 0);
Dan Gohman72677342009-08-02 16:10:52 +00001926 Chain = Move.getValue(1);
1927 ReplaceUses(N0.getValue(1), Chain);
Evan Cheng0114e942006-01-06 20:36:21 +00001928 } else {
Dan Gohman72677342009-08-02 16:10:52 +00001929 Move =
Dan Gohman602b0c82009-09-25 18:54:59 +00001930 SDValue(CurDAG->getMachineNode(X86::MOVZX16rr8, dl, MVT::i16, N0),0);
Dan Gohman72677342009-08-02 16:10:52 +00001931 Chain = CurDAG->getEntryNode();
1932 }
1933 Chain = CurDAG->getCopyToReg(Chain, dl, X86::AX, Move, SDValue());
1934 InFlag = Chain.getValue(1);
1935 } else {
1936 InFlag =
1937 CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl,
1938 LoReg, N0, SDValue()).getValue(1);
1939 if (isSigned && !signBitIsZero) {
1940 // Sign extend the low part into the high part.
Evan Cheng7e9b26f2006-02-09 07:17:49 +00001941 InFlag =
Dan Gohman602b0c82009-09-25 18:54:59 +00001942 SDValue(CurDAG->getMachineNode(SExtOpcode, dl, MVT::Flag, InFlag),0);
Dan Gohman72677342009-08-02 16:10:52 +00001943 } else {
1944 // Zero out the high part, effectively zero extending the input.
1945 SDValue ClrNode;
Evan Cheng0114e942006-01-06 20:36:21 +00001946
Owen Anderson825b72b2009-08-11 20:47:22 +00001947 if (NVT.getSimpleVT() == MVT::i64) {
Dan Gohman602b0c82009-09-25 18:54:59 +00001948 ClrNode = SDValue(CurDAG->getMachineNode(X86::MOV32r0, dl, MVT::i32),
Dan Gohman72677342009-08-02 16:10:52 +00001949 0);
1950 // We just did a 32-bit clear, insert it into a 64-bit register to
1951 // clear the whole 64-bit reg.
Dan Gohman7289ed22009-11-05 23:53:08 +00001952 SDValue Zero = CurDAG->getTargetConstant(0, MVT::i64);
Dan Gohman72677342009-08-02 16:10:52 +00001953 SDValue SubRegNo =
Owen Anderson825b72b2009-08-11 20:47:22 +00001954 CurDAG->getTargetConstant(X86::SUBREG_32BIT, MVT::i32);
Dan Gohman72677342009-08-02 16:10:52 +00001955 ClrNode =
Dan Gohman7289ed22009-11-05 23:53:08 +00001956 SDValue(CurDAG->getMachineNode(TargetInstrInfo::SUBREG_TO_REG, dl,
1957 MVT::i64, Zero, ClrNode, SubRegNo),
Dan Gohman72677342009-08-02 16:10:52 +00001958 0);
Dan Gohman525178c2007-10-08 18:33:35 +00001959 } else {
Dan Gohman602b0c82009-09-25 18:54:59 +00001960 ClrNode = SDValue(CurDAG->getMachineNode(ClrOpcode, dl, NVT), 0);
Dan Gohman525178c2007-10-08 18:33:35 +00001961 }
Dan Gohman72677342009-08-02 16:10:52 +00001962
1963 InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, HiReg,
1964 ClrNode, InFlag).getValue(1);
Dan Gohman525178c2007-10-08 18:33:35 +00001965 }
Evan Cheng948f3432006-01-06 23:19:29 +00001966 }
Dan Gohman525178c2007-10-08 18:33:35 +00001967
Dan Gohman72677342009-08-02 16:10:52 +00001968 if (foldedLoad) {
1969 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, N1.getOperand(0),
1970 InFlag };
1971 SDNode *CNode =
Dan Gohman602b0c82009-09-25 18:54:59 +00001972 CurDAG->getMachineNode(MOpc, dl, MVT::Other, MVT::Flag, Ops,
1973 array_lengthof(Ops));
Dan Gohman72677342009-08-02 16:10:52 +00001974 InFlag = SDValue(CNode, 1);
1975 // Update the chain.
1976 ReplaceUses(N1.getValue(1), SDValue(CNode, 0));
1977 } else {
1978 InFlag =
Dan Gohman602b0c82009-09-25 18:54:59 +00001979 SDValue(CurDAG->getMachineNode(Opc, dl, MVT::Flag, N1, InFlag), 0);
Dan Gohman72677342009-08-02 16:10:52 +00001980 }
Evan Cheng948f3432006-01-06 23:19:29 +00001981
Dan Gohman72677342009-08-02 16:10:52 +00001982 // Copy the division (low) result, if it is needed.
1983 if (!N.getValue(0).use_empty()) {
1984 SDValue Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
1985 LoReg, NVT, InFlag);
1986 InFlag = Result.getValue(2);
1987 ReplaceUses(N.getValue(0), Result);
1988#ifndef NDEBUG
Bill Wendling12321672009-08-07 21:33:25 +00001989 DEBUG({
1990 errs() << std::string(Indent-2, ' ') << "=> ";
1991 Result.getNode()->dump(CurDAG);
1992 errs() << '\n';
1993 });
Dan Gohman72677342009-08-02 16:10:52 +00001994#endif
1995 }
1996 // Copy the remainder (high) result, if it is needed.
1997 if (!N.getValue(1).use_empty()) {
1998 SDValue Result;
1999 if (HiReg == X86::AH && Subtarget->is64Bit()) {
2000 // Prevent use of AH in a REX instruction by referencing AX instead.
2001 // Shift it down 8 bits.
2002 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00002003 X86::AX, MVT::i16, InFlag);
Dan Gohmana37c9f72007-09-25 18:23:27 +00002004 InFlag = Result.getValue(2);
Dan Gohman602b0c82009-09-25 18:54:59 +00002005 Result = SDValue(CurDAG->getMachineNode(X86::SHR16ri, dl, MVT::i16,
Dan Gohman72677342009-08-02 16:10:52 +00002006 Result,
Owen Anderson825b72b2009-08-11 20:47:22 +00002007 CurDAG->getTargetConstant(8, MVT::i8)),
Dan Gohman72677342009-08-02 16:10:52 +00002008 0);
2009 // Then truncate it down to i8.
Dan Gohman6a402dc2009-08-19 18:16:17 +00002010 Result = CurDAG->getTargetExtractSubreg(X86::SUBREG_8BIT, dl,
2011 MVT::i8, Result);
Dan Gohman72677342009-08-02 16:10:52 +00002012 } else {
2013 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
2014 HiReg, NVT, InFlag);
2015 InFlag = Result.getValue(2);
Evan Chengf7ef26e2007-08-09 21:59:35 +00002016 }
Dan Gohman72677342009-08-02 16:10:52 +00002017 ReplaceUses(N.getValue(1), Result);
Dan Gohmana37c9f72007-09-25 18:23:27 +00002018#ifndef NDEBUG
Bill Wendling12321672009-08-07 21:33:25 +00002019 DEBUG({
2020 errs() << std::string(Indent-2, ' ') << "=> ";
2021 Result.getNode()->dump(CurDAG);
2022 errs() << '\n';
2023 });
Dan Gohmana37c9f72007-09-25 18:23:27 +00002024#endif
Dan Gohman72677342009-08-02 16:10:52 +00002025 }
Evan Chengf597dc72006-02-10 22:24:32 +00002026
2027#ifndef NDEBUG
Dan Gohman72677342009-08-02 16:10:52 +00002028 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00002029#endif
Evan Cheng64a752f2006-08-11 09:08:15 +00002030
Dan Gohman72677342009-08-02 16:10:52 +00002031 return NULL;
2032 }
2033
Dan Gohman6a402dc2009-08-19 18:16:17 +00002034 case X86ISD::CMP: {
Dan Gohman6a402dc2009-08-19 18:16:17 +00002035 SDValue N0 = Node->getOperand(0);
2036 SDValue N1 = Node->getOperand(1);
2037
2038 // Look for (X86cmp (and $op, $imm), 0) and see if we can convert it to
2039 // use a smaller encoding.
2040 if (N0.getNode()->getOpcode() == ISD::AND && N0.getNode()->hasOneUse() &&
2041 N0.getValueType() != MVT::i8 &&
2042 X86::isZeroNode(N1)) {
2043 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getNode()->getOperand(1));
2044 if (!C) break;
2045
2046 // For example, convert "testl %eax, $8" to "testb %al, $8"
Dan Gohman11596ed2009-10-09 20:35:19 +00002047 if ((C->getZExtValue() & ~UINT64_C(0xff)) == 0 &&
2048 (!(C->getZExtValue() & 0x80) ||
2049 HasNoSignedComparisonUses(Node))) {
Dan Gohman6a402dc2009-08-19 18:16:17 +00002050 SDValue Imm = CurDAG->getTargetConstant(C->getZExtValue(), MVT::i8);
2051 SDValue Reg = N0.getNode()->getOperand(0);
2052
2053 // On x86-32, only the ABCD registers have 8-bit subregisters.
2054 if (!Subtarget->is64Bit()) {
2055 TargetRegisterClass *TRC = 0;
2056 switch (N0.getValueType().getSimpleVT().SimpleTy) {
2057 case MVT::i32: TRC = &X86::GR32_ABCDRegClass; break;
2058 case MVT::i16: TRC = &X86::GR16_ABCDRegClass; break;
2059 default: llvm_unreachable("Unsupported TEST operand type!");
2060 }
2061 SDValue RC = CurDAG->getTargetConstant(TRC->getID(), MVT::i32);
Dan Gohman602b0c82009-09-25 18:54:59 +00002062 Reg = SDValue(CurDAG->getMachineNode(X86::COPY_TO_REGCLASS, dl,
2063 Reg.getValueType(), Reg, RC), 0);
Dan Gohman6a402dc2009-08-19 18:16:17 +00002064 }
2065
2066 // Extract the l-register.
2067 SDValue Subreg = CurDAG->getTargetExtractSubreg(X86::SUBREG_8BIT, dl,
2068 MVT::i8, Reg);
2069
2070 // Emit a testb.
Dan Gohman602b0c82009-09-25 18:54:59 +00002071 return CurDAG->getMachineNode(X86::TEST8ri, dl, MVT::i32, Subreg, Imm);
Dan Gohman6a402dc2009-08-19 18:16:17 +00002072 }
2073
2074 // For example, "testl %eax, $2048" to "testb %ah, $8".
Dan Gohman11596ed2009-10-09 20:35:19 +00002075 if ((C->getZExtValue() & ~UINT64_C(0xff00)) == 0 &&
2076 (!(C->getZExtValue() & 0x8000) ||
2077 HasNoSignedComparisonUses(Node))) {
Dan Gohman6a402dc2009-08-19 18:16:17 +00002078 // Shift the immediate right by 8 bits.
2079 SDValue ShiftedImm = CurDAG->getTargetConstant(C->getZExtValue() >> 8,
2080 MVT::i8);
2081 SDValue Reg = N0.getNode()->getOperand(0);
2082
2083 // Put the value in an ABCD register.
2084 TargetRegisterClass *TRC = 0;
2085 switch (N0.getValueType().getSimpleVT().SimpleTy) {
2086 case MVT::i64: TRC = &X86::GR64_ABCDRegClass; break;
2087 case MVT::i32: TRC = &X86::GR32_ABCDRegClass; break;
2088 case MVT::i16: TRC = &X86::GR16_ABCDRegClass; break;
2089 default: llvm_unreachable("Unsupported TEST operand type!");
2090 }
2091 SDValue RC = CurDAG->getTargetConstant(TRC->getID(), MVT::i32);
Dan Gohman602b0c82009-09-25 18:54:59 +00002092 Reg = SDValue(CurDAG->getMachineNode(X86::COPY_TO_REGCLASS, dl,
2093 Reg.getValueType(), Reg, RC), 0);
Dan Gohman6a402dc2009-08-19 18:16:17 +00002094
2095 // Extract the h-register.
2096 SDValue Subreg = CurDAG->getTargetExtractSubreg(X86::SUBREG_8BIT_HI, dl,
2097 MVT::i8, Reg);
2098
2099 // Emit a testb. No special NOREX tricks are needed since there's
2100 // only one GPR operand!
Dan Gohman602b0c82009-09-25 18:54:59 +00002101 return CurDAG->getMachineNode(X86::TEST8ri, dl, MVT::i32,
2102 Subreg, ShiftedImm);
Dan Gohman6a402dc2009-08-19 18:16:17 +00002103 }
2104
2105 // For example, "testl %eax, $32776" to "testw %ax, $32776".
2106 if ((C->getZExtValue() & ~UINT64_C(0xffff)) == 0 &&
Dan Gohman11596ed2009-10-09 20:35:19 +00002107 N0.getValueType() != MVT::i16 &&
2108 (!(C->getZExtValue() & 0x8000) ||
2109 HasNoSignedComparisonUses(Node))) {
Dan Gohman6a402dc2009-08-19 18:16:17 +00002110 SDValue Imm = CurDAG->getTargetConstant(C->getZExtValue(), MVT::i16);
2111 SDValue Reg = N0.getNode()->getOperand(0);
2112
2113 // Extract the 16-bit subregister.
2114 SDValue Subreg = CurDAG->getTargetExtractSubreg(X86::SUBREG_16BIT, dl,
2115 MVT::i16, Reg);
2116
2117 // Emit a testw.
Dan Gohman602b0c82009-09-25 18:54:59 +00002118 return CurDAG->getMachineNode(X86::TEST16ri, dl, MVT::i32, Subreg, Imm);
Dan Gohman6a402dc2009-08-19 18:16:17 +00002119 }
2120
2121 // For example, "testq %rax, $268468232" to "testl %eax, $268468232".
2122 if ((C->getZExtValue() & ~UINT64_C(0xffffffff)) == 0 &&
Dan Gohman11596ed2009-10-09 20:35:19 +00002123 N0.getValueType() == MVT::i64 &&
2124 (!(C->getZExtValue() & 0x80000000) ||
2125 HasNoSignedComparisonUses(Node))) {
Dan Gohman6a402dc2009-08-19 18:16:17 +00002126 SDValue Imm = CurDAG->getTargetConstant(C->getZExtValue(), MVT::i32);
2127 SDValue Reg = N0.getNode()->getOperand(0);
2128
2129 // Extract the 32-bit subregister.
2130 SDValue Subreg = CurDAG->getTargetExtractSubreg(X86::SUBREG_32BIT, dl,
2131 MVT::i32, Reg);
2132
2133 // Emit a testl.
Dan Gohman602b0c82009-09-25 18:54:59 +00002134 return CurDAG->getMachineNode(X86::TEST32ri, dl, MVT::i32, Subreg, Imm);
Dan Gohman6a402dc2009-08-19 18:16:17 +00002135 }
2136 }
2137 break;
2138 }
Chris Lattnerc961eea2005-11-16 01:54:32 +00002139 }
2140
Evan Cheng9ade2182006-08-26 05:34:46 +00002141 SDNode *ResNode = SelectCode(N);
Evan Cheng64a752f2006-08-11 09:08:15 +00002142
Evan Chengf597dc72006-02-10 22:24:32 +00002143#ifndef NDEBUG
Bill Wendling12321672009-08-07 21:33:25 +00002144 DEBUG({
2145 errs() << std::string(Indent-2, ' ') << "=> ";
2146 if (ResNode == NULL || ResNode == N.getNode())
2147 N.getNode()->dump(CurDAG);
2148 else
2149 ResNode->dump(CurDAG);
2150 errs() << '\n';
2151 });
Evan Cheng23addc02006-02-10 22:46:26 +00002152 Indent -= 2;
Evan Chengf597dc72006-02-10 22:24:32 +00002153#endif
Evan Cheng64a752f2006-08-11 09:08:15 +00002154
2155 return ResNode;
Chris Lattnerc961eea2005-11-16 01:54:32 +00002156}
2157
Chris Lattnerc0bad572006-06-08 18:03:49 +00002158bool X86DAGToDAGISel::
Dan Gohman475871a2008-07-27 21:46:04 +00002159SelectInlineAsmMemoryOperand(const SDValue &Op, char ConstraintCode,
Dan Gohmanf350b272008-08-23 02:25:05 +00002160 std::vector<SDValue> &OutOps) {
Rafael Espindola094fad32009-04-08 21:14:34 +00002161 SDValue Op0, Op1, Op2, Op3, Op4;
Chris Lattnerc0bad572006-06-08 18:03:49 +00002162 switch (ConstraintCode) {
2163 case 'o': // offsetable ??
2164 case 'v': // not offsetable ??
2165 default: return true;
2166 case 'm': // memory
Rafael Espindola094fad32009-04-08 21:14:34 +00002167 if (!SelectAddr(Op, Op, Op0, Op1, Op2, Op3, Op4))
Chris Lattnerc0bad572006-06-08 18:03:49 +00002168 return true;
2169 break;
2170 }
2171
Evan Cheng04699902006-08-26 01:05:16 +00002172 OutOps.push_back(Op0);
2173 OutOps.push_back(Op1);
2174 OutOps.push_back(Op2);
2175 OutOps.push_back(Op3);
Rafael Espindola094fad32009-04-08 21:14:34 +00002176 OutOps.push_back(Op4);
Chris Lattnerc0bad572006-06-08 18:03:49 +00002177 return false;
2178}
2179
Chris Lattnerc961eea2005-11-16 01:54:32 +00002180/// createX86ISelDag - This pass converts a legalized DAG into a
2181/// X86-specific DAG, ready for instruction scheduling.
2182///
Bill Wendling98a366d2009-04-29 23:29:43 +00002183FunctionPass *llvm::createX86ISelDag(X86TargetMachine &TM,
2184 llvm::CodeGenOpt::Level OptLevel) {
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +00002185 return new X86DAGToDAGISel(TM, OptLevel);
Chris Lattnerc961eea2005-11-16 01:54:32 +00002186}