File: SimplifyConstant.cpp

package info (click to toggle)
intel-graphics-compiler 1.0.12504.6-1%2Bdeb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 83,912 kB
  • sloc: cpp: 910,147; lisp: 202,655; ansic: 15,197; python: 4,025; yacc: 2,241; lex: 1,570; pascal: 244; sh: 104; makefile: 25
file content (682 lines) | stat: -rw-r--r-- 23,733 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
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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
/*========================== begin_copyright_notice ============================

Copyright (C) 2017-2021 Intel Corporation

SPDX-License-Identifier: MIT

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

#include "Compiler/CISACodeGen/SimplifyConstant.h"
#include "Compiler/IGCPassSupport.h"
#include "Compiler/CodeGenPublicEnums.h"
#include "common/igc_regkeys.hpp"
#include "common/LLVMWarningsPush.hpp"
#include <llvm/Analysis/LoopInfo.h>
#include "llvm/IR/Constants.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/IRBuilder.h"
#include "llvmWrapper/IR/DerivedTypes.h"
#include "llvmWrapper/IR/Value.h"
#include "common/LLVMWarningsPop.hpp"
#include "common/Types.hpp"
#include "Probe/Assertion.h"

using namespace llvm;
using namespace IGC;

namespace {

    /// \brief Perform by-value simplification of loading constant data.
    ///
    /// Currently this only applies to certain constant data array initializers.
    ///
    class SimplifyConstant : public ModulePass {
    public:
        static char ID; // Pass identification, replacement for typeid
        SimplifyConstant() : ModulePass(ID) {
            initializeSimplifyConstantPass(*PassRegistry::getPassRegistry());
        }
        bool runOnModule(Module& M);

    private:
        bool process(GlobalVariable* GV);
    };

} // namespace

namespace IGC {

    IGC_INITIALIZE_PASS_BEGIN(SimplifyConstant, "SimplifyConstant", "SimplifyConstant", false, false)
        IGC_INITIALIZE_PASS_END(SimplifyConstant, "SimplifyConstant", "SimplifyConstant", false, false)

} // namespace IGC

char SimplifyConstant::ID = 0;
ModulePass* IGC::createSimplifyConstantPass() { return new SimplifyConstant(); }

bool SimplifyConstant::runOnModule(Module& M) {
    bool Changed = false;
    for (auto I = M.global_begin(); I != M.global_end(); /*empty*/) {
        GlobalVariable* GV = &(*I++);
        if (GV->user_empty() || !GV->isConstant() || !GV->hasInitializer() ||
            GV->getType()->getAddressSpace() != ADDRESS_SPACE_CONSTANT)
            continue;
        Changed |= process(GV);
    }
    return Changed;
}

namespace {

    class ConstantLoader {
    public:
        enum CInitKind {
            CK_Unknown,
            // All elements are equal.
            CK_Splat,
            // f(i) = (i % 2) == 0 ? A : B;
            CK_OddEven,
            // f(i) = (i < Pivot) ? A : B
            CK_Ladder2
        };

        explicit ConstantLoader(GlobalVariable* GV)
            : GV(GV), Kind(CK_Unknown), Pivot(0) {
            analyze();
            simplify();
        }

        bool simplified() const { return Kind != CK_Unknown; }

    private:
        void analyze();
        void simplify();

        bool matchSplat(ConstantDataArray* CDA) {
            Constant* V0 = CDA->getElementAsConstant(0);
            for (unsigned i = 1, n = CDA->getNumElements(); i < n; ++i)
                if (CDA->getElementAsConstant(i) != V0)
                    return false;
            Kind = CK_Splat;
            return true;
        }
        bool matchOddEven(ConstantDataArray* CDA) {
            Constant* V0 = CDA->getElementAsConstant(0);
            Constant* V1 = CDA->getElementAsConstant(1);
            for (unsigned i = 2, n = CDA->getNumElements(); i < n; ++i) {
                Constant* Val = CDA->getElementAsConstant(i);
                if ((i % 2 == 0 && Val != V0) || (i % 2 == 1 && Val != V1))
                    return false;
            }
            Kind = CK_OddEven;
            return true;
        }
        bool matchLadder2(ConstantDataArray* CDA) {
            Constant* V = CDA->getElementAsConstant(0);
            unsigned k = 0;
            for (unsigned i = 1, n = CDA->getNumElements(); i < n; ++i) {
                Constant* Val = CDA->getElementAsConstant(i);
                if (Val != V) {
                    if (k > 0)
                        return false;

                    V = Val;
                    k = i;
                }
            }

            Kind = CK_Ladder2;
            Pivot = k;
            return true;
        }

