Anton Korobeynikov | 4403b93 | 2009-07-16 13:27:25 +0000 | [diff] [blame] | 1 | //==-- SystemZISelDAGToDAG.cpp - A dag to dag inst selector for SystemZ ---===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file defines an instruction selector for the SystemZ target. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "SystemZ.h" |
Anton Korobeynikov | 4403b93 | 2009-07-16 13:27:25 +0000 | [diff] [blame] | 15 | #include "SystemZTargetMachine.h" |
| 16 | #include "llvm/DerivedTypes.h" |
| 17 | #include "llvm/Function.h" |
| 18 | #include "llvm/Intrinsics.h" |
| 19 | #include "llvm/CallingConv.h" |
| 20 | #include "llvm/Constants.h" |
| 21 | #include "llvm/CodeGen/MachineFrameInfo.h" |
| 22 | #include "llvm/CodeGen/MachineFunction.h" |
| 23 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| 24 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
| 25 | #include "llvm/CodeGen/SelectionDAG.h" |
| 26 | #include "llvm/CodeGen/SelectionDAGISel.h" |
| 27 | #include "llvm/Target/TargetLowering.h" |
| 28 | #include "llvm/Support/Compiler.h" |
| 29 | #include "llvm/Support/Debug.h" |
Anton Korobeynikov | 7df8462 | 2009-07-16 14:36:52 +0000 | [diff] [blame] | 30 | #include "llvm/Support/raw_ostream.h" |
Anton Korobeynikov | 4403b93 | 2009-07-16 13:27:25 +0000 | [diff] [blame] | 31 | using namespace llvm; |
| 32 | |
Anton Korobeynikov | 8bd0db7 | 2009-07-16 14:18:17 +0000 | [diff] [blame] | 33 | static const unsigned subreg_even32 = 1; |
| 34 | static const unsigned subreg_odd32 = 2; |
| 35 | static const unsigned subreg_even = 3; |
| 36 | static const unsigned subreg_odd = 4; |
Anton Korobeynikov | 0a42d2b | 2009-07-16 14:14:33 +0000 | [diff] [blame] | 37 | |
Anton Korobeynikov | 3360da9 | 2009-07-16 13:44:00 +0000 | [diff] [blame] | 38 | namespace { |
| 39 | /// SystemZRRIAddressMode - This corresponds to rriaddr, but uses SDValue's |
| 40 | /// instead of register numbers for the leaves of the matched tree. |
| 41 | struct SystemZRRIAddressMode { |
| 42 | enum { |
| 43 | RegBase, |
| 44 | FrameIndexBase |
| 45 | } BaseType; |
| 46 | |
| 47 | struct { // This is really a union, discriminated by BaseType! |
| 48 | SDValue Reg; |
| 49 | int FrameIndex; |
| 50 | } Base; |
| 51 | |
| 52 | SDValue IndexReg; |
Anton Korobeynikov | 3240740 | 2009-07-16 13:48:23 +0000 | [diff] [blame] | 53 | int64_t Disp; |
Anton Korobeynikov | 1ed1e3e | 2009-07-16 14:10:17 +0000 | [diff] [blame] | 54 | bool isRI; |
Anton Korobeynikov | 3360da9 | 2009-07-16 13:44:00 +0000 | [diff] [blame] | 55 | |
Anton Korobeynikov | 1ed1e3e | 2009-07-16 14:10:17 +0000 | [diff] [blame] | 56 | SystemZRRIAddressMode(bool RI = false) |
| 57 | : BaseType(RegBase), IndexReg(), Disp(0), isRI(RI) { |
Anton Korobeynikov | 3360da9 | 2009-07-16 13:44:00 +0000 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | void dump() { |
Chris Lattner | 4437ae2 | 2009-08-23 07:05:07 +0000 | [diff] [blame] | 61 | errs() << "SystemZRRIAddressMode " << this << '\n'; |
Anton Korobeynikov | 3360da9 | 2009-07-16 13:44:00 +0000 | [diff] [blame] | 62 | if (BaseType == RegBase) { |
Chris Lattner | 4437ae2 | 2009-08-23 07:05:07 +0000 | [diff] [blame] | 63 | errs() << "Base.Reg "; |
| 64 | if (Base.Reg.getNode() != 0) |
| 65 | Base.Reg.getNode()->dump(); |
| 66 | else |
| 67 | errs() << "nul"; |
| 68 | errs() << '\n'; |
Anton Korobeynikov | 3360da9 | 2009-07-16 13:44:00 +0000 | [diff] [blame] | 69 | } else { |
Chris Lattner | 4437ae2 | 2009-08-23 07:05:07 +0000 | [diff] [blame] | 70 | errs() << " Base.FrameIndex " << Base.FrameIndex << '\n'; |
Anton Korobeynikov | 3360da9 | 2009-07-16 13:44:00 +0000 | [diff] [blame] | 71 | } |
Anton Korobeynikov | 1ed1e3e | 2009-07-16 14:10:17 +0000 | [diff] [blame] | 72 | if (!isRI) { |
Chris Lattner | 4437ae2 | 2009-08-23 07:05:07 +0000 | [diff] [blame] | 73 | errs() << "IndexReg "; |
Anton Korobeynikov | 1ed1e3e | 2009-07-16 14:10:17 +0000 | [diff] [blame] | 74 | if (IndexReg.getNode() != 0) IndexReg.getNode()->dump(); |
Chris Lattner | 4437ae2 | 2009-08-23 07:05:07 +0000 | [diff] [blame] | 75 | else errs() << "nul"; |
Anton Korobeynikov | 1ed1e3e | 2009-07-16 14:10:17 +0000 | [diff] [blame] | 76 | } |
Chris Lattner | 4437ae2 | 2009-08-23 07:05:07 +0000 | [diff] [blame] | 77 | errs() << " Disp " << Disp << '\n'; |
Anton Korobeynikov | 3360da9 | 2009-07-16 13:44:00 +0000 | [diff] [blame] | 78 | } |
| 79 | }; |
| 80 | } |
| 81 | |
Anton Korobeynikov | 4403b93 | 2009-07-16 13:27:25 +0000 | [diff] [blame] | 82 | /// SystemZDAGToDAGISel - SystemZ specific code to select SystemZ machine |
| 83 | /// instructions for SelectionDAG operations. |
| 84 | /// |
| 85 | namespace { |
| 86 | class SystemZDAGToDAGISel : public SelectionDAGISel { |
Dan Gohman | d858e90 | 2010-04-17 15:26:15 +0000 | [diff] [blame^] | 87 | const SystemZTargetLowering &Lowering; |
Anton Korobeynikov | 4403b93 | 2009-07-16 13:27:25 +0000 | [diff] [blame] | 88 | const SystemZSubtarget &Subtarget; |
| 89 | |
Anton Korobeynikov | 1ed1e3e | 2009-07-16 14:10:17 +0000 | [diff] [blame] | 90 | void getAddressOperandsRI(const SystemZRRIAddressMode &AM, |
| 91 | SDValue &Base, SDValue &Disp); |
Anton Korobeynikov | 720e3b0 | 2009-07-16 14:09:35 +0000 | [diff] [blame] | 92 | void getAddressOperands(const SystemZRRIAddressMode &AM, |
| 93 | SDValue &Base, SDValue &Disp, |
| 94 | SDValue &Index); |
| 95 | |
Anton Korobeynikov | 4403b93 | 2009-07-16 13:27:25 +0000 | [diff] [blame] | 96 | public: |
| 97 | SystemZDAGToDAGISel(SystemZTargetMachine &TM, CodeGenOpt::Level OptLevel) |
| 98 | : SelectionDAGISel(TM, OptLevel), |
| 99 | Lowering(*TM.getTargetLowering()), |
| 100 | Subtarget(*TM.getSubtargetImpl()) { } |
| 101 | |
Anton Korobeynikov | 4403b93 | 2009-07-16 13:27:25 +0000 | [diff] [blame] | 102 | virtual const char *getPassName() const { |
| 103 | return "SystemZ DAG->DAG Pattern Instruction Selection"; |
| 104 | } |
| 105 | |
Anton Korobeynikov | b6831cb | 2009-07-16 14:26:38 +0000 | [diff] [blame] | 106 | /// getI8Imm - Return a target constant with the specified value, of type |
| 107 | /// i8. |
| 108 | inline SDValue getI8Imm(uint64_t Imm) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 109 | return CurDAG->getTargetConstant(Imm, MVT::i8); |
Anton Korobeynikov | b6831cb | 2009-07-16 14:26:38 +0000 | [diff] [blame] | 110 | } |
| 111 | |
Anton Korobeynikov | 89edcd0 | 2009-07-16 13:33:57 +0000 | [diff] [blame] | 112 | /// getI16Imm - Return a target constant with the specified value, of type |
| 113 | /// i16. |
| 114 | inline SDValue getI16Imm(uint64_t Imm) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 115 | return CurDAG->getTargetConstant(Imm, MVT::i16); |
Anton Korobeynikov | 89edcd0 | 2009-07-16 13:33:57 +0000 | [diff] [blame] | 116 | } |
| 117 | |
Anton Korobeynikov | da308c9 | 2009-07-16 13:34:50 +0000 | [diff] [blame] | 118 | /// getI32Imm - Return a target constant with the specified value, of type |
| 119 | /// i32. |
| 120 | inline SDValue getI32Imm(uint64_t Imm) { |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 121 | return CurDAG->getTargetConstant(Imm, MVT::i32); |
Anton Korobeynikov | da308c9 | 2009-07-16 13:34:50 +0000 | [diff] [blame] | 122 | } |
| 123 | |
Anton Korobeynikov | 4403b93 | 2009-07-16 13:27:25 +0000 | [diff] [blame] | 124 | // Include the pieces autogenerated from the target description. |
Anton Korobeynikov | 89edcd0 | 2009-07-16 13:33:57 +0000 | [diff] [blame] | 125 | #include "SystemZGenDAGISel.inc" |
Anton Korobeynikov | 4403b93 | 2009-07-16 13:27:25 +0000 | [diff] [blame] | 126 | |
| 127 | private: |
Dan Gohman | eeb3a00 | 2010-01-05 01:24:18 +0000 | [diff] [blame] | 128 | bool SelectAddrRI12Only(SDNode *Op, SDValue& Addr, |
Anton Korobeynikov | 014d463 | 2009-07-16 14:13:24 +0000 | [diff] [blame] | 129 | SDValue &Base, SDValue &Disp); |
Dan Gohman | eeb3a00 | 2010-01-05 01:24:18 +0000 | [diff] [blame] | 130 | bool SelectAddrRI12(SDNode *Op, SDValue& Addr, |
Anton Korobeynikov | 014d463 | 2009-07-16 14:13:24 +0000 | [diff] [blame] | 131 | SDValue &Base, SDValue &Disp, |
| 132 | bool is12BitOnly = false); |
Dan Gohman | eeb3a00 | 2010-01-05 01:24:18 +0000 | [diff] [blame] | 133 | bool SelectAddrRI(SDNode *Op, SDValue& Addr, |
Anton Korobeynikov | 9e4816e | 2009-07-16 13:43:18 +0000 | [diff] [blame] | 134 | SDValue &Base, SDValue &Disp); |
Dan Gohman | eeb3a00 | 2010-01-05 01:24:18 +0000 | [diff] [blame] | 135 | bool SelectAddrRRI12(SDNode *Op, SDValue Addr, |
Anton Korobeynikov | 720e3b0 | 2009-07-16 14:09:35 +0000 | [diff] [blame] | 136 | SDValue &Base, SDValue &Disp, SDValue &Index); |
Dan Gohman | eeb3a00 | 2010-01-05 01:24:18 +0000 | [diff] [blame] | 137 | bool SelectAddrRRI20(SDNode *Op, SDValue Addr, |
Anton Korobeynikov | 720e3b0 | 2009-07-16 14:09:35 +0000 | [diff] [blame] | 138 | SDValue &Base, SDValue &Disp, SDValue &Index); |
Dan Gohman | eeb3a00 | 2010-01-05 01:24:18 +0000 | [diff] [blame] | 139 | bool SelectLAAddr(SDNode *Op, SDValue Addr, |
Anton Korobeynikov | c4368a1 | 2009-07-16 13:48:42 +0000 | [diff] [blame] | 140 | SDValue &Base, SDValue &Disp, SDValue &Index); |
| 141 | |
Dan Gohman | eeb3a00 | 2010-01-05 01:24:18 +0000 | [diff] [blame] | 142 | SDNode *Select(SDNode *Node); |
Anton Korobeynikov | 0a42d2b | 2009-07-16 14:14:33 +0000 | [diff] [blame] | 143 | |
Dan Gohman | eeb3a00 | 2010-01-05 01:24:18 +0000 | [diff] [blame] | 144 | bool TryFoldLoad(SDNode *P, SDValue N, |
Anton Korobeynikov | 0a42d2b | 2009-07-16 14:14:33 +0000 | [diff] [blame] | 145 | SDValue &Base, SDValue &Disp, SDValue &Index); |
| 146 | |
Anton Korobeynikov | 720e3b0 | 2009-07-16 14:09:35 +0000 | [diff] [blame] | 147 | bool MatchAddress(SDValue N, SystemZRRIAddressMode &AM, |
| 148 | bool is12Bit, unsigned Depth = 0); |
Anton Korobeynikov | 3360da9 | 2009-07-16 13:44:00 +0000 | [diff] [blame] | 149 | bool MatchAddressBase(SDValue N, SystemZRRIAddressMode &AM); |
Anton Korobeynikov | 1ed1e3e | 2009-07-16 14:10:17 +0000 | [diff] [blame] | 150 | bool MatchAddressRI(SDValue N, SystemZRRIAddressMode &AM, |
| 151 | bool is12Bit); |
Anton Korobeynikov | 4403b93 | 2009-07-16 13:27:25 +0000 | [diff] [blame] | 152 | }; |
| 153 | } // end anonymous namespace |
| 154 | |
| 155 | /// createSystemZISelDag - This pass converts a legalized DAG into a |
| 156 | /// SystemZ-specific DAG, ready for instruction scheduling. |
| 157 | /// |
| 158 | FunctionPass *llvm::createSystemZISelDag(SystemZTargetMachine &TM, |
| 159 | CodeGenOpt::Level OptLevel) { |
| 160 | return new SystemZDAGToDAGISel(TM, OptLevel); |
| 161 | } |
| 162 | |
Anton Korobeynikov | 9e4816e | 2009-07-16 13:43:18 +0000 | [diff] [blame] | 163 | /// isImmSExt20 - This method tests to see if the node is either a 32-bit |
| 164 | /// or 64-bit immediate, and if the value can be accurately represented as a |
| 165 | /// sign extension from a 20-bit value. If so, this returns true and the |
| 166 | /// immediate. |
Anton Korobeynikov | 3240740 | 2009-07-16 13:48:23 +0000 | [diff] [blame] | 167 | static bool isImmSExt20(int64_t Val, int64_t &Imm) { |
Anton Korobeynikov | 3360da9 | 2009-07-16 13:44:00 +0000 | [diff] [blame] | 168 | if (Val >= -524288 && Val <= 524287) { |
Anton Korobeynikov | 3240740 | 2009-07-16 13:48:23 +0000 | [diff] [blame] | 169 | Imm = Val; |
Anton Korobeynikov | 3360da9 | 2009-07-16 13:44:00 +0000 | [diff] [blame] | 170 | return true; |
| 171 | } |
| 172 | return false; |
| 173 | } |
| 174 | |
Anton Korobeynikov | 720e3b0 | 2009-07-16 14:09:35 +0000 | [diff] [blame] | 175 | /// isImmZExt12 - This method tests to see if the node is either a 32-bit |
Anton Korobeynikov | 3166a9a | 2009-07-16 14:03:41 +0000 | [diff] [blame] | 176 | /// or 64-bit immediate, and if the value can be accurately represented as a |
| 177 | /// zero extension from a 12-bit value. If so, this returns true and the |
| 178 | /// immediate. |
Anton Korobeynikov | 720e3b0 | 2009-07-16 14:09:35 +0000 | [diff] [blame] | 179 | static bool isImmZExt12(int64_t Val, int64_t &Imm) { |
| 180 | if (Val >= 0 && Val <= 0xFFF) { |
Anton Korobeynikov | 3166a9a | 2009-07-16 14:03:41 +0000 | [diff] [blame] | 181 | Imm = Val; |
| 182 | return true; |
| 183 | } |
Anton Korobeynikov | 3166a9a | 2009-07-16 14:03:41 +0000 | [diff] [blame] | 184 | return false; |
| 185 | } |
| 186 | |
Anton Korobeynikov | 3360da9 | 2009-07-16 13:44:00 +0000 | [diff] [blame] | 187 | /// MatchAddress - Add the specified node to the specified addressing mode, |
| 188 | /// returning true if it cannot be done. This just pattern matches for the |
| 189 | /// addressing mode. |
| 190 | bool SystemZDAGToDAGISel::MatchAddress(SDValue N, SystemZRRIAddressMode &AM, |
Anton Korobeynikov | 720e3b0 | 2009-07-16 14:09:35 +0000 | [diff] [blame] | 191 | bool is12Bit, unsigned Depth) { |
Anton Korobeynikov | 3360da9 | 2009-07-16 13:44:00 +0000 | [diff] [blame] | 192 | DebugLoc dl = N.getDebugLoc(); |
Chris Lattner | 893e1c9 | 2009-08-23 06:49:22 +0000 | [diff] [blame] | 193 | DEBUG(errs() << "MatchAddress: "; AM.dump()); |
Anton Korobeynikov | 3360da9 | 2009-07-16 13:44:00 +0000 | [diff] [blame] | 194 | // Limit recursion. |
| 195 | if (Depth > 5) |
| 196 | return MatchAddressBase(N, AM); |
| 197 | |
Anton Korobeynikov | dc28955 | 2009-07-16 13:44:30 +0000 | [diff] [blame] | 198 | // FIXME: We can perform better here. If we have something like |
| 199 | // (shift (add A, imm), N), we can try to reassociate stuff and fold shift of |
| 200 | // imm into addressing mode. |
Anton Korobeynikov | 3360da9 | 2009-07-16 13:44:00 +0000 | [diff] [blame] | 201 | switch (N.getOpcode()) { |
| 202 | default: break; |
| 203 | case ISD::Constant: { |
Anton Korobeynikov | 3240740 | 2009-07-16 13:48:23 +0000 | [diff] [blame] | 204 | int64_t Val = cast<ConstantSDNode>(N)->getSExtValue(); |
Daniel Dunbar | 19c29f5 | 2009-07-17 02:19:26 +0000 | [diff] [blame] | 205 | int64_t Imm = 0; |
Anton Korobeynikov | 720e3b0 | 2009-07-16 14:09:35 +0000 | [diff] [blame] | 206 | bool Match = (is12Bit ? |
| 207 | isImmZExt12(AM.Disp + Val, Imm) : |
| 208 | isImmSExt20(AM.Disp + Val, Imm)); |
| 209 | if (Match) { |
Anton Korobeynikov | 3360da9 | 2009-07-16 13:44:00 +0000 | [diff] [blame] | 210 | AM.Disp = Imm; |
| 211 | return false; |
| 212 | } |
| 213 | break; |
| 214 | } |
| 215 | |
| 216 | case ISD::FrameIndex: |
Anton Korobeynikov | 720e3b0 | 2009-07-16 14:09:35 +0000 | [diff] [blame] | 217 | if (AM.BaseType == SystemZRRIAddressMode::RegBase && |
| 218 | AM.Base.Reg.getNode() == 0) { |
Anton Korobeynikov | 3360da9 | 2009-07-16 13:44:00 +0000 | [diff] [blame] | 219 | AM.BaseType = SystemZRRIAddressMode::FrameIndexBase; |
| 220 | AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex(); |
| 221 | return false; |
| 222 | } |
| 223 | break; |
| 224 | |
| 225 | case ISD::SUB: { |
| 226 | // Given A-B, if A can be completely folded into the address and |
| 227 | // the index field with the index field unused, use -B as the index. |
| 228 | // This is a win if a has multiple parts that can be folded into |
| 229 | // the address. Also, this saves a mov if the base register has |
| 230 | // other uses, since it avoids a two-address sub instruction, however |
| 231 | // it costs an additional mov if the index register has other uses. |
| 232 | |
| 233 | // Test if the LHS of the sub can be folded. |
| 234 | SystemZRRIAddressMode Backup = AM; |
Anton Korobeynikov | 720e3b0 | 2009-07-16 14:09:35 +0000 | [diff] [blame] | 235 | if (MatchAddress(N.getNode()->getOperand(0), AM, is12Bit, Depth+1)) { |
Anton Korobeynikov | 3360da9 | 2009-07-16 13:44:00 +0000 | [diff] [blame] | 236 | AM = Backup; |
| 237 | break; |
| 238 | } |
| 239 | // Test if the index field is free for use. |
Anton Korobeynikov | 54681ec | 2009-07-16 14:31:14 +0000 | [diff] [blame] | 240 | if (AM.IndexReg.getNode() || AM.isRI) { |
Anton Korobeynikov | 3360da9 | 2009-07-16 13:44:00 +0000 | [diff] [blame] | 241 | AM = Backup; |
| 242 | break; |
| 243 | } |
| 244 | |
| 245 | // If the base is a register with multiple uses, this transformation may |
| 246 | // save a mov. Otherwise it's probably better not to do it. |
| 247 | if (AM.BaseType == SystemZRRIAddressMode::RegBase && |
| 248 | (!AM.Base.Reg.getNode() || AM.Base.Reg.getNode()->hasOneUse())) { |
| 249 | AM = Backup; |
| 250 | break; |
| 251 | } |
| 252 | |
| 253 | // Ok, the transformation is legal and appears profitable. Go for it. |
| 254 | SDValue RHS = N.getNode()->getOperand(1); |
| 255 | SDValue Zero = CurDAG->getConstant(0, N.getValueType()); |
| 256 | SDValue Neg = CurDAG->getNode(ISD::SUB, dl, N.getValueType(), Zero, RHS); |
| 257 | AM.IndexReg = Neg; |
| 258 | |
| 259 | // Insert the new nodes into the topological ordering. |
| 260 | if (Zero.getNode()->getNodeId() == -1 || |
| 261 | Zero.getNode()->getNodeId() > N.getNode()->getNodeId()) { |
| 262 | CurDAG->RepositionNode(N.getNode(), Zero.getNode()); |
| 263 | Zero.getNode()->setNodeId(N.getNode()->getNodeId()); |
| 264 | } |
| 265 | if (Neg.getNode()->getNodeId() == -1 || |
| 266 | Neg.getNode()->getNodeId() > N.getNode()->getNodeId()) { |
| 267 | CurDAG->RepositionNode(N.getNode(), Neg.getNode()); |
| 268 | Neg.getNode()->setNodeId(N.getNode()->getNodeId()); |
| 269 | } |
| 270 | return false; |
| 271 | } |
| 272 | |
| 273 | case ISD::ADD: { |
| 274 | SystemZRRIAddressMode Backup = AM; |
Anton Korobeynikov | 720e3b0 | 2009-07-16 14:09:35 +0000 | [diff] [blame] | 275 | if (!MatchAddress(N.getNode()->getOperand(0), AM, is12Bit, Depth+1) && |
| 276 | !MatchAddress(N.getNode()->getOperand(1), AM, is12Bit, Depth+1)) |
Anton Korobeynikov | 3360da9 | 2009-07-16 13:44:00 +0000 | [diff] [blame] | 277 | return false; |
| 278 | AM = Backup; |
Anton Korobeynikov | 720e3b0 | 2009-07-16 14:09:35 +0000 | [diff] [blame] | 279 | if (!MatchAddress(N.getNode()->getOperand(1), AM, is12Bit, Depth+1) && |
| 280 | !MatchAddress(N.getNode()->getOperand(0), AM, is12Bit, Depth+1)) |
Anton Korobeynikov | 3360da9 | 2009-07-16 13:44:00 +0000 | [diff] [blame] | 281 | return false; |
| 282 | AM = Backup; |
| 283 | |
| 284 | // If we couldn't fold both operands into the address at the same time, |
| 285 | // see if we can just put each operand into a register and fold at least |
| 286 | // the add. |
Anton Korobeynikov | 1ed1e3e | 2009-07-16 14:10:17 +0000 | [diff] [blame] | 287 | if (!AM.isRI && |
| 288 | AM.BaseType == SystemZRRIAddressMode::RegBase && |
Anton Korobeynikov | 3360da9 | 2009-07-16 13:44:00 +0000 | [diff] [blame] | 289 | !AM.Base.Reg.getNode() && !AM.IndexReg.getNode()) { |
| 290 | AM.Base.Reg = N.getNode()->getOperand(0); |
| 291 | AM.IndexReg = N.getNode()->getOperand(1); |
| 292 | return false; |
| 293 | } |
| 294 | break; |
| 295 | } |
| 296 | |
| 297 | case ISD::OR: |
| 298 | // Handle "X | C" as "X + C" iff X is known to have C bits clear. |
| 299 | if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) { |
| 300 | SystemZRRIAddressMode Backup = AM; |
Anton Korobeynikov | 3240740 | 2009-07-16 13:48:23 +0000 | [diff] [blame] | 301 | int64_t Offset = CN->getSExtValue(); |
Daniel Dunbar | 19c29f5 | 2009-07-17 02:19:26 +0000 | [diff] [blame] | 302 | int64_t Imm = 0; |
Anton Korobeynikov | 720e3b0 | 2009-07-16 14:09:35 +0000 | [diff] [blame] | 303 | bool MatchOffset = (is12Bit ? |
| 304 | isImmZExt12(AM.Disp + Offset, Imm) : |
| 305 | isImmSExt20(AM.Disp + Offset, Imm)); |
| 306 | // The resultant disp must fit in 12 or 20-bits. |
| 307 | if (MatchOffset && |
| 308 | // LHS should be an addr mode. |
| 309 | !MatchAddress(N.getOperand(0), AM, is12Bit, Depth+1) && |
Anton Korobeynikov | 3360da9 | 2009-07-16 13:44:00 +0000 | [diff] [blame] | 310 | // Check to see if the LHS & C is zero. |
| 311 | CurDAG->MaskedValueIsZero(N.getOperand(0), CN->getAPIntValue())) { |
| 312 | AM.Disp = Imm; |
| 313 | return false; |
| 314 | } |
| 315 | AM = Backup; |
| 316 | } |
| 317 | break; |
| 318 | } |
| 319 | |
| 320 | return MatchAddressBase(N, AM); |
| 321 | } |
| 322 | |
| 323 | /// MatchAddressBase - Helper for MatchAddress. Add the specified node to the |
| 324 | /// specified addressing mode without any further recursion. |
| 325 | bool SystemZDAGToDAGISel::MatchAddressBase(SDValue N, |
| 326 | SystemZRRIAddressMode &AM) { |
| 327 | // Is the base register already occupied? |
| 328 | if (AM.BaseType != SystemZRRIAddressMode::RegBase || AM.Base.Reg.getNode()) { |
Anton Korobeynikov | 4656760 | 2009-07-16 14:10:35 +0000 | [diff] [blame] | 329 | // If so, check to see if the index register is set. |
Anton Korobeynikov | 1ed1e3e | 2009-07-16 14:10:17 +0000 | [diff] [blame] | 330 | if (AM.IndexReg.getNode() == 0 && !AM.isRI) { |
Anton Korobeynikov | 3360da9 | 2009-07-16 13:44:00 +0000 | [diff] [blame] | 331 | AM.IndexReg = N; |
| 332 | return false; |
| 333 | } |
| 334 | |
| 335 | // Otherwise, we cannot select it. |
| 336 | return true; |
| 337 | } |
| 338 | |
| 339 | // Default, generate it as a register. |
| 340 | AM.BaseType = SystemZRRIAddressMode::RegBase; |
| 341 | AM.Base.Reg = N; |
| 342 | return false; |
| 343 | } |
| 344 | |
Anton Korobeynikov | 1ed1e3e | 2009-07-16 14:10:17 +0000 | [diff] [blame] | 345 | void SystemZDAGToDAGISel::getAddressOperandsRI(const SystemZRRIAddressMode &AM, |
| 346 | SDValue &Base, SDValue &Disp) { |
Anton Korobeynikov | 720e3b0 | 2009-07-16 14:09:35 +0000 | [diff] [blame] | 347 | if (AM.BaseType == SystemZRRIAddressMode::RegBase) |
| 348 | Base = AM.Base.Reg; |
| 349 | else |
| 350 | Base = CurDAG->getTargetFrameIndex(AM.Base.FrameIndex, TLI.getPointerTy()); |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 351 | Disp = CurDAG->getTargetConstant(AM.Disp, MVT::i64); |
Anton Korobeynikov | 720e3b0 | 2009-07-16 14:09:35 +0000 | [diff] [blame] | 352 | } |
| 353 | |
Anton Korobeynikov | 1ed1e3e | 2009-07-16 14:10:17 +0000 | [diff] [blame] | 354 | void SystemZDAGToDAGISel::getAddressOperands(const SystemZRRIAddressMode &AM, |
| 355 | SDValue &Base, SDValue &Disp, |
| 356 | SDValue &Index) { |
| 357 | getAddressOperandsRI(AM, Base, Disp); |
| 358 | Index = AM.IndexReg; |
| 359 | } |
| 360 | |
| 361 | /// Returns true if the address can be represented by a base register plus |
| 362 | /// an unsigned 12-bit displacement [r+imm]. |
Dan Gohman | eeb3a00 | 2010-01-05 01:24:18 +0000 | [diff] [blame] | 363 | bool SystemZDAGToDAGISel::SelectAddrRI12Only(SDNode *Op, SDValue& Addr, |
Anton Korobeynikov | 014d463 | 2009-07-16 14:13:24 +0000 | [diff] [blame] | 364 | SDValue &Base, SDValue &Disp) { |
| 365 | return SelectAddrRI12(Op, Addr, Base, Disp, /*is12BitOnly*/true); |
| 366 | } |
| 367 | |
Dan Gohman | eeb3a00 | 2010-01-05 01:24:18 +0000 | [diff] [blame] | 368 | bool SystemZDAGToDAGISel::SelectAddrRI12(SDNode *Op, SDValue& Addr, |
Anton Korobeynikov | 014d463 | 2009-07-16 14:13:24 +0000 | [diff] [blame] | 369 | SDValue &Base, SDValue &Disp, |
| 370 | bool is12BitOnly) { |
Anton Korobeynikov | 1ed1e3e | 2009-07-16 14:10:17 +0000 | [diff] [blame] | 371 | SystemZRRIAddressMode AM20(/*isRI*/true), AM12(/*isRI*/true); |
| 372 | bool Done = false; |
| 373 | |
| 374 | if (!Addr.hasOneUse()) { |
| 375 | unsigned Opcode = Addr.getOpcode(); |
| 376 | if (Opcode != ISD::Constant && Opcode != ISD::FrameIndex) { |
| 377 | // If we are able to fold N into addressing mode, then we'll allow it even |
| 378 | // if N has multiple uses. In general, addressing computation is used as |
| 379 | // addresses by all of its uses. But watch out for CopyToReg uses, that |
| 380 | // means the address computation is liveout. It will be computed by a LA |
| 381 | // so we want to avoid computing the address twice. |
| 382 | for (SDNode::use_iterator UI = Addr.getNode()->use_begin(), |
| 383 | UE = Addr.getNode()->use_end(); UI != UE; ++UI) { |
| 384 | if (UI->getOpcode() == ISD::CopyToReg) { |
| 385 | MatchAddressBase(Addr, AM12); |
| 386 | Done = true; |
| 387 | break; |
| 388 | } |
| 389 | } |
| 390 | } |
| 391 | } |
| 392 | if (!Done && MatchAddress(Addr, AM12, /* is12Bit */ true)) |
| 393 | return false; |
| 394 | |
| 395 | // Check, whether we can match stuff using 20-bit displacements |
Anton Korobeynikov | 014d463 | 2009-07-16 14:13:24 +0000 | [diff] [blame] | 396 | if (!Done && !is12BitOnly && |
| 397 | !MatchAddress(Addr, AM20, /* is12Bit */ false)) |
Anton Korobeynikov | 1ed1e3e | 2009-07-16 14:10:17 +0000 | [diff] [blame] | 398 | if (AM12.Disp == 0 && AM20.Disp != 0) |
| 399 | return false; |
| 400 | |
Chris Lattner | 893e1c9 | 2009-08-23 06:49:22 +0000 | [diff] [blame] | 401 | DEBUG(errs() << "MatchAddress (final): "; AM12.dump()); |
Anton Korobeynikov | 1ed1e3e | 2009-07-16 14:10:17 +0000 | [diff] [blame] | 402 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 403 | EVT VT = Addr.getValueType(); |
Anton Korobeynikov | 1ed1e3e | 2009-07-16 14:10:17 +0000 | [diff] [blame] | 404 | if (AM12.BaseType == SystemZRRIAddressMode::RegBase) { |
| 405 | if (!AM12.Base.Reg.getNode()) |
| 406 | AM12.Base.Reg = CurDAG->getRegister(0, VT); |
| 407 | } |
| 408 | |
| 409 | assert(AM12.IndexReg.getNode() == 0 && "Invalid reg-imm address mode!"); |
| 410 | |
| 411 | getAddressOperandsRI(AM12, Base, Disp); |
| 412 | |
| 413 | return true; |
| 414 | } |
| 415 | |
| 416 | /// Returns true if the address can be represented by a base register plus |
| 417 | /// a signed 20-bit displacement [r+imm]. |
Dan Gohman | eeb3a00 | 2010-01-05 01:24:18 +0000 | [diff] [blame] | 418 | bool SystemZDAGToDAGISel::SelectAddrRI(SDNode *Op, SDValue& Addr, |
Anton Korobeynikov | 1ed1e3e | 2009-07-16 14:10:17 +0000 | [diff] [blame] | 419 | SDValue &Base, SDValue &Disp) { |
| 420 | SystemZRRIAddressMode AM(/*isRI*/true); |
| 421 | bool Done = false; |
| 422 | |
| 423 | if (!Addr.hasOneUse()) { |
| 424 | unsigned Opcode = Addr.getOpcode(); |
| 425 | if (Opcode != ISD::Constant && Opcode != ISD::FrameIndex) { |
| 426 | // If we are able to fold N into addressing mode, then we'll allow it even |
| 427 | // if N has multiple uses. In general, addressing computation is used as |
| 428 | // addresses by all of its uses. But watch out for CopyToReg uses, that |
| 429 | // means the address computation is liveout. It will be computed by a LA |
| 430 | // so we want to avoid computing the address twice. |
| 431 | for (SDNode::use_iterator UI = Addr.getNode()->use_begin(), |
| 432 | UE = Addr.getNode()->use_end(); UI != UE; ++UI) { |
| 433 | if (UI->getOpcode() == ISD::CopyToReg) { |
| 434 | MatchAddressBase(Addr, AM); |
| 435 | Done = true; |
| 436 | break; |
| 437 | } |
| 438 | } |
| 439 | } |
| 440 | } |
| 441 | if (!Done && MatchAddress(Addr, AM, /* is12Bit */ false)) |
| 442 | return false; |
| 443 | |
Chris Lattner | 893e1c9 | 2009-08-23 06:49:22 +0000 | [diff] [blame] | 444 | DEBUG(errs() << "MatchAddress (final): "; AM.dump()); |
Anton Korobeynikov | 1ed1e3e | 2009-07-16 14:10:17 +0000 | [diff] [blame] | 445 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 446 | EVT VT = Addr.getValueType(); |
Anton Korobeynikov | 1ed1e3e | 2009-07-16 14:10:17 +0000 | [diff] [blame] | 447 | if (AM.BaseType == SystemZRRIAddressMode::RegBase) { |
| 448 | if (!AM.Base.Reg.getNode()) |
| 449 | AM.Base.Reg = CurDAG->getRegister(0, VT); |
| 450 | } |
| 451 | |
| 452 | assert(AM.IndexReg.getNode() == 0 && "Invalid reg-imm address mode!"); |
| 453 | |
| 454 | getAddressOperandsRI(AM, Base, Disp); |
| 455 | |
| 456 | return true; |
| 457 | } |
| 458 | |
Anton Korobeynikov | 720e3b0 | 2009-07-16 14:09:35 +0000 | [diff] [blame] | 459 | /// Returns true if the address can be represented by a base register plus |
| 460 | /// index register plus an unsigned 12-bit displacement [base + idx + imm]. |
Dan Gohman | eeb3a00 | 2010-01-05 01:24:18 +0000 | [diff] [blame] | 461 | bool SystemZDAGToDAGISel::SelectAddrRRI12(SDNode *Op, SDValue Addr, |
Anton Korobeynikov | 720e3b0 | 2009-07-16 14:09:35 +0000 | [diff] [blame] | 462 | SDValue &Base, SDValue &Disp, SDValue &Index) { |
Anton Korobeynikov | 4656760 | 2009-07-16 14:10:35 +0000 | [diff] [blame] | 463 | SystemZRRIAddressMode AM20, AM12; |
Anton Korobeynikov | 720e3b0 | 2009-07-16 14:09:35 +0000 | [diff] [blame] | 464 | bool Done = false; |
| 465 | |
| 466 | if (!Addr.hasOneUse()) { |
| 467 | unsigned Opcode = Addr.getOpcode(); |
| 468 | if (Opcode != ISD::Constant && Opcode != ISD::FrameIndex) { |
| 469 | // If we are able to fold N into addressing mode, then we'll allow it even |
| 470 | // if N has multiple uses. In general, addressing computation is used as |
| 471 | // addresses by all of its uses. But watch out for CopyToReg uses, that |
| 472 | // means the address computation is liveout. It will be computed by a LA |
| 473 | // so we want to avoid computing the address twice. |
| 474 | for (SDNode::use_iterator UI = Addr.getNode()->use_begin(), |
| 475 | UE = Addr.getNode()->use_end(); UI != UE; ++UI) { |
| 476 | if (UI->getOpcode() == ISD::CopyToReg) { |
| 477 | MatchAddressBase(Addr, AM12); |
| 478 | Done = true; |
| 479 | break; |
| 480 | } |
| 481 | } |
| 482 | } |
| 483 | } |
| 484 | if (!Done && MatchAddress(Addr, AM12, /* is12Bit */ true)) |
| 485 | return false; |
| 486 | |
| 487 | // Check, whether we can match stuff using 20-bit displacements |
| 488 | if (!Done && !MatchAddress(Addr, AM20, /* is12Bit */ false)) |
| 489 | if (AM12.Disp == 0 && AM20.Disp != 0) |
| 490 | return false; |
| 491 | |
Chris Lattner | 893e1c9 | 2009-08-23 06:49:22 +0000 | [diff] [blame] | 492 | DEBUG(errs() << "MatchAddress (final): "; AM12.dump()); |
Anton Korobeynikov | 720e3b0 | 2009-07-16 14:09:35 +0000 | [diff] [blame] | 493 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 494 | EVT VT = Addr.getValueType(); |
Anton Korobeynikov | 720e3b0 | 2009-07-16 14:09:35 +0000 | [diff] [blame] | 495 | if (AM12.BaseType == SystemZRRIAddressMode::RegBase) { |
| 496 | if (!AM12.Base.Reg.getNode()) |
| 497 | AM12.Base.Reg = CurDAG->getRegister(0, VT); |
| 498 | } |
| 499 | |
| 500 | if (!AM12.IndexReg.getNode()) |
| 501 | AM12.IndexReg = CurDAG->getRegister(0, VT); |
| 502 | |
| 503 | getAddressOperands(AM12, Base, Disp, Index); |
| 504 | |
| 505 | return true; |
| 506 | } |
| 507 | |
Anton Korobeynikov | 3360da9 | 2009-07-16 13:44:00 +0000 | [diff] [blame] | 508 | /// Returns true if the address can be represented by a base register plus |
| 509 | /// index register plus a signed 20-bit displacement [base + idx + imm]. |
Dan Gohman | eeb3a00 | 2010-01-05 01:24:18 +0000 | [diff] [blame] | 510 | bool SystemZDAGToDAGISel::SelectAddrRRI20(SDNode *Op, SDValue Addr, |
Anton Korobeynikov | c4368a1 | 2009-07-16 13:48:42 +0000 | [diff] [blame] | 511 | SDValue &Base, SDValue &Disp, SDValue &Index) { |
Anton Korobeynikov | 3360da9 | 2009-07-16 13:44:00 +0000 | [diff] [blame] | 512 | SystemZRRIAddressMode AM; |
| 513 | bool Done = false; |
| 514 | |
Anton Korobeynikov | 711d5b6 | 2009-07-16 13:47:59 +0000 | [diff] [blame] | 515 | if (!Addr.hasOneUse()) { |
| 516 | unsigned Opcode = Addr.getOpcode(); |
| 517 | if (Opcode != ISD::Constant && Opcode != ISD::FrameIndex) { |
| 518 | // If we are able to fold N into addressing mode, then we'll allow it even |
| 519 | // if N has multiple uses. In general, addressing computation is used as |
| 520 | // addresses by all of its uses. But watch out for CopyToReg uses, that |
| 521 | // means the address computation is liveout. It will be computed by a LA |
| 522 | // so we want to avoid computing the address twice. |
| 523 | for (SDNode::use_iterator UI = Addr.getNode()->use_begin(), |
| 524 | UE = Addr.getNode()->use_end(); UI != UE; ++UI) { |
| 525 | if (UI->getOpcode() == ISD::CopyToReg) { |
| 526 | MatchAddressBase(Addr, AM); |
| 527 | Done = true; |
| 528 | break; |
| 529 | } |
| 530 | } |
| 531 | } |
| 532 | } |
Anton Korobeynikov | 720e3b0 | 2009-07-16 14:09:35 +0000 | [diff] [blame] | 533 | if (!Done && MatchAddress(Addr, AM, /* is12Bit */ false)) |
Anton Korobeynikov | 3360da9 | 2009-07-16 13:44:00 +0000 | [diff] [blame] | 534 | return false; |
| 535 | |
Chris Lattner | 893e1c9 | 2009-08-23 06:49:22 +0000 | [diff] [blame] | 536 | DEBUG(errs() << "MatchAddress (final): "; AM.dump()); |
Anton Korobeynikov | 3240740 | 2009-07-16 13:48:23 +0000 | [diff] [blame] | 537 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 538 | EVT VT = Addr.getValueType(); |
Anton Korobeynikov | 3360da9 | 2009-07-16 13:44:00 +0000 | [diff] [blame] | 539 | if (AM.BaseType == SystemZRRIAddressMode::RegBase) { |
| 540 | if (!AM.Base.Reg.getNode()) |
| 541 | AM.Base.Reg = CurDAG->getRegister(0, VT); |
| 542 | } |
| 543 | |
| 544 | if (!AM.IndexReg.getNode()) |
| 545 | AM.IndexReg = CurDAG->getRegister(0, VT); |
| 546 | |
Anton Korobeynikov | 720e3b0 | 2009-07-16 14:09:35 +0000 | [diff] [blame] | 547 | getAddressOperands(AM, Base, Disp, Index); |
Anton Korobeynikov | 3360da9 | 2009-07-16 13:44:00 +0000 | [diff] [blame] | 548 | |
| 549 | return true; |
| 550 | } |
Anton Korobeynikov | 4403b93 | 2009-07-16 13:27:25 +0000 | [diff] [blame] | 551 | |
Anton Korobeynikov | 711d5b6 | 2009-07-16 13:47:59 +0000 | [diff] [blame] | 552 | /// SelectLAAddr - it calls SelectAddr and determines if the maximal addressing |
| 553 | /// mode it matches can be cost effectively emitted as an LA/LAY instruction. |
Dan Gohman | eeb3a00 | 2010-01-05 01:24:18 +0000 | [diff] [blame] | 554 | bool SystemZDAGToDAGISel::SelectLAAddr(SDNode *Op, SDValue Addr, |
Anton Korobeynikov | c4368a1 | 2009-07-16 13:48:42 +0000 | [diff] [blame] | 555 | SDValue &Base, SDValue &Disp, SDValue &Index) { |
Anton Korobeynikov | 711d5b6 | 2009-07-16 13:47:59 +0000 | [diff] [blame] | 556 | SystemZRRIAddressMode AM; |
| 557 | |
Anton Korobeynikov | 720e3b0 | 2009-07-16 14:09:35 +0000 | [diff] [blame] | 558 | if (MatchAddress(Addr, AM, false)) |
Anton Korobeynikov | 711d5b6 | 2009-07-16 13:47:59 +0000 | [diff] [blame] | 559 | return false; |
| 560 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 561 | EVT VT = Addr.getValueType(); |
Anton Korobeynikov | 711d5b6 | 2009-07-16 13:47:59 +0000 | [diff] [blame] | 562 | unsigned Complexity = 0; |
| 563 | if (AM.BaseType == SystemZRRIAddressMode::RegBase) |
| 564 | if (AM.Base.Reg.getNode()) |
| 565 | Complexity = 1; |
| 566 | else |
| 567 | AM.Base.Reg = CurDAG->getRegister(0, VT); |
| 568 | else if (AM.BaseType == SystemZRRIAddressMode::FrameIndexBase) |
| 569 | Complexity = 4; |
| 570 | |
| 571 | if (AM.IndexReg.getNode()) |
| 572 | Complexity += 1; |
| 573 | else |
| 574 | AM.IndexReg = CurDAG->getRegister(0, VT); |
| 575 | |
| 576 | if (AM.Disp && (AM.Base.Reg.getNode() || AM.IndexReg.getNode())) |
| 577 | Complexity += 1; |
| 578 | |
| 579 | if (Complexity > 2) { |
Anton Korobeynikov | 720e3b0 | 2009-07-16 14:09:35 +0000 | [diff] [blame] | 580 | getAddressOperands(AM, Base, Disp, Index); |
Anton Korobeynikov | 711d5b6 | 2009-07-16 13:47:59 +0000 | [diff] [blame] | 581 | return true; |
| 582 | } |
| 583 | |
| 584 | return false; |
| 585 | } |
| 586 | |
Dan Gohman | eeb3a00 | 2010-01-05 01:24:18 +0000 | [diff] [blame] | 587 | bool SystemZDAGToDAGISel::TryFoldLoad(SDNode *P, SDValue N, |
Anton Korobeynikov | 0a42d2b | 2009-07-16 14:14:33 +0000 | [diff] [blame] | 588 | SDValue &Base, SDValue &Disp, SDValue &Index) { |
| 589 | if (ISD::isNON_EXTLoad(N.getNode()) && |
Dan Gohman | d858e90 | 2010-04-17 15:26:15 +0000 | [diff] [blame^] | 590 | IsLegalToFold(N, P, P, OptLevel)) |
Anton Korobeynikov | 0a42d2b | 2009-07-16 14:14:33 +0000 | [diff] [blame] | 591 | return SelectAddrRRI20(P, N.getOperand(1), Base, Disp, Index); |
| 592 | return false; |
| 593 | } |
| 594 | |
Dan Gohman | eeb3a00 | 2010-01-05 01:24:18 +0000 | [diff] [blame] | 595 | SDNode *SystemZDAGToDAGISel::Select(SDNode *Node) { |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 596 | EVT NVT = Node->getValueType(0); |
Dan Gohman | eeb3a00 | 2010-01-05 01:24:18 +0000 | [diff] [blame] | 597 | DebugLoc dl = Node->getDebugLoc(); |
Anton Korobeynikov | 0a42d2b | 2009-07-16 14:14:33 +0000 | [diff] [blame] | 598 | unsigned Opcode = Node->getOpcode(); |
Anton Korobeynikov | 4403b93 | 2009-07-16 13:27:25 +0000 | [diff] [blame] | 599 | |
| 600 | // Dump information about the Node being selected |
Chris Lattner | 7c306da | 2010-03-02 06:34:30 +0000 | [diff] [blame] | 601 | DEBUG(errs() << "Selecting: "; Node->dump(CurDAG); errs() << "\n"); |
Anton Korobeynikov | 4403b93 | 2009-07-16 13:27:25 +0000 | [diff] [blame] | 602 | |
| 603 | // If we have a custom node, we already have selected! |
| 604 | if (Node->isMachineOpcode()) { |
Chris Lattner | 7c306da | 2010-03-02 06:34:30 +0000 | [diff] [blame] | 605 | DEBUG(errs() << "== "; Node->dump(CurDAG); errs() << "\n"); |
Anton Korobeynikov | 0a42d2b | 2009-07-16 14:14:33 +0000 | [diff] [blame] | 606 | return NULL; // Already selected. |
| 607 | } |
| 608 | |
| 609 | switch (Opcode) { |
| 610 | default: break; |
| 611 | case ISD::SDIVREM: { |
Anton Korobeynikov | 09e3900 | 2009-07-16 14:17:52 +0000 | [diff] [blame] | 612 | unsigned Opc, MOpc; |
Anton Korobeynikov | 0a42d2b | 2009-07-16 14:14:33 +0000 | [diff] [blame] | 613 | SDValue N0 = Node->getOperand(0); |
| 614 | SDValue N1 = Node->getOperand(1); |
| 615 | |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 616 | EVT ResVT; |
Anton Korobeynikov | 09e3900 | 2009-07-16 14:17:52 +0000 | [diff] [blame] | 617 | bool is32Bit = false; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 618 | switch (NVT.getSimpleVT().SimpleTy) { |
Anton Korobeynikov | 39784e1 | 2010-01-04 10:31:54 +0000 | [diff] [blame] | 619 | default: assert(0 && "Unsupported VT!"); |
| 620 | case MVT::i32: |
| 621 | Opc = SystemZ::SDIVREM32r; MOpc = SystemZ::SDIVREM32m; |
| 622 | ResVT = MVT::v2i64; |
| 623 | is32Bit = true; |
| 624 | break; |
| 625 | case MVT::i64: |
| 626 | Opc = SystemZ::SDIVREM64r; MOpc = SystemZ::SDIVREM64m; |
| 627 | ResVT = MVT::v2i64; |
| 628 | break; |
Anton Korobeynikov | 0a42d2b | 2009-07-16 14:14:33 +0000 | [diff] [blame] | 629 | } |
| 630 | |
| 631 | SDValue Tmp0, Tmp1, Tmp2; |
Dan Gohman | eeb3a00 | 2010-01-05 01:24:18 +0000 | [diff] [blame] | 632 | bool foldedLoad = TryFoldLoad(Node, N1, Tmp0, Tmp1, Tmp2); |
Anton Korobeynikov | 0a42d2b | 2009-07-16 14:14:33 +0000 | [diff] [blame] | 633 | |
| 634 | // Prepare the dividend |
Anton Korobeynikov | 09e3900 | 2009-07-16 14:17:52 +0000 | [diff] [blame] | 635 | SDNode *Dividend; |
| 636 | if (is32Bit) |
Dan Gohman | 602b0c8 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 637 | Dividend = CurDAG->getMachineNode(SystemZ::MOVSX64rr32, dl, MVT::i64, N0); |
Anton Korobeynikov | 09e3900 | 2009-07-16 14:17:52 +0000 | [diff] [blame] | 638 | else |
| 639 | Dividend = N0.getNode(); |
Anton Korobeynikov | 0a42d2b | 2009-07-16 14:14:33 +0000 | [diff] [blame] | 640 | |
| 641 | // Insert prepared dividend into suitable 'subreg' |
Chris Lattner | 518bb53 | 2010-02-09 19:54:29 +0000 | [diff] [blame] | 642 | SDNode *Tmp = CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF, |
Dan Gohman | 602b0c8 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 643 | dl, ResVT); |
Anton Korobeynikov | 0a42d2b | 2009-07-16 14:14:33 +0000 | [diff] [blame] | 644 | Dividend = |
Chris Lattner | 518bb53 | 2010-02-09 19:54:29 +0000 | [diff] [blame] | 645 | CurDAG->getMachineNode(TargetOpcode::INSERT_SUBREG, dl, ResVT, |
Dan Gohman | 602b0c8 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 646 | SDValue(Tmp, 0), SDValue(Dividend, 0), |
| 647 | CurDAG->getTargetConstant(subreg_odd, MVT::i32)); |
Anton Korobeynikov | 0a42d2b | 2009-07-16 14:14:33 +0000 | [diff] [blame] | 648 | |
Anton Korobeynikov | 0a42d2b | 2009-07-16 14:14:33 +0000 | [diff] [blame] | 649 | SDNode *Result; |
| 650 | SDValue DivVal = SDValue(Dividend, 0); |
| 651 | if (foldedLoad) { |
| 652 | SDValue Ops[] = { DivVal, Tmp0, Tmp1, Tmp2, N1.getOperand(0) }; |
Anton Korobeynikov | 39784e1 | 2010-01-04 10:31:54 +0000 | [diff] [blame] | 653 | Result = CurDAG->getMachineNode(MOpc, dl, ResVT, MVT::Other, |
Dan Gohman | 602b0c8 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 654 | Ops, array_lengthof(Ops)); |
Anton Korobeynikov | 0a42d2b | 2009-07-16 14:14:33 +0000 | [diff] [blame] | 655 | // Update the chain. |
Anton Korobeynikov | 39784e1 | 2010-01-04 10:31:54 +0000 | [diff] [blame] | 656 | ReplaceUses(N1.getValue(1), SDValue(Result, 1)); |
Anton Korobeynikov | 0a42d2b | 2009-07-16 14:14:33 +0000 | [diff] [blame] | 657 | } else { |
Dan Gohman | 602b0c8 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 658 | Result = CurDAG->getMachineNode(Opc, dl, ResVT, SDValue(Dividend, 0), N1); |
Anton Korobeynikov | 0a42d2b | 2009-07-16 14:14:33 +0000 | [diff] [blame] | 659 | } |
| 660 | |
| 661 | // Copy the division (odd subreg) result, if it is needed. |
Dan Gohman | eeb3a00 | 2010-01-05 01:24:18 +0000 | [diff] [blame] | 662 | if (!SDValue(Node, 0).use_empty()) { |
Anton Korobeynikov | 8bd0db7 | 2009-07-16 14:18:17 +0000 | [diff] [blame] | 663 | unsigned SubRegIdx = (is32Bit ? subreg_odd32 : subreg_odd); |
Chris Lattner | 518bb53 | 2010-02-09 19:54:29 +0000 | [diff] [blame] | 664 | SDNode *Div = CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG, |
Dan Gohman | 602b0c8 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 665 | dl, NVT, |
| 666 | SDValue(Result, 0), |
| 667 | CurDAG->getTargetConstant(SubRegIdx, |
| 668 | MVT::i32)); |
Anton Korobeynikov | 8bd0db7 | 2009-07-16 14:18:17 +0000 | [diff] [blame] | 669 | |
Dan Gohman | eeb3a00 | 2010-01-05 01:24:18 +0000 | [diff] [blame] | 670 | ReplaceUses(SDValue(Node, 0), SDValue(Div, 0)); |
Chris Lattner | 7c306da | 2010-03-02 06:34:30 +0000 | [diff] [blame] | 671 | DEBUG(errs() << "=> "; Result->dump(CurDAG); errs() << "\n"); |
Anton Korobeynikov | 0a42d2b | 2009-07-16 14:14:33 +0000 | [diff] [blame] | 672 | } |
| 673 | |
| 674 | // Copy the remainder (even subreg) result, if it is needed. |
Dan Gohman | eeb3a00 | 2010-01-05 01:24:18 +0000 | [diff] [blame] | 675 | if (!SDValue(Node, 1).use_empty()) { |
Anton Korobeynikov | 8bd0db7 | 2009-07-16 14:18:17 +0000 | [diff] [blame] | 676 | unsigned SubRegIdx = (is32Bit ? subreg_even32 : subreg_even); |
Chris Lattner | 518bb53 | 2010-02-09 19:54:29 +0000 | [diff] [blame] | 677 | SDNode *Rem = CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG, |
Dan Gohman | 602b0c8 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 678 | dl, NVT, |
| 679 | SDValue(Result, 0), |
| 680 | CurDAG->getTargetConstant(SubRegIdx, |
| 681 | MVT::i32)); |
Anton Korobeynikov | 09e3900 | 2009-07-16 14:17:52 +0000 | [diff] [blame] | 682 | |
Dan Gohman | eeb3a00 | 2010-01-05 01:24:18 +0000 | [diff] [blame] | 683 | ReplaceUses(SDValue(Node, 1), SDValue(Rem, 0)); |
Chris Lattner | 7c306da | 2010-03-02 06:34:30 +0000 | [diff] [blame] | 684 | DEBUG(errs() << "=> "; Result->dump(CurDAG); errs() << "\n"); |
Anton Korobeynikov | 0a42d2b | 2009-07-16 14:14:33 +0000 | [diff] [blame] | 685 | } |
| 686 | |
Anton Korobeynikov | 4403b93 | 2009-07-16 13:27:25 +0000 | [diff] [blame] | 687 | return NULL; |
| 688 | } |
Anton Korobeynikov | 0a42d2b | 2009-07-16 14:14:33 +0000 | [diff] [blame] | 689 | case ISD::UDIVREM: { |
| 690 | unsigned Opc, MOpc, ClrOpc; |
| 691 | SDValue N0 = Node->getOperand(0); |
| 692 | SDValue N1 = Node->getOperand(1); |
Owen Anderson | e50ed30 | 2009-08-10 22:56:29 +0000 | [diff] [blame] | 693 | EVT ResVT; |
Anton Korobeynikov | 0a42d2b | 2009-07-16 14:14:33 +0000 | [diff] [blame] | 694 | |
Anton Korobeynikov | 8bd0db7 | 2009-07-16 14:18:17 +0000 | [diff] [blame] | 695 | bool is32Bit = false; |
Owen Anderson | 825b72b | 2009-08-11 20:47:22 +0000 | [diff] [blame] | 696 | switch (NVT.getSimpleVT().SimpleTy) { |
Anton Korobeynikov | 39784e1 | 2010-01-04 10:31:54 +0000 | [diff] [blame] | 697 | default: assert(0 && "Unsupported VT!"); |
| 698 | case MVT::i32: |
| 699 | Opc = SystemZ::UDIVREM32r; MOpc = SystemZ::UDIVREM32m; |
| 700 | ClrOpc = SystemZ::MOV64Pr0_even; |
| 701 | ResVT = MVT::v2i32; |
| 702 | is32Bit = true; |
| 703 | break; |
| 704 | case MVT::i64: |
| 705 | Opc = SystemZ::UDIVREM64r; MOpc = SystemZ::UDIVREM64m; |
| 706 | ClrOpc = SystemZ::MOV128r0_even; |
| 707 | ResVT = MVT::v2i64; |
| 708 | break; |
Anton Korobeynikov | 0a42d2b | 2009-07-16 14:14:33 +0000 | [diff] [blame] | 709 | } |
| 710 | |
| 711 | SDValue Tmp0, Tmp1, Tmp2; |
Dan Gohman | eeb3a00 | 2010-01-05 01:24:18 +0000 | [diff] [blame] | 712 | bool foldedLoad = TryFoldLoad(Node, N1, Tmp0, Tmp1, Tmp2); |
Anton Korobeynikov | 0a42d2b | 2009-07-16 14:14:33 +0000 | [diff] [blame] | 713 | |
| 714 | // Prepare the dividend |
| 715 | SDNode *Dividend = N0.getNode(); |
| 716 | |
| 717 | // Insert prepared dividend into suitable 'subreg' |
Chris Lattner | 518bb53 | 2010-02-09 19:54:29 +0000 | [diff] [blame] | 718 | SDNode *Tmp = CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF, |
Dan Gohman | 602b0c8 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 719 | dl, ResVT); |
Anton Korobeynikov | 8bd0db7 | 2009-07-16 14:18:17 +0000 | [diff] [blame] | 720 | { |
| 721 | unsigned SubRegIdx = (is32Bit ? subreg_odd32 : subreg_odd); |
| 722 | Dividend = |
Chris Lattner | 518bb53 | 2010-02-09 19:54:29 +0000 | [diff] [blame] | 723 | CurDAG->getMachineNode(TargetOpcode::INSERT_SUBREG, dl, ResVT, |
Dan Gohman | 602b0c8 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 724 | SDValue(Tmp, 0), SDValue(Dividend, 0), |
| 725 | CurDAG->getTargetConstant(SubRegIdx, MVT::i32)); |
Anton Korobeynikov | 8bd0db7 | 2009-07-16 14:18:17 +0000 | [diff] [blame] | 726 | } |
Anton Korobeynikov | 0a42d2b | 2009-07-16 14:14:33 +0000 | [diff] [blame] | 727 | |
Anton Korobeynikov | e3a7f7a | 2009-07-16 14:14:54 +0000 | [diff] [blame] | 728 | // Zero out even subreg |
Dan Gohman | 602b0c8 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 729 | Dividend = CurDAG->getMachineNode(ClrOpc, dl, ResVT, SDValue(Dividend, 0)); |
Anton Korobeynikov | 0a42d2b | 2009-07-16 14:14:33 +0000 | [diff] [blame] | 730 | |
| 731 | SDValue DivVal = SDValue(Dividend, 0); |
| 732 | SDNode *Result; |
| 733 | if (foldedLoad) { |
| 734 | SDValue Ops[] = { DivVal, Tmp0, Tmp1, Tmp2, N1.getOperand(0) }; |
Anton Korobeynikov | 39784e1 | 2010-01-04 10:31:54 +0000 | [diff] [blame] | 735 | Result = CurDAG->getMachineNode(MOpc, dl, ResVT, MVT::Other, |
Dan Gohman | 602b0c8 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 736 | Ops, array_lengthof(Ops)); |
Anton Korobeynikov | 0a42d2b | 2009-07-16 14:14:33 +0000 | [diff] [blame] | 737 | // Update the chain. |
Anton Korobeynikov | 39784e1 | 2010-01-04 10:31:54 +0000 | [diff] [blame] | 738 | ReplaceUses(N1.getValue(1), SDValue(Result, 1)); |
Anton Korobeynikov | 0a42d2b | 2009-07-16 14:14:33 +0000 | [diff] [blame] | 739 | } else { |
Dan Gohman | 602b0c8 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 740 | Result = CurDAG->getMachineNode(Opc, dl, ResVT, DivVal, N1); |
Anton Korobeynikov | 0a42d2b | 2009-07-16 14:14:33 +0000 | [diff] [blame] | 741 | } |
| 742 | |
| 743 | // Copy the division (odd subreg) result, if it is needed. |
Dan Gohman | eeb3a00 | 2010-01-05 01:24:18 +0000 | [diff] [blame] | 744 | if (!SDValue(Node, 0).use_empty()) { |
Anton Korobeynikov | 8bd0db7 | 2009-07-16 14:18:17 +0000 | [diff] [blame] | 745 | unsigned SubRegIdx = (is32Bit ? subreg_odd32 : subreg_odd); |
Chris Lattner | 518bb53 | 2010-02-09 19:54:29 +0000 | [diff] [blame] | 746 | SDNode *Div = CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG, |
Dan Gohman | 602b0c8 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 747 | dl, NVT, |
| 748 | SDValue(Result, 0), |
| 749 | CurDAG->getTargetConstant(SubRegIdx, |
| 750 | MVT::i32)); |
Dan Gohman | eeb3a00 | 2010-01-05 01:24:18 +0000 | [diff] [blame] | 751 | ReplaceUses(SDValue(Node, 0), SDValue(Div, 0)); |
Chris Lattner | 7c306da | 2010-03-02 06:34:30 +0000 | [diff] [blame] | 752 | DEBUG(errs() << "=> "; Result->dump(CurDAG); errs() << "\n"); |
Anton Korobeynikov | 0a42d2b | 2009-07-16 14:14:33 +0000 | [diff] [blame] | 753 | } |
| 754 | |
| 755 | // Copy the remainder (even subreg) result, if it is needed. |
Dan Gohman | eeb3a00 | 2010-01-05 01:24:18 +0000 | [diff] [blame] | 756 | if (!SDValue(Node, 1).use_empty()) { |
Anton Korobeynikov | 8bd0db7 | 2009-07-16 14:18:17 +0000 | [diff] [blame] | 757 | unsigned SubRegIdx = (is32Bit ? subreg_even32 : subreg_even); |
Chris Lattner | 518bb53 | 2010-02-09 19:54:29 +0000 | [diff] [blame] | 758 | SDNode *Rem = CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG, |
Dan Gohman | 602b0c8 | 2009-09-25 18:54:59 +0000 | [diff] [blame] | 759 | dl, NVT, |
| 760 | SDValue(Result, 0), |
| 761 | CurDAG->getTargetConstant(SubRegIdx, |
| 762 | MVT::i32)); |
Dan Gohman | eeb3a00 | 2010-01-05 01:24:18 +0000 | [diff] [blame] | 763 | ReplaceUses(SDValue(Node, 1), SDValue(Rem, 0)); |
Chris Lattner | 7c306da | 2010-03-02 06:34:30 +0000 | [diff] [blame] | 764 | DEBUG(errs() << "=> "; Result->dump(CurDAG); errs() << "\n"); |
Anton Korobeynikov | 0a42d2b | 2009-07-16 14:14:33 +0000 | [diff] [blame] | 765 | } |
| 766 | |
Anton Korobeynikov | 0a42d2b | 2009-07-16 14:14:33 +0000 | [diff] [blame] | 767 | return NULL; |
| 768 | } |
| 769 | } |
Anton Korobeynikov | 4403b93 | 2009-07-16 13:27:25 +0000 | [diff] [blame] | 770 | |
| 771 | // Select the default instruction |
Dan Gohman | eeb3a00 | 2010-01-05 01:24:18 +0000 | [diff] [blame] | 772 | SDNode *ResNode = SelectCode(Node); |
Anton Korobeynikov | 4403b93 | 2009-07-16 13:27:25 +0000 | [diff] [blame] | 773 | |
Chris Lattner | 7c306da | 2010-03-02 06:34:30 +0000 | [diff] [blame] | 774 | DEBUG(errs() << "=> "; |
Dan Gohman | eeb3a00 | 2010-01-05 01:24:18 +0000 | [diff] [blame] | 775 | if (ResNode == NULL || ResNode == Node) |
| 776 | Node->dump(CurDAG); |
Chris Lattner | 893e1c9 | 2009-08-23 06:49:22 +0000 | [diff] [blame] | 777 | else |
| 778 | ResNode->dump(CurDAG); |
| 779 | errs() << "\n"; |
| 780 | ); |
Anton Korobeynikov | 4403b93 | 2009-07-16 13:27:25 +0000 | [diff] [blame] | 781 | return ResNode; |
| 782 | } |