[SystemZ] Use CLST to implement strcmp
[oota-llvm.git] / lib / Target / SystemZ / SystemZISelLowering.h
1 //===-- SystemZISelLowering.h - SystemZ DAG lowering interface --*- C++ -*-===//
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 the interfaces that SystemZ uses to lower LLVM code into a
11 // selection DAG.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_TARGET_SystemZ_ISELLOWERING_H
16 #define LLVM_TARGET_SystemZ_ISELLOWERING_H
17
18 #include "SystemZ.h"
19 #include "llvm/CodeGen/MachineBasicBlock.h"
20 #include "llvm/CodeGen/SelectionDAG.h"
21 #include "llvm/Target/TargetLowering.h"
22
23 namespace llvm {
24 namespace SystemZISD {
25   enum {
26     FIRST_NUMBER = ISD::BUILTIN_OP_END,
27
28     // Return with a flag operand.  Operand 0 is the chain operand.
29     RET_FLAG,
30
31     // Calls a function.  Operand 0 is the chain operand and operand 1
32     // is the target address.  The arguments start at operand 2.
33     // There is an optional glue operand at the end.
34     CALL,
35
36     // Wraps a TargetGlobalAddress that should be loaded using PC-relative
37     // accesses (LARL).  Operand 0 is the address.
38     PCREL_WRAPPER,
39
40     // Signed integer and floating-point comparisons.  The operands are the
41     // two values to compare.
42     CMP,
43
44     // Likewise unsigned integer comparison.
45     UCMP,
46
47     // Branches if a condition is true.  Operand 0 is the chain operand;
48     // operand 1 is the 4-bit condition-code mask, with bit N in
49     // big-endian order meaning "branch if CC=N"; operand 2 is the
50     // target block and operand 3 is the flag operand.
51     BR_CCMASK,
52
53     // Selects between operand 0 and operand 1.  Operand 2 is the
54     // mask of condition-code values for which operand 0 should be
55     // chosen over operand 1; it has the same form as BR_CCMASK.
56     // Operand 3 is the flag operand.
57     SELECT_CCMASK,
58
59     // Evaluates to the gap between the stack pointer and the
60     // base of the dynamically-allocatable area.
61     ADJDYNALLOC,
62
63     // Extracts the value of a 32-bit access register.  Operand 0 is
64     // the number of the register.
65     EXTRACT_ACCESS,
66
67     // Wrappers around the ISD opcodes of the same name.  The output and
68     // first input operands are GR128s.  The trailing numbers are the
69     // widths of the second operand in bits.
70     UMUL_LOHI64,
71     SDIVREM32,
72     SDIVREM64,
73     UDIVREM32,
74     UDIVREM64,
75
76     // Use MVC to copy bytes from one memory location to another.
77     // The first operand is the target address, the second operand is the
78     // source address, and the third operand is the constant length.
79     // This isn't a memory opcode because we'd need to attach two
80     // MachineMemOperands rather than one.
81     MVC,
82
83     // Use CLC to compare two blocks of memory, with the same comments
84     // as for MVC.
85     CLC,
86
87     // Use a CLST-based sequence to implement strcmp().  The two input operands
88     // are the addresses of the strings to compare.
89     STRCMP,
90
91     // Store the CC value in bits 29 and 28 of an integer.
92     IPM,
93
94     // Wrappers around the inner loop of an 8- or 16-bit ATOMIC_SWAP or
95     // ATOMIC_LOAD_<op>.
96     //
97     // Operand 0: the address of the containing 32-bit-aligned field
98     // Operand 1: the second operand of <op>, in the high bits of an i32
99     //            for everything except ATOMIC_SWAPW
100     // Operand 2: how many bits to rotate the i32 left to bring the first
101     //            operand into the high bits
102     // Operand 3: the negative of operand 2, for rotating the other way
103     // Operand 4: the width of the field in bits (8 or 16)
104     ATOMIC_SWAPW = ISD::FIRST_TARGET_MEMORY_OPCODE,
105     ATOMIC_LOADW_ADD,
106     ATOMIC_LOADW_SUB,
107     ATOMIC_LOADW_AND,
108     ATOMIC_LOADW_OR,
109     ATOMIC_LOADW_XOR,
110     ATOMIC_LOADW_NAND,
111     ATOMIC_LOADW_MIN,
112     ATOMIC_LOADW_MAX,
113     ATOMIC_LOADW_UMIN,
114     ATOMIC_LOADW_UMAX,
115
116     // A wrapper around the inner loop of an ATOMIC_CMP_SWAP.
117     //
118     // Operand 0: the address of the containing 32-bit-aligned field
119     // Operand 1: the compare value, in the low bits of an i32
120     // Operand 2: the swap value, in the low bits of an i32
121     // Operand 3: how many bits to rotate the i32 left to bring the first
122     //            operand into the high bits
123     // Operand 4: the negative of operand 2, for rotating the other way
124     // Operand 5: the width of the field in bits (8 or 16)
125     ATOMIC_CMP_SWAPW
126   };
127 }
128
129 class SystemZSubtarget;
130 class SystemZTargetMachine;
131
132 class SystemZTargetLowering : public TargetLowering {
133 public:
134   explicit SystemZTargetLowering(SystemZTargetMachine &TM);
135
136   // Override TargetLowering.
137   virtual MVT getScalarShiftAmountTy(EVT LHSTy) const LLVM_OVERRIDE {
138     return MVT::i32;
139   }
140   virtual EVT getSetCCResultType(LLVMContext &, EVT) const LLVM_OVERRIDE {
141     return MVT::i32;
142   }
143   virtual bool isFMAFasterThanFMulAndFAdd(EVT VT) const LLVM_OVERRIDE;
144   virtual bool isFPImmLegal(const APFloat &Imm, EVT VT) const LLVM_OVERRIDE;
145   virtual bool isLegalAddressingMode(const AddrMode &AM, Type *Ty) const
146      LLVM_OVERRIDE;
147   virtual bool allowsUnalignedMemoryAccesses(EVT VT, bool *Fast) const
148     LLVM_OVERRIDE;
149   virtual const char *getTargetNodeName(unsigned Opcode) const LLVM_OVERRIDE;
150   virtual std::pair<unsigned, const TargetRegisterClass *>
151     getRegForInlineAsmConstraint(const std::string &Constraint,
152                                  MVT VT) const LLVM_OVERRIDE;
153   virtual TargetLowering::ConstraintType
154     getConstraintType(const std::string &Constraint) const LLVM_OVERRIDE;
155   virtual TargetLowering::ConstraintWeight
156     getSingleConstraintMatchWeight(AsmOperandInfo &info,
157                                    const char *constraint) const LLVM_OVERRIDE;
158   virtual void
159     LowerAsmOperandForConstraint(SDValue Op,
160                                  std::string &Constraint,
161                                  std::vector<SDValue> &Ops,
162                                  SelectionDAG &DAG) const LLVM_OVERRIDE;
163   virtual MachineBasicBlock *
164     EmitInstrWithCustomInserter(MachineInstr *MI,
165                                 MachineBasicBlock *BB) const LLVM_OVERRIDE;
166   virtual SDValue LowerOperation(SDValue Op,
167                                  SelectionDAG &DAG) const LLVM_OVERRIDE;
168   virtual SDValue
169     LowerFormalArguments(SDValue Chain,
170                          CallingConv::ID CallConv, bool isVarArg,
171                          const SmallVectorImpl<ISD::InputArg> &Ins,
172                          SDLoc DL, SelectionDAG &DAG,
173                          SmallVectorImpl<SDValue> &InVals) const LLVM_OVERRIDE;
174   virtual SDValue
175     LowerCall(CallLoweringInfo &CLI,
176               SmallVectorImpl<SDValue> &InVals) const LLVM_OVERRIDE;
177
178   virtual SDValue
179     LowerReturn(SDValue Chain,
180                 CallingConv::ID CallConv, bool IsVarArg,
181                 const SmallVectorImpl<ISD::OutputArg> &Outs,
182                 const SmallVectorImpl<SDValue> &OutVals,
183                 SDLoc DL, SelectionDAG &DAG) const LLVM_OVERRIDE;
184
185 private:
186   const SystemZSubtarget &Subtarget;
187   const SystemZTargetMachine &TM;
188
189   // Implement LowerOperation for individual opcodes.
190   SDValue lowerBR_CC(SDValue Op, SelectionDAG &DAG) const;
191   SDValue lowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const;
192   SDValue lowerGlobalAddress(GlobalAddressSDNode *Node,
193                              SelectionDAG &DAG) const;
194   SDValue lowerGlobalTLSAddress(GlobalAddressSDNode *Node,
195                                 SelectionDAG &DAG) const;
196   SDValue lowerBlockAddress(BlockAddressSDNode *Node,
197                             SelectionDAG &DAG) const;
198   SDValue lowerJumpTable(JumpTableSDNode *JT, SelectionDAG &DAG) const;
199   SDValue lowerConstantPool(ConstantPoolSDNode *CP, SelectionDAG &DAG) const;
200   SDValue lowerVASTART(SDValue Op, SelectionDAG &DAG) const;
201   SDValue lowerVACOPY(SDValue Op, SelectionDAG &DAG) const;
202   SDValue lowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) const;
203   SDValue lowerUMUL_LOHI(SDValue Op, SelectionDAG &DAG) const;
204   SDValue lowerSDIVREM(SDValue Op, SelectionDAG &DAG) const;
205   SDValue lowerUDIVREM(SDValue Op, SelectionDAG &DAG) const;
206   SDValue lowerBITCAST(SDValue Op, SelectionDAG &DAG) const;
207   SDValue lowerOR(SDValue Op, SelectionDAG &DAG) const;
208   SDValue lowerATOMIC_LOAD(SDValue Op, SelectionDAG &DAG,
209                            unsigned Opcode) const;
210   SDValue lowerATOMIC_CMP_SWAP(SDValue Op, SelectionDAG &DAG) const;
211   SDValue lowerSTACKSAVE(SDValue Op, SelectionDAG &DAG) const;
212   SDValue lowerSTACKRESTORE(SDValue Op, SelectionDAG &DAG) const;
213
214   // If the last instruction before MBBI in MBB was some form of COMPARE,
215   // try to replace it with a COMPARE AND BRANCH just before MBBI.
216   // CCMask and Target are the BRC-like operands for the branch.
217   // Return true if the change was made.
218   bool convertPrevCompareToBranch(MachineBasicBlock *MBB,
219                                   MachineBasicBlock::iterator MBBI,
220                                   unsigned CCMask,
221                                   MachineBasicBlock *Target) const;
222
223   // Implement EmitInstrWithCustomInserter for individual operation types.
224   MachineBasicBlock *emitSelect(MachineInstr *MI,
225                                 MachineBasicBlock *BB) const;
226   MachineBasicBlock *emitCondStore(MachineInstr *MI,
227                                    MachineBasicBlock *BB,
228                                    unsigned StoreOpcode, unsigned STOCOpcode,
229                                    bool Invert) const;
230   MachineBasicBlock *emitExt128(MachineInstr *MI,
231                                 MachineBasicBlock *MBB,
232                                 bool ClearEven, unsigned SubReg) const;
233   MachineBasicBlock *emitAtomicLoadBinary(MachineInstr *MI,
234                                           MachineBasicBlock *BB,
235                                           unsigned BinOpcode, unsigned BitSize,
236                                           bool Invert = false) const;
237   MachineBasicBlock *emitAtomicLoadMinMax(MachineInstr *MI,
238                                           MachineBasicBlock *MBB,
239                                           unsigned CompareOpcode,
240                                           unsigned KeepOldMask,
241                                           unsigned BitSize) const;
242   MachineBasicBlock *emitAtomicCmpSwapW(MachineInstr *MI,
243                                         MachineBasicBlock *BB) const;
244   MachineBasicBlock *emitMemMemWrapper(MachineInstr *MI,
245                                        MachineBasicBlock *BB,
246                                        unsigned Opcode) const;
247   MachineBasicBlock *emitStringWrapper(MachineInstr *MI,
248                                        MachineBasicBlock *BB,
249                                        unsigned Opcode) const;
250 };
251 } // end namespace llvm
252
253 #endif // LLVM_TARGET_SystemZ_ISELLOWERING_H