        GlobalVariable* GV;
        CInitKind Kind;
        // Index of the second value if exists.
        unsigned Pivot;
    };

} // namespace

void ConstantLoader::analyze() {
    auto Init = GV->getInitializer();
    auto CDA = dyn_cast<ConstantDataArray>(Init);
    if (!CDA || CDA->isString() || CDA->getNumElements() <= 1)
        return;

    matchSplat(CDA) || matchOddEven(CDA) || matchLadder2(CDA);
}

void ConstantLoader::simplify() {
    if (Kind == CK_Unknown)
        return;

    Constant* Init = GV->getInitializer();
    auto CDA = cast<ConstantDataArray>(Init);
    Constant* V0 = CDA->getElementAsConstant(0);
    Constant* V1 = nullptr;
    if (Kind == CK_OddEven)
        V1 = CDA->getElementAsConstant(1);
    else if (Kind == CK_Ladder2)
        V1 = CDA->getElementAsConstant(Pivot);

    // Replace all GEP + load uses with V0 or a select of V0 and V1.
    for (auto UI = GV->user_begin(); UI != GV->user_end(); /* empty */) {
        auto GEP = dyn_cast<GetElementPtrInst>(*UI++);
        if (!GEP || !GEP->isInBounds())
            continue;

        // Skip optimizations on function with optnone.
        Function* F = GEP->getParent()->getParent();
        if (F->hasFnAttribute(Attribute::OptimizeNone))
            continue;

        for (auto I = GEP->user_begin(); I != GEP->user_end(); /* empty */) {
            LoadInst* LI = dyn_cast<LoadInst>(*I++);
            if (!LI)
                continue;
            IGC_ASSERT(GEP->getNumIndices() == 2);
            Value* Index = GEP->getOperand(2);
            Value* Val = V0;
            IRBuilder<> Builder(LI);
            if (Kind == CK_Ladder2) {
                Value* Cmp = Builder.CreateICmpULT(
                    Index, ConstantInt::get(Index->getType(), Pivot));
                Val = Builder.CreateSelect(Cmp, V0, V1);
            }
            else if (Kind == CK_OddEven) {
                Value* Cmp = Builder.CreateTrunc(Index, Builder.getInt1Ty());
                Val = Builder.CreateSelect(Cmp, V1, V0);
            }
            LI->replaceAllUsesWith(Val);
            LI->eraseFromParent();
        }

        if (GEP->use_empty())
            GEP->eraseFromParent();
    }
}

bool SimplifyConstant::process(GlobalVariable* GV) {
    ConstantLoader Loader(GV);
    if (Loader.simplified())
        return true;

    return false;
}

namespace {

    /// \brief Promote small constant data from global to register.
    ///
    class PromoteConstant : public FunctionPass {
    public:
        static char ID; // Pass identification, replacement for typeid
        PromoteConstant() : FunctionPass(ID) {
            initializePromoteConstantPass(*PassRegistry::getPassRegistry());
        }

        void getAnalysisUsage(AnalysisUsage& AU) const override {
            AU.setPreservesCFG();
            AU.addRequired<LoopInfoWrapperPass>();
            AU.addPreserved<LoopInfoWrapperPass>();
        }

        bool runOnFunction(Function& F) override;
    };

} // namespace

namespace IGC {

    IGC_INITIALIZE_PASS_BEGIN(PromoteConstant, "PromoteConstant", "PromoteConstant", false, false)
        IGC_INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
        IGC_INITIALIZE_PASS_END(PromoteConstant, "PromoteConstant", "PromoteConstant", false, false)

} // namespace IGC

char PromoteConstant::ID = 0;
FunctionPass* IGC::createPromoteConstantPass() { return new PromoteConstant(); }

