File: BreakConstantExpr.cpp

package info (click to toggle)
intel-graphics-compiler2 2.16.0-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 106,644 kB
  • sloc: cpp: 805,640; lisp: 287,672; ansic: 16,414; python: 3,952; yacc: 2,588; lex: 1,666; pascal: 313; sh: 186; makefile: 35
file content (309 lines) | stat: -rw-r--r-- 10,912 bytes parent folder | download
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
/*========================== begin_copyright_notice ============================

Copyright (C) 2017-2021 Intel Corporation

SPDX-License-Identifier: MIT

============================= end_copyright_notice ===========================*/

#include "Compiler/Optimizer/OpenCLPasses/BreakConstantExpr/BreakConstantExpr.hpp"
#include "Compiler/IGCPassSupport.h"
#include "Compiler/CodeGenPublic.h"
#include "common/LLVMWarningsPush.hpp"
#include <llvm/IR/Function.h>
#include <llvm/IR/Instructions.h>
#include <llvm/IR/Constants.h>
#include <llvm/IR/IntrinsicInst.h>
#include <llvm/IR/Metadata.h>
#include <llvm/IR/InstIterator.h>
#include "common/LLVMWarningsPop.hpp"

using namespace llvm;
using namespace IGC;

// Register pass to igc-opt
#define PASS_FLAG "igc-break-const-expr"
#define PASS_DESCRIPTION "Break constant expressions into instruction sequences"
#define PASS_CFG_ONLY false
#define PASS_ANALYSIS false
IGC_INITIALIZE_PASS_BEGIN(BreakConstantExpr, PASS_FLAG, PASS_DESCRIPTION, PASS_CFG_ONLY, PASS_ANALYSIS)
IGC_INITIALIZE_PASS_END(BreakConstantExpr, PASS_FLAG, PASS_DESCRIPTION, PASS_CFG_ONLY, PASS_ANALYSIS)

char BreakConstantExpr::ID = 0;

BreakConstantExpr::BreakConstantExpr() : FunctionPass(ID) {
  initializeBreakConstantExprPass(*PassRegistry::getPassRegistry());
}

bool BreakConstantExpr::hasConstantExpr(ConstantVector *cvec) const {
  const uint32_t VecSize = cvec->getNumOperands();
  for (unsigned elemIdx = 0; elemIdx < VecSize; ++elemIdx) {
    if (isa<ConstantExpr>(cvec->getOperand(elemIdx))) {
      return true;
    }
  }
  return false;
}

bool BreakConstantExpr::hasConstantExpr(ConstantStruct *cstruct) const {
  const uint32_t Size0 = cstruct->getNumOperands();
  for (uint32_t i = 0; i < Size0; ++i) {
    Value *Ci = cstruct->getOperand(i);
    if (ConstantStruct *aCS = dyn_cast<ConstantStruct>(Ci)) {
      // Allow 2-level struct (layout struct by LdStCombine is 2 levels)
      uint32_t Size1 = aCS->getNumOperands();
      for (uint32_t j = 0; j < Size1; ++j) {
        Value *Cij = aCS->getOperand(j);
        if (ConstantExpr *c = dyn_cast<ConstantExpr>(Cij)) {
          return true;
        }
        if (ConstantVector *CVec = dyn_cast<ConstantVector>(Cij)) {
          if (hasConstantExpr(CVec)) {
            return true;
          }
        }
      }
    } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Ci)) {
      return true;
    }
    if (ConstantVector *CVec = dyn_cast<ConstantVector>(Ci)) {
      if (hasConstantExpr(CVec)) {
        return true;
      }
    }
  }
  return false;
}

