From: Sanjay Patel Date: Mon, 16 Aug 2021 14:28:46 +0000 (-0400) Subject: [InstCombine] allow for constant-folding in GEP transform X-Git-Tag: upstream/15.0.7~33837 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=de285eacb0113f7be32163f0166ef2c0faf18d65;p=platform%2Fupstream%2Fllvm.git [InstCombine] allow for constant-folding in GEP transform This would crash the reduced test or as described in https://llvm.org/PR51485 ...because we can't mark a constant (-expression) with 'inbounds'. --- diff --git a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp index 434659c..1a80374 100644 --- a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp +++ b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp @@ -2118,10 +2118,12 @@ Instruction *InstCombinerImpl::visitGetElementPtrInst(GetElementPtrInst &GEP) { // -- have to recreate %src & %gep // put NewSrc at same location as %src Builder.SetInsertPoint(cast(PtrOp)); - auto *NewSrc = cast( - Builder.CreateGEP(GEPEltType, SO0, GO1, Src->getName())); - NewSrc->setIsInBounds(Src->isInBounds()); - auto *NewGEP = + Value *NewSrc = + Builder.CreateGEP(GEPEltType, SO0, GO1, Src->getName()); + // Propagate 'inbounds' if the new source was not constant-folded. + if (auto *NewSrcGEPI = dyn_cast(NewSrc)) + NewSrcGEPI->setIsInBounds(Src->isInBounds()); + GetElementPtrInst *NewGEP = GetElementPtrInst::Create(GEPEltType, NewSrc, {SO1}); NewGEP->setIsInBounds(GEP.isInBounds()); return NewGEP; diff --git a/llvm/test/Transforms/InstCombine/gep-combine-loop-invariant.ll b/llvm/test/Transforms/InstCombine/gep-combine-loop-invariant.ll index faaaff8..58d16e1 100644 --- a/llvm/test/Transforms/InstCombine/gep-combine-loop-invariant.ll +++ b/llvm/test/Transforms/InstCombine/gep-combine-loop-invariant.ll @@ -186,3 +186,26 @@ loop: call void @blackhole(<2 x i8*> %e6) br label %loop } + +; This would crash because we did not expect to be able to constant fold a GEP. + +define void @PR51485(<2 x i64> %v) { +; CHECK-LABEL: @PR51485( +; CHECK-NEXT: entry: +; CHECK-NEXT: br label [[LOOP:%.*]] +; CHECK: loop: +; CHECK-NEXT: [[SL1:%.*]] = shl nuw nsw <2 x i64> [[V:%.*]], +; CHECK-NEXT: [[E6:%.*]] = getelementptr inbounds i8, i8* getelementptr (i8, i8* bitcast (void (<2 x i64>)* @PR51485 to i8*), i64 80), <2 x i64> [[SL1]] +; CHECK-NEXT: call void @blackhole(<2 x i8*> [[E6]]) +; CHECK-NEXT: br label [[LOOP]] +; +entry: + br label %loop + +loop: + %sl1 = shl nuw nsw <2 x i64> %v, + %e5 = getelementptr inbounds i8, i8* bitcast (void (<2 x i64>)* @PR51485 to i8*), <2 x i64> %sl1 + %e6 = getelementptr inbounds i8, <2 x i8*> %e5, i64 80 + call void @blackhole(<2 x i8*> %e6) + br label %loop +}