diff options
| author | Rafael Espindola <rafael.espindola@gmail.com> | 2011-07-11 03:43:47 +0000 |
|---|---|---|
| committer | Rafael Espindola <rafael.espindola@gmail.com> | 2011-07-11 03:43:47 +0000 |
| commit | 403256763fc91a616a517a0ba0b128d28b8e166f (patch) | |
| tree | b30dddd941cf394f908a8df9371ced2b02f0fc26 | |
| parent | 776403b6429422c7460bee93e617cb8c1cb3f021 (diff) | |
| download | bcm5719-llvm-403256763fc91a616a517a0ba0b128d28b8e166f.tar.gz bcm5719-llvm-403256763fc91a616a517a0ba0b128d28b8e166f.zip | |
Don't duplicate the work done by a gep into a "bitcast" if the gep has
more than one use.
Fixes PR10322.
llvm-svn: 134883
| -rw-r--r-- | llvm/lib/Transforms/InstCombine/InstructionCombining.cpp | 8 | ||||
| -rw-r--r-- | llvm/test/Transforms/InstCombine/getelementptr.ll | 16 |
2 files changed, 24 insertions, 0 deletions
diff --git a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp index 92c10f5546c..278016c6ece 100644 --- a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp +++ b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp @@ -785,6 +785,14 @@ Instruction *InstCombiner::visitGetElementPtrInst(GetElementPtrInst &GEP) { // getelementptr instructions into a single instruction. // if (GEPOperator *Src = dyn_cast<GEPOperator>(PtrOp)) { + + // If this GEP has only 0 indices, it is the same pointer as + // Src. If Src is not a trivial GEP too, don't combine + // the indices. + if (GEP.hasAllZeroIndices() && !Src->hasAllZeroIndices() && + !Src->hasOneUse()) + return 0; + // Note that if our source is a gep chain itself that we wait for that // chain to be resolved before we perform this transformation. This // avoids us creating a TON of code in some cases. diff --git a/llvm/test/Transforms/InstCombine/getelementptr.ll b/llvm/test/Transforms/InstCombine/getelementptr.ll index aa2eba177b1..26c0e47f4b1 100644 --- a/llvm/test/Transforms/InstCombine/getelementptr.ll +++ b/llvm/test/Transforms/InstCombine/getelementptr.ll @@ -456,3 +456,19 @@ define i32* @test38(i32* %I, i32 %n) { ; CHECK: = sext i32 %n to i64 ; CHECK: %A = getelementptr i32* %I, i64 % } + +; Test that we don't duplicate work when the second gep is a "bitcast". +%pr10322_t = type { i8* } +declare void @pr10322_f2(%pr10322_t*) +declare void @pr10322_f3(i8**) +define void @pr10322_f1(%pr10322_t* %foo) { +entry: + %arrayidx8 = getelementptr inbounds %pr10322_t* %foo, i64 2 + call void @pr10322_f2(%pr10322_t* %arrayidx8) nounwind + %tmp2 = getelementptr inbounds %pr10322_t* %arrayidx8, i64 0, i32 0 + call void @pr10322_f3(i8** %tmp2) nounwind + ret void + +; CHECK: @pr10322_f1 +; CHECK: %tmp2 = getelementptr inbounds %pr10322_t* %arrayidx8, i64 0, i32 0 +} |

