michael@0: /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- michael@0: * vim: set ts=8 sts=4 et sw=4 tw=99: michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "jit/EffectiveAddressAnalysis.h" michael@0: #include "jit/MIR.h" michael@0: #include "jit/MIRGraph.h" michael@0: michael@0: using namespace js; michael@0: using namespace jit; michael@0: michael@0: static void michael@0: AnalyzeLsh(TempAllocator &alloc, MLsh *lsh) michael@0: { michael@0: if (lsh->specialization() != MIRType_Int32) michael@0: return; michael@0: michael@0: MDefinition *index = lsh->lhs(); michael@0: JS_ASSERT(index->type() == MIRType_Int32); michael@0: michael@0: MDefinition *shift = lsh->rhs(); michael@0: if (!shift->isConstant()) michael@0: return; michael@0: michael@0: Value shiftValue = shift->toConstant()->value(); michael@0: if (!shiftValue.isInt32() || !IsShiftInScaleRange(shiftValue.toInt32())) michael@0: return; michael@0: michael@0: Scale scale = ShiftToScale(shiftValue.toInt32()); michael@0: michael@0: int32_t displacement = 0; michael@0: MInstruction *last = lsh; michael@0: MDefinition *base = nullptr; michael@0: while (true) { michael@0: if (!last->hasOneUse()) michael@0: break; michael@0: michael@0: MUseIterator use = last->usesBegin(); michael@0: if (!use->consumer()->isDefinition() || !use->consumer()->toDefinition()->isAdd()) michael@0: break; michael@0: michael@0: MAdd *add = use->consumer()->toDefinition()->toAdd(); michael@0: if (add->specialization() != MIRType_Int32 || !add->isTruncated()) michael@0: break; michael@0: michael@0: MDefinition *other = add->getOperand(1 - use->index()); michael@0: michael@0: if (other->isConstant()) { michael@0: displacement += other->toConstant()->value().toInt32(); michael@0: } else { michael@0: if (base) michael@0: break; michael@0: base = other; michael@0: } michael@0: michael@0: last = add; michael@0: } michael@0: michael@0: if (!base) { michael@0: uint32_t elemSize = 1 << ScaleToShift(scale); michael@0: if (displacement % elemSize != 0) michael@0: return; michael@0: michael@0: if (!last->hasOneUse()) michael@0: return; michael@0: michael@0: MUseIterator use = last->usesBegin(); michael@0: if (!use->consumer()->isDefinition() || !use->consumer()->toDefinition()->isBitAnd()) michael@0: return; michael@0: michael@0: MBitAnd *bitAnd = use->consumer()->toDefinition()->toBitAnd(); michael@0: MDefinition *other = bitAnd->getOperand(1 - use->index()); michael@0: if (!other->isConstant() || !other->toConstant()->value().isInt32()) michael@0: return; michael@0: michael@0: uint32_t bitsClearedByShift = elemSize - 1; michael@0: uint32_t bitsClearedByMask = ~uint32_t(other->toConstant()->value().toInt32()); michael@0: if ((bitsClearedByShift & bitsClearedByMask) != bitsClearedByMask) michael@0: return; michael@0: michael@0: bitAnd->replaceAllUsesWith(last); michael@0: return; michael@0: } michael@0: michael@0: MEffectiveAddress *eaddr = MEffectiveAddress::New(alloc, base, index, scale, displacement); michael@0: last->replaceAllUsesWith(eaddr); michael@0: last->block()->insertAfter(last, eaddr); michael@0: } michael@0: michael@0: // This analysis converts patterns of the form: michael@0: // truncate(x + (y << {0,1,2,3})) michael@0: // truncate(x + (y << {0,1,2,3}) + imm32) michael@0: // into a single lea instruction, and patterns of the form: michael@0: // asmload(x + imm32) michael@0: // asmload(x << {0,1,2,3}) michael@0: // asmload((x << {0,1,2,3}) + imm32) michael@0: // asmload((x << {0,1,2,3}) & mask) (where mask is redundant with shift) michael@0: // asmload(((x << {0,1,2,3}) + imm32) & mask) (where mask is redundant with shift + imm32) michael@0: // into a single asmload instruction (and for asmstore too). michael@0: // michael@0: // Additionally, we should consider the general forms: michael@0: // truncate(x + y + imm32) michael@0: // truncate((y << {0,1,2,3}) + imm32) michael@0: bool michael@0: EffectiveAddressAnalysis::analyze() michael@0: { michael@0: for (ReversePostorderIterator block(graph_.rpoBegin()); block != graph_.rpoEnd(); block++) { michael@0: for (MInstructionIterator i = block->begin(); i != block->end(); i++) { michael@0: if (i->isLsh()) michael@0: AnalyzeLsh(graph_.alloc(), i->toLsh()); michael@0: } michael@0: } michael@0: return true; michael@0: }