Hi chandlerc, rnk, qcolombet,

This is not meant as an actual code review to get this patch submitted but to 
have a basis for further discussion.

It is meant to experiment around a solution for http://llvm.org/PR22230

With this patch, LLVM, generates pretty nice code for the example from the bug 
report (see below). Obviously it is far from complete or correct.

        .section        __TEXT,__text,regular,pure_instructions
        .macosx_version_min 10, 10
        .globl  __Z1fPhP1A
        .align  4, 0x90
__Z1fPhP1A:                             ## @_Z1fPhP1A
        .cfi_startproc
## BB#0:                                ## %entry
        pushq   %rbp
Ltmp0:
        .cfi_def_cfa_offset 16
Ltmp1:
        .cfi_offset %rbp, -16
        movq    %rsp, %rbp
Ltmp2:
        .cfi_def_cfa_register %rbp
        pushq   %r15
        pushq   %r14
        pushq   %rbx
        pushq   %rax
Ltmp3:
        .cfi_offset %rbx, -40
Ltmp4:
        .cfi_offset %r14, -32
Ltmp5:
        .cfi_offset %r15, -24
        movq    %rsi, %r14
        movq    %rdi, %rbx
        incq    %rbx
        leaq    LJTI0_0(%rip), %r15
        jmp     LBB0_1
        .align  4, 0x90
LBB0_6:                                 ## %for.cond.backedge
                                        ##   in Loop: Header=BB0_1 Depth=1
        incq    %rbx
LBB0_1:                                 ## %for.cond
                                        ## =>This Inner Loop Header: Depth=1
        movzbl  (%rbx), %eax
        cmpq    $3, %rax
        ja      LBB0_6
## BB#2:                                ## %for.cond
                                        ##   in Loop: Header=BB0_1 Depth=1
        movslq  (%r15,%rax,4), %rax
        addq    %r15, %rax
        jmpq    *%rax
LBB0_3:                                 ## %if.then
                                        ##   in Loop: Header=BB0_1 Depth=1
        movq    %r14, %rdi
        jmp     LBB0_5
LBB0_4:                                 ## %if.then4
                                        ##   in Loop: Header=BB0_1 Depth=1
        leaq    4(%r14), %rdi
        jmp     LBB0_5
LBB0_7:                                 ## %if.then8
                                        ##   in Loop: Header=BB0_1 Depth=1
        leaq    8(%r14), %rdi
        jmp     LBB0_5
LBB0_8:                                 ## %if.then12
                                        ##   in Loop: Header=BB0_1 Depth=1
        leaq    12(%r14), %rdi
LBB0_5:                                 ## %for.cond.backedge
                                        ##   in Loop: Header=BB0_1 Depth=1
        callq   __Z6assignPj
        jmp     LBB0_6
        .cfi_endproc
        .align  2, 0x90
L0_0_set_3 = LBB0_3-LJTI0_0
L0_0_set_4 = LBB0_4-LJTI0_0
L0_0_set_7 = LBB0_7-LJTI0_0
L0_0_set_8 = LBB0_8-LJTI0_0
LJTI0_0:
        .long   L0_0_set_3
        .long   L0_0_set_4
        .long   L0_0_set_7
        .long   L0_0_set_8


.subsections_via_symbols

http://reviews.llvm.org/D7259

Files:
  lib/CodeGen/CodeGenPrepare.cpp
  lib/CodeGen/RegisterCoalescer.cpp

EMAIL PREFERENCES
  http://reviews.llvm.org/settings/panel/emailpreferences/
Index: lib/CodeGen/CodeGenPrepare.cpp
===================================================================
--- lib/CodeGen/CodeGenPrepare.cpp
+++ lib/CodeGen/CodeGenPrepare.cpp
@@ -1206,102 +1206,6 @@
   CI->eraseFromParent();
 }
 
