Chris Lattner | 5930d3d | 2005-11-16 22:59:19 +0000 | [diff] [blame] | 1 | //===- X86ISelDAGToDAG.cpp - A DAG pattern matching inst selector for X86 -===// |
Chris Lattner | 655e7df | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | f3ebc3f | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Chris Lattner | 655e7df | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 7 | // |
| 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 | |
| 15 | #include "X86.h" |
Evan Cheng | bc7a0f44 | 2006-01-11 06:09:51 +0000 | [diff] [blame] | 16 | #include "X86InstrBuilder.h" |
Evan Cheng | f55b738 | 2008-01-05 00:41:47 +0000 | [diff] [blame] | 17 | #include "X86MachineFunctionInfo.h" |
Chris Lattner | 7c55126 | 2006-01-11 01:15:34 +0000 | [diff] [blame] | 18 | #include "X86RegisterInfo.h" |
Chris Lattner | 655e7df | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 19 | #include "X86Subtarget.h" |
Evan Cheng | 2dd2c65 | 2006-03-13 23:20:37 +0000 | [diff] [blame] | 20 | #include "X86TargetMachine.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/Statistic.h" |
Evan Cheng | 73a1ad9 | 2006-01-10 20:26:56 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/MachineFrameInfo.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/MachineFunction.h" |
Chris Lattner | 7c55126 | 2006-01-11 01:15:34 +0000 | [diff] [blame] | 24 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
Chris Lattner | a10fff5 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 25 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Chris Lattner | 655e7df | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 26 | #include "llvm/CodeGen/SelectionDAGISel.h" |
Eric Christopher | 79cc1e3 | 2014-09-02 22:28:02 +0000 | [diff] [blame] | 27 | #include "llvm/IR/Function.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 28 | #include "llvm/IR/Instructions.h" |
| 29 | #include "llvm/IR/Intrinsics.h" |
| 30 | #include "llvm/IR/Type.h" |
Evan Cheng | 11b0a5d | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 31 | #include "llvm/Support/Debug.h" |
Torok Edwin | fb8d6d5 | 2009-07-08 20:53:28 +0000 | [diff] [blame] | 32 | #include "llvm/Support/ErrorHandling.h" |
Evan Cheng | 11b0a5d | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 33 | #include "llvm/Support/MathExtras.h" |
Torok Edwin | fb8d6d5 | 2009-07-08 20:53:28 +0000 | [diff] [blame] | 34 | #include "llvm/Support/raw_ostream.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 35 | #include "llvm/Target/TargetMachine.h" |
| 36 | #include "llvm/Target/TargetOptions.h" |
Robin Morisset | 880580b | 2014-10-07 23:53:57 +0000 | [diff] [blame] | 37 | #include <stdint.h> |
Chris Lattner | 655e7df | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 38 | using namespace llvm; |
| 39 | |
Chandler Carruth | 84e68b2 | 2014-04-22 02:41:26 +0000 | [diff] [blame] | 40 | #define DEBUG_TYPE "x86-isel" |
| 41 | |
Chris Lattner | 1ef9cd4 | 2006-12-19 22:59:26 +0000 | [diff] [blame] | 42 | STATISTIC(NumLoadMoved, "Number of loads moved below TokenFactor"); |
| 43 | |
Chris Lattner | 655e7df | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 44 | //===----------------------------------------------------------------------===// |
| 45 | // Pattern Matcher Implementation |
| 46 | //===----------------------------------------------------------------------===// |
| 47 | |
| 48 | namespace { |
Sanjay Patel | b5723d0 | 2015-10-13 15:12:27 +0000 | [diff] [blame] | 49 | /// This corresponds to X86AddressMode, but uses SDValue's instead of register |
| 50 | /// numbers for the leaves of the matched tree. |
Chris Lattner | 3f0f71b | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 51 | struct X86ISelAddressMode { |
| 52 | enum { |
| 53 | RegBase, |
Chris Lattner | aa237256 | 2006-05-24 17:04:05 +0000 | [diff] [blame] | 54 | FrameIndexBase |
Chris Lattner | 3f0f71b | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 55 | } BaseType; |
| 56 | |
Dan Gohman | 0fd54fb | 2010-04-29 23:30:41 +0000 | [diff] [blame] | 57 | // This is really a union, discriminated by BaseType! |
| 58 | SDValue Base_Reg; |
| 59 | int Base_FrameIndex; |
Chris Lattner | 3f0f71b | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 60 | |
| 61 | unsigned Scale; |
Chad Rosier | 24c19d2 | 2012-08-01 18:39:17 +0000 | [diff] [blame] | 62 | SDValue IndexReg; |
Dan Gohman | 059c4fa | 2008-11-11 15:52:29 +0000 | [diff] [blame] | 63 | int32_t Disp; |
Rafael Espindola | 3b2df10 | 2009-04-08 21:14:34 +0000 | [diff] [blame] | 64 | SDValue Segment; |
Dan Gohman | bcaf681 | 2010-04-15 01:51:59 +0000 | [diff] [blame] | 65 | const GlobalValue *GV; |
| 66 | const Constant *CP; |
| 67 | const BlockAddress *BlockAddr; |
Evan Cheng | 11b0a5d | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 68 | const char *ES; |
Rafael Espindola | 36b718f | 2015-06-22 17:46:53 +0000 | [diff] [blame] | 69 | MCSymbol *MCSym; |
Evan Cheng | 11b0a5d | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 70 | int JT; |
Evan Cheng | 77d86ff | 2006-02-25 10:09:08 +0000 | [diff] [blame] | 71 | unsigned Align; // CP alignment. |
Chris Lattner | bd7e26d | 2009-06-26 05:51:45 +0000 | [diff] [blame] | 72 | unsigned char SymbolFlags; // X86II::MO_* |
Chris Lattner | 3f0f71b | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 73 | |
| 74 | X86ISelAddressMode() |
Rafael Espindola | 36b718f | 2015-06-22 17:46:53 +0000 | [diff] [blame] | 75 | : BaseType(RegBase), Base_FrameIndex(0), Scale(1), IndexReg(), Disp(0), |
| 76 | Segment(), GV(nullptr), CP(nullptr), BlockAddr(nullptr), ES(nullptr), |
| 77 | MCSym(nullptr), JT(-1), Align(0), SymbolFlags(X86II::MO_NO_FLAG) {} |
Dan Gohman | 4e3e3de | 2009-02-07 00:43:41 +0000 | [diff] [blame] | 78 | |
| 79 | bool hasSymbolicDisplacement() const { |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 80 | return GV != nullptr || CP != nullptr || ES != nullptr || |
Rafael Espindola | 36b718f | 2015-06-22 17:46:53 +0000 | [diff] [blame] | 81 | MCSym != nullptr || JT != -1 || BlockAddr != nullptr; |
Dan Gohman | 4e3e3de | 2009-02-07 00:43:41 +0000 | [diff] [blame] | 82 | } |
Chad Rosier | 24c19d2 | 2012-08-01 18:39:17 +0000 | [diff] [blame] | 83 | |
Chris Lattner | fea81da | 2009-06-27 04:16:01 +0000 | [diff] [blame] | 84 | bool hasBaseOrIndexReg() const { |
Tim Northover | 97347a8 | 2013-09-19 11:33:53 +0000 | [diff] [blame] | 85 | return BaseType == FrameIndexBase || |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 86 | IndexReg.getNode() != nullptr || Base_Reg.getNode() != nullptr; |
Chris Lattner | fea81da | 2009-06-27 04:16:01 +0000 | [diff] [blame] | 87 | } |
Chad Rosier | 24c19d2 | 2012-08-01 18:39:17 +0000 | [diff] [blame] | 88 | |
Sanjay Patel | b5723d0 | 2015-10-13 15:12:27 +0000 | [diff] [blame] | 89 | /// Return true if this addressing mode is already RIP-relative. |
Chris Lattner | fea81da | 2009-06-27 04:16:01 +0000 | [diff] [blame] | 90 | bool isRIPRelative() const { |
| 91 | if (BaseType != RegBase) return false; |
| 92 | if (RegisterSDNode *RegNode = |
Dan Gohman | 0fd54fb | 2010-04-29 23:30:41 +0000 | [diff] [blame] | 93 | dyn_cast_or_null<RegisterSDNode>(Base_Reg.getNode())) |
Chris Lattner | fea81da | 2009-06-27 04:16:01 +0000 | [diff] [blame] | 94 | return RegNode->getReg() == X86::RIP; |
| 95 | return false; |
| 96 | } |
Chad Rosier | 24c19d2 | 2012-08-01 18:39:17 +0000 | [diff] [blame] | 97 | |
Chris Lattner | fea81da | 2009-06-27 04:16:01 +0000 | [diff] [blame] | 98 | void setBaseReg(SDValue Reg) { |
| 99 | BaseType = RegBase; |
Dan Gohman | 0fd54fb | 2010-04-29 23:30:41 +0000 | [diff] [blame] | 100 | Base_Reg = Reg; |
Chris Lattner | fea81da | 2009-06-27 04:16:01 +0000 | [diff] [blame] | 101 | } |
Dan Gohman | 4e3e3de | 2009-02-07 00:43:41 +0000 | [diff] [blame] | 102 | |
Manman Ren | 19f49ac | 2012-09-11 22:23:19 +0000 | [diff] [blame] | 103 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
Dale Johannesen | dafdbf7 | 2008-08-11 23:46:25 +0000 | [diff] [blame] | 104 | void dump() { |
David Greene | dbdb1b2 | 2010-01-05 01:29:08 +0000 | [diff] [blame] | 105 | dbgs() << "X86ISelAddressMode " << this << '\n'; |
Dan Gohman | 0fd54fb | 2010-04-29 23:30:41 +0000 | [diff] [blame] | 106 | dbgs() << "Base_Reg "; |
Craig Topper | e73658d | 2014-04-28 04:05:08 +0000 | [diff] [blame] | 107 | if (Base_Reg.getNode()) |
Chad Rosier | 24c19d2 | 2012-08-01 18:39:17 +0000 | [diff] [blame] | 108 | Base_Reg.getNode()->dump(); |
Bill Wendling | fe3bdb4 | 2009-08-07 21:33:25 +0000 | [diff] [blame] | 109 | else |
David Greene | dbdb1b2 | 2010-01-05 01:29:08 +0000 | [diff] [blame] | 110 | dbgs() << "nul"; |
Dan Gohman | 0fd54fb | 2010-04-29 23:30:41 +0000 | [diff] [blame] | 111 | dbgs() << " Base.FrameIndex " << Base_FrameIndex << '\n' |
Benjamin Kramer | 940fbb0 | 2009-08-23 11:52:17 +0000 | [diff] [blame] | 112 | << " Scale" << Scale << '\n' |
| 113 | << "IndexReg "; |
Craig Topper | e73658d | 2014-04-28 04:05:08 +0000 | [diff] [blame] | 114 | if (IndexReg.getNode()) |
Bill Wendling | fe3bdb4 | 2009-08-07 21:33:25 +0000 | [diff] [blame] | 115 | IndexReg.getNode()->dump(); |
| 116 | else |
Chad Rosier | 24c19d2 | 2012-08-01 18:39:17 +0000 | [diff] [blame] | 117 | dbgs() << "nul"; |
David Greene | dbdb1b2 | 2010-01-05 01:29:08 +0000 | [diff] [blame] | 118 | dbgs() << " Disp " << Disp << '\n' |
Benjamin Kramer | 940fbb0 | 2009-08-23 11:52:17 +0000 | [diff] [blame] | 119 | << "GV "; |
Bill Wendling | fe3bdb4 | 2009-08-07 21:33:25 +0000 | [diff] [blame] | 120 | if (GV) |
| 121 | GV->dump(); |
| 122 | else |
David Greene | dbdb1b2 | 2010-01-05 01:29:08 +0000 | [diff] [blame] | 123 | dbgs() << "nul"; |
| 124 | dbgs() << " CP "; |
Bill Wendling | fe3bdb4 | 2009-08-07 21:33:25 +0000 | [diff] [blame] | 125 | if (CP) |
| 126 | CP->dump(); |
| 127 | else |
David Greene | dbdb1b2 | 2010-01-05 01:29:08 +0000 | [diff] [blame] | 128 | dbgs() << "nul"; |
| 129 | dbgs() << '\n' |
Benjamin Kramer | 940fbb0 | 2009-08-23 11:52:17 +0000 | [diff] [blame] | 130 | << "ES "; |
Bill Wendling | fe3bdb4 | 2009-08-07 21:33:25 +0000 | [diff] [blame] | 131 | if (ES) |
David Greene | dbdb1b2 | 2010-01-05 01:29:08 +0000 | [diff] [blame] | 132 | dbgs() << ES; |
Bill Wendling | fe3bdb4 | 2009-08-07 21:33:25 +0000 | [diff] [blame] | 133 | else |
David Greene | dbdb1b2 | 2010-01-05 01:29:08 +0000 | [diff] [blame] | 134 | dbgs() << "nul"; |
Rafael Espindola | 36b718f | 2015-06-22 17:46:53 +0000 | [diff] [blame] | 135 | dbgs() << " MCSym "; |
| 136 | if (MCSym) |
| 137 | dbgs() << MCSym; |
| 138 | else |
| 139 | dbgs() << "nul"; |
David Greene | dbdb1b2 | 2010-01-05 01:29:08 +0000 | [diff] [blame] | 140 | dbgs() << " JT" << JT << " Align" << Align << '\n'; |
Dale Johannesen | dafdbf7 | 2008-08-11 23:46:25 +0000 | [diff] [blame] | 141 | } |
Manman Ren | 742534c | 2012-09-06 19:06:06 +0000 | [diff] [blame] | 142 | #endif |
Chris Lattner | 3f0f71b | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 143 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 144 | } |
Chris Lattner | 3f0f71b | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 145 | |
| 146 | namespace { |
Chris Lattner | 655e7df | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 147 | //===--------------------------------------------------------------------===// |
Sanjay Patel | b5723d0 | 2015-10-13 15:12:27 +0000 | [diff] [blame] | 148 | /// ISel - X86-specific code to select X86 machine instructions for |
Chris Lattner | 655e7df | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 149 | /// SelectionDAG operations. |
| 150 | /// |
Craig Topper | 26eec09 | 2014-03-31 06:22:15 +0000 | [diff] [blame] | 151 | class X86DAGToDAGISel final : public SelectionDAGISel { |
Sanjay Patel | b5723d0 | 2015-10-13 15:12:27 +0000 | [diff] [blame] | 152 | /// Keep a pointer to the X86Subtarget around so that we can |
Chris Lattner | 655e7df | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 153 | /// make the right decision when generating code for different targets. |
| 154 | const X86Subtarget *Subtarget; |
Evan Cheng | 5588de9 | 2006-02-18 00:15:05 +0000 | [diff] [blame] | 155 | |
Sanjay Patel | b5723d0 | 2015-10-13 15:12:27 +0000 | [diff] [blame] | 156 | /// If true, selector should try to optimize for code size instead of |
| 157 | /// performance. |
Evan Cheng | 7d6fa97 | 2008-09-26 23:41:32 +0000 | [diff] [blame] | 158 | bool OptForSize; |
| 159 | |
Chris Lattner | 655e7df | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 160 | public: |
Bill Wendling | 026e5d7 | 2009-04-29 23:29:43 +0000 | [diff] [blame] | 161 | explicit X86DAGToDAGISel(X86TargetMachine &tm, CodeGenOpt::Level OptLevel) |
Eric Christopher | 05b8197 | 2015-02-02 17:38:43 +0000 | [diff] [blame] | 162 | : SelectionDAGISel(tm, OptLevel), OptForSize(false) {} |
Chris Lattner | 655e7df | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 163 | |
Craig Topper | 2d9361e | 2014-03-09 07:44:38 +0000 | [diff] [blame] | 164 | const char *getPassName() const override { |
Chris Lattner | 655e7df | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 165 | return "X86 DAG->DAG Instruction Selection"; |
| 166 | } |
| 167 | |
Eric Christopher | 4f09c59 | 2014-05-22 01:53:26 +0000 | [diff] [blame] | 168 | bool runOnMachineFunction(MachineFunction &MF) override { |
| 169 | // Reset the subtarget each time through. |
Eric Christopher | 05b8197 | 2015-02-02 17:38:43 +0000 | [diff] [blame] | 170 | Subtarget = &MF.getSubtarget<X86Subtarget>(); |
Eric Christopher | 4f09c59 | 2014-05-22 01:53:26 +0000 | [diff] [blame] | 171 | SelectionDAGISel::runOnMachineFunction(MF); |
| 172 | return true; |
| 173 | } |
| 174 | |
Craig Topper | 2d9361e | 2014-03-09 07:44:38 +0000 | [diff] [blame] | 175 | void EmitFunctionEntryCode() override; |
Anton Korobeynikov | 9091074 | 2007-09-25 21:52:30 +0000 | [diff] [blame] | 176 | |
Craig Topper | 2d9361e | 2014-03-09 07:44:38 +0000 | [diff] [blame] | 177 | bool IsProfitableToFold(SDValue N, SDNode *U, SDNode *Root) const override; |
Evan Cheng | 5e73ff2 | 2010-02-15 19:41:07 +0000 | [diff] [blame] | 178 | |
Craig Topper | 2d9361e | 2014-03-09 07:44:38 +0000 | [diff] [blame] | 179 | void PreprocessISelDAG() override; |
Chris Lattner | f98f124 | 2010-03-02 06:34:30 +0000 | [diff] [blame] | 180 | |
Jakob Stoklund Olesen | 08aede2 | 2010-09-03 00:35:18 +0000 | [diff] [blame] | 181 | inline bool immSext8(SDNode *N) const { |
| 182 | return isInt<8>(cast<ConstantSDNode>(N)->getSExtValue()); |
| 183 | } |
| 184 | |
Sanjay Patel | b5723d0 | 2015-10-13 15:12:27 +0000 | [diff] [blame] | 185 | // True if the 64-bit immediate fits in a 32-bit sign-extended field. |
Jakob Stoklund Olesen | 08aede2 | 2010-09-03 00:35:18 +0000 | [diff] [blame] | 186 | inline bool i64immSExt32(SDNode *N) const { |
| 187 | uint64_t v = cast<ConstantSDNode>(N)->getZExtValue(); |
| 188 | return (int64_t)v == (int32_t)v; |
| 189 | } |
| 190 | |
Chris Lattner | 655e7df | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 191 | // Include the pieces autogenerated from the target description. |
| 192 | #include "X86GenDAGISel.inc" |
| 193 | |
| 194 | private: |
Craig Topper | 2d9361e | 2014-03-09 07:44:38 +0000 | [diff] [blame] | 195 | SDNode *Select(SDNode *N) override; |
Sanjay Patel | 85030aa | 2015-10-13 16:23:00 +0000 | [diff] [blame^] | 196 | SDNode *selectGather(SDNode *N, unsigned Opc); |
| 197 | SDNode *selectAtomicLoadArith(SDNode *Node, MVT NVT); |
Chris Lattner | 655e7df | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 198 | |
Sanjay Patel | 85030aa | 2015-10-13 16:23:00 +0000 | [diff] [blame^] | 199 | bool foldOffsetIntoAddress(uint64_t Offset, X86ISelAddressMode &AM); |
| 200 | bool matchLoadInAddress(LoadSDNode *N, X86ISelAddressMode &AM); |
| 201 | bool matchWrapper(SDValue N, X86ISelAddressMode &AM); |
| 202 | bool matchAddress(SDValue N, X86ISelAddressMode &AM); |
| 203 | bool matchAddressRecursively(SDValue N, X86ISelAddressMode &AM, |
Dan Gohman | 824ab40 | 2009-07-22 23:26:55 +0000 | [diff] [blame] | 204 | unsigned Depth); |
Sanjay Patel | 85030aa | 2015-10-13 16:23:00 +0000 | [diff] [blame^] | 205 | bool matchAddressBase(SDValue N, X86ISelAddressMode &AM); |
| 206 | bool selectAddr(SDNode *Parent, SDValue N, SDValue &Base, |
Rafael Espindola | 3b2df10 | 2009-04-08 21:14:34 +0000 | [diff] [blame] | 207 | SDValue &Scale, SDValue &Index, SDValue &Disp, |
| 208 | SDValue &Segment); |
Sanjay Patel | 85030aa | 2015-10-13 16:23:00 +0000 | [diff] [blame^] | 209 | bool selectVectorAddr(SDNode *Parent, SDValue N, SDValue &Base, |
Elena Demikhovsky | e1eda8a | 2015-04-30 08:38:48 +0000 | [diff] [blame] | 210 | SDValue &Scale, SDValue &Index, SDValue &Disp, |
| 211 | SDValue &Segment); |
Sanjay Patel | 85030aa | 2015-10-13 16:23:00 +0000 | [diff] [blame^] | 212 | bool selectMOV64Imm32(SDValue N, SDValue &Imm); |
| 213 | bool selectLEAAddr(SDValue N, SDValue &Base, |
Chris Lattner | f469307 | 2010-07-08 23:46:44 +0000 | [diff] [blame] | 214 | SDValue &Scale, SDValue &Index, SDValue &Disp, |
| 215 | SDValue &Segment); |
Sanjay Patel | 85030aa | 2015-10-13 16:23:00 +0000 | [diff] [blame^] | 216 | bool selectLEA64_32Addr(SDValue N, SDValue &Base, |
Tim Northover | 6833e3f | 2013-06-10 20:43:49 +0000 | [diff] [blame] | 217 | SDValue &Scale, SDValue &Index, SDValue &Disp, |
| 218 | SDValue &Segment); |
Sanjay Patel | 85030aa | 2015-10-13 16:23:00 +0000 | [diff] [blame^] | 219 | bool selectTLSADDRAddr(SDValue N, SDValue &Base, |
Chris Lattner | f469307 | 2010-07-08 23:46:44 +0000 | [diff] [blame] | 220 | SDValue &Scale, SDValue &Index, SDValue &Disp, |
| 221 | SDValue &Segment); |
Sanjay Patel | 85030aa | 2015-10-13 16:23:00 +0000 | [diff] [blame^] | 222 | bool selectScalarSSELoad(SDNode *Root, SDValue N, |
Chris Lattner | afac7dad | 2010-02-16 22:35:06 +0000 | [diff] [blame] | 223 | SDValue &Base, SDValue &Scale, |
Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 224 | SDValue &Index, SDValue &Disp, |
Rafael Espindola | 3b2df10 | 2009-04-08 21:14:34 +0000 | [diff] [blame] | 225 | SDValue &Segment, |
Chris Lattner | 18a32ce | 2010-02-21 03:17:59 +0000 | [diff] [blame] | 226 | SDValue &NodeWithChain); |
Chad Rosier | 24c19d2 | 2012-08-01 18:39:17 +0000 | [diff] [blame] | 227 | |
Sanjay Patel | 85030aa | 2015-10-13 16:23:00 +0000 | [diff] [blame^] | 228 | bool tryFoldLoad(SDNode *P, SDValue N, |
Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 229 | SDValue &Base, SDValue &Scale, |
Rafael Espindola | 3b2df10 | 2009-04-08 21:14:34 +0000 | [diff] [blame] | 230 | SDValue &Index, SDValue &Disp, |
| 231 | SDValue &Segment); |
Chad Rosier | 24c19d2 | 2012-08-01 18:39:17 +0000 | [diff] [blame] | 232 | |
Sanjay Patel | b5723d0 | 2015-10-13 15:12:27 +0000 | [diff] [blame] | 233 | /// Implement addressing mode selection for inline asm expressions. |
Craig Topper | 2d9361e | 2014-03-09 07:44:38 +0000 | [diff] [blame] | 234 | bool SelectInlineAsmMemoryOperand(const SDValue &Op, |
Daniel Sanders | 60f1db0 | 2015-03-13 12:45:09 +0000 | [diff] [blame] | 235 | unsigned ConstraintID, |
Craig Topper | 2d9361e | 2014-03-09 07:44:38 +0000 | [diff] [blame] | 236 | std::vector<SDValue> &OutOps) override; |
Chad Rosier | 24c19d2 | 2012-08-01 18:39:17 +0000 | [diff] [blame] | 237 | |
Sanjay Patel | 85030aa | 2015-10-13 16:23:00 +0000 | [diff] [blame^] | 238 | void emitSpecialCodeForMain(); |
Anton Korobeynikov | 9091074 | 2007-09-25 21:52:30 +0000 | [diff] [blame] | 239 | |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 240 | inline void getAddressOperands(X86ISelAddressMode &AM, SDLoc DL, |
| 241 | SDValue &Base, SDValue &Scale, |
| 242 | SDValue &Index, SDValue &Disp, |
| 243 | SDValue &Segment) { |
Eric Christopher | b17140d | 2014-10-08 07:32:17 +0000 | [diff] [blame] | 244 | Base = (AM.BaseType == X86ISelAddressMode::FrameIndexBase) |
Mehdi Amini | 44ede33 | 2015-07-09 02:09:04 +0000 | [diff] [blame] | 245 | ? CurDAG->getTargetFrameIndex( |
| 246 | AM.Base_FrameIndex, |
| 247 | TLI->getPointerTy(CurDAG->getDataLayout())) |
Eric Christopher | b17140d | 2014-10-08 07:32:17 +0000 | [diff] [blame] | 248 | : AM.Base_Reg; |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 249 | Scale = getI8Imm(AM.Scale, DL); |
Evan Cheng | 67ed58e | 2005-12-12 21:49:40 +0000 | [diff] [blame] | 250 | Index = AM.IndexReg; |
Sanjay Patel | b5723d0 | 2015-10-13 15:12:27 +0000 | [diff] [blame] | 251 | // These are 32-bit even in 64-bit mode since RIP-relative offset |
Evan Cheng | 11b0a5d | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 252 | // is 32-bit. |
| 253 | if (AM.GV) |
Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 254 | Disp = CurDAG->getTargetGlobalAddress(AM.GV, SDLoc(), |
Devang Patel | a3ca21b | 2010-07-06 22:08:15 +0000 | [diff] [blame] | 255 | MVT::i32, AM.Disp, |
Chris Lattner | bd7e26d | 2009-06-26 05:51:45 +0000 | [diff] [blame] | 256 | AM.SymbolFlags); |
Evan Cheng | 11b0a5d | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 257 | else if (AM.CP) |
Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 258 | Disp = CurDAG->getTargetConstantPool(AM.CP, MVT::i32, |
Chris Lattner | bd7e26d | 2009-06-26 05:51:45 +0000 | [diff] [blame] | 259 | AM.Align, AM.Disp, AM.SymbolFlags); |
Michael Liao | abb87d4 | 2012-09-12 21:43:09 +0000 | [diff] [blame] | 260 | else if (AM.ES) { |
| 261 | assert(!AM.Disp && "Non-zero displacement is ignored with ES."); |
Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 262 | Disp = CurDAG->getTargetExternalSymbol(AM.ES, MVT::i32, AM.SymbolFlags); |
Rafael Espindola | 36b718f | 2015-06-22 17:46:53 +0000 | [diff] [blame] | 263 | } else if (AM.MCSym) { |
| 264 | assert(!AM.Disp && "Non-zero displacement is ignored with MCSym."); |
| 265 | assert(AM.SymbolFlags == 0 && "oo"); |
| 266 | Disp = CurDAG->getMCSymbol(AM.MCSym, MVT::i32); |
Michael Liao | abb87d4 | 2012-09-12 21:43:09 +0000 | [diff] [blame] | 267 | } else if (AM.JT != -1) { |
| 268 | assert(!AM.Disp && "Non-zero displacement is ignored with JT."); |
Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 269 | Disp = CurDAG->getTargetJumpTable(AM.JT, MVT::i32, AM.SymbolFlags); |
Michael Liao | abb87d4 | 2012-09-12 21:43:09 +0000 | [diff] [blame] | 270 | } else if (AM.BlockAddr) |
| 271 | Disp = CurDAG->getTargetBlockAddress(AM.BlockAddr, MVT::i32, AM.Disp, |
| 272 | AM.SymbolFlags); |
Evan Cheng | 11b0a5d | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 273 | else |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 274 | Disp = CurDAG->getTargetConstant(AM.Disp, DL, MVT::i32); |
Rafael Espindola | 3b2df10 | 2009-04-08 21:14:34 +0000 | [diff] [blame] | 275 | |
| 276 | if (AM.Segment.getNode()) |
| 277 | Segment = AM.Segment; |
| 278 | else |
Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 279 | Segment = CurDAG->getRegister(0, MVT::i32); |
Evan Cheng | 67ed58e | 2005-12-12 21:49:40 +0000 | [diff] [blame] | 280 | } |
| 281 | |
Michael Kuperstein | 243c073 | 2015-08-11 14:10:58 +0000 | [diff] [blame] | 282 | // Utility function to determine whether we should avoid selecting |
| 283 | // immediate forms of instructions for better code size or not. |
| 284 | // At a high level, we'd like to avoid such instructions when |
| 285 | // we have similar constants used within the same basic block |
| 286 | // that can be kept in a register. |
| 287 | // |
| 288 | bool shouldAvoidImmediateInstFormsForSize(SDNode *N) const { |
| 289 | uint32_t UseCount = 0; |
| 290 | |
| 291 | // Do not want to hoist if we're not optimizing for size. |
| 292 | // TODO: We'd like to remove this restriction. |
| 293 | // See the comment in X86InstrInfo.td for more info. |
| 294 | if (!OptForSize) |
| 295 | return false; |
| 296 | |
| 297 | // Walk all the users of the immediate. |
| 298 | for (SDNode::use_iterator UI = N->use_begin(), |
| 299 | UE = N->use_end(); (UI != UE) && (UseCount < 2); ++UI) { |
| 300 | |
| 301 | SDNode *User = *UI; |
| 302 | |
| 303 | // This user is already selected. Count it as a legitimate use and |
| 304 | // move on. |
| 305 | if (User->isMachineOpcode()) { |
| 306 | UseCount++; |
| 307 | continue; |
| 308 | } |
| 309 | |
| 310 | // We want to count stores of immediates as real uses. |
| 311 | if (User->getOpcode() == ISD::STORE && |
| 312 | User->getOperand(1).getNode() == N) { |
| 313 | UseCount++; |
| 314 | continue; |
| 315 | } |
| 316 | |
| 317 | // We don't currently match users that have > 2 operands (except |
| 318 | // for stores, which are handled above) |
| 319 | // Those instruction won't match in ISEL, for now, and would |
| 320 | // be counted incorrectly. |
| 321 | // This may change in the future as we add additional instruction |
| 322 | // types. |
| 323 | if (User->getNumOperands() != 2) |
| 324 | continue; |
| 325 | |
| 326 | // Immediates that are used for offsets as part of stack |
| 327 | // manipulation should be left alone. These are typically |
| 328 | // used to indicate SP offsets for argument passing and |
| 329 | // will get pulled into stores/pushes (implicitly). |
| 330 | if (User->getOpcode() == X86ISD::ADD || |
| 331 | User->getOpcode() == ISD::ADD || |
| 332 | User->getOpcode() == X86ISD::SUB || |
| 333 | User->getOpcode() == ISD::SUB) { |
| 334 | |
| 335 | // Find the other operand of the add/sub. |
| 336 | SDValue OtherOp = User->getOperand(0); |
| 337 | if (OtherOp.getNode() == N) |
| 338 | OtherOp = User->getOperand(1); |
| 339 | |
| 340 | // Don't count if the other operand is SP. |
| 341 | RegisterSDNode *RegNode; |
| 342 | if (OtherOp->getOpcode() == ISD::CopyFromReg && |
| 343 | (RegNode = dyn_cast_or_null<RegisterSDNode>( |
| 344 | OtherOp->getOperand(1).getNode()))) |
| 345 | if ((RegNode->getReg() == X86::ESP) || |
| 346 | (RegNode->getReg() == X86::RSP)) |
| 347 | continue; |
| 348 | } |
| 349 | |
| 350 | // ... otherwise, count this and move on. |
| 351 | UseCount++; |
| 352 | } |
| 353 | |
| 354 | // If we have more than 1 use, then recommend for hoisting. |
| 355 | return (UseCount > 1); |
| 356 | } |
| 357 | |
Sanjay Patel | b5723d0 | 2015-10-13 15:12:27 +0000 | [diff] [blame] | 358 | /// Return a target constant with the specified value of type i8. |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 359 | inline SDValue getI8Imm(unsigned Imm, SDLoc DL) { |
| 360 | return CurDAG->getTargetConstant(Imm, DL, MVT::i8); |
Chris Lattner | 3f0f71b | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 361 | } |
| 362 | |
Sanjay Patel | b5723d0 | 2015-10-13 15:12:27 +0000 | [diff] [blame] | 363 | /// Return a target constant with the specified value, of type i32. |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 364 | inline SDValue getI32Imm(unsigned Imm, SDLoc DL) { |
| 365 | return CurDAG->getTargetConstant(Imm, DL, MVT::i32); |
Chris Lattner | 655e7df | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 366 | } |
Evan Cheng | d49cc36 | 2006-02-10 22:24:32 +0000 | [diff] [blame] | 367 | |
Sanjay Patel | b5723d0 | 2015-10-13 15:12:27 +0000 | [diff] [blame] | 368 | /// Return an SDNode that returns the value of the global base register. |
| 369 | /// Output instructions required to initialize the global base register, |
| 370 | /// if necessary. |
Evan Cheng | 61413a3 | 2006-08-26 05:34:46 +0000 | [diff] [blame] | 371 | SDNode *getGlobalBaseReg(); |
Evan Cheng | 5588de9 | 2006-02-18 00:15:05 +0000 | [diff] [blame] | 372 | |
Sanjay Patel | b5723d0 | 2015-10-13 15:12:27 +0000 | [diff] [blame] | 373 | /// Return a reference to the TargetMachine, casted to the target-specific |
| 374 | /// type. |
Jakub Staszak | e167cf5 | 2013-02-19 21:54:59 +0000 | [diff] [blame] | 375 | const X86TargetMachine &getTargetMachine() const { |
Dan Gohman | 4751bb9 | 2009-06-03 20:20:00 +0000 | [diff] [blame] | 376 | return static_cast<const X86TargetMachine &>(TM); |
| 377 | } |
| 378 | |
Sanjay Patel | b5723d0 | 2015-10-13 15:12:27 +0000 | [diff] [blame] | 379 | /// Return a reference to the TargetInstrInfo, casted to the target-specific |
| 380 | /// type. |
Jakub Staszak | e167cf5 | 2013-02-19 21:54:59 +0000 | [diff] [blame] | 381 | const X86InstrInfo *getInstrInfo() const { |
Eric Christopher | 05b8197 | 2015-02-02 17:38:43 +0000 | [diff] [blame] | 382 | return Subtarget->getInstrInfo(); |
Dan Gohman | 4751bb9 | 2009-06-03 20:20:00 +0000 | [diff] [blame] | 383 | } |
Adam Nemet | ff63a2d | 2014-10-03 20:00:34 +0000 | [diff] [blame] | 384 | |
| 385 | /// \brief Address-mode matching performs shift-of-and to and-of-shift |
| 386 | /// reassociation in order to expose more scaled addressing |
| 387 | /// opportunities. |
| 388 | bool ComplexPatternFuncMutatesDAG() const override { |
| 389 | return true; |
| 390 | } |
Chris Lattner | 655e7df | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 391 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 392 | } |
Chris Lattner | 655e7df | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 393 | |
Evan Cheng | 72bb66a | 2006-08-08 00:31:00 +0000 | [diff] [blame] | 394 | |
Evan Cheng | 5e73ff2 | 2010-02-15 19:41:07 +0000 | [diff] [blame] | 395 | bool |
| 396 | X86DAGToDAGISel::IsProfitableToFold(SDValue N, SDNode *U, SDNode *Root) const { |
Bill Wendling | 026e5d7 | 2009-04-29 23:29:43 +0000 | [diff] [blame] | 397 | if (OptLevel == CodeGenOpt::None) return false; |
Evan Cheng | b86375c | 2006-10-14 08:33:25 +0000 | [diff] [blame] | 398 | |
Evan Cheng | 5e73ff2 | 2010-02-15 19:41:07 +0000 | [diff] [blame] | 399 | if (!N.hasOneUse()) |
| 400 | return false; |
| 401 | |
| 402 | if (N.getOpcode() != ISD::LOAD) |
| 403 | return true; |
| 404 | |
| 405 | // If N is a load, do additional profitability checks. |
| 406 | if (U == Root) { |
Evan Cheng | 83bdb38 | 2008-11-27 00:49:46 +0000 | [diff] [blame] | 407 | switch (U->getOpcode()) { |
| 408 | default: break; |
Dan Gohman | 85d4fdf | 2010-01-04 20:51:50 +0000 | [diff] [blame] | 409 | case X86ISD::ADD: |
| 410 | case X86ISD::SUB: |
| 411 | case X86ISD::AND: |
| 412 | case X86ISD::XOR: |
| 413 | case X86ISD::OR: |
Evan Cheng | 83bdb38 | 2008-11-27 00:49:46 +0000 | [diff] [blame] | 414 | case ISD::ADD: |
| 415 | case ISD::ADDC: |
| 416 | case ISD::ADDE: |
| 417 | case ISD::AND: |
| 418 | case ISD::OR: |
| 419 | case ISD::XOR: { |
Rafael Espindola | bb834f0 | 2009-04-10 10:09:34 +0000 | [diff] [blame] | 420 | SDValue Op1 = U->getOperand(1); |
| 421 | |
Evan Cheng | 83bdb38 | 2008-11-27 00:49:46 +0000 | [diff] [blame] | 422 | // If the other operand is a 8-bit immediate we should fold the immediate |
| 423 | // instead. This reduces code size. |
| 424 | // e.g. |
| 425 | // movl 4(%esp), %eax |
| 426 | // addl $4, %eax |
| 427 | // vs. |
| 428 | // movl $4, %eax |
| 429 | // addl 4(%esp), %eax |
| 430 | // The former is 2 bytes shorter. In case where the increment is 1, then |
| 431 | // the saving can be 4 bytes (by using incl %eax). |
Rafael Espindola | bb834f0 | 2009-04-10 10:09:34 +0000 | [diff] [blame] | 432 | if (ConstantSDNode *Imm = dyn_cast<ConstantSDNode>(Op1)) |
Dan Gohman | 2293eb6 | 2009-03-14 02:07:16 +0000 | [diff] [blame] | 433 | if (Imm->getAPIntValue().isSignedIntN(8)) |
| 434 | return false; |
Rafael Espindola | bb834f0 | 2009-04-10 10:09:34 +0000 | [diff] [blame] | 435 | |
| 436 | // If the other operand is a TLS address, we should fold it instead. |
| 437 | // This produces |
| 438 | // movl %gs:0, %eax |
| 439 | // leal i@NTPOFF(%eax), %eax |
| 440 | // instead of |
| 441 | // movl $i@NTPOFF, %eax |
| 442 | // addl %gs:0, %eax |
| 443 | // if the block also has an access to a second TLS address this will save |
| 444 | // a load. |
Alp Toker | f907b89 | 2013-12-05 05:44:44 +0000 | [diff] [blame] | 445 | // FIXME: This is probably also true for non-TLS addresses. |
Rafael Espindola | bb834f0 | 2009-04-10 10:09:34 +0000 | [diff] [blame] | 446 | if (Op1.getOpcode() == X86ISD::Wrapper) { |
| 447 | SDValue Val = Op1.getOperand(0); |
| 448 | if (Val.getOpcode() == ISD::TargetGlobalTLSAddress) |
| 449 | return false; |
| 450 | } |
Evan Cheng | 83bdb38 | 2008-11-27 00:49:46 +0000 | [diff] [blame] | 451 | } |
| 452 | } |
Evan Cheng | 5e73ff2 | 2010-02-15 19:41:07 +0000 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | return true; |
| 456 | } |
| 457 | |
Sanjay Patel | b5723d0 | 2015-10-13 15:12:27 +0000 | [diff] [blame] | 458 | /// Replace the original chain operand of the call with |
Evan Cheng | d703df6 | 2010-03-14 03:48:46 +0000 | [diff] [blame] | 459 | /// load's chain operand and move load below the call's chain operand. |
Sanjay Patel | 85030aa | 2015-10-13 16:23:00 +0000 | [diff] [blame^] | 460 | static void moveBelowOrigChain(SelectionDAG *CurDAG, SDValue Load, |
Evan Cheng | 214156c | 2012-10-02 23:49:13 +0000 | [diff] [blame] | 461 | SDValue Call, SDValue OrigChain) { |
Evan Cheng | f00f1e5 | 2008-08-25 21:27:18 +0000 | [diff] [blame] | 462 | SmallVector<SDValue, 8> Ops; |
Evan Cheng | d703df6 | 2010-03-14 03:48:46 +0000 | [diff] [blame] | 463 | SDValue Chain = OrigChain.getOperand(0); |
Evan Cheng | 6c7e851 | 2009-01-26 18:43:34 +0000 | [diff] [blame] | 464 | if (Chain.getNode() == Load.getNode()) |
| 465 | Ops.push_back(Load.getOperand(0)); |
| 466 | else { |
| 467 | assert(Chain.getOpcode() == ISD::TokenFactor && |
Evan Cheng | d703df6 | 2010-03-14 03:48:46 +0000 | [diff] [blame] | 468 | "Unexpected chain operand"); |
Evan Cheng | 6c7e851 | 2009-01-26 18:43:34 +0000 | [diff] [blame] | 469 | for (unsigned i = 0, e = Chain.getNumOperands(); i != e; ++i) |
| 470 | if (Chain.getOperand(i).getNode() == Load.getNode()) |
| 471 | Ops.push_back(Load.getOperand(0)); |
| 472 | else |
| 473 | Ops.push_back(Chain.getOperand(i)); |
| 474 | SDValue NewChain = |
Craig Topper | 48d114b | 2014-04-26 18:35:24 +0000 | [diff] [blame] | 475 | CurDAG->getNode(ISD::TokenFactor, SDLoc(Load), MVT::Other, Ops); |
Evan Cheng | 6c7e851 | 2009-01-26 18:43:34 +0000 | [diff] [blame] | 476 | Ops.clear(); |
| 477 | Ops.push_back(NewChain); |
| 478 | } |
Benjamin Kramer | 6cd780f | 2015-02-17 15:29:18 +0000 | [diff] [blame] | 479 | Ops.append(OrigChain->op_begin() + 1, OrigChain->op_end()); |
Craig Topper | 8c0b4d0 | 2014-04-28 05:57:50 +0000 | [diff] [blame] | 480 | CurDAG->UpdateNodeOperands(OrigChain.getNode(), Ops); |
Dan Gohman | 92c11ac | 2010-06-18 15:30:29 +0000 | [diff] [blame] | 481 | CurDAG->UpdateNodeOperands(Load.getNode(), Call.getOperand(0), |
Evan Cheng | f00f1e5 | 2008-08-25 21:27:18 +0000 | [diff] [blame] | 482 | Load.getOperand(1), Load.getOperand(2)); |
Evan Cheng | 214156c | 2012-10-02 23:49:13 +0000 | [diff] [blame] | 483 | |
Evan Cheng | f00f1e5 | 2008-08-25 21:27:18 +0000 | [diff] [blame] | 484 | Ops.clear(); |
Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 485 | Ops.push_back(SDValue(Load.getNode(), 1)); |
Benjamin Kramer | 6cd780f | 2015-02-17 15:29:18 +0000 | [diff] [blame] | 486 | Ops.append(Call->op_begin() + 1, Call->op_end()); |
Craig Topper | 8c0b4d0 | 2014-04-28 05:57:50 +0000 | [diff] [blame] | 487 | CurDAG->UpdateNodeOperands(Call.getNode(), Ops); |
Evan Cheng | f00f1e5 | 2008-08-25 21:27:18 +0000 | [diff] [blame] | 488 | } |
| 489 | |
Sanjay Patel | b5723d0 | 2015-10-13 15:12:27 +0000 | [diff] [blame] | 490 | /// Return true if call address is a load and it can be |
Evan Cheng | f00f1e5 | 2008-08-25 21:27:18 +0000 | [diff] [blame] | 491 | /// moved below CALLSEQ_START and the chains leading up to the call. |
| 492 | /// Return the CALLSEQ_START by reference as a second output. |
Evan Cheng | d703df6 | 2010-03-14 03:48:46 +0000 | [diff] [blame] | 493 | /// In the case of a tail call, there isn't a callseq node between the call |
| 494 | /// chain and the load. |
| 495 | static bool isCalleeLoad(SDValue Callee, SDValue &Chain, bool HasCallSeq) { |
Evan Cheng | 847ad44 | 2012-10-05 01:48:22 +0000 | [diff] [blame] | 496 | // The transformation is somewhat dangerous if the call's chain was glued to |
| 497 | // the call. After MoveBelowOrigChain the load is moved between the call and |
| 498 | // the chain, this can create a cycle if the load is not folded. So it is |
| 499 | // *really* important that we are sure the load will be folded. |
Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 500 | if (Callee.getNode() == Chain.getNode() || !Callee.hasOneUse()) |
Evan Cheng | f00f1e5 | 2008-08-25 21:27:18 +0000 | [diff] [blame] | 501 | return false; |
Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 502 | LoadSDNode *LD = dyn_cast<LoadSDNode>(Callee.getNode()); |
Evan Cheng | f00f1e5 | 2008-08-25 21:27:18 +0000 | [diff] [blame] | 503 | if (!LD || |
| 504 | LD->isVolatile() || |
| 505 | LD->getAddressingMode() != ISD::UNINDEXED || |
| 506 | LD->getExtensionType() != ISD::NON_EXTLOAD) |
| 507 | return false; |
| 508 | |
| 509 | // Now let's find the callseq_start. |
Evan Cheng | d703df6 | 2010-03-14 03:48:46 +0000 | [diff] [blame] | 510 | while (HasCallSeq && Chain.getOpcode() != ISD::CALLSEQ_START) { |
Evan Cheng | f00f1e5 | 2008-08-25 21:27:18 +0000 | [diff] [blame] | 511 | if (!Chain.hasOneUse()) |
| 512 | return false; |
| 513 | Chain = Chain.getOperand(0); |
| 514 | } |
Evan Cheng | d703df6 | 2010-03-14 03:48:46 +0000 | [diff] [blame] | 515 | |
| 516 | if (!Chain.getNumOperands()) |
| 517 | return false; |
Evan Cheng | 3fb03e2 | 2013-01-06 19:00:15 +0000 | [diff] [blame] | 518 | // Since we are not checking for AA here, conservatively abort if the chain |
| 519 | // writes to memory. It's not safe to move the callee (a load) across a store. |
| 520 | if (isa<MemSDNode>(Chain.getNode()) && |
| 521 | cast<MemSDNode>(Chain.getNode())->writeMem()) |
| 522 | return false; |
Evan Cheng | 6c7e851 | 2009-01-26 18:43:34 +0000 | [diff] [blame] | 523 | if (Chain.getOperand(0).getNode() == Callee.getNode()) |
| 524 | return true; |
| 525 | if (Chain.getOperand(0).getOpcode() == ISD::TokenFactor && |
Dan Gohman | 520a685 | 2009-09-15 01:22:01 +0000 | [diff] [blame] | 526 | Callee.getValue(1).isOperandOf(Chain.getOperand(0).getNode()) && |
| 527 | Callee.getValue(1).hasOneUse()) |
Evan Cheng | 6c7e851 | 2009-01-26 18:43:34 +0000 | [diff] [blame] | 528 | return true; |
| 529 | return false; |
Evan Cheng | f00f1e5 | 2008-08-25 21:27:18 +0000 | [diff] [blame] | 530 | } |
| 531 | |
Chris Lattner | 8d63704 | 2010-03-02 23:12:51 +0000 | [diff] [blame] | 532 | void X86DAGToDAGISel::PreprocessISelDAG() { |
Chris Lattner | 82cc533 | 2010-03-04 01:43:43 +0000 | [diff] [blame] | 533 | // OptForSize is used in pattern predicates that isel is matching. |
Sanjay Patel | 68b0325 | 2015-08-10 16:47:47 +0000 | [diff] [blame] | 534 | OptForSize = MF->getFunction()->optForSize(); |
Chad Rosier | 24c19d2 | 2012-08-01 18:39:17 +0000 | [diff] [blame] | 535 | |
Dan Gohman | eb0cee9 | 2008-08-23 02:25:05 +0000 | [diff] [blame] | 536 | for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(), |
| 537 | E = CurDAG->allnodes_end(); I != E; ) { |
Chris Lattner | a91f77e | 2008-01-24 08:07:48 +0000 | [diff] [blame] | 538 | SDNode *N = I++; // Preincrement iterator to avoid invalidation issues. |
Chris Lattner | 8d63704 | 2010-03-02 23:12:51 +0000 | [diff] [blame] | 539 | |
Evan Cheng | d703df6 | 2010-03-14 03:48:46 +0000 | [diff] [blame] | 540 | if (OptLevel != CodeGenOpt::None && |
Michael Liao | 96b4260 | 2013-03-28 23:13:21 +0000 | [diff] [blame] | 541 | // Only does this when target favors doesn't favor register indirect |
| 542 | // call. |
| 543 | ((N->getOpcode() == X86ISD::CALL && !Subtarget->callRegIndirect()) || |
Evan Cheng | 847ad44 | 2012-10-05 01:48:22 +0000 | [diff] [blame] | 544 | (N->getOpcode() == X86ISD::TC_RETURN && |
Nick Lewycky | f41a80e | 2013-01-13 19:03:55 +0000 | [diff] [blame] | 545 | // Only does this if load can be folded into TC_RETURN. |
Evan Cheng | 847ad44 | 2012-10-05 01:48:22 +0000 | [diff] [blame] | 546 | (Subtarget->is64Bit() || |
| 547 | getTargetMachine().getRelocationModel() != Reloc::PIC_)))) { |
Chris Lattner | 8d63704 | 2010-03-02 23:12:51 +0000 | [diff] [blame] | 548 | /// Also try moving call address load from outside callseq_start to just |
| 549 | /// before the call to allow it to be folded. |
| 550 | /// |
| 551 | /// [Load chain] |
| 552 | /// ^ |
| 553 | /// | |
| 554 | /// [Load] |
| 555 | /// ^ ^ |
| 556 | /// | | |
| 557 | /// / \-- |
| 558 | /// / | |
| 559 | ///[CALLSEQ_START] | |
| 560 | /// ^ | |
| 561 | /// | | |
| 562 | /// [LOAD/C2Reg] | |
| 563 | /// | | |
| 564 | /// \ / |
| 565 | /// \ / |
| 566 | /// [CALL] |
Evan Cheng | d703df6 | 2010-03-14 03:48:46 +0000 | [diff] [blame] | 567 | bool HasCallSeq = N->getOpcode() == X86ISD::CALL; |
Chris Lattner | 8d63704 | 2010-03-02 23:12:51 +0000 | [diff] [blame] | 568 | SDValue Chain = N->getOperand(0); |
| 569 | SDValue Load = N->getOperand(1); |
Evan Cheng | d703df6 | 2010-03-14 03:48:46 +0000 | [diff] [blame] | 570 | if (!isCalleeLoad(Load, Chain, HasCallSeq)) |
Chris Lattner | 8d63704 | 2010-03-02 23:12:51 +0000 | [diff] [blame] | 571 | continue; |
Sanjay Patel | 85030aa | 2015-10-13 16:23:00 +0000 | [diff] [blame^] | 572 | moveBelowOrigChain(CurDAG, Load, SDValue(N, 0), Chain); |
Chris Lattner | 8d63704 | 2010-03-02 23:12:51 +0000 | [diff] [blame] | 573 | ++NumLoadMoved; |
| 574 | continue; |
| 575 | } |
Chad Rosier | 24c19d2 | 2012-08-01 18:39:17 +0000 | [diff] [blame] | 576 | |
Chris Lattner | 8d63704 | 2010-03-02 23:12:51 +0000 | [diff] [blame] | 577 | // Lower fpround and fpextend nodes that target the FP stack to be store and |
| 578 | // load to the stack. This is a gross hack. We would like to simply mark |
| 579 | // these as being illegal, but when we do that, legalize produces these when |
| 580 | // it expands calls, then expands these in the same legalize pass. We would |
| 581 | // like dag combine to be able to hack on these between the call expansion |
| 582 | // and the node legalization. As such this pass basically does "really |
| 583 | // late" legalization of these inline with the X86 isel pass. |
| 584 | // FIXME: This should only happen when not compiled with -O0. |
Chris Lattner | a91f77e | 2008-01-24 08:07:48 +0000 | [diff] [blame] | 585 | if (N->getOpcode() != ISD::FP_ROUND && N->getOpcode() != ISD::FP_EXTEND) |
| 586 | continue; |
Chad Rosier | 24c19d2 | 2012-08-01 18:39:17 +0000 | [diff] [blame] | 587 | |
Craig Topper | 83e042a | 2013-08-15 05:57:07 +0000 | [diff] [blame] | 588 | MVT SrcVT = N->getOperand(0).getSimpleValueType(); |
| 589 | MVT DstVT = N->getSimpleValueType(0); |
Bruno Cardoso Lopes | 616fe60 | 2011-08-01 21:54:05 +0000 | [diff] [blame] | 590 | |
| 591 | // If any of the sources are vectors, no fp stack involved. |
| 592 | if (SrcVT.isVector() || DstVT.isVector()) |
| 593 | continue; |
| 594 | |
| 595 | // If the source and destination are SSE registers, then this is a legal |
| 596 | // conversion that should not be lowered. |
Benjamin Kramer | 02ff1cd | 2013-06-27 11:07:42 +0000 | [diff] [blame] | 597 | const X86TargetLowering *X86Lowering = |
Eric Christopher | b17140d | 2014-10-08 07:32:17 +0000 | [diff] [blame] | 598 | static_cast<const X86TargetLowering *>(TLI); |
Bill Wendling | a3cd350 | 2013-06-19 21:36:55 +0000 | [diff] [blame] | 599 | bool SrcIsSSE = X86Lowering->isScalarFPTypeInSSEReg(SrcVT); |
| 600 | bool DstIsSSE = X86Lowering->isScalarFPTypeInSSEReg(DstVT); |
Chris Lattner | a91f77e | 2008-01-24 08:07:48 +0000 | [diff] [blame] | 601 | if (SrcIsSSE && DstIsSSE) |
| 602 | continue; |
| 603 | |
Chris Lattner | d587e58 | 2008-03-09 07:05:32 +0000 | [diff] [blame] | 604 | if (!SrcIsSSE && !DstIsSSE) { |
| 605 | // If this is an FPStack extension, it is a noop. |
| 606 | if (N->getOpcode() == ISD::FP_EXTEND) |
| 607 | continue; |
| 608 | // If this is a value-preserving FPStack truncation, it is a noop. |
| 609 | if (N->getConstantOperandVal(1)) |
| 610 | continue; |
| 611 | } |
Chad Rosier | 24c19d2 | 2012-08-01 18:39:17 +0000 | [diff] [blame] | 612 | |
Chris Lattner | a91f77e | 2008-01-24 08:07:48 +0000 | [diff] [blame] | 613 | // Here we could have an FP stack truncation or an FPStack <-> SSE convert. |
| 614 | // FPStack has extload and truncstore. SSE can fold direct loads into other |
| 615 | // operations. Based on this, decide what we want to do. |
Craig Topper | 83e042a | 2013-08-15 05:57:07 +0000 | [diff] [blame] | 616 | MVT MemVT; |
Chris Lattner | a91f77e | 2008-01-24 08:07:48 +0000 | [diff] [blame] | 617 | if (N->getOpcode() == ISD::FP_ROUND) |
| 618 | MemVT = DstVT; // FP_ROUND must use DstVT, we can't do a 'trunc load'. |
| 619 | else |
| 620 | MemVT = SrcIsSSE ? SrcVT : DstVT; |
Chad Rosier | 24c19d2 | 2012-08-01 18:39:17 +0000 | [diff] [blame] | 621 | |
Dan Gohman | eb0cee9 | 2008-08-23 02:25:05 +0000 | [diff] [blame] | 622 | SDValue MemTmp = CurDAG->CreateStackTemporary(MemVT); |
Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 623 | SDLoc dl(N); |
Chad Rosier | 24c19d2 | 2012-08-01 18:39:17 +0000 | [diff] [blame] | 624 | |
Chris Lattner | a91f77e | 2008-01-24 08:07:48 +0000 | [diff] [blame] | 625 | // FIXME: optimize the case where the src/dest is a load or store? |
Dale Johannesen | 14f2d9d | 2009-02-03 21:48:12 +0000 | [diff] [blame] | 626 | SDValue Store = CurDAG->getTruncStore(CurDAG->getEntryNode(), dl, |
Dan Gohman | eb0cee9 | 2008-08-23 02:25:05 +0000 | [diff] [blame] | 627 | N->getOperand(0), |
Chris Lattner | 3d178ed | 2010-09-21 17:04:51 +0000 | [diff] [blame] | 628 | MemTmp, MachinePointerInfo(), MemVT, |
David Greene | cbd39c5 | 2010-02-15 16:57:43 +0000 | [diff] [blame] | 629 | false, false, 0); |
Stuart Hastings | 81c4306 | 2011-02-16 16:23:55 +0000 | [diff] [blame] | 630 | SDValue Result = CurDAG->getExtLoad(ISD::EXTLOAD, dl, DstVT, Store, MemTmp, |
Chris Lattner | 3d178ed | 2010-09-21 17:04:51 +0000 | [diff] [blame] | 631 | MachinePointerInfo(), |
Louis Gerbarg | 67474e3 | 2014-07-31 21:45:05 +0000 | [diff] [blame] | 632 | MemVT, false, false, false, 0); |
Chris Lattner | a91f77e | 2008-01-24 08:07:48 +0000 | [diff] [blame] | 633 | |
| 634 | // We're about to replace all uses of the FP_ROUND/FP_EXTEND with the |
| 635 | // extload we created. This will cause general havok on the dag because |
| 636 | // anything below the conversion could be folded into other existing nodes. |
| 637 | // To avoid invalidating 'I', back it up to the convert node. |
| 638 | --I; |
Dan Gohman | eb0cee9 | 2008-08-23 02:25:05 +0000 | [diff] [blame] | 639 | CurDAG->ReplaceAllUsesOfValueWith(SDValue(N, 0), Result); |
Chad Rosier | 24c19d2 | 2012-08-01 18:39:17 +0000 | [diff] [blame] | 640 | |
Chris Lattner | a91f77e | 2008-01-24 08:07:48 +0000 | [diff] [blame] | 641 | // Now that we did that, the node is dead. Increment the iterator to the |
| 642 | // next node to process, then delete N. |
| 643 | ++I; |
Dan Gohman | eb0cee9 | 2008-08-23 02:25:05 +0000 | [diff] [blame] | 644 | CurDAG->DeleteNode(N); |
Chad Rosier | 24c19d2 | 2012-08-01 18:39:17 +0000 | [diff] [blame] | 645 | } |
Chris Lattner | a91f77e | 2008-01-24 08:07:48 +0000 | [diff] [blame] | 646 | } |
| 647 | |
Chris Lattner | 655e7df | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 648 | |
Sanjay Patel | b5723d0 | 2015-10-13 15:12:27 +0000 | [diff] [blame] | 649 | /// Emit any code that needs to be executed only in the main function. |
Sanjay Patel | 85030aa | 2015-10-13 16:23:00 +0000 | [diff] [blame^] | 650 | void X86DAGToDAGISel::emitSpecialCodeForMain() { |
Bill Wendling | 81d4071 | 2011-01-06 00:47:10 +0000 | [diff] [blame] | 651 | if (Subtarget->isTargetCygMing()) { |
David Majnemer | d5ab35f | 2015-02-21 05:49:45 +0000 | [diff] [blame] | 652 | TargetLowering::ArgListTy Args; |
Mehdi Amini | 44ede33 | 2015-07-09 02:09:04 +0000 | [diff] [blame] | 653 | auto &DL = CurDAG->getDataLayout(); |
David Majnemer | d5ab35f | 2015-02-21 05:49:45 +0000 | [diff] [blame] | 654 | |
| 655 | TargetLowering::CallLoweringInfo CLI(*CurDAG); |
| 656 | CLI.setChain(CurDAG->getRoot()) |
| 657 | .setCallee(CallingConv::C, Type::getVoidTy(*CurDAG->getContext()), |
Mehdi Amini | 44ede33 | 2015-07-09 02:09:04 +0000 | [diff] [blame] | 658 | CurDAG->getExternalSymbol("__main", TLI->getPointerTy(DL)), |
David Majnemer | d5ab35f | 2015-02-21 05:49:45 +0000 | [diff] [blame] | 659 | std::move(Args), 0); |
| 660 | const TargetLowering &TLI = CurDAG->getTargetLoweringInfo(); |
| 661 | std::pair<SDValue, SDValue> Result = TLI.LowerCallTo(CLI); |
| 662 | CurDAG->setRoot(Result.second); |
Bill Wendling | 81d4071 | 2011-01-06 00:47:10 +0000 | [diff] [blame] | 663 | } |
Anton Korobeynikov | 9091074 | 2007-09-25 21:52:30 +0000 | [diff] [blame] | 664 | } |
| 665 | |
Dan Gohman | c87b74d | 2010-04-14 20:17:22 +0000 | [diff] [blame] | 666 | void X86DAGToDAGISel::EmitFunctionEntryCode() { |
Anton Korobeynikov | 9091074 | 2007-09-25 21:52:30 +0000 | [diff] [blame] | 667 | // If this is main, emit special code for main. |
Dan Gohman | c87b74d | 2010-04-14 20:17:22 +0000 | [diff] [blame] | 668 | if (const Function *Fn = MF->getFunction()) |
| 669 | if (Fn->hasExternalLinkage() && Fn->getName() == "main") |
Sanjay Patel | 85030aa | 2015-10-13 16:23:00 +0000 | [diff] [blame^] | 670 | emitSpecialCodeForMain(); |
Anton Korobeynikov | 9091074 | 2007-09-25 21:52:30 +0000 | [diff] [blame] | 671 | } |
| 672 | |
Eli Friedman | 344ec79 | 2011-07-13 21:29:53 +0000 | [diff] [blame] | 673 | static bool isDispSafeForFrameIndex(int64_t Val) { |
| 674 | // On 64-bit platforms, we can run into an issue where a frame index |
| 675 | // includes a displacement that, when added to the explicit displacement, |
| 676 | // will overflow the displacement field. Assuming that the frame index |
| 677 | // displacement fits into a 31-bit integer (which is only slightly more |
| 678 | // aggressive than the current fundamental assumption that it fits into |
| 679 | // a 32-bit integer), a 31-bit disp should always be safe. |
| 680 | return isInt<31>(Val); |
| 681 | } |
| 682 | |
Sanjay Patel | 85030aa | 2015-10-13 16:23:00 +0000 | [diff] [blame^] | 683 | bool X86DAGToDAGISel::foldOffsetIntoAddress(uint64_t Offset, |
Eli Friedman | ef67e7d | 2011-07-13 20:44:23 +0000 | [diff] [blame] | 684 | X86ISelAddressMode &AM) { |
Reid Kleckner | 9dad227 | 2015-05-04 23:22:36 +0000 | [diff] [blame] | 685 | // Cannot combine ExternalSymbol displacements with integer offsets. |
Rafael Espindola | 36b718f | 2015-06-22 17:46:53 +0000 | [diff] [blame] | 686 | if (Offset != 0 && (AM.ES || AM.MCSym)) |
Reid Kleckner | 9dad227 | 2015-05-04 23:22:36 +0000 | [diff] [blame] | 687 | return true; |
Eli Friedman | ef67e7d | 2011-07-13 20:44:23 +0000 | [diff] [blame] | 688 | int64_t Val = AM.Disp + Offset; |
| 689 | CodeModel::Model M = TM.getCodeModel(); |
Eli Friedman | 344ec79 | 2011-07-13 21:29:53 +0000 | [diff] [blame] | 690 | if (Subtarget->is64Bit()) { |
| 691 | if (!X86::isOffsetSuitableForCodeModel(Val, M, |
| 692 | AM.hasSymbolicDisplacement())) |
| 693 | return true; |
| 694 | // In addition to the checks required for a register base, check that |
| 695 | // we do not try to use an unsafe Disp with a frame index. |
| 696 | if (AM.BaseType == X86ISelAddressMode::FrameIndexBase && |
| 697 | !isDispSafeForFrameIndex(Val)) |
| 698 | return true; |
Eli Friedman | ef67e7d | 2011-07-13 20:44:23 +0000 | [diff] [blame] | 699 | } |
Eli Friedman | 344ec79 | 2011-07-13 21:29:53 +0000 | [diff] [blame] | 700 | AM.Disp = Val; |
| 701 | return false; |
| 702 | |
Eli Friedman | ef67e7d | 2011-07-13 20:44:23 +0000 | [diff] [blame] | 703 | } |
Rafael Espindola | 3b2df10 | 2009-04-08 21:14:34 +0000 | [diff] [blame] | 704 | |
Sanjay Patel | 85030aa | 2015-10-13 16:23:00 +0000 | [diff] [blame^] | 705 | bool X86DAGToDAGISel::matchLoadInAddress(LoadSDNode *N, X86ISelAddressMode &AM){ |
Chris Lattner | 8a236b6 | 2010-09-22 04:39:11 +0000 | [diff] [blame] | 706 | SDValue Address = N->getOperand(1); |
Chad Rosier | 24c19d2 | 2012-08-01 18:39:17 +0000 | [diff] [blame] | 707 | |
Chris Lattner | 8a236b6 | 2010-09-22 04:39:11 +0000 | [diff] [blame] | 708 | // load gs:0 -> GS segment register. |
| 709 | // load fs:0 -> FS segment register. |
| 710 | // |
Rafael Espindola | 3b2df10 | 2009-04-08 21:14:34 +0000 | [diff] [blame] | 711 | // This optimization is valid because the GNU TLS model defines that |
| 712 | // gs:0 (or fs:0 on X86-64) contains its own address. |
| 713 | // For more information see http://people.redhat.com/drepper/tls.pdf |
Chris Lattner | 8a236b6 | 2010-09-22 04:39:11 +0000 | [diff] [blame] | 714 | if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Address)) |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 715 | if (C->getSExtValue() == 0 && AM.Segment.getNode() == nullptr && |
David Chisnall | 5b8c168 | 2012-07-24 20:04:16 +0000 | [diff] [blame] | 716 | Subtarget->isTargetLinux()) |
Chris Lattner | 8a236b6 | 2010-09-22 04:39:11 +0000 | [diff] [blame] | 717 | switch (N->getPointerInfo().getAddrSpace()) { |
| 718 | case 256: |
| 719 | AM.Segment = CurDAG->getRegister(X86::GS, MVT::i16); |
| 720 | return false; |
| 721 | case 257: |
| 722 | AM.Segment = CurDAG->getRegister(X86::FS, MVT::i16); |
| 723 | return false; |
| 724 | } |
Chad Rosier | 24c19d2 | 2012-08-01 18:39:17 +0000 | [diff] [blame] | 725 | |
Rafael Espindola | 3b2df10 | 2009-04-08 21:14:34 +0000 | [diff] [blame] | 726 | return true; |
| 727 | } |
| 728 | |
Sanjay Patel | b5723d0 | 2015-10-13 15:12:27 +0000 | [diff] [blame] | 729 | /// Try to match X86ISD::Wrapper and X86ISD::WrapperRIP nodes into an addressing |
| 730 | /// mode. These wrap things that will resolve down into a symbol reference. |
| 731 | /// If no match is possible, this returns true, otherwise it returns false. |
Sanjay Patel | 85030aa | 2015-10-13 16:23:00 +0000 | [diff] [blame^] | 732 | bool X86DAGToDAGISel::matchWrapper(SDValue N, X86ISelAddressMode &AM) { |
Chris Lattner | fea81da | 2009-06-27 04:16:01 +0000 | [diff] [blame] | 733 | // If the addressing mode already has a symbol as the displacement, we can |
| 734 | // never match another symbol. |
Rafael Espindola | 6688b0a | 2009-04-12 21:55:03 +0000 | [diff] [blame] | 735 | if (AM.hasSymbolicDisplacement()) |
| 736 | return true; |
Rafael Espindola | 6688b0a | 2009-04-12 21:55:03 +0000 | [diff] [blame] | 737 | |
| 738 | SDValue N0 = N.getOperand(0); |
Anton Korobeynikov | 741ea0d | 2009-08-05 23:01:26 +0000 | [diff] [blame] | 739 | CodeModel::Model M = TM.getCodeModel(); |
| 740 | |
Chris Lattner | fea81da | 2009-06-27 04:16:01 +0000 | [diff] [blame] | 741 | // Handle X86-64 rip-relative addresses. We check this before checking direct |
| 742 | // folding because RIP is preferable to non-RIP accesses. |
Chandler Carruth | 3779ac1 | 2012-04-09 02:13:06 +0000 | [diff] [blame] | 743 | if (Subtarget->is64Bit() && N.getOpcode() == X86ISD::WrapperRIP && |
Chris Lattner | fea81da | 2009-06-27 04:16:01 +0000 | [diff] [blame] | 744 | // Under X86-64 non-small code model, GV (and friends) are 64-bits, so |
| 745 | // they cannot be folded into immediate fields. |
| 746 | // FIXME: This can be improved for kernel and other models? |
Chandler Carruth | 3779ac1 | 2012-04-09 02:13:06 +0000 | [diff] [blame] | 747 | (M == CodeModel::Small || M == CodeModel::Kernel)) { |
| 748 | // Base and index reg must be 0 in order to use %rip as base. |
| 749 | if (AM.hasBaseOrIndexReg()) |
| 750 | return true; |
Chris Lattner | fea81da | 2009-06-27 04:16:01 +0000 | [diff] [blame] | 751 | if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(N0)) { |
Eli Friedman | ef67e7d | 2011-07-13 20:44:23 +0000 | [diff] [blame] | 752 | X86ISelAddressMode Backup = AM; |
Chris Lattner | fea81da | 2009-06-27 04:16:01 +0000 | [diff] [blame] | 753 | AM.GV = G->getGlobal(); |
Chris Lattner | bd7e26d | 2009-06-26 05:51:45 +0000 | [diff] [blame] | 754 | AM.SymbolFlags = G->getTargetFlags(); |
Sanjay Patel | 85030aa | 2015-10-13 16:23:00 +0000 | [diff] [blame^] | 755 | if (foldOffsetIntoAddress(G->getOffset(), AM)) { |
Eli Friedman | ef67e7d | 2011-07-13 20:44:23 +0000 | [diff] [blame] | 756 | AM = Backup; |
| 757 | return true; |
| 758 | } |
Chris Lattner | fea81da | 2009-06-27 04:16:01 +0000 | [diff] [blame] | 759 | } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N0)) { |
Eli Friedman | ef67e7d | 2011-07-13 20:44:23 +0000 | [diff] [blame] | 760 | X86ISelAddressMode Backup = AM; |
Rafael Espindola | 6688b0a | 2009-04-12 21:55:03 +0000 | [diff] [blame] | 761 | AM.CP = CP->getConstVal(); |
| 762 | AM.Align = CP->getAlignment(); |
Chris Lattner | 1d3b65a | 2009-06-26 05:56:49 +0000 | [diff] [blame] | 763 | AM.SymbolFlags = CP->getTargetFlags(); |
Sanjay Patel | 85030aa | 2015-10-13 16:23:00 +0000 | [diff] [blame^] | 764 | if (foldOffsetIntoAddress(CP->getOffset(), AM)) { |
Eli Friedman | ef67e7d | 2011-07-13 20:44:23 +0000 | [diff] [blame] | 765 | AM = Backup; |
| 766 | return true; |
| 767 | } |
Chris Lattner | fea81da | 2009-06-27 04:16:01 +0000 | [diff] [blame] | 768 | } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(N0)) { |
| 769 | AM.ES = S->getSymbol(); |
| 770 | AM.SymbolFlags = S->getTargetFlags(); |
Rafael Espindola | 36b718f | 2015-06-22 17:46:53 +0000 | [diff] [blame] | 771 | } else if (auto *S = dyn_cast<MCSymbolSDNode>(N0)) { |
| 772 | AM.MCSym = S->getMCSymbol(); |
Chris Lattner | 50ba5c3 | 2009-11-01 03:25:03 +0000 | [diff] [blame] | 773 | } else if (JumpTableSDNode *J = dyn_cast<JumpTableSDNode>(N0)) { |
Chris Lattner | fea81da | 2009-06-27 04:16:01 +0000 | [diff] [blame] | 774 | AM.JT = J->getIndex(); |
| 775 | AM.SymbolFlags = J->getTargetFlags(); |
Michael Liao | abb87d4 | 2012-09-12 21:43:09 +0000 | [diff] [blame] | 776 | } else if (BlockAddressSDNode *BA = dyn_cast<BlockAddressSDNode>(N0)) { |
| 777 | X86ISelAddressMode Backup = AM; |
| 778 | AM.BlockAddr = BA->getBlockAddress(); |
| 779 | AM.SymbolFlags = BA->getTargetFlags(); |
Sanjay Patel | 85030aa | 2015-10-13 16:23:00 +0000 | [diff] [blame^] | 780 | if (foldOffsetIntoAddress(BA->getOffset(), AM)) { |
Michael Liao | abb87d4 | 2012-09-12 21:43:09 +0000 | [diff] [blame] | 781 | AM = Backup; |
| 782 | return true; |
| 783 | } |
| 784 | } else |
| 785 | llvm_unreachable("Unhandled symbol reference node."); |
Anton Korobeynikov | 741ea0d | 2009-08-05 23:01:26 +0000 | [diff] [blame] | 786 | |
Chris Lattner | fea81da | 2009-06-27 04:16:01 +0000 | [diff] [blame] | 787 | if (N.getOpcode() == X86ISD::WrapperRIP) |
Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 788 | AM.setBaseReg(CurDAG->getRegister(X86::RIP, MVT::i64)); |
Rafael Espindola | 6688b0a | 2009-04-12 21:55:03 +0000 | [diff] [blame] | 789 | return false; |
Chris Lattner | fea81da | 2009-06-27 04:16:01 +0000 | [diff] [blame] | 790 | } |
| 791 | |
| 792 | // Handle the case when globals fit in our immediate field: This is true for |
Chandler Carruth | 3779ac1 | 2012-04-09 02:13:06 +0000 | [diff] [blame] | 793 | // X86-32 always and X86-64 when in -mcmodel=small mode. In 64-bit |
| 794 | // mode, this only applies to a non-RIP-relative computation. |
Chris Lattner | fea81da | 2009-06-27 04:16:01 +0000 | [diff] [blame] | 795 | if (!Subtarget->is64Bit() || |
Chandler Carruth | 3779ac1 | 2012-04-09 02:13:06 +0000 | [diff] [blame] | 796 | M == CodeModel::Small || M == CodeModel::Kernel) { |
| 797 | assert(N.getOpcode() != X86ISD::WrapperRIP && |
| 798 | "RIP-relative addressing already handled"); |
Chris Lattner | fea81da | 2009-06-27 04:16:01 +0000 | [diff] [blame] | 799 | if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(N0)) { |
| 800 | AM.GV = G->getGlobal(); |
| 801 | AM.Disp += G->getOffset(); |
| 802 | AM.SymbolFlags = G->getTargetFlags(); |
| 803 | } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N0)) { |
| 804 | AM.CP = CP->getConstVal(); |
| 805 | AM.Align = CP->getAlignment(); |
| 806 | AM.Disp += CP->getOffset(); |
| 807 | AM.SymbolFlags = CP->getTargetFlags(); |
| 808 | } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(N0)) { |
| 809 | AM.ES = S->getSymbol(); |
| 810 | AM.SymbolFlags = S->getTargetFlags(); |
Rafael Espindola | 36b718f | 2015-06-22 17:46:53 +0000 | [diff] [blame] | 811 | } else if (auto *S = dyn_cast<MCSymbolSDNode>(N0)) { |
| 812 | AM.MCSym = S->getMCSymbol(); |
Chris Lattner | 50ba5c3 | 2009-11-01 03:25:03 +0000 | [diff] [blame] | 813 | } else if (JumpTableSDNode *J = dyn_cast<JumpTableSDNode>(N0)) { |
Chris Lattner | fea81da | 2009-06-27 04:16:01 +0000 | [diff] [blame] | 814 | AM.JT = J->getIndex(); |
| 815 | AM.SymbolFlags = J->getTargetFlags(); |
Michael Liao | abb87d4 | 2012-09-12 21:43:09 +0000 | [diff] [blame] | 816 | } else if (BlockAddressSDNode *BA = dyn_cast<BlockAddressSDNode>(N0)) { |
| 817 | AM.BlockAddr = BA->getBlockAddress(); |
| 818 | AM.Disp += BA->getOffset(); |
| 819 | AM.SymbolFlags = BA->getTargetFlags(); |
| 820 | } else |
| 821 | llvm_unreachable("Unhandled symbol reference node."); |
Rafael Espindola | 6688b0a | 2009-04-12 21:55:03 +0000 | [diff] [blame] | 822 | return false; |
| 823 | } |
| 824 | |
| 825 | return true; |
| 826 | } |
| 827 | |
Sanjay Patel | b5723d0 | 2015-10-13 15:12:27 +0000 | [diff] [blame] | 828 | /// Add the specified node to the specified addressing mode, returning true if |
| 829 | /// it cannot be done. This just pattern matches for the addressing mode. |
Sanjay Patel | 85030aa | 2015-10-13 16:23:00 +0000 | [diff] [blame^] | 830 | bool X86DAGToDAGISel::matchAddress(SDValue N, X86ISelAddressMode &AM) { |
| 831 | if (matchAddressRecursively(N, AM, 0)) |
Dan Gohman | 824ab40 | 2009-07-22 23:26:55 +0000 | [diff] [blame] | 832 | return true; |
| 833 | |
| 834 | // Post-processing: Convert lea(,%reg,2) to lea(%reg,%reg), which has |
| 835 | // a smaller encoding and avoids a scaled-index. |
| 836 | if (AM.Scale == 2 && |
| 837 | AM.BaseType == X86ISelAddressMode::RegBase && |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 838 | AM.Base_Reg.getNode() == nullptr) { |
Dan Gohman | 0fd54fb | 2010-04-29 23:30:41 +0000 | [diff] [blame] | 839 | AM.Base_Reg = AM.IndexReg; |
Dan Gohman | 824ab40 | 2009-07-22 23:26:55 +0000 | [diff] [blame] | 840 | AM.Scale = 1; |
| 841 | } |
| 842 | |
Dan Gohman | 0504608 | 2009-08-20 18:23:44 +0000 | [diff] [blame] | 843 | // Post-processing: Convert foo to foo(%rip), even in non-PIC mode, |
| 844 | // because it has a smaller encoding. |
| 845 | // TODO: Which other code models can use this? |
| 846 | if (TM.getCodeModel() == CodeModel::Small && |
| 847 | Subtarget->is64Bit() && |
| 848 | AM.Scale == 1 && |
| 849 | AM.BaseType == X86ISelAddressMode::RegBase && |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 850 | AM.Base_Reg.getNode() == nullptr && |
| 851 | AM.IndexReg.getNode() == nullptr && |
Dan Gohman | 0f6bf2d | 2009-08-25 17:47:44 +0000 | [diff] [blame] | 852 | AM.SymbolFlags == X86II::MO_NO_FLAG && |
Dan Gohman | 0504608 | 2009-08-20 18:23:44 +0000 | [diff] [blame] | 853 | AM.hasSymbolicDisplacement()) |
Dan Gohman | 0fd54fb | 2010-04-29 23:30:41 +0000 | [diff] [blame] | 854 | AM.Base_Reg = CurDAG->getRegister(X86::RIP, MVT::i64); |
Dan Gohman | 0504608 | 2009-08-20 18:23:44 +0000 | [diff] [blame] | 855 | |
Dan Gohman | 824ab40 | 2009-07-22 23:26:55 +0000 | [diff] [blame] | 856 | return false; |
| 857 | } |
| 858 | |
Chandler Carruth | 3eacfb8 | 2012-01-11 11:04:36 +0000 | [diff] [blame] | 859 | // Insert a node into the DAG at least before the Pos node's position. This |
| 860 | // will reposition the node as needed, and will assign it a node ID that is <= |
| 861 | // the Pos node's ID. Note that this does *not* preserve the uniqueness of node |
| 862 | // IDs! The selection DAG must no longer depend on their uniqueness when this |
| 863 | // is used. |
Sanjay Patel | 85030aa | 2015-10-13 16:23:00 +0000 | [diff] [blame^] | 864 | static void insertDAGNode(SelectionDAG &DAG, SDValue Pos, SDValue N) { |
Chandler Carruth | 3eacfb8 | 2012-01-11 11:04:36 +0000 | [diff] [blame] | 865 | if (N.getNode()->getNodeId() == -1 || |
| 866 | N.getNode()->getNodeId() > Pos.getNode()->getNodeId()) { |
| 867 | DAG.RepositionNode(Pos.getNode(), N.getNode()); |
| 868 | N.getNode()->setNodeId(Pos.getNode()->getNodeId()); |
| 869 | } |
| 870 | } |
| 871 | |
Adam Nemet | 0c7caf4 | 2014-09-16 17:14:10 +0000 | [diff] [blame] | 872 | // Transform "(X >> (8-C1)) & (0xff << C1)" to "((X >> 8) & 0xff) << C1" if |
| 873 | // safe. This allows us to convert the shift and and into an h-register |
| 874 | // extract and a scaled index. Returns false if the simplification is |
| 875 | // performed. |
Sanjay Patel | 85030aa | 2015-10-13 16:23:00 +0000 | [diff] [blame^] | 876 | static bool foldMaskAndShiftToExtract(SelectionDAG &DAG, SDValue N, |
Chandler Carruth | 51d3076 | 2012-01-11 08:48:20 +0000 | [diff] [blame] | 877 | uint64_t Mask, |
| 878 | SDValue Shift, SDValue X, |
| 879 | X86ISelAddressMode &AM) { |
| 880 | if (Shift.getOpcode() != ISD::SRL || |
| 881 | !isa<ConstantSDNode>(Shift.getOperand(1)) || |
| 882 | !Shift.hasOneUse()) |
| 883 | return true; |
| 884 | |
| 885 | int ScaleLog = 8 - Shift.getConstantOperandVal(1); |
| 886 | if (ScaleLog <= 0 || ScaleLog >= 4 || |
| 887 | Mask != (0xffu << ScaleLog)) |
| 888 | return true; |
| 889 | |
Craig Topper | 83e042a | 2013-08-15 05:57:07 +0000 | [diff] [blame] | 890 | MVT VT = N.getSimpleValueType(); |
Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 891 | SDLoc DL(N); |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 892 | SDValue Eight = DAG.getConstant(8, DL, MVT::i8); |
| 893 | SDValue NewMask = DAG.getConstant(0xff, DL, VT); |
Chandler Carruth | 51d3076 | 2012-01-11 08:48:20 +0000 | [diff] [blame] | 894 | SDValue Srl = DAG.getNode(ISD::SRL, DL, VT, X, Eight); |
| 895 | SDValue And = DAG.getNode(ISD::AND, DL, VT, Srl, NewMask); |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 896 | SDValue ShlCount = DAG.getConstant(ScaleLog, DL, MVT::i8); |
Chandler Carruth | 51d3076 | 2012-01-11 08:48:20 +0000 | [diff] [blame] | 897 | SDValue Shl = DAG.getNode(ISD::SHL, DL, VT, And, ShlCount); |
| 898 | |
Chandler Carruth | eb21da0 | 2012-01-12 01:34:44 +0000 | [diff] [blame] | 899 | // Insert the new nodes into the topological ordering. We must do this in |
| 900 | // a valid topological ordering as nothing is going to go back and re-sort |
| 901 | // these nodes. We continually insert before 'N' in sequence as this is |
| 902 | // essentially a pre-flattened and pre-sorted sequence of nodes. There is no |
| 903 | // hierarchy left to express. |
Sanjay Patel | 85030aa | 2015-10-13 16:23:00 +0000 | [diff] [blame^] | 904 | insertDAGNode(DAG, N, Eight); |
| 905 | insertDAGNode(DAG, N, Srl); |
| 906 | insertDAGNode(DAG, N, NewMask); |
| 907 | insertDAGNode(DAG, N, And); |
| 908 | insertDAGNode(DAG, N, ShlCount); |
| 909 | insertDAGNode(DAG, N, Shl); |
Chandler Carruth | 51d3076 | 2012-01-11 08:48:20 +0000 | [diff] [blame] | 910 | DAG.ReplaceAllUsesWith(N, Shl); |
| 911 | AM.IndexReg = And; |
| 912 | AM.Scale = (1 << ScaleLog); |
| 913 | return false; |
| 914 | } |
| 915 | |
Chandler Carruth | aa01e66 | 2012-01-11 09:35:00 +0000 | [diff] [blame] | 916 | // Transforms "(X << C1) & C2" to "(X & (C2>>C1)) << C1" if safe and if this |
| 917 | // allows us to fold the shift into this addressing mode. Returns false if the |
| 918 | // transform succeeded. |
Sanjay Patel | 85030aa | 2015-10-13 16:23:00 +0000 | [diff] [blame^] | 919 | static bool foldMaskedShiftToScaledMask(SelectionDAG &DAG, SDValue N, |
Chandler Carruth | aa01e66 | 2012-01-11 09:35:00 +0000 | [diff] [blame] | 920 | uint64_t Mask, |
| 921 | SDValue Shift, SDValue X, |
| 922 | X86ISelAddressMode &AM) { |
| 923 | if (Shift.getOpcode() != ISD::SHL || |
| 924 | !isa<ConstantSDNode>(Shift.getOperand(1))) |
| 925 | return true; |
| 926 | |
| 927 | // Not likely to be profitable if either the AND or SHIFT node has more |
| 928 | // than one use (unless all uses are for address computation). Besides, |
| 929 | // isel mechanism requires their node ids to be reused. |
| 930 | if (!N.hasOneUse() || !Shift.hasOneUse()) |
| 931 | return true; |
| 932 | |
| 933 | // Verify that the shift amount is something we can fold. |
| 934 | unsigned ShiftAmt = Shift.getConstantOperandVal(1); |
| 935 | if (ShiftAmt != 1 && ShiftAmt != 2 && ShiftAmt != 3) |
| 936 | return true; |
| 937 | |
Craig Topper | 83e042a | 2013-08-15 05:57:07 +0000 | [diff] [blame] | 938 | MVT VT = N.getSimpleValueType(); |
Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 939 | SDLoc DL(N); |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 940 | SDValue NewMask = DAG.getConstant(Mask >> ShiftAmt, DL, VT); |
Chandler Carruth | aa01e66 | 2012-01-11 09:35:00 +0000 | [diff] [blame] | 941 | SDValue NewAnd = DAG.getNode(ISD::AND, DL, VT, X, NewMask); |
| 942 | SDValue NewShift = DAG.getNode(ISD::SHL, DL, VT, NewAnd, Shift.getOperand(1)); |
| 943 | |
Chandler Carruth | eb21da0 | 2012-01-12 01:34:44 +0000 | [diff] [blame] | 944 | // Insert the new nodes into the topological ordering. We must do this in |
| 945 | // a valid topological ordering as nothing is going to go back and re-sort |
| 946 | // these nodes. We continually insert before 'N' in sequence as this is |
| 947 | // essentially a pre-flattened and pre-sorted sequence of nodes. There is no |
| 948 | // hierarchy left to express. |
Sanjay Patel | 85030aa | 2015-10-13 16:23:00 +0000 | [diff] [blame^] | 949 | insertDAGNode(DAG, N, NewMask); |
| 950 | insertDAGNode(DAG, N, NewAnd); |
| 951 | insertDAGNode(DAG, N, NewShift); |
Chandler Carruth | aa01e66 | 2012-01-11 09:35:00 +0000 | [diff] [blame] | 952 | DAG.ReplaceAllUsesWith(N, NewShift); |
| 953 | |
| 954 | AM.Scale = 1 << ShiftAmt; |
| 955 | AM.IndexReg = NewAnd; |
| 956 | return false; |
| 957 | } |
| 958 | |
Chandler Carruth | 55b2cde | 2012-01-11 08:41:08 +0000 | [diff] [blame] | 959 | // Implement some heroics to detect shifts of masked values where the mask can |
| 960 | // be replaced by extending the shift and undoing that in the addressing mode |
| 961 | // scale. Patterns such as (shl (srl x, c1), c2) are canonicalized into (and |
| 962 | // (srl x, SHIFT), MASK) by DAGCombines that don't know the shl can be done in |
| 963 | // the addressing mode. This results in code such as: |
| 964 | // |
| 965 | // int f(short *y, int *lookup_table) { |
| 966 | // ... |
| 967 | // return *y + lookup_table[*y >> 11]; |
| 968 | // } |
| 969 | // |
| 970 | // Turning into: |
| 971 | // movzwl (%rdi), %eax |
| 972 | // movl %eax, %ecx |
| 973 | // shrl $11, %ecx |
| 974 | // addl (%rsi,%rcx,4), %eax |
| 975 | // |
| 976 | // Instead of: |
| 977 | // movzwl (%rdi), %eax |
| 978 | // movl %eax, %ecx |
| 979 | // shrl $9, %ecx |
| 980 | // andl $124, %rcx |
| 981 | // addl (%rsi,%rcx), %eax |
| 982 | // |
Chandler Carruth | 3dbcda8 | 2012-01-11 09:35:02 +0000 | [diff] [blame] | 983 | // Note that this function assumes the mask is provided as a mask *after* the |
| 984 | // value is shifted. The input chain may or may not match that, but computing |
| 985 | // such a mask is trivial. |
Sanjay Patel | 85030aa | 2015-10-13 16:23:00 +0000 | [diff] [blame^] | 986 | static bool foldMaskAndShiftToScale(SelectionDAG &DAG, SDValue N, |
Chandler Carruth | 3dbcda8 | 2012-01-11 09:35:02 +0000 | [diff] [blame] | 987 | uint64_t Mask, |
| 988 | SDValue Shift, SDValue X, |
Chandler Carruth | 55b2cde | 2012-01-11 08:41:08 +0000 | [diff] [blame] | 989 | X86ISelAddressMode &AM) { |
Chandler Carruth | 3dbcda8 | 2012-01-11 09:35:02 +0000 | [diff] [blame] | 990 | if (Shift.getOpcode() != ISD::SRL || !Shift.hasOneUse() || |
| 991 | !isa<ConstantSDNode>(Shift.getOperand(1))) |
Chandler Carruth | 55b2cde | 2012-01-11 08:41:08 +0000 | [diff] [blame] | 992 | return true; |
Chandler Carruth | 55b2cde | 2012-01-11 08:41:08 +0000 | [diff] [blame] | 993 | |
Chandler Carruth | 55b2cde | 2012-01-11 08:41:08 +0000 | [diff] [blame] | 994 | unsigned ShiftAmt = Shift.getConstantOperandVal(1); |
Michael J. Spencer | df1ecbd7 | 2013-05-24 22:23:49 +0000 | [diff] [blame] | 995 | unsigned MaskLZ = countLeadingZeros(Mask); |
| 996 | unsigned MaskTZ = countTrailingZeros(Mask); |
Chandler Carruth | 55b2cde | 2012-01-11 08:41:08 +0000 | [diff] [blame] | 997 | |
| 998 | // The amount of shift we're trying to fit into the addressing mode is taken |
Chandler Carruth | 3dbcda8 | 2012-01-11 09:35:02 +0000 | [diff] [blame] | 999 | // from the trailing zeros of the mask. |
| 1000 | unsigned AMShiftAmt = MaskTZ; |
Chandler Carruth | 55b2cde | 2012-01-11 08:41:08 +0000 | [diff] [blame] | 1001 | |
| 1002 | // There is nothing we can do here unless the mask is removing some bits. |
| 1003 | // Also, the addressing mode can only represent shifts of 1, 2, or 3 bits. |
| 1004 | if (AMShiftAmt <= 0 || AMShiftAmt > 3) return true; |
| 1005 | |
| 1006 | // We also need to ensure that mask is a continuous run of bits. |
Benjamin Kramer | 5f6a907 | 2015-02-12 15:35:40 +0000 | [diff] [blame] | 1007 | if (countTrailingOnes(Mask >> MaskTZ) + MaskTZ + MaskLZ != 64) return true; |
Chandler Carruth | 55b2cde | 2012-01-11 08:41:08 +0000 | [diff] [blame] | 1008 | |
| 1009 | // Scale the leading zero count down based on the actual size of the value. |
Chandler Carruth | 3dbcda8 | 2012-01-11 09:35:02 +0000 | [diff] [blame] | 1010 | // Also scale it down based on the size of the shift. |
Craig Topper | 83e042a | 2013-08-15 05:57:07 +0000 | [diff] [blame] | 1011 | MaskLZ -= (64 - X.getSimpleValueType().getSizeInBits()) + ShiftAmt; |
Chandler Carruth | 55b2cde | 2012-01-11 08:41:08 +0000 | [diff] [blame] | 1012 | |
| 1013 | // The final check is to ensure that any masked out high bits of X are |
| 1014 | // already known to be zero. Otherwise, the mask has a semantic impact |
| 1015 | // other than masking out a couple of low bits. Unfortunately, because of |
| 1016 | // the mask, zero extensions will be removed from operands in some cases. |
| 1017 | // This code works extra hard to look through extensions because we can |
| 1018 | // replace them with zero extensions cheaply if necessary. |
| 1019 | bool ReplacingAnyExtend = false; |
| 1020 | if (X.getOpcode() == ISD::ANY_EXTEND) { |
Craig Topper | 83e042a | 2013-08-15 05:57:07 +0000 | [diff] [blame] | 1021 | unsigned ExtendBits = X.getSimpleValueType().getSizeInBits() - |
| 1022 | X.getOperand(0).getSimpleValueType().getSizeInBits(); |
Chandler Carruth | 55b2cde | 2012-01-11 08:41:08 +0000 | [diff] [blame] | 1023 | // Assume that we'll replace the any-extend with a zero-extend, and |
| 1024 | // narrow the search to the extended value. |
| 1025 | X = X.getOperand(0); |
| 1026 | MaskLZ = ExtendBits > MaskLZ ? 0 : MaskLZ - ExtendBits; |
| 1027 | ReplacingAnyExtend = true; |
| 1028 | } |
Craig Topper | 83e042a | 2013-08-15 05:57:07 +0000 | [diff] [blame] | 1029 | APInt MaskedHighBits = |
| 1030 | APInt::getHighBitsSet(X.getSimpleValueType().getSizeInBits(), MaskLZ); |
Chandler Carruth | 55b2cde | 2012-01-11 08:41:08 +0000 | [diff] [blame] | 1031 | APInt KnownZero, KnownOne; |
Jay Foad | a0653a3 | 2014-05-14 21:14:37 +0000 | [diff] [blame] | 1032 | DAG.computeKnownBits(X, KnownZero, KnownOne); |
Chandler Carruth | 55b2cde | 2012-01-11 08:41:08 +0000 | [diff] [blame] | 1033 | if (MaskedHighBits != KnownZero) return true; |
| 1034 | |
| 1035 | // We've identified a pattern that can be transformed into a single shift |
| 1036 | // and an addressing mode. Make it so. |
Craig Topper | 83e042a | 2013-08-15 05:57:07 +0000 | [diff] [blame] | 1037 | MVT VT = N.getSimpleValueType(); |
Chandler Carruth | 55b2cde | 2012-01-11 08:41:08 +0000 | [diff] [blame] | 1038 | if (ReplacingAnyExtend) { |
| 1039 | assert(X.getValueType() != VT); |
| 1040 | // We looked through an ANY_EXTEND node, insert a ZERO_EXTEND. |
Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1041 | SDValue NewX = DAG.getNode(ISD::ZERO_EXTEND, SDLoc(X), VT, X); |
Sanjay Patel | 85030aa | 2015-10-13 16:23:00 +0000 | [diff] [blame^] | 1042 | insertDAGNode(DAG, N, NewX); |
Chandler Carruth | 55b2cde | 2012-01-11 08:41:08 +0000 | [diff] [blame] | 1043 | X = NewX; |
| 1044 | } |
Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1045 | SDLoc DL(N); |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1046 | SDValue NewSRLAmt = DAG.getConstant(ShiftAmt + AMShiftAmt, DL, MVT::i8); |
Chandler Carruth | 55b2cde | 2012-01-11 08:41:08 +0000 | [diff] [blame] | 1047 | SDValue NewSRL = DAG.getNode(ISD::SRL, DL, VT, X, NewSRLAmt); |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1048 | SDValue NewSHLAmt = DAG.getConstant(AMShiftAmt, DL, MVT::i8); |
Chandler Carruth | 55b2cde | 2012-01-11 08:41:08 +0000 | [diff] [blame] | 1049 | SDValue NewSHL = DAG.getNode(ISD::SHL, DL, VT, NewSRL, NewSHLAmt); |
Chandler Carruth | eb21da0 | 2012-01-12 01:34:44 +0000 | [diff] [blame] | 1050 | |
| 1051 | // Insert the new nodes into the topological ordering. We must do this in |
| 1052 | // a valid topological ordering as nothing is going to go back and re-sort |
| 1053 | // these nodes. We continually insert before 'N' in sequence as this is |
| 1054 | // essentially a pre-flattened and pre-sorted sequence of nodes. There is no |
| 1055 | // hierarchy left to express. |
Sanjay Patel | 85030aa | 2015-10-13 16:23:00 +0000 | [diff] [blame^] | 1056 | insertDAGNode(DAG, N, NewSRLAmt); |
| 1057 | insertDAGNode(DAG, N, NewSRL); |
| 1058 | insertDAGNode(DAG, N, NewSHLAmt); |
| 1059 | insertDAGNode(DAG, N, NewSHL); |
Chandler Carruth | 55b2cde | 2012-01-11 08:41:08 +0000 | [diff] [blame] | 1060 | DAG.ReplaceAllUsesWith(N, NewSHL); |
| 1061 | |
| 1062 | AM.Scale = 1 << AMShiftAmt; |
| 1063 | AM.IndexReg = NewSRL; |
| 1064 | return false; |
| 1065 | } |
| 1066 | |
Sanjay Patel | 85030aa | 2015-10-13 16:23:00 +0000 | [diff] [blame^] | 1067 | bool X86DAGToDAGISel::matchAddressRecursively(SDValue N, X86ISelAddressMode &AM, |
Dan Gohman | 824ab40 | 2009-07-22 23:26:55 +0000 | [diff] [blame] | 1068 | unsigned Depth) { |
Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1069 | SDLoc dl(N); |
Bill Wendling | fe3bdb4 | 2009-08-07 21:33:25 +0000 | [diff] [blame] | 1070 | DEBUG({ |
David Greene | dbdb1b2 | 2010-01-05 01:29:08 +0000 | [diff] [blame] | 1071 | dbgs() << "MatchAddress: "; |
Bill Wendling | fe3bdb4 | 2009-08-07 21:33:25 +0000 | [diff] [blame] | 1072 | AM.dump(); |
| 1073 | }); |
Dan Gohman | ccb3611 | 2007-08-13 20:03:06 +0000 | [diff] [blame] | 1074 | // Limit recursion. |
| 1075 | if (Depth > 5) |
Sanjay Patel | 85030aa | 2015-10-13 16:23:00 +0000 | [diff] [blame^] | 1076 | return matchAddressBase(N, AM); |
Anton Korobeynikov | 741ea0d | 2009-08-05 23:01:26 +0000 | [diff] [blame] | 1077 | |
Chris Lattner | fea81da | 2009-06-27 04:16:01 +0000 | [diff] [blame] | 1078 | // If this is already a %rip relative address, we can only merge immediates |
| 1079 | // into it. Instead of handling this in every case, we handle it here. |
Evan Cheng | 11b0a5d | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 1080 | // RIP relative addressing: %rip + 32-bit displacement! |
Chris Lattner | fea81da | 2009-06-27 04:16:01 +0000 | [diff] [blame] | 1081 | if (AM.isRIPRelative()) { |
| 1082 | // FIXME: JumpTable and ExternalSymbol address currently don't like |
| 1083 | // displacements. It isn't very important, but this should be fixed for |
| 1084 | // consistency. |
Rafael Espindola | 36b718f | 2015-06-22 17:46:53 +0000 | [diff] [blame] | 1085 | if (!(AM.ES || AM.MCSym) && AM.JT != -1) |
| 1086 | return true; |
Anton Korobeynikov | 741ea0d | 2009-08-05 23:01:26 +0000 | [diff] [blame] | 1087 | |
Eli Friedman | ef67e7d | 2011-07-13 20:44:23 +0000 | [diff] [blame] | 1088 | if (ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(N)) |
Sanjay Patel | 85030aa | 2015-10-13 16:23:00 +0000 | [diff] [blame^] | 1089 | if (!foldOffsetIntoAddress(Cst->getSExtValue(), AM)) |
Evan Cheng | 11b0a5d | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 1090 | return false; |
Evan Cheng | 11b0a5d | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 1091 | return true; |
| 1092 | } |
| 1093 | |
Chris Lattner | 3f0f71b | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 1094 | switch (N.getOpcode()) { |
| 1095 | default: break; |
Reid Kleckner | 6038179 | 2015-07-07 22:25:32 +0000 | [diff] [blame] | 1096 | case ISD::LOCAL_RECOVER: { |
Reid Kleckner | 9dad227 | 2015-05-04 23:22:36 +0000 | [diff] [blame] | 1097 | if (!AM.hasSymbolicDisplacement() && AM.Disp == 0) |
Rafael Espindola | 36b718f | 2015-06-22 17:46:53 +0000 | [diff] [blame] | 1098 | if (const auto *ESNode = dyn_cast<MCSymbolSDNode>(N.getOperand(0))) { |
| 1099 | // Use the symbol and don't prefix it. |
| 1100 | AM.MCSym = ESNode->getMCSymbol(); |
| 1101 | return false; |
| 1102 | } |
David Majnemer | 71b9b6b | 2015-03-05 18:50:12 +0000 | [diff] [blame] | 1103 | break; |
| 1104 | } |
Evan Cheng | 11b0a5d | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 1105 | case ISD::Constant: { |
Dan Gohman | 059c4fa | 2008-11-11 15:52:29 +0000 | [diff] [blame] | 1106 | uint64_t Val = cast<ConstantSDNode>(N)->getSExtValue(); |
Sanjay Patel | 85030aa | 2015-10-13 16:23:00 +0000 | [diff] [blame^] | 1107 | if (!foldOffsetIntoAddress(Val, AM)) |
Evan Cheng | 11b0a5d | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 1108 | return false; |
Evan Cheng | 11b0a5d | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 1109 | break; |
| 1110 | } |
Evan Cheng | 77d86ff | 2006-02-25 10:09:08 +0000 | [diff] [blame] | 1111 | |
Rafael Espindola | 6688b0a | 2009-04-12 21:55:03 +0000 | [diff] [blame] | 1112 | case X86ISD::Wrapper: |
Chris Lattner | fea81da | 2009-06-27 04:16:01 +0000 | [diff] [blame] | 1113 | case X86ISD::WrapperRIP: |
Sanjay Patel | 85030aa | 2015-10-13 16:23:00 +0000 | [diff] [blame^] | 1114 | if (!matchWrapper(N, AM)) |
Rafael Espindola | 6688b0a | 2009-04-12 21:55:03 +0000 | [diff] [blame] | 1115 | return false; |
Evan Cheng | 77d86ff | 2006-02-25 10:09:08 +0000 | [diff] [blame] | 1116 | break; |
| 1117 | |
Rafael Espindola | 3b2df10 | 2009-04-08 21:14:34 +0000 | [diff] [blame] | 1118 | case ISD::LOAD: |
Sanjay Patel | 85030aa | 2015-10-13 16:23:00 +0000 | [diff] [blame^] | 1119 | if (!matchLoadInAddress(cast<LoadSDNode>(N), AM)) |
Rafael Espindola | 3b2df10 | 2009-04-08 21:14:34 +0000 | [diff] [blame] | 1120 | return false; |
| 1121 | break; |
| 1122 | |
Chris Lattner | 3f0f71b | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 1123 | case ISD::FrameIndex: |
Eli Friedman | 344ec79 | 2011-07-13 21:29:53 +0000 | [diff] [blame] | 1124 | if (AM.BaseType == X86ISelAddressMode::RegBase && |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 1125 | AM.Base_Reg.getNode() == nullptr && |
Eli Friedman | 344ec79 | 2011-07-13 21:29:53 +0000 | [diff] [blame] | 1126 | (!Subtarget->is64Bit() || isDispSafeForFrameIndex(AM.Disp))) { |
Chris Lattner | 3f0f71b | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 1127 | AM.BaseType = X86ISelAddressMode::FrameIndexBase; |
Dan Gohman | 0fd54fb | 2010-04-29 23:30:41 +0000 | [diff] [blame] | 1128 | AM.Base_FrameIndex = cast<FrameIndexSDNode>(N)->getIndex(); |
Chris Lattner | 3f0f71b | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 1129 | return false; |
| 1130 | } |
| 1131 | break; |
Evan Cheng | c9fab31 | 2005-12-08 02:01:35 +0000 | [diff] [blame] | 1132 | |
Chris Lattner | 3f0f71b | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 1133 | case ISD::SHL: |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 1134 | if (AM.IndexReg.getNode() != nullptr || AM.Scale != 1) |
Chris Lattner | ff87f05e | 2007-12-08 07:22:58 +0000 | [diff] [blame] | 1135 | break; |
Chad Rosier | 24c19d2 | 2012-08-01 18:39:17 +0000 | [diff] [blame] | 1136 | |
Gabor Greif | 81d6a38 | 2008-08-31 15:37:04 +0000 | [diff] [blame] | 1137 | if (ConstantSDNode |
| 1138 | *CN = dyn_cast<ConstantSDNode>(N.getNode()->getOperand(1))) { |
Dan Gohman | effb894 | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 1139 | unsigned Val = CN->getZExtValue(); |
Dan Gohman | 824ab40 | 2009-07-22 23:26:55 +0000 | [diff] [blame] | 1140 | // Note that we handle x<<1 as (,x,2) rather than (x,x) here so |
| 1141 | // that the base operand remains free for further matching. If |
| 1142 | // the base doesn't end up getting used, a post-processing step |
| 1143 | // in MatchAddress turns (,x,2) into (x,x), which is cheaper. |
Chris Lattner | ff87f05e | 2007-12-08 07:22:58 +0000 | [diff] [blame] | 1144 | if (Val == 1 || Val == 2 || Val == 3) { |
| 1145 | AM.Scale = 1 << Val; |
Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1146 | SDValue ShVal = N.getNode()->getOperand(0); |
Chris Lattner | 3f0f71b | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 1147 | |
Chris Lattner | ff87f05e | 2007-12-08 07:22:58 +0000 | [diff] [blame] | 1148 | // Okay, we know that we have a scale by now. However, if the scaled |
| 1149 | // value is an add of something and a constant, we can fold the |
| 1150 | // constant into the disp field here. |
Chris Lattner | 46c01a3 | 2011-02-13 22:25:43 +0000 | [diff] [blame] | 1151 | if (CurDAG->isBaseWithConstantOffset(ShVal)) { |
Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1152 | AM.IndexReg = ShVal.getNode()->getOperand(0); |
Chris Lattner | ff87f05e | 2007-12-08 07:22:58 +0000 | [diff] [blame] | 1153 | ConstantSDNode *AddVal = |
Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1154 | cast<ConstantSDNode>(ShVal.getNode()->getOperand(1)); |
Richard Smith | 228e6d4 | 2012-08-24 23:29:28 +0000 | [diff] [blame] | 1155 | uint64_t Disp = (uint64_t)AddVal->getSExtValue() << Val; |
Sanjay Patel | 85030aa | 2015-10-13 16:23:00 +0000 | [diff] [blame^] | 1156 | if (!foldOffsetIntoAddress(Disp, AM)) |
Eli Friedman | ef67e7d | 2011-07-13 20:44:23 +0000 | [diff] [blame] | 1157 | return false; |
Chris Lattner | 3f0f71b | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 1158 | } |
Eli Friedman | ef67e7d | 2011-07-13 20:44:23 +0000 | [diff] [blame] | 1159 | |
| 1160 | AM.IndexReg = ShVal; |
Chris Lattner | ff87f05e | 2007-12-08 07:22:58 +0000 | [diff] [blame] | 1161 | return false; |
Chris Lattner | 3f0f71b | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 1162 | } |
Chris Lattner | ff87f05e | 2007-12-08 07:22:58 +0000 | [diff] [blame] | 1163 | } |
Jakub Staszak | 43fafaf | 2013-01-04 23:01:26 +0000 | [diff] [blame] | 1164 | break; |
Evan Cheng | c9fab31 | 2005-12-08 02:01:35 +0000 | [diff] [blame] | 1165 | |
Chandler Carruth | 3dbcda8 | 2012-01-11 09:35:02 +0000 | [diff] [blame] | 1166 | case ISD::SRL: { |
| 1167 | // Scale must not be used already. |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 1168 | if (AM.IndexReg.getNode() != nullptr || AM.Scale != 1) break; |
Chandler Carruth | 3dbcda8 | 2012-01-11 09:35:02 +0000 | [diff] [blame] | 1169 | |
| 1170 | SDValue And = N.getOperand(0); |
| 1171 | if (And.getOpcode() != ISD::AND) break; |
| 1172 | SDValue X = And.getOperand(0); |
| 1173 | |
| 1174 | // We only handle up to 64-bit values here as those are what matter for |
| 1175 | // addressing mode optimizations. |
Craig Topper | 83e042a | 2013-08-15 05:57:07 +0000 | [diff] [blame] | 1176 | if (X.getSimpleValueType().getSizeInBits() > 64) break; |
Chandler Carruth | 3dbcda8 | 2012-01-11 09:35:02 +0000 | [diff] [blame] | 1177 | |
| 1178 | // The mask used for the transform is expected to be post-shift, but we |
| 1179 | // found the shift first so just apply the shift to the mask before passing |
| 1180 | // it down. |
| 1181 | if (!isa<ConstantSDNode>(N.getOperand(1)) || |
| 1182 | !isa<ConstantSDNode>(And.getOperand(1))) |
| 1183 | break; |
| 1184 | uint64_t Mask = And.getConstantOperandVal(1) >> N.getConstantOperandVal(1); |
| 1185 | |
Chandler Carruth | 55b2cde | 2012-01-11 08:41:08 +0000 | [diff] [blame] | 1186 | // Try to fold the mask and shift into the scale, and return false if we |
| 1187 | // succeed. |
Sanjay Patel | 85030aa | 2015-10-13 16:23:00 +0000 | [diff] [blame^] | 1188 | if (!foldMaskAndShiftToScale(*CurDAG, N, Mask, N, X, AM)) |
Chandler Carruth | 55b2cde | 2012-01-11 08:41:08 +0000 | [diff] [blame] | 1189 | return false; |
| 1190 | break; |
Chandler Carruth | 3dbcda8 | 2012-01-11 09:35:02 +0000 | [diff] [blame] | 1191 | } |
Chandler Carruth | 55b2cde | 2012-01-11 08:41:08 +0000 | [diff] [blame] | 1192 | |
Dan Gohman | bf47495 | 2007-10-22 20:22:24 +0000 | [diff] [blame] | 1193 | case ISD::SMUL_LOHI: |
| 1194 | case ISD::UMUL_LOHI: |
| 1195 | // A mul_lohi where we need the low part can be folded as a plain multiply. |
Gabor Greif | abfdf92 | 2008-08-26 22:36:50 +0000 | [diff] [blame] | 1196 | if (N.getResNo() != 0) break; |
Dan Gohman | bf47495 | 2007-10-22 20:22:24 +0000 | [diff] [blame] | 1197 | // FALL THROUGH |
Chris Lattner | 3f0f71b | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 1198 | case ISD::MUL: |
Evan Cheng | a84a318 | 2009-03-30 21:36:47 +0000 | [diff] [blame] | 1199 | case X86ISD::MUL_IMM: |
Chris Lattner | 3f0f71b | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 1200 | // X*[3,5,9] -> X+X*[2,4,8] |
Dan Gohman | f14b77e | 2008-11-05 04:14:16 +0000 | [diff] [blame] | 1201 | if (AM.BaseType == X86ISelAddressMode::RegBase && |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 1202 | AM.Base_Reg.getNode() == nullptr && |
| 1203 | AM.IndexReg.getNode() == nullptr) { |
Gabor Greif | 81d6a38 | 2008-08-31 15:37:04 +0000 | [diff] [blame] | 1204 | if (ConstantSDNode |
| 1205 | *CN = dyn_cast<ConstantSDNode>(N.getNode()->getOperand(1))) |
Dan Gohman | effb894 | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 1206 | if (CN->getZExtValue() == 3 || CN->getZExtValue() == 5 || |
| 1207 | CN->getZExtValue() == 9) { |
| 1208 | AM.Scale = unsigned(CN->getZExtValue())-1; |
Chris Lattner | 3f0f71b | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 1209 | |
Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1210 | SDValue MulVal = N.getNode()->getOperand(0); |
Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1211 | SDValue Reg; |
Chris Lattner | 3f0f71b | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 1212 | |
| 1213 | // Okay, we know that we have a scale by now. However, if the scaled |
| 1214 | // value is an add of something and a constant, we can fold the |
| 1215 | // constant into the disp field here. |
Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1216 | if (MulVal.getNode()->getOpcode() == ISD::ADD && MulVal.hasOneUse() && |
| 1217 | isa<ConstantSDNode>(MulVal.getNode()->getOperand(1))) { |
| 1218 | Reg = MulVal.getNode()->getOperand(0); |
Chris Lattner | 3f0f71b | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 1219 | ConstantSDNode *AddVal = |
Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1220 | cast<ConstantSDNode>(MulVal.getNode()->getOperand(1)); |
Eli Friedman | ef67e7d | 2011-07-13 20:44:23 +0000 | [diff] [blame] | 1221 | uint64_t Disp = AddVal->getSExtValue() * CN->getZExtValue(); |
Sanjay Patel | 85030aa | 2015-10-13 16:23:00 +0000 | [diff] [blame^] | 1222 | if (foldOffsetIntoAddress(Disp, AM)) |
Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1223 | Reg = N.getNode()->getOperand(0); |
Chris Lattner | 3f0f71b | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 1224 | } else { |
Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1225 | Reg = N.getNode()->getOperand(0); |
Chris Lattner | 3f0f71b | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 1226 | } |
| 1227 | |
Dan Gohman | 0fd54fb | 2010-04-29 23:30:41 +0000 | [diff] [blame] | 1228 | AM.IndexReg = AM.Base_Reg = Reg; |
Chris Lattner | 3f0f71b | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 1229 | return false; |
| 1230 | } |
Chris Lattner | fe8c530 | 2007-02-04 20:18:17 +0000 | [diff] [blame] | 1231 | } |
Chris Lattner | 3f0f71b | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 1232 | break; |
| 1233 | |
Dan Gohman | faf75c8 | 2009-05-11 18:02:53 +0000 | [diff] [blame] | 1234 | case ISD::SUB: { |
| 1235 | // Given A-B, if A can be completely folded into the address and |
| 1236 | // the index field with the index field unused, use -B as the index. |
| 1237 | // This is a win if a has multiple parts that can be folded into |
| 1238 | // the address. Also, this saves a mov if the base register has |
| 1239 | // other uses, since it avoids a two-address sub instruction, however |
| 1240 | // it costs an additional mov if the index register has other uses. |
| 1241 | |
Dan Gohman | 99ba4da | 2010-06-18 01:24:29 +0000 | [diff] [blame] | 1242 | // Add an artificial use to this node so that we can keep track of |
| 1243 | // it if it gets CSE'd with a different node. |
| 1244 | HandleSDNode Handle(N); |
| 1245 | |
Dan Gohman | faf75c8 | 2009-05-11 18:02:53 +0000 | [diff] [blame] | 1246 | // Test if the LHS of the sub can be folded. |
| 1247 | X86ISelAddressMode Backup = AM; |
Sanjay Patel | 85030aa | 2015-10-13 16:23:00 +0000 | [diff] [blame^] | 1248 | if (matchAddressRecursively(N.getNode()->getOperand(0), AM, Depth+1)) { |
Dan Gohman | faf75c8 | 2009-05-11 18:02:53 +0000 | [diff] [blame] | 1249 | AM = Backup; |
| 1250 | break; |
| 1251 | } |
| 1252 | // Test if the index field is free for use. |
Chris Lattner | fea81da | 2009-06-27 04:16:01 +0000 | [diff] [blame] | 1253 | if (AM.IndexReg.getNode() || AM.isRIPRelative()) { |
Dan Gohman | faf75c8 | 2009-05-11 18:02:53 +0000 | [diff] [blame] | 1254 | AM = Backup; |
| 1255 | break; |
| 1256 | } |
Evan Cheng | 68333f5 | 2010-03-17 23:58:35 +0000 | [diff] [blame] | 1257 | |
Dan Gohman | faf75c8 | 2009-05-11 18:02:53 +0000 | [diff] [blame] | 1258 | int Cost = 0; |
Dan Gohman | 99ba4da | 2010-06-18 01:24:29 +0000 | [diff] [blame] | 1259 | SDValue RHS = Handle.getValue().getNode()->getOperand(1); |
Dan Gohman | faf75c8 | 2009-05-11 18:02:53 +0000 | [diff] [blame] | 1260 | // If the RHS involves a register with multiple uses, this |
| 1261 | // transformation incurs an extra mov, due to the neg instruction |
| 1262 | // clobbering its operand. |
| 1263 | if (!RHS.getNode()->hasOneUse() || |
| 1264 | RHS.getNode()->getOpcode() == ISD::CopyFromReg || |
| 1265 | RHS.getNode()->getOpcode() == ISD::TRUNCATE || |
| 1266 | RHS.getNode()->getOpcode() == ISD::ANY_EXTEND || |
| 1267 | (RHS.getNode()->getOpcode() == ISD::ZERO_EXTEND && |
Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1268 | RHS.getNode()->getOperand(0).getValueType() == MVT::i32)) |
Dan Gohman | faf75c8 | 2009-05-11 18:02:53 +0000 | [diff] [blame] | 1269 | ++Cost; |
| 1270 | // If the base is a register with multiple uses, this |
| 1271 | // transformation may save a mov. |
| 1272 | if ((AM.BaseType == X86ISelAddressMode::RegBase && |
Dan Gohman | 0fd54fb | 2010-04-29 23:30:41 +0000 | [diff] [blame] | 1273 | AM.Base_Reg.getNode() && |
| 1274 | !AM.Base_Reg.getNode()->hasOneUse()) || |
Dan Gohman | faf75c8 | 2009-05-11 18:02:53 +0000 | [diff] [blame] | 1275 | AM.BaseType == X86ISelAddressMode::FrameIndexBase) |
| 1276 | --Cost; |
| 1277 | // If the folded LHS was interesting, this transformation saves |
| 1278 | // address arithmetic. |
| 1279 | if ((AM.hasSymbolicDisplacement() && !Backup.hasSymbolicDisplacement()) + |
| 1280 | ((AM.Disp != 0) && (Backup.Disp == 0)) + |
| 1281 | (AM.Segment.getNode() && !Backup.Segment.getNode()) >= 2) |
| 1282 | --Cost; |
| 1283 | // If it doesn't look like it may be an overall win, don't do it. |
| 1284 | if (Cost >= 0) { |
| 1285 | AM = Backup; |
| 1286 | break; |
| 1287 | } |
| 1288 | |
| 1289 | // Ok, the transformation is legal and appears profitable. Go for it. |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1290 | SDValue Zero = CurDAG->getConstant(0, dl, N.getValueType()); |
Dan Gohman | faf75c8 | 2009-05-11 18:02:53 +0000 | [diff] [blame] | 1291 | SDValue Neg = CurDAG->getNode(ISD::SUB, dl, N.getValueType(), Zero, RHS); |
| 1292 | AM.IndexReg = Neg; |
| 1293 | AM.Scale = 1; |
| 1294 | |
| 1295 | // Insert the new nodes into the topological ordering. |
Sanjay Patel | 85030aa | 2015-10-13 16:23:00 +0000 | [diff] [blame^] | 1296 | insertDAGNode(*CurDAG, N, Zero); |
| 1297 | insertDAGNode(*CurDAG, N, Neg); |
Dan Gohman | faf75c8 | 2009-05-11 18:02:53 +0000 | [diff] [blame] | 1298 | return false; |
| 1299 | } |
| 1300 | |
Evan Cheng | bf38a5e | 2009-01-17 07:09:27 +0000 | [diff] [blame] | 1301 | case ISD::ADD: { |
Dan Gohman | 99ba4da | 2010-06-18 01:24:29 +0000 | [diff] [blame] | 1302 | // Add an artificial use to this node so that we can keep track of |
| 1303 | // it if it gets CSE'd with a different node. |
| 1304 | HandleSDNode Handle(N); |
Dan Gohman | 99ba4da | 2010-06-18 01:24:29 +0000 | [diff] [blame] | 1305 | |
Evan Cheng | bf38a5e | 2009-01-17 07:09:27 +0000 | [diff] [blame] | 1306 | X86ISelAddressMode Backup = AM; |
Sanjay Patel | 85030aa | 2015-10-13 16:23:00 +0000 | [diff] [blame^] | 1307 | if (!matchAddressRecursively(N.getOperand(0), AM, Depth+1) && |
| 1308 | !matchAddressRecursively(Handle.getValue().getOperand(1), AM, Depth+1)) |
Dan Gohman | 99ba4da | 2010-06-18 01:24:29 +0000 | [diff] [blame] | 1309 | return false; |
| 1310 | AM = Backup; |
Chad Rosier | 24c19d2 | 2012-08-01 18:39:17 +0000 | [diff] [blame] | 1311 | |
Evan Cheng | 68333f5 | 2010-03-17 23:58:35 +0000 | [diff] [blame] | 1312 | // Try again after commuting the operands. |
Sanjay Patel | 85030aa | 2015-10-13 16:23:00 +0000 | [diff] [blame^] | 1313 | if (!matchAddressRecursively(Handle.getValue().getOperand(1), AM, Depth+1)&& |
| 1314 | !matchAddressRecursively(Handle.getValue().getOperand(0), AM, Depth+1)) |
Dan Gohman | 99ba4da | 2010-06-18 01:24:29 +0000 | [diff] [blame] | 1315 | return false; |
Evan Cheng | bf38a5e | 2009-01-17 07:09:27 +0000 | [diff] [blame] | 1316 | AM = Backup; |
Dan Gohman | a1d9242 | 2009-03-13 02:25:09 +0000 | [diff] [blame] | 1317 | |
| 1318 | // If we couldn't fold both operands into the address at the same time, |
| 1319 | // see if we can just put each operand into a register and fold at least |
| 1320 | // the add. |
| 1321 | if (AM.BaseType == X86ISelAddressMode::RegBase && |
Dan Gohman | 0fd54fb | 2010-04-29 23:30:41 +0000 | [diff] [blame] | 1322 | !AM.Base_Reg.getNode() && |
Chris Lattner | fea81da | 2009-06-27 04:16:01 +0000 | [diff] [blame] | 1323 | !AM.IndexReg.getNode()) { |
Chris Lattner | 35a2e65 | 2011-01-16 08:48:11 +0000 | [diff] [blame] | 1324 | N = Handle.getValue(); |
| 1325 | AM.Base_Reg = N.getOperand(0); |
| 1326 | AM.IndexReg = N.getOperand(1); |
Dan Gohman | a1d9242 | 2009-03-13 02:25:09 +0000 | [diff] [blame] | 1327 | AM.Scale = 1; |
| 1328 | return false; |
| 1329 | } |
Chris Lattner | 35a2e65 | 2011-01-16 08:48:11 +0000 | [diff] [blame] | 1330 | N = Handle.getValue(); |
Chris Lattner | 3f0f71b | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 1331 | break; |
Evan Cheng | bf38a5e | 2009-01-17 07:09:27 +0000 | [diff] [blame] | 1332 | } |
Evan Cheng | 734e1e2 | 2006-05-30 06:59:36 +0000 | [diff] [blame] | 1333 | |
Chris Lattner | fe8c530 | 2007-02-04 20:18:17 +0000 | [diff] [blame] | 1334 | case ISD::OR: |
Sylvestre Ledru | 91ce36c | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 1335 | // Handle "X | C" as "X + C" iff X is known to have C bits clear. |
Chris Lattner | 46c01a3 | 2011-02-13 22:25:43 +0000 | [diff] [blame] | 1336 | if (CurDAG->isBaseWithConstantOffset(N)) { |
Chris Lattner | ff87f05e | 2007-12-08 07:22:58 +0000 | [diff] [blame] | 1337 | X86ISelAddressMode Backup = AM; |
Chris Lattner | 8477678 | 2010-04-20 23:18:40 +0000 | [diff] [blame] | 1338 | ConstantSDNode *CN = cast<ConstantSDNode>(N.getOperand(1)); |
Evan Cheng | 68333f5 | 2010-03-17 23:58:35 +0000 | [diff] [blame] | 1339 | |
Chris Lattner | ff87f05e | 2007-12-08 07:22:58 +0000 | [diff] [blame] | 1340 | // Start with the LHS as an addr mode. |
Sanjay Patel | 85030aa | 2015-10-13 16:23:00 +0000 | [diff] [blame^] | 1341 | if (!matchAddressRecursively(N.getOperand(0), AM, Depth+1) && |
| 1342 | !foldOffsetIntoAddress(CN->getSExtValue(), AM)) |
Chris Lattner | ff87f05e | 2007-12-08 07:22:58 +0000 | [diff] [blame] | 1343 | return false; |
Chris Lattner | ff87f05e | 2007-12-08 07:22:58 +0000 | [diff] [blame] | 1344 | AM = Backup; |
Evan Cheng | 734e1e2 | 2006-05-30 06:59:36 +0000 | [diff] [blame] | 1345 | } |
| 1346 | break; |
Chad Rosier | 24c19d2 | 2012-08-01 18:39:17 +0000 | [diff] [blame] | 1347 | |
Evan Cheng | 827d30d | 2007-12-13 00:43:27 +0000 | [diff] [blame] | 1348 | case ISD::AND: { |
Dan Gohman | 57d6bd3 | 2009-04-13 16:09:41 +0000 | [diff] [blame] | 1349 | // Perform some heroic transforms on an and of a constant-count shift |
| 1350 | // with a constant to enable use of the scaled offset field. |
| 1351 | |
Evan Cheng | 827d30d | 2007-12-13 00:43:27 +0000 | [diff] [blame] | 1352 | // Scale must not be used already. |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 1353 | if (AM.IndexReg.getNode() != nullptr || AM.Scale != 1) break; |
Evan Cheng | a20a773 | 2008-02-07 08:53:49 +0000 | [diff] [blame] | 1354 | |
Chandler Carruth | aa01e66 | 2012-01-11 09:35:00 +0000 | [diff] [blame] | 1355 | SDValue Shift = N.getOperand(0); |
| 1356 | if (Shift.getOpcode() != ISD::SRL && Shift.getOpcode() != ISD::SHL) break; |
Dan Gohman | 57d6bd3 | 2009-04-13 16:09:41 +0000 | [diff] [blame] | 1357 | SDValue X = Shift.getOperand(0); |
Chandler Carruth | aa01e66 | 2012-01-11 09:35:00 +0000 | [diff] [blame] | 1358 | |
| 1359 | // We only handle up to 64-bit values here as those are what matter for |
| 1360 | // addressing mode optimizations. |
Craig Topper | 83e042a | 2013-08-15 05:57:07 +0000 | [diff] [blame] | 1361 | if (X.getSimpleValueType().getSizeInBits() > 64) break; |
Chandler Carruth | aa01e66 | 2012-01-11 09:35:00 +0000 | [diff] [blame] | 1362 | |
Chandler Carruth | b0049f4 | 2012-01-11 09:35:04 +0000 | [diff] [blame] | 1363 | if (!isa<ConstantSDNode>(N.getOperand(1))) |
| 1364 | break; |
| 1365 | uint64_t Mask = N.getConstantOperandVal(1); |
Evan Cheng | 827d30d | 2007-12-13 00:43:27 +0000 | [diff] [blame] | 1366 | |
Chandler Carruth | 51d3076 | 2012-01-11 08:48:20 +0000 | [diff] [blame] | 1367 | // Try to fold the mask and shift into an extract and scale. |
Sanjay Patel | 85030aa | 2015-10-13 16:23:00 +0000 | [diff] [blame^] | 1368 | if (!foldMaskAndShiftToExtract(*CurDAG, N, Mask, Shift, X, AM)) |
Chandler Carruth | 51d3076 | 2012-01-11 08:48:20 +0000 | [diff] [blame] | 1369 | return false; |
Dan Gohman | 57d6bd3 | 2009-04-13 16:09:41 +0000 | [diff] [blame] | 1370 | |
Chandler Carruth | 51d3076 | 2012-01-11 08:48:20 +0000 | [diff] [blame] | 1371 | // Try to fold the mask and shift directly into the scale. |
Sanjay Patel | 85030aa | 2015-10-13 16:23:00 +0000 | [diff] [blame^] | 1372 | if (!foldMaskAndShiftToScale(*CurDAG, N, Mask, Shift, X, AM)) |
Chandler Carruth | 55b2cde | 2012-01-11 08:41:08 +0000 | [diff] [blame] | 1373 | return false; |
| 1374 | |
Chandler Carruth | aa01e66 | 2012-01-11 09:35:00 +0000 | [diff] [blame] | 1375 | // Try to swap the mask and shift to place shifts which can be done as |
| 1376 | // a scale on the outside of the mask. |
Sanjay Patel | 85030aa | 2015-10-13 16:23:00 +0000 | [diff] [blame^] | 1377 | if (!foldMaskedShiftToScaledMask(*CurDAG, N, Mask, Shift, X, AM)) |
Chandler Carruth | aa01e66 | 2012-01-11 09:35:00 +0000 | [diff] [blame] | 1378 | return false; |
| 1379 | break; |
Evan Cheng | 827d30d | 2007-12-13 00:43:27 +0000 | [diff] [blame] | 1380 | } |
Evan Cheng | 734e1e2 | 2006-05-30 06:59:36 +0000 | [diff] [blame] | 1381 | } |
Chris Lattner | 3f0f71b | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 1382 | |
Sanjay Patel | 85030aa | 2015-10-13 16:23:00 +0000 | [diff] [blame^] | 1383 | return matchAddressBase(N, AM); |
Dan Gohman | ccb3611 | 2007-08-13 20:03:06 +0000 | [diff] [blame] | 1384 | } |
| 1385 | |
Sanjay Patel | b5723d0 | 2015-10-13 15:12:27 +0000 | [diff] [blame] | 1386 | /// Helper for MatchAddress. Add the specified node to the |
Dan Gohman | ccb3611 | 2007-08-13 20:03:06 +0000 | [diff] [blame] | 1387 | /// specified addressing mode without any further recursion. |
Sanjay Patel | 85030aa | 2015-10-13 16:23:00 +0000 | [diff] [blame^] | 1388 | bool X86DAGToDAGISel::matchAddressBase(SDValue N, X86ISelAddressMode &AM) { |
Chris Lattner | 3f0f71b | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 1389 | // Is the base register already occupied? |
Dan Gohman | 0fd54fb | 2010-04-29 23:30:41 +0000 | [diff] [blame] | 1390 | if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base_Reg.getNode()) { |
Chris Lattner | 3f0f71b | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 1391 | // If so, check to see if the scale index register is set. |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 1392 | if (!AM.IndexReg.getNode()) { |
Chris Lattner | 3f0f71b | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 1393 | AM.IndexReg = N; |
| 1394 | AM.Scale = 1; |
| 1395 | return false; |
| 1396 | } |
| 1397 | |
| 1398 | // Otherwise, we cannot select it. |
| 1399 | return true; |
| 1400 | } |
| 1401 | |
| 1402 | // Default, generate it as a register. |
| 1403 | AM.BaseType = X86ISelAddressMode::RegBase; |
Dan Gohman | 0fd54fb | 2010-04-29 23:30:41 +0000 | [diff] [blame] | 1404 | AM.Base_Reg = N; |
Chris Lattner | 3f0f71b | 2005-11-19 02:11:08 +0000 | [diff] [blame] | 1405 | return false; |
| 1406 | } |
| 1407 | |
Sanjay Patel | 85030aa | 2015-10-13 16:23:00 +0000 | [diff] [blame^] | 1408 | bool X86DAGToDAGISel::selectVectorAddr(SDNode *Parent, SDValue N, SDValue &Base, |
Elena Demikhovsky | e1eda8a | 2015-04-30 08:38:48 +0000 | [diff] [blame] | 1409 | SDValue &Scale, SDValue &Index, |
| 1410 | SDValue &Disp, SDValue &Segment) { |
| 1411 | |
| 1412 | MaskedGatherScatterSDNode *Mgs = dyn_cast<MaskedGatherScatterSDNode>(Parent); |
| 1413 | if (!Mgs) |
| 1414 | return false; |
| 1415 | X86ISelAddressMode AM; |
| 1416 | unsigned AddrSpace = Mgs->getPointerInfo().getAddrSpace(); |
| 1417 | // AddrSpace 256 -> GS, 257 -> FS. |
| 1418 | if (AddrSpace == 256) |
| 1419 | AM.Segment = CurDAG->getRegister(X86::GS, MVT::i16); |
| 1420 | if (AddrSpace == 257) |
| 1421 | AM.Segment = CurDAG->getRegister(X86::FS, MVT::i16); |
| 1422 | |
| 1423 | SDLoc DL(N); |
| 1424 | Base = Mgs->getBasePtr(); |
| 1425 | Index = Mgs->getIndex(); |
| 1426 | unsigned ScalarSize = Mgs->getValue().getValueType().getScalarSizeInBits(); |
| 1427 | Scale = getI8Imm(ScalarSize/8, DL); |
| 1428 | |
| 1429 | // If Base is 0, the whole address is in index and the Scale is 1 |
Daniel Jasper | 232778a | 2015-04-30 09:01:21 +0000 | [diff] [blame] | 1430 | if (isa<ConstantSDNode>(Base)) { |
| 1431 | assert(dyn_cast<ConstantSDNode>(Base)->isNullValue() && |
| 1432 | "Unexpected base in gather/scatter"); |
Elena Demikhovsky | e1eda8a | 2015-04-30 08:38:48 +0000 | [diff] [blame] | 1433 | Scale = getI8Imm(1, DL); |
| 1434 | Base = CurDAG->getRegister(0, MVT::i32); |
| 1435 | } |
| 1436 | if (AM.Segment.getNode()) |
| 1437 | Segment = AM.Segment; |
| 1438 | else |
| 1439 | Segment = CurDAG->getRegister(0, MVT::i32); |
| 1440 | Disp = CurDAG->getTargetConstant(0, DL, MVT::i32); |
| 1441 | return true; |
| 1442 | } |
| 1443 | |
Sanjay Patel | b5723d0 | 2015-10-13 15:12:27 +0000 | [diff] [blame] | 1444 | /// Returns true if it is able to pattern match an addressing mode. |
Evan Cheng | c9fab31 | 2005-12-08 02:01:35 +0000 | [diff] [blame] | 1445 | /// It returns the operands which make up the maximal addressing mode it can |
| 1446 | /// match by reference. |
Chris Lattner | d58d7c1 | 2010-09-21 22:07:31 +0000 | [diff] [blame] | 1447 | /// |
| 1448 | /// Parent is the parent node of the addr operand that is being matched. It |
| 1449 | /// is always a load, store, atomic node, or null. It is only null when |
| 1450 | /// checking memory operands for inline asm nodes. |
Sanjay Patel | 85030aa | 2015-10-13 16:23:00 +0000 | [diff] [blame^] | 1451 | bool X86DAGToDAGISel::selectAddr(SDNode *Parent, SDValue N, SDValue &Base, |
Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1452 | SDValue &Scale, SDValue &Index, |
Rafael Espindola | 3b2df10 | 2009-04-08 21:14:34 +0000 | [diff] [blame] | 1453 | SDValue &Disp, SDValue &Segment) { |
Evan Cheng | c9fab31 | 2005-12-08 02:01:35 +0000 | [diff] [blame] | 1454 | X86ISelAddressMode AM; |
Chad Rosier | 24c19d2 | 2012-08-01 18:39:17 +0000 | [diff] [blame] | 1455 | |
Chris Lattner | 8a236b6 | 2010-09-22 04:39:11 +0000 | [diff] [blame] | 1456 | if (Parent && |
| 1457 | // This list of opcodes are all the nodes that have an "addr:$ptr" operand |
| 1458 | // that are not a MemSDNode, and thus don't have proper addrspace info. |
Chris Lattner | 8a236b6 | 2010-09-22 04:39:11 +0000 | [diff] [blame] | 1459 | Parent->getOpcode() != ISD::INTRINSIC_W_CHAIN && // unaligned loads, fixme |
Eric Christopher | c1b3e07 | 2010-09-22 20:42:08 +0000 | [diff] [blame] | 1460 | Parent->getOpcode() != ISD::INTRINSIC_VOID && // nontemporal stores |
Michael Liao | 97bf363 | 2012-10-15 22:39:43 +0000 | [diff] [blame] | 1461 | Parent->getOpcode() != X86ISD::TLSCALL && // Fixme |
| 1462 | Parent->getOpcode() != X86ISD::EH_SJLJ_SETJMP && // setjmp |
| 1463 | Parent->getOpcode() != X86ISD::EH_SJLJ_LONGJMP) { // longjmp |
Chris Lattner | 8a236b6 | 2010-09-22 04:39:11 +0000 | [diff] [blame] | 1464 | unsigned AddrSpace = |
| 1465 | cast<MemSDNode>(Parent)->getPointerInfo().getAddrSpace(); |
| 1466 | // AddrSpace 256 -> GS, 257 -> FS. |
| 1467 | if (AddrSpace == 256) |
| 1468 | AM.Segment = CurDAG->getRegister(X86::GS, MVT::i16); |
| 1469 | if (AddrSpace == 257) |
| 1470 | AM.Segment = CurDAG->getRegister(X86::FS, MVT::i16); |
| 1471 | } |
Chad Rosier | 24c19d2 | 2012-08-01 18:39:17 +0000 | [diff] [blame] | 1472 | |
Sanjay Patel | 85030aa | 2015-10-13 16:23:00 +0000 | [diff] [blame^] | 1473 | if (matchAddress(N, AM)) |
Evan Cheng | bc7a0f44 | 2006-01-11 06:09:51 +0000 | [diff] [blame] | 1474 | return false; |
Evan Cheng | c9fab31 | 2005-12-08 02:01:35 +0000 | [diff] [blame] | 1475 | |
Craig Topper | 83e042a | 2013-08-15 05:57:07 +0000 | [diff] [blame] | 1476 | MVT VT = N.getSimpleValueType(); |
Evan Cheng | bc7a0f44 | 2006-01-11 06:09:51 +0000 | [diff] [blame] | 1477 | if (AM.BaseType == X86ISelAddressMode::RegBase) { |
Dan Gohman | 0fd54fb | 2010-04-29 23:30:41 +0000 | [diff] [blame] | 1478 | if (!AM.Base_Reg.getNode()) |
| 1479 | AM.Base_Reg = CurDAG->getRegister(0, VT); |
Evan Cheng | c9fab31 | 2005-12-08 02:01:35 +0000 | [diff] [blame] | 1480 | } |
Evan Cheng | bc7a0f44 | 2006-01-11 06:09:51 +0000 | [diff] [blame] | 1481 | |
Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1482 | if (!AM.IndexReg.getNode()) |
Evan Cheng | 11b0a5d | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 1483 | AM.IndexReg = CurDAG->getRegister(0, VT); |
Evan Cheng | bc7a0f44 | 2006-01-11 06:09:51 +0000 | [diff] [blame] | 1484 | |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1485 | getAddressOperands(AM, SDLoc(N), Base, Scale, Index, Disp, Segment); |
Evan Cheng | bc7a0f44 | 2006-01-11 06:09:51 +0000 | [diff] [blame] | 1486 | return true; |
Evan Cheng | c9fab31 | 2005-12-08 02:01:35 +0000 | [diff] [blame] | 1487 | } |
| 1488 | |
Sanjay Patel | b5723d0 | 2015-10-13 15:12:27 +0000 | [diff] [blame] | 1489 | /// Match a scalar SSE load. In particular, we want to match a load whose top |
| 1490 | /// elements are either undef or zeros. The load flavor is derived from the |
| 1491 | /// type of N, which is either v4f32 or v2f64. |
Chris Lattner | 3f48215 | 2010-02-17 06:07:47 +0000 | [diff] [blame] | 1492 | /// |
| 1493 | /// We also return: |
Chris Lattner | 18a32ce | 2010-02-21 03:17:59 +0000 | [diff] [blame] | 1494 | /// PatternChainNode: this is the matched node that has a chain input and |
| 1495 | /// output. |
Sanjay Patel | 85030aa | 2015-10-13 16:23:00 +0000 | [diff] [blame^] | 1496 | bool X86DAGToDAGISel::selectScalarSSELoad(SDNode *Root, |
Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1497 | SDValue N, SDValue &Base, |
| 1498 | SDValue &Scale, SDValue &Index, |
Rafael Espindola | 3b2df10 | 2009-04-08 21:14:34 +0000 | [diff] [blame] | 1499 | SDValue &Disp, SDValue &Segment, |
Chris Lattner | 18a32ce | 2010-02-21 03:17:59 +0000 | [diff] [blame] | 1500 | SDValue &PatternNodeWithChain) { |
Chris Lattner | 398195e | 2006-10-07 21:55:32 +0000 | [diff] [blame] | 1501 | if (N.getOpcode() == ISD::SCALAR_TO_VECTOR) { |
Chris Lattner | 18a32ce | 2010-02-21 03:17:59 +0000 | [diff] [blame] | 1502 | PatternNodeWithChain = N.getOperand(0); |
| 1503 | if (ISD::isNON_EXTLoad(PatternNodeWithChain.getNode()) && |
| 1504 | PatternNodeWithChain.hasOneUse() && |
Chris Lattner | 3c29aff | 2010-02-21 04:53:34 +0000 | [diff] [blame] | 1505 | IsProfitableToFold(N.getOperand(0), N.getNode(), Root) && |
Dan Gohman | 21cea8a | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 1506 | IsLegalToFold(N.getOperand(0), N.getNode(), Root, OptLevel)) { |
Chris Lattner | 18a32ce | 2010-02-21 03:17:59 +0000 | [diff] [blame] | 1507 | LoadSDNode *LD = cast<LoadSDNode>(PatternNodeWithChain); |
Sanjay Patel | 85030aa | 2015-10-13 16:23:00 +0000 | [diff] [blame^] | 1508 | if (!selectAddr(LD, LD->getBasePtr(), Base, Scale, Index, Disp, Segment)) |
Chris Lattner | 398195e | 2006-10-07 21:55:32 +0000 | [diff] [blame] | 1509 | return false; |
| 1510 | return true; |
| 1511 | } |
| 1512 | } |
Chris Lattner | d5fcfaa | 2006-10-11 22:09:58 +0000 | [diff] [blame] | 1513 | |
| 1514 | // Also handle the case where we explicitly require zeros in the top |
Chris Lattner | 398195e | 2006-10-07 21:55:32 +0000 | [diff] [blame] | 1515 | // elements. This is a vector shuffle from the zero vector. |
Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1516 | if (N.getOpcode() == X86ISD::VZEXT_MOVL && N.getNode()->hasOneUse() && |
Chris Lattner | 5728bdd | 2007-11-25 00:24:49 +0000 | [diff] [blame] | 1517 | // Check to see if the top elements are all zeros (or bitcast of zeros). |
Chad Rosier | 24c19d2 | 2012-08-01 18:39:17 +0000 | [diff] [blame] | 1518 | N.getOperand(0).getOpcode() == ISD::SCALAR_TO_VECTOR && |
Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1519 | N.getOperand(0).getNode()->hasOneUse() && |
| 1520 | ISD::isNON_EXTLoad(N.getOperand(0).getOperand(0).getNode()) && |
Chris Lattner | afac7dad | 2010-02-16 22:35:06 +0000 | [diff] [blame] | 1521 | N.getOperand(0).getOperand(0).hasOneUse() && |
| 1522 | IsProfitableToFold(N.getOperand(0), N.getNode(), Root) && |
Dan Gohman | 21cea8a | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 1523 | IsLegalToFold(N.getOperand(0), N.getNode(), Root, OptLevel)) { |
Evan Cheng | 78af38c | 2008-05-08 00:57:18 +0000 | [diff] [blame] | 1524 | // Okay, this is a zero extending load. Fold it. |
| 1525 | LoadSDNode *LD = cast<LoadSDNode>(N.getOperand(0).getOperand(0)); |
Sanjay Patel | 85030aa | 2015-10-13 16:23:00 +0000 | [diff] [blame^] | 1526 | if (!selectAddr(LD, LD->getBasePtr(), Base, Scale, Index, Disp, Segment)) |
Evan Cheng | 78af38c | 2008-05-08 00:57:18 +0000 | [diff] [blame] | 1527 | return false; |
Chris Lattner | 18a32ce | 2010-02-21 03:17:59 +0000 | [diff] [blame] | 1528 | PatternNodeWithChain = SDValue(LD, 0); |
Evan Cheng | 78af38c | 2008-05-08 00:57:18 +0000 | [diff] [blame] | 1529 | return true; |
Chris Lattner | d5fcfaa | 2006-10-11 22:09:58 +0000 | [diff] [blame] | 1530 | } |
Chris Lattner | 398195e | 2006-10-07 21:55:32 +0000 | [diff] [blame] | 1531 | return false; |
| 1532 | } |
| 1533 | |
| 1534 | |
Sanjay Patel | 85030aa | 2015-10-13 16:23:00 +0000 | [diff] [blame^] | 1535 | bool X86DAGToDAGISel::selectMOV64Imm32(SDValue N, SDValue &Imm) { |
Tim Northover | 3a1fd4c | 2013-06-01 09:55:14 +0000 | [diff] [blame] | 1536 | if (const ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) { |
| 1537 | uint64_t ImmVal = CN->getZExtValue(); |
| 1538 | if ((uint32_t)ImmVal != (uint64_t)ImmVal) |
| 1539 | return false; |
| 1540 | |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1541 | Imm = CurDAG->getTargetConstant(ImmVal, SDLoc(N), MVT::i64); |
Tim Northover | 3a1fd4c | 2013-06-01 09:55:14 +0000 | [diff] [blame] | 1542 | return true; |
| 1543 | } |
| 1544 | |
| 1545 | // In static codegen with small code model, we can get the address of a label |
| 1546 | // into a register with 'movl'. TableGen has already made sure we're looking |
| 1547 | // at a label of some kind. |
Tim Northover | 6833e3f | 2013-06-10 20:43:49 +0000 | [diff] [blame] | 1548 | assert(N->getOpcode() == X86ISD::Wrapper && |
| 1549 | "Unexpected node type for MOV32ri64"); |
Tim Northover | 3a1fd4c | 2013-06-01 09:55:14 +0000 | [diff] [blame] | 1550 | N = N.getOperand(0); |
| 1551 | |
| 1552 | if (N->getOpcode() != ISD::TargetConstantPool && |
| 1553 | N->getOpcode() != ISD::TargetJumpTable && |
| 1554 | N->getOpcode() != ISD::TargetGlobalAddress && |
| 1555 | N->getOpcode() != ISD::TargetExternalSymbol && |
Rafael Espindola | 36b718f | 2015-06-22 17:46:53 +0000 | [diff] [blame] | 1556 | N->getOpcode() != ISD::MCSymbol && |
Tim Northover | 3a1fd4c | 2013-06-01 09:55:14 +0000 | [diff] [blame] | 1557 | N->getOpcode() != ISD::TargetBlockAddress) |
| 1558 | return false; |
| 1559 | |
| 1560 | Imm = N; |
| 1561 | return TM.getCodeModel() == CodeModel::Small; |
| 1562 | } |
| 1563 | |
Sanjay Patel | 85030aa | 2015-10-13 16:23:00 +0000 | [diff] [blame^] | 1564 | bool X86DAGToDAGISel::selectLEA64_32Addr(SDValue N, SDValue &Base, |
Tim Northover | 6833e3f | 2013-06-10 20:43:49 +0000 | [diff] [blame] | 1565 | SDValue &Scale, SDValue &Index, |
| 1566 | SDValue &Disp, SDValue &Segment) { |
Sanjay Patel | 85030aa | 2015-10-13 16:23:00 +0000 | [diff] [blame^] | 1567 | if (!selectLEAAddr(N, Base, Scale, Index, Disp, Segment)) |
Tim Northover | 6833e3f | 2013-06-10 20:43:49 +0000 | [diff] [blame] | 1568 | return false; |
| 1569 | |
| 1570 | SDLoc DL(N); |
| 1571 | RegisterSDNode *RN = dyn_cast<RegisterSDNode>(Base); |
| 1572 | if (RN && RN->getReg() == 0) |
| 1573 | Base = CurDAG->getRegister(0, MVT::i64); |
Pavel Chupin | 01a4e0a | 2014-08-20 11:59:22 +0000 | [diff] [blame] | 1574 | else if (Base.getValueType() == MVT::i32 && !dyn_cast<FrameIndexSDNode>(Base)) { |
Tim Northover | 6833e3f | 2013-06-10 20:43:49 +0000 | [diff] [blame] | 1575 | // Base could already be %rip, particularly in the x32 ABI. |
| 1576 | Base = SDValue(CurDAG->getMachineNode( |
| 1577 | TargetOpcode::SUBREG_TO_REG, DL, MVT::i64, |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1578 | CurDAG->getTargetConstant(0, DL, MVT::i64), |
Tim Northover | 6833e3f | 2013-06-10 20:43:49 +0000 | [diff] [blame] | 1579 | Base, |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1580 | CurDAG->getTargetConstant(X86::sub_32bit, DL, MVT::i32)), |
Tim Northover | 6833e3f | 2013-06-10 20:43:49 +0000 | [diff] [blame] | 1581 | 0); |
| 1582 | } |
| 1583 | |
| 1584 | RN = dyn_cast<RegisterSDNode>(Index); |
| 1585 | if (RN && RN->getReg() == 0) |
| 1586 | Index = CurDAG->getRegister(0, MVT::i64); |
| 1587 | else { |
| 1588 | assert(Index.getValueType() == MVT::i32 && |
| 1589 | "Expect to be extending 32-bit registers for use in LEA"); |
| 1590 | Index = SDValue(CurDAG->getMachineNode( |
| 1591 | TargetOpcode::SUBREG_TO_REG, DL, MVT::i64, |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1592 | CurDAG->getTargetConstant(0, DL, MVT::i64), |
Tim Northover | 6833e3f | 2013-06-10 20:43:49 +0000 | [diff] [blame] | 1593 | Index, |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1594 | CurDAG->getTargetConstant(X86::sub_32bit, DL, |
| 1595 | MVT::i32)), |
Tim Northover | 6833e3f | 2013-06-10 20:43:49 +0000 | [diff] [blame] | 1596 | 0); |
| 1597 | } |
| 1598 | |
| 1599 | return true; |
| 1600 | } |
| 1601 | |
Sanjay Patel | b5723d0 | 2015-10-13 15:12:27 +0000 | [diff] [blame] | 1602 | /// Calls SelectAddr and determines if the maximal addressing |
Evan Cheng | 77d86ff | 2006-02-25 10:09:08 +0000 | [diff] [blame] | 1603 | /// mode it matches can be cost effectively emitted as an LEA instruction. |
Sanjay Patel | 85030aa | 2015-10-13 16:23:00 +0000 | [diff] [blame^] | 1604 | bool X86DAGToDAGISel::selectLEAAddr(SDValue N, |
Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1605 | SDValue &Base, SDValue &Scale, |
Chris Lattner | f469307 | 2010-07-08 23:46:44 +0000 | [diff] [blame] | 1606 | SDValue &Index, SDValue &Disp, |
| 1607 | SDValue &Segment) { |
Evan Cheng | 77d86ff | 2006-02-25 10:09:08 +0000 | [diff] [blame] | 1608 | X86ISelAddressMode AM; |
Rafael Espindola | bb834f0 | 2009-04-10 10:09:34 +0000 | [diff] [blame] | 1609 | |
| 1610 | // Set AM.Segment to prevent MatchAddress from using one. LEA doesn't support |
| 1611 | // segments. |
| 1612 | SDValue Copy = AM.Segment; |
Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1613 | SDValue T = CurDAG->getRegister(0, MVT::i32); |
Rafael Espindola | bb834f0 | 2009-04-10 10:09:34 +0000 | [diff] [blame] | 1614 | AM.Segment = T; |
Sanjay Patel | 85030aa | 2015-10-13 16:23:00 +0000 | [diff] [blame^] | 1615 | if (matchAddress(N, AM)) |
Evan Cheng | 77d86ff | 2006-02-25 10:09:08 +0000 | [diff] [blame] | 1616 | return false; |
Rafael Espindola | bb834f0 | 2009-04-10 10:09:34 +0000 | [diff] [blame] | 1617 | assert (T == AM.Segment); |
| 1618 | AM.Segment = Copy; |
Rafael Espindola | 3b2df10 | 2009-04-08 21:14:34 +0000 | [diff] [blame] | 1619 | |
Craig Topper | 83e042a | 2013-08-15 05:57:07 +0000 | [diff] [blame] | 1620 | MVT VT = N.getSimpleValueType(); |
Evan Cheng | 77d86ff | 2006-02-25 10:09:08 +0000 | [diff] [blame] | 1621 | unsigned Complexity = 0; |
| 1622 | if (AM.BaseType == X86ISelAddressMode::RegBase) |
Dan Gohman | 0fd54fb | 2010-04-29 23:30:41 +0000 | [diff] [blame] | 1623 | if (AM.Base_Reg.getNode()) |
Evan Cheng | 77d86ff | 2006-02-25 10:09:08 +0000 | [diff] [blame] | 1624 | Complexity = 1; |
| 1625 | else |
Dan Gohman | 0fd54fb | 2010-04-29 23:30:41 +0000 | [diff] [blame] | 1626 | AM.Base_Reg = CurDAG->getRegister(0, VT); |
Evan Cheng | 77d86ff | 2006-02-25 10:09:08 +0000 | [diff] [blame] | 1627 | else if (AM.BaseType == X86ISelAddressMode::FrameIndexBase) |
| 1628 | Complexity = 4; |
| 1629 | |
Gabor Greif | f304a7a | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 1630 | if (AM.IndexReg.getNode()) |
Evan Cheng | 77d86ff | 2006-02-25 10:09:08 +0000 | [diff] [blame] | 1631 | Complexity++; |
| 1632 | else |
Evan Cheng | 11b0a5d | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 1633 | AM.IndexReg = CurDAG->getRegister(0, VT); |
Evan Cheng | 77d86ff | 2006-02-25 10:09:08 +0000 | [diff] [blame] | 1634 | |
Chris Lattner | 3e1d917 | 2007-03-20 06:08:29 +0000 | [diff] [blame] | 1635 | // Don't match just leal(,%reg,2). It's cheaper to do addl %reg, %reg, or with |
| 1636 | // a simple shift. |
| 1637 | if (AM.Scale > 1) |
Evan Cheng | 990c360 | 2006-02-28 21:13:57 +0000 | [diff] [blame] | 1638 | Complexity++; |
Evan Cheng | 77d86ff | 2006-02-25 10:09:08 +0000 | [diff] [blame] | 1639 | |
| 1640 | // FIXME: We are artificially lowering the criteria to turn ADD %reg, $GA |
Sanjay Patel | b814ef1 | 2015-10-12 16:09:59 +0000 | [diff] [blame] | 1641 | // to a LEA. This is determined with some experimentation but is by no means |
Evan Cheng | 77d86ff | 2006-02-25 10:09:08 +0000 | [diff] [blame] | 1642 | // optimal (especially for code size consideration). LEA is nice because of |
| 1643 | // its three-address nature. Tweak the cost function again when we can run |
| 1644 | // convertToThreeAddress() at register allocation time. |
Dan Gohman | 4e3e3de | 2009-02-07 00:43:41 +0000 | [diff] [blame] | 1645 | if (AM.hasSymbolicDisplacement()) { |
Sanjay Patel | b814ef1 | 2015-10-12 16:09:59 +0000 | [diff] [blame] | 1646 | // For X86-64, always use LEA to materialize RIP-relative addresses. |
Evan Cheng | 47e181c | 2006-12-05 22:03:40 +0000 | [diff] [blame] | 1647 | if (Subtarget->is64Bit()) |
Evan Cheng | 11b0a5d | 2006-09-08 06:48:29 +0000 | [diff] [blame] | 1648 | Complexity = 4; |
| 1649 | else |
| 1650 | Complexity += 2; |
| 1651 | } |
Evan Cheng | 77d86ff | 2006-02-25 10:09:08 +0000 | [diff] [blame] | 1652 | |
Dan Gohman | 0fd54fb | 2010-04-29 23:30:41 +0000 | [diff] [blame] | 1653 | if (AM.Disp && (AM.Base_Reg.getNode() || AM.IndexReg.getNode())) |
Evan Cheng | 77d86ff | 2006-02-25 10:09:08 +0000 | [diff] [blame] | 1654 | Complexity++; |
| 1655 | |
Chris Lattner | 4d10f1a | 2009-07-11 22:50:33 +0000 | [diff] [blame] | 1656 | // If it isn't worth using an LEA, reject it. |
Chris Lattner | 48cee9b | 2009-07-11 23:07:30 +0000 | [diff] [blame] | 1657 | if (Complexity <= 2) |
Chris Lattner | 4d10f1a | 2009-07-11 22:50:33 +0000 | [diff] [blame] | 1658 | return false; |
Chad Rosier | 24c19d2 | 2012-08-01 18:39:17 +0000 | [diff] [blame] | 1659 | |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1660 | getAddressOperands(AM, SDLoc(N), Base, Scale, Index, Disp, Segment); |
Chris Lattner | 4d10f1a | 2009-07-11 22:50:33 +0000 | [diff] [blame] | 1661 | return true; |
Evan Cheng | 77d86ff | 2006-02-25 10:09:08 +0000 | [diff] [blame] | 1662 | } |
| 1663 | |
Sanjay Patel | b5723d0 | 2015-10-13 15:12:27 +0000 | [diff] [blame] | 1664 | /// This is only run on TargetGlobalTLSAddress nodes. |
Sanjay Patel | 85030aa | 2015-10-13 16:23:00 +0000 | [diff] [blame^] | 1665 | bool X86DAGToDAGISel::selectTLSADDRAddr(SDValue N, SDValue &Base, |
Chris Lattner | 7d2b049 | 2009-06-20 20:38:48 +0000 | [diff] [blame] | 1666 | SDValue &Scale, SDValue &Index, |
Chris Lattner | f469307 | 2010-07-08 23:46:44 +0000 | [diff] [blame] | 1667 | SDValue &Disp, SDValue &Segment) { |
Chris Lattner | 7d2b049 | 2009-06-20 20:38:48 +0000 | [diff] [blame] | 1668 | assert(N.getOpcode() == ISD::TargetGlobalTLSAddress); |
| 1669 | const GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N); |
Chad Rosier | 24c19d2 | 2012-08-01 18:39:17 +0000 | [diff] [blame] | 1670 | |
Chris Lattner | 7d2b049 | 2009-06-20 20:38:48 +0000 | [diff] [blame] | 1671 | X86ISelAddressMode AM; |
| 1672 | AM.GV = GA->getGlobal(); |
| 1673 | AM.Disp += GA->getOffset(); |
Dan Gohman | 0fd54fb | 2010-04-29 23:30:41 +0000 | [diff] [blame] | 1674 | AM.Base_Reg = CurDAG->getRegister(0, N.getValueType()); |
Chris Lattner | 899abc4 | 2009-06-26 21:18:37 +0000 | [diff] [blame] | 1675 | AM.SymbolFlags = GA->getTargetFlags(); |
| 1676 | |
Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1677 | if (N.getValueType() == MVT::i32) { |
Chris Lattner | 7d2b049 | 2009-06-20 20:38:48 +0000 | [diff] [blame] | 1678 | AM.Scale = 1; |
Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1679 | AM.IndexReg = CurDAG->getRegister(X86::EBX, MVT::i32); |
Chris Lattner | 7d2b049 | 2009-06-20 20:38:48 +0000 | [diff] [blame] | 1680 | } else { |
Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 1681 | AM.IndexReg = CurDAG->getRegister(0, MVT::i64); |
Chris Lattner | 7d2b049 | 2009-06-20 20:38:48 +0000 | [diff] [blame] | 1682 | } |
Chad Rosier | 24c19d2 | 2012-08-01 18:39:17 +0000 | [diff] [blame] | 1683 | |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1684 | getAddressOperands(AM, SDLoc(N), Base, Scale, Index, Disp, Segment); |
Chris Lattner | 7d2b049 | 2009-06-20 20:38:48 +0000 | [diff] [blame] | 1685 | return true; |
| 1686 | } |
| 1687 | |
| 1688 | |
Sanjay Patel | 85030aa | 2015-10-13 16:23:00 +0000 | [diff] [blame^] | 1689 | bool X86DAGToDAGISel::tryFoldLoad(SDNode *P, SDValue N, |
Dan Gohman | 2ce6f2a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 1690 | SDValue &Base, SDValue &Scale, |
Rafael Espindola | 3b2df10 | 2009-04-08 21:14:34 +0000 | [diff] [blame] | 1691 | SDValue &Index, SDValue &Disp, |
| 1692 | SDValue &Segment) { |
Chris Lattner | dd03070 | 2010-03-02 22:20:06 +0000 | [diff] [blame] | 1693 | if (!ISD::isNON_EXTLoad(N.getNode()) || |
| 1694 | !IsProfitableToFold(N, P, P) || |
Dan Gohman | 21cea8a | 2010-04-17 15:26:15 +0000 | [diff] [blame] | 1695 | !IsLegalToFold(N, P, P, OptLevel)) |
Chris Lattner | dd03070 | 2010-03-02 22:20:06 +0000 | [diff] [blame] | 1696 | return false; |
Chad Rosier | 24c19d2 | 2012-08-01 18:39:17 +0000 | [diff] [blame] | 1697 | |
Sanjay Patel | 85030aa | 2015-10-13 16:23:00 +0000 | [diff] [blame^] | 1698 | return selectAddr(N.getNode(), |
Chris Lattner | d58d7c1 | 2010-09-21 22:07:31 +0000 | [diff] [blame] | 1699 | N.getOperand(1), Base, Scale, Index, Disp, Segment); |
Evan Cheng | 10d2790 | 2006-01-06 20:36:21 +0000 | [diff] [blame] | 1700 | } |
| 1701 | |
Sanjay Patel | b5723d0 | 2015-10-13 15:12:27 +0000 | [diff] [blame] | 1702 | /// Return an SDNode that returns the value of the global base register. |
| 1703 | /// Output instructions required to initialize the global base register, |
| 1704 | /// if necessary. |
Evan Cheng | 61413a3 | 2006-08-26 05:34:46 +0000 | [diff] [blame] | 1705 | SDNode *X86DAGToDAGISel::getGlobalBaseReg() { |
Dan Gohman | 4751bb9 | 2009-06-03 20:20:00 +0000 | [diff] [blame] | 1706 | unsigned GlobalBaseReg = getInstrInfo()->getGlobalBaseReg(MF); |
Mehdi Amini | 44ede33 | 2015-07-09 02:09:04 +0000 | [diff] [blame] | 1707 | auto &DL = MF->getDataLayout(); |
| 1708 | return CurDAG->getRegister(GlobalBaseReg, TLI->getPointerTy(DL)).getNode(); |
Evan Cheng | 5588de9 | 2006-02-18 00:15:05 +0000 | [diff] [blame] | 1709 | } |
| 1710 | |
Michael Liao | 8372539 | 2012-09-19 19:36:58 +0000 | [diff] [blame] | 1711 | /// Atomic opcode table |
| 1712 | /// |
Eric Christopher | eb47a2a | 2011-05-17 07:47:55 +0000 | [diff] [blame] | 1713 | enum AtomicOpc { |
Michael Liao | 8372539 | 2012-09-19 19:36:58 +0000 | [diff] [blame] | 1714 | ADD, |
| 1715 | SUB, |
| 1716 | INC, |
| 1717 | DEC, |
Eric Christopher | abfe313 | 2011-05-17 07:50:41 +0000 | [diff] [blame] | 1718 | OR, |
Eric Christopher | a1d9e29 | 2011-05-17 08:10:18 +0000 | [diff] [blame] | 1719 | AND, |
| 1720 | XOR, |
Eric Christopher | abfe313 | 2011-05-17 07:50:41 +0000 | [diff] [blame] | 1721 | AtomicOpcEnd |
Eric Christopher | eb47a2a | 2011-05-17 07:47:55 +0000 | [diff] [blame] | 1722 | }; |
| 1723 | |
| 1724 | enum AtomicSz { |
| 1725 | ConstantI8, |
| 1726 | I8, |
| 1727 | SextConstantI16, |
| 1728 | ConstantI16, |
| 1729 | I16, |
| 1730 | SextConstantI32, |
| 1731 | ConstantI32, |
| 1732 | I32, |
| 1733 | SextConstantI64, |
| 1734 | ConstantI64, |
Eric Christopher | abfe313 | 2011-05-17 07:50:41 +0000 | [diff] [blame] | 1735 | I64, |
| 1736 | AtomicSzEnd |
Eric Christopher | eb47a2a | 2011-05-17 07:47:55 +0000 | [diff] [blame] | 1737 | }; |
| 1738 | |
Craig Topper | 2dac962 | 2012-03-09 07:45:21 +0000 | [diff] [blame] | 1739 | static const uint16_t AtomicOpcTbl[AtomicOpcEnd][AtomicSzEnd] = { |
Eric Christopher | 2a9dbbb | 2011-05-11 21:44:58 +0000 | [diff] [blame] | 1740 | { |
Michael Liao | 8372539 | 2012-09-19 19:36:58 +0000 | [diff] [blame] | 1741 | X86::LOCK_ADD8mi, |
| 1742 | X86::LOCK_ADD8mr, |
| 1743 | X86::LOCK_ADD16mi8, |
| 1744 | X86::LOCK_ADD16mi, |
| 1745 | X86::LOCK_ADD16mr, |
| 1746 | X86::LOCK_ADD32mi8, |
| 1747 | X86::LOCK_ADD32mi, |
| 1748 | X86::LOCK_ADD32mr, |
| 1749 | X86::LOCK_ADD64mi8, |
| 1750 | X86::LOCK_ADD64mi32, |
| 1751 | X86::LOCK_ADD64mr, |
| 1752 | }, |
| 1753 | { |
| 1754 | X86::LOCK_SUB8mi, |
| 1755 | X86::LOCK_SUB8mr, |
| 1756 | X86::LOCK_SUB16mi8, |
| 1757 | X86::LOCK_SUB16mi, |
| 1758 | X86::LOCK_SUB16mr, |
| 1759 | X86::LOCK_SUB32mi8, |
| 1760 | X86::LOCK_SUB32mi, |
| 1761 | X86::LOCK_SUB32mr, |
| 1762 | X86::LOCK_SUB64mi8, |
| 1763 | X86::LOCK_SUB64mi32, |
| 1764 | X86::LOCK_SUB64mr, |
| 1765 | }, |
| 1766 | { |
| 1767 | 0, |
| 1768 | X86::LOCK_INC8m, |
| 1769 | 0, |
| 1770 | 0, |
| 1771 | X86::LOCK_INC16m, |
| 1772 | 0, |
| 1773 | 0, |
| 1774 | X86::LOCK_INC32m, |
| 1775 | 0, |
| 1776 | 0, |
| 1777 | X86::LOCK_INC64m, |
| 1778 | }, |
| 1779 | { |
| 1780 | 0, |
| 1781 | X86::LOCK_DEC8m, |
| 1782 | 0, |
| 1783 | 0, |
| 1784 | X86::LOCK_DEC16m, |
| 1785 | 0, |
| 1786 | 0, |
| 1787 | X86::LOCK_DEC32m, |
| 1788 | 0, |
| 1789 | 0, |
| 1790 | X86::LOCK_DEC64m, |
| 1791 | }, |
| 1792 | { |
Eric Christopher | 2a9dbbb | 2011-05-11 21:44:58 +0000 | [diff] [blame] | 1793 | X86::LOCK_OR8mi, |
| 1794 | X86::LOCK_OR8mr, |
| 1795 | X86::LOCK_OR16mi8, |
| 1796 | X86::LOCK_OR16mi, |
| 1797 | X86::LOCK_OR16mr, |
| 1798 | X86::LOCK_OR32mi8, |
| 1799 | X86::LOCK_OR32mi, |
| 1800 | X86::LOCK_OR32mr, |
| 1801 | X86::LOCK_OR64mi8, |
| 1802 | X86::LOCK_OR64mi32, |
Michael Liao | 8372539 | 2012-09-19 19:36:58 +0000 | [diff] [blame] | 1803 | X86::LOCK_OR64mr, |
Eric Christopher | a1d9e29 | 2011-05-17 08:10:18 +0000 | [diff] [blame] | 1804 | }, |
| 1805 | { |
| 1806 | X86::LOCK_AND8mi, |
| 1807 | X86::LOCK_AND8mr, |
| 1808 | X86::LOCK_AND16mi8, |
| 1809 | X86::LOCK_AND16mi, |
| 1810 | X86::LOCK_AND16mr, |
| 1811 | X86::LOCK_AND32mi8, |
| 1812 | X86::LOCK_AND32mi, |
| 1813 | X86::LOCK_AND32mr, |
| 1814 | X86::LOCK_AND64mi8, |
| 1815 | X86::LOCK_AND64mi32, |
Michael Liao | 8372539 | 2012-09-19 19:36:58 +0000 | [diff] [blame] | 1816 | X86::LOCK_AND64mr, |
Eric Christopher | a1d9e29 | 2011-05-17 08:10:18 +0000 | [diff] [blame] | 1817 | }, |
| 1818 | { |
| 1819 | X86::LOCK_XOR8mi, |
| 1820 | X86::LOCK_XOR8mr, |
| 1821 | X86::LOCK_XOR16mi8, |
| 1822 | X86::LOCK_XOR16mi, |
| 1823 | X86::LOCK_XOR16mr, |
| 1824 | X86::LOCK_XOR32mi8, |
| 1825 | X86::LOCK_XOR32mi, |
| 1826 | X86::LOCK_XOR32mr, |
| 1827 | X86::LOCK_XOR64mi8, |
| 1828 | X86::LOCK_XOR64mi32, |
Michael Liao | 8372539 | 2012-09-19 19:36:58 +0000 | [diff] [blame] | 1829 | X86::LOCK_XOR64mr, |
Eric Christopher | 2a9dbbb | 2011-05-11 21:44:58 +0000 | [diff] [blame] | 1830 | } |
| 1831 | }; |
| 1832 | |
Michael Liao | 8372539 | 2012-09-19 19:36:58 +0000 | [diff] [blame] | 1833 | // Return the target constant operand for atomic-load-op and do simple |
| 1834 | // translations, such as from atomic-load-add to lock-sub. The return value is |
| 1835 | // one of the following 3 cases: |
| 1836 | // + target-constant, the operand could be supported as a target constant. |
| 1837 | // + empty, the operand is not needed any more with the new op selected. |
| 1838 | // + non-empty, otherwise. |
| 1839 | static SDValue getAtomicLoadArithTargetConstant(SelectionDAG *CurDAG, |
Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1840 | SDLoc dl, |
Craig Topper | 83e042a | 2013-08-15 05:57:07 +0000 | [diff] [blame] | 1841 | enum AtomicOpc &Op, MVT NVT, |
Robin Morisset | 6f3d04e | 2014-10-08 23:16:23 +0000 | [diff] [blame] | 1842 | SDValue Val, |
| 1843 | const X86Subtarget *Subtarget) { |
Michael Liao | 8372539 | 2012-09-19 19:36:58 +0000 | [diff] [blame] | 1844 | if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Val)) { |
| 1845 | int64_t CNVal = CN->getSExtValue(); |
| 1846 | // Quit if not 32-bit imm. |
| 1847 | if ((int32_t)CNVal != CNVal) |
| 1848 | return Val; |
Robin Morisset | 880580b | 2014-10-07 23:53:57 +0000 | [diff] [blame] | 1849 | // Quit if INT32_MIN: it would be negated as it is negative and overflow, |
| 1850 | // producing an immediate that does not fit in the 32 bits available for |
| 1851 | // an immediate operand to sub. However, it still fits in 32 bits for the |
| 1852 | // add (since it is not negated) so we can return target-constant. |
| 1853 | if (CNVal == INT32_MIN) |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1854 | return CurDAG->getTargetConstant(CNVal, dl, NVT); |
Michael Liao | 8372539 | 2012-09-19 19:36:58 +0000 | [diff] [blame] | 1855 | // For atomic-load-add, we could do some optimizations. |
| 1856 | if (Op == ADD) { |
| 1857 | // Translate to INC/DEC if ADD by 1 or -1. |
Robin Morisset | 6f3d04e | 2014-10-08 23:16:23 +0000 | [diff] [blame] | 1858 | if (((CNVal == 1) || (CNVal == -1)) && !Subtarget->slowIncDec()) { |
Michael Liao | 8372539 | 2012-09-19 19:36:58 +0000 | [diff] [blame] | 1859 | Op = (CNVal == 1) ? INC : DEC; |
| 1860 | // No more constant operand after being translated into INC/DEC. |
| 1861 | return SDValue(); |
| 1862 | } |
| 1863 | // Translate to SUB if ADD by negative value. |
| 1864 | if (CNVal < 0) { |
| 1865 | Op = SUB; |
| 1866 | CNVal = -CNVal; |
| 1867 | } |
| 1868 | } |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1869 | return CurDAG->getTargetConstant(CNVal, dl, NVT); |
Michael Liao | 8372539 | 2012-09-19 19:36:58 +0000 | [diff] [blame] | 1870 | } |
| 1871 | |
| 1872 | // If the value operand is single-used, try to optimize it. |
| 1873 | if (Op == ADD && Val.hasOneUse()) { |
| 1874 | // Translate (atomic-load-add ptr (sub 0 x)) back to (lock-sub x). |
| 1875 | if (Val.getOpcode() == ISD::SUB && X86::isZeroNode(Val.getOperand(0))) { |
| 1876 | Op = SUB; |
| 1877 | return Val.getOperand(1); |
| 1878 | } |
| 1879 | // A special case for i16, which needs truncating as, in most cases, it's |
| 1880 | // promoted to i32. We will translate |
| 1881 | // (atomic-load-add (truncate (sub 0 x))) to (lock-sub (EXTRACT_SUBREG x)) |
| 1882 | if (Val.getOpcode() == ISD::TRUNCATE && NVT == MVT::i16 && |
| 1883 | Val.getOperand(0).getOpcode() == ISD::SUB && |
| 1884 | X86::isZeroNode(Val.getOperand(0).getOperand(0))) { |
| 1885 | Op = SUB; |
| 1886 | Val = Val.getOperand(0); |
| 1887 | return CurDAG->getTargetExtractSubreg(X86::sub_16bit, dl, NVT, |
| 1888 | Val.getOperand(1)); |
| 1889 | } |
| 1890 | } |
| 1891 | |
| 1892 | return Val; |
| 1893 | } |
| 1894 | |
Sanjay Patel | 85030aa | 2015-10-13 16:23:00 +0000 | [diff] [blame^] | 1895 | SDNode *X86DAGToDAGISel::selectAtomicLoadArith(SDNode *Node, MVT NVT) { |
Eric Christopher | 4a34e61 | 2011-05-10 23:57:45 +0000 | [diff] [blame] | 1896 | if (Node->hasAnyUseOfValue(0)) |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 1897 | return nullptr; |
Chad Rosier | 24c19d2 | 2012-08-01 18:39:17 +0000 | [diff] [blame] | 1898 | |
Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1899 | SDLoc dl(Node); |
Michael Liao | 8372539 | 2012-09-19 19:36:58 +0000 | [diff] [blame] | 1900 | |
Eric Christopher | 56a42eb | 2011-05-17 08:16:14 +0000 | [diff] [blame] | 1901 | // Optimize common patterns for __sync_or_and_fetch and similar arith |
| 1902 | // operations where the result is not used. This allows us to use the "lock" |
| 1903 | // version of the arithmetic instruction. |
Eric Christopher | 4a34e61 | 2011-05-10 23:57:45 +0000 | [diff] [blame] | 1904 | SDValue Chain = Node->getOperand(0); |
| 1905 | SDValue Ptr = Node->getOperand(1); |
| 1906 | SDValue Val = Node->getOperand(2); |
Robin Morisset | 5ce0ce4 | 2014-08-29 20:19:23 +0000 | [diff] [blame] | 1907 | SDValue Base, Scale, Index, Disp, Segment; |
Sanjay Patel | 85030aa | 2015-10-13 16:23:00 +0000 | [diff] [blame^] | 1908 | if (!selectAddr(Node, Ptr, Base, Scale, Index, Disp, Segment)) |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 1909 | return nullptr; |
Eric Christopher | 4a34e61 | 2011-05-10 23:57:45 +0000 | [diff] [blame] | 1910 | |
Eric Christopher | a1d9e29 | 2011-05-17 08:10:18 +0000 | [diff] [blame] | 1911 | // Which index into the table. |
| 1912 | enum AtomicOpc Op; |
| 1913 | switch (Node->getOpcode()) { |
Michael Liao | 8372539 | 2012-09-19 19:36:58 +0000 | [diff] [blame] | 1914 | default: |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 1915 | return nullptr; |
Eric Christopher | a1d9e29 | 2011-05-17 08:10:18 +0000 | [diff] [blame] | 1916 | case ISD::ATOMIC_LOAD_OR: |
| 1917 | Op = OR; |
| 1918 | break; |
| 1919 | case ISD::ATOMIC_LOAD_AND: |
| 1920 | Op = AND; |
| 1921 | break; |
| 1922 | case ISD::ATOMIC_LOAD_XOR: |
| 1923 | Op = XOR; |
| 1924 | break; |
Michael Liao | 8372539 | 2012-09-19 19:36:58 +0000 | [diff] [blame] | 1925 | case ISD::ATOMIC_LOAD_ADD: |
| 1926 | Op = ADD; |
| 1927 | break; |
Eric Christopher | a1d9e29 | 2011-05-17 08:10:18 +0000 | [diff] [blame] | 1928 | } |
Andrew Trick | 52b8387 | 2013-04-13 06:07:36 +0000 | [diff] [blame] | 1929 | |
Robin Morisset | 6f3d04e | 2014-10-08 23:16:23 +0000 | [diff] [blame] | 1930 | Val = getAtomicLoadArithTargetConstant(CurDAG, dl, Op, NVT, Val, Subtarget); |
Michael Liao | 8372539 | 2012-09-19 19:36:58 +0000 | [diff] [blame] | 1931 | bool isUnOp = !Val.getNode(); |
| 1932 | bool isCN = Val.getNode() && (Val.getOpcode() == ISD::TargetConstant); |
Chad Rosier | 24c19d2 | 2012-08-01 18:39:17 +0000 | [diff] [blame] | 1933 | |
Eric Christopher | 4a34e61 | 2011-05-10 23:57:45 +0000 | [diff] [blame] | 1934 | unsigned Opc = 0; |
Craig Topper | 83e042a | 2013-08-15 05:57:07 +0000 | [diff] [blame] | 1935 | switch (NVT.SimpleTy) { |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 1936 | default: return nullptr; |
Eric Christopher | 4a34e61 | 2011-05-10 23:57:45 +0000 | [diff] [blame] | 1937 | case MVT::i8: |
| 1938 | if (isCN) |
Eric Christopher | eb47a2a | 2011-05-17 07:47:55 +0000 | [diff] [blame] | 1939 | Opc = AtomicOpcTbl[Op][ConstantI8]; |
Eric Christopher | 4a34e61 | 2011-05-10 23:57:45 +0000 | [diff] [blame] | 1940 | else |
Eric Christopher | eb47a2a | 2011-05-17 07:47:55 +0000 | [diff] [blame] | 1941 | Opc = AtomicOpcTbl[Op][I8]; |
Eric Christopher | 4a34e61 | 2011-05-10 23:57:45 +0000 | [diff] [blame] | 1942 | break; |
| 1943 | case MVT::i16: |
| 1944 | if (isCN) { |
| 1945 | if (immSext8(Val.getNode())) |
Eric Christopher | eb47a2a | 2011-05-17 07:47:55 +0000 | [diff] [blame] | 1946 | Opc = AtomicOpcTbl[Op][SextConstantI16]; |
Eric Christopher | 4a34e61 | 2011-05-10 23:57:45 +0000 | [diff] [blame] | 1947 | else |
Eric Christopher | eb47a2a | 2011-05-17 07:47:55 +0000 | [diff] [blame] | 1948 | Opc = AtomicOpcTbl[Op][ConstantI16]; |
Eric Christopher | 4a34e61 | 2011-05-10 23:57:45 +0000 | [diff] [blame] | 1949 | } else |
Eric Christopher | eb47a2a | 2011-05-17 07:47:55 +0000 | [diff] [blame] | 1950 | Opc = AtomicOpcTbl[Op][I16]; |
Eric Christopher | 4a34e61 | 2011-05-10 23:57:45 +0000 | [diff] [blame] | 1951 | break; |
| 1952 | case MVT::i32: |
| 1953 | if (isCN) { |
| 1954 | if (immSext8(Val.getNode())) |
Eric Christopher | eb47a2a | 2011-05-17 07:47:55 +0000 | [diff] [blame] | 1955 | Opc = AtomicOpcTbl[Op][SextConstantI32]; |
Eric Christopher | 4a34e61 | 2011-05-10 23:57:45 +0000 | [diff] [blame] | 1956 | else |
Eric Christopher | eb47a2a | 2011-05-17 07:47:55 +0000 | [diff] [blame] | 1957 | Opc = AtomicOpcTbl[Op][ConstantI32]; |
Eric Christopher | 4a34e61 | 2011-05-10 23:57:45 +0000 | [diff] [blame] | 1958 | } else |
Eric Christopher | eb47a2a | 2011-05-17 07:47:55 +0000 | [diff] [blame] | 1959 | Opc = AtomicOpcTbl[Op][I32]; |
Eric Christopher | 4a34e61 | 2011-05-10 23:57:45 +0000 | [diff] [blame] | 1960 | break; |
| 1961 | case MVT::i64: |
| 1962 | if (isCN) { |
| 1963 | if (immSext8(Val.getNode())) |
Eric Christopher | eb47a2a | 2011-05-17 07:47:55 +0000 | [diff] [blame] | 1964 | Opc = AtomicOpcTbl[Op][SextConstantI64]; |
Eric Christopher | 4a34e61 | 2011-05-10 23:57:45 +0000 | [diff] [blame] | 1965 | else if (i64immSExt32(Val.getNode())) |
Eric Christopher | eb47a2a | 2011-05-17 07:47:55 +0000 | [diff] [blame] | 1966 | Opc = AtomicOpcTbl[Op][ConstantI64]; |
Robin Morisset | 880580b | 2014-10-07 23:53:57 +0000 | [diff] [blame] | 1967 | else |
| 1968 | llvm_unreachable("True 64 bits constant in SelectAtomicLoadArith"); |
Robin Morisset | 5ce0ce4 | 2014-08-29 20:19:23 +0000 | [diff] [blame] | 1969 | } else |
| 1970 | Opc = AtomicOpcTbl[Op][I64]; |
Eric Christopher | 4a34e61 | 2011-05-10 23:57:45 +0000 | [diff] [blame] | 1971 | break; |
| 1972 | } |
Chad Rosier | 24c19d2 | 2012-08-01 18:39:17 +0000 | [diff] [blame] | 1973 | |
Eric Christopher | c9321737 | 2011-06-30 00:48:30 +0000 | [diff] [blame] | 1974 | assert(Opc != 0 && "Invalid arith lock transform!"); |
| 1975 | |
Robin Morisset | 5ce0ce4 | 2014-08-29 20:19:23 +0000 | [diff] [blame] | 1976 | // Building the new node. |
Michael Liao | 8372539 | 2012-09-19 19:36:58 +0000 | [diff] [blame] | 1977 | SDValue Ret; |
Michael Liao | 8372539 | 2012-09-19 19:36:58 +0000 | [diff] [blame] | 1978 | if (isUnOp) { |
Robin Morisset | 5ce0ce4 | 2014-08-29 20:19:23 +0000 | [diff] [blame] | 1979 | SDValue Ops[] = { Base, Scale, Index, Disp, Segment, Chain }; |
Michael Liao | b53d896 | 2013-04-19 22:22:57 +0000 | [diff] [blame] | 1980 | Ret = SDValue(CurDAG->getMachineNode(Opc, dl, MVT::Other, Ops), 0); |
Michael Liao | 8372539 | 2012-09-19 19:36:58 +0000 | [diff] [blame] | 1981 | } else { |
Robin Morisset | 5ce0ce4 | 2014-08-29 20:19:23 +0000 | [diff] [blame] | 1982 | SDValue Ops[] = { Base, Scale, Index, Disp, Segment, Val, Chain }; |
Michael Liao | b53d896 | 2013-04-19 22:22:57 +0000 | [diff] [blame] | 1983 | Ret = SDValue(CurDAG->getMachineNode(Opc, dl, MVT::Other, Ops), 0); |
Michael Liao | 8372539 | 2012-09-19 19:36:58 +0000 | [diff] [blame] | 1984 | } |
Robin Morisset | 5ce0ce4 | 2014-08-29 20:19:23 +0000 | [diff] [blame] | 1985 | |
| 1986 | // Copying the MachineMemOperand. |
| 1987 | MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1); |
| 1988 | MemOp[0] = cast<MemSDNode>(Node)->getMemOperand(); |
Eric Christopher | 4a34e61 | 2011-05-10 23:57:45 +0000 | [diff] [blame] | 1989 | cast<MachineSDNode>(Ret)->setMemRefs(MemOp, MemOp + 1); |
Robin Morisset | 5ce0ce4 | 2014-08-29 20:19:23 +0000 | [diff] [blame] | 1990 | |
| 1991 | // We need to have two outputs as that is what the original instruction had. |
| 1992 | // So we add a dummy, undefined output. This is safe as we checked first |
| 1993 | // that no-one uses our output anyway. |
| 1994 | SDValue Undef = SDValue(CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF, |
| 1995 | dl, NVT), 0); |
Eric Christopher | 4a34e61 | 2011-05-10 23:57:45 +0000 | [diff] [blame] | 1996 | SDValue RetVals[] = { Undef, Ret }; |
Craig Topper | 64941d9 | 2014-04-27 19:20:57 +0000 | [diff] [blame] | 1997 | return CurDAG->getMergeValues(RetVals, dl).getNode(); |
Eric Christopher | 4a34e61 | 2011-05-10 23:57:45 +0000 | [diff] [blame] | 1998 | } |
| 1999 | |
Sanjay Patel | b5723d0 | 2015-10-13 15:12:27 +0000 | [diff] [blame] | 2000 | /// Test whether the given X86ISD::CMP node has any uses which require the SF |
| 2001 | /// or OF bits to be accurate. |
Sanjay Patel | 85030aa | 2015-10-13 16:23:00 +0000 | [diff] [blame^] | 2002 | static bool hasNoSignedComparisonUses(SDNode *N) { |
Dan Gohman | 7d9dffb | 2009-10-09 20:35:19 +0000 | [diff] [blame] | 2003 | // Examine each user of the node. |
| 2004 | for (SDNode::use_iterator UI = N->use_begin(), |
| 2005 | UE = N->use_end(); UI != UE; ++UI) { |
| 2006 | // Only examine CopyToReg uses. |
| 2007 | if (UI->getOpcode() != ISD::CopyToReg) |
| 2008 | return false; |
| 2009 | // Only examine CopyToReg uses that copy to EFLAGS. |
| 2010 | if (cast<RegisterSDNode>(UI->getOperand(1))->getReg() != |
| 2011 | X86::EFLAGS) |
| 2012 | return false; |
| 2013 | // Examine each user of the CopyToReg use. |
| 2014 | for (SDNode::use_iterator FlagUI = UI->use_begin(), |
| 2015 | FlagUE = UI->use_end(); FlagUI != FlagUE; ++FlagUI) { |
| 2016 | // Only examine the Flag result. |
| 2017 | if (FlagUI.getUse().getResNo() != 1) continue; |
| 2018 | // Anything unusual: assume conservatively. |
| 2019 | if (!FlagUI->isMachineOpcode()) return false; |
| 2020 | // Examine the opcode of the user. |
| 2021 | switch (FlagUI->getMachineOpcode()) { |
| 2022 | // These comparisons don't treat the most significant bit specially. |
| 2023 | case X86::SETAr: case X86::SETAEr: case X86::SETBr: case X86::SETBEr: |
| 2024 | case X86::SETEr: case X86::SETNEr: case X86::SETPr: case X86::SETNPr: |
| 2025 | case X86::SETAm: case X86::SETAEm: case X86::SETBm: case X86::SETBEm: |
| 2026 | case X86::SETEm: case X86::SETNEm: case X86::SETPm: case X86::SETNPm: |
Craig Topper | 49758aa | 2015-01-06 04:23:53 +0000 | [diff] [blame] | 2027 | case X86::JA_1: case X86::JAE_1: case X86::JB_1: case X86::JBE_1: |
| 2028 | case X86::JE_1: case X86::JNE_1: case X86::JP_1: case X86::JNP_1: |
Dan Gohman | 7d9dffb | 2009-10-09 20:35:19 +0000 | [diff] [blame] | 2029 | case X86::CMOVA16rr: case X86::CMOVA16rm: |
| 2030 | case X86::CMOVA32rr: case X86::CMOVA32rm: |
| 2031 | case X86::CMOVA64rr: case X86::CMOVA64rm: |
| 2032 | case X86::CMOVAE16rr: case X86::CMOVAE16rm: |
| 2033 | case X86::CMOVAE32rr: case X86::CMOVAE32rm: |
| 2034 | case X86::CMOVAE64rr: case X86::CMOVAE64rm: |
| 2035 | case X86::CMOVB16rr: case X86::CMOVB16rm: |
| 2036 | case X86::CMOVB32rr: case X86::CMOVB32rm: |
| 2037 | case X86::CMOVB64rr: case X86::CMOVB64rm: |
Chris Lattner | 1a1c600 | 2010-10-05 23:00:14 +0000 | [diff] [blame] | 2038 | case X86::CMOVBE16rr: case X86::CMOVBE16rm: |
| 2039 | case X86::CMOVBE32rr: case X86::CMOVBE32rm: |
| 2040 | case X86::CMOVBE64rr: case X86::CMOVBE64rm: |
Dan Gohman | 7d9dffb | 2009-10-09 20:35:19 +0000 | [diff] [blame] | 2041 | case X86::CMOVE16rr: case X86::CMOVE16rm: |
| 2042 | case X86::CMOVE32rr: case X86::CMOVE32rm: |
| 2043 | case X86::CMOVE64rr: case X86::CMOVE64rm: |
| 2044 | case X86::CMOVNE16rr: case X86::CMOVNE16rm: |
| 2045 | case X86::CMOVNE32rr: case X86::CMOVNE32rm: |
| 2046 | case X86::CMOVNE64rr: case X86::CMOVNE64rm: |
| 2047 | case X86::CMOVNP16rr: case X86::CMOVNP16rm: |
| 2048 | case X86::CMOVNP32rr: case X86::CMOVNP32rm: |
| 2049 | case X86::CMOVNP64rr: case X86::CMOVNP64rm: |
| 2050 | case X86::CMOVP16rr: case X86::CMOVP16rm: |
| 2051 | case X86::CMOVP32rr: case X86::CMOVP32rm: |
| 2052 | case X86::CMOVP64rr: case X86::CMOVP64rm: |
| 2053 | continue; |
| 2054 | // Anything else: assume conservatively. |
| 2055 | default: return false; |
| 2056 | } |
| 2057 | } |
| 2058 | } |
| 2059 | return true; |
| 2060 | } |
| 2061 | |
Sanjay Patel | b5723d0 | 2015-10-13 15:12:27 +0000 | [diff] [blame] | 2062 | /// Check whether or not the chain ending in StoreNode is suitable for doing |
| 2063 | /// the {load; increment or decrement; store} to modify transformation. |
Chad Rosier | 24c19d2 | 2012-08-01 18:39:17 +0000 | [diff] [blame] | 2064 | static bool isLoadIncOrDecStore(StoreSDNode *StoreNode, unsigned Opc, |
Evan Cheng | 3e869f0 | 2012-04-12 19:14:21 +0000 | [diff] [blame] | 2065 | SDValue StoredVal, SelectionDAG *CurDAG, |
| 2066 | LoadSDNode* &LoadNode, SDValue &InputChain) { |
Joel Jones | 68d59e8 | 2012-03-29 05:45:48 +0000 | [diff] [blame] | 2067 | |
| 2068 | // is the value stored the result of a DEC or INC? |
| 2069 | if (!(Opc == X86ISD::DEC || Opc == X86ISD::INC)) return false; |
| 2070 | |
Joel Jones | 68d59e8 | 2012-03-29 05:45:48 +0000 | [diff] [blame] | 2071 | // is the stored value result 0 of the load? |
| 2072 | if (StoredVal.getResNo() != 0) return false; |
| 2073 | |
| 2074 | // are there other uses of the loaded value than the inc or dec? |
| 2075 | if (!StoredVal.getNode()->hasNUsesOfValue(1, 0)) return false; |
| 2076 | |
Joel Jones | 68d59e8 | 2012-03-29 05:45:48 +0000 | [diff] [blame] | 2077 | // is the store non-extending and non-indexed? |
Evan Cheng | 3e869f0 | 2012-04-12 19:14:21 +0000 | [diff] [blame] | 2078 | if (!ISD::isNormalStore(StoreNode) || StoreNode->isNonTemporal()) |
Joel Jones | 68d59e8 | 2012-03-29 05:45:48 +0000 | [diff] [blame] | 2079 | return false; |
| 2080 | |
Evan Cheng | 3e869f0 | 2012-04-12 19:14:21 +0000 | [diff] [blame] | 2081 | SDValue Load = StoredVal->getOperand(0); |
| 2082 | // Is the stored value a non-extending and non-indexed load? |
| 2083 | if (!ISD::isNormalLoad(Load.getNode())) return false; |
| 2084 | |
| 2085 | // Return LoadNode by reference. |
| 2086 | LoadNode = cast<LoadSDNode>(Load); |
| 2087 | // is the size of the value one that we can handle? (i.e. 64, 32, 16, or 8) |
Chad Rosier | 24c19d2 | 2012-08-01 18:39:17 +0000 | [diff] [blame] | 2088 | EVT LdVT = LoadNode->getMemoryVT(); |
| 2089 | if (LdVT != MVT::i64 && LdVT != MVT::i32 && LdVT != MVT::i16 && |
Evan Cheng | 3e869f0 | 2012-04-12 19:14:21 +0000 | [diff] [blame] | 2090 | LdVT != MVT::i8) |
| 2091 | return false; |
| 2092 | |
| 2093 | // Is store the only read of the loaded value? |
| 2094 | if (!Load.hasOneUse()) |
| 2095 | return false; |
Chad Rosier | 24c19d2 | 2012-08-01 18:39:17 +0000 | [diff] [blame] | 2096 | |
Evan Cheng | 3e869f0 | 2012-04-12 19:14:21 +0000 | [diff] [blame] | 2097 | // Is the address of the store the same as the load? |
| 2098 | if (LoadNode->getBasePtr() != StoreNode->getBasePtr() || |
| 2099 | LoadNode->getOffset() != StoreNode->getOffset()) |
| 2100 | return false; |
| 2101 | |
| 2102 | // Check if the chain is produced by the load or is a TokenFactor with |
| 2103 | // the load output chain as an operand. Return InputChain by reference. |
| 2104 | SDValue Chain = StoreNode->getChain(); |
| 2105 | |
| 2106 | bool ChainCheck = false; |
| 2107 | if (Chain == Load.getValue(1)) { |
| 2108 | ChainCheck = true; |
| 2109 | InputChain = LoadNode->getChain(); |
| 2110 | } else if (Chain.getOpcode() == ISD::TokenFactor) { |
| 2111 | SmallVector<SDValue, 4> ChainOps; |
| 2112 | for (unsigned i = 0, e = Chain.getNumOperands(); i != e; ++i) { |
| 2113 | SDValue Op = Chain.getOperand(i); |
| 2114 | if (Op == Load.getValue(1)) { |
| 2115 | ChainCheck = true; |
| 2116 | continue; |
| 2117 | } |
Evan Cheng | 58a95f0 | 2012-05-16 01:54:27 +0000 | [diff] [blame] | 2118 | |
| 2119 | // Make sure using Op as part of the chain would not cause a cycle here. |
| 2120 | // In theory, we could check whether the chain node is a predecessor of |
| 2121 | // the load. But that can be very expensive. Instead visit the uses and |
| 2122 | // make sure they all have smaller node id than the load. |
| 2123 | int LoadId = LoadNode->getNodeId(); |
| 2124 | for (SDNode::use_iterator UI = Op.getNode()->use_begin(), |
| 2125 | UE = UI->use_end(); UI != UE; ++UI) { |
| 2126 | if (UI.getUse().getResNo() != 0) |
| 2127 | continue; |
| 2128 | if (UI->getNodeId() > LoadId) |
| 2129 | return false; |
| 2130 | } |
| 2131 | |
Evan Cheng | 3e869f0 | 2012-04-12 19:14:21 +0000 | [diff] [blame] | 2132 | ChainOps.push_back(Op); |
| 2133 | } |
| 2134 | |
| 2135 | if (ChainCheck) |
| 2136 | // Make a new TokenFactor with all the other input chains except |
| 2137 | // for the load. |
Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2138 | InputChain = CurDAG->getNode(ISD::TokenFactor, SDLoc(Chain), |
Craig Topper | 48d114b | 2014-04-26 18:35:24 +0000 | [diff] [blame] | 2139 | MVT::Other, ChainOps); |
Evan Cheng | 3e869f0 | 2012-04-12 19:14:21 +0000 | [diff] [blame] | 2140 | } |
| 2141 | if (!ChainCheck) |
Joel Jones | 68d59e8 | 2012-03-29 05:45:48 +0000 | [diff] [blame] | 2142 | return false; |
| 2143 | |
| 2144 | return true; |
| 2145 | } |
| 2146 | |
Sanjay Patel | b5723d0 | 2015-10-13 15:12:27 +0000 | [diff] [blame] | 2147 | /// Get the appropriate X86 opcode for an in-memory increment or decrement. |
| 2148 | /// Opc should be X86ISD::DEC or X86ISD::INC. |
Joel Jones | 68d59e8 | 2012-03-29 05:45:48 +0000 | [diff] [blame] | 2149 | static unsigned getFusedLdStOpcode(EVT &LdVT, unsigned Opc) { |
| 2150 | if (Opc == X86ISD::DEC) { |
| 2151 | if (LdVT == MVT::i64) return X86::DEC64m; |
| 2152 | if (LdVT == MVT::i32) return X86::DEC32m; |
| 2153 | if (LdVT == MVT::i16) return X86::DEC16m; |
| 2154 | if (LdVT == MVT::i8) return X86::DEC8m; |
Benjamin Kramer | 8619c37 | 2012-03-29 12:37:26 +0000 | [diff] [blame] | 2155 | } else { |
| 2156 | assert(Opc == X86ISD::INC && "unrecognized opcode"); |
Joel Jones | 68d59e8 | 2012-03-29 05:45:48 +0000 | [diff] [blame] | 2157 | if (LdVT == MVT::i64) return X86::INC64m; |
| 2158 | if (LdVT == MVT::i32) return X86::INC32m; |
| 2159 | if (LdVT == MVT::i16) return X86::INC16m; |
| 2160 | if (LdVT == MVT::i8) return X86::INC8m; |
Joel Jones | 68d59e8 | 2012-03-29 05:45:48 +0000 | [diff] [blame] | 2161 | } |
Benjamin Kramer | 8619c37 | 2012-03-29 12:37:26 +0000 | [diff] [blame] | 2162 | llvm_unreachable("unrecognized size for LdVT"); |
Joel Jones | 68d59e8 | 2012-03-29 05:45:48 +0000 | [diff] [blame] | 2163 | } |
| 2164 | |
Sanjay Patel | b5723d0 | 2015-10-13 15:12:27 +0000 | [diff] [blame] | 2165 | /// Customized ISel for GATHER operations. |
Sanjay Patel | 85030aa | 2015-10-13 16:23:00 +0000 | [diff] [blame^] | 2166 | SDNode *X86DAGToDAGISel::selectGather(SDNode *Node, unsigned Opc) { |
Manman Ren | a098204 | 2012-06-26 19:47:59 +0000 | [diff] [blame] | 2167 | // Operands of Gather: VSrc, Base, VIdx, VMask, Scale |
| 2168 | SDValue Chain = Node->getOperand(0); |
| 2169 | SDValue VSrc = Node->getOperand(2); |
| 2170 | SDValue Base = Node->getOperand(3); |
| 2171 | SDValue VIdx = Node->getOperand(4); |
| 2172 | SDValue VMask = Node->getOperand(5); |
| 2173 | ConstantSDNode *Scale = dyn_cast<ConstantSDNode>(Node->getOperand(6)); |
Craig Topper | fbb954f7 | 2012-07-01 02:17:08 +0000 | [diff] [blame] | 2174 | if (!Scale) |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 2175 | return nullptr; |
Manman Ren | a098204 | 2012-06-26 19:47:59 +0000 | [diff] [blame] | 2176 | |
Craig Topper | f7755df | 2012-07-12 06:52:41 +0000 | [diff] [blame] | 2177 | SDVTList VTs = CurDAG->getVTList(VSrc.getValueType(), VSrc.getValueType(), |
| 2178 | MVT::Other); |
| 2179 | |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2180 | SDLoc DL(Node); |
| 2181 | |
Manman Ren | a098204 | 2012-06-26 19:47:59 +0000 | [diff] [blame] | 2182 | // Memory Operands: Base, Scale, Index, Disp, Segment |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2183 | SDValue Disp = CurDAG->getTargetConstant(0, DL, MVT::i32); |
Manman Ren | a098204 | 2012-06-26 19:47:59 +0000 | [diff] [blame] | 2184 | SDValue Segment = CurDAG->getRegister(0, MVT::i32); |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2185 | const SDValue Ops[] = { VSrc, Base, getI8Imm(Scale->getSExtValue(), DL), VIdx, |
Manman Ren | a098204 | 2012-06-26 19:47:59 +0000 | [diff] [blame] | 2186 | Disp, Segment, VMask, Chain}; |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2187 | SDNode *ResNode = CurDAG->getMachineNode(Opc, DL, VTs, Ops); |
Craig Topper | f7755df | 2012-07-12 06:52:41 +0000 | [diff] [blame] | 2188 | // Node has 2 outputs: VDst and MVT::Other. |
| 2189 | // ResNode has 3 outputs: VDst, VMask_wb, and MVT::Other. |
| 2190 | // We replace VDst of Node with VDst of ResNode, and Other of Node with Other |
| 2191 | // of ResNode. |
| 2192 | ReplaceUses(SDValue(Node, 0), SDValue(ResNode, 0)); |
| 2193 | ReplaceUses(SDValue(Node, 1), SDValue(ResNode, 2)); |
Manman Ren | a098204 | 2012-06-26 19:47:59 +0000 | [diff] [blame] | 2194 | return ResNode; |
| 2195 | } |
| 2196 | |
Dan Gohman | ea6f91f | 2010-01-05 01:24:18 +0000 | [diff] [blame] | 2197 | SDNode *X86DAGToDAGISel::Select(SDNode *Node) { |
Craig Topper | 83e042a | 2013-08-15 05:57:07 +0000 | [diff] [blame] | 2198 | MVT NVT = Node->getSimpleValueType(0); |
Evan Cheng | 10d2790 | 2006-01-06 20:36:21 +0000 | [diff] [blame] | 2199 | unsigned Opc, MOpc; |
| 2200 | unsigned Opcode = Node->getOpcode(); |
Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2201 | SDLoc dl(Node); |
Chad Rosier | 24c19d2 | 2012-08-01 18:39:17 +0000 | [diff] [blame] | 2202 | |
Chris Lattner | f98f124 | 2010-03-02 06:34:30 +0000 | [diff] [blame] | 2203 | DEBUG(dbgs() << "Selecting: "; Node->dump(CurDAG); dbgs() << '\n'); |
Evan Cheng | d49cc36 | 2006-02-10 22:24:32 +0000 | [diff] [blame] | 2204 | |
Dan Gohman | 1705968 | 2008-07-17 19:10:17 +0000 | [diff] [blame] | 2205 | if (Node->isMachineOpcode()) { |
Chris Lattner | f98f124 | 2010-03-02 06:34:30 +0000 | [diff] [blame] | 2206 | DEBUG(dbgs() << "== "; Node->dump(CurDAG); dbgs() << '\n'); |
Tim Northover | 31d093c | 2013-09-22 08:21:56 +0000 | [diff] [blame] | 2207 | Node->setNodeId(-1); |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 2208 | return nullptr; // Already selected. |
Evan Cheng | 6dc90ca | 2006-02-09 00:37:58 +0000 | [diff] [blame] | 2209 | } |
Evan Cheng | 2ae799a | 2006-01-11 22:15:18 +0000 | [diff] [blame] | 2210 | |
Evan Cheng | 10d2790 | 2006-01-06 20:36:21 +0000 | [diff] [blame] | 2211 | switch (Opcode) { |
Tobias Grosser | 85508e8 | 2015-08-19 11:35:10 +0000 | [diff] [blame] | 2212 | default: break; |
JF Bastien | 5ab87ed | 2015-08-19 16:17:08 +0000 | [diff] [blame] | 2213 | case ISD::BRIND: { |
| 2214 | if (Subtarget->isTargetNaCl()) |
| 2215 | // NaCl has its own pass where jmp %r32 are converted to jmp %r64. We |
| 2216 | // leave the instruction alone. |
| 2217 | break; |
| 2218 | if (Subtarget->isTarget64BitILP32()) { |
| 2219 | // Converts a 32-bit register to a 64-bit, zero-extended version of |
| 2220 | // it. This is needed because x86-64 can do many things, but jmp %r32 |
| 2221 | // ain't one of them. |
| 2222 | const SDValue &Target = Node->getOperand(1); |
| 2223 | assert(Target.getSimpleValueType() == llvm::MVT::i32); |
| 2224 | SDValue ZextTarget = CurDAG->getZExtOrTrunc(Target, dl, EVT(MVT::i64)); |
| 2225 | SDValue Brind = CurDAG->getNode(ISD::BRIND, dl, MVT::Other, |
| 2226 | Node->getOperand(0), ZextTarget); |
| 2227 | ReplaceUses(SDValue(Node, 0), Brind); |
| 2228 | SelectCode(ZextTarget.getNode()); |
| 2229 | SelectCode(Brind.getNode()); |
| 2230 | return nullptr; |
| 2231 | } |
| 2232 | break; |
| 2233 | } |
Manman Ren | a098204 | 2012-06-26 19:47:59 +0000 | [diff] [blame] | 2234 | case ISD::INTRINSIC_W_CHAIN: { |
| 2235 | unsigned IntNo = cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue(); |
| 2236 | switch (IntNo) { |
| 2237 | default: break; |
| 2238 | case Intrinsic::x86_avx2_gather_d_pd: |
Manman Ren | a098204 | 2012-06-26 19:47:59 +0000 | [diff] [blame] | 2239 | case Intrinsic::x86_avx2_gather_d_pd_256: |
Manman Ren | a098204 | 2012-06-26 19:47:59 +0000 | [diff] [blame] | 2240 | case Intrinsic::x86_avx2_gather_q_pd: |
Manman Ren | a098204 | 2012-06-26 19:47:59 +0000 | [diff] [blame] | 2241 | case Intrinsic::x86_avx2_gather_q_pd_256: |
Manman Ren | a098204 | 2012-06-26 19:47:59 +0000 | [diff] [blame] | 2242 | case Intrinsic::x86_avx2_gather_d_ps: |
Manman Ren | a098204 | 2012-06-26 19:47:59 +0000 | [diff] [blame] | 2243 | case Intrinsic::x86_avx2_gather_d_ps_256: |
Manman Ren | a098204 | 2012-06-26 19:47:59 +0000 | [diff] [blame] | 2244 | case Intrinsic::x86_avx2_gather_q_ps: |
Manman Ren | a098204 | 2012-06-26 19:47:59 +0000 | [diff] [blame] | 2245 | case Intrinsic::x86_avx2_gather_q_ps_256: |
Manman Ren | 98a5bf2 | 2012-06-29 00:54:20 +0000 | [diff] [blame] | 2246 | case Intrinsic::x86_avx2_gather_d_q: |
Manman Ren | 98a5bf2 | 2012-06-29 00:54:20 +0000 | [diff] [blame] | 2247 | case Intrinsic::x86_avx2_gather_d_q_256: |
Manman Ren | 98a5bf2 | 2012-06-29 00:54:20 +0000 | [diff] [blame] | 2248 | case Intrinsic::x86_avx2_gather_q_q: |
Manman Ren | 98a5bf2 | 2012-06-29 00:54:20 +0000 | [diff] [blame] | 2249 | case Intrinsic::x86_avx2_gather_q_q_256: |
Manman Ren | 98a5bf2 | 2012-06-29 00:54:20 +0000 | [diff] [blame] | 2250 | case Intrinsic::x86_avx2_gather_d_d: |
Manman Ren | 98a5bf2 | 2012-06-29 00:54:20 +0000 | [diff] [blame] | 2251 | case Intrinsic::x86_avx2_gather_d_d_256: |
Manman Ren | 98a5bf2 | 2012-06-29 00:54:20 +0000 | [diff] [blame] | 2252 | case Intrinsic::x86_avx2_gather_q_d: |
Craig Topper | def044b | 2012-07-01 02:05:52 +0000 | [diff] [blame] | 2253 | case Intrinsic::x86_avx2_gather_q_d_256: { |
Michael Liao | 00b20cc | 2013-06-05 18:12:26 +0000 | [diff] [blame] | 2254 | if (!Subtarget->hasAVX2()) |
| 2255 | break; |
Craig Topper | def044b | 2012-07-01 02:05:52 +0000 | [diff] [blame] | 2256 | unsigned Opc; |
| 2257 | switch (IntNo) { |
Craig Topper | 3af251d | 2012-07-01 02:55:34 +0000 | [diff] [blame] | 2258 | default: llvm_unreachable("Impossible intrinsic"); |
Craig Topper | def044b | 2012-07-01 02:05:52 +0000 | [diff] [blame] | 2259 | case Intrinsic::x86_avx2_gather_d_pd: Opc = X86::VGATHERDPDrm; break; |
| 2260 | case Intrinsic::x86_avx2_gather_d_pd_256: Opc = X86::VGATHERDPDYrm; break; |
| 2261 | case Intrinsic::x86_avx2_gather_q_pd: Opc = X86::VGATHERQPDrm; break; |
| 2262 | case Intrinsic::x86_avx2_gather_q_pd_256: Opc = X86::VGATHERQPDYrm; break; |
| 2263 | case Intrinsic::x86_avx2_gather_d_ps: Opc = X86::VGATHERDPSrm; break; |
| 2264 | case Intrinsic::x86_avx2_gather_d_ps_256: Opc = X86::VGATHERDPSYrm; break; |
| 2265 | case Intrinsic::x86_avx2_gather_q_ps: Opc = X86::VGATHERQPSrm; break; |
| 2266 | case Intrinsic::x86_avx2_gather_q_ps_256: Opc = X86::VGATHERQPSYrm; break; |
| 2267 | case Intrinsic::x86_avx2_gather_d_q: Opc = X86::VPGATHERDQrm; break; |
| 2268 | case Intrinsic::x86_avx2_gather_d_q_256: Opc = X86::VPGATHERDQYrm; break; |
| 2269 | case Intrinsic::x86_avx2_gather_q_q: Opc = X86::VPGATHERQQrm; break; |
| 2270 | case Intrinsic::x86_avx2_gather_q_q_256: Opc = X86::VPGATHERQQYrm; break; |
| 2271 | case Intrinsic::x86_avx2_gather_d_d: Opc = X86::VPGATHERDDrm; break; |
| 2272 | case Intrinsic::x86_avx2_gather_d_d_256: Opc = X86::VPGATHERDDYrm; break; |
| 2273 | case Intrinsic::x86_avx2_gather_q_d: Opc = X86::VPGATHERQDrm; break; |
| 2274 | case Intrinsic::x86_avx2_gather_q_d_256: Opc = X86::VPGATHERQDYrm; break; |
| 2275 | } |
Sanjay Patel | 85030aa | 2015-10-13 16:23:00 +0000 | [diff] [blame^] | 2276 | SDNode *RetVal = selectGather(Node, Opc); |
Craig Topper | fbb954f7 | 2012-07-01 02:17:08 +0000 | [diff] [blame] | 2277 | if (RetVal) |
Craig Topper | f7755df | 2012-07-12 06:52:41 +0000 | [diff] [blame] | 2278 | // We already called ReplaceUses inside SelectGather. |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 2279 | return nullptr; |
Craig Topper | e15e5f7 | 2012-07-01 02:18:18 +0000 | [diff] [blame] | 2280 | break; |
Craig Topper | def044b | 2012-07-01 02:05:52 +0000 | [diff] [blame] | 2281 | } |
Manman Ren | a098204 | 2012-06-26 19:47:59 +0000 | [diff] [blame] | 2282 | } |
| 2283 | break; |
| 2284 | } |
Dan Gohman | 757eee8 | 2009-08-02 16:10:52 +0000 | [diff] [blame] | 2285 | case X86ISD::GlobalBaseReg: |
| 2286 | return getGlobalBaseReg(); |
Evan Cheng | e0ed6ec | 2006-02-23 20:41:18 +0000 | [diff] [blame] | 2287 | |
Quentin Colombet | dbe33e7 | 2014-11-06 02:25:03 +0000 | [diff] [blame] | 2288 | case X86ISD::SHRUNKBLEND: { |
| 2289 | // SHRUNKBLEND selects like a regular VSELECT. |
| 2290 | SDValue VSelect = CurDAG->getNode( |
| 2291 | ISD::VSELECT, SDLoc(Node), Node->getValueType(0), Node->getOperand(0), |
| 2292 | Node->getOperand(1), Node->getOperand(2)); |
| 2293 | ReplaceUses(SDValue(Node, 0), VSelect); |
| 2294 | SelectCode(VSelect.getNode()); |
| 2295 | // We already called ReplaceUses. |
| 2296 | return nullptr; |
| 2297 | } |
Craig Topper | 3af251d | 2012-07-01 02:55:34 +0000 | [diff] [blame] | 2298 | |
Eric Christopher | a1d9e29 | 2011-05-17 08:10:18 +0000 | [diff] [blame] | 2299 | case ISD::ATOMIC_LOAD_XOR: |
| 2300 | case ISD::ATOMIC_LOAD_AND: |
Michael Liao | 8372539 | 2012-09-19 19:36:58 +0000 | [diff] [blame] | 2301 | case ISD::ATOMIC_LOAD_OR: |
| 2302 | case ISD::ATOMIC_LOAD_ADD: { |
Sanjay Patel | 85030aa | 2015-10-13 16:23:00 +0000 | [diff] [blame^] | 2303 | SDNode *RetVal = selectAtomicLoadArith(Node, NVT); |
Eric Christopher | 4a34e61 | 2011-05-10 23:57:45 +0000 | [diff] [blame] | 2304 | if (RetVal) |
| 2305 | return RetVal; |
| 2306 | break; |
| 2307 | } |
Tobias Grosser | 85508e8 | 2015-08-19 11:35:10 +0000 | [diff] [blame] | 2308 | case ISD::AND: |
Benjamin Kramer | 4c81624 | 2011-04-22 15:30:40 +0000 | [diff] [blame] | 2309 | case ISD::OR: |
| 2310 | case ISD::XOR: { |
| 2311 | // For operations of the form (x << C1) op C2, check if we can use a smaller |
| 2312 | // encoding for C2 by transforming it into (x op (C2>>C1)) << C1. |
| 2313 | SDValue N0 = Node->getOperand(0); |
| 2314 | SDValue N1 = Node->getOperand(1); |
| 2315 | |
| 2316 | if (N0->getOpcode() != ISD::SHL || !N0->hasOneUse()) |
| 2317 | break; |
| 2318 | |
| 2319 | // i8 is unshrinkable, i16 should be promoted to i32. |
| 2320 | if (NVT != MVT::i32 && NVT != MVT::i64) |
| 2321 | break; |
| 2322 | |
| 2323 | ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(N1); |
| 2324 | ConstantSDNode *ShlCst = dyn_cast<ConstantSDNode>(N0->getOperand(1)); |
| 2325 | if (!Cst || !ShlCst) |
| 2326 | break; |
| 2327 | |
| 2328 | int64_t Val = Cst->getSExtValue(); |
| 2329 | uint64_t ShlVal = ShlCst->getZExtValue(); |
| 2330 | |
| 2331 | // Make sure that we don't change the operation by removing bits. |
| 2332 | // This only matters for OR and XOR, AND is unaffected. |
Richard Smith | 228e6d4 | 2012-08-24 23:29:28 +0000 | [diff] [blame] | 2333 | uint64_t RemovedBitsMask = (1ULL << ShlVal) - 1; |
| 2334 | if (Opcode != ISD::AND && (Val & RemovedBitsMask) != 0) |
Benjamin Kramer | 4c81624 | 2011-04-22 15:30:40 +0000 | [diff] [blame] | 2335 | break; |
| 2336 | |
Benjamin Kramer | 3a16a36 | 2015-04-01 19:01:09 +0000 | [diff] [blame] | 2337 | unsigned ShlOp, AddOp, Op; |
Craig Topper | 83e042a | 2013-08-15 05:57:07 +0000 | [diff] [blame] | 2338 | MVT CstVT = NVT; |
Benjamin Kramer | 4c81624 | 2011-04-22 15:30:40 +0000 | [diff] [blame] | 2339 | |
| 2340 | // Check the minimum bitwidth for the new constant. |
| 2341 | // TODO: AND32ri is the same as AND64ri32 with zext imm. |
| 2342 | // TODO: MOV32ri+OR64r is cheaper than MOV64ri64+OR64rr |
| 2343 | // TODO: Using 16 and 8 bit operations is also possible for or32 & xor32. |
| 2344 | if (!isInt<8>(Val) && isInt<8>(Val >> ShlVal)) |
| 2345 | CstVT = MVT::i8; |
| 2346 | else if (!isInt<32>(Val) && isInt<32>(Val >> ShlVal)) |
| 2347 | CstVT = MVT::i32; |
| 2348 | |
| 2349 | // Bail if there is no smaller encoding. |
| 2350 | if (NVT == CstVT) |
| 2351 | break; |
| 2352 | |
Craig Topper | 83e042a | 2013-08-15 05:57:07 +0000 | [diff] [blame] | 2353 | switch (NVT.SimpleTy) { |
Benjamin Kramer | 4c81624 | 2011-04-22 15:30:40 +0000 | [diff] [blame] | 2354 | default: llvm_unreachable("Unsupported VT!"); |
| 2355 | case MVT::i32: |
| 2356 | assert(CstVT == MVT::i8); |
| 2357 | ShlOp = X86::SHL32ri; |
Benjamin Kramer | 3a16a36 | 2015-04-01 19:01:09 +0000 | [diff] [blame] | 2358 | AddOp = X86::ADD32rr; |
Benjamin Kramer | 4c81624 | 2011-04-22 15:30:40 +0000 | [diff] [blame] | 2359 | |
| 2360 | switch (Opcode) { |
Craig Topper | 22cb0c5 | 2012-08-11 17:44:14 +0000 | [diff] [blame] | 2361 | default: llvm_unreachable("Impossible opcode"); |
Benjamin Kramer | 4c81624 | 2011-04-22 15:30:40 +0000 | [diff] [blame] | 2362 | case ISD::AND: Op = X86::AND32ri8; break; |
| 2363 | case ISD::OR: Op = X86::OR32ri8; break; |
| 2364 | case ISD::XOR: Op = X86::XOR32ri8; break; |
| 2365 | } |
| 2366 | break; |
| 2367 | case MVT::i64: |
| 2368 | assert(CstVT == MVT::i8 || CstVT == MVT::i32); |
| 2369 | ShlOp = X86::SHL64ri; |
Benjamin Kramer | 3a16a36 | 2015-04-01 19:01:09 +0000 | [diff] [blame] | 2370 | AddOp = X86::ADD64rr; |
Benjamin Kramer | 4c81624 | 2011-04-22 15:30:40 +0000 | [diff] [blame] | 2371 | |
| 2372 | switch (Opcode) { |
Craig Topper | 22cb0c5 | 2012-08-11 17:44:14 +0000 | [diff] [blame] | 2373 | default: llvm_unreachable("Impossible opcode"); |
Benjamin Kramer | 4c81624 | 2011-04-22 15:30:40 +0000 | [diff] [blame] | 2374 | case ISD::AND: Op = CstVT==MVT::i8? X86::AND64ri8 : X86::AND64ri32; break; |
| 2375 | case ISD::OR: Op = CstVT==MVT::i8? X86::OR64ri8 : X86::OR64ri32; break; |
| 2376 | case ISD::XOR: Op = CstVT==MVT::i8? X86::XOR64ri8 : X86::XOR64ri32; break; |
| 2377 | } |
| 2378 | break; |
| 2379 | } |
| 2380 | |
| 2381 | // Emit the smaller op and the shift. |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2382 | SDValue NewCst = CurDAG->getTargetConstant(Val >> ShlVal, dl, CstVT); |
Benjamin Kramer | 4c81624 | 2011-04-22 15:30:40 +0000 | [diff] [blame] | 2383 | SDNode *New = CurDAG->getMachineNode(Op, dl, NVT, N0->getOperand(0),NewCst); |
Benjamin Kramer | 3a16a36 | 2015-04-01 19:01:09 +0000 | [diff] [blame] | 2384 | if (ShlVal == 1) |
| 2385 | return CurDAG->SelectNodeTo(Node, AddOp, NVT, SDValue(New, 0), |
| 2386 | SDValue(New, 0)); |
Benjamin Kramer | 4c81624 | 2011-04-22 15:30:40 +0000 | [diff] [blame] | 2387 | return CurDAG->SelectNodeTo(Node, ShlOp, NVT, SDValue(New, 0), |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2388 | getI8Imm(ShlVal, dl)); |
Benjamin Kramer | 4c81624 | 2011-04-22 15:30:40 +0000 | [diff] [blame] | 2389 | } |
Ahmed Bougacha | 5175bcf | 2014-10-23 21:55:31 +0000 | [diff] [blame] | 2390 | case X86ISD::UMUL8: |
| 2391 | case X86ISD::SMUL8: { |
| 2392 | SDValue N0 = Node->getOperand(0); |
| 2393 | SDValue N1 = Node->getOperand(1); |
| 2394 | |
| 2395 | Opc = (Opcode == X86ISD::SMUL8 ? X86::IMUL8r : X86::MUL8r); |
| 2396 | |
| 2397 | SDValue InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, X86::AL, |
| 2398 | N0, SDValue()).getValue(1); |
| 2399 | |
| 2400 | SDVTList VTs = CurDAG->getVTList(NVT, MVT::i32); |
| 2401 | SDValue Ops[] = {N1, InFlag}; |
| 2402 | SDNode *CNode = CurDAG->getMachineNode(Opc, dl, VTs, Ops); |
| 2403 | |
| 2404 | ReplaceUses(SDValue(Node, 0), SDValue(CNode, 0)); |
| 2405 | ReplaceUses(SDValue(Node, 1), SDValue(CNode, 1)); |
| 2406 | return nullptr; |
| 2407 | } |
| 2408 | |
Chris Lattner | 364bb0a | 2010-12-05 07:30:36 +0000 | [diff] [blame] | 2409 | case X86ISD::UMUL: { |
| 2410 | SDValue N0 = Node->getOperand(0); |
| 2411 | SDValue N1 = Node->getOperand(1); |
Chad Rosier | 24c19d2 | 2012-08-01 18:39:17 +0000 | [diff] [blame] | 2412 | |
Ted Kremenek | b5241b2 | 2011-01-14 22:34:13 +0000 | [diff] [blame] | 2413 | unsigned LoReg; |
Craig Topper | 83e042a | 2013-08-15 05:57:07 +0000 | [diff] [blame] | 2414 | switch (NVT.SimpleTy) { |
Chris Lattner | 364bb0a | 2010-12-05 07:30:36 +0000 | [diff] [blame] | 2415 | default: llvm_unreachable("Unsupported VT!"); |
Ted Kremenek | b5241b2 | 2011-01-14 22:34:13 +0000 | [diff] [blame] | 2416 | case MVT::i8: LoReg = X86::AL; Opc = X86::MUL8r; break; |
| 2417 | case MVT::i16: LoReg = X86::AX; Opc = X86::MUL16r; break; |
| 2418 | case MVT::i32: LoReg = X86::EAX; Opc = X86::MUL32r; break; |
| 2419 | case MVT::i64: LoReg = X86::RAX; Opc = X86::MUL64r; break; |
Chris Lattner | 364bb0a | 2010-12-05 07:30:36 +0000 | [diff] [blame] | 2420 | } |
Chad Rosier | 24c19d2 | 2012-08-01 18:39:17 +0000 | [diff] [blame] | 2421 | |
Chris Lattner | 364bb0a | 2010-12-05 07:30:36 +0000 | [diff] [blame] | 2422 | SDValue InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, LoReg, |
| 2423 | N0, SDValue()).getValue(1); |
Chad Rosier | 24c19d2 | 2012-08-01 18:39:17 +0000 | [diff] [blame] | 2424 | |
Chris Lattner | 364bb0a | 2010-12-05 07:30:36 +0000 | [diff] [blame] | 2425 | SDVTList VTs = CurDAG->getVTList(NVT, NVT, MVT::i32); |
| 2426 | SDValue Ops[] = {N1, InFlag}; |
Michael Liao | b53d896 | 2013-04-19 22:22:57 +0000 | [diff] [blame] | 2427 | SDNode *CNode = CurDAG->getMachineNode(Opc, dl, VTs, Ops); |
Chad Rosier | 24c19d2 | 2012-08-01 18:39:17 +0000 | [diff] [blame] | 2428 | |
Chris Lattner | 364bb0a | 2010-12-05 07:30:36 +0000 | [diff] [blame] | 2429 | ReplaceUses(SDValue(Node, 0), SDValue(CNode, 0)); |
| 2430 | ReplaceUses(SDValue(Node, 1), SDValue(CNode, 1)); |
| 2431 | ReplaceUses(SDValue(Node, 2), SDValue(CNode, 2)); |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 2432 | return nullptr; |
Chris Lattner | 364bb0a | 2010-12-05 07:30:36 +0000 | [diff] [blame] | 2433 | } |
Chad Rosier | 24c19d2 | 2012-08-01 18:39:17 +0000 | [diff] [blame] | 2434 | |
Dan Gohman | 757eee8 | 2009-08-02 16:10:52 +0000 | [diff] [blame] | 2435 | case ISD::SMUL_LOHI: |
| 2436 | case ISD::UMUL_LOHI: { |
| 2437 | SDValue N0 = Node->getOperand(0); |
| 2438 | SDValue N1 = Node->getOperand(1); |
| 2439 | |
| 2440 | bool isSigned = Opcode == ISD::SMUL_LOHI; |
Michael Liao | f9f7b55 | 2012-09-26 08:22:37 +0000 | [diff] [blame] | 2441 | bool hasBMI2 = Subtarget->hasBMI2(); |
Bill Wendling | fe3bdb4 | 2009-08-07 21:33:25 +0000 | [diff] [blame] | 2442 | if (!isSigned) { |
Craig Topper | 83e042a | 2013-08-15 05:57:07 +0000 | [diff] [blame] | 2443 | switch (NVT.SimpleTy) { |
Dan Gohman | 757eee8 | 2009-08-02 16:10:52 +0000 | [diff] [blame] | 2444 | default: llvm_unreachable("Unsupported VT!"); |
Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2445 | case MVT::i8: Opc = X86::MUL8r; MOpc = X86::MUL8m; break; |
| 2446 | case MVT::i16: Opc = X86::MUL16r; MOpc = X86::MUL16m; break; |
Michael Liao | f9f7b55 | 2012-09-26 08:22:37 +0000 | [diff] [blame] | 2447 | case MVT::i32: Opc = hasBMI2 ? X86::MULX32rr : X86::MUL32r; |
| 2448 | MOpc = hasBMI2 ? X86::MULX32rm : X86::MUL32m; break; |
| 2449 | case MVT::i64: Opc = hasBMI2 ? X86::MULX64rr : X86::MUL64r; |
| 2450 | MOpc = hasBMI2 ? X86::MULX64rm : X86::MUL64m; break; |
Dan Gohman | 757eee8 | 2009-08-02 16:10:52 +0000 | [diff] [blame] | 2451 | } |
Bill Wendling | fe3bdb4 | 2009-08-07 21:33:25 +0000 | [diff] [blame] | 2452 | } else { |
Craig Topper | 83e042a | 2013-08-15 05:57:07 +0000 | [diff] [blame] | 2453 | switch (NVT.SimpleTy) { |
Dan Gohman | 757eee8 | 2009-08-02 16:10:52 +0000 | [diff] [blame] | 2454 | default: llvm_unreachable("Unsupported VT!"); |
Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2455 | case MVT::i8: Opc = X86::IMUL8r; MOpc = X86::IMUL8m; break; |
| 2456 | case MVT::i16: Opc = X86::IMUL16r; MOpc = X86::IMUL16m; break; |
| 2457 | case MVT::i32: Opc = X86::IMUL32r; MOpc = X86::IMUL32m; break; |
| 2458 | case MVT::i64: Opc = X86::IMUL64r; MOpc = X86::IMUL64m; break; |
Dan Gohman | 757eee8 | 2009-08-02 16:10:52 +0000 | [diff] [blame] | 2459 | } |
Bill Wendling | fe3bdb4 | 2009-08-07 21:33:25 +0000 | [diff] [blame] | 2460 | } |
Dan Gohman | 757eee8 | 2009-08-02 16:10:52 +0000 | [diff] [blame] | 2461 | |
Michael Liao | f9f7b55 | 2012-09-26 08:22:37 +0000 | [diff] [blame] | 2462 | unsigned SrcReg, LoReg, HiReg; |
| 2463 | switch (Opc) { |
| 2464 | default: llvm_unreachable("Unknown MUL opcode!"); |
| 2465 | case X86::IMUL8r: |
| 2466 | case X86::MUL8r: |
| 2467 | SrcReg = LoReg = X86::AL; HiReg = X86::AH; |
| 2468 | break; |
| 2469 | case X86::IMUL16r: |
| 2470 | case X86::MUL16r: |
| 2471 | SrcReg = LoReg = X86::AX; HiReg = X86::DX; |
| 2472 | break; |
| 2473 | case X86::IMUL32r: |
| 2474 | case X86::MUL32r: |
| 2475 | SrcReg = LoReg = X86::EAX; HiReg = X86::EDX; |
| 2476 | break; |
| 2477 | case X86::IMUL64r: |
| 2478 | case X86::MUL64r: |
| 2479 | SrcReg = LoReg = X86::RAX; HiReg = X86::RDX; |
| 2480 | break; |
| 2481 | case X86::MULX32rr: |
| 2482 | SrcReg = X86::EDX; LoReg = HiReg = 0; |
| 2483 | break; |
| 2484 | case X86::MULX64rr: |
| 2485 | SrcReg = X86::RDX; LoReg = HiReg = 0; |
| 2486 | break; |
Dan Gohman | 757eee8 | 2009-08-02 16:10:52 +0000 | [diff] [blame] | 2487 | } |
| 2488 | |
| 2489 | SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4; |
Sanjay Patel | 85030aa | 2015-10-13 16:23:00 +0000 | [diff] [blame^] | 2490 | bool foldedLoad = tryFoldLoad(Node, N1, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4); |
Bill Wendling | fe3bdb4 | 2009-08-07 21:33:25 +0000 | [diff] [blame] | 2491 | // Multiply is commmutative. |
Dan Gohman | 757eee8 | 2009-08-02 16:10:52 +0000 | [diff] [blame] | 2492 | if (!foldedLoad) { |
Sanjay Patel | 85030aa | 2015-10-13 16:23:00 +0000 | [diff] [blame^] | 2493 | foldedLoad = tryFoldLoad(Node, N0, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4); |
Dan Gohman | 757eee8 | 2009-08-02 16:10:52 +0000 | [diff] [blame] | 2494 | if (foldedLoad) |
| 2495 | std::swap(N0, N1); |
| 2496 | } |
| 2497 | |
Michael Liao | f9f7b55 | 2012-09-26 08:22:37 +0000 | [diff] [blame] | 2498 | SDValue InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, SrcReg, |
Craig Topper | a4fd6d6 | 2012-05-23 05:44:51 +0000 | [diff] [blame] | 2499 | N0, SDValue()).getValue(1); |
Michael Liao | f9f7b55 | 2012-09-26 08:22:37 +0000 | [diff] [blame] | 2500 | SDValue ResHi, ResLo; |
Dan Gohman | 757eee8 | 2009-08-02 16:10:52 +0000 | [diff] [blame] | 2501 | |
| 2502 | if (foldedLoad) { |
Michael Liao | f9f7b55 | 2012-09-26 08:22:37 +0000 | [diff] [blame] | 2503 | SDValue Chain; |
Dan Gohman | 757eee8 | 2009-08-02 16:10:52 +0000 | [diff] [blame] | 2504 | SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, N1.getOperand(0), |
| 2505 | InFlag }; |
Michael Liao | f9f7b55 | 2012-09-26 08:22:37 +0000 | [diff] [blame] | 2506 | if (MOpc == X86::MULX32rm || MOpc == X86::MULX64rm) { |
| 2507 | SDVTList VTs = CurDAG->getVTList(NVT, NVT, MVT::Other, MVT::Glue); |
Michael Liao | b53d896 | 2013-04-19 22:22:57 +0000 | [diff] [blame] | 2508 | SDNode *CNode = CurDAG->getMachineNode(MOpc, dl, VTs, Ops); |
Michael Liao | f9f7b55 | 2012-09-26 08:22:37 +0000 | [diff] [blame] | 2509 | ResHi = SDValue(CNode, 0); |
| 2510 | ResLo = SDValue(CNode, 1); |
| 2511 | Chain = SDValue(CNode, 2); |
| 2512 | InFlag = SDValue(CNode, 3); |
| 2513 | } else { |
| 2514 | SDVTList VTs = CurDAG->getVTList(MVT::Other, MVT::Glue); |
Michael Liao | b53d896 | 2013-04-19 22:22:57 +0000 | [diff] [blame] | 2515 | SDNode *CNode = CurDAG->getMachineNode(MOpc, dl, VTs, Ops); |
Michael Liao | f9f7b55 | 2012-09-26 08:22:37 +0000 | [diff] [blame] | 2516 | Chain = SDValue(CNode, 0); |
| 2517 | InFlag = SDValue(CNode, 1); |
| 2518 | } |
Chris Lattner | 364bb0a | 2010-12-05 07:30:36 +0000 | [diff] [blame] | 2519 | |
Dan Gohman | 757eee8 | 2009-08-02 16:10:52 +0000 | [diff] [blame] | 2520 | // Update the chain. |
Michael Liao | f9f7b55 | 2012-09-26 08:22:37 +0000 | [diff] [blame] | 2521 | ReplaceUses(N1.getValue(1), Chain); |
Dan Gohman | 757eee8 | 2009-08-02 16:10:52 +0000 | [diff] [blame] | 2522 | } else { |
Michael Liao | f9f7b55 | 2012-09-26 08:22:37 +0000 | [diff] [blame] | 2523 | SDValue Ops[] = { N1, InFlag }; |
| 2524 | if (Opc == X86::MULX32rr || Opc == X86::MULX64rr) { |
| 2525 | SDVTList VTs = CurDAG->getVTList(NVT, NVT, MVT::Glue); |
Michael Liao | b53d896 | 2013-04-19 22:22:57 +0000 | [diff] [blame] | 2526 | SDNode *CNode = CurDAG->getMachineNode(Opc, dl, VTs, Ops); |
Michael Liao | f9f7b55 | 2012-09-26 08:22:37 +0000 | [diff] [blame] | 2527 | ResHi = SDValue(CNode, 0); |
| 2528 | ResLo = SDValue(CNode, 1); |
| 2529 | InFlag = SDValue(CNode, 2); |
| 2530 | } else { |
| 2531 | SDVTList VTs = CurDAG->getVTList(MVT::Glue); |
Michael Liao | b53d896 | 2013-04-19 22:22:57 +0000 | [diff] [blame] | 2532 | SDNode *CNode = CurDAG->getMachineNode(Opc, dl, VTs, Ops); |
Michael Liao | f9f7b55 | 2012-09-26 08:22:37 +0000 | [diff] [blame] | 2533 | InFlag = SDValue(CNode, 0); |
| 2534 | } |
Dan Gohman | 757eee8 | 2009-08-02 16:10:52 +0000 | [diff] [blame] | 2535 | } |
| 2536 | |
Jakob Stoklund Olesen | d7d0d4e | 2010-06-26 00:39:23 +0000 | [diff] [blame] | 2537 | // Prevent use of AH in a REX instruction by referencing AX instead. |
| 2538 | if (HiReg == X86::AH && Subtarget->is64Bit() && |
| 2539 | !SDValue(Node, 1).use_empty()) { |
| 2540 | SDValue Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl, |
| 2541 | X86::AX, MVT::i16, InFlag); |
| 2542 | InFlag = Result.getValue(2); |
| 2543 | // Get the low part if needed. Don't use getCopyFromReg for aliasing |
| 2544 | // registers. |
| 2545 | if (!SDValue(Node, 0).use_empty()) |
| 2546 | ReplaceUses(SDValue(Node, 1), |
| 2547 | CurDAG->getTargetExtractSubreg(X86::sub_8bit, dl, MVT::i8, Result)); |
| 2548 | |
| 2549 | // Shift AX down 8 bits. |
| 2550 | Result = SDValue(CurDAG->getMachineNode(X86::SHR16ri, dl, MVT::i16, |
| 2551 | Result, |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2552 | CurDAG->getTargetConstant(8, dl, MVT::i8)), |
| 2553 | 0); |
Jakob Stoklund Olesen | d7d0d4e | 2010-06-26 00:39:23 +0000 | [diff] [blame] | 2554 | // Then truncate it down to i8. |
| 2555 | ReplaceUses(SDValue(Node, 1), |
| 2556 | CurDAG->getTargetExtractSubreg(X86::sub_8bit, dl, MVT::i8, Result)); |
| 2557 | } |
Dan Gohman | 757eee8 | 2009-08-02 16:10:52 +0000 | [diff] [blame] | 2558 | // Copy the low half of the result, if it is needed. |
Dan Gohman | ea6f91f | 2010-01-05 01:24:18 +0000 | [diff] [blame] | 2559 | if (!SDValue(Node, 0).use_empty()) { |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 2560 | if (!ResLo.getNode()) { |
Michael Liao | f9f7b55 | 2012-09-26 08:22:37 +0000 | [diff] [blame] | 2561 | assert(LoReg && "Register for low half is not defined!"); |
| 2562 | ResLo = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl, LoReg, NVT, |
| 2563 | InFlag); |
| 2564 | InFlag = ResLo.getValue(2); |
| 2565 | } |
| 2566 | ReplaceUses(SDValue(Node, 0), ResLo); |
| 2567 | DEBUG(dbgs() << "=> "; ResLo.getNode()->dump(CurDAG); dbgs() << '\n'); |
Dan Gohman | 757eee8 | 2009-08-02 16:10:52 +0000 | [diff] [blame] | 2568 | } |
| 2569 | // Copy the high half of the result, if it is needed. |
Dan Gohman | ea6f91f | 2010-01-05 01:24:18 +0000 | [diff] [blame] | 2570 | if (!SDValue(Node, 1).use_empty()) { |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 2571 | if (!ResHi.getNode()) { |
Michael Liao | f9f7b55 | 2012-09-26 08:22:37 +0000 | [diff] [blame] | 2572 | assert(HiReg && "Register for high half is not defined!"); |
| 2573 | ResHi = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl, HiReg, NVT, |
| 2574 | InFlag); |
| 2575 | InFlag = ResHi.getValue(2); |
| 2576 | } |
| 2577 | ReplaceUses(SDValue(Node, 1), ResHi); |
| 2578 | DEBUG(dbgs() << "=> "; ResHi.getNode()->dump(CurDAG); dbgs() << '\n'); |
Dan Gohman | 757eee8 | 2009-08-02 16:10:52 +0000 | [diff] [blame] | 2579 | } |
Chad Rosier | 24c19d2 | 2012-08-01 18:39:17 +0000 | [diff] [blame] | 2580 | |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 2581 | return nullptr; |
Dan Gohman | 757eee8 | 2009-08-02 16:10:52 +0000 | [diff] [blame] | 2582 | } |
| 2583 | |
| 2584 | case ISD::SDIVREM: |
Ahmed Bougacha | 12eb558 | 2014-11-03 20:26:35 +0000 | [diff] [blame] | 2585 | case ISD::UDIVREM: |
| 2586 | case X86ISD::SDIVREM8_SEXT_HREG: |
| 2587 | case X86ISD::UDIVREM8_ZEXT_HREG: { |
Dan Gohman | 757eee8 | 2009-08-02 16:10:52 +0000 | [diff] [blame] | 2588 | SDValue N0 = Node->getOperand(0); |
| 2589 | SDValue N1 = Node->getOperand(1); |
| 2590 | |
Ahmed Bougacha | 12eb558 | 2014-11-03 20:26:35 +0000 | [diff] [blame] | 2591 | bool isSigned = (Opcode == ISD::SDIVREM || |
| 2592 | Opcode == X86ISD::SDIVREM8_SEXT_HREG); |
Bill Wendling | fe3bdb4 | 2009-08-07 21:33:25 +0000 | [diff] [blame] | 2593 | if (!isSigned) { |
Craig Topper | 83e042a | 2013-08-15 05:57:07 +0000 | [diff] [blame] | 2594 | switch (NVT.SimpleTy) { |
Dan Gohman | 757eee8 | 2009-08-02 16:10:52 +0000 | [diff] [blame] | 2595 | default: llvm_unreachable("Unsupported VT!"); |
Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2596 | case MVT::i8: Opc = X86::DIV8r; MOpc = X86::DIV8m; break; |
| 2597 | case MVT::i16: Opc = X86::DIV16r; MOpc = X86::DIV16m; break; |
| 2598 | case MVT::i32: Opc = X86::DIV32r; MOpc = X86::DIV32m; break; |
| 2599 | case MVT::i64: Opc = X86::DIV64r; MOpc = X86::DIV64m; break; |
Dan Gohman | 757eee8 | 2009-08-02 16:10:52 +0000 | [diff] [blame] | 2600 | } |
Bill Wendling | fe3bdb4 | 2009-08-07 21:33:25 +0000 | [diff] [blame] | 2601 | } else { |
Craig Topper | 83e042a | 2013-08-15 05:57:07 +0000 | [diff] [blame] | 2602 | switch (NVT.SimpleTy) { |
Dan Gohman | 757eee8 | 2009-08-02 16:10:52 +0000 | [diff] [blame] | 2603 | default: llvm_unreachable("Unsupported VT!"); |
Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2604 | case MVT::i8: Opc = X86::IDIV8r; MOpc = X86::IDIV8m; break; |
| 2605 | case MVT::i16: Opc = X86::IDIV16r; MOpc = X86::IDIV16m; break; |
| 2606 | case MVT::i32: Opc = X86::IDIV32r; MOpc = X86::IDIV32m; break; |
| 2607 | case MVT::i64: Opc = X86::IDIV64r; MOpc = X86::IDIV64m; break; |
Dan Gohman | 757eee8 | 2009-08-02 16:10:52 +0000 | [diff] [blame] | 2608 | } |
Bill Wendling | fe3bdb4 | 2009-08-07 21:33:25 +0000 | [diff] [blame] | 2609 | } |
Dan Gohman | 757eee8 | 2009-08-02 16:10:52 +0000 | [diff] [blame] | 2610 | |
Chris Lattner | 518b037 | 2009-12-23 01:45:04 +0000 | [diff] [blame] | 2611 | unsigned LoReg, HiReg, ClrReg; |
Tim Northover | 64ec0ff | 2013-05-30 13:19:42 +0000 | [diff] [blame] | 2612 | unsigned SExtOpcode; |
Craig Topper | 83e042a | 2013-08-15 05:57:07 +0000 | [diff] [blame] | 2613 | switch (NVT.SimpleTy) { |
Dan Gohman | 757eee8 | 2009-08-02 16:10:52 +0000 | [diff] [blame] | 2614 | default: llvm_unreachable("Unsupported VT!"); |
Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2615 | case MVT::i8: |
Chris Lattner | 518b037 | 2009-12-23 01:45:04 +0000 | [diff] [blame] | 2616 | LoReg = X86::AL; ClrReg = HiReg = X86::AH; |
Dan Gohman | 757eee8 | 2009-08-02 16:10:52 +0000 | [diff] [blame] | 2617 | SExtOpcode = X86::CBW; |
| 2618 | break; |
Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2619 | case MVT::i16: |
Dan Gohman | 757eee8 | 2009-08-02 16:10:52 +0000 | [diff] [blame] | 2620 | LoReg = X86::AX; HiReg = X86::DX; |
Tim Northover | 64ec0ff | 2013-05-30 13:19:42 +0000 | [diff] [blame] | 2621 | ClrReg = X86::DX; |
Dan Gohman | 757eee8 | 2009-08-02 16:10:52 +0000 | [diff] [blame] | 2622 | SExtOpcode = X86::CWD; |
| 2623 | break; |
Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2624 | case MVT::i32: |
Chris Lattner | 518b037 | 2009-12-23 01:45:04 +0000 | [diff] [blame] | 2625 | LoReg = X86::EAX; ClrReg = HiReg = X86::EDX; |
Dan Gohman | 757eee8 | 2009-08-02 16:10:52 +0000 | [diff] [blame] | 2626 | SExtOpcode = X86::CDQ; |
| 2627 | break; |
Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2628 | case MVT::i64: |
Chris Lattner | 518b037 | 2009-12-23 01:45:04 +0000 | [diff] [blame] | 2629 | LoReg = X86::RAX; ClrReg = HiReg = X86::RDX; |
Dan Gohman | 757eee8 | 2009-08-02 16:10:52 +0000 | [diff] [blame] | 2630 | SExtOpcode = X86::CQO; |
Evan Cheng | e62288f | 2009-07-30 08:33:02 +0000 | [diff] [blame] | 2631 | break; |
| 2632 | } |
| 2633 | |
Dan Gohman | 757eee8 | 2009-08-02 16:10:52 +0000 | [diff] [blame] | 2634 | SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4; |
Sanjay Patel | 85030aa | 2015-10-13 16:23:00 +0000 | [diff] [blame^] | 2635 | bool foldedLoad = tryFoldLoad(Node, N1, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4); |
Dan Gohman | 757eee8 | 2009-08-02 16:10:52 +0000 | [diff] [blame] | 2636 | bool signBitIsZero = CurDAG->SignBitIsZero(N0); |
Dan Gohman | a160361 | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 2637 | |
Dan Gohman | 757eee8 | 2009-08-02 16:10:52 +0000 | [diff] [blame] | 2638 | SDValue InFlag; |
Owen Anderson | 9f94459 | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 2639 | if (NVT == MVT::i8 && (!isSigned || signBitIsZero)) { |
Dan Gohman | 757eee8 | 2009-08-02 16:10:52 +0000 | [diff] [blame] | 2640 | // Special case for div8, just use a move with zero extension to AX to |
| 2641 | // clear the upper 8 bits (AH). |
| 2642 | SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, Move, Chain; |
Sanjay Patel | 85030aa | 2015-10-13 16:23:00 +0000 | [diff] [blame^] | 2643 | if (tryFoldLoad(Node, N0, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4)) { |
Dan Gohman | 757eee8 | 2009-08-02 16:10:52 +0000 | [diff] [blame] | 2644 | SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, N0.getOperand(0) }; |
| 2645 | Move = |
Stuart Hastings | 91f1d24 | 2011-05-20 19:04:40 +0000 | [diff] [blame] | 2646 | SDValue(CurDAG->getMachineNode(X86::MOVZX32rm8, dl, MVT::i32, |
Michael Liao | b53d896 | 2013-04-19 22:22:57 +0000 | [diff] [blame] | 2647 | MVT::Other, Ops), 0); |
Dan Gohman | 757eee8 | 2009-08-02 16:10:52 +0000 | [diff] [blame] | 2648 | Chain = Move.getValue(1); |
| 2649 | ReplaceUses(N0.getValue(1), Chain); |
Evan Cheng | 10d2790 | 2006-01-06 20:36:21 +0000 | [diff] [blame] | 2650 | } else { |
Dan Gohman | 757eee8 | 2009-08-02 16:10:52 +0000 | [diff] [blame] | 2651 | Move = |
Stuart Hastings | 91f1d24 | 2011-05-20 19:04:40 +0000 | [diff] [blame] | 2652 | SDValue(CurDAG->getMachineNode(X86::MOVZX32rr8, dl, MVT::i32, N0),0); |
Dan Gohman | 757eee8 | 2009-08-02 16:10:52 +0000 | [diff] [blame] | 2653 | Chain = CurDAG->getEntryNode(); |
| 2654 | } |
Stuart Hastings | 91f1d24 | 2011-05-20 19:04:40 +0000 | [diff] [blame] | 2655 | Chain = CurDAG->getCopyToReg(Chain, dl, X86::EAX, Move, SDValue()); |
Dan Gohman | 757eee8 | 2009-08-02 16:10:52 +0000 | [diff] [blame] | 2656 | InFlag = Chain.getValue(1); |
| 2657 | } else { |
| 2658 | InFlag = |
| 2659 | CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, |
| 2660 | LoReg, N0, SDValue()).getValue(1); |
| 2661 | if (isSigned && !signBitIsZero) { |
| 2662 | // Sign extend the low part into the high part. |
Evan Cheng | d1b82d8 | 2006-02-09 07:17:49 +0000 | [diff] [blame] | 2663 | InFlag = |
Chris Lattner | 3e5fbd7 | 2010-12-21 02:38:05 +0000 | [diff] [blame] | 2664 | SDValue(CurDAG->getMachineNode(SExtOpcode, dl, MVT::Glue, InFlag),0); |
Dan Gohman | 757eee8 | 2009-08-02 16:10:52 +0000 | [diff] [blame] | 2665 | } else { |
| 2666 | // Zero out the high part, effectively zero extending the input. |
Michael Liao | 5bf9578 | 2014-12-04 05:20:33 +0000 | [diff] [blame] | 2667 | SDValue ClrNode = SDValue(CurDAG->getMachineNode(X86::MOV32r0, dl, NVT), 0); |
Craig Topper | 83e042a | 2013-08-15 05:57:07 +0000 | [diff] [blame] | 2668 | switch (NVT.SimpleTy) { |
Tim Northover | 64ec0ff | 2013-05-30 13:19:42 +0000 | [diff] [blame] | 2669 | case MVT::i16: |
| 2670 | ClrNode = |
| 2671 | SDValue(CurDAG->getMachineNode( |
| 2672 | TargetOpcode::EXTRACT_SUBREG, dl, MVT::i16, ClrNode, |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2673 | CurDAG->getTargetConstant(X86::sub_16bit, dl, |
| 2674 | MVT::i32)), |
Tim Northover | 64ec0ff | 2013-05-30 13:19:42 +0000 | [diff] [blame] | 2675 | 0); |
| 2676 | break; |
| 2677 | case MVT::i32: |
| 2678 | break; |
| 2679 | case MVT::i64: |
| 2680 | ClrNode = |
| 2681 | SDValue(CurDAG->getMachineNode( |
| 2682 | TargetOpcode::SUBREG_TO_REG, dl, MVT::i64, |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2683 | CurDAG->getTargetConstant(0, dl, MVT::i64), ClrNode, |
| 2684 | CurDAG->getTargetConstant(X86::sub_32bit, dl, |
| 2685 | MVT::i32)), |
Tim Northover | 64ec0ff | 2013-05-30 13:19:42 +0000 | [diff] [blame] | 2686 | 0); |
| 2687 | break; |
| 2688 | default: |
| 2689 | llvm_unreachable("Unexpected division source"); |
| 2690 | } |
| 2691 | |
Chris Lattner | 518b037 | 2009-12-23 01:45:04 +0000 | [diff] [blame] | 2692 | InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, ClrReg, |
Dan Gohman | 757eee8 | 2009-08-02 16:10:52 +0000 | [diff] [blame] | 2693 | ClrNode, InFlag).getValue(1); |
Dan Gohman | a160361 | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 2694 | } |
Evan Cheng | 92e2797 | 2006-01-06 23:19:29 +0000 | [diff] [blame] | 2695 | } |
Dan Gohman | a160361 | 2007-10-08 18:33:35 +0000 | [diff] [blame] | 2696 | |
Dan Gohman | 757eee8 | 2009-08-02 16:10:52 +0000 | [diff] [blame] | 2697 | if (foldedLoad) { |
| 2698 | SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, N1.getOperand(0), |
| 2699 | InFlag }; |
| 2700 | SDNode *CNode = |
Michael Liao | b53d896 | 2013-04-19 22:22:57 +0000 | [diff] [blame] | 2701 | CurDAG->getMachineNode(MOpc, dl, MVT::Other, MVT::Glue, Ops); |
Dan Gohman | 757eee8 | 2009-08-02 16:10:52 +0000 | [diff] [blame] | 2702 | InFlag = SDValue(CNode, 1); |
| 2703 | // Update the chain. |
| 2704 | ReplaceUses(N1.getValue(1), SDValue(CNode, 0)); |
| 2705 | } else { |
| 2706 | InFlag = |
Chris Lattner | 3e5fbd7 | 2010-12-21 02:38:05 +0000 | [diff] [blame] | 2707 | SDValue(CurDAG->getMachineNode(Opc, dl, MVT::Glue, N1, InFlag), 0); |
Dan Gohman | 757eee8 | 2009-08-02 16:10:52 +0000 | [diff] [blame] | 2708 | } |
Evan Cheng | 92e2797 | 2006-01-06 23:19:29 +0000 | [diff] [blame] | 2709 | |
Ahmed Bougacha | 12eb558 | 2014-11-03 20:26:35 +0000 | [diff] [blame] | 2710 | // Prevent use of AH in a REX instruction by explicitly copying it to |
| 2711 | // an ABCD_L register. |
Jim Grosbach | 340b6da | 2013-07-09 02:07:28 +0000 | [diff] [blame] | 2712 | // |
| 2713 | // The current assumption of the register allocator is that isel |
Ahmed Bougacha | 12eb558 | 2014-11-03 20:26:35 +0000 | [diff] [blame] | 2714 | // won't generate explicit references to the GR8_ABCD_H registers. If |
Jim Grosbach | 340b6da | 2013-07-09 02:07:28 +0000 | [diff] [blame] | 2715 | // the allocator and/or the backend get enhanced to be more robust in |
| 2716 | // that regard, this can be, and should be, removed. |
Ahmed Bougacha | 12eb558 | 2014-11-03 20:26:35 +0000 | [diff] [blame] | 2717 | if (HiReg == X86::AH && !SDValue(Node, 1).use_empty()) { |
| 2718 | SDValue AHCopy = CurDAG->getRegister(X86::AH, MVT::i8); |
| 2719 | unsigned AHExtOpcode = |
| 2720 | isSigned ? X86::MOVSX32_NOREXrr8 : X86::MOVZX32_NOREXrr8; |
Jakob Stoklund Olesen | d7d0d4e | 2010-06-26 00:39:23 +0000 | [diff] [blame] | 2721 | |
Ahmed Bougacha | 12eb558 | 2014-11-03 20:26:35 +0000 | [diff] [blame] | 2722 | SDNode *RNode = CurDAG->getMachineNode(AHExtOpcode, dl, MVT::i32, |
| 2723 | MVT::Glue, AHCopy, InFlag); |
| 2724 | SDValue Result(RNode, 0); |
| 2725 | InFlag = SDValue(RNode, 1); |
Jakob Stoklund Olesen | d7d0d4e | 2010-06-26 00:39:23 +0000 | [diff] [blame] | 2726 | |
Ahmed Bougacha | 12eb558 | 2014-11-03 20:26:35 +0000 | [diff] [blame] | 2727 | if (Opcode == X86ISD::UDIVREM8_ZEXT_HREG || |
| 2728 | Opcode == X86ISD::SDIVREM8_SEXT_HREG) { |
| 2729 | if (Node->getValueType(1) == MVT::i64) { |
| 2730 | // It's not possible to directly movsx AH to a 64bit register, because |
| 2731 | // the latter needs the REX prefix, but the former can't have it. |
| 2732 | assert(Opcode != X86ISD::SDIVREM8_SEXT_HREG && |
| 2733 | "Unexpected i64 sext of h-register"); |
| 2734 | Result = |
| 2735 | SDValue(CurDAG->getMachineNode( |
| 2736 | TargetOpcode::SUBREG_TO_REG, dl, MVT::i64, |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2737 | CurDAG->getTargetConstant(0, dl, MVT::i64), Result, |
| 2738 | CurDAG->getTargetConstant(X86::sub_32bit, dl, |
| 2739 | MVT::i32)), |
Ahmed Bougacha | 12eb558 | 2014-11-03 20:26:35 +0000 | [diff] [blame] | 2740 | 0); |
| 2741 | } |
| 2742 | } else { |
| 2743 | Result = |
| 2744 | CurDAG->getTargetExtractSubreg(X86::sub_8bit, dl, MVT::i8, Result); |
| 2745 | } |
| 2746 | ReplaceUses(SDValue(Node, 1), Result); |
| 2747 | DEBUG(dbgs() << "=> "; Result.getNode()->dump(CurDAG); dbgs() << '\n'); |
Jakob Stoklund Olesen | d7d0d4e | 2010-06-26 00:39:23 +0000 | [diff] [blame] | 2748 | } |
Dan Gohman | 757eee8 | 2009-08-02 16:10:52 +0000 | [diff] [blame] | 2749 | // Copy the division (low) result, if it is needed. |
Dan Gohman | ea6f91f | 2010-01-05 01:24:18 +0000 | [diff] [blame] | 2750 | if (!SDValue(Node, 0).use_empty()) { |
Dan Gohman | 757eee8 | 2009-08-02 16:10:52 +0000 | [diff] [blame] | 2751 | SDValue Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl, |
| 2752 | LoReg, NVT, InFlag); |
| 2753 | InFlag = Result.getValue(2); |
Dan Gohman | ea6f91f | 2010-01-05 01:24:18 +0000 | [diff] [blame] | 2754 | ReplaceUses(SDValue(Node, 0), Result); |
Chris Lattner | f98f124 | 2010-03-02 06:34:30 +0000 | [diff] [blame] | 2755 | DEBUG(dbgs() << "=> "; Result.getNode()->dump(CurDAG); dbgs() << '\n'); |
Dan Gohman | 757eee8 | 2009-08-02 16:10:52 +0000 | [diff] [blame] | 2756 | } |
| 2757 | // Copy the remainder (high) result, if it is needed. |
Dan Gohman | ea6f91f | 2010-01-05 01:24:18 +0000 | [diff] [blame] | 2758 | if (!SDValue(Node, 1).use_empty()) { |
Jakob Stoklund Olesen | d7d0d4e | 2010-06-26 00:39:23 +0000 | [diff] [blame] | 2759 | SDValue Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl, |
| 2760 | HiReg, NVT, InFlag); |
| 2761 | InFlag = Result.getValue(2); |
Dan Gohman | ea6f91f | 2010-01-05 01:24:18 +0000 | [diff] [blame] | 2762 | ReplaceUses(SDValue(Node, 1), Result); |
Chris Lattner | f98f124 | 2010-03-02 06:34:30 +0000 | [diff] [blame] | 2763 | DEBUG(dbgs() << "=> "; Result.getNode()->dump(CurDAG); dbgs() << '\n'); |
Dan Gohman | 757eee8 | 2009-08-02 16:10:52 +0000 | [diff] [blame] | 2764 | } |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 2765 | return nullptr; |
Dan Gohman | 757eee8 | 2009-08-02 16:10:52 +0000 | [diff] [blame] | 2766 | } |
| 2767 | |
Manman Ren | 1be131b | 2012-08-08 00:51:41 +0000 | [diff] [blame] | 2768 | case X86ISD::CMP: |
| 2769 | case X86ISD::SUB: { |
| 2770 | // Sometimes a SUB is used to perform comparison. |
| 2771 | if (Opcode == X86ISD::SUB && Node->hasAnyUseOfValue(0)) |
| 2772 | // This node is not a CMP. |
| 2773 | break; |
Dan Gohman | ac33a90 | 2009-08-19 18:16:17 +0000 | [diff] [blame] | 2774 | SDValue N0 = Node->getOperand(0); |
| 2775 | SDValue N1 = Node->getOperand(1); |
| 2776 | |
Elena Demikhovsky | 34d2d76 | 2014-08-18 11:59:06 +0000 | [diff] [blame] | 2777 | if (N0.getOpcode() == ISD::TRUNCATE && N0.hasOneUse() && |
Sanjay Patel | 85030aa | 2015-10-13 16:23:00 +0000 | [diff] [blame^] | 2778 | hasNoSignedComparisonUses(Node)) |
Elena Demikhovsky | 34d2d76 | 2014-08-18 11:59:06 +0000 | [diff] [blame] | 2779 | N0 = N0.getOperand(0); |
Elena Demikhovsky | d2cb3c8 | 2015-02-12 08:40:34 +0000 | [diff] [blame] | 2780 | |
Dan Gohman | ac33a90 | 2009-08-19 18:16:17 +0000 | [diff] [blame] | 2781 | // Look for (X86cmp (and $op, $imm), 0) and see if we can convert it to |
| 2782 | // use a smaller encoding. |
Elena Demikhovsky | 34d2d76 | 2014-08-18 11:59:06 +0000 | [diff] [blame] | 2783 | // Look past the truncate if CMP is the only use of it. |
Dan Gohman | 198b7ff | 2011-11-03 21:49:52 +0000 | [diff] [blame] | 2784 | if ((N0.getNode()->getOpcode() == ISD::AND || |
| 2785 | (N0.getResNo() == 0 && N0.getNode()->getOpcode() == X86ISD::AND)) && |
| 2786 | N0.getNode()->hasOneUse() && |
Dan Gohman | ac33a90 | 2009-08-19 18:16:17 +0000 | [diff] [blame] | 2787 | N0.getValueType() != MVT::i8 && |
| 2788 | X86::isZeroNode(N1)) { |
| 2789 | ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getNode()->getOperand(1)); |
| 2790 | if (!C) break; |
| 2791 | |
| 2792 | // For example, convert "testl %eax, $8" to "testb %al, $8" |
Dan Gohman | 7d9dffb | 2009-10-09 20:35:19 +0000 | [diff] [blame] | 2793 | if ((C->getZExtValue() & ~UINT64_C(0xff)) == 0 && |
| 2794 | (!(C->getZExtValue() & 0x80) || |
Sanjay Patel | 85030aa | 2015-10-13 16:23:00 +0000 | [diff] [blame^] | 2795 | hasNoSignedComparisonUses(Node))) { |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2796 | SDValue Imm = CurDAG->getTargetConstant(C->getZExtValue(), dl, MVT::i8); |
Dan Gohman | ac33a90 | 2009-08-19 18:16:17 +0000 | [diff] [blame] | 2797 | SDValue Reg = N0.getNode()->getOperand(0); |
| 2798 | |
| 2799 | // On x86-32, only the ABCD registers have 8-bit subregisters. |
| 2800 | if (!Subtarget->is64Bit()) { |
Craig Topper | cc830f8 | 2012-02-22 07:28:11 +0000 | [diff] [blame] | 2801 | const TargetRegisterClass *TRC; |
Craig Topper | 5671010 | 2013-08-15 02:33:50 +0000 | [diff] [blame] | 2802 | switch (N0.getSimpleValueType().SimpleTy) { |
Dan Gohman | ac33a90 | 2009-08-19 18:16:17 +0000 | [diff] [blame] | 2803 | case MVT::i32: TRC = &X86::GR32_ABCDRegClass; break; |
| 2804 | case MVT::i16: TRC = &X86::GR16_ABCDRegClass; break; |
| 2805 | default: llvm_unreachable("Unsupported TEST operand type!"); |
| 2806 | } |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2807 | SDValue RC = CurDAG->getTargetConstant(TRC->getID(), dl, MVT::i32); |
Dan Gohman | 32f71d7 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 2808 | Reg = SDValue(CurDAG->getMachineNode(X86::COPY_TO_REGCLASS, dl, |
| 2809 | Reg.getValueType(), Reg, RC), 0); |
Dan Gohman | ac33a90 | 2009-08-19 18:16:17 +0000 | [diff] [blame] | 2810 | } |
| 2811 | |
| 2812 | // Extract the l-register. |
Jakob Stoklund Olesen | 9340ea5 | 2010-05-24 14:48:17 +0000 | [diff] [blame] | 2813 | SDValue Subreg = CurDAG->getTargetExtractSubreg(X86::sub_8bit, dl, |
Dan Gohman | ac33a90 | 2009-08-19 18:16:17 +0000 | [diff] [blame] | 2814 | MVT::i8, Reg); |
| 2815 | |
| 2816 | // Emit a testb. |
Manman Ren | 511c6d0 | 2012-09-28 18:53:24 +0000 | [diff] [blame] | 2817 | SDNode *NewNode = CurDAG->getMachineNode(X86::TEST8ri, dl, MVT::i32, |
| 2818 | Subreg, Imm); |
| 2819 | // Replace SUB|CMP with TEST, since SUB has two outputs while TEST has |
| 2820 | // one, do not call ReplaceAllUsesWith. |
| 2821 | ReplaceUses(SDValue(Node, (Opcode == X86ISD::SUB ? 1 : 0)), |
| 2822 | SDValue(NewNode, 0)); |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 2823 | return nullptr; |
Dan Gohman | ac33a90 | 2009-08-19 18:16:17 +0000 | [diff] [blame] | 2824 | } |
| 2825 | |
| 2826 | // For example, "testl %eax, $2048" to "testb %ah, $8". |
Dan Gohman | 7d9dffb | 2009-10-09 20:35:19 +0000 | [diff] [blame] | 2827 | if ((C->getZExtValue() & ~UINT64_C(0xff00)) == 0 && |
| 2828 | (!(C->getZExtValue() & 0x8000) || |
Sanjay Patel | 85030aa | 2015-10-13 16:23:00 +0000 | [diff] [blame^] | 2829 | hasNoSignedComparisonUses(Node))) { |
Dan Gohman | ac33a90 | 2009-08-19 18:16:17 +0000 | [diff] [blame] | 2830 | // Shift the immediate right by 8 bits. |
| 2831 | SDValue ShiftedImm = CurDAG->getTargetConstant(C->getZExtValue() >> 8, |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2832 | dl, MVT::i8); |
Dan Gohman | ac33a90 | 2009-08-19 18:16:17 +0000 | [diff] [blame] | 2833 | SDValue Reg = N0.getNode()->getOperand(0); |
| 2834 | |
| 2835 | // Put the value in an ABCD register. |
Craig Topper | cc830f8 | 2012-02-22 07:28:11 +0000 | [diff] [blame] | 2836 | const TargetRegisterClass *TRC; |
Craig Topper | 5671010 | 2013-08-15 02:33:50 +0000 | [diff] [blame] | 2837 | switch (N0.getSimpleValueType().SimpleTy) { |
Dan Gohman | ac33a90 | 2009-08-19 18:16:17 +0000 | [diff] [blame] | 2838 | case MVT::i64: TRC = &X86::GR64_ABCDRegClass; break; |
| 2839 | case MVT::i32: TRC = &X86::GR32_ABCDRegClass; break; |
| 2840 | case MVT::i16: TRC = &X86::GR16_ABCDRegClass; break; |
| 2841 | default: llvm_unreachable("Unsupported TEST operand type!"); |
| 2842 | } |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2843 | SDValue RC = CurDAG->getTargetConstant(TRC->getID(), dl, MVT::i32); |
Dan Gohman | 32f71d7 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 2844 | Reg = SDValue(CurDAG->getMachineNode(X86::COPY_TO_REGCLASS, dl, |
| 2845 | Reg.getValueType(), Reg, RC), 0); |
Dan Gohman | ac33a90 | 2009-08-19 18:16:17 +0000 | [diff] [blame] | 2846 | |
| 2847 | // Extract the h-register. |
Jakob Stoklund Olesen | 9340ea5 | 2010-05-24 14:48:17 +0000 | [diff] [blame] | 2848 | SDValue Subreg = CurDAG->getTargetExtractSubreg(X86::sub_8bit_hi, dl, |
Dan Gohman | ac33a90 | 2009-08-19 18:16:17 +0000 | [diff] [blame] | 2849 | MVT::i8, Reg); |
| 2850 | |
Jakob Stoklund Olesen | 729abd3 | 2011-10-08 18:28:28 +0000 | [diff] [blame] | 2851 | // Emit a testb. The EXTRACT_SUBREG becomes a COPY that can only |
| 2852 | // target GR8_NOREX registers, so make sure the register class is |
| 2853 | // forced. |
Manman Ren | 511c6d0 | 2012-09-28 18:53:24 +0000 | [diff] [blame] | 2854 | SDNode *NewNode = CurDAG->getMachineNode(X86::TEST8ri_NOREX, dl, |
| 2855 | MVT::i32, Subreg, ShiftedImm); |
| 2856 | // Replace SUB|CMP with TEST, since SUB has two outputs while TEST has |
| 2857 | // one, do not call ReplaceAllUsesWith. |
| 2858 | ReplaceUses(SDValue(Node, (Opcode == X86ISD::SUB ? 1 : 0)), |
| 2859 | SDValue(NewNode, 0)); |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 2860 | return nullptr; |
Dan Gohman | ac33a90 | 2009-08-19 18:16:17 +0000 | [diff] [blame] | 2861 | } |
| 2862 | |
| 2863 | // For example, "testl %eax, $32776" to "testw %ax, $32776". |
| 2864 | if ((C->getZExtValue() & ~UINT64_C(0xffff)) == 0 && |
Dan Gohman | 7d9dffb | 2009-10-09 20:35:19 +0000 | [diff] [blame] | 2865 | N0.getValueType() != MVT::i16 && |
| 2866 | (!(C->getZExtValue() & 0x8000) || |
Sanjay Patel | 85030aa | 2015-10-13 16:23:00 +0000 | [diff] [blame^] | 2867 | hasNoSignedComparisonUses(Node))) { |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2868 | SDValue Imm = CurDAG->getTargetConstant(C->getZExtValue(), dl, |
| 2869 | MVT::i16); |
Dan Gohman | ac33a90 | 2009-08-19 18:16:17 +0000 | [diff] [blame] | 2870 | SDValue Reg = N0.getNode()->getOperand(0); |
| 2871 | |
| 2872 | // Extract the 16-bit subregister. |
Jakob Stoklund Olesen | 9340ea5 | 2010-05-24 14:48:17 +0000 | [diff] [blame] | 2873 | SDValue Subreg = CurDAG->getTargetExtractSubreg(X86::sub_16bit, dl, |
Dan Gohman | ac33a90 | 2009-08-19 18:16:17 +0000 | [diff] [blame] | 2874 | MVT::i16, Reg); |
| 2875 | |
| 2876 | // Emit a testw. |
Manman Ren | 511c6d0 | 2012-09-28 18:53:24 +0000 | [diff] [blame] | 2877 | SDNode *NewNode = CurDAG->getMachineNode(X86::TEST16ri, dl, MVT::i32, |
| 2878 | Subreg, Imm); |
| 2879 | // Replace SUB|CMP with TEST, since SUB has two outputs while TEST has |
| 2880 | // one, do not call ReplaceAllUsesWith. |
| 2881 | ReplaceUses(SDValue(Node, (Opcode == X86ISD::SUB ? 1 : 0)), |
| 2882 | SDValue(NewNode, 0)); |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 2883 | return nullptr; |
Dan Gohman | ac33a90 | 2009-08-19 18:16:17 +0000 | [diff] [blame] | 2884 | } |
| 2885 | |
| 2886 | // For example, "testq %rax, $268468232" to "testl %eax, $268468232". |
| 2887 | if ((C->getZExtValue() & ~UINT64_C(0xffffffff)) == 0 && |
Dan Gohman | 7d9dffb | 2009-10-09 20:35:19 +0000 | [diff] [blame] | 2888 | N0.getValueType() == MVT::i64 && |
| 2889 | (!(C->getZExtValue() & 0x80000000) || |
Sanjay Patel | 85030aa | 2015-10-13 16:23:00 +0000 | [diff] [blame^] | 2890 | hasNoSignedComparisonUses(Node))) { |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 2891 | SDValue Imm = CurDAG->getTargetConstant(C->getZExtValue(), dl, |
| 2892 | MVT::i32); |
Dan Gohman | ac33a90 | 2009-08-19 18:16:17 +0000 | [diff] [blame] | 2893 | SDValue Reg = N0.getNode()->getOperand(0); |
| 2894 | |
| 2895 | // Extract the 32-bit subregister. |
Jakob Stoklund Olesen | 9340ea5 | 2010-05-24 14:48:17 +0000 | [diff] [blame] | 2896 | SDValue Subreg = CurDAG->getTargetExtractSubreg(X86::sub_32bit, dl, |
Dan Gohman | ac33a90 | 2009-08-19 18:16:17 +0000 | [diff] [blame] | 2897 | MVT::i32, Reg); |
| 2898 | |
| 2899 | // Emit a testl. |
Manman Ren | 511c6d0 | 2012-09-28 18:53:24 +0000 | [diff] [blame] | 2900 | SDNode *NewNode = CurDAG->getMachineNode(X86::TEST32ri, dl, MVT::i32, |
| 2901 | Subreg, Imm); |
| 2902 | // Replace SUB|CMP with TEST, since SUB has two outputs while TEST has |
| 2903 | // one, do not call ReplaceAllUsesWith. |
| 2904 | ReplaceUses(SDValue(Node, (Opcode == X86ISD::SUB ? 1 : 0)), |
| 2905 | SDValue(NewNode, 0)); |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 2906 | return nullptr; |
Dan Gohman | ac33a90 | 2009-08-19 18:16:17 +0000 | [diff] [blame] | 2907 | } |
| 2908 | } |
| 2909 | break; |
| 2910 | } |
Pete Cooper | 7c7ba1b | 2011-11-15 21:57:53 +0000 | [diff] [blame] | 2911 | case ISD::STORE: { |
Joel Jones | 68d59e8 | 2012-03-29 05:45:48 +0000 | [diff] [blame] | 2912 | // Change a chain of {load; incr or dec; store} of the same value into |
| 2913 | // a simple increment or decrement through memory of that value, if the |
| 2914 | // uses of the modified value and its address are suitable. |
Pete Cooper | 48784ed | 2011-11-16 19:03:23 +0000 | [diff] [blame] | 2915 | // The DEC64m tablegen pattern is currently not able to match the case where |
Chad Rosier | 24c19d2 | 2012-08-01 18:39:17 +0000 | [diff] [blame] | 2916 | // the EFLAGS on the original DEC are used. (This also applies to |
Joel Jones | 68d59e8 | 2012-03-29 05:45:48 +0000 | [diff] [blame] | 2917 | // {INC,DEC}X{64,32,16,8}.) |
| 2918 | // We'll need to improve tablegen to allow flags to be transferred from a |
Pete Cooper | 48784ed | 2011-11-16 19:03:23 +0000 | [diff] [blame] | 2919 | // node in the pattern to the result node. probably with a new keyword |
| 2920 | // for example, we have this |
| 2921 | // def DEC64m : RI<0xFF, MRM1m, (outs), (ins i64mem:$dst), "dec{q}\t$dst", |
| 2922 | // [(store (add (loadi64 addr:$dst), -1), addr:$dst), |
| 2923 | // (implicit EFLAGS)]>; |
| 2924 | // but maybe need something like this |
| 2925 | // def DEC64m : RI<0xFF, MRM1m, (outs), (ins i64mem:$dst), "dec{q}\t$dst", |
| 2926 | // [(store (add (loadi64 addr:$dst), -1), addr:$dst), |
| 2927 | // (transferrable EFLAGS)]>; |
Joel Jones | 68d59e8 | 2012-03-29 05:45:48 +0000 | [diff] [blame] | 2928 | |
Pete Cooper | 7c7ba1b | 2011-11-15 21:57:53 +0000 | [diff] [blame] | 2929 | StoreSDNode *StoreNode = cast<StoreSDNode>(Node); |
Pete Cooper | 7c7ba1b | 2011-11-15 21:57:53 +0000 | [diff] [blame] | 2930 | SDValue StoredVal = StoreNode->getOperand(1); |
Joel Jones | 68d59e8 | 2012-03-29 05:45:48 +0000 | [diff] [blame] | 2931 | unsigned Opc = StoredVal->getOpcode(); |
Pete Cooper | 7c7ba1b | 2011-11-15 21:57:53 +0000 | [diff] [blame] | 2932 | |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 2933 | LoadSDNode *LoadNode = nullptr; |
Evan Cheng | 3e869f0 | 2012-04-12 19:14:21 +0000 | [diff] [blame] | 2934 | SDValue InputChain; |
| 2935 | if (!isLoadIncOrDecStore(StoreNode, Opc, StoredVal, CurDAG, |
| 2936 | LoadNode, InputChain)) |
| 2937 | break; |
Pete Cooper | 7c7ba1b | 2011-11-15 21:57:53 +0000 | [diff] [blame] | 2938 | |
| 2939 | SDValue Base, Scale, Index, Disp, Segment; |
Sanjay Patel | 85030aa | 2015-10-13 16:23:00 +0000 | [diff] [blame^] | 2940 | if (!selectAddr(LoadNode, LoadNode->getBasePtr(), |
Pete Cooper | 7c7ba1b | 2011-11-15 21:57:53 +0000 | [diff] [blame] | 2941 | Base, Scale, Index, Disp, Segment)) |
| 2942 | break; |
| 2943 | |
| 2944 | MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(2); |
| 2945 | MemOp[0] = StoreNode->getMemOperand(); |
| 2946 | MemOp[1] = LoadNode->getMemOperand(); |
| 2947 | const SDValue Ops[] = { Base, Scale, Index, Disp, Segment, InputChain }; |
Chad Rosier | 24c19d2 | 2012-08-01 18:39:17 +0000 | [diff] [blame] | 2948 | EVT LdVT = LoadNode->getMemoryVT(); |
Joel Jones | 68d59e8 | 2012-03-29 05:45:48 +0000 | [diff] [blame] | 2949 | unsigned newOpc = getFusedLdStOpcode(LdVT, Opc); |
| 2950 | MachineSDNode *Result = CurDAG->getMachineNode(newOpc, |
Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 2951 | SDLoc(Node), |
Michael Liao | b53d896 | 2013-04-19 22:22:57 +0000 | [diff] [blame] | 2952 | MVT::i32, MVT::Other, Ops); |
Pete Cooper | 7c7ba1b | 2011-11-15 21:57:53 +0000 | [diff] [blame] | 2953 | Result->setMemRefs(MemOp, MemOp + 2); |
| 2954 | |
| 2955 | ReplaceUses(SDValue(StoreNode, 0), SDValue(Result, 1)); |
| 2956 | ReplaceUses(SDValue(StoredVal.getNode(), 1), SDValue(Result, 0)); |
| 2957 | |
| 2958 | return Result; |
| 2959 | } |
Chris Lattner | 655e7df | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 2960 | } |
| 2961 | |
Dan Gohman | ea6f91f | 2010-01-05 01:24:18 +0000 | [diff] [blame] | 2962 | SDNode *ResNode = SelectCode(Node); |
Evan Cheng | bd1c5a8 | 2006-08-11 09:08:15 +0000 | [diff] [blame] | 2963 | |
Chris Lattner | f98f124 | 2010-03-02 06:34:30 +0000 | [diff] [blame] | 2964 | DEBUG(dbgs() << "=> "; |
Craig Topper | e73658d | 2014-04-28 04:05:08 +0000 | [diff] [blame] | 2965 | if (ResNode == nullptr || ResNode == Node) |
Chris Lattner | f98f124 | 2010-03-02 06:34:30 +0000 | [diff] [blame] | 2966 | Node->dump(CurDAG); |
| 2967 | else |
| 2968 | ResNode->dump(CurDAG); |
| 2969 | dbgs() << '\n'); |
Evan Cheng | bd1c5a8 | 2006-08-11 09:08:15 +0000 | [diff] [blame] | 2970 | |
| 2971 | return ResNode; |
Chris Lattner | 655e7df | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 2972 | } |
| 2973 | |
Chris Lattner | ba1ed58 | 2006-06-08 18:03:49 +0000 | [diff] [blame] | 2974 | bool X86DAGToDAGISel:: |
Daniel Sanders | 60f1db0 | 2015-03-13 12:45:09 +0000 | [diff] [blame] | 2975 | SelectInlineAsmMemoryOperand(const SDValue &Op, unsigned ConstraintID, |
Dan Gohman | eb0cee9 | 2008-08-23 02:25:05 +0000 | [diff] [blame] | 2976 | std::vector<SDValue> &OutOps) { |
Rafael Espindola | 3b2df10 | 2009-04-08 21:14:34 +0000 | [diff] [blame] | 2977 | SDValue Op0, Op1, Op2, Op3, Op4; |
Daniel Sanders | 60f1db0 | 2015-03-13 12:45:09 +0000 | [diff] [blame] | 2978 | switch (ConstraintID) { |
Daniel Sanders | d049669 | 2015-05-16 12:09:54 +0000 | [diff] [blame] | 2979 | default: |
| 2980 | llvm_unreachable("Unexpected asm memory constraint"); |
| 2981 | case InlineAsm::Constraint_i: |
| 2982 | // FIXME: It seems strange that 'i' is needed here since it's supposed to |
| 2983 | // be an immediate and not a memory constraint. |
| 2984 | // Fallthrough. |
Daniel Sanders | 60f1db0 | 2015-03-13 12:45:09 +0000 | [diff] [blame] | 2985 | case InlineAsm::Constraint_o: // offsetable ?? |
| 2986 | case InlineAsm::Constraint_v: // not offsetable ?? |
Daniel Sanders | 60f1db0 | 2015-03-13 12:45:09 +0000 | [diff] [blame] | 2987 | case InlineAsm::Constraint_m: // memory |
Daniel Sanders | d049669 | 2015-05-16 12:09:54 +0000 | [diff] [blame] | 2988 | case InlineAsm::Constraint_X: |
Sanjay Patel | 85030aa | 2015-10-13 16:23:00 +0000 | [diff] [blame^] | 2989 | if (!selectAddr(nullptr, Op, Op0, Op1, Op2, Op3, Op4)) |
Chris Lattner | ba1ed58 | 2006-06-08 18:03:49 +0000 | [diff] [blame] | 2990 | return true; |
| 2991 | break; |
| 2992 | } |
Chad Rosier | 24c19d2 | 2012-08-01 18:39:17 +0000 | [diff] [blame] | 2993 | |
Evan Cheng | 2d48722 | 2006-08-26 01:05:16 +0000 | [diff] [blame] | 2994 | OutOps.push_back(Op0); |
| 2995 | OutOps.push_back(Op1); |
| 2996 | OutOps.push_back(Op2); |
| 2997 | OutOps.push_back(Op3); |
Rafael Espindola | 3b2df10 | 2009-04-08 21:14:34 +0000 | [diff] [blame] | 2998 | OutOps.push_back(Op4); |
Chris Lattner | ba1ed58 | 2006-06-08 18:03:49 +0000 | [diff] [blame] | 2999 | return false; |
| 3000 | } |
| 3001 | |
Sanjay Patel | b5723d0 | 2015-10-13 15:12:27 +0000 | [diff] [blame] | 3002 | /// This pass converts a legalized DAG into a X86-specific DAG, |
| 3003 | /// ready for instruction scheduling. |
Bill Wendling | 026e5d7 | 2009-04-29 23:29:43 +0000 | [diff] [blame] | 3004 | FunctionPass *llvm::createX86ISelDag(X86TargetMachine &TM, |
Craig Topper | f6e7e12 | 2012-03-27 07:21:54 +0000 | [diff] [blame] | 3005 | CodeGenOpt::Level OptLevel) { |
Bill Wendling | 084669a | 2009-04-29 00:15:41 +0000 | [diff] [blame] | 3006 | return new X86DAGToDAGISel(TM, OptLevel); |
Chris Lattner | 655e7df | 2005-11-16 01:54:32 +0000 | [diff] [blame] | 3007 | } |