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