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