<div dir="ltr">Looks like this has broken "check-sanitizer" on Windows:<div>sanitizer_atomic_test.cc <br></div><div><div><br></div><div>1.<span class="" style="white-space:pre">  </span><eof> parser at end of file</div>

<div>2.<span class="" style="white-space:pre">  </span>Per-module optimization passes<br></div><div>3.<span class="" style="white-space:pre"> </span>Running pass 'CallGraph Pass Manager' on module 'sanitizer_atomic_test.cc'.<br>

</div><div>4.<span class="" style="white-space:pre">      </span>Running pass 'Combine redundant instructions' on function '@"\01?do_put@?$num_put@DV?$ostreambuf_iterator@DU?$char_traits@D@std@@@std@@@std@@MBE?AV?$ostreambuf_iterator@DU?$char_traits@D@std@@@2@V32@AAVios_base@2@DO@Z"'<br>

</div><div><br></div></div><div class="gmail_extra"><br><br><div class="gmail_quote">2014-05-28 21:38 GMT+04:00 Louis Gerbarg <span dir="ltr"><<a href="mailto:lgg@apple.com" target="_blank" class="cremed">lgg@apple.com</a>></span>:<br>

<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Author: louis<br>
Date: Wed May 28 12:38:31 2014<br>
New Revision: 209755<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=209755&view=rev" target="_blank" class="cremed">http://llvm.org/viewvc/llvm-project?rev=209755&view=rev</a><br>
Log:<br>
Add support for combining GEPs across PHI nodes<br>
<br>
Currently LLVM will generally merge GEPs. This allows backends to use more<br>
complex addressing modes. In some cases this is not happening because there<br>
is PHI inbetween the two GEPs:<br>
<br>
  GEP1--\<br>
        |-->PHI1-->GEP3<br>
  GEP2--/<br>
<br>
This patch checks to see if GEP1 and GEP2 are similiar enough that they can be<br>
cloned (GEP12) in GEP3's BB, allowing GEP->GEP merging (GEP123):<br>
<br>
  GEP1--\                     --\                           --\<br>
        |-->PHI1-->GEP3  ==>    |-->PHI2->GEP12->GEP3 == >    |-->PHI2->GEP123<br>
  GEP2--/                     --/                           --/<br>
<br>
This also breaks certain use chains that are preventing GEP->GEP merges that the<br>
the existing instcombine would merge otherwise.<br>
<br>
Tests included.<br>
<br>
Added:<br>
    llvm/trunk/test/Transforms/InstCombine/gepphigep.ll<br>
Modified:<br>
    llvm/trunk/lib/Transforms/InstCombine/InstructionCombining.cpp<br>
<br>
Modified: llvm/trunk/lib/Transforms/InstCombine/InstructionCombining.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstructionCombining.cpp?rev=209755&r1=209754&r2=209755&view=diff" target="_blank" class="cremed">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstructionCombining.cpp?rev=209755&r1=209754&r2=209755&view=diff</a><br>


==============================================================================<br>
--- llvm/trunk/lib/Transforms/InstCombine/InstructionCombining.cpp (original)<br>
+++ llvm/trunk/lib/Transforms/InstCombine/InstructionCombining.cpp Wed May 28 12:38:31 2014<br>
@@ -1220,6 +1220,85 @@ Instruction *InstCombiner::visitGetEleme<br>
     if (MadeChange) return &GEP;<br>
   }<br>
