2005-01-07 07:44:53 +00:00
|
|
|
//===-- TargetLowering.cpp - Implement the TargetLowering class -----------===//
|
2005-04-21 22:55:34 +00:00
|
|
|
//
|
2005-01-07 07:44:53 +00:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by the LLVM research group and is distributed under
|
|
|
|
// the University of Illinois Open Source License. See LICENSE.TXT for details.
|
2005-04-21 22:55:34 +00:00
|
|
|
//
|
2005-01-07 07:44:53 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This implements the TargetLowering class.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Target/TargetLowering.h"
|
|
|
|
#include "llvm/Target/TargetMachine.h"
|
2006-01-26 20:37:03 +00:00
|
|
|
#include "llvm/Target/MRegisterInfo.h"
|
2005-01-07 07:44:53 +00:00
|
|
|
#include "llvm/CodeGen/SelectionDAG.h"
|
2006-01-26 20:37:03 +00:00
|
|
|
#include "llvm/ADT/StringExtras.h"
|
2006-01-30 04:09:27 +00:00
|
|
|
#include "llvm/Support/MathExtras.h"
|
2005-01-07 07:44:53 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
TargetLowering::TargetLowering(TargetMachine &tm)
|
2006-01-29 08:41:12 +00:00
|
|
|
: TM(tm), TD(TM.getTargetData()) {
|
2005-01-07 07:44:53 +00:00
|
|
|
assert(ISD::BUILTIN_OP_END <= 128 &&
|
|
|
|
"Fixed size array in TargetLowering is not large enough!");
|
2005-01-16 07:28:11 +00:00
|
|
|
// All operations default to being supported.
|
|
|
|
memset(OpActions, 0, sizeof(OpActions));
|
2005-01-07 07:44:53 +00:00
|
|
|
|
|
|
|
IsLittleEndian = TD.isLittleEndian();
|
2005-01-16 23:59:48 +00:00
|
|
|
ShiftAmountTy = SetCCResultTy = PointerTy = getValueType(TD.getIntPtrType());
|
2005-01-19 03:36:14 +00:00
|
|
|
ShiftAmtHandling = Undefined;
|
2005-01-07 07:44:53 +00:00
|
|
|
memset(RegClassForVT, 0,MVT::LAST_VALUETYPE*sizeof(TargetRegisterClass*));
|
2005-08-27 19:09:02 +00:00
|
|
|
maxStoresPerMemSet = maxStoresPerMemCpy = maxStoresPerMemMove = 8;
|
|
|
|
allowUnalignedMemoryAccesses = false;
|
2005-09-27 22:13:56 +00:00
|
|
|
UseUnderscoreSetJmpLongJmp = false;
|
2005-10-21 00:02:42 +00:00
|
|
|
IntDivIsCheap = false;
|
|
|
|
Pow2DivIsCheap = false;
|
2006-01-25 18:57:15 +00:00
|
|
|
StackPointerRegisterToSaveRestore = 0;
|
2006-01-25 18:52:42 +00:00
|
|
|
SchedPreferenceInfo = SchedulingForLatency;
|
2005-01-07 07:44:53 +00:00
|
|
|
}
|
|
|
|
|
2005-01-16 07:28:11 +00:00
|
|
|
TargetLowering::~TargetLowering() {}
|
|
|
|
|
2005-01-16 01:10:58 +00:00
|
|
|
/// setValueTypeAction - Set the action for a particular value type. This
|
|
|
|
/// assumes an action has not already been set for this value type.
|
2005-01-16 07:28:11 +00:00
|
|
|
static void SetValueTypeAction(MVT::ValueType VT,
|
|
|
|
TargetLowering::LegalizeAction Action,
|
2005-01-16 01:10:58 +00:00
|
|
|
TargetLowering &TLI,
|
|
|
|
MVT::ValueType *TransformToType,
|
2006-01-29 08:41:12 +00:00
|
|
|
TargetLowering::ValueTypeActionImpl &ValueTypeActions) {
|
|
|
|
ValueTypeActions.setTypeAction(VT, Action);
|
2005-01-16 07:28:11 +00:00
|
|
|
if (Action == TargetLowering::Promote) {
|
2005-01-16 01:10:58 +00:00
|
|
|
MVT::ValueType PromoteTo;
|
|
|
|
if (VT == MVT::f32)
|
|
|
|
PromoteTo = MVT::f64;
|
|
|
|
else {
|
|
|
|
unsigned LargerReg = VT+1;
|
2005-08-24 16:34:12 +00:00
|
|
|
while (!TLI.isTypeLegal((MVT::ValueType)LargerReg)) {
|
2005-01-16 01:10:58 +00:00
|
|
|
++LargerReg;
|
|
|
|
assert(MVT::isInteger((MVT::ValueType)LargerReg) &&
|
|
|
|
"Nothing to promote to??");
|
|
|
|
}
|
|
|
|
PromoteTo = (MVT::ValueType)LargerReg;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(MVT::isInteger(VT) == MVT::isInteger(PromoteTo) &&
|
|
|
|
MVT::isFloatingPoint(VT) == MVT::isFloatingPoint(PromoteTo) &&
|
|
|
|
"Can only promote from int->int or fp->fp!");
|
|
|
|
assert(VT < PromoteTo && "Must promote to a larger type!");
|
|
|
|
TransformToType[VT] = PromoteTo;
|
2005-01-16 07:28:11 +00:00
|
|
|
} else if (Action == TargetLowering::Expand) {
|
2005-11-22 01:29:36 +00:00
|
|
|
assert((VT == MVT::Vector || MVT::isInteger(VT)) && VT > MVT::i8 &&
|
2005-01-16 01:10:58 +00:00
|
|
|
"Cannot expand this type: target must support SOME integer reg!");
|
|
|
|
// Expand to the next smaller integer type!
|
|
|
|
TransformToType[VT] = (MVT::ValueType)(VT-1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-01-07 07:44:53 +00:00
|
|
|
/// computeRegisterProperties - Once all of the register classes are added,
|
|
|
|
/// this allows us to compute derived properties we expose.
|
|
|
|
void TargetLowering::computeRegisterProperties() {
|
2005-11-29 05:45:29 +00:00
|
|
|
assert(MVT::LAST_VALUETYPE <= 32 &&
|
2005-01-16 01:10:58 +00:00
|
|
|
"Too many value types for ValueTypeActions to hold!");
|
|
|
|
|
2005-01-07 07:44:53 +00:00
|
|
|
// Everything defaults to one.
|
|
|
|
for (unsigned i = 0; i != MVT::LAST_VALUETYPE; ++i)
|
|
|
|
NumElementsForVT[i] = 1;
|
2005-04-21 22:55:34 +00:00
|
|
|
|
2005-01-07 07:44:53 +00:00
|
|
|
// Find the largest integer register class.
|
|
|
|
unsigned LargestIntReg = MVT::i128;
|
|
|
|
for (; RegClassForVT[LargestIntReg] == 0; --LargestIntReg)
|
|
|
|
assert(LargestIntReg != MVT::i1 && "No integer registers defined!");
|
|
|
|
|
|
|
|
// Every integer value type larger than this largest register takes twice as
|
|
|
|
// many registers to represent as the previous ValueType.
|
|
|
|
unsigned ExpandedReg = LargestIntReg; ++LargestIntReg;
|
|
|
|
for (++ExpandedReg; MVT::isInteger((MVT::ValueType)ExpandedReg);++ExpandedReg)
|
|
|
|
NumElementsForVT[ExpandedReg] = 2*NumElementsForVT[ExpandedReg-1];
|
|
|
|
|
2005-01-16 01:10:58 +00:00
|
|
|
// Inspect all of the ValueType's possible, deciding how to process them.
|
|
|
|
for (unsigned IntReg = MVT::i1; IntReg <= MVT::i128; ++IntReg)
|
|
|
|
// If we are expanding this type, expand it!
|
|
|
|
if (getNumElements((MVT::ValueType)IntReg) != 1)
|
2005-01-16 07:28:11 +00:00
|
|
|
SetValueTypeAction((MVT::ValueType)IntReg, Expand, *this, TransformToType,
|
2005-01-16 01:10:58 +00:00
|
|
|
ValueTypeActions);
|
2005-08-24 16:34:12 +00:00
|
|
|
else if (!isTypeLegal((MVT::ValueType)IntReg))
|
2005-01-16 01:10:58 +00:00
|
|
|
// Otherwise, if we don't have native support, we must promote to a
|
|
|
|
// larger type.
|
2005-01-16 07:28:11 +00:00
|
|
|
SetValueTypeAction((MVT::ValueType)IntReg, Promote, *this,
|
|
|
|
TransformToType, ValueTypeActions);
|
2005-01-16 01:20:18 +00:00
|
|
|
else
|
|
|
|
TransformToType[(MVT::ValueType)IntReg] = (MVT::ValueType)IntReg;
|
2005-04-21 22:55:34 +00:00
|
|
|
|
2005-01-16 01:10:58 +00:00
|
|
|
// If the target does not have native support for F32, promote it to F64.
|
2005-08-24 16:34:12 +00:00
|
|
|
if (!isTypeLegal(MVT::f32))
|
2005-01-16 07:28:11 +00:00
|
|
|
SetValueTypeAction(MVT::f32, Promote, *this,
|
|
|
|
TransformToType, ValueTypeActions);
|
2005-01-16 01:20:18 +00:00
|
|
|
else
|
|
|
|
TransformToType[MVT::f32] = MVT::f32;
|
2005-11-22 01:29:36 +00:00
|
|
|
|
|
|
|
// Set MVT::Vector to always be Expanded
|
|
|
|
SetValueTypeAction(MVT::Vector, Expand, *this, TransformToType,
|
|
|
|
ValueTypeActions);
|
2005-01-16 01:20:18 +00:00
|
|
|
|
2005-08-24 16:34:12 +00:00
|
|
|
assert(isTypeLegal(MVT::f64) && "Target does not support FP?");
|
2005-01-16 01:20:18 +00:00
|
|
|
TransformToType[MVT::f64] = MVT::f64;
|
2005-01-16 01:10:58 +00:00
|
|
|
}
|
2005-01-16 07:28:11 +00:00
|
|
|
|
2005-12-20 06:22:03 +00:00
|
|
|
const char *TargetLowering::getTargetNodeName(unsigned Opcode) const {
|
|
|
|
return NULL;
|
|
|
|
}
|
2005-12-21 23:05:39 +00:00
|
|
|
|
2006-01-30 04:09:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
/// MaskedValueIsZero - Return true if 'Op & Mask' is known to be zero. We use
|
|
|
|
/// this predicate to simplify operations downstream. Op and Mask are known to
|
|
|
|
/// be the same type.
|
|
|
|
bool TargetLowering::MaskedValueIsZero(const SDOperand &Op,
|
|
|
|
uint64_t Mask) const {
|
|
|
|
unsigned SrcBits;
|
|
|
|
if (Mask == 0) return true;
|
|
|
|
|
|
|
|
// If we know the result of a setcc has the top bits zero, use this info.
|
|
|
|
switch (Op.getOpcode()) {
|
|
|
|
case ISD::Constant:
|
|
|
|
return (cast<ConstantSDNode>(Op)->getValue() & Mask) == 0;
|
|
|
|
case ISD::SETCC:
|
|
|
|
return ((Mask & 1) == 0) &&
|
|
|
|
getSetCCResultContents() == TargetLowering::ZeroOrOneSetCCResult;
|
|
|
|
case ISD::ZEXTLOAD:
|
|
|
|
SrcBits = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(3))->getVT());
|
|
|
|
return (Mask & ((1ULL << SrcBits)-1)) == 0; // Returning only the zext bits.
|
|
|
|
case ISD::ZERO_EXTEND:
|
|
|
|
SrcBits = MVT::getSizeInBits(Op.getOperand(0).getValueType());
|
|
|
|
return MaskedValueIsZero(Op.getOperand(0),Mask & (~0ULL >> (64-SrcBits)));
|
|
|
|
case ISD::AssertZext:
|
|
|
|
SrcBits = MVT::getSizeInBits(cast<VTSDNode>(Op.getOperand(1))->getVT());
|
|
|
|
return (Mask & ((1ULL << SrcBits)-1)) == 0; // Returning only the zext bits.
|
|
|
|
case ISD::AND:
|
|
|
|
// If either of the operands has zero bits, the result will too.
|
|
|
|
if (MaskedValueIsZero(Op.getOperand(1), Mask) ||
|
|
|
|
MaskedValueIsZero(Op.getOperand(0), Mask))
|
|
|
|
return true;
|
|
|
|
// (X & C1) & C2 == 0 iff C1 & C2 == 0.
|
|
|
|
if (ConstantSDNode *AndRHS = dyn_cast<ConstantSDNode>(Op.getOperand(1)))
|
|
|
|
return MaskedValueIsZero(Op.getOperand(0),AndRHS->getValue() & Mask);
|
|
|
|
return false;
|
|
|
|
case ISD::OR:
|
|
|
|
case ISD::XOR:
|
|
|
|
return MaskedValueIsZero(Op.getOperand(0), Mask) &&
|
|
|
|
MaskedValueIsZero(Op.getOperand(1), Mask);
|
|
|
|
case ISD::SELECT:
|
|
|
|
return MaskedValueIsZero(Op.getOperand(1), Mask) &&
|
|
|
|
MaskedValueIsZero(Op.getOperand(2), Mask);
|
|
|
|
case ISD::SELECT_CC:
|
|
|
|
return MaskedValueIsZero(Op.getOperand(2), Mask) &&
|
|
|
|
MaskedValueIsZero(Op.getOperand(3), Mask);
|
|
|
|
case ISD::SRL:
|
|
|
|
// (ushr X, C1) & C2 == 0 iff X & (C2 << C1) == 0
|
|
|
|
if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
|
|
|
|
uint64_t NewVal = Mask << ShAmt->getValue();
|
|
|
|
SrcBits = MVT::getSizeInBits(Op.getValueType());
|
|
|
|
if (SrcBits != 64) NewVal &= (1ULL << SrcBits)-1;
|
|
|
|
return MaskedValueIsZero(Op.getOperand(0), NewVal);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
case ISD::SHL:
|
|
|
|
// (ushl X, C1) & C2 == 0 iff X & (C2 >> C1) == 0
|
|
|
|
if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
|
|
|
|
uint64_t NewVal = Mask >> ShAmt->getValue();
|
|
|
|
return MaskedValueIsZero(Op.getOperand(0), NewVal);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
case ISD::ADD:
|
|
|
|
// (add X, Y) & C == 0 iff (X&C)|(Y&C) == 0 and all bits are low bits.
|
|
|
|
if ((Mask&(Mask+1)) == 0) { // All low bits
|
|
|
|
if (MaskedValueIsZero(Op.getOperand(0), Mask) &&
|
|
|
|
MaskedValueIsZero(Op.getOperand(1), Mask))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ISD::SUB:
|
|
|
|
if (ConstantSDNode *CLHS = dyn_cast<ConstantSDNode>(Op.getOperand(0))) {
|
|
|
|
// We know that the top bits of C-X are clear if X contains less bits
|
|
|
|
// than C (i.e. no wrap-around can happen). For example, 20-X is
|
|
|
|
// positive if we can prove that X is >= 0 and < 16.
|
|
|
|
unsigned Bits = MVT::getSizeInBits(CLHS->getValueType(0));
|
|
|
|
if ((CLHS->getValue() & (1 << (Bits-1))) == 0) { // sign bit clear
|
|
|
|
unsigned NLZ = CountLeadingZeros_64(CLHS->getValue()+1);
|
|
|
|
uint64_t MaskV = (1ULL << (63-NLZ))-1;
|
|
|
|
if (MaskedValueIsZero(Op.getOperand(1), ~MaskV)) {
|
|
|
|
// High bits are clear this value is known to be >= C.
|
|
|
|
unsigned NLZ2 = CountLeadingZeros_64(CLHS->getValue());
|
|
|
|
if ((Mask & ((1ULL << (64-NLZ2))-1)) == 0)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ISD::CTTZ:
|
|
|
|
case ISD::CTLZ:
|
|
|
|
case ISD::CTPOP:
|
|
|
|
// Bit counting instructions can not set the high bits of the result
|
|
|
|
// register. The max number of bits sets depends on the input.
|
|
|
|
return (Mask & (MVT::getSizeInBits(Op.getValueType())*2-1)) == 0;
|
|
|
|
default:
|
|
|
|
// Allow the target to implement this method for its nodes.
|
|
|
|
if (Op.getOpcode() >= ISD::BUILTIN_OP_END)
|
|
|
|
return isMaskedValueZeroForTargetNode(Op, Mask);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2005-12-21 23:14:54 +00:00
|
|
|
bool TargetLowering::isMaskedValueZeroForTargetNode(const SDOperand &Op,
|
2006-01-30 04:09:27 +00:00
|
|
|
uint64_t Mask) const {
|
|
|
|
assert(Op.getOpcode() >= ISD::BUILTIN_OP_END &&
|
|
|
|
"Should use MaskedValueIsZero if you don't know whether Op"
|
|
|
|
" is a target node!");
|
2005-12-21 23:05:39 +00:00
|
|
|
return false;
|
|
|
|
}
|
2006-01-26 20:37:03 +00:00
|
|
|
|
|
|
|
std::vector<unsigned> TargetLowering::
|
|
|
|
getRegForInlineAsmConstraint(const std::string &Constraint) const {
|
2006-02-01 01:29:47 +00:00
|
|
|
// Not a physreg, must not be a register reference or something.
|
|
|
|
if (Constraint[0] != '{') return std::vector<unsigned>();
|
|
|
|
assert(*(Constraint.end()-1) == '}' && "Not a brace enclosed constraint?");
|
|
|
|
|
|
|
|
// Remove the braces from around the name.
|
|
|
|
std::string RegName(Constraint.begin()+1, Constraint.end()-1);
|
|
|
|
|
2006-01-26 20:37:03 +00:00
|
|
|
// Scan to see if this constraint is a register name.
|
|
|
|
const MRegisterInfo *RI = TM.getRegisterInfo();
|
|
|
|
for (unsigned i = 1, e = RI->getNumRegs(); i != e; ++i) {
|
|
|
|
if (const char *Name = RI->get(i).Name)
|
2006-02-01 01:29:47 +00:00
|
|
|
if (StringsEqualNoCase(RegName, Name))
|
2006-01-26 20:37:03 +00:00
|
|
|
return std::vector<unsigned>(1, i);
|
|
|
|
}
|
2006-02-01 01:29:47 +00:00
|
|
|
|
|
|
|
// Unknown physreg.
|
2006-01-26 20:37:03 +00:00
|
|
|
return std::vector<unsigned>();
|
|
|
|
}
|
|
|
|
|