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