<br>
+  // Check to see if the inputs to the PHI node are getelementptr instructions.<br>
+  if (PHINode *PN = dyn_cast<PHINode>(PtrOp)) {<br>
+    GetElementPtrInst *Op1 = dyn_cast<GetElementPtrInst>(PN->getOperand(0));<br>
+    if (!Op1)<br>
+      return nullptr;<br>
+<br>
+    signed DI = -1;<br>
+<br>
+    for (auto I = PN->op_begin()+1, E = PN->op_end(); I !=E; ++I) {<br>
+      GetElementPtrInst *Op2 = dyn_cast<GetElementPtrInst>(*I);<br>
+      if (!Op2 || Op1->getNumOperands() != Op2->getNumOperands())<br>
+        return nullptr;<br>
+<br>
+      for (unsigned J = 0, F = Op1->getNumOperands(); J != F; ++J) {<br>
+        if (Op1->getOperand(J)->getType() != Op2->getOperand(J)->getType())<br>
+          return nullptr;<br>
+<br>
+        if (Op1->getOperand(J) != Op2->getOperand(J)) {<br>
+          if (DI == -1) {<br>
+            // We have not seen any differences yet in the GEPs feeding the<br>
+            // PHI yet, so we record this one if it is allowed to be a<br>
+            // variable.<br>
+<br>
+            // The first two arguments can vary for any GEP, the rest have to be<br>
+            // static for struct slots<br>
+            if (J > 1) {<br>
+              SmallVector<Value*, 8> Idxs(GEP.idx_begin(), GEP.idx_begin()+J);<br>
+              Type *Ty =<br>
+                GetElementPtrInst::getIndexedType(Op1->getOperand(0)->getType(),<br>
+                                                  Idxs);<br>
+              if (Ty->isStructTy())<br>
+                return nullptr;<br>
+            }<br>
+<br>
+            DI = J;<br>
+          } else {<br>
+            // The GEP is different by more than one input. While this could be<br>
+            // extended to support GEPs that vary by more than one variable it<br>
+            // doesn't make sense since it greatly increases the complexity and<br>
+            // would result in an R+R+R addressing mode which no backend<br>
+            // directly supports and would need to be broken into several<br>
+            // simpler instructions anyway.<br>
+            return nullptr;<br>
+          }<br>
+        }<br>
+      }<br>
+    }<br>
+<br>
+    GetElementPtrInst *NewGEP = cast<GetElementPtrInst>(Op1->clone());<br>
+<br>
+    if (DI == -1) {<br>
+      // All the GEPs feeding the PHI are identical. Clone one down into our<br>
+      // BB so that it can be merged with the current GEP.<br>
+      GEP.getParent()->getInstList().insert(GEP.getParent()->getFirstNonPHI(),<br>
+                                            NewGEP);<br>
+    } else {<br>
+      // All the GEPs feeding the PHI differ at a single offset. Clone a GEP<br>
+      // into the current block so it can be merged, and create a new PHI to<br>
+      // set that index.<br>
+      Instruction *InsertPt = Builder->GetInsertPoint();<br>
+      Builder->SetInsertPoint(PN);<br>
+      PHINode *NewPN = Builder->CreatePHI(Op1->getOperand(DI)->getType(),<br>
+                                          PN->getNumOperands());<br>
+      Builder->SetInsertPoint(InsertPt);<br>
+<br>
+      for (auto &I : PN->operands())<br>
+        NewPN->addIncoming(cast<GEPOperator>(I)->getOperand(DI),<br>
+                           PN->getIncomingBlock(I));<br>
+<br>
+      NewGEP->setOperand(DI, NewPN);<br>
+      GEP.getParent()->getInstList().insert(GEP.getParent()->getFirstNonPHI(),<br>
+                                            NewGEP);<br>
+      NewGEP->setOperand(DI, NewPN);<br>
+    }<br>
+<br>
+    GEP.setOperand(0, NewGEP);<br>
+    PtrOp = NewGEP;<br>
+  }<br>
+<br>
   // Combine Indices - If the source pointer to this getelementptr instruction<br>
   // is a getelementptr instruction, combine the indices of the two<br>
   // getelementptr instructions into a single instruction.<br>
<br>
Added: llvm/trunk/test/Transforms/InstCombine/gepphigep.ll<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/gepphigep.ll?rev=209755&view=auto" target="_blank" class="cremed">http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/gepphigep.ll?rev=209755&view=auto</a><br>


