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