bool BreakConstantExpr::runOnFunction(Function &F) {
  bool changed = false;
  // Go over all the instructions in the function
  for (inst_iterator it = inst_begin(F), e = inst_end(F); it != e; ++it) {
    Instruction *pInst = &*it;
    if (DbgDeclareInst *DbgDclInst = dyn_cast<DbgDeclareInst>(pInst)) {
      // For DbgDeclareInst, the operand is a metadata that might
      // contain a constant expression.
      Value *op = DbgDclInst->getAddress();
      // If the debug adress is a constant expression, recursively break it up.
      if (ConstantExpr *expr = dyn_cast_or_null<ConstantExpr>(op)) {
        breakExpressions(expr, 0, pInst);
        changed = true;
      }
      continue;
    }
#if 0
        //Disable handling of llvm.dbg.value instruction as it needs
        //proper handling of metadata.
        if (DbgValueInst * DbgValInst = dyn_cast<DbgValueInst>(pInst)) {
            // For DbgValueInst, the operand is a metadata that might
            // contain a constant expression.
            Value* op = DbgValInst->getValue();
            // If the debug value operand is a constant expression, recursively break it up.
            if (ConstantExpr * expr = dyn_cast_or_null<ConstantExpr>(op))
            {
                breakExpressions(expr, 0, pInst);
                changed = true;
            }
            continue;
        }
#endif

    // And all the operands of each instruction
    int numOperands = it->getNumOperands();
    for (int i = 0; i < numOperands; ++i) {
      Value *op = it->getOperand(i);

      // If the operand is a constant expression, recursively break it up.
      if (ConstantExpr *expr = dyn_cast<ConstantExpr>(op)) {
        breakExpressions(expr, i, pInst);
        changed = true;
      } else if (ConstantVector *cvec = dyn_cast<ConstantVector>(op)) {
        changed |= breakExpressionsInVector(cvec, i, pInst);
      } else if (ConstantStruct *CS = dyn_cast<ConstantStruct>(op)) {
        changed |= breakConstantStruct(CS, i, pInst);
      }
    }
  }
  return changed;
}

void BreakConstantExpr::replaceConstantWith(llvm::Constant *exprOrVec, llvm::Instruction *newInst, int operandIndex,
                                            llvm::Instruction *user) {
  if (PHINode *phi = dyn_cast<PHINode>(user)) {
    newInst->insertBefore(phi->getIncomingBlock(operandIndex)->getTerminator());
    user->setOperand(operandIndex, newInst);
  } else if (dyn_cast<DbgInfoIntrinsic>(user)) {
    newInst->insertBefore(user);
    // For debug info intrinsic, the operand is a metadata that
    // contains the constant expression.
    if (auto *DDI = dyn_cast<DbgDeclareInst>(user)) {
      MetadataAsValue *MAV = MetadataAsValue::get(user->getContext(), ValueAsMetadata::get(newInst));
      user->setOperand(operandIndex, MAV);
    } else {
      user->setOperand(operandIndex, newInst);
    }
  } else {
    newInst->insertBefore(user);
    user->replaceUsesOfWith(exprOrVec, newInst);
  }
  if (exprOrVec->use_empty()) {
    exprOrVec->destroyConstant();
  }
}

void BreakConstantExpr::breakExpressions(llvm::ConstantExpr *expr, int operandIndex, llvm::Instruction *user) {
  // Create a new instruction, and insert it at the appropriate point.
  Instruction *newInst = expr->getAsInstruction();
  newInst->setDebugLoc(user->getDebugLoc());

  replaceConstantWith(expr, newInst, operandIndex, user);

  // Thew new instruction may itself reference constant expressions.
  // So, recursively process all of its arguments.
  int numOperands = newInst->getNumOperands();
  for (int i = 0; i < numOperands; ++i) {
    Value *op = newInst->getOperand(i);
    ConstantExpr *innerExpr = dyn_cast<ConstantExpr>(op);
    if (innerExpr) {
      breakExpressions(innerExpr, i, newInst);
    } else if (ConstantVector *cvec = dyn_cast<ConstantVector>(op)) {
      breakExpressionsInVector(cvec, i, newInst);
    }
  }
}

bool BreakConstantExpr::breakExpressionsInVector(llvm::ConstantVector *cvec, int operandIndex,
                                                 llvm::Instruction *user) {
  if (!hasConstantExpr(cvec))
    return false;

  Value *currVec = UndefValue::get(cvec->getType());
  const unsigned vecSize = cvec->getNumOperands();

  for (unsigned elemIdx = 0; elemIdx < vecSize; ++elemIdx) {
    Value *op = cvec->getOperand(elemIdx);
    Instruction *newInst =
        InsertElementInst::Create(currVec, op, ConstantInt::get(Type::getInt32Ty(user->getContext()), elemIdx));

    if (elemIdx < cvec->getNumOperands() - 1) {
      if (PHINode *phi = dyn_cast<PHINode>(user)) {
        newInst->insertBefore(phi->getIncomingBlock(operandIndex)->getTerminator());
      } else {
        newInst->insertBefore(user);
      }
    } else {
      // cvec can be destroyed inside!
      replaceConstantWith(cvec, newInst, operandIndex, user);
    }

    if (ConstantExpr *expr = dyn_cast<ConstantExpr>(op)) {
      breakExpressions(expr, 1, newInst);
    }

    currVec = newInst;
  }
  return true;
}

