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