1 /*
   2  * Copyright (c) 2014, 2019, 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 
  24 
  25 package org.graalvm.compiler.lir.dfa;
  26 
  27 import static jdk.vm.ci.code.ValueUtil.asRegister;
  28 import static jdk.vm.ci.code.ValueUtil.isRegister;
  29 import static jdk.vm.ci.code.ValueUtil.isStackSlot;
  30 
  31 import org.graalvm.compiler.core.common.LIRKind;
  32 import org.graalvm.compiler.lir.LIR;
  33 import org.graalvm.compiler.lir.LIRFrameState;
  34 import org.graalvm.compiler.lir.LIRInstruction;
  35 import org.graalvm.compiler.lir.framemap.FrameMap;
  36 import org.graalvm.compiler.lir.framemap.ReferenceMapBuilder;
  37 import org.graalvm.compiler.lir.gen.LIRGenerationResult;
  38 import org.graalvm.compiler.lir.phases.AllocationPhase;
  39 
  40 import jdk.vm.ci.code.ReferenceMap;
  41 import jdk.vm.ci.code.Register;
  42 import jdk.vm.ci.code.RegisterAttributes;
  43 import jdk.vm.ci.code.TargetDescription;
  44 import jdk.vm.ci.meta.Value;
  45 
  46 /**
  47  * Mark all live references for a frame state. The frame state uses this information to build the
  48  * {@link ReferenceMap}s.
  49  */
  50 public final class LocationMarkerPhase extends AllocationPhase {
  51 
  52     @Override
  53     protected void run(TargetDescription target, LIRGenerationResult lirGenRes, AllocationContext context) {
  54         new Marker(lirGenRes.getLIR(), lirGenRes.getFrameMap()).build();
  55     }
  56 
  57     static final class Marker extends LocationMarker<RegStackValueSet> {
  58 
  59         private final RegisterAttributes[] registerAttributes;
  60 
  61         private Marker(LIR lir, FrameMap frameMap) {
  62             super(lir, frameMap);
  63             this.registerAttributes = frameMap.getRegisterConfig().getAttributesMap();
  64         }
  65 
  66         @Override
  67         protected RegStackValueSet newLiveValueSet() {
  68             return new RegStackValueSet(frameMap);
  69         }
  70 
  71         @Override
  72         protected boolean shouldProcessValue(Value operand) {
  73             if (isRegister(operand)) {
  74                 Register reg = asRegister(operand);
  75                 if (!reg.mayContainReference() || !attributes(reg).isAllocatable()) {
  76                     // register that's not allocatable or not part of the reference map
  77                     return false;
  78                 }
  79             } else if (!isStackSlot(operand)) {
  80                 // neither register nor stack slot
  81                 return false;
  82             }
  83 
  84             return !operand.getValueKind().equals(LIRKind.Illegal);
  85         }
  86 
  87         /**
  88          * This method does the actual marking.
  89          */
  90         @Override
  91         protected void processState(LIRInstruction op, LIRFrameState info, RegStackValueSet values) {
  92             if (!info.hasDebugInfo()) {
  93                 info.initDebugInfo(frameMap, !op.destroysCallerSavedRegisters() || !frameMap.getRegisterConfig().areAllAllocatableRegistersCallerSaved());
  94             }
  95 
  96             ReferenceMapBuilder refMap = frameMap.newReferenceMapBuilder();
  97             values.addLiveValues(refMap);
  98 
  99             info.debugInfo().setReferenceMap(refMap.finish(info));
 100         }
 101 
 102         /**
 103          * Gets an object describing the attributes of a given register according to this register
 104          * configuration.
 105          */
 106         private RegisterAttributes attributes(Register reg) {
 107             return registerAttributes[reg.number];
 108         }
 109 
 110     }
 111 }