// Check uses. Within this function, this GV must be used and only used by
// in-bound GEPs each of which is also only used by loads.
//
// We only promote when there is load of GV inside loops.
//
static bool checkUses(GlobalVariable* GV, const Function* F, LoopInfo& LI, const ConstantArray* llvm_used) {
    bool LoadInLoop = false;
    for (auto U : GV->users()) {
        // Because of ProgramScopeConstantAnalysis there might be user in llvm.used
        // let's ignore this one, as it doesn't prevent opt.
        // TODO: is there any other case of Constant to worry about. (?)
        if (auto Const = dyn_cast<Constant>(U))
        {
            bool foundInLLVMUsed = false;
            unsigned numOperands = llvm_used ? llvm_used->getNumOperands() : 0;
            for (unsigned i = 0; i != numOperands; ++i)
            {
                Value* gvi = llvm_used->getOperand(i)->stripPointerCastsNoFollowAliases();
                if (gvi == Const->stripPointerCastsNoFollowAliases())
                {
                    foundInLLVMUsed = true;
                    break;
                }
            }
            if (foundInLLVMUsed)
                continue;
        }

        auto Inst = dyn_cast<Instruction>(U);
        if (!Inst)
            // TODO: this may be a const expr.
            return false;

        if (F != Inst->getParent()->getParent())
            continue;

        auto GEP = dyn_cast<GetElementPtrInst>(Inst);
        if (!GEP || !GEP->isInBounds())
            return false;

        for (auto V : GEP->users()) {
            auto Inst = dyn_cast<LoadInst>(V);
            if (!Inst)
                return false;
            if (LI.getLoopFor(Inst->getParent()) != nullptr)
                LoadInLoop = true;
        }
    }

    if (IGC_IS_FLAG_ENABLED(AllowNonLoopConstantPromotion))
        return true;

    return LoadInLoop;
}

// IGC only allows the following vector sizes:
//
//------------------------------------
//                 name       size
//                         in elements
//------------------------------------
// IGC_IR_VECTOR_TYPE(x1,           1)
// IGC_IR_VECTOR_TYPE(x2,           2)
// IGC_IR_VECTOR_TYPE(x3,           3)
// IGC_IR_VECTOR_TYPE(x4,           4)
// IGC_IR_VECTOR_TYPE(x5,           5)
// IGC_IR_VECTOR_TYPE(x8,           8)
// IGC_IR_VECTOR_TYPE(x10,         10)
// IGC_IR_VECTOR_TYPE(x16,         16)
// IGC_IR_VECTOR_TYPE(x32,         32)
//
// check or return a legal vector size. 0 means illegal.
static unsigned getLegalVectorSize(unsigned N) {
#if 0
    if (N > 32)
        return 0;

    if (N == 6 || N == 7)
        return 8;
    if (N == 9)
        return 10;
    if (N >= 11 && N <= 16)
        return 16;
    if (N >= 17 && N <= 32)
        return 32;
    return N;
#else
    // Only emit power-of-two vector sizes.
    N = 1U << Log2_32_Ceil(N);
    return (N > 32) ? 0 : N;
#endif
}

// Check vector size. We may demote the data type if all values can fit into
// smaller data type.
//
static bool checkSize(GlobalVariable* GV, IGCLLVM::FixedVectorType*& DataType,
    bool& IsSigned) {
    Constant* Init = GV->getInitializer();
    IGC_ASSERT(isa<ArrayType>(Init->getType()));
    ArrayType* ArrayTy = cast<ArrayType>(Init->getType());
    unsigned N = (unsigned)ArrayTy->getArrayNumElements();
    Type* BaseTy = ArrayTy->getArrayElementType();
    unsigned VectorSize = 1;
    if (auto VT = dyn_cast<IGCLLVM::FixedVectorType>(BaseTy)) {
        BaseTy = VT->getElementType();
        VectorSize = int_cast<unsigned>(VT->getNumElements());
        N *= VectorSize;
    }
    unsigned VS = getLegalVectorSize(N);

    // Allow experimental overriding for bigger than 32 elem. tables
    if (IGC_IS_FLAG_ENABLED(AllowNonLoopConstantPromotion))
        VS = 1U << Log2_32_Ceil(N);

    if (VS == 0)
        return false;

    if (BaseTy->isIntegerTy()) {
        // Cached integer types.
        Type* Int8Ty = IntegerType::get(GV->getContext(), 8);
        Type* Int16Ty = IntegerType::get(GV->getContext(), 16);

        // [i32 -1, i32 1] can be represented as {<i8 -1, i8 1>, Signed}
        // and [i32 200, i32 1] can be represented as {<i8 200, i8 1>, !Signed}
        int64_t Min = INT64_MAX, Max = INT64_MIN;
        for (unsigned i = 0; i < ArrayTy->getArrayNumElements(); ++i) {
            auto Elt = Init->getAggregateElement(i);
            if (isa<UndefValue>(Elt))
                continue;
            if (Elt->getType()->isVectorTy()) {
                for (unsigned j = 0; j < VectorSize; ++j) {
                    auto VecElt = Elt->getAggregateElement(j);
                    int64_t Val = cast<ConstantInt>(VecElt)->getSExtValue();
                    Min = std::min(Min, Val);
                    Max = std::max(Max, Val);
                }
            }
            else {
                int64_t Val = cast<ConstantInt>(Elt)->getSExtValue();
                Min = std::min(Min, Val);
                Max = std::max(Max, Val);
            }
        }

        unsigned BaseSzInBits = (unsigned int)BaseTy->getPrimitiveSizeInBits();
        IGC_ASSERT(BaseSzInBits >= 8);
        if (BaseSzInBits > 8 && Min >= 0 && Max <= UINT8_MAX) {
            BaseTy = Int8Ty;
            IsSigned = false;
        }
        else if (BaseSzInBits > 8 && Min >= INT8_MIN && Max <= INT8_MAX) {
            BaseTy = Int8Ty;
            IsSigned = true;
        }
        else if (BaseSzInBits > 16 && Min >= 0 && Max <= UINT16_MAX) {
            BaseTy = Int16Ty;
            IsSigned = false;
        }
        else if (BaseSzInBits > 16 && Min >= INT16_MIN && Max <= INT16_MAX) {
            BaseTy = Int16Ty;
            IsSigned = true;
        }
    }

    // Two GRFs by default.
    unsigned ThresholdInBits = IGC_GET_FLAG_VALUE(ConstantPromotionSize) * 256;
    unsigned TotalSzInBits = (unsigned int)BaseTy->getPrimitiveSizeInBits() * VS;
    if (TotalSzInBits <= ThresholdInBits) {
        DataType = IGCLLVM::FixedVectorType::get(BaseTy, VS);
        return true;
    }

    return false;
}

