@@ -87,9 +87,8 @@ static void fixI8UseChain(Instruction &I,
8787 return ;
8888 }
8989
90- if (auto *Load = dyn_cast<LoadInst>(&I)) {
91- if (!I.getType ()->isIntegerTy (8 ))
92- return ;
90+ if (auto *Load = dyn_cast<LoadInst>(&I);
91+ Load && I.getType ()->isIntegerTy (8 )) {
9392 SmallVector<Value *> NewOperands;
9493 ProcessOperands (NewOperands);
9594 Type *ElementType = NewOperands[0 ]->getType ();
@@ -103,6 +102,31 @@ static void fixI8UseChain(Instruction &I,
103102 return ;
104103 }
105104
105+ if (auto *Load = dyn_cast<LoadInst>(&I);
106+ Load && isa<ConstantExpr>(Load->getPointerOperand ())) {
107+ auto *CE = dyn_cast<ConstantExpr>(Load->getPointerOperand ());
108+ if (!(CE->getOpcode () == Instruction::GetElementPtr))
109+ return ;
110+ auto *GEP = dyn_cast<GEPOperator>(CE);
111+ if (!GEP->getSourceElementType ()->isIntegerTy (8 ))
112+ return ;
113+
114+ Type *ElementType = Load->getType ();
115+ ConstantInt *Offset = dyn_cast<ConstantInt>(GEP->getOperand (1 ));
116+ uint32_t ByteOffset = Offset->getZExtValue ();
117+ uint32_t ElemSize = Load->getDataLayout ().getTypeAllocSize (ElementType);
118+ uint32_t Index = ByteOffset / ElemSize;
119+ Value *NewGEP = Builder.CreateGEP (ElementType, GEP->getPointerOperand (),
120+ Builder.getInt32 (Index), GEP->getName (),
121+ GEP->getNoWrapFlags ());
122+
123+ LoadInst *NewLoad = Builder.CreateLoad (ElementType, NewGEP);
124+ ReplacedValues[Load] = NewLoad;
125+ Load->replaceAllUsesWith (NewLoad);
126+ ToRemove.push_back (Load);
127+ return ;
128+ }
129+
106130 if (auto *BO = dyn_cast<BinaryOperator>(&I)) {
107131 if (!I.getType ()->isIntegerTy (8 ))
108132 return ;
@@ -178,6 +202,8 @@ static void fixI8UseChain(Instruction &I,
178202 Type *ElementType = BasePtr->getType ();
179203 if (auto *AI = dyn_cast<AllocaInst>(BasePtr))
180204 ElementType = AI->getAllocatedType ();
205+ if (auto *GV = dyn_cast<GlobalVariable>(BasePtr))
206+ ElementType = GV->getValueType ();
181207 if (auto *ArrTy = dyn_cast<ArrayType>(ElementType))
182208 ElementType = ArrTy->getArrayElementType ();
183209
0 commit comments