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