// Check if a vector of data could be built out of initializer.
static bool checkType(GlobalVariable* GV) {
    Constant* Init = GV->getInitializer();
    if (auto CDA = dyn_cast<ConstantDataArray>(Init))
        return !CDA->isString();

    // Not simply data. Need to check if all elements have the same type in a
    // constant array.
    if (auto CA = dyn_cast<ConstantArray>(Init)) {
        auto EltTy = CA->getType()->getElementType();
        return EltTy->isFloatingPointTy() || EltTy->isIntegerTy() ||
            EltTy->isVectorTy();
    }

    return false;
}

// Extract N elements from a vector, Idx, ... Idx + N - 1. Return a scalar
// or a vector value depending on N.
static Value* extractNElts(unsigned N, Value* VectorData, Value* Offset,
    IRBuilder<>& IRB) {
    if (N == 1)
        return IRB.CreateExtractElement(VectorData, Offset);

    Type* Ty = cast<VectorType>(VectorData->getType())->getElementType();
    Ty = IGCLLVM::FixedVectorType::get(Ty, N);
    Value* Result = UndefValue::get(Ty);
    for (unsigned i = 0; i < N; ++i) {
        Value* VectorIdx = ConstantInt::get(Offset->getType(), i);
        if (i == 0) {
            auto Val = IRB.CreateExtractElement(VectorData, Offset);
            Result = IRB.CreateInsertElement(Result, Val, VectorIdx);
        }
        else {
            auto Idx = IRB.CreateAdd(Offset, VectorIdx);
            auto Val = IRB.CreateExtractElement(VectorData, Idx);
            Result = IRB.CreateInsertElement(Result, Val, VectorIdx);
        }
    }

    return Result;
}

static Constant* getConstantVal(Type* VEltTy, Constant* V, bool IsSigned) {
    if (V->getType() == VEltTy)
        return V;
    IGC_ASSERT(VEltTy->isIntegerTy());
    int64_t IVal = cast<ConstantInt>(V)->getSExtValue();
    return ConstantInt::get(VEltTy, IVal, IsSigned);
}

