160
160
#include " llvm/Analysis/ValueTracking.h"
161
161
#include " llvm/IR/Constants.h"
162
162
#include " llvm/IR/DataLayout.h"
163
+ #include " llvm/IR/Dominators.h"
163
164
#include " llvm/IR/Instructions.h"
164
165
#include " llvm/IR/LLVMContext.h"
165
166
#include " llvm/IR/Module.h"
@@ -202,23 +203,26 @@ namespace {
202
203
// / 5); nor can we transform (3 * (a + 5)) to (3 * a + 5), however in this case,
203
204
// / -instcombine probably already optimized (3 * (a + 5)) to (3 * a + 15).
204
205
class ConstantOffsetExtractor {
205
- public:
206
+ public:
206
207
// / Extracts a constant offset from the given GEP index. It returns the
207
208
// / new index representing the remainder (equal to the original index minus
208
209
// / the constant offset), or nullptr if we cannot extract a constant offset.
209
210
// / \p Idx The given GEP index
210
211
// / \p GEP The given GEP
211
212
// / \p UserChainTail Outputs the tail of UserChain so that we can
212
213
// / garbage-collect unused instructions in UserChain.
213
- static Value *Extract (Value *Idx, GetElementPtrInst *GEP,
214
- User *&UserChainTail);
214
+ static Value *Extract (Value *Idx, GetElementPtrInst *GEP,
215
+ User *&UserChainTail, const DominatorTree *DT );
215
216
// / Looks for a constant offset from the given GEP index without extracting
216
217
// / it. It returns the numeric value of the extracted constant offset (0 if
217
218
// / failed). The meaning of the arguments are the same as Extract.
218
- static int64_t Find (Value *Idx, GetElementPtrInst *GEP);
219
+ static int64_t Find (Value *Idx, GetElementPtrInst *GEP,
220
+ const DominatorTree *DT);
219
221
220
- private:
221
- ConstantOffsetExtractor (Instruction *InsertionPt) : IP(InsertionPt) {}
222
+ private:
223
+ ConstantOffsetExtractor (Instruction *InsertionPt, const DominatorTree *DT)
224
+ : IP(InsertionPt), DL(InsertionPt->getModule ()->getDataLayout()), DT(DT) {
225
+ }
222
226
// / Searches the expression that computes V for a non-zero constant C s.t.
223
227
// / V can be reassociated into the form V' + C. If the searching is
224
228
// / successful, returns C and update UserChain as a def-use chain from C to V;
@@ -276,13 +280,6 @@ class ConstantOffsetExtractor {
276
280
// / returns "sext i32 (zext i16 V to i32) to i64".
277
281
Value *applyExts (Value *V);
278
282
279
- // / Returns true if LHS and RHS have no bits in common, i.e., for every n
280
- // / the n-th bit of either LHS, or RHS is 0.
281
- bool NoCommonBits (Value *LHS, Value *RHS) const ;
282
- // / Computes which bits are known to be one or zero.
283
- // / \p KnownOne Mask of all bits that are known to be one.
284
- // / \p KnownZero Mask of all bits that are known to be zero.
285
- void ComputeKnownBits (Value *V, APInt &KnownOne, APInt &KnownZero) const ;
286
283
// / A helper function that returns whether we can trace into the operands
287
284
// / of binary operator BO for a constant offset.
288
285
// /
@@ -304,28 +301,35 @@ class ConstantOffsetExtractor {
304
301
// / sext/zext instructions along UserChain.
305
302
SmallVector<CastInst *, 16 > ExtInsts;
306
303
Instruction *IP; // / Insertion position of cloned instructions.
304
+ const DataLayout &DL;
305
+ const DominatorTree *DT;
307
306
};
308
307
309
308
// / \brief A pass that tries to split every GEP in the function into a variadic
310
309
// / base and a constant offset. It is a FunctionPass because searching for the
311
310
// / constant offset may inspect other basic blocks.
312
311
class SeparateConstOffsetFromGEP : public FunctionPass {
313
- public:
312
+ public:
314
313
static char ID;
315
314
SeparateConstOffsetFromGEP (const TargetMachine *TM = nullptr ,
316
315
bool LowerGEP = false )
317
- : FunctionPass(ID), TM(TM), LowerGEP(LowerGEP) {
316
+ : FunctionPass(ID), DL( nullptr ), DT( nullptr ), TM(TM), LowerGEP(LowerGEP) {
318
317
initializeSeparateConstOffsetFromGEPPass (*PassRegistry::getPassRegistry ());
319
318
}
320
319
321
320
void getAnalysisUsage (AnalysisUsage &AU) const override {
321
+ AU.addRequired <DominatorTreeWrapperPass>();
322
322
AU.addRequired <TargetTransformInfoWrapperPass>();
323
323
AU.setPreservesCFG ();
324
324
}
325
325
326
+ bool doInitialization (Module &M) override {
327
+ DL = &M.getDataLayout ();
328
+ return false ;
329
+ }
326
330
bool runOnFunction (Function &F) override ;
327
331
328
- private:
332
+ private:
329
333
// / Tries to split the given GEP into a variadic base and a constant offset,
330
334
// / and returns true if the splitting succeeds.
331
335
bool splitGEP (GetElementPtrInst *GEP);
@@ -372,6 +376,8 @@ class SeparateConstOffsetFromGEP : public FunctionPass {
372
376
// / Verify F is free of dead code.
373
377
void verifyNoDeadCode (Function &F);
374
378
379
+ const DataLayout *DL;
380
+ const DominatorTree *DT;
375
381
const TargetMachine *TM;
376
382
// / Whether to lower a GEP with multiple indices into arithmetic operations or
377
383
// / multiple GEPs with a single index.
@@ -384,6 +390,7 @@ INITIALIZE_PASS_BEGIN(
384
390
SeparateConstOffsetFromGEP, " separate-const-offset-from-gep" ,
385
391
" Split GEPs to a variadic base and a constant offset for better CSE" , false ,
386
392
false )
393
+ INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
387
394
INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass)
388
395
INITIALIZE_PASS_END(
389
396
SeparateConstOffsetFromGEP, " separate-const-offset-from-gep" ,
@@ -412,7 +419,8 @@ bool ConstantOffsetExtractor::CanTraceInto(bool SignExtended,
412
419
Value *LHS = BO->getOperand (0 ), *RHS = BO->getOperand (1 );
413
420
// Do not trace into "or" unless it is equivalent to "add". If LHS and RHS
414
421
// don't have common bits, (LHS | RHS) is equivalent to (LHS + RHS).
415
- if (BO->getOpcode () == Instruction::Or && !NoCommonBits (LHS, RHS))
422
+ if (BO->getOpcode () == Instruction::Or &&
423
+ !haveNoCommonBitsSet (LHS, RHS, DL, nullptr , BO, DT))
416
424
return false ;
417
425
418
426
// In addition, tracing into BO requires that its surrounding s/zext (if
@@ -497,9 +505,8 @@ APInt ConstantOffsetExtractor::find(Value *V, bool SignExtended,
497
505
ConstantOffset = CI->getValue ();
498
506
} else if (BinaryOperator *BO = dyn_cast<BinaryOperator>(V)) {
499
507
// Trace into subexpressions for more hoisting opportunities.
500
- if (CanTraceInto (SignExtended, ZeroExtended, BO, NonNegative)) {
508
+ if (CanTraceInto (SignExtended, ZeroExtended, BO, NonNegative))
501
509
ConstantOffset = findInEitherOperand (BO, SignExtended, ZeroExtended);
502
- }
503
510
} else if (isa<SExtInst>(V)) {
504
511
ConstantOffset = find (U->getOperand (0 ), /* SignExtended */ true ,
505
512
ZeroExtended, NonNegative).sext (BitWidth);
@@ -642,8 +649,9 @@ Value *ConstantOffsetExtractor::removeConstOffset(unsigned ChainIndex) {
642
649
}
643
650
644
651
Value *ConstantOffsetExtractor::Extract (Value *Idx, GetElementPtrInst *GEP,
645
- User *&UserChainTail) {
646
- ConstantOffsetExtractor Extractor (GEP);
652
+ User *&UserChainTail,
653
+ const DominatorTree *DT) {
654
+ ConstantOffsetExtractor Extractor (GEP, DT);
647
655
// Find a non-zero constant offset first.
648
656
APInt ConstantOffset =
649
657
Extractor.find (Idx, /* SignExtended */ false , /* ZeroExtended */ false ,
@@ -658,37 +666,19 @@ Value *ConstantOffsetExtractor::Extract(Value *Idx, GetElementPtrInst *GEP,
658
666
return IdxWithoutConstOffset;
659
667
}
660
668
661
- int64_t ConstantOffsetExtractor::Find (Value *Idx, GetElementPtrInst *GEP) {
669
+ int64_t ConstantOffsetExtractor::Find (Value *Idx, GetElementPtrInst *GEP,
670
+ const DominatorTree *DT) {
662
671
// If Idx is an index of an inbound GEP, Idx is guaranteed to be non-negative.
663
- return ConstantOffsetExtractor (GEP)
672
+ return ConstantOffsetExtractor (GEP, DT )
664
673
.find (Idx, /* SignExtended */ false , /* ZeroExtended */ false ,
665
674
GEP->isInBounds ())
666
675
.getSExtValue ();
667
676
}
668
677
669
- void ConstantOffsetExtractor::ComputeKnownBits (Value *V, APInt &KnownOne,
670
- APInt &KnownZero) const {
671
- IntegerType *IT = cast<IntegerType>(V->getType ());
672
- KnownOne = APInt (IT->getBitWidth (), 0 );
673
- KnownZero = APInt (IT->getBitWidth (), 0 );
674
- const DataLayout &DL = IP->getModule ()->getDataLayout ();
675
- llvm::computeKnownBits (V, KnownZero, KnownOne, DL, 0 );
676
- }
677
-
678
- bool ConstantOffsetExtractor::NoCommonBits (Value *LHS, Value *RHS) const {
679
- assert (LHS->getType () == RHS->getType () &&
680
- " LHS and RHS should have the same type" );
681
- APInt LHSKnownOne, LHSKnownZero, RHSKnownOne, RHSKnownZero;
682
- ComputeKnownBits (LHS, LHSKnownOne, LHSKnownZero);
683
- ComputeKnownBits (RHS, RHSKnownOne, RHSKnownZero);
684
- return (LHSKnownZero | RHSKnownZero).isAllOnesValue ();
685
- }
686
-
687
678
bool SeparateConstOffsetFromGEP::canonicalizeArrayIndicesToPointerSize (
688
679
GetElementPtrInst *GEP) {
689
680
bool Changed = false ;
690
- const DataLayout &DL = GEP->getModule ()->getDataLayout ();
691
- Type *IntPtrTy = DL.getIntPtrType (GEP->getType ());
681
+ Type *IntPtrTy = DL->getIntPtrType (GEP->getType ());
692
682
gep_type_iterator GTI = gep_type_begin (*GEP);
693
683
for (User::op_iterator I = GEP->op_begin () + 1 , E = GEP->op_end ();
694
684
I != E; ++I, ++GTI) {
@@ -709,19 +699,18 @@ SeparateConstOffsetFromGEP::accumulateByteOffset(GetElementPtrInst *GEP,
709
699
NeedsExtraction = false ;
710
700
int64_t AccumulativeByteOffset = 0 ;
711
701
gep_type_iterator GTI = gep_type_begin (*GEP);
712
- const DataLayout &DL = GEP->getModule ()->getDataLayout ();
713
702
for (unsigned I = 1 , E = GEP->getNumOperands (); I != E; ++I, ++GTI) {
714
703
if (isa<SequentialType>(*GTI)) {
715
704
// Tries to extract a constant offset from this GEP index.
716
705
int64_t ConstantOffset =
717
- ConstantOffsetExtractor::Find (GEP->getOperand (I), GEP);
706
+ ConstantOffsetExtractor::Find (GEP->getOperand (I), GEP, DT );
718
707
if (ConstantOffset != 0 ) {
719
708
NeedsExtraction = true ;
720
709
// A GEP may have multiple indices. We accumulate the extracted
721
710
// constant offset to a byte offset, and later offset the remainder of
722
711
// the original GEP with this byte offset.
723
712
AccumulativeByteOffset +=
724
- ConstantOffset * DL. getTypeAllocSize (GTI.getIndexedType ());
713
+ ConstantOffset * DL-> getTypeAllocSize (GTI.getIndexedType ());
725
714
}
726
715
} else if (LowerGEP) {
727
716
StructType *StTy = cast<StructType>(*GTI);
@@ -730,7 +719,7 @@ SeparateConstOffsetFromGEP::accumulateByteOffset(GetElementPtrInst *GEP,
730
719
if (Field != 0 ) {
731
720
NeedsExtraction = true ;
732
721
AccumulativeByteOffset +=
733
- DL. getStructLayout (StTy)->getElementOffset (Field);
722
+ DL-> getStructLayout (StTy)->getElementOffset (Field);
734
723
}
735
724
}
736
725
}
@@ -740,8 +729,7 @@ SeparateConstOffsetFromGEP::accumulateByteOffset(GetElementPtrInst *GEP,
740
729
void SeparateConstOffsetFromGEP::lowerToSingleIndexGEPs (
741
730
GetElementPtrInst *Variadic, int64_t AccumulativeByteOffset) {
742
731
IRBuilder<> Builder (Variadic);
743
- const DataLayout &DL = Variadic->getModule ()->getDataLayout ();
744
- Type *IntPtrTy = DL.getIntPtrType (Variadic->getType ());
732
+ Type *IntPtrTy = DL->getIntPtrType (Variadic->getType ());
745
733
746
734
Type *I8PtrTy =
747
735
Builder.getInt8PtrTy (Variadic->getType ()->getPointerAddressSpace ());
@@ -761,7 +749,7 @@ void SeparateConstOffsetFromGEP::lowerToSingleIndexGEPs(
761
749
continue ;
762
750
763
751
APInt ElementSize = APInt (IntPtrTy->getIntegerBitWidth (),
764
- DL. getTypeAllocSize (GTI.getIndexedType ()));
752
+ DL-> getTypeAllocSize (GTI.getIndexedType ()));
765
753
// Scale the index by element size.
766
754
if (ElementSize != 1 ) {
767
755
if (ElementSize.isPowerOf2 ()) {
794
782
SeparateConstOffsetFromGEP::lowerToArithmetics (GetElementPtrInst *Variadic,
795
783
int64_t AccumulativeByteOffset) {
796
784
IRBuilder<> Builder (Variadic);
797
- const DataLayout &DL = Variadic->getModule ()->getDataLayout ();
798
- Type *IntPtrTy = DL.getIntPtrType (Variadic->getType ());
785
+ Type *IntPtrTy = DL->getIntPtrType (Variadic->getType ());
799
786
800
787
Value *ResultPtr = Builder.CreatePtrToInt (Variadic->getOperand (0 ), IntPtrTy);
801
788
gep_type_iterator GTI = gep_type_begin (*Variadic);
@@ -811,7 +798,7 @@ SeparateConstOffsetFromGEP::lowerToArithmetics(GetElementPtrInst *Variadic,
811
798
continue ;
812
799
813
800
APInt ElementSize = APInt (IntPtrTy->getIntegerBitWidth (),
814
- DL. getTypeAllocSize (GTI.getIndexedType ()));
801
+ DL-> getTypeAllocSize (GTI.getIndexedType ()));
815
802
// Scale the index by element size.
816
803
if (ElementSize != 1 ) {
817
804
if (ElementSize.isPowerOf2 ()) {
@@ -887,7 +874,7 @@ bool SeparateConstOffsetFromGEP::splitGEP(GetElementPtrInst *GEP) {
887
874
Value *OldIdx = GEP->getOperand (I);
888
875
User *UserChainTail;
889
876
Value *NewIdx =
890
- ConstantOffsetExtractor::Extract (OldIdx, GEP, UserChainTail);
877
+ ConstantOffsetExtractor::Extract (OldIdx, GEP, UserChainTail, DT );
891
878
if (NewIdx != nullptr ) {
892
879
// Switches to the index with the constant offset removed.
893
880
GEP->setOperand (I, NewIdx);
@@ -969,10 +956,9 @@ bool SeparateConstOffsetFromGEP::splitGEP(GetElementPtrInst *GEP) {
969
956
// Per ANSI C standard, signed / unsigned = unsigned and signed % unsigned =
970
957
// unsigned.. Therefore, we cast ElementTypeSizeOfGEP to signed because it is
971
958
// used with unsigned integers later.
972
- const DataLayout &DL = GEP->getModule ()->getDataLayout ();
973
959
int64_t ElementTypeSizeOfGEP = static_cast <int64_t >(
974
- DL. getTypeAllocSize (GEP->getType ()->getElementType ()));
975
- Type *IntPtrTy = DL. getIntPtrType (GEP->getType ());
960
+ DL-> getTypeAllocSize (GEP->getType ()->getElementType ()));
961
+ Type *IntPtrTy = DL-> getIntPtrType (GEP->getType ());
976
962
if (AccumulativeByteOffset % ElementTypeSizeOfGEP == 0 ) {
977
963
// Very likely. As long as %gep is natually aligned, the byte offset we
978
964
// extracted should be a multiple of sizeof(*%gep).
@@ -1019,6 +1005,8 @@ bool SeparateConstOffsetFromGEP::runOnFunction(Function &F) {
1019
1005
if (DisableSeparateConstOffsetFromGEP)
1020
1006
return false ;
1021
1007
1008
+ DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree ();
1009
+
1022
1010
bool Changed = false ;
1023
1011
for (Function::iterator B = F.begin (), BE = F.end (); B != BE; ++B) {
1024
1012
for (BasicBlock::iterator I = B->begin (), IE = B->end (); I != IE; ) {
0 commit comments