hqlpopt.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2012 HPCC Systems®.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ############################################################################## */
  13. #include "jliball.hpp"
  14. #include "hql.hpp"
  15. #include "platform.h"
  16. #include "jlib.hpp"
  17. #include "jexcept.hpp"
  18. #include "jmisc.hpp"
  19. #include "javahash.hpp"
  20. #include "jmd5.hpp"
  21. #include "jfile.hpp"
  22. #include "eclhelper.hpp"
  23. #include "hqlfunc.hpp"
  24. #include "hqlattr.hpp"
  25. #include "hqlcpp.ipp"
  26. #include "hqlpopt.hpp"
  27. //Optimize IF(a,b,c) op x to IF(a,b op x, c OP x)
  28. //But be careful because it uncommons attributes increasing the size of the queries.
  29. static IHqlExpression * peepholeOptimizeCompare(BuildCtx & ctx, IHqlExpression * expr)
  30. {
  31. IHqlExpression * lhs = expr->queryChild(0);
  32. if (ctx.queryMatchExpr(lhs))
  33. return LINK(expr);
  34. IHqlExpression * rhs = expr->queryChild(1);
  35. if (!rhs->isConstant() || (lhs->getOperator() != no_if))
  36. return LINK(expr);
  37. IHqlExpression * ifCond = lhs->queryChild(0);
  38. IHqlExpression * ifTrue = lhs->queryChild(1);
  39. IHqlExpression * ifFalse = lhs->queryChild(2);
  40. assertex(ifFalse);
  41. node_operator op = expr->getOperator();
  42. OwnedHqlExpr newTrue = createValue(op, makeBoolType(), LINK(ifTrue), LINK(rhs));
  43. OwnedHqlExpr newFalse = createValue(op, makeBoolType(), LINK(ifFalse), LINK(rhs));
  44. OwnedHqlExpr newIf = createValue(no_if, makeBoolType(), LINK(ifCond), peepholeOptimize(ctx, newTrue), peepholeOptimize(ctx, newFalse));
  45. return expr->cloneAllAnnotations(newIf);
  46. }
  47. //Optimize search [not] in set(ds, value) to
  48. //[not] exists(ds, value=search); - really need link counts on the expressions, and should be done in the real optimizer
  49. IHqlExpression * peepholeOptimize(BuildCtx & ctx, IHqlExpression * expr)
  50. {
  51. switch (expr->getOperator())
  52. {
  53. case no_eq:
  54. case no_ne:
  55. case no_le:
  56. case no_lt:
  57. case no_ge:
  58. case no_gt:
  59. return peepholeOptimizeCompare(ctx, expr);
  60. case no_and:
  61. case no_or:
  62. case no_not:
  63. {
  64. HqlExprArray args;
  65. bool same = true;
  66. ForEachChild(i, expr)
  67. {
  68. IHqlExpression * cur = expr->queryChild(i);
  69. IHqlExpression * optimized = peepholeOptimize(ctx, expr);
  70. args.append(*optimized);
  71. if (cur != optimized)
  72. same = false;
  73. }
  74. if (!same)
  75. return expr->clone(args);
  76. break;
  77. }
  78. }
  79. return LINK(expr);
  80. }