<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On Thu, Nov 6, 2014 at 9:05 AM, Eli Bendersky <span dir="ltr"><<a href="mailto:eliben@google.com" target="_blank">eliben@google.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Author: eliben<br>
Date: Thu Nov  6 11:05:49 2014<br>
New Revision: 221464<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=221464&view=rev" target="_blank">http://llvm.org/viewvc/llvm-project?rev=221464&view=rev</a><br>
Log:<br>
Clean up NVPTXLowerStructArgs.cpp. NFC<br>
<br>
* Remove unnecessary const_casts and C-style casts<br>
* Simplify attribute access code<br>
* Simplify ArrayRef creation<br>
* 80-col and clang-format<br></blockquote><div><br></div><div>Thanks!</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
<br>
Modified:<br>
    llvm/trunk/lib/Target/NVPTX/NVPTXLowerStructArgs.cpp<br>
<br>
Modified: llvm/trunk/lib/Target/NVPTX/NVPTXLowerStructArgs.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/NVPTX/NVPTXLowerStructArgs.cpp?rev=221464&r1=221463&r2=221464&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/NVPTX/NVPTXLowerStructArgs.cpp?rev=221464&r1=221463&r2=221464&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/lib/Target/NVPTX/NVPTXLowerStructArgs.cpp (original)<br>
+++ llvm/trunk/lib/Target/NVPTX/NVPTXLowerStructArgs.cpp Thu Nov  6 11:05:49 2014<br>
@@ -57,14 +57,12 @@ INITIALIZE_PASS(NVPTXLowerStructArgs, "n<br>
 void NVPTXLowerStructArgs::handleParam(Argument *Arg) {<br>
   Function *Func = Arg->getParent();<br>
   Instruction *FirstInst = &(Func->getEntryBlock().front());<br>
-  const PointerType *PType = dyn_cast<PointerType>(Arg->getType());<br>
+  PointerType *PType = dyn_cast<PointerType>(Arg->getType());<br>
<br>
   assert(PType && "Expecting pointer type in handleParam");<br>
<br>
-  const Type *StructType = PType->getElementType();<br>
-<br>
-  AllocaInst *AllocA =<br>
-    new AllocaInst(const_cast<Type *>(StructType), Arg->getName(), FirstInst);<br>
+  Type *StructType = PType->getElementType();<br>
+  AllocaInst *AllocA = new AllocaInst(StructType, Arg->getName(), FirstInst);<br>
<br>
   /* Set the alignment to alignment of the byval parameter. This is because,<br>
    * later load/stores assume that alignment, and we are going to replace<br>
@@ -75,74 +73,59 @@ void NVPTXLowerStructArgs::handleParam(A<br>
   Arg->replaceAllUsesWith(AllocA);<br>
<br>
   // Get the cvt.gen.to.param intrinsic<br>
-  const Type *CvtTypes[2] = {<br>
-    Type::getInt8PtrTy(Func->getParent()->getContext(), ADDRESS_SPACE_PARAM),<br>
-    Type::getInt8PtrTy(Func->getParent()->getContext(), ADDRESS_SPACE_GENERIC)<br>
-  };<br>
+  Type *CvtTypes[] = {<br>
+      Type::getInt8PtrTy(Func->getParent()->getContext(), ADDRESS_SPACE_PARAM),<br>
+      Type::getInt8PtrTy(Func->getParent()->getContext(),<br>
+                         ADDRESS_SPACE_GENERIC)};<br>
   Function *CvtFunc = Intrinsic::getDeclaration(<br>
-      Func->getParent(), Intrinsic::nvvm_ptr_gen_to_param,<br>
-      ArrayRef<Type *>(const_cast<Type **>(CvtTypes), 2));<br>
-  std::vector<Value *> BC1;<br>
-  BC1.push_back(<br>
+      Func->getParent(), Intrinsic::nvvm_ptr_gen_to_param, CvtTypes);<br>
+<br>
+  Value *BitcastArgs[] = {<br>
       new BitCastInst(Arg, Type::getInt8PtrTy(Func->getParent()->getContext(),<br>
                                               ADDRESS_SPACE_GENERIC),<br>
-                      Arg->getName(), FirstInst));<br>
-  CallInst *CallCVT = CallInst::Create(CvtFunc, ArrayRef<Value *>(BC1),<br>
-                                       "cvt_to_param", FirstInst);<br>
-<br>
-  BitCastInst *BitCast =<br>
-      new BitCastInst(CallCVT, PointerType::get(const_cast<Type *>(StructType),<br>
-                                                ADDRESS_SPACE_PARAM),<br>
-                      Arg->getName(), FirstInst);<br>
+                      Arg->getName(), FirstInst)};<br></blockquote><div><br></div><div>FWIW, like the BitCast below, this could be simplified one step further, since it's an array of a single arg, you can just rely on the implicit conversion from T to ArrayRef<T>:<br><br>  BitCastInst *BitCastArg = new BitCastInst(...);<br>  ...<br>    CallInst::Create(..., BitCastArg, ...);<br><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
+  CallInst *CallCVT =<br>
+      CallInst::Create(CvtFunc, BitcastArgs, "cvt_to_param", FirstInst);<br>
+<br>
+  BitCastInst *BitCast = new BitCastInst(<br>
+      CallCVT, PointerType::get(StructType, ADDRESS_SPACE_PARAM),<br>
+      Arg->getName(), FirstInst);<br>
   LoadInst *LI = new LoadInst(BitCast, Arg->getName(), FirstInst);<br>
   new StoreInst(LI, AllocA, FirstInst);<br>
 }<br>
<br>
-/// =============================================================================<br>
-/// If the function had a struct ptr arg, say foo(%struct.x *byval %d), then<br>
-/// add the following instructions to the first basic block :<br>
-///<br>
-/// %temp = alloca %struct.x, align 8<br>
-/// %tt1 = bitcast %struct.x * %d to i8 *<br>
-/// %tt2 = llvm.nvvm.cvt.gen.to.param %tt2<br>
-/// %tempd = bitcast i8 addrspace(101) * to %struct.x addrspace(101) *<br>
-/// %tv = load %struct.x addrspace(101) * %tempd<br>
-/// store %struct.x %tv, %struct.x * %temp, align 8<br>
-///<br>
-/// The above code allocates some space in the stack and copies the incoming<br>
-/// struct from param space to local space.<br>
-/// Then replace all occurences of %d by %temp.<br>
-/// =============================================================================<br>
+// =============================================================================<br>
+// If the function had a struct ptr arg, say foo(%struct.x *byval %d), then<br>
+// add the following instructions to the first basic block :<br>
+//<br>
+// %temp = alloca %struct.x, align 8<br>
+// %tt1 = bitcast %struct.x * %d to i8 *<br>
+// %tt2 = llvm.nvvm.cvt.gen.to.param %tt2<br>
+// %tempd = bitcast i8 addrspace(101) * to %struct.x addrspace(101) *<br>
+// %tv = load %struct.x addrspace(101) * %tempd<br>
+// store %struct.x %tv, %struct.x * %temp, align 8<br>
+//<br>
+// The above code allocates some space in the stack and copies the incoming<br>
+// struct from param space to local space.<br>
+// Then replace all occurences of %d by %temp.<br>
+// =============================================================================<br>
 void NVPTXLowerStructArgs::handleStructPtrArgs(Function &F) {<br>
-  const AttributeSet &PAL = F.getAttributes();<br>
-<br>
-  unsigned Idx = 1;<br>
-<br>
   for (Argument &Arg : F.args()) {<br>
-    const Type *Ty = Arg.getType();<br>
-<br>
-    const PointerType *PTy = dyn_cast<PointerType>(Ty);<br>
-<br>
-    if (PTy) {<br>
-      if (PAL.hasAttribute(Idx, Attribute::ByVal)) {<br>
-        //  cout << "Has struct ptr args" << std::endl;<br>
-        handleParam(&Arg);<br>
-      }<br>
+    if (Arg.getType()->isPointerTy() && Arg.hasByValAttr()) {<br>
+      handleParam(&Arg);<br>
     }<br></blockquote><div><br></div><div>Usually we leave the {} off single statement blocks, though it's not exactly a hard-and-fast rule.</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
-    Idx++;<br>
   }<br>
 }<br>
<br>
-/// =============================================================================<br>
-/// Main function for this pass.<br>
-/// =============================================================================<br>
+// =============================================================================<br>
+// Main function for this pass.<br>
+// =============================================================================<br>
 bool NVPTXLowerStructArgs::runOnFunction(Function &F) {<br>
   // Skip non-kernels. See the comments at the top of this file.<br>
   if (!isKernelFunction(F))<br>
     return false;<br>
<br>
   handleStructPtrArgs(F);<br>
-<br>
   return true;<br>
 }<br>
<br>
<br>
<br>
_______________________________________________<br>
llvm-commits mailing list<br>
<a href="mailto:llvm-commits@cs.uiuc.edu">llvm-commits@cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits</a><br>
</blockquote></div><br></div></div>