Refactor code to use new attribute getters on CallSite for NoCapture and ByVal.
authorNick Lewycky <nicholas@mxc.ca>
Sun, 20 Nov 2011 19:09:04 +0000 (19:09 +0000)
committerNick Lewycky <nicholas@mxc.ca>
Sun, 20 Nov 2011 19:09:04 +0000 (19:09 +0000)
Suggested in code review by Eli.

That code in InstCombine looks kinda suspicious.

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

include/llvm/Analysis/CaptureTracking.h
include/llvm/Support/CallSite.h
lib/Analysis/BasicAliasAnalysis.cpp
lib/Analysis/MemoryDependenceAnalysis.cpp
lib/Transforms/InstCombine/InstCombineCalls.cpp
lib/Transforms/Scalar/MemCpyOptimizer.cpp
lib/Transforms/Scalar/ScalarReplAggregates.cpp
lib/Transforms/Utils/InlineFunction.cpp

index 44615649fdab8dc04dfff3566e9e0d48ccb3408c..b033f14cec10a1e6bab5dedb1738493d5fa12154 100644 (file)
@@ -104,7 +104,7 @@ void llvm::PointerMayBeCaptured(const llvm::Value *V, CaptureTracker &Tracker) {
       // (think of self-referential objects).
       CallSite::arg_iterator B = CS.arg_begin(), E = CS.arg_end();
       for (CallSite::arg_iterator A = B; A != E; ++A)
-        if (A->get() == V && !CS.paramHasAttr(A - B + 1, Attribute::NoCapture))
+        if (A->get() == V && !CS.doesNotCapture(A - B))
           // The parameter is not marked 'nocapture' - captured.
           if (Tracker.captured(I))
             return;
index 04b8c4e69c52e6ae933d1fb70bfaf6872f15dbc1..20634ede7644cbb612b401cea7ff927049e86c8c 100644 (file)
@@ -237,6 +237,16 @@ public:
 #undef CALLSITE_DELEGATE_GETTER
 #undef CALLSITE_DELEGATE_SETTER
 
+  /// @brief Determine whether this argument is not captured.
+  bool doesNotCapture(unsigned ArgNo) const {
+    return paramHasAttr(ArgNo + 1, Attribute::NoCapture);
+  }
+
+  /// @brief Determine whether this argument is passed by value.
+  bool isByValArgument(unsigned ArgNo) const {
+    return paramHasAttr(ArgNo + 1, Attribute::ByVal);
+  }
+
   /// hasArgument - Returns true if this CallSite passes the given Value* as an
   /// argument to the called function.
   bool hasArgument(const Value *Arg) const {
index af400ba7e70ed98e5d26c753d509d19702db81d4..568983ae65db3c62b6ad28412c4086ff3d6f6933 100644 (file)
@@ -706,8 +706,7 @@ BasicAliasAnalysis::getModRefInfo(ImmutableCallSite CS,
       // pointer were passed to arguments that were neither of these, then it
       // couldn't be no-capture.
       if (!(*CI)->getType()->isPointerTy() ||
-          (!CS.paramHasAttr(ArgNo+1, Attribute::NoCapture) &&
-           !CS.paramHasAttr(ArgNo+1, Attribute::ByVal)))
+          (!CS.doesNotCapture(ArgNo) && !CS.isByValArgument(ArgNo)))
         continue;
       
       // If this is a no-capture pointer argument, see if we can tell that it
index 323c84f7f6767bd9f2eec5dcc9c7f69932e05f43..e779bf2b998074d9c4410c8f390afe8adc829394 100644 (file)
@@ -393,8 +393,7 @@ MemoryDependenceAnalysis::getModRefInfo(const Instruction *Inst,
     // pointer were passed to arguments that were neither of these, then it
     // couldn't be no-capture.
     if (!(*CI)->getType()->isPointerTy() ||
-        (!CS.paramHasAttr(ArgNo+1, Attribute::NoCapture) &&
-         !CS.paramHasAttr(ArgNo+1, Attribute::ByVal)))
+        (!CS.doesNotCapture(ArgNo) && !CS.isByValArgument(ArgNo)))
       continue;
 
     // If this is a no-capture pointer argument, see if we can tell that it
index c7b3ff8504acb00e5462c581b9fdfdca85a7982f..e8136ab77ff14f3c49e4ffae1e573b03297f8483 100644 (file)
@@ -760,7 +760,7 @@ static bool isSafeToEliminateVarargsCast(const CallSite CS,
   // The size of ByVal arguments is derived from the type, so we
   // can't change to a type with a different size.  If the size were
   // passed explicitly we could avoid this check.
-  if (!CS.paramHasAttr(ix, Attribute::ByVal))
+  if (!CS.isByValArgument(ix))
     return true;
 
   Type* SrcTy = 
@@ -960,7 +960,7 @@ Instruction *InstCombiner::visitCallSite(CallSite CS) {
   PointerType *PTy = cast<PointerType>(Callee->getType());
   FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
   if (FTy->isVarArg()) {
-    int ix = FTy->getNumParams() + (isa<InvokeInst>(Callee) ? 3 : 1);
+    int ix = FTy->getNumParams() + (isa<InvokeInst>(Callee) ? 2 : 0);
     // See if we can optimize any arguments passed through the varargs area of
     // the call.
     for (CallSite::arg_iterator I = CS.arg_begin()+FTy->getNumParams(),
index 298d6927497ca86c7fee6c45909038489cecb334..9e4f51f4549318951eca73fd729efb10795a1427 100644 (file)
@@ -950,7 +950,7 @@ bool MemCpyOpt::iterateOnFunction(Function &F) {
         RepeatInstruction = processMemMove(M);
       else if (CallSite CS = (Value*)I) {
         for (unsigned i = 0, e = CS.arg_size(); i != e; ++i)
-          if (CS.paramHasAttr(i+1, Attribute::ByVal))
+          if (CS.isByValArgument(i))
             MadeChange |= processByValArgument(CS, i);
       }
 
index c12f403d57e177b96c35f7facdd27347a479eec9..4b14efcd063fb81cb2a35a11437696e0ce5c49f7 100644 (file)
@@ -2534,13 +2534,12 @@ isOnlyCopiedFromConstantGlobal(Value *V, MemTransferInst *&TheCopy,
       // ignore it if we know that the value isn't captured.
       unsigned ArgNo = CS.getArgumentNo(UI);
       if (CS.onlyReadsMemory() &&
-          (CS.getInstruction()->use_empty() ||
-           CS.paramHasAttr(ArgNo+1, Attribute::NoCapture)))
+          (CS.getInstruction()->use_empty() || CS.doesNotCapture(ArgNo)))
         continue;
 
       // If this is being passed as a byval argument, the caller is making a
       // copy, so it is only a read of the alloca.
-      if (CS.paramHasAttr(ArgNo+1, Attribute::ByVal))
+      if (CS.isByValArgument(ArgNo))
         continue;
     }
 
index 5464dbc4a8308b7608166037fe7a89e2426c625f..dd4a659b735ab0bbf309e181bdac796f267ddf6c 100644 (file)
@@ -987,7 +987,7 @@ bool llvm::InlineFunction(CallSite CS, InlineFunctionInfo &IFI) {
       // by them explicit.  However, we don't do this if the callee is readonly
       // or readnone, because the copy would be unneeded: the callee doesn't
       // modify the struct.
-      if (CalledFunc->paramHasAttr(ArgNo+1, Attribute::ByVal)) {
+      if (CS.isByValArgument(ArgNo)) {
         ActualArg = HandleByValArgument(ActualArg, TheCall, CalledFunc, IFI,
                                         CalledFunc->getParamAlignment(ArgNo+1));