bool BreakConstantExpr::breakConstantStruct(ConstantStruct *cs, int operandIndex, Instruction *user) {
  if (!hasConstantExpr(cs))
    return false;

  // [TODO] Here, localize the changes inside breakConstantStruct to minimize
  //        the impact. It may be worth refactoring breakConstantExpr
  auto insertBefore = [=](Instruction *newInst, Instruction *InsertPos) {
    if (PHINode *phi = dyn_cast<PHINode>(InsertPos)) {
      newInst->insertBefore(phi->getIncomingBlock(operandIndex)->getTerminator());
    } else {
      newInst->insertBefore(InsertPos);
    }
  };

  auto breakInst = [this](Instruction *I) {
    bool changed = false;
    const int numOperands = (int)I->getNumOperands();
    for (int i = 0; i < numOperands; ++i) {
      Value *op = I->getOperand(i);

      if (ConstantExpr *expr = dyn_cast<ConstantExpr>(op)) {
        breakExpressions(expr, i, I);
        changed = true;
      } else if (ConstantVector *cvec = dyn_cast<ConstantVector>(op)) { // can this happen ?
        changed |= breakExpressionsInVector(cvec, i, I);
      } else if (ConstantStruct *cs = dyn_cast<ConstantStruct>(op)) { // can this happen ?
        changed |= breakConstantStruct(cs, i, I);
      }
    }
    return changed;
  };

  StructType *structType = cs->getType();
  IRBuilder<> B(user);
  Value *newStruct = UndefValue::get(structType);

  // Create a structure from scratch, replace every constant operand by an
  // instruction.
  for (unsigned i = 0; i < cs->getNumOperands(); ++i) {
    Value *Ci = cs->getOperand(i);

    if (ConstantStruct *aCS = dyn_cast<ConstantStruct>(Ci)) {
      // Allow 2-level struct (layout struct by LdStCombine is 2 levels)
      for (unsigned j = 0; j < aCS->getNumOperands(); ++j) {
        Value *Cij = aCS->getOperand(j);
        uint32_t idx[2] = {i, j};

        ConstantVector *CE = dyn_cast<ConstantVector>(Cij);
        if (CE && hasConstantExpr(CE)) {
          Instruction *newInst = InsertValueInst::Create(newStruct, CE, idx);
          newInst->setDebugLoc(user->getDebugLoc());
          insertBefore(newInst, user);

          breakExpressionsInVector(CE, 1, newInst);
          newStruct = newInst;
          continue;
        }

        Value *newCij = Cij;
        if (ConstantExpr *c = dyn_cast<ConstantExpr>(Cij)) {
          Instruction *newInst = c->getAsInstruction();
          newInst->setDebugLoc(user->getDebugLoc());
          newCij = newInst;

          insertBefore(newInst, user);
          (void)breakInst(newInst);
        }
        newStruct = B.CreateInsertValue(newStruct, newCij, idx);
      }
      continue;
    }

    ConstantVector *CE = dyn_cast<ConstantVector>(Ci);
    if (CE && hasConstantExpr(CE)) {
      Instruction *newInst = InsertValueInst::Create(newStruct, CE, i);
      newInst->setDebugLoc(user->getDebugLoc());
      insertBefore(newInst, user);

      breakExpressionsInVector(CE, 1, newInst);
      newStruct = newInst;
      continue;
    }

    Value *newCi = Ci;
    if (ConstantExpr *c = dyn_cast<ConstantExpr>(Ci)) {
      Instruction *newInst = c->getAsInstruction();
      newInst->setDebugLoc(user->getDebugLoc());
      newCi = newInst;

      insertBefore(newInst, user);
      (void)breakInst(newInst);
    }
    newStruct = B.CreateInsertValue(newStruct, newCi, i);
  }

  user->replaceUsesOfWith(cs, newStruct);

  if (cs->use_empty()) {
    cs->destroyConstant();
  }
  return true;
}