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