static void promote(GlobalVariable* GV, IGCLLVM::FixedVectorType* AllocaType, bool IsSigned,
    Function* F) {
    // Build the constant vector from constant array.
    unsigned VS = int_cast<unsigned>(AllocaType->getNumElements());
    SmallVector<Constant*, 16> Vals(VS, nullptr);
    Type* VEltTy = AllocaType->getElementType();
    auto Init = GV->getInitializer();

    if (auto CDA = dyn_cast<ConstantDataArray>(Init)) {
        unsigned NElts = CDA->getNumElements();
        for (unsigned i = 0; i < NElts; ++i) {
            Constant* Elt = CDA->getAggregateElement(i);
            IGC_ASSERT_MESSAGE(nullptr != Elt, "Null AggregateElement");
            Vals[i] = getConstantVal(VEltTy, Elt, IsSigned);
        }
    }
    else {
        IGC_ASSERT_MESSAGE(isa<ConstantArray>(Init), "out of sync");
        ConstantArray* CA = cast<ConstantArray>(Init);
        unsigned NElts = CA->getNumOperands();
        for (unsigned i = 0; i < NElts; ++i) {
            Constant* const Elt = CA->getAggregateElement(i);
            IGC_ASSERT_MESSAGE(nullptr != Elt, "Null AggregateElement");
            if (auto EltTy = dyn_cast<VectorType>(Elt->getType())) {
                unsigned VectorSize = (unsigned)cast<IGCLLVM::FixedVectorType>(EltTy)->getNumElements();
                for (unsigned j = 0; j < VectorSize; ++j) {
                    Constant* V = Elt->getAggregateElement(j);
                    Vals[i * VectorSize + j] = getConstantVal(VEltTy, V, IsSigned);
                }
            }
            else
                Vals[i] = getConstantVal(VEltTy, Elt, IsSigned);
        }
    }
    // Fill the missing values with undef, if any.
    for (int i = VS - 1; i >= 0; --i) {
        if (Vals[i] != nullptr)
            break;
        Vals[i] = UndefValue::get(VEltTy);
    }
    Constant* VectorData = ConstantVector::get(Vals);

    // Transform all uses
    for (auto UI = GV->user_begin(); UI != GV->user_end(); /*empty*/) {
        auto GEP = dyn_cast<GetElementPtrInst>(*UI++);
        // might be Constant user in llvm.used
        if (!GEP || GEP->getParent()->getParent() != F)
            continue;
        IGC_ASSERT(GEP->getNumIndices() == 2);
        // This is the index to address the array, and the first index is to address
        // the global variable itself.
        Value* Index = GEP->getOperand(2);
        // Demote the index type to int32 to avoid 64 multiplications during vISA
        // emission, e.g. it is illegal to emit Q x W.
        if (Index->getType()->getPrimitiveSizeInBits() > 32) {
            IRBuilder<> Builder(GEP);
            Index = Builder.CreateTrunc(Index, Builder.getInt32Ty());
        }
        for (auto I = GEP->user_begin(); I != GEP->user_end(); /*empty*/) {
            auto LI = dyn_cast<LoadInst>(*I++);
            IGC_ASSERT_MESSAGE(nullptr != LI, "nullptr");
            IRBuilder<> Builder(LI);
            Type* Ty = LI->getType();
            unsigned N = 1;
            Value* Offset = Index;
            if (Ty->isVectorTy()) {
                N = (unsigned)cast<IGCLLVM::FixedVectorType>(Ty)->getNumElements();
                Offset = Builder.CreateMul(Offset, ConstantInt::get(Offset->getType(), N));
            }
            Value* Val = extractNElts(N, VectorData, Offset, Builder);
            if (Val->getType() != LI->getType()) {
                IGC_ASSERT(Val->getType()->isIntOrIntVectorTy());
                Val = Builder.CreateIntCast(Val, LI->getType(), IsSigned);
            }
            LI->replaceAllUsesWith(Val);
            LI->eraseFromParent();
        }
    }
}

static Constant* getElt(Constant* Init, int k) {
    int n = (int)Init->getType()->getArrayNumElements();
    if (k >= n)
        return UndefValue::get(Init->getType()->getArrayElementType());
    return Init->getAggregateElement(k);
};

// Recursively emit cmp+sel tree.
static Value* getVal(IRBuilder<>& Builder, Constant* Init, Value* Index,
    int Low, int Hi) {
    IGC_ASSERT(Hi > Low);
    Type* IdxTy = Index->getType();
    // base case.
    if (Hi == 1 + Low) {
        Value* Cmp = Builder.CreateICmpEQ(Index, ConstantInt::get(IdxTy, Low));
        return Builder.CreateSelect(Cmp, getElt(Init, Low), getElt(Init, Hi));
    }

    // There are more than two elements to be compared.
    int Mid = (Low + Hi + 1) / 2;
    Value* LHS = getVal(Builder, Init, Index, Low, Mid - 1);
    Value* RHS = getVal(Builder, Init, Index, Mid, Hi);
    Value* Cmp = Builder.CreateICmpSLT(Index, ConstantInt::get(IdxTy, Mid));
    return Builder.CreateSelect(Cmp, LHS, RHS);
}

