]> granicus.if.org Git - llvm/commitdiff
[IR] andIRFlags and copyIRFlags needs to handle GEP
authorDavid Majnemer <david.majnemer@gmail.com>
Fri, 15 Jul 2016 05:02:31 +0000 (05:02 +0000)
committerDavid Majnemer <david.majnemer@gmail.com>
Fri, 15 Jul 2016 05:02:31 +0000 (05:02 +0000)
We didn't consider the inbounds flag on GEPs leading to downstream users
introducing UB.

This fixes PR28562.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@275532 91177308-0d34-0410-b5e6-96231b3b80d8

lib/IR/Instruction.cpp
test/Transforms/GVN/pr28562.ll [new file with mode: 0644]

index 6ca5c8c4637e396298b1d50e5a675bb03b7c0625..ed08f85c60b6d8d51cdd0158d4150a5121ffbcee 100644 (file)
@@ -232,6 +232,10 @@ void Instruction::copyIRFlags(const Value *V) {
   if (auto *FP = dyn_cast<FPMathOperator>(V))
     if (isa<FPMathOperator>(this))
       copyFastMathFlags(FP->getFastMathFlags());
+
+  if (auto *SrcGEP = dyn_cast<GetElementPtrInst>(V))
+    if (auto *DestGEP = dyn_cast<GetElementPtrInst>(this))
+      DestGEP->setIsInBounds(SrcGEP->isInBounds() | DestGEP->isInBounds());
 }
 
 void Instruction::andIRFlags(const Value *V) {
@@ -253,6 +257,10 @@ void Instruction::andIRFlags(const Value *V) {
       copyFastMathFlags(FM);
     }
   }
+
+  if (auto *SrcGEP = dyn_cast<GetElementPtrInst>(V))
+    if (auto *DestGEP = dyn_cast<GetElementPtrInst>(this))
+      DestGEP->setIsInBounds(SrcGEP->isInBounds() & DestGEP->isInBounds());
 }
 
 const char *Instruction::getOpcodeName(unsigned OpCode) {
diff --git a/test/Transforms/GVN/pr28562.ll b/test/Transforms/GVN/pr28562.ll
new file mode 100644 (file)
index 0000000..b34be31
--- /dev/null
@@ -0,0 +1,9 @@
+; RUN: opt -S -gvn < %s | FileCheck %s
+define i32* @test1(i32* %a) {
+  %x1 = getelementptr inbounds i32, i32* %a, i32 10
+  %x2 = getelementptr i32, i32* %a, i32 10
+  ret i32* %x2
+; CHECK-LABEL: @test1(
+; CHECK: %[[x:.*]] = getelementptr i32, i32* %a, i32 10
+; CHECK: ret i32* %[[x]]
+}