-bool CodeGenPrepare::OptimizeCallInst(CallInst *CI, bool& ModifiedDT) {
-  BasicBlock *BB = CI->getParent();
-
-  // Lower inline assembly if we can.
-  // If we found an inline asm expession, and if the target knows how to
-  // lower it to normal LLVM code, do so now.
-  if (TLI && isa<InlineAsm>(CI->getCalledValue())) {
-    if (TLI->ExpandInlineAsm(CI)) {
-      // Avoid invalidating the iterator.
-      CurInstIterator = BB->begin();
-      // Avoid processing instructions out of order, which could cause
-      // reuse before a value is defined.
-      SunkAddrs.clear();
-      return true;
-    }
-    // Sink address computing for memory operands into the block.
-    if (OptimizeInlineAsmInst(CI))
-      return true;
-  }
-
-  IntrinsicInst *II = dyn_cast<IntrinsicInst>(CI);
-  if (II) {
-    switch (II->getIntrinsicID()) {
-    default: break;
-    case Intrinsic::objectsize: {
-      // Lower all uses of llvm.objectsize.*
-      bool Min = (cast<ConstantInt>(II->getArgOperand(1))->getZExtValue() == 1);
-      Type *ReturnTy = CI->getType();
-      Constant *RetVal = ConstantInt::get(ReturnTy, Min ? 0 : -1ULL);
-
-      // Substituting this can cause recursive simplifications, which can
-      // invalidate our iterator.  Use a WeakVH to hold onto it in case this
-      // happens.
-      WeakVH IterHandle(CurInstIterator);
-
-      replaceAndRecursivelySimplify(CI, RetVal,
-                                    TLI ? TLI->getDataLayout() : nullptr,
-                                    TLInfo, ModifiedDT ? nullptr : DT);
-
-      // If the iterator instruction was recursively deleted, start over at the
-      // start of the block.
-      if (IterHandle != CurInstIterator) {
-        CurInstIterator = BB->begin();
-        SunkAddrs.clear();
-      }
-      return true;
-    }
-    case Intrinsic::masked_load: {
-      // Scalarize unsupported vector masked load
-      if (!TTI->isLegalMaskedLoad(CI->getType(), 1)) {
-        ScalarizeMaskedLoad(CI);
-        ModifiedDT = true;
-        return true;
-      }
-      return false;
-    }
-    case Intrinsic::masked_store: {
-      if (!TTI->isLegalMaskedStore(CI->getArgOperand(0)->getType(), 1)) {
-        ScalarizeMaskedStore(CI);
-        ModifiedDT = true;
-        return true;
-      }
-      return false;
-    }
-    }
-
-    if (TLI) {
-      SmallVector<Value*, 2> PtrOps;
-      Type *AccessTy;
-      if (TLI->GetAddrModeArguments(II, PtrOps, AccessTy))
-        while (!PtrOps.empty())
-          if (OptimizeMemoryInst(II, PtrOps.pop_back_val(), AccessTy))
-            return true;
-    }
-  }
-
-  // From here on out we're working with named functions.
-  if (!CI->getCalledFunction()) return false;
-
-  // We'll need DataLayout from here on out.
-  const DataLayout *TD = TLI ? TLI->getDataLayout() : nullptr;
-  if (!TD) return false;
-
-  // Lower all default uses of _chk calls.  This is very similar
-  // to what InstCombineCalls does, but here we are only lowering calls
-  // to fortified library functions (e.g. __memcpy_chk) that have the default
-  // "don't know" as the objectsize.  Anything else should be left alone.
-  FortifiedLibCallSimplifier Simplifier(TD, TLInfo, true);
-  if (Value *V = Simplifier.optimizeCall(CI)) {
-    CI->replaceAllUsesWith(V);
-    CI->eraseFromParent();
-    return true;
-  }
-  return false;
-}
-
 /// DupRetToEnableTailCallOpts - Look for opportunities to duplicate return
 /// instructions to the predecessor to enable tail call optimizations. The
 /// case it is currently looking for is:
@@ -3363,6 +3267,144 @@
   return true;
 }
 
