Anton Korobeynikov | 4403b93 | 2009-07-16 13:27:25 +0000 | [diff] [blame] | 1 | //==-- SystemZISelDAGToDAG.cpp - A dag to dag inst selector for SystemZ ---===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file defines an instruction selector for the SystemZ target. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "SystemZ.h" |
| 15 | #include "SystemZISelLowering.h" |
| 16 | #include "SystemZTargetMachine.h" |
| 17 | #include "llvm/DerivedTypes.h" |
| 18 | #include "llvm/Function.h" |
| 19 | #include "llvm/Intrinsics.h" |
| 20 | #include "llvm/CallingConv.h" |
| 21 | #include "llvm/Constants.h" |
| 22 | #include "llvm/CodeGen/MachineFrameInfo.h" |
| 23 | #include "llvm/CodeGen/MachineFunction.h" |
| 24 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| 25 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
| 26 | #include "llvm/CodeGen/SelectionDAG.h" |
| 27 | #include "llvm/CodeGen/SelectionDAGISel.h" |
| 28 | #include "llvm/Target/TargetLowering.h" |
| 29 | #include "llvm/Support/Compiler.h" |
| 30 | #include "llvm/Support/Debug.h" |
| 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() { |
Anton Korobeynikov | 961bb6f | 2009-07-16 13:45:00 +0000 | [diff] [blame] | 56 | cerr << "SystemZRRIAddressMode " << this << '\n'; |
Anton Korobeynikov | 3360da9 | 2009-07-16 13:44:00 +0000 | [diff] [blame] | 57 | if (BaseType == RegBase) { |
| 58 | cerr << "Base.Reg "; |
| 59 | if (Base.Reg.getNode() != 0) Base.Reg.getNode()->dump(); |
| 60 | else cerr << "nul"; |
Anton Korobeynikov | 961bb6f | 2009-07-16 13:45:00 +0000 | [diff] [blame] | 61 | cerr << '\n'; |
Anton Korobeynikov | 3360da9 | 2009-07-16 13:44:00 +0000 | [diff] [blame] | 62 | } else { |
Anton Korobeynikov | 961bb6f | 2009-07-16 13:45:00 +0000 | [diff] [blame] | 63 | cerr << " Base.FrameIndex " << Base.FrameIndex << '\n'; |
Anton Korobeynikov | 3360da9 | 2009-07-16 13:44:00 +0000 | [diff] [blame] | 64 | } |
Anton Korobeynikov | 1ed1e3e | 2009-07-16 14:10:17 +0000 | [diff] [blame] | 65 | if (!isRI) { |
| 66 | cerr << "IndexReg "; |
| 67 | if (IndexReg.getNode() != 0) IndexReg.getNode()->dump(); |
| 68 | else cerr << "nul"; |
| 69 | } |
Anton Korobeynikov | 961bb6f | 2009-07-16 13:45:00 +0000 | [diff] [blame] | 70 | cerr << " Disp " << Disp << '\n'; |
Anton Korobeynikov | 3360da9 | 2009-07-16 13:44:00 +0000 | [diff] [blame] | 71 | } |
| 72 | }; |
| 73 | } |
| 74 | |
Anton Korobeynikov | 4403b93 | 2009-07-16 13:27:25 +0000 | [diff] [blame] | 75 | /// SystemZDAGToDAGISel - SystemZ specific code to select SystemZ machine |
| 76 | /// instructions for SelectionDAG operations. |
| 77 | /// |
| 78 | namespace { |
| 79 | class SystemZDAGToDAGISel : public SelectionDAGISel { |
| 80 | SystemZTargetLowering &Lowering; |
| 81 | const SystemZSubtarget &Subtarget; |
| 82 | |
Anton Korobeynikov | 1ed1e3e | 2009-07-16 14:10:17 +0000 | [diff] [blame] | 83 | void getAddressOperandsRI(const SystemZRRIAddressMode &AM, |
| 84 | SDValue &Base, SDValue &Disp); |
Anton Korobeynikov | 720e3b0 | 2009-07-16 14:09:35 +0000 | [diff] [blame] | 85 | void getAddressOperands(const SystemZRRIAddressMode &AM, |
| 86 | SDValue &Base, SDValue &Disp, |
| 87 | SDValue &Index); |
| 88 | |
Anton Korobeynikov | 4403b93 | 2009-07-16 13:27:25 +0000 | [diff] [blame] | 89 | public: |
| 90 | SystemZDAGToDAGISel(SystemZTargetMachine &TM, CodeGenOpt::Level OptLevel) |
| 91 | : SelectionDAGISel(TM, OptLevel), |
| 92 | Lowering(*TM.getTargetLowering()), |
| 93 | Subtarget(*TM.getSubtargetImpl()) { } |
| 94 | |
| 95 | virtual void InstructionSelect(); |
| 96 | |
| 97 | virtual const char *getPassName() const { |
| 98 | return "SystemZ DAG->DAG Pattern Instruction Selection"; |
| 99 | } |
| 100 | |
Anton Korobeynikov | 89edcd0 | 2009-07-16 13:33:57 +0000 | [diff] [blame] | 101 | /// getI16Imm - Return a target constant with the specified value, of type |
| 102 | /// i16. |
| 103 | inline SDValue getI16Imm(uint64_t Imm) { |
| 104 | return CurDAG->getTargetConstant(Imm, MVT::i16); |
| 105 | } |
| 106 | |
Anton Korobeynikov | da308c9 | 2009-07-16 13:34:50 +0000 | [diff] [blame] | 107 | /// getI32Imm - Return a target constant with the specified value, of type |
| 108 | /// i32. |
| 109 | inline SDValue getI32Imm(uint64_t Imm) { |
| 110 | return CurDAG->getTargetConstant(Imm, MVT::i32); |
| 111 | } |
| 112 | |
Anton Korobeynikov | 4403b93 | 2009-07-16 13:27:25 +0000 | [diff] [blame] | 113 | // Include the pieces autogenerated from the target description. |
Anton Korobeynikov | 89edcd0 | 2009-07-16 13:33:57 +0000 | [diff] [blame] | 114 | #include "SystemZGenDAGISel.inc" |
Anton Korobeynikov | 4403b93 | 2009-07-16 13:27:25 +0000 | [diff] [blame] | 115 | |
| 116 | private: |
Anton Korobeynikov | 014d463 | 2009-07-16 14:13:24 +0000 | [diff] [blame^] | 117 | bool SelectAddrRI12Only(SDValue Op, SDValue& Addr, |
| 118 | SDValue &Base, SDValue &Disp); |
Anton Korobeynikov | 1ed1e3e | 2009-07-16 14:10:17 +0000 | [diff] [blame] | 119 | bool SelectAddrRI12(SDValue Op, SDValue& Addr, |
Anton Korobeynikov | 014d463 | 2009-07-16 14:13:24 +0000 | [diff] [blame^] | 120 | SDValue &Base, SDValue &Disp, |
| 121 | bool is12BitOnly = false); |
Anton Korobeynikov | 1ed1e3e | 2009-07-16 14:10:17 +0000 | [diff] [blame] | 122 | bool SelectAddrRI(SDValue Op, SDValue& Addr, |
Anton Korobeynikov | 9e4816e | 2009-07-16 13:43:18 +0000 | [diff] [blame] | 123 | SDValue &Base, SDValue &Disp); |
Anton Korobeynikov | 720e3b0 | 2009-07-16 14:09:35 +0000 | [diff] [blame] | 124 | bool SelectAddrRRI12(SDValue Op, SDValue Addr, |
| 125 | SDValue &Base, SDValue &Disp, SDValue &Index); |
| 126 | bool SelectAddrRRI20(SDValue Op, SDValue Addr, |
| 127 | SDValue &Base, SDValue &Disp, SDValue &Index); |
Anton Korobeynikov | c4368a1 | 2009-07-16 13:48:42 +0000 | [diff] [blame] | 128 | bool SelectLAAddr(SDValue Op, SDValue Addr, |
| 129 | SDValue &Base, SDValue &Disp, SDValue &Index); |
| 130 | |
| 131 | SDNode *Select(SDValue Op); |
Anton Korobeynikov | 720e3b0 | 2009-07-16 14:09:35 +0000 | [diff] [blame] | 132 | bool MatchAddress(SDValue N, SystemZRRIAddressMode &AM, |
| 133 | bool is12Bit, unsigned Depth = 0); |
Anton Korobeynikov | 3360da9 | 2009-07-16 13:44:00 +0000 | [diff] [blame] | 134 | bool MatchAddressBase(SDValue N, SystemZRRIAddressMode &AM); |
Anton Korobeynikov | 1ed1e3e | 2009-07-16 14:10:17 +0000 | [diff] [blame] | 135 | bool MatchAddressRI(SDValue N, SystemZRRIAddressMode &AM, |
| 136 | bool is12Bit); |
Anton Korobeynikov | 4403b93 | 2009-07-16 13:27:25 +0000 | [diff] [blame] | 137 | |
| 138 | #ifndef NDEBUG |
| 139 | unsigned Indent; |
| 140 | #endif |
| 141 | }; |
| 142 | } // end anonymous namespace |
| 143 | |
| 144 | /// createSystemZISelDag - This pass converts a legalized DAG into a |
| 145 | /// SystemZ-specific DAG, ready for instruction scheduling. |
| 146 | /// |
| 147 | FunctionPass *llvm::createSystemZISelDag(SystemZTargetMachine &TM, |
| 148 | CodeGenOpt::Level OptLevel) { |
| 149 | return new SystemZDAGToDAGISel(TM, OptLevel); |
| 150 | } |
| 151 | |
Anton Korobeynikov | 9e4816e | 2009-07-16 13:43:18 +0000 | [diff] [blame] | 152 | /// isImmSExt20 - This method tests to see if the node is either a 32-bit |
| 153 | /// or 64-bit immediate, and if the value can be accurately represented as a |
| 154 | /// sign extension from a 20-bit value. If so, this returns true and the |
| 155 | /// immediate. |
Anton Korobeynikov | 3240740 | 2009-07-16 13:48:23 +0000 | [diff] [blame] | 156 | static bool isImmSExt20(int64_t Val, int64_t &Imm) { |
Anton Korobeynikov | 3360da9 | 2009-07-16 13:44:00 +0000 | [diff] [blame] | 157 | if (Val >= -524288 && Val <= 524287) { |
Anton Korobeynikov | 3240740 | 2009-07-16 13:48:23 +0000 | [diff] [blame] | 158 | Imm = Val; |
Anton Korobeynikov | 3360da9 | 2009-07-16 13:44:00 +0000 | [diff] [blame] | 159 | return true; |
| 160 | } |
| 161 | return false; |
| 162 | } |
| 163 | |
Anton Korobeynikov | 720e3b0 | 2009-07-16 14:09:35 +0000 | [diff] [blame] | 164 | /// 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] | 165 | /// or 64-bit immediate, and if the value can be accurately represented as a |
| 166 | /// zero extension from a 12-bit value. If so, this returns true and the |
| 167 | /// immediate. |
Anton Korobeynikov | 720e3b0 | 2009-07-16 14:09:35 +0000 | [diff] [blame] | 168 | static bool isImmZExt12(int64_t Val, int64_t &Imm) { |
| 169 | if (Val >= 0 && Val <= 0xFFF) { |
Anton Korobeynikov | 3166a9a | 2009-07-16 14:03:41 +0000 | [diff] [blame] | 170 | Imm = Val; |
| 171 | return true; |
| 172 | } |
Anton Korobeynikov | 3166a9a | 2009-07-16 14:03:41 +0000 | [diff] [blame] | 173 | return false; |
| 174 | } |
| 175 | |
Anton Korobeynikov | 3360da9 | 2009-07-16 13:44:00 +0000 | [diff] [blame] | 176 | /// MatchAddress - Add the specified node to the specified addressing mode, |
| 177 | /// returning true if it cannot be done. This just pattern matches for the |
| 178 | /// addressing mode. |
| 179 | bool SystemZDAGToDAGISel::MatchAddress(SDValue N, SystemZRRIAddressMode &AM, |
Anton Korobeynikov | 720e3b0 | 2009-07-16 14:09:35 +0000 | [diff] [blame] | 180 | bool is12Bit, unsigned Depth) { |
Anton Korobeynikov | 3360da9 | 2009-07-16 13:44:00 +0000 | [diff] [blame] | 181 | DebugLoc dl = N.getDebugLoc(); |
| 182 | DOUT << "MatchAddress: "; DEBUG(AM.dump()); |
| 183 | // Limit recursion. |
| 184 | if (Depth > 5) |
| 185 | return MatchAddressBase(N, AM); |
| 186 | |
Anton Korobeynikov | dc28955 | 2009-07-16 13:44:30 +0000 | [diff] [blame] | 187 | // FIXME: We can perform better here. If we have something like |
| 188 | // (shift (add A, imm), N), we can try to reassociate stuff and fold shift of |
| 189 | // imm into addressing mode. |
Anton Korobeynikov | 3360da9 | 2009-07-16 13:44:00 +0000 | [diff] [blame] | 190 | switch (N.getOpcode()) { |
| 191 | default: break; |
| 192 | case ISD::Constant: { |
Anton Korobeynikov | 3240740 | 2009-07-16 13:48:23 +0000 | [diff] [blame] | 193 | int64_t Val = cast<ConstantSDNode>(N)->getSExtValue(); |
| 194 | int64_t Imm; |
Anton Korobeynikov | 720e3b0 | 2009-07-16 14:09:35 +0000 | [diff] [blame] | 195 | bool Match = (is12Bit ? |
| 196 | isImmZExt12(AM.Disp + Val, Imm) : |
| 197 | isImmSExt20(AM.Disp + Val, Imm)); |
| 198 | if (Match) { |
Anton Korobeynikov | 3360da9 | 2009-07-16 13:44:00 +0000 | [diff] [blame] | 199 | AM.Disp = Imm; |
| 200 | return false; |
| 201 | } |
| 202 | break; |
| 203 | } |
| 204 | |
| 205 | case ISD::FrameIndex: |
Anton Korobeynikov | 720e3b0 | 2009-07-16 14:09:35 +0000 | [diff] [blame] | 206 | if (AM.BaseType == SystemZRRIAddressMode::RegBase && |
| 207 | AM.Base.Reg.getNode() == 0) { |
Anton Korobeynikov | 3360da9 | 2009-07-16 13:44:00 +0000 | [diff] [blame] | 208 | AM.BaseType = SystemZRRIAddressMode::FrameIndexBase; |
| 209 | AM.Base.FrameIndex = cast<FrameIndexSDNode>(N)->getIndex(); |
| 210 | return false; |
| 211 | } |
| 212 | break; |
| 213 | |
| 214 | case ISD::SUB: { |
| 215 | // Given A-B, if A can be completely folded into the address and |
| 216 | // the index field with the index field unused, use -B as the index. |
| 217 | // This is a win if a has multiple parts that can be folded into |
| 218 | // the address. Also, this saves a mov if the base register has |
| 219 | // other uses, since it avoids a two-address sub instruction, however |
| 220 | // it costs an additional mov if the index register has other uses. |
| 221 | |
| 222 | // Test if the LHS of the sub can be folded. |
| 223 | SystemZRRIAddressMode Backup = AM; |
Anton Korobeynikov | 720e3b0 | 2009-07-16 14:09:35 +0000 | [diff] [blame] | 224 | if (MatchAddress(N.getNode()->getOperand(0), AM, is12Bit, Depth+1)) { |
Anton Korobeynikov | 3360da9 | 2009-07-16 13:44:00 +0000 | [diff] [blame] | 225 | AM = Backup; |
| 226 | break; |
| 227 | } |
| 228 | // Test if the index field is free for use. |
Anton Korobeynikov | 1ed1e3e | 2009-07-16 14:10:17 +0000 | [diff] [blame] | 229 | if (AM.IndexReg.getNode() && !AM.isRI) { |
Anton Korobeynikov | 3360da9 | 2009-07-16 13:44:00 +0000 | [diff] [blame] | 230 | AM = Backup; |
| 231 | break; |
| 232 | } |
| 233 | |
| 234 | // If the base is a register with multiple uses, this transformation may |
| 235 | // save a mov. Otherwise it's probably better not to do it. |
| 236 | if (AM.BaseType == SystemZRRIAddressMode::RegBase && |
| 237 | (!AM.Base.Reg.getNode() || AM.Base.Reg.getNode()->hasOneUse())) { |
| 238 | AM = Backup; |
| 239 | break; |
| 240 | } |
| 241 | |
| 242 | // Ok, the transformation is legal and appears profitable. Go for it. |
| 243 | SDValue RHS = N.getNode()->getOperand(1); |
| 244 | SDValue Zero = CurDAG->getConstant(0, N.getValueType()); |
| 245 | SDValue Neg = CurDAG->getNode(ISD::SUB, dl, N.getValueType(), Zero, RHS); |
| 246 | AM.IndexReg = Neg; |
| 247 | |
| 248 | // Insert the new nodes into the topological ordering. |
| 249 | if (Zero.getNode()->getNodeId() == -1 || |
| 250 | Zero.getNode()->getNodeId() > N.getNode()->getNodeId()) { |
| 251 | CurDAG->RepositionNode(N.getNode(), Zero.getNode()); |
| 252 | Zero.getNode()->setNodeId(N.getNode()->getNodeId()); |
| 253 | } |
| 254 | if (Neg.getNode()->getNodeId() == -1 || |
| 255 | Neg.getNode()->getNodeId() > N.getNode()->getNodeId()) { |
| 256 | CurDAG->RepositionNode(N.getNode(), Neg.getNode()); |
| 257 | Neg.getNode()->setNodeId(N.getNode()->getNodeId()); |
| 258 | } |
| 259 | return false; |
| 260 | } |
| 261 | |
| 262 | case ISD::ADD: { |
| 263 | SystemZRRIAddressMode Backup = AM; |
Anton Korobeynikov | 720e3b0 | 2009-07-16 14:09:35 +0000 | [diff] [blame] | 264 | if (!MatchAddress(N.getNode()->getOperand(0), AM, is12Bit, Depth+1) && |
| 265 | !MatchAddress(N.getNode()->getOperand(1), AM, is12Bit, Depth+1)) |
Anton Korobeynikov | 3360da9 | 2009-07-16 13:44:00 +0000 | [diff] [blame] | 266 | return false; |
| 267 | AM = Backup; |
Anton Korobeynikov | 720e3b0 | 2009-07-16 14:09:35 +0000 | [diff] [blame] | 268 | if (!MatchAddress(N.getNode()->getOperand(1), AM, is12Bit, Depth+1) && |
| 269 | !MatchAddress(N.getNode()->getOperand(0), AM, is12Bit, Depth+1)) |
Anton Korobeynikov | 3360da9 | 2009-07-16 13:44:00 +0000 | [diff] [blame] | 270 | return false; |
| 271 | AM = Backup; |
| 272 | |
| 273 | // If we couldn't fold both operands into the address at the same time, |
| 274 | // see if we can just put each operand into a register and fold at least |
| 275 | // the add. |
Anton Korobeynikov | 1ed1e3e | 2009-07-16 14:10:17 +0000 | [diff] [blame] | 276 | if (!AM.isRI && |
| 277 | AM.BaseType == SystemZRRIAddressMode::RegBase && |
Anton Korobeynikov | 3360da9 | 2009-07-16 13:44:00 +0000 | [diff] [blame] | 278 | !AM.Base.Reg.getNode() && !AM.IndexReg.getNode()) { |
| 279 | AM.Base.Reg = N.getNode()->getOperand(0); |
| 280 | AM.IndexReg = N.getNode()->getOperand(1); |
| 281 | return false; |
| 282 | } |
| 283 | break; |
| 284 | } |
| 285 | |
| 286 | case ISD::OR: |
| 287 | // Handle "X | C" as "X + C" iff X is known to have C bits clear. |
| 288 | if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N.getOperand(1))) { |
| 289 | SystemZRRIAddressMode Backup = AM; |
Anton Korobeynikov | 3240740 | 2009-07-16 13:48:23 +0000 | [diff] [blame] | 290 | int64_t Offset = CN->getSExtValue(); |
| 291 | int64_t Imm; |
Anton Korobeynikov | 720e3b0 | 2009-07-16 14:09:35 +0000 | [diff] [blame] | 292 | bool MatchOffset = (is12Bit ? |
| 293 | isImmZExt12(AM.Disp + Offset, Imm) : |
| 294 | isImmSExt20(AM.Disp + Offset, Imm)); |
| 295 | // The resultant disp must fit in 12 or 20-bits. |
| 296 | if (MatchOffset && |
| 297 | // LHS should be an addr mode. |
| 298 | !MatchAddress(N.getOperand(0), AM, is12Bit, Depth+1) && |
Anton Korobeynikov | 3360da9 | 2009-07-16 13:44:00 +0000 | [diff] [blame] | 299 | // Check to see if the LHS & C is zero. |
| 300 | CurDAG->MaskedValueIsZero(N.getOperand(0), CN->getAPIntValue())) { |
| 301 | AM.Disp = Imm; |
| 302 | return false; |
| 303 | } |
| 304 | AM = Backup; |
| 305 | } |
| 306 | break; |
| 307 | } |
| 308 | |
| 309 | return MatchAddressBase(N, AM); |
| 310 | } |
| 311 | |
| 312 | /// MatchAddressBase - Helper for MatchAddress. Add the specified node to the |
| 313 | /// specified addressing mode without any further recursion. |
| 314 | bool SystemZDAGToDAGISel::MatchAddressBase(SDValue N, |
| 315 | SystemZRRIAddressMode &AM) { |
| 316 | // Is the base register already occupied? |
| 317 | if (AM.BaseType != SystemZRRIAddressMode::RegBase || AM.Base.Reg.getNode()) { |
Anton Korobeynikov | 4656760 | 2009-07-16 14:10:35 +0000 | [diff] [blame] | 318 | // If so, check to see if the index register is set. |
Anton Korobeynikov | 1ed1e3e | 2009-07-16 14:10:17 +0000 | [diff] [blame] | 319 | if (AM.IndexReg.getNode() == 0 && !AM.isRI) { |
Anton Korobeynikov | 3360da9 | 2009-07-16 13:44:00 +0000 | [diff] [blame] | 320 | AM.IndexReg = N; |
| 321 | return false; |
| 322 | } |
| 323 | |
| 324 | // Otherwise, we cannot select it. |
| 325 | return true; |
| 326 | } |
| 327 | |
| 328 | // Default, generate it as a register. |
| 329 | AM.BaseType = SystemZRRIAddressMode::RegBase; |
| 330 | AM.Base.Reg = N; |
| 331 | return false; |
| 332 | } |
| 333 | |
Anton Korobeynikov | 1ed1e3e | 2009-07-16 14:10:17 +0000 | [diff] [blame] | 334 | void SystemZDAGToDAGISel::getAddressOperandsRI(const SystemZRRIAddressMode &AM, |
| 335 | SDValue &Base, SDValue &Disp) { |
Anton Korobeynikov | 720e3b0 | 2009-07-16 14:09:35 +0000 | [diff] [blame] | 336 | if (AM.BaseType == SystemZRRIAddressMode::RegBase) |
| 337 | Base = AM.Base.Reg; |
| 338 | else |
| 339 | Base = CurDAG->getTargetFrameIndex(AM.Base.FrameIndex, TLI.getPointerTy()); |
Anton Korobeynikov | 720e3b0 | 2009-07-16 14:09:35 +0000 | [diff] [blame] | 340 | Disp = CurDAG->getTargetConstant(AM.Disp, MVT::i64); |
| 341 | } |
| 342 | |
Anton Korobeynikov | 1ed1e3e | 2009-07-16 14:10:17 +0000 | [diff] [blame] | 343 | void SystemZDAGToDAGISel::getAddressOperands(const SystemZRRIAddressMode &AM, |
| 344 | SDValue &Base, SDValue &Disp, |
| 345 | SDValue &Index) { |
| 346 | getAddressOperandsRI(AM, Base, Disp); |
| 347 | Index = AM.IndexReg; |
| 348 | } |
| 349 | |
| 350 | /// Returns true if the address can be represented by a base register plus |
| 351 | /// an unsigned 12-bit displacement [r+imm]. |
Anton Korobeynikov | 014d463 | 2009-07-16 14:13:24 +0000 | [diff] [blame^] | 352 | bool SystemZDAGToDAGISel::SelectAddrRI12Only(SDValue Op, SDValue& Addr, |
| 353 | SDValue &Base, SDValue &Disp) { |
| 354 | return SelectAddrRI12(Op, Addr, Base, Disp, /*is12BitOnly*/true); |
| 355 | } |
| 356 | |
Anton Korobeynikov | 1ed1e3e | 2009-07-16 14:10:17 +0000 | [diff] [blame] | 357 | bool SystemZDAGToDAGISel::SelectAddrRI12(SDValue Op, SDValue& Addr, |
Anton Korobeynikov | 014d463 | 2009-07-16 14:13:24 +0000 | [diff] [blame^] | 358 | SDValue &Base, SDValue &Disp, |
| 359 | bool is12BitOnly) { |
Anton Korobeynikov | 1ed1e3e | 2009-07-16 14:10:17 +0000 | [diff] [blame] | 360 | SystemZRRIAddressMode AM20(/*isRI*/true), AM12(/*isRI*/true); |
| 361 | bool Done = false; |
| 362 | |
| 363 | if (!Addr.hasOneUse()) { |
| 364 | unsigned Opcode = Addr.getOpcode(); |
| 365 | if (Opcode != ISD::Constant && Opcode != ISD::FrameIndex) { |
| 366 | // If we are able to fold N into addressing mode, then we'll allow it even |
| 367 | // if N has multiple uses. In general, addressing computation is used as |
| 368 | // addresses by all of its uses. But watch out for CopyToReg uses, that |
| 369 | // means the address computation is liveout. It will be computed by a LA |
| 370 | // so we want to avoid computing the address twice. |
| 371 | for (SDNode::use_iterator UI = Addr.getNode()->use_begin(), |
| 372 | UE = Addr.getNode()->use_end(); UI != UE; ++UI) { |
| 373 | if (UI->getOpcode() == ISD::CopyToReg) { |
| 374 | MatchAddressBase(Addr, AM12); |
| 375 | Done = true; |
| 376 | break; |
| 377 | } |
| 378 | } |
| 379 | } |
| 380 | } |
| 381 | if (!Done && MatchAddress(Addr, AM12, /* is12Bit */ true)) |
| 382 | return false; |
| 383 | |
| 384 | // Check, whether we can match stuff using 20-bit displacements |
Anton Korobeynikov | 014d463 | 2009-07-16 14:13:24 +0000 | [diff] [blame^] | 385 | if (!Done && !is12BitOnly && |
| 386 | !MatchAddress(Addr, AM20, /* is12Bit */ false)) |
Anton Korobeynikov | 1ed1e3e | 2009-07-16 14:10:17 +0000 | [diff] [blame] | 387 | if (AM12.Disp == 0 && AM20.Disp != 0) |
| 388 | return false; |
| 389 | |
| 390 | DOUT << "MatchAddress (final): "; DEBUG(AM12.dump()); |
| 391 | |
| 392 | MVT VT = Addr.getValueType(); |
| 393 | if (AM12.BaseType == SystemZRRIAddressMode::RegBase) { |
| 394 | if (!AM12.Base.Reg.getNode()) |
| 395 | AM12.Base.Reg = CurDAG->getRegister(0, VT); |
| 396 | } |
| 397 | |
| 398 | assert(AM12.IndexReg.getNode() == 0 && "Invalid reg-imm address mode!"); |
| 399 | |
| 400 | getAddressOperandsRI(AM12, Base, Disp); |
| 401 | |
| 402 | return true; |
| 403 | } |
| 404 | |
| 405 | /// Returns true if the address can be represented by a base register plus |
| 406 | /// a signed 20-bit displacement [r+imm]. |
| 407 | bool SystemZDAGToDAGISel::SelectAddrRI(SDValue Op, SDValue& Addr, |
| 408 | SDValue &Base, SDValue &Disp) { |
| 409 | SystemZRRIAddressMode AM(/*isRI*/true); |
| 410 | bool Done = false; |
| 411 | |
| 412 | if (!Addr.hasOneUse()) { |
| 413 | unsigned Opcode = Addr.getOpcode(); |
| 414 | if (Opcode != ISD::Constant && Opcode != ISD::FrameIndex) { |
| 415 | // If we are able to fold N into addressing mode, then we'll allow it even |
| 416 | // if N has multiple uses. In general, addressing computation is used as |
| 417 | // addresses by all of its uses. But watch out for CopyToReg uses, that |
| 418 | // means the address computation is liveout. It will be computed by a LA |
| 419 | // so we want to avoid computing the address twice. |
| 420 | for (SDNode::use_iterator UI = Addr.getNode()->use_begin(), |
| 421 | UE = Addr.getNode()->use_end(); UI != UE; ++UI) { |
| 422 | if (UI->getOpcode() == ISD::CopyToReg) { |
| 423 | MatchAddressBase(Addr, AM); |
| 424 | Done = true; |
| 425 | break; |
| 426 | } |
| 427 | } |
| 428 | } |
| 429 | } |
| 430 | if (!Done && MatchAddress(Addr, AM, /* is12Bit */ false)) |
| 431 | return false; |
| 432 | |
| 433 | DOUT << "MatchAddress (final): "; DEBUG(AM.dump()); |
| 434 | |
| 435 | MVT VT = Addr.getValueType(); |
| 436 | if (AM.BaseType == SystemZRRIAddressMode::RegBase) { |
| 437 | if (!AM.Base.Reg.getNode()) |
| 438 | AM.Base.Reg = CurDAG->getRegister(0, VT); |
| 439 | } |
| 440 | |
| 441 | assert(AM.IndexReg.getNode() == 0 && "Invalid reg-imm address mode!"); |
| 442 | |
| 443 | getAddressOperandsRI(AM, Base, Disp); |
| 444 | |
| 445 | return true; |
| 446 | } |
| 447 | |
Anton Korobeynikov | 720e3b0 | 2009-07-16 14:09:35 +0000 | [diff] [blame] | 448 | /// Returns true if the address can be represented by a base register plus |
| 449 | /// index register plus an unsigned 12-bit displacement [base + idx + imm]. |
| 450 | bool SystemZDAGToDAGISel::SelectAddrRRI12(SDValue Op, SDValue Addr, |
| 451 | SDValue &Base, SDValue &Disp, SDValue &Index) { |
Anton Korobeynikov | 4656760 | 2009-07-16 14:10:35 +0000 | [diff] [blame] | 452 | SystemZRRIAddressMode AM20, AM12; |
Anton Korobeynikov | 720e3b0 | 2009-07-16 14:09:35 +0000 | [diff] [blame] | 453 | bool Done = false; |
| 454 | |
| 455 | if (!Addr.hasOneUse()) { |
| 456 | unsigned Opcode = Addr.getOpcode(); |
| 457 | if (Opcode != ISD::Constant && Opcode != ISD::FrameIndex) { |
| 458 | // If we are able to fold N into addressing mode, then we'll allow it even |
| 459 | // if N has multiple uses. In general, addressing computation is used as |
| 460 | // addresses by all of its uses. But watch out for CopyToReg uses, that |
| 461 | // means the address computation is liveout. It will be computed by a LA |
| 462 | // so we want to avoid computing the address twice. |
| 463 | for (SDNode::use_iterator UI = Addr.getNode()->use_begin(), |
| 464 | UE = Addr.getNode()->use_end(); UI != UE; ++UI) { |
| 465 | if (UI->getOpcode() == ISD::CopyToReg) { |
| 466 | MatchAddressBase(Addr, AM12); |
| 467 | Done = true; |
| 468 | break; |
| 469 | } |
| 470 | } |
| 471 | } |
| 472 | } |
| 473 | if (!Done && MatchAddress(Addr, AM12, /* is12Bit */ true)) |
| 474 | return false; |
| 475 | |
| 476 | // Check, whether we can match stuff using 20-bit displacements |
| 477 | if (!Done && !MatchAddress(Addr, AM20, /* is12Bit */ false)) |
| 478 | if (AM12.Disp == 0 && AM20.Disp != 0) |
| 479 | return false; |
| 480 | |
| 481 | DOUT << "MatchAddress (final): "; DEBUG(AM12.dump()); |
| 482 | |
| 483 | MVT VT = Addr.getValueType(); |
| 484 | if (AM12.BaseType == SystemZRRIAddressMode::RegBase) { |
| 485 | if (!AM12.Base.Reg.getNode()) |
| 486 | AM12.Base.Reg = CurDAG->getRegister(0, VT); |
| 487 | } |
| 488 | |
| 489 | if (!AM12.IndexReg.getNode()) |
| 490 | AM12.IndexReg = CurDAG->getRegister(0, VT); |
| 491 | |
| 492 | getAddressOperands(AM12, Base, Disp, Index); |
| 493 | |
| 494 | return true; |
| 495 | } |
| 496 | |
Anton Korobeynikov | 3360da9 | 2009-07-16 13:44:00 +0000 | [diff] [blame] | 497 | /// Returns true if the address can be represented by a base register plus |
| 498 | /// index register plus a signed 20-bit displacement [base + idx + imm]. |
Anton Korobeynikov | 720e3b0 | 2009-07-16 14:09:35 +0000 | [diff] [blame] | 499 | bool SystemZDAGToDAGISel::SelectAddrRRI20(SDValue Op, SDValue Addr, |
Anton Korobeynikov | c4368a1 | 2009-07-16 13:48:42 +0000 | [diff] [blame] | 500 | SDValue &Base, SDValue &Disp, SDValue &Index) { |
Anton Korobeynikov | 3360da9 | 2009-07-16 13:44:00 +0000 | [diff] [blame] | 501 | SystemZRRIAddressMode AM; |
| 502 | bool Done = false; |
| 503 | |
Anton Korobeynikov | 711d5b6 | 2009-07-16 13:47:59 +0000 | [diff] [blame] | 504 | if (!Addr.hasOneUse()) { |
| 505 | unsigned Opcode = Addr.getOpcode(); |
| 506 | if (Opcode != ISD::Constant && Opcode != ISD::FrameIndex) { |
| 507 | // If we are able to fold N into addressing mode, then we'll allow it even |
| 508 | // if N has multiple uses. In general, addressing computation is used as |
| 509 | // addresses by all of its uses. But watch out for CopyToReg uses, that |
| 510 | // means the address computation is liveout. It will be computed by a LA |
| 511 | // so we want to avoid computing the address twice. |
| 512 | for (SDNode::use_iterator UI = Addr.getNode()->use_begin(), |
| 513 | UE = Addr.getNode()->use_end(); UI != UE; ++UI) { |
| 514 | if (UI->getOpcode() == ISD::CopyToReg) { |
| 515 | MatchAddressBase(Addr, AM); |
| 516 | Done = true; |
| 517 | break; |
| 518 | } |
| 519 | } |
| 520 | } |
| 521 | } |
Anton Korobeynikov | 720e3b0 | 2009-07-16 14:09:35 +0000 | [diff] [blame] | 522 | if (!Done && MatchAddress(Addr, AM, /* is12Bit */ false)) |
Anton Korobeynikov | 3360da9 | 2009-07-16 13:44:00 +0000 | [diff] [blame] | 523 | return false; |
| 524 | |
Anton Korobeynikov | 3240740 | 2009-07-16 13:48:23 +0000 | [diff] [blame] | 525 | DOUT << "MatchAddress (final): "; DEBUG(AM.dump()); |
| 526 | |
Anton Korobeynikov | 3360da9 | 2009-07-16 13:44:00 +0000 | [diff] [blame] | 527 | MVT VT = Addr.getValueType(); |
| 528 | if (AM.BaseType == SystemZRRIAddressMode::RegBase) { |
| 529 | if (!AM.Base.Reg.getNode()) |
| 530 | AM.Base.Reg = CurDAG->getRegister(0, VT); |
| 531 | } |
| 532 | |
| 533 | if (!AM.IndexReg.getNode()) |
| 534 | AM.IndexReg = CurDAG->getRegister(0, VT); |
| 535 | |
Anton Korobeynikov | 720e3b0 | 2009-07-16 14:09:35 +0000 | [diff] [blame] | 536 | getAddressOperands(AM, Base, Disp, Index); |
Anton Korobeynikov | 3360da9 | 2009-07-16 13:44:00 +0000 | [diff] [blame] | 537 | |
| 538 | return true; |
| 539 | } |
Anton Korobeynikov | 4403b93 | 2009-07-16 13:27:25 +0000 | [diff] [blame] | 540 | |
Anton Korobeynikov | 711d5b6 | 2009-07-16 13:47:59 +0000 | [diff] [blame] | 541 | /// SelectLAAddr - it calls SelectAddr and determines if the maximal addressing |
| 542 | /// mode it matches can be cost effectively emitted as an LA/LAY instruction. |
| 543 | bool SystemZDAGToDAGISel::SelectLAAddr(SDValue Op, SDValue Addr, |
Anton Korobeynikov | c4368a1 | 2009-07-16 13:48:42 +0000 | [diff] [blame] | 544 | SDValue &Base, SDValue &Disp, SDValue &Index) { |
Anton Korobeynikov | 711d5b6 | 2009-07-16 13:47:59 +0000 | [diff] [blame] | 545 | SystemZRRIAddressMode AM; |
| 546 | |
Anton Korobeynikov | 720e3b0 | 2009-07-16 14:09:35 +0000 | [diff] [blame] | 547 | if (MatchAddress(Addr, AM, false)) |
Anton Korobeynikov | 711d5b6 | 2009-07-16 13:47:59 +0000 | [diff] [blame] | 548 | return false; |
| 549 | |
| 550 | MVT VT = Addr.getValueType(); |
| 551 | unsigned Complexity = 0; |
| 552 | if (AM.BaseType == SystemZRRIAddressMode::RegBase) |
| 553 | if (AM.Base.Reg.getNode()) |
| 554 | Complexity = 1; |
| 555 | else |
| 556 | AM.Base.Reg = CurDAG->getRegister(0, VT); |
| 557 | else if (AM.BaseType == SystemZRRIAddressMode::FrameIndexBase) |
| 558 | Complexity = 4; |
| 559 | |
| 560 | if (AM.IndexReg.getNode()) |
| 561 | Complexity += 1; |
| 562 | else |
| 563 | AM.IndexReg = CurDAG->getRegister(0, VT); |
| 564 | |
| 565 | if (AM.Disp && (AM.Base.Reg.getNode() || AM.IndexReg.getNode())) |
| 566 | Complexity += 1; |
| 567 | |
| 568 | if (Complexity > 2) { |
Anton Korobeynikov | 720e3b0 | 2009-07-16 14:09:35 +0000 | [diff] [blame] | 569 | getAddressOperands(AM, Base, Disp, Index); |
Anton Korobeynikov | 711d5b6 | 2009-07-16 13:47:59 +0000 | [diff] [blame] | 570 | return true; |
| 571 | } |
| 572 | |
| 573 | return false; |
| 574 | } |
| 575 | |
Anton Korobeynikov | 4403b93 | 2009-07-16 13:27:25 +0000 | [diff] [blame] | 576 | /// InstructionSelect - This callback is invoked by |
| 577 | /// SelectionDAGISel when it has created a SelectionDAG for us to codegen. |
| 578 | void SystemZDAGToDAGISel::InstructionSelect() { |
| 579 | DEBUG(BB->dump()); |
| 580 | |
| 581 | // Codegen the basic block. |
| 582 | #ifndef NDEBUG |
| 583 | DOUT << "===== Instruction selection begins:\n"; |
| 584 | Indent = 0; |
| 585 | #endif |
| 586 | SelectRoot(*CurDAG); |
| 587 | #ifndef NDEBUG |
| 588 | DOUT << "===== Instruction selection ends:\n"; |
| 589 | #endif |
| 590 | |
| 591 | CurDAG->RemoveDeadNodes(); |
| 592 | } |
| 593 | |
| 594 | SDNode *SystemZDAGToDAGISel::Select(SDValue Op) { |
| 595 | SDNode *Node = Op.getNode(); |
| 596 | DebugLoc dl = Op.getDebugLoc(); |
| 597 | |
| 598 | // Dump information about the Node being selected |
| 599 | #ifndef NDEBUG |
| 600 | DOUT << std::string(Indent, ' ') << "Selecting: "; |
| 601 | DEBUG(Node->dump(CurDAG)); |
| 602 | DOUT << "\n"; |
| 603 | Indent += 2; |
| 604 | #endif |
| 605 | |
| 606 | // If we have a custom node, we already have selected! |
| 607 | if (Node->isMachineOpcode()) { |
| 608 | #ifndef NDEBUG |
| 609 | DOUT << std::string(Indent-2, ' ') << "== "; |
| 610 | DEBUG(Node->dump(CurDAG)); |
| 611 | DOUT << "\n"; |
| 612 | Indent -= 2; |
| 613 | #endif |
| 614 | return NULL; |
| 615 | } |
| 616 | |
| 617 | // Select the default instruction |
| 618 | SDNode *ResNode = SelectCode(Op); |
| 619 | |
| 620 | #ifndef NDEBUG |
| 621 | DOUT << std::string(Indent-2, ' ') << "=> "; |
| 622 | if (ResNode == NULL || ResNode == Op.getNode()) |
| 623 | DEBUG(Op.getNode()->dump(CurDAG)); |
| 624 | else |
| 625 | DEBUG(ResNode->dump(CurDAG)); |
| 626 | DOUT << "\n"; |
| 627 | Indent -= 2; |
| 628 | #endif |
| 629 | |
| 630 | return ResNode; |
| 631 | } |