1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
|
/*
* Copyright (C) 2015 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef ART_COMPILER_OPTIMIZING_INDUCTION_VAR_RANGE_H_
#define ART_COMPILER_OPTIMIZING_INDUCTION_VAR_RANGE_H_
#include "base/macros.h"
#include "induction_var_analysis.h"
namespace art HIDDEN {
/**
* This class implements range analysis on expressions within loops. It takes the results
* of induction variable analysis in the constructor and provides a public API to obtain
* a conservative lower and upper bound value or last value on each instruction in the HIR.
* The public API also provides a few general-purpose utility methods related to induction.
*
* The range analysis is done with a combination of symbolic and partial integral evaluation
* of expressions. The analysis avoids complications with wrap-around arithmetic on the integral
* parts but all clients should be aware that wrap-around may occur on any of the symbolic parts.
* For example, given a known range for [0,100] for i, the evaluation yields range [-100,100]
* for expression -2*i+100, which is exact, and range [x,x+100] for expression i+x, which may
* wrap-around anywhere in the range depending on the actual value of x.
*/
class InductionVarRange {
public:
/*
* A value that can be represented as "a * instruction + b" for 32-bit constants, where
* Value() denotes an unknown lower and upper bound. Although range analysis could yield
* more complex values, the format is sufficiently powerful to represent useful cases
* and feeds directly into optimizations like bounds check elimination.
*/
struct Value {
Value() : instruction(nullptr), a_constant(0), b_constant(0), is_known(false) {}
Value(HInstruction* i, int32_t a, int32_t b)
: instruction(a != 0 ? i : nullptr), a_constant(a), b_constant(b), is_known(true) {}
explicit Value(int32_t b) : Value(nullptr, 0, b) {}
// Representation as: a_constant x instruction + b_constant.
HInstruction* instruction;
int32_t a_constant;
int32_t b_constant;
// If true, represented by prior fields. Otherwise unknown value.
bool is_known;
};
explicit InductionVarRange(HInductionVarAnalysis* induction);
/**
* Given a context block, returns a possibly conservative lower
* and upper bound on the instruction's value in the output parameters min_val and max_val,
* respectively. The need_finite_test flag denotes if an additional finite-test is needed
* to protect the range evaluation inside its loop. The parameter chase_hint defines an
* instruction at which chasing may stop. Returns false on failure.
*/
bool GetInductionRange(const HBasicBlock* context,
HInstruction* instruction,
HInstruction* chase_hint,
/*out*/ Value* min_val,
/*out*/ Value* max_val,
/*out*/ bool* needs_finite_test);
/**
* Returns true if range analysis is able to generate code for the lower and upper
* bound expressions on the instruction in the given context. The need_finite_test
* and need_taken test flags denote if an additional finite-test and/or taken-test
* are needed to protect the range evaluation inside its loop.
*/
bool CanGenerateRange(const HBasicBlock* context,
HInstruction* instruction,
/*out*/ bool* needs_finite_test,
/*out*/ bool* needs_taken_test);
/**
* Generates the actual code in the HIR for the lower and upper bound expressions on the
* instruction in the given context. Code for the lower and upper bound expression are
* generated in given block and graph and are returned in the output parameters lower and
* upper, respectively. For a loop invariant, lower is not set.
*
* For example, given expression x+i with range [0, 5] for i, calling this method
* will generate the following sequence:
*
* block:
* lower: add x, 0
* upper: add x, 5
*
* Precondition: CanGenerateRange() returns true.
*/
void GenerateRange(const HBasicBlock* context,
HInstruction* instruction,
HGraph* graph,
HBasicBlock* block,
/*out*/ HInstruction** lower,
/*out*/ HInstruction** upper);
/**
* Generates explicit taken-test for the given `loop_control` instruction. Code is generated in
* given block and graph. Returns generated taken-test.
*
* Precondition: CanGenerateRange() returns true and needs_taken_test is set.
*/
HInstruction* GenerateTakenTest(HInstruction* loop_control, HGraph* graph, HBasicBlock* block);
/**
* Returns true if induction analysis is able to generate code for last value of
* the given instruction inside the closest enveloping loop.
*/
bool CanGenerateLastValue(HInstruction* instruction);
/**
* Generates last value of the given instruction in the closest enveloping loop.
* Code is generated in given block and graph. Returns generated last value.
*
* Precondition: CanGenerateLastValue() returns true.
*/
HInstruction* GenerateLastValue(HInstruction* instruction, HGraph* graph, HBasicBlock* block);
/**
* Updates all matching fetches with the given replacement in all induction information
* that is associated with the given instruction.
*/
void Replace(HInstruction* instruction, HInstruction* fetch, HInstruction* replacement);
/**
* Incrementally updates induction information for just the given loop.
*/
void ReVisit(const HLoopInformation* loop) {
induction_analysis_->induction_.erase(loop);
for (HInstructionIterator it(loop->GetHeader()->GetPhis()); !it.Done(); it.Advance()) {
induction_analysis_->cycles_.erase(it.Current()->AsPhi());
}
induction_analysis_->VisitLoop(loop);
}
/**
* Lookup an interesting cycle associated with an entry phi.
*/
ArenaSet<HInstruction*>* LookupCycle(HPhi* phi) const {
return induction_analysis_->LookupCycle(phi);
}
/**
* Checks if the given phi instruction has been classified as anything by
* induction variable analysis. Returns false for anything that cannot be
* classified statically, such as reductions or other complex cycles.
*/
bool IsClassified(HPhi* phi) const {
HLoopInformation* lp = phi->GetBlock()->GetLoopInformation(); // closest enveloping loop
return (lp != nullptr) && (induction_analysis_->LookupInfo(lp, phi) != nullptr);
}
/**
* Checks if header logic of a loop terminates. If trip count is known sets 'trip_count' to its
* value.
*/
bool IsFinite(const HLoopInformation* loop, /*out*/ int64_t* trip_count) const;
/**
* Checks if a trip count is known for the loop and sets 'trip_count' to its value in this case.
*/
bool HasKnownTripCount(const HLoopInformation* loop, /*out*/ int64_t* trip_count) const;
/**
* Checks if the given instruction is a unit stride induction inside the closest enveloping
* loop of the context that is defined by the first parameter (e.g. pass an array reference
* as context and the index as instruction to make sure the stride is tested against the
* loop that envelops the reference the closest). Returns invariant offset on success.
*/
bool IsUnitStride(const HBasicBlock* context,
HInstruction* instruction,
HGraph* graph,
/*out*/ HInstruction** offset) const;
/**
* Generates the trip count expression for the given loop. Code is generated in given block
* and graph. The expression is guarded by a taken test if needed. Returns the trip count
* expression on success or null otherwise.
*/
HInstruction* GenerateTripCount(const HLoopInformation* loop, HGraph* graph, HBasicBlock* block);
private:
/*
* Enum used in IsConstant() request.
*/
enum ConstantRequest {
kExact,
kAtMost,
kAtLeast
};
/**
* Checks if header logic of a loop terminates. If trip count is known (constant) sets
* 'is_constant' to true and 'trip_count' to the trip count value.
*/
bool CheckForFiniteAndConstantProps(const HLoopInformation* loop,
/*out*/ bool* is_constant,
/*out*/ int64_t* trip_count) const;
/**
* Returns true if exact or upper/lower bound on the given induction
* information is known as a 64-bit constant, which is returned in value.
*/
bool IsConstant(const HBasicBlock* context,
const HLoopInformation* loop,
HInductionVarAnalysis::InductionInfo* info,
ConstantRequest request,
/*out*/ int64_t* value) const;
/** Returns whether induction information can be obtained. */
bool HasInductionInfo(const HBasicBlock* context,
HInstruction* instruction,
/*out*/ const HLoopInformation** loop,
/*out*/ HInductionVarAnalysis::InductionInfo** info,
/*out*/ HInductionVarAnalysis::InductionInfo** trip) const;
bool HasFetchInLoop(HInductionVarAnalysis::InductionInfo* info) const;
bool NeedsTripCount(const HBasicBlock* context,
const HLoopInformation* loop,
HInductionVarAnalysis::InductionInfo* info,
/*out*/ int64_t* stride_value) const;
bool IsBodyTripCount(HInductionVarAnalysis::InductionInfo* trip) const;
bool IsUnsafeTripCount(HInductionVarAnalysis::InductionInfo* trip) const;
bool IsWellBehavedTripCount(const HBasicBlock* context,
const HLoopInformation* loop,
HInductionVarAnalysis::InductionInfo* trip) const;
Value GetLinear(const HBasicBlock* context,
const HLoopInformation* loop,
HInductionVarAnalysis::InductionInfo* info,
HInductionVarAnalysis::InductionInfo* trip,
bool is_min) const;
Value GetPolynomial(const HBasicBlock* context,
const HLoopInformation* loop,
HInductionVarAnalysis::InductionInfo* info,
HInductionVarAnalysis::InductionInfo* trip,
bool is_min) const;
Value GetGeometric(const HBasicBlock* context,
const HLoopInformation* loop,
HInductionVarAnalysis::InductionInfo* info,
HInductionVarAnalysis::InductionInfo* trip,
bool is_min) const;
Value GetFetch(const HBasicBlock* context,
const HLoopInformation* loop,
HInstruction* instruction,
HInductionVarAnalysis::InductionInfo* trip,
bool is_min) const;
Value GetVal(const HBasicBlock* context,
const HLoopInformation* loop,
HInductionVarAnalysis::InductionInfo* info,
HInductionVarAnalysis::InductionInfo* trip,
bool is_min) const;
Value GetMul(const HBasicBlock* context,
const HLoopInformation* loop,
HInductionVarAnalysis::InductionInfo* info1,
HInductionVarAnalysis::InductionInfo* info2,
HInductionVarAnalysis::InductionInfo* trip,
bool is_min) const;
Value GetDiv(const HBasicBlock* context,
const HLoopInformation* loop,
HInductionVarAnalysis::InductionInfo* info1,
HInductionVarAnalysis::InductionInfo* info2,
HInductionVarAnalysis::InductionInfo* trip,
bool is_min) const;
Value GetRem(const HBasicBlock* context,
const HLoopInformation* loop,
HInductionVarAnalysis::InductionInfo* info1,
HInductionVarAnalysis::InductionInfo* info2) const;
Value GetXor(const HBasicBlock* context,
const HLoopInformation* loop,
HInductionVarAnalysis::InductionInfo* info1,
HInductionVarAnalysis::InductionInfo* info2) const;
Value MulRangeAndConstant(const HBasicBlock* context,
const HLoopInformation* loop,
int64_t value,
HInductionVarAnalysis::InductionInfo* info,
HInductionVarAnalysis::InductionInfo* trip,
bool is_min) const;
Value DivRangeAndConstant(const HBasicBlock* context,
const HLoopInformation* loop,
int64_t value,
HInductionVarAnalysis::InductionInfo* info,
HInductionVarAnalysis::InductionInfo* trip,
bool is_min) const;
Value AddValue(Value v1, Value v2) const;
Value SubValue(Value v1, Value v2) const;
Value MulValue(Value v1, Value v2) const;
Value DivValue(Value v1, Value v2) const;
Value MergeVal(Value v1, Value v2, bool is_min) const;
/**
* Generates code for lower/upper/taken-test or last value in the HIR. Returns true on
* success. With values nullptr, the method can be used to determine if code generation
* would be successful without generating actual code yet.
*/
bool GenerateRangeOrLastValue(const HBasicBlock* context,
HInstruction* instruction,
bool is_last_val,
HGraph* graph,
HBasicBlock* block,
/*out*/ HInstruction** lower,
/*out*/ HInstruction** upper,
/*out*/ HInstruction** taken_test,
/*out*/ int64_t* stride_value,
/*out*/ bool* needs_finite_test,
/*out*/ bool* needs_taken_test) const;
bool GenerateLastValueLinear(const HBasicBlock* context,
const HLoopInformation* loop,
HInductionVarAnalysis::InductionInfo* info,
HInductionVarAnalysis::InductionInfo* trip,
HGraph* graph,
HBasicBlock* block,
bool is_min,
/*out*/ HInstruction** result) const;
bool GenerateLastValuePolynomial(const HBasicBlock* context,
const HLoopInformation* loop,
HInductionVarAnalysis::InductionInfo* info,
HInductionVarAnalysis::InductionInfo* trip,
HGraph* graph,
HBasicBlock* block,
/*out*/HInstruction** result) const;
bool GenerateLastValueGeometric(const HBasicBlock* context,
const HLoopInformation* loop,
HInductionVarAnalysis::InductionInfo* info,
HInductionVarAnalysis::InductionInfo* trip,
HGraph* graph,
HBasicBlock* block,
/*out*/HInstruction** result) const;
bool GenerateLastValueWrapAround(const HBasicBlock* context,
const HLoopInformation* loop,
HInductionVarAnalysis::InductionInfo* info,
HInductionVarAnalysis::InductionInfo* trip,
HGraph* graph,
HBasicBlock* block,
/*out*/HInstruction** result) const;
bool GenerateLastValuePeriodic(const HBasicBlock* context,
const HLoopInformation* loop,
HInductionVarAnalysis::InductionInfo* info,
HInductionVarAnalysis::InductionInfo* trip,
HGraph* graph,
HBasicBlock* block,
/*out*/HInstruction** result,
/*out*/ bool* needs_taken_test) const;
bool GenerateCode(const HBasicBlock* context,
const HLoopInformation* loop,
HInductionVarAnalysis::InductionInfo* info,
HInductionVarAnalysis::InductionInfo* trip,
HGraph* graph,
HBasicBlock* block,
bool is_min,
/*out*/ HInstruction** result) const;
void ReplaceInduction(HInductionVarAnalysis::InductionInfo* info,
HInstruction* fetch,
HInstruction* replacement);
/** Results of prior induction variable analysis. */
HInductionVarAnalysis* induction_analysis_;
/** Instruction at which chasing may stop. */
HInstruction* chase_hint_;
friend class HInductionVarAnalysis;
friend class InductionVarRangeTest;
DISALLOW_COPY_AND_ASSIGN(InductionVarRange);
};
} // namespace art
#endif // ART_COMPILER_OPTIMIZING_INDUCTION_VAR_RANGE_H_
|