static bool rewriteAsCmpSel(GlobalVariable* GV, Function& F) {
    bool Changed = false;

    Type* Ty = GV->getInitializer()->getType();
    IGC_ASSERT(Ty->isArrayTy());
    unsigned NElts = (unsigned)Ty->getArrayNumElements();

    for (auto UI = GV->user_begin(); UI != GV->user_end(); /*empty*/) {
        auto GEP = dyn_cast<GetElementPtrInst>(*UI++);
        // might be Constant user in llvm.used
        if (!GEP || GEP->getParent()->getParent() != &F)
            continue;

        // This is the index to address the array, and the first index is to
        // address the global variable itself.
        IGC_ASSERT(GEP->getNumIndices() == 2);
        Value* Index = GEP->getOperand(2);
        if (Index->getType()->getPrimitiveSizeInBits() > 32) {
            IRBuilder<> Builder(GEP);
            Index = Builder.CreateTrunc(Index, Builder.getInt32Ty());
        }
        for (auto I = GEP->user_begin(); I != GEP->user_end(); /*empty*/) {
            auto LI = dyn_cast<LoadInst>(*I++);
            IGC_ASSERT_MESSAGE(nullptr != LI, "nullptr");
            IRBuilder<> Builder(LI);

            int n = (int)NextPowerOf2(NElts - 1);
            Value* Val = nullptr;
            if (n > 1)
                Val = getVal(Builder, GV->getInitializer(), Index, 0, n - 1);
            else
                Val = getElt(GV->getInitializer(), 0);
            LI->replaceAllUsesWith(Val);
            LI->eraseFromParent();
            Changed = true;
        }
    }

    return Changed;
}

// Check if it is profitable to emit cmp-sel.
//
// For an array of size N = 2^k, (N - 1) cmp + (N - 1) sel is needed to extract
// a single element. We use the following threshold:
//
// (1) if N <= 4, return true, or
// (2) if N <= 8 and element type is vector, return true, otherwise
// (3) return false.
//
// [4 x float],          6 ops, presumably better than a send
// [4 x <2 x float> ],   6 ops, ditto
// [8 x <2 x float> ],  14 ops, ditto
// [16 x <2 x float> ], 30 ops, close to a send
//
static bool isCmpSelProfitable(GlobalVariable* GV) {
    Constant* Init = GV->getInitializer();
    unsigned NElts = (unsigned)Init->getType()->getArrayNumElements();
    unsigned CmpSelSize = IGC_GET_FLAG_VALUE(ConstantPromotionCmpSelSize);

    if (NElts <= CmpSelSize)
        return true;
    Type* EltTy = Init->getType()->getArrayElementType();
    return EltTy->isVectorTy() && NElts <= CmpSelSize * 2;
}

bool PromoteConstant::runOnFunction(Function& F) {
    if (skipFunction(F))
        return false;

    LoopInfo& LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
    Module* M = F.getParent();
    bool Changed = false;

    auto gv = M->getGlobalVariable("llvm.used");
    ConstantArray* llvm_used = gv ? dyn_cast_or_null<ConstantArray>(gv->getInitializer()) : nullptr;

    for (auto I = M->global_begin(); I != M->global_end(); /*empty*/) {
        GlobalVariable* GV = &(*I++);
        if (GV->user_empty() || !GV->isConstant() || !GV->hasInitializer() ||
            GV->getType()->getAddressSpace() != ADDRESS_SPACE_CONSTANT)
            continue;
        if (!checkType(GV))
            continue;
        if (!checkUses(GV, &F, LI, llvm_used))
            continue;

        // Rewrite as cmp+sel sequence if profitable.
        if (isCmpSelProfitable(GV)) {
            Changed |= rewriteAsCmpSel(GV, F);
            continue;
        }

        // If possible demote the data into smaller type. Uses of value will be
        // promoted back with ZExt or SExt.
        IGCLLVM::FixedVectorType* AllocaType = nullptr;
        bool IsSigned = false;
        if (!checkSize(GV, AllocaType, IsSigned))
            continue;

        IGC_ASSERT(AllocaType);
        promote(GV, AllocaType, IsSigned, &F);
        Changed = true;
    }

    return Changed;
}