1 /*
   2  * Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 package org.graalvm.compiler.lir.dfa;
  24 
  25 import static jdk.vm.ci.code.ValueUtil.asRegister;
  26 import static jdk.vm.ci.code.ValueUtil.isRegister;
  27 import static jdk.vm.ci.code.ValueUtil.isStackSlot;
  28 
  29 import org.graalvm.compiler.core.common.LIRKind;
  30 import org.graalvm.compiler.lir.LIR;
  31 import org.graalvm.compiler.lir.LIRFrameState;
  32 import org.graalvm.compiler.lir.LIRInstruction;
  33 import org.graalvm.compiler.lir.framemap.FrameMap;
  34 import org.graalvm.compiler.lir.framemap.ReferenceMapBuilder;
  35 import org.graalvm.compiler.lir.gen.LIRGenerationResult;
  36 import org.graalvm.compiler.lir.phases.AllocationPhase;
  37 
  38 import jdk.vm.ci.code.Register;
  39 import jdk.vm.ci.code.RegisterAttributes;
  40 import jdk.vm.ci.code.TargetDescription;
  41 import jdk.vm.ci.meta.Value;
  42 
  43 /**
  44  * Mark all live references for a frame state. The frame state use this information to build the OOP
  45  * maps.
  46  */
  47 public final class LocationMarkerPhase extends AllocationPhase {
  48 
  49     @Override
  50     protected void run(TargetDescription target, LIRGenerationResult lirGenRes, AllocationContext context) {
  51         new Marker(lirGenRes.getLIR(), lirGenRes.getFrameMap()).build();
  52     }
  53 
  54     static final class Marker extends LocationMarker<RegStackValueSet> {
  55 
  56         private final RegisterAttributes[] registerAttributes;
  57 
  58         private Marker(LIR lir, FrameMap frameMap) {
  59             super(lir, frameMap);
  60             this.registerAttributes = frameMap.getRegisterConfig().getAttributesMap();
  61         }
  62 
  63         @Override
  64         protected RegStackValueSet newLiveValueSet() {
  65             return new RegStackValueSet(frameMap);
  66         }
  67 
  68         @Override
  69         protected boolean shouldProcessValue(Value operand) {
  70             if (isRegister(operand)) {
  71                 Register reg = asRegister(operand);
  72                 if (!reg.mayContainReference() || !attributes(reg).isAllocatable()) {
  73                     // register that's not allocatable or not part of the reference map
  74                     return false;
  75                 }
  76             } else if (!isStackSlot(operand)) {
  77                 // neither register nor stack slot
  78                 return false;
  79             }
  80 
  81             return !operand.getValueKind().equals(LIRKind.Illegal);
  82         }
  83 
  84         /**
  85          * This method does the actual marking.
  86          */
  87         @Override
  88         protected void processState(LIRInstruction op, LIRFrameState info, RegStackValueSet values) {
  89             if (!info.hasDebugInfo()) {
  90                 info.initDebugInfo(frameMap, !op.destroysCallerSavedRegisters() || !frameMap.getRegisterConfig().areAllAllocatableRegistersCallerSaved());
  91             }
  92 
  93             ReferenceMapBuilder refMap = frameMap.newReferenceMapBuilder();
  94             frameMap.addLiveValues(refMap);
  95             values.addLiveValues(refMap);
  96 
  97             info.debugInfo().setReferenceMap(refMap.finish(info));
  98         }
  99 
 100         /**
 101          * Gets an object describing the attributes of a given register according to this register
 102          * configuration.
 103          */
 104         private RegisterAttributes attributes(Register reg) {
 105             return registerAttributes[reg.number];
 106         }
 107 
 108     }
 109 }