==============================================================================<br>
--- llvm/trunk/test/Transforms/InstCombine/gepphigep.ll (added)<br>
+++ llvm/trunk/test/Transforms/InstCombine/gepphigep.ll Wed May 28 12:38:31 2014<br>
@@ -0,0 +1,56 @@<br>
+; RUN: opt -instcombine -S  < %s | FileCheck %s<br>
+<br>
+%struct1 = type { %struct2*, i32, i32, i32 }<br>
+%struct2 = type { i32, i32 }<br>
+<br>
+define i32 @test1(%struct1* %dm, i1 %tmp4, i64 %tmp9, i64 %tmp19) {<br>
+bb:<br>
+  %tmp = getelementptr inbounds %struct1* %dm, i64 0, i32 0<br>
+  %tmp1 = load %struct2** %tmp, align 8<br>
+  br i1 %tmp4, label %bb1, label %bb2<br>
+<br>
+bb1:<br>
+  %tmp10 = getelementptr inbounds %struct2* %tmp1, i64 %tmp9<br>
+  %tmp11 = getelementptr inbounds %struct2* %tmp10, i64 0, i32 0<br>
+  store i32 0, i32* %tmp11, align 4<br>
+  br label %bb3<br>
+<br>
+bb2:<br>
+  %tmp20 = getelementptr inbounds %struct2* %tmp1, i64 %tmp19<br>
+  %tmp21 = getelementptr inbounds %struct2* %tmp20, i64 0, i32 0<br>
+  store i32 0, i32* %tmp21, align 4<br>
+  br label %bb3<br>
+<br>
+bb3:<br>
+  %phi = phi %struct2* [ %tmp10, %bb1 ], [ %tmp20, %bb2 ]<br>
+  %tmp24 = getelementptr inbounds %struct2* %phi, i64 0, i32 1<br>
+  %tmp25 = load i32* %tmp24, align 4<br>
+  ret i32 %tmp25<br>
+<br>
+; CHECK-LABEL: @test1(<br>
+; CHECK: getelementptr inbounds %struct2* %tmp1, i64 %tmp9, i32 0<br>
+; CHECK: getelementptr inbounds %struct2* %tmp1, i64 %tmp19, i32 0<br>
+; CHECK: %[[PHI:[0-9A-Za-z]+]] = phi i64 [ %tmp9, %bb1 ], [ %tmp19, %bb2 ]<br>
+; CHECK: getelementptr inbounds %struct2* %tmp1, i64 %[[PHI]], i32 1<br>
+<br>
+}<br>
+<br>
+define i32 @test2(%struct1* %dm, i1 %tmp4, i64 %tmp9, i64 %tmp19) {<br>
+bb:<br>
+  %tmp = getelementptr inbounds %struct1* %dm, i64 0, i32 0<br>
+  %tmp1 = load %struct2** %tmp, align 8<br>
+  %tmp10 = getelementptr inbounds %struct2* %tmp1, i64 %tmp9<br>
+  %tmp11 = getelementptr inbounds %struct2* %tmp10, i64 0, i32 0<br>
+  store i32 0, i32* %tmp11, align 4<br>
+  %tmp20 = getelementptr inbounds %struct2* %tmp1, i64 %tmp19<br>
+  %tmp21 = getelementptr inbounds %struct2* %tmp20, i64 0, i32 0<br>
+  store i32 0, i32* %tmp21, align 4<br>
+  %tmp24 = getelementptr inbounds %struct2* %tmp10, i64 0, i32 1<br>
+  %tmp25 = load i32* %tmp24, align 4<br>
+  ret i32 %tmp25<br>
+<br>
+; CHECK-LABEL: @test2(<br>
+; CHECK: getelementptr inbounds %struct2* %tmp1, i64 %tmp9, i32 0<br>
+; CHECK: getelementptr inbounds %struct2* %tmp1, i64 %tmp19, i32 0<br>
+; CHECK: getelementptr inbounds %struct2* %tmp1, i64 %tmp9, i32 1<br>
+}<br>
<br>
<br>
_______________________________________________<br>
llvm-commits mailing list<br>
<a href="mailto:llvm-commits@cs.uiuc.edu" class="cremed">llvm-commits@cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits" target="_blank" class="cremed">http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits</a><br>
</blockquote></div><br></div></div>