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