+bool CodeGenPrepare::OptimizeCallInst(CallInst *CI, bool& ModifiedDT) {
+  BasicBlock *BB = CI->getParent();
+  Value *Addr = CI->getArgOperand(0);
+  if (IsNonLocalValue(Addr, CI->getParent())) {
+    Value *&SunkAddr = SunkAddrs[Addr];
+    if (SunkAddr) {
+      llvm::errs() << "AAAA\n";
+    } else {
+      Type *IntPtrTy = TLI->getDataLayout()->getIntPtrType(Addr->getType());
+      Value *V = CI->getArgOperand(0);
+      SmallVector<Instruction *, 16> NewAddrModeInsts;
+      TypePromotionTransaction TPT;
+      ExtAddrMode Mode = AddressingModeMatcher::Match(
+          V, V->getType(), CI, NewAddrModeInsts, *TLI, InsertedTruncsSet,
+          PromotedInsts, TPT);
+      // llvm::errs() << Mode.BaseReg << " " << Mode.Scale << " " << Mode.BaseGV
+      //              << " " << Mode.BaseOffs << "\n";
+      IRBuilder<> Builder(CI);
+      Value *Result = nullptr;
+      if (Mode.BaseReg) {
+        Value *V = Mode.BaseReg;
+        if (V->getType()->isPointerTy())
+          V = Builder.CreatePtrToInt(V, IntPtrTy, "sunkaddr");
+        if (V->getType() != IntPtrTy)
+          V = Builder.CreateIntCast(V, IntPtrTy, /*isSigned=*/true, "sunkaddr");
+        Result = V;
+      }
+
+      if (Mode.BaseOffs) {
+        Value *V = ConstantInt::get(IntPtrTy, Mode.BaseOffs);
+        if (Result)
+          Result = Builder.CreateAdd(Result, V, "sunkaddr");
+        else
+          Result = V;
+      }
+      if (!Result)
+        SunkAddr = Constant::getNullValue(Addr->getType());
+      else
+        SunkAddr = Builder.CreateIntToPtr(Result, Addr->getType(), "sunkaddr");
+      CI->replaceUsesOfWith(Addr, SunkAddr);
+    }
+    RecursivelyDeleteTriviallyDeadInstructions(Addr, TLInfo);
+    return true;
+  }
+
+  // Lower inline assembly if we can.
+  // If we found an inline asm expession, and if the target knows how to
+  // lower it to normal LLVM code, do so now.
+  if (TLI && isa<InlineAsm>(CI->getCalledValue())) {
+    if (TLI->ExpandInlineAsm(CI)) {
+      // Avoid invalidating the iterator.
+      CurInstIterator = BB->begin();
+      // Avoid processing instructions out of order, which could cause
+      // reuse before a value is defined.
+      SunkAddrs.clear();
+      return true;
+    }
+    // Sink address computing for memory operands into the block.
+    if (OptimizeInlineAsmInst(CI))
+      return true;
+  }
+
+  IntrinsicInst *II = dyn_cast<IntrinsicInst>(CI);
+  if (II) {
+    switch (II->getIntrinsicID()) {
+    default: break;
+    case Intrinsic::objectsize: {
+      // Lower all uses of llvm.objectsize.*
+      bool Min = (cast<ConstantInt>(II->getArgOperand(1))->getZExtValue() == 1);
+      Type *ReturnTy = CI->getType();
+      Constant *RetVal = ConstantInt::get(ReturnTy, Min ? 0 : -1ULL);
+
+      // Substituting this can cause recursive simplifications, which can
+      // invalidate our iterator.  Use a WeakVH to hold onto it in case this
+      // happens.
+      WeakVH IterHandle(CurInstIterator);
+
+      replaceAndRecursivelySimplify(CI, RetVal,
+                                    TLI ? TLI->getDataLayout() : nullptr,
+                                    TLInfo, ModifiedDT ? nullptr : DT);
+
+      // If the iterator instruction was recursively deleted, start over at the
+      // start of the block.
+      if (IterHandle != CurInstIterator) {
+        CurInstIterator = BB->begin();
+        SunkAddrs.clear();
+      }
+      return true;
+    }
+    case Intrinsic::masked_load: {
+      // Scalarize unsupported vector masked load
+      if (!TTI->isLegalMaskedLoad(CI->getType(), 1)) {
+        ScalarizeMaskedLoad(CI);
+        ModifiedDT = true;
+        return true;
+      }
+      return false;
+    }
+    case Intrinsic::masked_store: {
+      if (!TTI->isLegalMaskedStore(CI->getArgOperand(0)->getType(), 1)) {
+        ScalarizeMaskedStore(CI);
+        ModifiedDT = true;
+        return true;
+      }
+      return false;
+    }
+    }
+
+    if (TLI) {
+      SmallVector<Value*, 2> PtrOps;
+      Type *AccessTy;
+      if (TLI->GetAddrModeArguments(II, PtrOps, AccessTy))
+        while (!PtrOps.empty())
+          if (OptimizeMemoryInst(II, PtrOps.pop_back_val(), AccessTy))
+            return true;
+    }
+  }
+
+  // From here on out we're working with named functions.
+  if (!CI->getCalledFunction()) return false;
+
+  // We'll need DataLayout from here on out.
+  const DataLayout *TD = TLI ? TLI->getDataLayout() : nullptr;
+  if (!TD) return false;
+
+  // Lower all default uses of _chk calls.  This is very similar
+  // to what InstCombineCalls does, but here we are only lowering calls
+  // to fortified library functions (e.g. __memcpy_chk) that have the default
+  // "don't know" as the objectsize.  Anything else should be left alone.
+  FortifiedLibCallSimplifier Simplifier(TD, TLInfo, true);
+  if (Value *V = Simplifier.optimizeCall(CI)) {
+    CI->replaceAllUsesWith(V);
+    CI->eraseFromParent();
+    return true;
+  }
+  return false;
+}
+
 /// OptimizeInlineAsmInst - If there are any memory operands, use
 /// OptimizeMemoryInst to sink their address computing into the block when
 /// possible / profitable.
Index: lib/CodeGen/RegisterCoalescer.cpp
===================================================================
--- lib/CodeGen/RegisterCoalescer.cpp
+++ lib/CodeGen/RegisterCoalescer.cpp
@@ -840,10 +840,14 @@
     IsDefCopy = true;
     return false;
   }
-  if (!TII->isAsCheapAsAMove(DefMI))
-    return false;
-  if (!TII->isTriviallyReMaterializable(DefMI, AA))
-    return false;
+  llvm::errs() << "AAA\n---\n";
+  DefMI->dump();
+  llvm::errs() << TII->isAsCheapAsAMove(DefMI) << " "
+               << TII->isTriviallyReMaterializable(DefMI, AA) << "\n";
+  //if (!TII->isAsCheapAsAMove(DefMI))
+  //  return false;
+  //if (!TII->isTriviallyReMaterializable(DefMI, AA))
+  //  return false;
   bool SawStore = false;
   if (!DefMI->isSafeToMove(TII, AA, SawStore))
     return false;
_______________________________________________
cfe-commits mailing list
[email protected]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits

Reply via email to